Skip to content

Incorrect ORDER BY results after sort pushdown through custom execution plans #26656

Description

@geoffreyclaude

Describe the bug

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.

This example exercises the generic custom-plan fallback. A built-in ProjectionExec uses a separate, specialized handle_projection_pushdown path.

Expected behavior

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.

Activity

  1. 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
  2. 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
  3. added 4 commits that reference this issue on Oct 23, 2026
    5436945
    42e3370
    5d0adb0
    3d94eb9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions