Skip to content

ROW_NUMBER() OVER (ORDER BY …) ignores the window's ORDER BY; PARTITION BY works only on already-sorted output #2591

Open

Description

@mathiasrw

ROW_NUMBER() OVER (...) numbers the rows in the order the final result comes out. The ORDER BY inside OVER (...) is read by the parser and then ignored. PARTITION BY only works when the result already arrives sorted by the partition column, and only when that column is in the select list.

Reproduction

v4.20.0, compared with SQLite 3.43.

alasql('CREATE TABLE r (c INT)');
alasql('INSERT INTO r VALUES (10), (20), (30)');
alasql('SELECT c, ROW_NUMBER() OVER (ORDER BY c DESC) AS rn FROM r');
alasql SQLite
c = 30 rn = 3 rn = 1
c = 20 rn = 2 rn = 2
c = 10 rn = 1 rn = 3

The numbers only come out right when the outer query has the same ORDER BY (... FROM r ORDER BY c DESC).

With a partition:

alasql('CREATE TABLE p (g INT, c INT)');
alasql('INSERT INTO p VALUES (1,10), (2,5), (1,20), (2,1)');
Query alasql SQLite
SELECT g, c, ROW_NUMBER() OVER (PARTITION BY g ORDER BY c DESC) AS rn FROM p rn is 1 on every row: the partition value changes on every row, so the count restarts each time g=1: c=20 → 1, c=10 → 2; g=2: c=5 → 1, c=1 → 2
the same, ... FROM p ORDER BY g g=1: c=10 → 1, c=20 → 2 (wrong order inside the partition) as above
the same, ... FROM p ORDER BY g, c DESC correct as above
SELECT c, ROW_NUMBER() OVER (PARTITION BY g ORDER BY c DESC) AS rn FROM p (g not selected) 1, 2, 3, 4: no partition at all numbered per g

All of these return a result without an error.

Where in the code

  • src/424select.js, around line 552. For ROW_NUMBER / ROWNUM it records only the alias (query.rownums), or the alias and the partition column names (query.grouprownums). col.over.order is never read.
  • src/40select.js, around line 415. After the result is built and sorted, it numbers res in output order. For a partition, it restarts the count whenever the partition value differs from the previous row's value. It reads the partition value from the output row (res[i][col]), so a column that is not selected reads as undefined on every row.
  • stdlib.ROW_NUMBER in src/55functions.js returns the constant '1'. The real number is written in afterwards.

Direction

A window function needs its own sort: sort by (partition, window order), number, then apply the outer ORDER BY. It also needs the partition and order values from the source row, not from the output row. The same machinery is missing for aggregates with OVER (ORDER BY ...) (#156) and for LEAD/LAG/FIRST_VALUE/LAST_VALUE (#2409). So this is better solved once for all window functions than patched for ROW_NUMBER alone.

Until then, a query with an ORDER BY inside OVER that the engine cannot honour could throw instead of returning numbers in another order. That is a change in error behaviour, so it is a decision for the breaking list.

Related: #1126 (per-group row numbers, closed by the partition code above), #156, #2408, #2409.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions