-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathexpr.rs
More file actions
657 lines (613 loc) · 23 KB
/
Copy pathexpr.rs
File metadata and controls
657 lines (613 loc) · 23 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright 2024 KipData/KiteSQL
//
// Licensed 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::catalog::ColumnRef;
use crate::errors::DatabaseError;
use crate::expression;
use crate::expression::agg::AggKind;
use crate::iter_ext::Itertools;
use super::{Binder, BinderContext, QueryBindStep, SubQueryType};
use crate::expression::function::scala::{ArcScalarFunctionImpl, ScalarFunction};
use crate::expression::function::table::TableFunction;
use crate::expression::function::FunctionSummary;
use crate::expression::{AliasType, ScalarExpression};
use crate::planner::operator::mark_apply::MarkApplyQuantifier;
use crate::planner::operator::scalar_subquery::ScalarSubqueryOperator;
use crate::planner::{LogicalPlan, PlanArena};
use crate::storage::Transaction;
use crate::types::value::{DataValue, Utf8Type};
use crate::types::{CharLengthUnits, LogicalType};
macro_rules! try_default {
($table_name:expr, $column_name:expr) => {
if let (None, "default") = ($table_name, $column_name.as_ref()) {
return Ok(ScalarExpression::Empty);
}
};
}
impl<'a, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '_, T, A> {
fn find_column_in_schema<'schema>(
schema_ref: impl IntoIterator<Item = &'schema ColumnRef>,
arena: &PlanArena,
column_name: &str,
) -> Option<(usize, ColumnRef)> {
schema_ref
.into_iter()
.enumerate()
.find(|(_, column)| arena.column(**column).name() == column_name)
.map(|(position, column)| (position, *column))
}
fn find_column_in_scope(
context: &BinderContext<'a, T>,
arena: &mut PlanArena,
column_name: &str,
) -> Option<ScalarExpression> {
let mut position_offset = 0;
for bound_source in &context.bind_table {
let source = &bound_source.source;
if let Some((position, column)) =
Self::find_column_in_schema(source.schema().iter(), arena, column_name)
{
return Some(ScalarExpression::column_expr(
column,
position_offset + position,
));
}
position_offset += source.schema_len();
}
None
}
pub(crate) fn bind_temp_table(
&mut self,
expr: ScalarExpression,
sub_query: LogicalPlan,
arena: &mut PlanArena,
) -> Result<(ScalarExpression, LogicalPlan), DatabaseError> {
let (exprs, is_tuple) = match expr {
ScalarExpression::Tuple(exprs) => (exprs, true),
expr => (vec![expr], false),
};
let mut alias_exprs = Vec::with_capacity(exprs.len());
let mut alias_refs = Vec::with_capacity(exprs.len());
for (position, expr) in exprs.into_iter().enumerate() {
let (alias_expr, alias_ref) = self.bind_temp_table_alias(expr, position, arena);
if !is_tuple {
let alias_plan = Self::build_project_plan(sub_query, vec![alias_expr.clone()]);
return Ok((alias_expr, alias_plan));
}
alias_exprs.push(alias_expr);
alias_refs.push(alias_ref);
}
let alias_plan = Self::build_project_plan(sub_query, alias_exprs);
Ok((ScalarExpression::Tuple(alias_refs), alias_plan))
}
pub(crate) fn bind_temp_table_alias(
&mut self,
expr: ScalarExpression,
position: usize,
arena: &mut PlanArena,
) -> (ScalarExpression, ScalarExpression) {
let output_column = expr.output_column_ref(arena);
let mut alias_column = arena.clone_column(output_column);
alias_column.set_ref_table(arena.temp_table(), 0, true);
let alias_column = arena.alloc_column(alias_column);
let alias_ref = ScalarExpression::column_expr(alias_column, position);
(
ScalarExpression::Alias {
expr: Box::new(expr),
alias: AliasType::Expr(Box::new(alias_ref.clone())),
},
alias_ref,
)
}
pub(crate) fn bind_subquery_plan<'arena, F>(
&mut self,
arena: &mut PlanArena<'arena>,
build: F,
) -> Result<(LogicalPlan, bool), DatabaseError>
where
F: FnOnce(
&mut Binder<'a, '_, T, A>,
&mut PlanArena<'arena>,
) -> Result<LogicalPlan, DatabaseError>,
{
let BinderContext {
table_cache,
view_cache,
transaction,
scala_functions,
table_functions,
..
} = &self.context;
let mut binder = Binder::new(
BinderContext::new(
table_cache,
view_cache,
*transaction,
scala_functions,
table_functions,
),
self.args,
Some(&self.context),
);
let sub_query = build(&mut binder, arena)?;
let correlated = binder.context.has_outer_refs();
Ok((sub_query, correlated))
}
pub(crate) fn bind_subquery_plan_with_output<'arena, F>(
&mut self,
value_ty: Option<&LogicalType>,
arena: &mut PlanArena<'arena>,
build: F,
) -> Result<(LogicalPlan, ScalarExpression, bool), DatabaseError>
where
F: FnOnce(
&mut Binder<'a, '_, T, A>,
&mut PlanArena<'arena>,
) -> Result<LogicalPlan, DatabaseError>,
{
let (mut sub_query, correlated) = self.bind_subquery_plan(arena, build)?;
let sub_query_schema = sub_query.output_schema(arena);
let fn_check = |len: usize| {
if sub_query_schema.len() != len {
return Err(DatabaseError::MisMatch(
"expects only one expression to be returned",
"the expression returned by the subquery",
));
}
Ok(())
};
let expr = if let Some(LogicalType::Tuple(tys)) = value_ty {
fn_check(tys.len())?;
let columns = sub_query_schema
.iter()
.enumerate()
.map(|(position, column)| ScalarExpression::column_expr(*column, position))
.collect::<Vec<_>>();
ScalarExpression::Tuple(columns)
} else {
fn_check(1)?;
ScalarExpression::column_expr(sub_query_schema[0], 0)
};
Ok((sub_query, expr, correlated))
}
pub(crate) fn bind_scalar_subquery_plan<'arena, F>(
&mut self,
arena: &mut PlanArena<'arena>,
build: F,
) -> Result<ScalarExpression, DatabaseError>
where
F: FnOnce(
&mut Binder<'a, '_, T, A>,
&mut PlanArena<'arena>,
) -> Result<LogicalPlan, DatabaseError>,
{
let (sub_query, column, correlated) =
self.bind_subquery_plan_with_output(None, arena, build)?;
let sub_query = ScalarSubqueryOperator::build(sub_query);
let (expr, sub_query) = match self.context.step_now() {
QueryBindStep::Where => (column, sub_query),
QueryBindStep::Project => self.bind_temp_table(column, sub_query, arena)?,
_ => {
return Err(DatabaseError::UnsupportedStmt(
"scalar subqueries can only appear in `WHERE` or SELECT list".to_string(),
))
}
};
self.context.sub_query(SubQueryType::SubQuery {
plan: sub_query,
correlated,
});
Ok(expr)
}
pub(crate) fn bind_exists_subquery_plan<'arena, F>(
&mut self,
negated: bool,
arena: &mut PlanArena<'arena>,
build: F,
) -> Result<ScalarExpression, DatabaseError>
where
F: FnOnce(
&mut Binder<'a, '_, T, A>,
&mut PlanArena<'arena>,
) -> Result<LogicalPlan, DatabaseError>,
{
if !self.context.is_step(&QueryBindStep::Where) {
return Err(DatabaseError::UnsupportedStmt(
"EXISTS subqueries can only appear in `WHERE`".to_string(),
));
}
let (sub_query, correlated) = self.bind_subquery_plan(arena, build)?;
let (_, marker_ref) = self.bind_temp_table_alias(
ScalarExpression::Constant(DataValue::Boolean(true)),
0,
arena,
);
let output_column = marker_ref.output_column_ref(arena);
self.context.sub_query(SubQueryType::ExistsSubQuery {
plan: sub_query,
correlated,
output_column,
});
if negated {
Ok(ScalarExpression::Unary {
op: expression::UnaryOperator::Not,
expr: Box::new(marker_ref),
evaluator: None,
ty: LogicalType::Boolean,
})
} else {
Ok(marker_ref)
}
}
pub(crate) fn bind_quantified_subquery_plan<'arena, F>(
&mut self,
quantifier: MarkApplyQuantifier,
negated: bool,
left_expr: ScalarExpression,
compare_op: expression::BinaryOperator,
arena: &mut PlanArena<'arena>,
build: F,
) -> Result<ScalarExpression, DatabaseError>
where
F: FnOnce(
&mut Binder<'a, '_, T, A>,
&mut PlanArena<'arena>,
) -> Result<LogicalPlan, DatabaseError>,
{
let left_ty = left_expr.return_type(arena).into_owned();
let (sub_query, column, correlated) =
self.bind_subquery_plan_with_output(Some(&left_ty), arena, build)?;
if !self.context.is_step(&QueryBindStep::Where) {
return Err(DatabaseError::UnsupportedStmt(
"quantified subqueries can only appear in `WHERE`".to_string(),
));
}
let (alias_expr, sub_query) = self.bind_temp_table(column, sub_query, arena)?;
let predicate = ScalarExpression::Binary {
op: compare_op,
left_expr: Box::new(left_expr),
right_expr: Box::new(alias_expr),
evaluator: None,
ty: LogicalType::Boolean,
};
let (_, marker_ref) = self.bind_temp_table_alias(
ScalarExpression::Constant(DataValue::Boolean(true)),
0,
arena,
);
let output_column = marker_ref.output_column_ref(arena);
self.context.sub_query(SubQueryType::QuantifiedSubQuery {
quantifier,
negated,
plan: sub_query,
correlated,
output_column,
predicate,
});
if negated {
Ok(ScalarExpression::Unary {
op: expression::UnaryOperator::Not,
expr: Box::new(marker_ref),
evaluator: None,
ty: LogicalType::Boolean,
})
} else {
Ok(marker_ref)
}
}
pub(crate) fn bind_column_ref_by_name(
&mut self,
table_name: Option<&str>,
column_name: &str,
bind_table_name: Option<&str>,
arena: &mut PlanArena,
) -> Result<ScalarExpression, DatabaseError> {
if table_name.is_none() {
if let Some((_, expr)) = self
.context
.expr_aliases
.iter()
.find(|((table, column), _)| table.is_none() && column == column_name)
{
return Ok(ScalarExpression::Alias {
expr: Box::new(expr.clone()),
alias: AliasType::Name(column_name.to_string()),
});
}
}
if self.context.allow_default {
try_default!(&table_name, column_name);
}
if let Some(table) = table_name.or(bind_table_name) {
let (source, position_offset) =
match Self::resolve_source_columns_in_scope(&self.context, table) {
Ok(source) => source,
Err(err) => {
if let Some(parent) = self.parent {
self.context.mark_outer_ref();
Self::resolve_source_columns_in_scope(parent, table).map_err(|_| err)?
} else {
return Err(err);
}
}
};
let (position, column) =
Self::find_column_in_schema(source.schema().iter(), arena, column_name)
.ok_or_else(|| DatabaseError::column_not_found(column_name.to_string()))?;
Ok(ScalarExpression::column_expr(
column,
position_offset + position,
))
} else {
// handle col syntax
let mut find_visible_column =
|context: &BinderContext<'a, T>| -> Result<Option<ScalarExpression>, DatabaseError> {
Ok(context
.using
.get(column_name)
.map(|using_column| using_column.visible_expr(arena))
.transpose()?
.or_else(|| {
Self::find_column_in_scope(context, arena, column_name)
}))
};
let mut got_column = find_visible_column(&self.context)?;
if got_column.is_none() {
if let Some(parent) = self.parent {
self.context.mark_outer_ref();
got_column = find_visible_column(parent)?;
}
}
match got_column {
Some(column) => Ok(column),
None => Err(DatabaseError::column_not_found(column_name.to_string())),
}
}
}
pub(crate) fn bind_binary_op_expr(
&mut self,
left_expr: ScalarExpression,
right_expr: ScalarExpression,
op: expression::BinaryOperator,
arena: &mut PlanArena,
) -> Result<ScalarExpression, DatabaseError> {
let left_expr = Box::new(left_expr);
let right_expr = Box::new(right_expr);
let left_ty = left_expr.return_type(arena);
let right_ty = right_expr.return_type(arena);
let ty = match &op {
expression::BinaryOperator::Plus
| expression::BinaryOperator::Minus
| expression::BinaryOperator::Multiply
| expression::BinaryOperator::Modulo => {
LogicalType::max_logical_type(&left_ty, &right_ty)?.into_owned()
}
expression::BinaryOperator::Divide => {
if let LogicalType::Decimal(precision, scale) =
LogicalType::max_logical_type(&left_ty, &right_ty)?.into_owned()
{
LogicalType::Decimal(precision, scale)
} else {
LogicalType::Double
}
}
expression::BinaryOperator::Gt
| expression::BinaryOperator::Lt
| expression::BinaryOperator::GtEq
| expression::BinaryOperator::LtEq
| expression::BinaryOperator::Eq
| expression::BinaryOperator::NotEq
| expression::BinaryOperator::Like(_)
| expression::BinaryOperator::NotLike(_)
| expression::BinaryOperator::And
| expression::BinaryOperator::Or => LogicalType::Boolean,
expression::BinaryOperator::StringConcat => {
LogicalType::Varchar(None, CharLengthUnits::Characters)
}
op => return Err(DatabaseError::UnsupportedStmt(format!("{op}"))),
};
Ok(ScalarExpression::Binary {
op,
left_expr,
right_expr,
evaluator: None,
ty,
})
}
pub(crate) fn bind_unary_op_expr(
&mut self,
expr: ScalarExpression,
op: expression::UnaryOperator,
arena: &mut PlanArena,
) -> Result<ScalarExpression, DatabaseError> {
let expr = Box::new(expr);
let ty = if let expression::UnaryOperator::Not = op {
LogicalType::Boolean
} else {
expr.return_type(arena).into_owned()
};
Ok(ScalarExpression::Unary {
op,
expr,
evaluator: None,
ty,
})
}
pub(crate) fn bind_aggregate_function(
&mut self,
kind: AggKind,
args: Vec<ScalarExpression>,
is_distinct: bool,
arena: &mut PlanArena,
) -> Result<ScalarExpression, DatabaseError> {
let ty = match kind {
AggKind::Count => {
if args.len() != 1 {
return Err(DatabaseError::MisMatch("number of count() parameters", "1"));
}
LogicalType::Integer
}
AggKind::Sum => {
if args.len() != 1 {
return Err(DatabaseError::MisMatch("number of sum() parameters", "1"));
}
args[0].return_type(arena).into_owned()
}
AggKind::Min => {
if args.len() != 1 {
return Err(DatabaseError::MisMatch("number of min() parameters", "1"));
}
args[0].return_type(arena).into_owned()
}
AggKind::Max => {
if args.len() != 1 {
return Err(DatabaseError::MisMatch("number of max() parameters", "1"));
}
args[0].return_type(arena).into_owned()
}
AggKind::Avg => {
if args.len() != 1 {
return Err(DatabaseError::MisMatch("number of avg() parameters", "1"));
}
LogicalType::Double
}
};
Ok(ScalarExpression::AggCall {
distinct: is_distinct,
kind,
args,
ty,
})
}
pub(crate) fn bind_function_call(
&mut self,
function_name: String,
mut args: Vec<ScalarExpression>,
arena: &mut PlanArena,
) -> Result<ScalarExpression, DatabaseError> {
match function_name.as_str() {
"if" => {
if args.len() != 3 {
return Err(DatabaseError::MisMatch("number of if() parameters", "3"));
}
let ty = Self::return_type(&args[1], &args[2], arena)?;
let right_expr = Box::new(args.pop().unwrap());
let left_expr = Box::new(args.pop().unwrap());
let condition = Box::new(args.pop().unwrap());
return Ok(ScalarExpression::If {
condition,
left_expr,
right_expr,
ty,
});
}
"nullif" => {
if args.len() != 2 {
return Err(DatabaseError::MisMatch(
"number of nullif() parameters",
"3",
));
}
let ty = Self::return_type(&args[0], &args[1], arena)?;
let right_expr = Box::new(args.pop().unwrap());
let left_expr = Box::new(args.pop().unwrap());
return Ok(ScalarExpression::NullIf {
left_expr,
right_expr,
ty,
});
}
"ifnull" => {
if args.len() != 2 {
return Err(DatabaseError::MisMatch(
"number of ifnull() parameters",
"3",
));
}
let ty = Self::return_type(&args[0], &args[1], arena)?;
let right_expr = Box::new(args.pop().unwrap());
let left_expr = Box::new(args.pop().unwrap());
return Ok(ScalarExpression::IfNull {
left_expr,
right_expr,
ty,
});
}
"coalesce" => {
let mut ty = LogicalType::SqlNull;
if !args.is_empty() {
ty = args[0].return_type(arena).into_owned();
for arg in args.iter_mut() {
let temp_ty = arg.return_type(arena).into_owned();
if temp_ty == LogicalType::SqlNull {
continue;
}
if ty == LogicalType::SqlNull && temp_ty != LogicalType::SqlNull {
ty = temp_ty;
} else if ty != temp_ty {
ty = LogicalType::max_logical_type(&ty, &temp_ty)?.into_owned();
}
}
}
return Ok(ScalarExpression::Coalesce { exprs: args, ty });
}
_ => (),
}
let arg_types = args
.iter()
.map(|arg| arg.return_type(arena).into_owned())
.collect_vec();
let summary = FunctionSummary {
name: function_name.into(),
arg_types,
};
if let Some(function) = self.context.scala_functions.get(&summary) {
return Ok(ScalarExpression::ScalaFunction(ScalarFunction {
args,
inner: ArcScalarFunctionImpl(function.clone()),
}));
}
if let Some(function) = self.context.table_functions.get(&summary) {
if !matches!(self.context.step_now(), QueryBindStep::From) {
return Err(DatabaseError::UnsupportedStmt(
"`TableFunction` cannot bind in non-From step".to_string(),
));
}
return Ok(ScalarExpression::TableFunction(TableFunction {
args,
catalog: function.clone(),
}));
}
Err(DatabaseError::function_not_found(summary.name.to_string()))
}
pub(crate) fn return_type(
expr_1: &ScalarExpression,
expr_2: &ScalarExpression,
arena: &PlanArena,
) -> Result<LogicalType, DatabaseError> {
let temp_ty_1 = expr_1.return_type(arena);
let temp_ty_2 = expr_2.return_type(arena);
match (temp_ty_1.as_ref(), temp_ty_2.as_ref()) {
(LogicalType::SqlNull, LogicalType::SqlNull) => Ok(LogicalType::SqlNull),
(ty, LogicalType::SqlNull) | (LogicalType::SqlNull, ty) => Ok(ty.clone()),
(ty_1, ty_2) => Ok(LogicalType::max_logical_type(ty_1, ty_2)?.into_owned()),
}
}
pub(crate) fn wildcard_expr() -> ScalarExpression {
ScalarExpression::Constant(DataValue::Utf8 {
value: "*".to_string(),
ty: Utf8Type::Variable(None),
unit: CharLengthUnits::Characters,
})
}
}