refactor: moving WindowTopN before EnsureRequirements - #24191
Merged
alamb merged 8 commits intoAug 11, 2026
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #24191 +/- ##
==========================================
- Coverage 80.98% 80.98% -0.01%
==========================================
Files 1106 1106
Lines 383495 383498 +3
Branches 383495 383498 +3
==========================================
Hits 310590 310590
+ Misses 54572 54571 -1
- Partials 18333 18337 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Author
|
@SubhamSinghal & @2010YOUY01 - ready for review if you two have the time! Thanks so much in advance! |
jayzhan211
approved these changes
Aug 9, 2026
Contributor
Author
|
Thank you @jayzhan211 for the review! Please let me know if there's anything else needed on my end to get this in :) |
kosiew
pushed a commit
to kosiew/datafusion
that referenced
this pull request
Aug 12, 2026
## Which issue does this PR close? - Closes apache#21594 ## Rationale for this change The WindowTopN physical optimizer rule currently runs after EnsureRequirements, which means it must pattern-match through SortExec nodes that EnsureRequirements inserts: ``` FilterExec(rn <= K) [optional ProjectionExec] BoundedWindowAggExec(ROW_NUMBER PARTITION BY ... ORDER BY ...) SortExec(partition_keys, order_keys) ← inserted by EnforceSorting ``` By moving WindowTopN to run before EnsureRequirements, we can simplify the logic to avoid pattern-matching through `SortExec` node and instead use the `ORDER BY` and `PARTITION BY` on the `BoundedWindowAggExec` ## What changes are included in this PR? Moved `WindowTopN` to run before `EnsureRequirements`, and updated its logic to not expect a `SortExec` and instead use the `ORDER BY` and `PARTITION BY` on the `BoundedWindowAggExec` ## Are these changes tested? Yes ## Are there any user-facing changes? These are just optimizer changes (order of optimizations and internal logic of `WindowTopN`), no API changes
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
The WindowTopN physical optimizer rule currently runs after EnsureRequirements, which means it must pattern-match through SortExec nodes that EnsureRequirements inserts:
By moving WindowTopN to run before EnsureRequirements, we can simplify the logic to avoid pattern-matching through
SortExecnode and instead use theORDER BYandPARTITION BYon theBoundedWindowAggExecWhat changes are included in this PR?
Moved
WindowTopNto run beforeEnsureRequirements, and updated its logic to not expect aSortExecand instead use theORDER BYandPARTITION BYon theBoundedWindowAggExecAre these changes tested?
Yes
Are there any user-facing changes?
These are just optimizer changes (order of optimizations and internal logic of
WindowTopN), no API changes