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
Incorrect results: ORDER BY b, a over a subquery with ORDER BY b LIMIT n is not sorted by (b, a) when SortPreservingMergeExec has a fetch #26162
ORDER BY b, a on top of a subquery with ORDER BY b LIMIT n returns rows that are not sorted by (b, a) when the input has multiple partitions.
The sort pushdown optimizer pushes the finer [b, a] requirement below a SortPreservingMergeExec that has a fetch, but the merge itself keeps its original key [b]. The resulting plan has no operator that actually orders the output by (b, a).
Found by @jayzhan211 while reviewing #25408 (see #25408 (comment)). The bug is not introduced by that PR; it reproduces on main (754d3c1) as well. That comment also discusses the root cause and possible fixes.
To Reproduce
Run the following in datafusion-cli:
setdatafusion.execution.target_partitions =4;
CREATETABLEsrc (a INT, b INT) ASVALUES (1, 3), (2, 1), (3, 2), (4, 3), (5, 1), (6, 2), (7, 3), (8, 1);
CREATETABLEtASSELECT a, b FROM src UNION ALLSELECT a +10, b FROM src
UNION ALLSELECT a +20, b FROM src UNION ALLSELECT a +30, b FROM src;
SELECT*FROM (SELECT a, b FROM t ORDER BY b LIMIT10) ORDER BY b, a;
Actual output (rows are not ordered by a within b = 1):
Activity
siddubakka commentedon Oct 9, 2026
take
alamb commentedon Oct 9, 2026
FYI @siddubakka you probably need to wait for #25408 to merge first
siddubakka commentedon Oct 9, 2026