Repository navigation
fix: classify leaf extraction aliases by placement - #26969
mattsu2020 wants to merge 3 commits into
Conversation
|
I also reviewed the AI-generated code myself. |
kosiew
left a comment
There was a problem hiding this comment.
@mattsu2020, thanks for centralizing extraction-alias recognition. The approach fixes the reported misclassification and adds useful regression coverage.
However, the allocator can still generate aliases rejected by the new validator, causing a reproducible optimizer failure. Please address this before merging.
5cdcb2c to
837c515
Compare
kosiew
left a comment
There was a problem hiding this comment.
@mattsu2020, thanks for the follow-up. The suffix parser correctly protects alias counter seeding, but it is insufficient for identifying extracted expressions.
I'm requesting changes because canonical user aliases can still bypass filter guards, while generated aliases near the counter limit can cause schema ambiguity. Classification should depend on expression pushdown eligibility, not numeric suffix validity.
|
|
||
| /// Returns true if `exprs` are the expressions of a *pure extraction | ||
| /// projection*: every expression is either `Alias(EXTRACTED_EXPR_PREFIX, ...)` | ||
| /// projection*: every expression is either an alias named `__datafusion_extracted_<number>` |
There was a problem hiding this comment.
Please update the rustdoc to describe prefix and expression-based classification rather than canonical numeric suffixes. Reserve numeric suffix validation for generator seeding.
kosiew
left a comment
There was a problem hiding this comment.
Thanks for addressing the previous findings. Separating expression placement from counter seeding is the right approach.
One alias collision still causes valid SQL to fail. Please fix this case, add a regression test, and update the PR description to reflect the current implementation.
b98c6bc to
2ab85f3
Compare
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.
2ab85f3 to
5923630
Compare
kosiew
left a comment
There was a problem hiding this comment.
Thanks for addressing the previous feedback. Separating expression eligibility from numeric ID seeding is the right approach, and the guarded-division and direct collision regressions are addressed.
One blocking correctness issue remains: leaf projection pushdown must preserve column bindings at every destination, not just the immediate input.
kosiew
left a comment
There was a problem hiding this comment.
Thanks for addressing the destination alias collisions. The current approach resolves the reproduced correctness failures.
Before approval, please add a SQL regression test that fails without the destination fix. The semi-join optimization tradeoff and projection merge simplification are non-blocking considerations.
| let is_join = matches!(node, LogicalPlan::Join(_)); | ||
| for (idx, input) in inputs.into_iter().enumerate() { | ||
| if per_input[idx].pairs.is_empty() { | ||
| if is_join |
There was a problem hiding this comment.
Non-blocking: this guard prevents Parquet leaf pushdown in the tested LEFT SEMI join despite the conflicting right-hand field being excluded from the output. Is this intentional? If narrowing the guard, please preserve output, join key and filter bindings.
| statement ok | ||
| DROP TABLE review_alias_collision; | ||
|
|
||
| # Nested UNION ALL where a carried or retried extraction alias collides with a |
There was a problem hiding this comment.
Both UNION ALL queries pass without the destination checks. Please retain the Rust regressions and add a SQL test that fails without the fix, using the previous nested LIMIT reproducer with pointer-width handling or a portable equivalent.
| return Ok(false); | ||
| } | ||
|
|
||
| let existing_extractions: IndexMap<Expr, String> = existing |
There was a problem hiding this comment.
Non-blocking suggestion: could the guard and projection builder share the expression substitution and deduplication decision to prevent future divergence in collision detection and safe alias reuse?
|
Thanks for working through all the review rounds. Looking at the whole change, I think it contains two separate fixes that would be easier to review and land apart, and the slow progress is partly my fault. 1. Classifying by placement fixes a real user-facing bug. 2. Avoiding name collisions is a different problem. It only shows up when user columns or aliases use the reserved I don't think more checks at more sites will settle this, and my crafted-name witnesses pushed the PR that way. So I suggest:
Sorry for the churn. Splitting it this way lets the real fix merge now and leaves the collision design to its own discussion. |
|
@kosiew Should I split this PR? |
Yes. |
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.
cb3cc32 to
76acab8
Compare
|
@kosiew I've split this PR as suggested. |
kosiew
left a comment
There was a problem hiding this comment.
Thanks for separating the placement fix from the broader alias collision work. The placement-based classification is appropriate, and the regression tests demonstrate that guarded expressions remain above filters.
However, the narrowed seeding policy introduces a reproducible compatibility regression. Please preserve the previous parsing behavior while retaining overflow protection.
| .strip_prefix(EXTRACTED_EXPR_PREFIX)? | ||
| .strip_prefix('_')?; | ||
| // Integer parsing alone also accepts leading zeros and a leading '+'. | ||
| if suffix.starts_with('0') || !suffix.bytes().all(|b| b.is_ascii_digit()) { |
There was a problem hiding this comment.
Rejecting _01 or _+1 during seeding can allocate _1 despite an existing scan field, causing previously working SQL to fail with schema ambiguity. Please restore the previous parsing behavior for IDs below usize::MAX and add the _01 SQL regression.
There was a problem hiding this comment.
eg this passes on main but fails in this PR
SET datafusion.optimizer.enable_leaf_expression_pushdown = true;
CREATE TABLE seed AS
SELECT column1 AS __datafusion_extracted_1, column2 AS s
FROM VALUES (0, {a: 10}), (1, {a: 20});
SELECT 99 AS __datafusion_extracted_01
FROM seed WHERE __datafusion_extracted_1 > 0 AND s['a'] = 20;
Which issue does this PR close?
Partially addresses #25449; this does not close it. This is a narrower follow-up to closed #26769; the typed-marker design remains open, and name-collision handling for the reserved
__datafusion_extractedprefix is deferred to a separate discussion/follow-up.Rationale for this change
Any alias starting with
__datafusion_extractedis currently treated as an optimizer-generated extraction alias regardless of expression placement:A user alias such as
__datafusion_extracted_manualor__datafusion_extracted_1on a non-pushable expression can cause the optimizer to move it below a filter. For example, this query fails with division by zero even though the filter excludes zero:With this change it returns
10, as do queries using bare__datafusion_extracted,_0,_01,_1,_1x, and_+1suffixes.Invalid or boundary suffixes (
_0,_01,_+1,usize::MAX) should not advance or overflowAliasGeneratorinadvance_generator_past_existing.What changes are included in this PR?
is_extracted_alias): Treat an alias starting with__datafusion_extracted(EXTRACTED_EXPR_PREFIX) as an extraction alias only when its inner expression is eligible for leaf pushdown (ExpressionPlacement::should_push_to_leaves). Update all classification sites (routing_extract,build_extraction_projection_impl,split_and_push_projection, andis_pure_extraction_projection) and helper documentation accordingly.extracted_expr_id): Restrictextracted_expr_idto canonical positive decimal IDs in1..usize::MAXwithout leading zeros or signs so invalid suffixes orusize::MAXdo not advance or overflowAliasGeneratorinadvance_generator_past_existing.What is the testing strategy for this PR?
datafusion/sqllogictest/test_files/projection_pushdown.slt:10 / idguarded byid > 0with bare__datafusion_extractedand suffixes_manual,_1x,_+1,_0,_01, and_1.id > 0withs['a'] = 1inextracted_alias_names_struct(verifying bothEXPLAINplan structure and query results) so leaf extraction pushesget_field(s, 'a')while keeping division above the filter.datafusion/optimizer/src/extract_leaf_expressions.rs:test_extracted_expr_id_boundariesandtest_advance_generator_ignores_invalid_extraction_idscover leading zeros, signs, non-ASCII digits,usize::MAX, and out-of-range suffixes.test_canonical_user_alias_not_pushed_below_filterand new cases intest_is_pure_extraction_projection(usize::MAX,0,Column,KeepInPlace,Literal) verify placement-based classification.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?
User aliases sharing the
__datafusion_extractedprefix (including canonical names like__datafusion_extracted_1) no longer cause non-pushable expressions to be treated as internal extractions. Public Rust APIs and protobuf formats are unchanged.User-defined aliases with the
__datafusion_extractedprefix that wrap pushable expressions can still be classified as extraction aliases (and may collide with existing names); full disambiguation and collision handling are left to #25449 or a separate follow-up.This PR was prepared with AI assistance.