forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.rs
More file actions
427 lines (392 loc) · 14.9 KB
/
query.rs
File metadata and controls
427 lines (392 loc) · 14.9 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// 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 std::sync::Arc;
use crate::planner::{ContextProvider, PlannerContext, SqlToRel};
use crate::stack::StackGuard;
use datafusion_common::{Constraints, DFSchema, Result, not_impl_err};
use datafusion_expr::expr::{Sort, WildcardOptions};
use datafusion_expr::select_expr::SelectExpr;
use datafusion_expr::{
CreateMemoryTable, DdlStatement, Distinct, Expr, LogicalPlan, LogicalPlanBuilder,
};
use sqlparser::ast::{
Expr as SQLExpr, ExprWithAliasAndOrderBy, Ident, LimitClause, Offset, OffsetRows,
OrderBy, OrderByExpr, OrderByKind, PipeOperator, Query, SelectInto, SetExpr,
SetOperator, SetQuantifier, TableAlias,
};
use sqlparser::tokenizer::Span;
impl<S: ContextProvider> SqlToRel<'_, S> {
/// Generate a logical plan from an SQL query/subquery
pub(crate) fn query_to_plan(
&self,
query: Query,
outer_planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
// Each query has its own planner context, including CTEs that are visible within that query.
// It also inherits the CTEs from the outer query by cloning the outer planner context.
let mut query_plan_context = outer_planner_context.clone();
let planner_context = &mut query_plan_context;
let Query {
with,
body,
order_by,
limit_clause,
fetch,
locks: _,
for_clause: _,
settings: _,
format_clause: _,
pipe_operators,
} = query;
if fetch.is_some() {
return not_impl_err!("FETCH clause is not supported yet");
}
if let Some(with) = with {
self.plan_with_clause(with, planner_context)?;
}
let set_expr = *body;
let plan = match set_expr {
SetExpr::Select(mut select) => {
let select_into = select.into.take();
let plan =
self.select_to_plan(*select, order_by.clone(), planner_context)?;
let plan = self.limit(plan, limit_clause.clone(), planner_context)?;
// Process the `SELECT INTO` after `LIMIT`.
self.select_into(plan, select_into)
}
other => {
// The functions called from `set_expr_to_plan()` need more than 128KB
// stack in debug builds as investigated in:
// https://github.com/apache/datafusion/pull/13310#discussion_r1836813902
let plan = {
// scope for dropping _guard
let _guard = StackGuard::new(256 * 1024);
self.set_expr_to_plan(other, planner_context)
}?;
let oby_exprs = to_order_by_exprs(order_by)?;
let order_by_rex = self.order_by_to_sort_expr(
oby_exprs,
plan.schema(),
planner_context,
true,
None,
)?;
let plan = self.order_by(plan, order_by_rex)?;
self.limit(plan, limit_clause, planner_context)
}
}?;
self.pipe_operators(plan, pipe_operators, planner_context)
}
/// Apply pipe operators to a plan
fn pipe_operators(
&self,
mut plan: LogicalPlan,
pipe_operators: Vec<PipeOperator>,
planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
for pipe_operator in pipe_operators {
plan = self.pipe_operator(plan, pipe_operator, planner_context)?;
}
Ok(plan)
}
/// Apply a pipe operator to a plan
fn pipe_operator(
&self,
plan: LogicalPlan,
pipe_operator: PipeOperator,
planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
match pipe_operator {
PipeOperator::Where { expr } => {
self.plan_selection(Some(expr), plan, planner_context)
}
PipeOperator::OrderBy { exprs } => {
let sort_exprs = self.order_by_to_sort_expr(
exprs,
plan.schema(),
planner_context,
true,
None,
)?;
self.order_by(plan, sort_exprs)
}
PipeOperator::Limit { expr, offset } => self.limit(
plan,
Some(LimitClause::LimitOffset {
limit: Some(expr),
offset: offset.map(|offset| Offset {
value: offset,
rows: OffsetRows::None,
}),
limit_by: vec![],
}),
planner_context,
),
PipeOperator::Select { exprs } => {
let empty_from = matches!(plan, LogicalPlan::EmptyRelation(_));
let select_exprs =
self.prepare_select_exprs(&plan, exprs, empty_from, planner_context)?;
self.project(plan, select_exprs, None)
}
PipeOperator::Extend { exprs } => {
let empty_from = matches!(plan, LogicalPlan::EmptyRelation(_));
let extend_exprs =
self.prepare_select_exprs(&plan, exprs, empty_from, planner_context)?;
let all_exprs =
std::iter::once(SelectExpr::Wildcard(WildcardOptions::default()))
.chain(extend_exprs)
.collect();
self.project(plan, all_exprs, None)
}
PipeOperator::As { alias } => self.apply_table_alias(
plan,
TableAlias {
name: alias,
// Apply to all fields
columns: vec![],
explicit: true,
},
),
PipeOperator::Union {
set_quantifier,
queries,
} => self.pipe_operator_set(
plan,
SetOperator::Union,
set_quantifier,
queries,
planner_context,
),
PipeOperator::Intersect {
set_quantifier,
queries,
} => self.pipe_operator_set(
plan,
SetOperator::Intersect,
set_quantifier,
queries,
planner_context,
),
PipeOperator::Except {
set_quantifier,
queries,
} => self.pipe_operator_set(
plan,
SetOperator::Except,
set_quantifier,
queries,
planner_context,
),
PipeOperator::Aggregate {
full_table_exprs,
group_by_expr,
} => self.pipe_operator_aggregate(
plan,
full_table_exprs,
group_by_expr,
planner_context,
),
PipeOperator::Join(join) => {
self.parse_relation_join(plan, join, planner_context)
}
x => not_impl_err!("`{x}` pipe operator is not supported yet"),
}
}
/// Handle Union/Intersect/Except pipe operators
fn pipe_operator_set(
&self,
mut plan: LogicalPlan,
set_operator: SetOperator,
set_quantifier: SetQuantifier,
queries: Vec<Query>,
planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
for query in queries {
let right_plan = self.query_to_plan(query, planner_context)?;
plan = self.set_operation_to_plan(
set_operator,
plan,
right_plan,
set_quantifier,
)?;
}
Ok(plan)
}
/// Wrap a plan in a limit
fn limit(
&self,
input: LogicalPlan,
limit_clause: Option<LimitClause>,
planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
let Some(limit_clause) = limit_clause else {
return Ok(input);
};
let empty_schema = DFSchema::empty();
let (skip, fetch, limit_by_exprs) = match limit_clause {
LimitClause::LimitOffset {
limit,
offset,
limit_by,
} => {
let skip = offset
.map(|o| self.sql_to_expr(o.value, &empty_schema, planner_context))
.transpose()?;
let fetch = limit
.map(|e| self.sql_to_expr(e, &empty_schema, planner_context))
.transpose()?;
let limit_by_exprs = limit_by
.into_iter()
.map(|e| self.sql_to_expr(e, &empty_schema, planner_context))
.collect::<Result<Vec<_>>>()?;
(skip, fetch, limit_by_exprs)
}
LimitClause::OffsetCommaLimit { offset, limit } => {
let skip =
Some(self.sql_to_expr(offset, &empty_schema, planner_context)?);
let fetch =
Some(self.sql_to_expr(limit, &empty_schema, planner_context)?);
(skip, fetch, vec![])
}
};
if !limit_by_exprs.is_empty() {
return not_impl_err!("LIMIT BY clause is not supported yet");
}
if skip.is_none() && fetch.is_none() {
return Ok(input);
}
LogicalPlanBuilder::from(input)
.limit_by_expr(skip, fetch)?
.build()
}
/// Wrap the logical in a sort
pub(super) fn order_by(
&self,
plan: LogicalPlan,
order_by: Vec<Sort>,
) -> Result<LogicalPlan> {
if order_by.is_empty() {
return Ok(plan);
}
if let LogicalPlan::Distinct(Distinct::On(ref distinct_on)) = plan {
// In case of `DISTINCT ON` we must capture the sort expressions since during the plan
// optimization we're effectively doing a `first_value` aggregation according to them.
let distinct_on = distinct_on.clone().with_sort_expr(order_by)?;
Ok(LogicalPlan::Distinct(Distinct::On(distinct_on)))
} else {
LogicalPlanBuilder::from(plan).sort(order_by)?.build()
}
}
/// Handle AGGREGATE pipe operator
fn pipe_operator_aggregate(
&self,
plan: LogicalPlan,
full_table_exprs: Vec<ExprWithAliasAndOrderBy>,
group_by_expr: Vec<ExprWithAliasAndOrderBy>,
planner_context: &mut PlannerContext,
) -> Result<LogicalPlan> {
let plan_schema = plan.schema();
let process_expr =
|expr_with_alias_and_order_by: ExprWithAliasAndOrderBy,
planner_context: &mut PlannerContext| {
let expr_with_alias = expr_with_alias_and_order_by.expr;
let sql_expr = expr_with_alias.expr;
let alias = expr_with_alias.alias;
let df_expr = self.sql_to_expr(sql_expr, plan_schema, planner_context)?;
match alias {
Some(alias_ident) => df_expr.alias_if_changed(alias_ident.value),
None => Ok(df_expr),
}
};
let aggr_exprs: Vec<Expr> = full_table_exprs
.into_iter()
.map(|e| process_expr(e, planner_context))
.collect::<Result<Vec<_>>>()?;
let group_by_exprs: Vec<Expr> = group_by_expr
.into_iter()
.map(|e| process_expr(e, planner_context))
.collect::<Result<Vec<_>>>()?;
LogicalPlanBuilder::from(plan)
.aggregate(group_by_exprs, aggr_exprs)?
.build()
}
/// Wrap the logical plan in a `SelectInto`
fn select_into(
&self,
plan: LogicalPlan,
select_into: Option<SelectInto>,
) -> Result<LogicalPlan> {
match select_into {
Some(into) => Ok(LogicalPlan::Ddl(DdlStatement::CreateMemoryTable(
CreateMemoryTable {
name: self.object_name_to_table_reference(into.name)?,
constraints: Constraints::default(),
input: Arc::new(plan),
if_not_exists: false,
or_replace: false,
temporary: false,
column_defaults: vec![],
},
))),
_ => Ok(plan),
}
}
}
/// Returns the order by expressions from the query.
fn to_order_by_exprs(order_by: Option<OrderBy>) -> Result<Vec<OrderByExpr>> {
to_order_by_exprs_with_select(order_by, None)
}
/// Returns the order by expressions from the query with the select expressions.
pub(crate) fn to_order_by_exprs_with_select(
order_by: Option<OrderBy>,
select_exprs: Option<&Vec<Expr>>,
) -> Result<Vec<OrderByExpr>> {
let Some(OrderBy { kind, interpolate }) = order_by else {
// If no order by, return an empty array.
return Ok(vec![]);
};
if let Some(_interpolate) = interpolate {
return not_impl_err!("ORDER BY INTERPOLATE is not supported");
}
match kind {
OrderByKind::All(order_by_options) => {
let Some(exprs) = select_exprs else {
return Ok(vec![]);
};
let order_by_exprs = exprs
.iter()
.map(|select_expr| match select_expr {
Expr::Column(column) => Ok(OrderByExpr {
expr: SQLExpr::Identifier(Ident {
value: column.name.clone(),
quote_style: None,
span: Span::empty(),
}),
options: order_by_options,
with_fill: None,
}),
// TODO: Support other types of expressions
_ => not_impl_err!(
"ORDER BY ALL is not supported for non-column expressions"
),
})
.collect::<Result<Vec<_>>>()?;
Ok(order_by_exprs)
}
OrderByKind::Expressions(order_by_exprs) => Ok(order_by_exprs),
}
}