Repository navigation
fix: avoid __datafusion_extracted alias collisions in leaf extraction - #27945
Draft
mattsu2020 wants to merge 3 commits into
Draft
mattsu2020 wants to merge 3 commits into
mattsu2020 wants to merge 3 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #27944.
Rationale for this change
Follow-up split out from #26969. When user columns or aliases use the reserved
__datafusion_extractedprefix,ExtractLeafExpressionsandPushDownLeafProjectionscan encounter name collisions:__datafusion_extracted*names nearusize::MAX(or standalonePushDownLeafProjectionsruns withoutExtractLeafExpressions) can causeAliasGeneratorto emit names that collide with existing columns or aliases.__datafusion_extracted*alias down through a projection when that name already exists inproj.input's subtree (e.g., shadowing an input column or colliding with a column in anotherUNION ALLbranch) causesAmbiguousReference/ duplicate field errors.Instead of scattering per-node destination checks across merge/join/union sites (which previously duplicated merge-builder logic and regressed
LEFT SEMIleaf pushdown), this PR handles collisions in a single place.What changes are included in this PR?
__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 seedAliasGeneratorin bothExtractLeafExpressionsandPushDownLeafProjections.next_available_alias): Probe for the next alias not inreserved_names(bounded byreserved_names.len() + 1so wraparound never loops or collides), and skip extraction without mutatingcolumns_neededif no free name is found.split_and_push_projection: When a projection contains a pushable__datafusion_extracted*alias whose name already exists anywhere inproj.input's subtree, unwrap and route it throughrouting_extractso it is extracted under a fresh plan-wide unique name and restored to its original alias inrecovery_exprs. Optimizer-generated aliases from earlier passes do not exist inproj.input's subtree, so they continue to be carried down directly without blocking ordinary pushdowns (includingLEFT SEMIjoins).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: nestedUNION ALLwhere an extraction alias collides with a column name present only in the second branch.datafusion/optimizer/src/extract_leaf_expressions.rs:test_existing_max_and_zero_aliases_avoid_schema_ambiguity: verifies wraparound allocation skips existing_MAXand_0aliases.test_standalone_pushdown_skips_existing_extracted_aliases: verifies standalonePushDownLeafProjectionsskips existing__datafusion_extracted*aliases.test_left_semi_join_leaf_pushdown_preserved: verifies ordinaryLEFT SEMIjoin leaf pushdown is not blocked.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, andcargo test --test sqllogictests -- projection_pushdownpassed.Are there any user-facing changes?
Queries with user-defined columns or aliases sharing the
__datafusion_extractedprefix no longer fail withAmbiguousReference/ duplicate field errors during leaf expression extraction and pushdown. Public Rust APIs and protobuf formats are unchanged.This PR was prepared with AI assistance.