A window function with an aggregate in its ORDER BY is accepted in the ORDER BY of a non-aggregate query
In a query without aggregates, an ORDER BY term that is a window function with an aggregate inside the window's ORDER BY is an error in SQLite. turso accepts it and returns one row.
SELECT 1 ORDER BY row_number() OVER (ORDER BY sum(1));
-- SQLite: misuse of aggregate: sum() turso: 1
CREATE TABLE t(a INTEGER);
INSERT INTO t VALUES (1), (2);
SELECT a FROM t ORDER BY row_number() OVER (ORDER BY sum(a));
-- SQLite: misuse of aggregate: sum() turso: one row, 1The aggregate turns the query into a one-row aggregate query in turso, so the second statement loses a row. Both engines accept SELECT sum(1) ORDER BY row_number() OVER (ORDER BY sum(1)) and SELECT row_number() OVER (ORDER BY sum(1)), where the query is an aggregate query.
Found by differential testing of turso dc5fda12 against SQLite 3.53.2 with the FrankenSQLite harness (statements derived from crates/fsqlite-core/src/connection.rs at commit d0edcc10 of Dicklesworthstone/frankensqlite).
Permalink: https://github.com/Dicklesworthstone/frankensqlite/blob/d0edcc10f3ef59b0d62b9a277eb7c8ca57e1c2ad/crates/fsqlite-core/src/connection.rs#L156308
Source: tursodatabase/turso