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
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

1 participant