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

   ## Which issue does this PR close?
   
   - Closes #27458.
   
   ## Rationale for this change
   
   `nth_value(x, n ORDER BY ...)` with `n >= 2` returns the wrong row when the 
optimizer swaps the join below the aggregate. The rebuilt `Partial` reverses 
the expression to match the new input ordering, but the `Final` keeps the 
expressions it copied at planning time and reads the reversed state in the 
wrong direction.
   
   ## What changes are included in this PR?
   
   - When an `AggregateExec` that merges partial state (`Final`, 
`FinalPartitioned`, `PartialReduce`) is rebuilt with `Recompute`, it walks down 
single-child nodes to its `Partial` and adopts that `Partial`'s aggregate 
expressions. They are adopted only if the count and names match. Otherwise it 
keeps its own expressions, which is the current behavior.
   - `reorder_aggregate_keys` also builds a `Final` directly and needs no 
change. It only reorders group keys, and the reversal decision treats 
`group_by` as a set (`get_aggregate_expr_req`), so the rebuilt `Partial` makes 
the same choice.
   
   ## What is the testing strategy for this PR?
   
   - Added the issue's reproducer to `aggregate.slt`. Query 1 returned `a` 
before this change and returns `b` now.
   - Ran the extended test suite locally and it passes.
   - No benchmark: the change only runs at planning time, when the plan is 
rebuilt.
   
   ## Are there any user-facing changes?
   
   No.
   


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