Skip to content

Compute window functions per partition over the source rows, sorted by the window's ORDER BY #2702

Open

Description

@mathiasrw

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 2

What to change

  1. 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 BY plus ORDER 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.
  2. Partition keys, sort keys and arguments are compiled expressions over the source row, not names read from the output row.
  3. ROW_NUMBER numbers 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. Without ORDER BY the frame is the whole partition. With ORDER BY it runs from the start of the partition to the current row and its peers. Explicit frames (ROWS BETWEEN ...) are not part of this step.
  4. The result of each window function is a value beside the row, read by the select list like any other column.
  5. Remove grouprownums, windowaggrs and the second pass in src/select/select.js.
  6. Add the rule windowOrder to Rules that differ between dialects become named rules chosen when a statement is compiled #3170 with the values sql (the new default) and v4. With v4, ROW_NUMBER ignores the window's ORDER BY. Add it to alasql/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) over x 1, x 2, y 5 with g or v not 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, and MAX(d) OVER (...) on dates returning a date.
  • The dialect test against SQLite gets a window matrix (ROW_NUMBER, SUM, COUNT, MIN, MAX × with and without PARTITION BY × with and without ORDER 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.

Fixes

Fixes #2591, #2689.

Depends on

#3165, #2660, #3168, #3170, #3172, #2696

Activity

  1. added this to the v5.0.0 milestone on Nov 26, 2026
  2. changed the title [-]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[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions