forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_expr.rs
More file actions
248 lines (236 loc) · 10.1 KB
/
set_expr.rs
File metadata and controls
248 lines (236 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use datafusion_common::{
DFSchemaRef, DataFusionError, Diagnostic, Result, Span, not_impl_err, plan_err,
};
use datafusion_expr::{LogicalPlan, LogicalPlanBuilder};
use sqlparser::ast::{
Expr as SQLExpr, Ident, SelectItem, SetExpr, SetOperator, SetQuantifier, Spanned,
};
impl<S: ContextProvider> SqlToRel<'_, S> {
#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
pub(super) fn set_expr_to_plan(
&self,
set_expr: SetExpr,
planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
let set_expr_span = Span::try_from_sqlparser_span(set_expr.span());
match set_expr {
SetExpr::Select(s) => self.select_to_plan(*s, None, planner_context),
SetExpr::Values(v) => self.sql_values_to_plan(v, planner_context),
SetExpr::SetOperation {
op,
left,
mut right,
set_quantifier,
} => {
let left_span = Span::try_from_sqlparser_span(left.span());
let right_span = Span::try_from_sqlparser_span(right.span());
let left_plan = self.set_expr_to_plan(*left, planner_context);
// For non-*ByName operations, add missing aliases to right side using left schema's
// column names. This allows queries like
// `SELECT 1 a, 1 b UNION ALL SELECT 2, 2`
// where the right side has duplicate literal values.
// We only do this if the left side succeeded.
if let Ok(plan) = &left_plan
&& plan.schema().fields().len() > 1
&& matches!(
set_quantifier,
SetQuantifier::All
| SetQuantifier::Distinct
| SetQuantifier::None
)
{
alias_set_expr(&mut right, plan.schema())
}
let right_plan = self.set_expr_to_plan(*right, planner_context);
// Handle errors from both sides, collecting them if both failed
let (left_plan, right_plan) = match (left_plan, right_plan) {
(Ok(left_plan), Ok(right_plan)) => (left_plan, right_plan),
(Err(left_err), Err(right_err)) => {
return Err(DataFusionError::Collection(vec![
left_err, right_err,
]));
}
(Err(err), _) | (_, Err(err)) => {
return Err(err);
}
};
if !(set_quantifier == SetQuantifier::ByName
|| set_quantifier == SetQuantifier::AllByName)
{
self.validate_set_expr_num_of_columns(
op,
left_span,
right_span,
&left_plan,
&right_plan,
set_expr_span,
)?;
}
self.set_operation_to_plan(op, left_plan, right_plan, set_quantifier)
}
SetExpr::Query(q) => self.query_to_plan(*q, planner_context),
_ => not_impl_err!("Query {set_expr} not implemented yet"),
}
}
pub(super) fn is_union_all(set_quantifier: SetQuantifier) -> Result<bool> {
match set_quantifier {
SetQuantifier::All | SetQuantifier::AllByName => Ok(true),
SetQuantifier::Distinct
| SetQuantifier::ByName
| SetQuantifier::DistinctByName
| SetQuantifier::None => Ok(false),
}
}
fn validate_set_expr_num_of_columns(
&self,
op: SetOperator,
left_span: Option<Span>,
right_span: Option<Span>,
left_plan: &LogicalPlan,
right_plan: &LogicalPlan,
set_expr_span: Option<Span>,
) -> Result<()> {
if left_plan.schema().fields().len() == right_plan.schema().fields().len() {
return Ok(());
}
let diagnostic = Diagnostic::new_error(
format!("{op} queries have different number of columns"),
set_expr_span,
)
.with_note(
format!("this side has {} fields", left_plan.schema().fields().len()),
left_span,
)
.with_note(
format!(
"this side has {} fields",
right_plan.schema().fields().len()
),
right_span,
);
plan_err!("{} queries have different number of columns", op; diagnostic =diagnostic)
}
pub(super) fn set_operation_to_plan(
&self,
op: SetOperator,
left_plan: LogicalPlan,
right_plan: LogicalPlan,
set_quantifier: SetQuantifier,
) -> Result<LogicalPlan> {
match (op, set_quantifier) {
(SetOperator::Union, SetQuantifier::All) => {
LogicalPlanBuilder::from(left_plan)
.union(right_plan)?
.build()
}
(SetOperator::Union, SetQuantifier::AllByName) => {
LogicalPlanBuilder::from(left_plan)
.union_by_name(right_plan)?
.build()
}
(SetOperator::Union, SetQuantifier::Distinct | SetQuantifier::None) => {
LogicalPlanBuilder::from(left_plan)
.union_distinct(right_plan)?
.build()
}
(
SetOperator::Union,
SetQuantifier::ByName | SetQuantifier::DistinctByName,
) => LogicalPlanBuilder::from(left_plan)
.union_by_name_distinct(right_plan)?
.build(),
(SetOperator::Intersect, SetQuantifier::All) => {
LogicalPlanBuilder::intersect(left_plan, right_plan, true)
}
(SetOperator::Intersect, SetQuantifier::Distinct | SetQuantifier::None) => {
LogicalPlanBuilder::intersect(left_plan, right_plan, false)
}
(SetOperator::Except, SetQuantifier::All) => {
LogicalPlanBuilder::except(left_plan, right_plan, true)
}
(SetOperator::Except, SetQuantifier::Distinct | SetQuantifier::None) => {
LogicalPlanBuilder::except(left_plan, right_plan, false)
}
(op, quantifier) => {
not_impl_err!("{op} {quantifier} not implemented")
}
}
}
}
// Adds aliases to SELECT items in a SetExpr using the provided schema.
// This ensures that unnamed expressions on the right side of a UNION/INTERSECT/EXCEPT
// get aliased with the column names from the left side, allowing queries like
// `SELECT 1 AS a, 0 AS b, 0 AS c UNION ALL SELECT 2, 0, 0` to work correctly.
fn alias_set_expr(set_expr: &mut SetExpr, schema: &DFSchemaRef) {
match set_expr {
SetExpr::Select(select) => alias_select_items(&mut select.projection, schema),
// For nested set operations, only alias the leftmost branch
SetExpr::SetOperation { left, .. } => alias_set_expr(left, schema),
// Handle parenthesized queries like (SELECT ... UNION ALL SELECT ...)
SetExpr::Query(query) => alias_set_expr(&mut query.body, schema),
// For other cases (Values, etc.), return as-is
_other => (),
}
}
// Aliases unnamed expressions in the provided select items using the provided schema.
// This helps with set expression queries where the right side has duplicate expressions,
// but the left side has unique column names, which control the output schema anyway.
fn alias_select_items(items: &mut [SelectItem], schema: &DFSchemaRef) {
// Figure out how many (qualified) wildcards we got. We only handle
// the case of a single unqualified wildcard; for multiple or qualified
// wildcards we can't reliably determine column counts, so bail out.
let (wildcard_count, qualified_wildcard_count) =
items.iter().fold((0, 0), |(wc, qwc), item| match item {
SelectItem::Wildcard(_) => (wc + 1, qwc),
SelectItem::QualifiedWildcard(_, _) => (wc, qwc + 1),
_ => (wc, qwc),
});
if qualified_wildcard_count > 0 || wildcard_count > 1 {
return;
}
let wildcard_expansion = schema.fields().len().saturating_sub(items.len() - 1);
let mut col_idx = 0;
for item in items.iter_mut() {
match item {
SelectItem::UnnamedExpr(expr) => {
if !matches!(
expr,
SQLExpr::Identifier(_) | SQLExpr::CompoundIdentifier(_)
) && let Some(field) = schema.fields().get(col_idx)
{
*item = SelectItem::ExprWithAlias {
expr: expr.clone(),
alias: Ident::new(field.name()),
};
}
col_idx += 1;
}
SelectItem::ExprWithAlias { .. } => {
col_idx += 1;
}
SelectItem::Wildcard(_) => {
col_idx += wildcard_expansion;
}
SelectItem::QualifiedWildcard(_, _) => {
unreachable!("qualified wildcards are handled above")
}
}
}
}