Sriram-PR opened a new pull request, #26702:
URL: https://github.com/apache/datafusion/pull/26702

   ## Which issue does this PR close?
   
   - Closes #26621.
   
   ## 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 `Column`s (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.
   


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