Repository navigation
Conversation
TopK aggregation tracked the ORDER BY column through projections by name. When a projection computed a new value under the aggregate's output name (for example `-max(v) AS "max(v)"`), the rule still applied a limit to the aggregate and kept groups by the original value. Stop following the sort column when the matching projection expression is not a plain column reference, so TopK aggregation is not applied in that case. Plain renames and sibling computed columns still allow it. Closes apache#26691.
Contributor
|
Closing due to
It's impossible to contribute responsibly in such PR volume, please don't do automated contribution in this project |
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.
Important
Fork CI setup: For pull requests from a fork to
main, enable workflows in your fork's Actions tab before you push.See the fork CI setup guide.
Which issue does this PR close?
Rationale for this change
TopKAggregationwalks from aSortExecwithfetchdown to anAggregateExec, renaming the sort column when it passes through aProjectionExec. It only updated the tracked name for plainColumnprojections and ignored every other expression.That skips the important case: a projection that computes a new value and reuses the aggregate's output name, for example
-max(v) AS "max(v)". The rule still finds the aggregate by that name and applieslim, so the aggregate keeps groups by the original max while the sort orders by the negated value. The wrong groups survive.What changes are included in this PR?
In the projection walk, when the expression that produces the tracked sort column is not a
Column, stop applying TopK aggregation for that sort. Plain column renames still update the tracked name. A sibling computed column does not block TopK when the sort key itself is still a column.What is the testing strategy for this PR?
aggregates_topk.sltfor the#26691query (expectsa/-2andd/-3, and an explain plan withoutlimon the aggregate).lim=[2].cargo test -p datafusion-sqllogictest --test sqllogictests -- aggregates_topkAre there any user-facing changes?
Bug fix only. Plans that sort by a computed projection of an aggregate no longer get an incorrect TopK limit on that aggregate. Queries that sort by the aggregate column itself are unchanged.
AI assistance
An AI coding assistant helped with the patch and tests. I reviewed the change against the issue and wrote this description.