You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wrong results: ORDER BY ... NULLS LAST on a running MAX window function puts the NULL row first #27267
A query that computes MAX(x) OVER (ORDER BY ts) and then sorts on that column with ASC NULLS LAST returns rows in the wrong order when x contains NULLs. The NULL row is returned first, where the query asked for it last.
The same wrong order appears for:
MIN(x) OVER (ORDER BY ts) sorted DESC NULLS LAST
MAX(x) OVER (ORDER BY ts ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) sorted DESC NULLS FIRST
To Reproduce
setdatafusion.execution.target_partitions =1;
CREATETABLEt(ts INT, x INT) ASVALUES (1, NULL), (2, 3), (3, 1), (4, 5);
SELECT ts, m
FROM (
SELECT ts, MAX(x) OVER (ORDER BY ts) AS m
FROM (SELECT*FROM t ORDER BY ts)
)
ORDER BY m ASC NULLS LAST;
EXPLAIN shows the final sort on m is removed, so the planner believes the window output already satisfies m ASC NULLS LAST. Reproduced on main at 94486b1.
Found by @jayzhan211 while reviewing the EquivalenceProperties docs in #27250. The original report and repro are in this review comment: #27250 (comment)
I reproduced all three cases and tried a fix that keeps the sort when the window result can be NULL. That fixes the wrong row order, but it also adds a sort to an existing test for a running SUM over a NOT NULL column.
Is that trade-off okay for this fix, or would you prefer that I keep the optimization for cases where we can prove the result won’t be NULL?
Activity
Surajsuthar commentedon Nov 9, 2026
take
Make window aggregates claim no output order (#354) (#391)
Surajsuthar commentedon Nov 10, 2026
untake
itang06 commentedon Nov 11, 2026
take
itang06 commentedon Nov 11, 2026
I reproduced all three cases and tried a fix that keeps the sort when the window result can be NULL. That fixes the wrong row order, but it also adds a sort to an existing test for a running
SUMover aNOT NULLcolumn.Is that trade-off okay for this fix, or would you prefer that I keep the optimization for cases where we can prove the result won’t be NULL?