Skip to content

Simplify WindowTopN by moving it before EnforceSorting #21594

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

Activity

  1. SubhamSinghal commented on Apr 13, 2026

    @SubhamSinghal
    ContributorAuthor

    take

  2. saadtajwar commented on Aug 3, 2026

    @saadtajwar
    Contributor

    Hey folks - was wondering if this is still open/if @SubhamSinghal you're working on this currently? Happy to help out if needed! Thanks!

  3. SubhamSinghal commented on Aug 4, 2026

    @SubhamSinghal
    ContributorAuthor

    Feel free to take it.

  4. saadtajwar commented on Aug 4, 2026

    @saadtajwar
    Contributor

    Sweet, thanks! Will dive into this over the next couple of days!

  5. saadtajwar commented on Aug 4, 2026

    @saadtajwar
    Contributor

    take

  6. saadtajwar commented on Aug 8, 2026

    @saadtajwar
    Contributor

    #24191 PR ready for review! Thanks!

  7. added a commit that references this issue on Aug 11, 2026
    0ef1aaa
  8. added a commit that references this issue on Aug 12, 2026
    a218140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions