mattsu2020 opened a new pull request, #27945:
URL: https://github.com/apache/datafusion/pull/27945

   ## Which issue does this PR close?
   
   Closes #27944.
   
   > **Note:** This Draft PR is stacked on top of #26969 and will be rebased 
onto `main` once #26969 merges.
   
   ## Rationale for this change
   
   Follow-up split out from #26969. When user columns or aliases use the 
reserved `__datafusion_extracted` prefix, `ExtractLeafExpressions` and 
`PushDownLeafProjections` can encounter name collisions:
   
   1. **Allocation collisions / wraparound**: Existing 
`__datafusion_extracted*` names near `usize::MAX` (or standalone 
`PushDownLeafProjections` runs without `ExtractLeafExpressions`) can cause 
`AliasGenerator` to emit names that collide with existing columns or aliases.
   2. **Subtree shadowing / destination branch collisions**: Carrying a 
`__datafusion_extracted*` alias down through a projection when that name 
already exists in `proj.input`'s subtree (e.g., shadowing an input column or 
colliding with a column in another `UNION ALL` branch) causes 
`AmbiguousReference` / duplicate field errors.
   
   Instead of scattering per-node destination checks across merge/join/union 
sites (which previously duplicated merge-builder logic and regressed `LEFT 
SEMI` leaf pushdown), this PR handles collisions in a single place.
   
   ## What changes are included in this PR?
   
   - **Collect plan/subtree reserved `__datafusion_extracted*` names 
(`collect_extracted_names` & `advance_generator_past_existing`)**: Scan schema 
fields and alias expressions (including subqueries) to build a reserved-name 
set and seed `AliasGenerator` in both `ExtractLeafExpressions` and 
`PushDownLeafProjections`.
   - **Skip reserved names on allocation (`next_available_alias`)**: Probe for 
the next alias not in `reserved_names` (bounded by `reserved_names.len() + 1` 
so wraparound never loops or collides), and skip extraction without mutating 
`columns_needed` if no free name is found.
   - **Single-place collision check in `split_and_push_projection`**: When a 
projection contains a pushable `__datafusion_extracted*` alias whose name 
already exists anywhere in `proj.input`'s subtree, unwrap and route it through 
`routing_extract` so it is extracted under a fresh plan-wide unique name and 
restored to its original alias in `recovery_exprs`. Optimizer-generated aliases 
from earlier passes do not exist in `proj.input`'s subtree, so they continue to 
be carried down directly without blocking ordinary pushdowns (including `LEFT 
SEMI` joins).
   
   ## What is the testing strategy for this PR?
   
   - **`datafusion/sqllogictest/test_files/projection_pushdown.slt`**:
     - `review_alias_collision`: direct query and nested subquery where an 
extracted struct field aliases to an existing input column name 
(`__datafusion_extracted_manual`).
     - `review_union_left` / `review_union_right`: nested `UNION ALL` where an 
extraction alias collides with a column name present only in the second branch.
   - **Rust unit tests in 
`datafusion/optimizer/src/extract_leaf_expressions.rs`**:
     - `test_existing_max_and_zero_aliases_avoid_schema_ambiguity`: verifies 
wraparound allocation skips existing `_MAX` and `_0` aliases.
     - `test_standalone_pushdown_skips_existing_extracted_aliases`: verifies 
standalone `PushDownLeafProjections` skips existing `__datafusion_extracted*` 
aliases.
     - `test_left_semi_join_leaf_pushdown_preserved`: verifies ordinary `LEFT 
SEMI` join leaf pushdown is not blocked.
   - **Local verification**: `cargo fmt --all -- --check`, `cargo clippy -p 
datafusion-optimizer --all-targets --all-features --no-deps -- -D warnings`, 
`cargo test -p datafusion-optimizer extract_leaf_expressions`, and `cargo test 
--test sqllogictests -- projection_pushdown` passed.
   
   ## Are there any user-facing changes?
   
   Queries with user-defined columns or aliases sharing the 
`__datafusion_extracted` prefix no longer fail with `AmbiguousReference` / 
duplicate field errors during leaf expression extraction and pushdown. Public 
Rust APIs and protobuf formats are unchanged.
   
   This PR was prepared with AI assistance.
   


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