ump45nose opened a new pull request, #26912:
URL: https://github.com/apache/datafusion/pull/26912

   > [!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](https://datafusion.apache.org/contributor-guide/#pull-requests-from-forks).
   
   ## Which issue does this PR close?
   
   - Closes #26691.
   
   ## 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to