Skip to content

Handle name collisions for __datafusion_extracted aliases in leaf expression extraction #27944

Backlog

Description

@mattsu2020

Describe the bug

Split out from #26969 as a follow-up (related to #25449, which tracks replacing the string-prefix protocol with a typed marker).

While #26969 fixes placement-based classification (is_extracted_alias) and canonical-ID seeding (extracted_expr_id), name collisions can still occur when user columns or aliases use the reserved __datafusion_extracted prefix:

  1. Alias allocation collisions / wraparound: When existing __datafusion_extracted* names are near usize::MAX (causing AliasGenerator wraparound to _MAX / _0) or when PushDownLeafProjections runs without ExtractLeafExpressions, newly allocated extraction aliases can collide with existing column or alias names.

  2. Input-column shadowing: When a projection aliases a pushdown-eligible expression to a __datafusion_extracted* name that already exists in its input schema (and the input column is also referenced), pushing that alias down directly causes an AmbiguousReference / duplicate field error:

    CREATE TABLE t AS
    SELECT column1 AS __datafusion_extracted_manual, column2 AS s
    FROM VALUES (0, {a: 1}), (1, {a: 2});
    
    SELECT s['a'] AS __datafusion_extracted_manual
    FROM t
    WHERE t.__datafusion_extracted_manual > 0;
  3. Destination branch collisions (e.g., UNION ALL): Carrying a __datafusion_extracted* alias down across a set operation or join can collide with a column of the same name present only in another branch/side.

Expected behavior

Queries using columns or aliases with the __datafusion_extracted prefix should not fail with schema ambiguity errors, and collision avoidance should not regress leaf pushdown for ordinary queries (such as LEFT SEMI Parquet leaf pushdown).

Design notes

As discussed in #26969, adding ad-hoc destination checks at multiple sites duplicates merge-builder decisions and risks regressing unrelated pushdowns. Possible directions:

  • Handle collisions in a single place: reserve all existing __datafusion_extracted* names across the entire plan upfront, re-extract under a fresh plan-wide unique name instead of moving a potentially colliding alias down, and skip extraction instead of wrapping when no free name is available (verifying how this interacts with optimizer-generated aliases from earlier passes).
  • Or track this as a known limitation of the reserved prefix until Replace the __datafusion_extracted / __common_expr alias prefix protocol with a typed marker #25449 introduces a typed marker.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions