Skip to content

fix: skip TopK aggregation when sort column is computed - #26912

Closed
ump45nose wants to merge 1 commit into
apache:mainfrom
ump45nose:fix/26691-topk-agg-computed-projection
Closed

ump45nose wants to merge 1 commit into
apache:mainfrom
ump45nose:fix/26691-topk-agg-computed-projection

Conversation

@ump45nose

Copy link
Copy Markdown
Contributor

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

TopKAggregation walks from a SortExec with fetch down to an AggregateExec, renaming the sort column when it passes through a ProjectionExec. It only updated the tracked name for plain Column projections 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 applies lim, 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?

  • Added a regression case in aggregates_topk.slt for the #26691 query (expects a/-2 and d/-3, and an explain plan without lim on the aggregate).
  • Added a sibling-computed-column explain that still shows lim=[2].
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- aggregates_topk

Are 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.

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.
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Oct 30, 2026
@2010YOUY01

Copy link
Copy Markdown
Contributor

Closing due to

It's impossible to contribute responsibly in such PR volume, please don't do automated contribution in this project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TopK aggregation keeps the wrong groups when a projection computes a new column with the aggregate's name

2 participants