Skip to content

fix: keep the qualified column in the CSE projection when a CAST group key shares its name - #26702

Open
Sriram-PR wants to merge 1 commit into
apache:mainfrom
Sriram-PR:fix/cse-qualified-field-dedupe
Open

Sriram-PR wants to merge 1 commit into
apache:mainfrom
Sriram-PR:fix/cse-qualified-field-dedupe

Conversation

@Sriram-PR

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

SELECT a, a + 1, (a + 1) * 2 FROM t GROUP BY CAST(a AS VARCHAR), a fails to plan with Optimizer rule 'optimize_projections' failed ... No field named t.a, and a close variant (COALESCE(min('x'), CAST(a AS VARCHAR)) in the select list) panics in optimize_projections about half the time. Without the CAST group key the same query works.

What changes are included in this PR?

The aggregate outputs an unqualified field named t.a (the CAST key) next to the qualified t.a. build_common_expr_project_plan in common_subexpr_eliminate.rs deduplicated the input fields by their flat name, qualified_name(qualifier, name), which is "t.a" for both, so the qualified column was left out of the intermediate projection while the outer projection still referenced it.

The dedup set now holds Columns (qualifier and name), so the two fields stay distinct. The common expression aliases go in as unqualified columns, which still keeps an input field with the same unqualified name from being projected twice.

What is the testing strategy for this PR?

Added both queries from the issue to cse.slt. Without the change the new cases fail with the FieldNotFound panic from the issue; with it they return 1 2 4 and 1 2 x.

Ran locally:

  • cargo test -p datafusion-sqllogictest --test sqllogictests: all 540 files pass
  • cargo test -p datafusion-optimizer: 987 passed
  • cargo fmt --all -- --check, cargo clippy -p datafusion-optimizer --all-targets --all-features -- -D warnings

Are there any user-facing changes?

Queries like the ones above now plan and return results instead of erroring. No API changes.

…p key shares its name

build_common_expr_project_plan deduplicated input fields by their flat
qualified name, so the unqualified `t.a` field produced by a
`CAST(a AS VARCHAR)` group key hid the qualified `t.a` column and
optimize_projections failed with "No field named t.a" (or panicked).
Deduplicate by Column instead.

Closes apache#26621
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GROUP BY CAST(a AS VARCHAR), a with a common subexpression fails in optimize_projections ("No field named t.a", sometimes a panic)

1 participant