[QECO-1569] Preserve projected constants in aggregate arguments - #24005
Draft
Adam-Alani wants to merge 1 commit into
Draft
[QECO-1569] Preserve projected constants in aggregate arguments#24005Adam-Alani wants to merge 1 commit into
Adam-Alani wants to merge 1 commit into
Conversation
Preserve column-free constants through projection and alias boundaries so aggregate arguments can be simplified without accepting row-varying columns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Aggregate arguments that are constant can lose that property when a subquery or CTE projects them as columns.
approx_percentile_contthen rejects a projected literal because its accumulator cannot evaluate the physical column without input, even though the defining expression is query-global and constant.What
This preserves column-free, non-volatile aggregate arguments through projection and subquery-alias boundaries so normal expression simplification can evaluate them before physical planning. Row-varying columns remain unchanged and continue to be rejected by percentile validation.
How
The simplify-expressions rule collects safe constant definitions from projection chains, remaps them through subquery aliases, and substitutes only matching aggregate-expression columns while preserving output names. Scalar subqueries, volatile expressions, and expressions with column dependencies are excluded.
Validated with:
cargo test --test sqllogictests -p datafusion-sqllogictest -- aggregate.sltcargo test -p datafusion-optimizer simplify_expressions::simplify_exprscargo clippy -p datafusion-optimizer --all-features -- -D warningscargo fmt --all -- --checkCompanion to #23997; the PRs are independent and can merge in either order.