Skip to content

fix: keep Final aggregate expressions in sync with a rebuilt Partial - #27827

Open
CuteChuanChuan wants to merge 1 commit into
apache:mainfrom
CuteChuanChuan:issue-27458/sync-final-aggr-exprs
Open

CuteChuanChuan wants to merge 1 commit into
apache:mainfrom
CuteChuanChuan:issue-27458/sync-final-aggr-exprs

Conversation

@CuteChuanChuan

@CuteChuanChuan CuteChuanChuan commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

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.

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.

Are there any user-facing changes?

No.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Nov 28, 2026
@CuteChuanChuan

Copy link
Copy Markdown
Contributor Author

Hi @jayzhan211 ,
could you PTAL when you have a chance? Thanks! 🙏

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

Labels

physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nth_value(x, n ORDER BY ...) returns the wrong row when the join below the aggregate is swapped

1 participant