Skip to content

Simplify WindowTopN by moving it before EnforceSorting #21594

@SubhamSinghal

Description

@SubhamSinghal

Is your feature request related to a problem or challenge?

Discussion thread

The WindowTopN physical optimizer rule currently runs after EnforceSorting, which means it must pattern-match through SortExec nodes that EnforceSorting inserts:

FilterExec(rn <= K)                                                                                                                                                               
  [optional ProjectionExec]                                                                                                                                                       
    BoundedWindowAggExec(ROW_NUMBER PARTITION BY ... ORDER BY ...)
      SortExec(partition_keys, order_keys)   ← inserted by EnforceSorting

Describe the solution you'd like

Move WindowTopN to run before EnforceSorting. Before EnforceSorting runs, the plan is simpler — BoundedWindowAggExec declares its required ordering/distribution but no SortExec exists yet:

FilterExec(rn <= K)
  [optional ProjectionExec]
    BoundedWindowAggExec(ROW_NUMBER PARTITION BY ... ORDER BY ...)
      <input without SortExec>

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions