Skip to content

fix: avoid __datafusion_extracted alias collisions in leaf extraction - #27945

Draft
mattsu2020 wants to merge 3 commits into
apache:mainfrom
mattsu2020:fix/leaf-extraction-alias-collisions
Draft

mattsu2020 wants to merge 3 commits into
apache:mainfrom
mattsu2020:fix/leaf-extraction-alias-collisions

Conversation

@mattsu2020

Copy link
Copy Markdown
Contributor

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.

Require extracted alias suffixes parsed by `advance_generator_past_existing`
to be canonical positive decimal integers in `1..usize::MAX` without leading
zeros or signs. This prevents invalid suffixes (`_0`, `_01`, `_+1`, etc.)
from advancing the counter and prevents `usize::MAX` from overflowing when
`update_min_id` increments it.
Classify an alias as an extraction alias only when its name starts with
`__datafusion_extracted` and its inner expression is eligible for leaf
pushdown (`ExpressionPlacement::should_push_to_leaves`).

This prevents user-defined aliases with canonical names such as
`10 / id AS __datafusion_extracted_1` from being hoisted below filters,
while still accepting optimizer-generated `usize::MAX` and wrapped `_0`
aliases. Numeric suffix parsing remains reserved for generator seeding.
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Dec 2, 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.

Handle name collisions for __datafusion_extracted aliases in leaf expression extraction

1 participant