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 ORDER BY results after sort pushdown through custom execution plans #26656
Sort pushdown through a custom execution plan can remove a necessary sort, producing incorrectly ordered results or a plan rejected by SanityCheckPlan.
The generic handle_custom_pushdown fallback maps output sort keys to child columns by position when maintains_input_order() marks a child as order-preserving. But preserving the sequence of rows does not guarantee that transformed output values have the same ordering. The fallback does not check that the custom operator's output properties satisfy the requested ordering after sorting its children.
To Reproduce
Construct a custom ExecutionPlan wrapping a ProjectionExec that evaluates -a AS a. The custom node returns vec![true] from maintains_input_order(), delegates its properties to the projection, and recomputes those properties when replacing its child.
With a single input partition containing a = [2, 1, 3], place an ascending SortExec above it (schematic plan):
SortExec: a ASC
CustomProjectionExec: -a AS a
Input: a = [2, 1, 3]
The fallback pushes the sort below the custom operator:
CustomProjectionExec: -a AS a
SortExec: a ASC
Input: a = [2, 1, 3]
Sorting the original input produces [1, 2, 3]; negating it then produces [-1, -2, -3], which is descending. The requested result is [-3, -2, -1]. In this single-partition example, SanityCheckPlan does not reject the plan, so the query returns incorrectly ordered rows.
Keep the sort above the custom operator unless its output properties prove that sorting its children satisfies the requested output ordering.
Additional context
Fix and regression coverage: #26118. A fixed custom projection negates a single column, and the SQL regression checks its output order and uses EXPLAIN to show the retained sort above the custom operator.
Willingness to contribute
I plan to contribute a fix for this bug independently.
changed the title [-]Sort pushdown through a custom execution plan[/-][+]Sort pushdown through custom execution plans can remove a required sort[/+]on Oct 23, 2026
changed the title [-]Sort pushdown through custom execution plans can remove a required sort[/-][+]Incorrect ORDER BY results after sort pushdown through custom execution plans[/+]on Oct 23, 2026
Activity
[-]Sort pushdown through a custom execution plan[/-][+]Sort pushdown through custom execution plans can remove a required sort[/+][-]Sort pushdown through custom execution plans can remove a required sort[/-][+]Incorrect ORDER BY results after sort pushdown through custom execution plans[/+]fix: preserve required sorts above custom execution plans
fix: preserve required sorts above custom execution plans
fix: preserve required sorts above custom execution plans
fix: preserve required sorts above custom execution plans