Skip to content

fix: map remaining union input by position in propagate_empty_relation - #26557

Merged
alamb merged 2 commits into
apache:mainfrom
moraistejerina:fix-propagate-empty-relation-union-projection
Oct 23, 2026
Merged

alamb merged 2 commits into
apache:mainfrom
moraistejerina:fix-propagate-empty-relation-union-projection

Conversation

@moraistejerina

@moraistejerina moraistejerina commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

A UNION ALL fails to plan with Schema error: No field named ... when the optimizer removes its first input and a later input remains. For example:

SELECT 1 WHERE false UNION ALL (SELECT 2 LIMIT 1);

propagate_empty_relation puts a projection over the remaining input with Projection::new_from_schema. That function makes column references from the union's field names, which come from the first input. When the remaining input has other names, the projection refers to columns that do not exist, and optimize_projections fails.

What changes are included in this PR?

  • When one input remains and its schema is different from the union's schema, map its columns to the union's output fields by position, alias each one to the union's name, and build the projection with Projection::try_new (which also checks the result).
  • Three existing snapshots change their text from Projection: a, b, c to Projection: test.a AS a, test.b AS b, test.c AS c. In those tests the remaining input is the first input, so the old by-name references worked. The output schema is the same.
  • This also changes the EXPLAIN output of the struct.slt test added in fix: resolve pass-through columns against the input when merging an extraction projection #25412. That PR made push_down_leaf_projections handle the bare-name projection that propagate_empty_relation left after it removed an empty union input. With this change, the projection aliases the qualified input columns (leaf_merge_source.s AS s). The test still plans and returns the same rows. fix: resolve pass-through columns against the input when merging an extraction projection #25412 also handles this same-name alias form, and its unit tests still build the bare-name shape directly.

What is the testing strategy for this PR?

  • New unit test propagate_union_remaining_input_with_other_names: the first input is empty and the second input has other column names. Without the fix, the rule produces Projection: a, b, c over an input that only has x, y, z.
  • New union.slt cases with the two queries from the issue.

Are there any user-facing changes?

No. The queries that failed now return results. There are no public API changes.

@github-actions github-actions Bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Oct 21, 2026
@moraistejerina
moraistejerina force-pushed the fix-propagate-empty-relation-union-projection branch 3 times, most recently from d681fc5 to 9a53541 Compare October 21, 2026 09:54
@moraistejerina
moraistejerina marked this pull request as ready for review October 21, 2026 09:58
@moraistejerina
moraistejerina force-pushed the fix-propagate-empty-relation-union-projection branch from 9a53541 to d11f89a Compare October 21, 2026 10:06
@moraistejerina

Copy link
Copy Markdown
Contributor Author

@alamb could you take a look when you have a chance? This fixes #26516: when propagate_empty_relation removes the first input of a union, it builds a projection with column names that the remaining input does not have.

When propagate_empty_relation removes all but one input of a Union, it
put a Projection over the remaining input with Projection::new_from_schema.
That made column references from the union's field names, which come from
the first input. If a later input remained and had other names, the
projection referred to columns that did not exist, and optimize_projections
failed with "No field named ...".

Map the remaining input's columns to the union's fields by position and
build the projection with Projection::try_new.

Closes apache#26516.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@moraistejerina
moraistejerina force-pushed the fix-propagate-empty-relation-union-projection branch from d11f89a to 5b65023 Compare October 21, 2026 10:59

@gabotechs gabotechs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 no comments, all good here!

I've verified that this is an actual problem present today that other SQL engines do not have:

@gabotechs
gabotechs added this pull request to the merge queue Oct 22, 2026
@gabotechs

Copy link
Copy Markdown
Contributor

Thanks @moraistejerina!

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Oct 22, 2026
@alamb
alamb enabled auto-merge October 22, 2026 14:49
@alamb
alamb added this pull request to the merge queue Oct 23, 2026
Merged via the queue into apache:main with commit 80e9788 Oct 23, 2026
45 checks passed
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) v56.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optimize_projections fails with "No field named ..." after propagate_empty_relation removes the first branch of a UNION

3 participants