Skip to content

fix: alias surviving union inputs after an empty input is removed - #27612

Open
Adam-Alani wants to merge 1 commit into
apache:mainfrom
Adam-Alani:fix-union-remaining-input-names
Open

Adam-Alani wants to merge 1 commit into
apache:mainfrom
Adam-Alani:fix-union-remaining-input-names

Conversation

@Adam-Alani

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

SELECT a FROM t WHERE false UNION ALL SELECT b FROM s UNION ALL SELECT c FROM u fails while creating a physical plan:

Assertion failed: col.name() == matching_name: Input field name b does not match with the projection expression a

propagate_empty_relation drops the empty input and keeps the union schema, whose names came from that input. #26557 maps the remaining input by position when it is the only one left. When two or more inputs survive, the union is rebuilt with those names and the other inputs are left unchanged.

What changes are included in this PR?

When an empty union input is removed and more than one input remains, each surviving input whose field names differ from the union schema is wrapped in a projection that aliases its columns by position. Inputs that already use those names are left as they are. The single-remaining-input path uses the same projection helper.

What is the testing strategy for this PR?

  • propagate_union_children_different_schema now expects the positional aliases.
  • propagate_union_multiple_remaining_inputs_with_other_names covers two surviving inputs with different names.
  • union.slt executes the UNION ALL and UNION forms.
  • cargo test -p datafusion-optimizer --lib propagate_union passes locally.

Are there any user-facing changes?

Queries that previously failed to plan now return rows. Plans whose remaining inputs already share the union's field names are unchanged.

propagate_empty_relation kept the removed input's field names when two or
more union inputs survived. Physical planning then asserted that each
input's column name matched that projection.
@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Nov 20, 2026
@Subodh-17

Copy link
Copy Markdown

Thanks @Adam-Alani, this looks like a solid fix that completes the positional alignment for multi-input unions following the empty-input elimination.

I independently reproduced the issue against unpatched main. When PropagateEmptyRelation removes the empty first input, the Union retains its original schema names, while the surviving inputs may retain different names.

The failure is particularly visible when the branches aren't bare projections—for example, branches containing LIMIT—where analyzer TypeCoercion doesn't pre-alias the inputs. An outer expression such as SELECT a + 1 FROM (...) then triggers the physical planning assertion:

Internal error: Assertion failed: col.name() == matching_name:
Input field name b does not match with the projection expression a.

Without an outer expression, I also observed the output schema drifting to the surviving input's name.

With this PR's align_union_input logic, I verified that both the assertion and schema mismatch are resolved.

One minor test observation: the added bare-projection SQL example can be masked by TypeCoercion::coerce_union on unpatched main. A branch with LIMIT, or an outer expression, makes the reproduction more direct. The optimizer unit test already covers the rule itself, so this is only a possible integration-test improvement.

Thanks for addressing this edge case.

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.

UNION fails to plan when the first input is empty and later inputs use different column names

2 participants