fix: preserve window inputs during TopK aggregation - #25066
Draft
discord9 wants to merge 1 commit into
Draft
Conversation
Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #25066 +/- ##
==========================================
- Coverage 81.74% 81.74% -0.01%
==========================================
Files 1128 1128
Lines 416644 416650 +6
Branches 416644 416650 +6
==========================================
- Hits 340585 340581 -4
- Misses 55999 56009 +10
Partials 20060 20060 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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?
No linked issue. This is an independently reproduced correctness fix.
Rationale for this change
TopK aggregation can discard groups that are needed to compute window values. With two groups
g = 1, 2, the default SQL pipeline returns(1, 1)instead of(1, 2)for:The window preserves the number of input rows, but its output values can depend on groups that an ancestor TopK would discard. Cardinality preservation alone does not make this propagation valid.
What changes are included in this PR?
Stop this ancestor TopK's aggregate-limit propagation at
WindowAggExecandBoundedWindowAggExec, using the existing traversal flag. Keep aggregate/projection handling and independent optimization of eligible sorts within window inputs unchanged.This change is independent of #23800 and #25065 and does not modify their limit-pushdown or distribution-enforcement paths.
What is the testing strategy for this PR?
Added SQLLogicTest coverage in
aggregates_topk.sltfor:COUNT(*) OVER (), with TopK aggregation enabled and disabled;Both window guards were independently removed during regression verification: each corresponding enabled query changed from
(1, 2)to(1, 1). Both guards were restored, and the full test file passed.Verified locally:
cargo check -p datafusion-physical-optimizercargo test -p datafusion-sqllogictest --test sqllogictests -- aggregates_topkcargo fmt --all --checkcargo clippy --workspace --all-targets --all-features -- -D warningsThe full workspace runtime test suite was not run.
Are there any user-facing changes?
Correct window results for affected grouped TopK queries. No public API or configuration changes. This conservatively prevents an invalid optimization across windows rather than changing their execution semantics.