Skip to content

fix: apply the left side schema on the right side in set expressions#21052

Open
gruuya wants to merge 1 commit intoapache:mainfrom
splitgraph:fix-set-expr-via-planner-context
Open

fix: apply the left side schema on the right side in set expressions#21052
gruuya wants to merge 1 commit intoapache:mainfrom
splitgraph:fix-set-expr-via-planner-context

Conversation

@gruuya
Copy link
Contributor

@gruuya gruuya commented Mar 19, 2026

Which issue does this PR close?

Rationale for this change

DataFusion requires all projected expressions to have unique names during planning, so it doesn't support select 0, 0 for instance.

However this shouldn't be an issue when this is just a sub-SELECT in a larger query which does abide by this rule. For example a set expression (UNION, EXCEPT, INTERSECT) query should only require the first SELECT to provide a unique schema, and that should be sufficient.

Furthermore, this requirement is even more redundant, since all field name/aliases other than those in the first SELECT are discarded anyway.

What changes are included in this PR?

  • when we're processing a set expression (UNION, EXCEPT, INTERSECT), save the left side schema to planner context
  • when we're inside SqlToRel::select_to_plan pop the schema and pass it down to
  • a new project_with_validation_and_schema function in LogicalPlanBuilder to properly alias them

The benefit of this approach compared to #20819 is that wildcards are unwrapped and we can properly handle them as well.

The downside is that we need to thread the left schema via the planner context now.

Are these changes tested?

Yes, there are unit tests and SLTs.

Are there any user-facing changes?

New method in LogicalPlanBuilder called project_with_validation_and_schema which will alias the projection with the provided schema.

@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Mar 19, 2026
@gruuya gruuya force-pushed the fix-set-expr-via-planner-context branch from 584cdb4 to 706f8f8 Compare March 19, 2026 13:41
@gruuya gruuya force-pushed the fix-set-expr-via-planner-context branch from 706f8f8 to 6669e8b Compare March 19, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected set expression query planning error

1 participant