Repository navigation
Open
Labels
Milestone
Description
What happens today
compileSelect (src/select/columns.js) records ROW_NUMBER() OVER (...) in query.grouprownums and window aggregates in query.windowaggrs, with the partition as column names. col.over.order is never read. src/select/select.js then makes a second pass over the output rows. It restarts the row number when the partition value differs from the previous output row, and it groups window aggregates by the value in the output row.
const p = [{g: 1, c: 10}, {g: 2, c: 5}, {g: 1, c: 20}, {g: 2, c: 1}];
alasql('SELECT g, c, ROW_NUMBER() OVER (PARTITION BY g ORDER BY c DESC) AS rn FROM ?', [p]);
// rn is 1 on every row SQLite: g=1 c=20 is 1, c=10 is 2; g=2 c=5 is 1, c=1 is 2What to change
- Add a window step that runs after WHERE (and after GROUP BY when there is one) and before the select list is finished. For each distinct window (
PARTITION BYplusORDER BY) it partitions the rows with the partitioning step of Group rows for GROUP BY, grouping sets and DISTINCT in one partitioning step #3168, then sorts each partition with the sort specification and comparator of Keep ORDER BY sort keys beside the rows instead of inside them #3165 and Sort NULL and mixed values in ORDER BY as SQLite, and honour NULLS FIRST and NULLS LAST, with a dialect keeping the old order #3172. - Partition keys, sort keys and arguments are compiled expressions over the source row, not names read from the output row.
ROW_NUMBERnumbers within the sorted partition. Window aggregates use the aggregate objects of Give every aggregate the same init, step and result interface #2660 over the default frame. WithoutORDER BYthe frame is the whole partition. WithORDER BYit runs from the start of the partition to the current row and its peers. Explicit frames (ROWS BETWEEN ...) are not part of this step.- The result of each window function is a value beside the row, read by the select list like any other column.
- Remove
grouprownums,windowaggrsand the second pass insrc/select/select.js. - Add the rule
windowOrderto Rules that differ between dialects become named rules chosen when a statement is compiled #3170 with the valuessql(the new default) andv4. Withv4,ROW_NUMBERignores the window'sORDER BY. Add it toalasql/dialect/v4.
What stays the same
ROW_NUMBER() OVER ()numbers rows as before.- A window aggregate whose partition and argument are selected under their own names, with no
ORDER BY, gives the same value. - Window results that depended on the output order or on output names change.
Done when
- A new test file named after this issue covers the example above,
SUM(v) OVER (PARTITION BY g)overx 1, x 2, y 5withgorvnot selected (SQLite gives 3, 3, 5),SUM(a) OVER (ORDER BY a)as a running total, an expression as argument, two windows in one query, a window over a grouped query, andMAX(d) OVER (...)on dates returning a date. - The dialect test against SQLite gets a window matrix (
ROW_NUMBER,SUM,COUNT,MIN,MAX× with and withoutPARTITION BY× with and withoutORDER BY× column selected or not). The only expected differences are the explicit frames. - The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.
Activity
[-]Review #2208 for code improvements: ROW_NUMBER() OVER (PARTITION BY) numbers the output rows[/-][+]Compute window functions per partition over the source rows, sorted by the window's ORDER BY[/+]