Subodh-17 opened a new pull request, #27767:
URL: https://github.com/apache/datafusion/pull/27767

   ## Which issue does this PR close?
   
   - Closes #27703.
   
   ## Rationale for this change
   
   When an equijoin key contains an expression over a struct field on an 
aliased table (for example `SELECT * FROM a x JOIN b ON x.s['f'] + 1 = b.f`), 
`ExtractLeafExpressions` replaces the struct field lookup with an unqualified 
extracted column (`__datafusion_extracted_1`) and `PushDownLeafProjections` 
pushes that extraction projection below `SubqueryAlias: x`. As a result, 
`Join.left.schema()` exposes `x.__datafusion_extracted_1` while `Join.on` still 
holds `Column("__datafusion_extracted_1") + Int64(1)`.
   
   During physical planning, `wrap_projection_for_join_if_necessary` constructs 
the wrapper `Projection` via `LogicalPlanBuilder::from(input).project(...)`, 
which normalizes `Column("__datafusion_extracted_1")` to 
`x.__datafusion_extracted_1` (producing field `"x.__datafusion_extracted_1 + 
Int64(1)"`), whereas `join_on` was derived from the unnormalized expressions 
(`"__datafusion_extracted_1 + Int64(1)"`), failing with a schema mismatch.
   
   ## What changes are included in this PR?
   
   - Inside `wrap_projection_for_join_if_necessary` 
(`datafusion/expr/src/logical_plan/builder.rs`), normalize and columnize 
`alias_join_keys` via `ColumnNormalizer::new(&input)` and 
`Columnizer::new(&input)` when `need_project` is `true`, before deduplicating 
`join_key_items`, building the wrapper `Projection`, and deriving `join_on`.
   - Keep the bare-column `need_project == false` path unchanged and preserve 
`#4478` `Cast`/`TryCast` explicit aliasing.
   - Add unit test 
`wrap_projection_for_join_if_necessary_normalizes_aliased_keys` in 
`datafusion/expr/src/logical_plan/builder.rs` covering single aliased keys, 
deduplication of equivalent qualified/unqualified keys, and mixed column + 
`Cast` keys.
   - Add end-to-end SQL regression cases in 
`datafusion/sqllogictest/test_files/struct.slt` covering the reported query, 
both join sides aliased with struct-field expressions, and a derived subquery 
alias `LEFT JOIN` with `CAST`.
   
   ## What is the testing strategy for this PR?
   
   **Pre-fix ablation on upstream `main` (`4c7a78b`):**
   - `cargo test -p datafusion-expr --lib 
wrap_projection_for_join_if_necessary_normalizes_aliased_keys`: failed as 
expected (`"__datafusion_extracted_1 + Int64(1)"` vs 
`"x.__datafusion_extracted_1 + Int64(1)"`).
   - `cargo test -p datafusion-sqllogictest --test sqllogictests -- struct`: 
failed as expected with 3 `Schema error` failures at the 3 new `#27703` SQL 
queries.
   
   **Post-fix validation on `4c7a78b`:**
   - `cargo fmt --all -- --check`: passed
   - `cargo clippy -p datafusion-expr --all-targets -- -D warnings`: passed (0 
warnings)
   - `cargo test -p datafusion-expr --lib`: 310 passed, 0 failed
   - `cargo test -p datafusion-optimizer --lib`: 983 passed, 0 failed
   - `cargo test -p datafusion --lib physical_planner`: 57 passed, 0 failed 
(435 filtered out; run with `ARROW_TEST_DATA` and `PARQUET_TEST_DATA`)
   - `cargo test -p datafusion-sqllogictest --test sqllogictests -- struct`: 
3/3 files passed
   - `cargo test -p datafusion-sqllogictest --test sqllogictests -- joins`: 4/4 
files passed
   
   ## Are there any user-facing changes?
   
   Equijoin queries whose join keys combine expressions with struct-field 
access (or other extracted leaf expressions) on aliased tables now plan and 
execute cleanly instead of failing with a schema mismatch. No public 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