Skip to content

Commit cd578a8

Browse files
committed
ref: alter set expressions directly instead of creating new ones
1 parent 4aa6a92 commit cd578a8

File tree

1 file changed

+15
-35
lines changed

1 file changed

+15
-35
lines changed

datafusion/sql/src/set_expr.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
3838
SetExpr::SetOperation {
3939
op,
4040
left,
41-
right,
41+
mut right,
4242
set_quantifier,
4343
} => {
4444
let left_span = Span::try_from_sqlparser_span(left.span());
@@ -47,23 +47,22 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
4747

4848
// For non-*ByName operations, add missing aliases to right side using left schema's
4949
// column names. This allows queries like
50-
// `SELECT 1 c1, 0 c2, 0 c3 UNION ALL SELECT 2, 0, 0`
50+
// `SELECT 1 a, 1 b UNION ALL SELECT 2, 2`
5151
// where the right side has duplicate literal values.
5252
// We only do this if the left side succeeded.
53-
let right = if let Ok(plan) = &left_plan
53+
if let Ok(plan) = &left_plan
5454
&& plan.schema().fields().len() > 1
5555
&& matches!(
5656
set_quantifier,
5757
SetQuantifier::All
5858
| SetQuantifier::Distinct
5959
| SetQuantifier::None
60-
) {
61-
alias_set_expr(*right, plan.schema())
62-
} else {
63-
*right
64-
};
60+
)
61+
{
62+
alias_set_expr(&mut *right, plan.schema())
63+
}
6564

66-
let right_plan = self.set_expr_to_plan(right, planner_context);
65+
let right_plan = self.set_expr_to_plan(*right, planner_context);
6766

6867
// Handle errors from both sides, collecting them if both failed
6968
let (left_plan, right_plan) = match (left_plan, right_plan) {
@@ -188,34 +187,15 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
188187
// This ensures that unnamed expressions on the right side of a UNION/INTERSECT/EXCEPT
189188
// get aliased with the column names from the left side, allowing queries like
190189
// `SELECT 1 AS a, 0 AS b, 0 AS c UNION ALL SELECT 2, 0, 0` to work correctly.
191-
fn alias_set_expr(set_expr: SetExpr, schema: &DFSchemaRef) -> SetExpr {
190+
fn alias_set_expr(set_expr: &mut SetExpr, schema: &DFSchemaRef) {
192191
match set_expr {
193-
SetExpr::Select(mut select) => {
194-
alias_select_items(&mut select.projection, schema);
195-
SetExpr::Select(select)
196-
}
197-
SetExpr::SetOperation {
198-
op,
199-
left,
200-
right,
201-
set_quantifier,
202-
} => {
203-
// For nested set operations, only alias the leftmost branch
204-
// since that's what determines the output column names
205-
SetExpr::SetOperation {
206-
op,
207-
left: Box::new(alias_set_expr(*left, schema)),
208-
right,
209-
set_quantifier,
210-
}
211-
}
212-
SetExpr::Query(mut query) => {
213-
// Handle parenthesized queries like (SELECT ... UNION ALL SELECT ...)
214-
query.body = Box::new(alias_set_expr(*query.body, schema));
215-
SetExpr::Query(query)
216-
}
192+
SetExpr::Select(select) => alias_select_items(&mut select.projection, schema),
193+
// For nested set operations, only alias the leftmost branch
194+
SetExpr::SetOperation { left, .. } => alias_set_expr(left, schema),
195+
// Handle parenthesized queries like (SELECT ... UNION ALL SELECT ...)
196+
SetExpr::Query(query) => alias_set_expr(&mut *query.body, schema),
217197
// For other cases (Values, etc.), return as-is
218-
other => other,
198+
_other => (),
219199
}
220200
}
221201

0 commit comments

Comments
 (0)