Skip to content

fix: preserve aggregate scope when unparsing#23327

Open
Phoenix500526 wants to merge 1 commit into
apache:mainfrom
Phoenix500526:fix/23317
Open

fix: preserve aggregate scope when unparsing#23327
Phoenix500526 wants to merge 1 commit into
apache:mainfrom
Phoenix500526:fix/23317

Conversation

@Phoenix500526

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

The unparser can fold a Projection into its Aggregate and then keep walking into the Aggregate input. When that input is another Aggregate or an unnamed derived Projection, the generated SQL moved references above the SELECT block that defined them. Optimizer aliases such as group_alias_0 and base-table qualifiers such as c.signup_date were no longer in scope.

Stop crossing those scope boundaries blindly. Render nested aggregate inputs as derived relations, and when an aggregate reads from a derived Projection, rewrite only input-schema columns to the derived output names before rendering SELECT and GROUP BY expressions.

What changes are included in this PR?

Are these changes tested?

Yes

Are there any user-facing changes?

No

The unparser can fold a Projection into its Aggregate and then keep
walking into the Aggregate input. When that input is another Aggregate
or an unnamed derived Projection, the generated SQL moved references
above the SELECT block that defined them. Optimizer aliases such as
`group_alias_0` and base-table qualifiers such as `c.signup_date`
were no longer in scope.

Stop crossing those scope boundaries blindly. Render nested aggregate
inputs as derived relations, and when an aggregate reads from a derived
Projection, rewrite only input-schema columns to the derived output
names before rendering SELECT and GROUP BY expressions.

CLOSES apache#23317
Signed-off-by: Jiawei Zhao <Phoenix500526@163.com>
@Phoenix500526 Phoenix500526 marked this pull request as draft July 5, 2026 13:30
@github-actions github-actions Bot added sql SQL Planner core Core DataFusion crate labels Jul 5, 2026
@Phoenix500526 Phoenix500526 marked this pull request as ready for review July 5, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate sql SQL Planner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL Unparser generates incorrect column references

1 participant