-
Notifications
You must be signed in to change notification settings - Fork 552
/
planner.rs
813 lines (765 loc) · 33.7 KB
/
planner.rs
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use super::super::parser::ast;
use super::super::types::schema::{Column, Table};
use super::super::types::{Expression, Value};
use super::{plan::Node, plan::Plan, Aggregate, Direction};
use crate::errinput;
use crate::error::Result;
use crate::sql::engine::Catalog;
use std::collections::{HashMap, HashSet};
use std::mem::replace;
/// A query plan builder.
pub struct Planner<'a, C: Catalog> {
catalog: &'a C,
}
impl<'a, C: Catalog> Planner<'a, C> {
/// Creates a new planner.
pub fn new(catalog: &'a C) -> Self {
Self { catalog }
}
/// Builds a plan for an AST statement.
pub fn build(&mut self, statement: ast::Statement) -> Result<Plan> {
self.build_statement(statement)
}
/// Builds a plan for a statement.
fn build_statement(&self, statement: ast::Statement) -> Result<Plan> {
Ok(match statement {
// Transaction control and explain statements should have been handled by session.
ast::Statement::Begin { .. } | ast::Statement::Commit | ast::Statement::Rollback => {
panic!("unexpected transaction statement {statement:?}")
}
ast::Statement::Explain(_) => panic!("unexpected explain statement"),
// DDL statements (schema changes).
ast::Statement::CreateTable { name, columns } => {
let Some(primary_key) = columns.iter().position(|c| c.primary_key) else {
return errinput!("no primary key for table {name}");
};
if columns.iter().filter(|c| c.primary_key).count() > 1 {
return errinput!("multiple primary keys for table {name}");
}
let columns = columns
.into_iter()
.map(|c| {
let nullable = c.nullable.unwrap_or(!c.primary_key);
Ok(Column {
name: c.name,
datatype: c.datatype,
nullable,
default: match c.default {
Some(expr) => Some(self.evaluate_constant(expr)?),
None if nullable => Some(Value::Null),
None => None,
},
unique: c.unique || c.primary_key,
index: (c.index || c.unique || c.references.is_some())
&& !c.primary_key,
references: c.references,
})
})
.collect::<Result<_>>()?;
Plan::CreateTable { schema: Table { name, primary_key, columns } }
}
ast::Statement::DropTable { name, if_exists } => {
Plan::DropTable { table: name, if_exists }
}
// DML statements (mutations).
ast::Statement::Delete { table, r#where } => {
let table = self.catalog.must_get_table(&table)?;
let scope = &mut Scope::from_table(table.clone())?;
Plan::Delete {
table: table.name.clone(),
key_index: table.primary_key,
source: Node::Scan {
table,
alias: None,
filter: r#where.map(|e| self.build_expression(scope, e)).transpose()?,
},
}
}
ast::Statement::Insert { table, columns, values } => {
let table = self.catalog.must_get_table(&table)?;
Plan::Insert {
table,
columns: columns.unwrap_or_default(),
expressions: values
.into_iter()
.map(|exprs| {
exprs
.into_iter()
.map(|expr| self.build_expression(&mut Scope::constant(), expr))
.collect::<Result<_>>()
})
.collect::<Result<_>>()?,
}
}
ast::Statement::Update { table, set, r#where } => {
let table = self.catalog.must_get_table(&table)?;
let scope = &mut Scope::from_table(table.clone())?;
Plan::Update {
table: table.name.clone(),
key_index: table.primary_key,
source: Node::Scan {
table,
alias: None,
filter: r#where.map(|e| self.build_expression(scope, e)).transpose()?,
},
expressions: set
.into_iter()
.map(|(c, e)| {
Ok((
scope.resolve(None, &c)?,
Some(c),
self.build_expression(scope, e)?,
))
})
.collect::<Result<_>>()?,
}
}
// Queries.
ast::Statement::Select {
mut select,
from,
r#where,
group_by,
mut having,
mut order,
offset,
limit,
} => {
let scope = &mut Scope::new();
// Build FROM clause.
let mut node = if !from.is_empty() {
self.build_from_clause(scope, from)?
} else if select.is_empty() {
return errinput!("can't select * without a table");
} else {
Node::Nothing
};
// Build WHERE clause.
if let Some(expr) = r#where {
node = Node::Filter {
source: Box::new(node),
predicate: self.build_expression(scope, expr)?,
};
};
// Build SELECT clause.
let mut hidden = 0;
if !select.is_empty() {
// Inject hidden SELECT columns for fields and aggregates used in ORDER BY and
// HAVING expressions but not present in existing SELECT output. These will be
// removed again by a later projection.
if let Some(ref mut expr) = having {
hidden += self.inject_hidden(expr, &mut select)?;
}
for (expr, _) in order.iter_mut() {
hidden += self.inject_hidden(expr, &mut select)?;
}
// Extract any aggregate functions and GROUP BY expressions, replacing them with
// Column placeholders. Aggregations are handled by evaluating group expressions
// and aggregate function arguments in a pre-projection, passing the results
// to an aggregation node, and then evaluating the final SELECT expressions
// in the post-projection. For example:
//
// SELECT (MAX(rating * 100) - MIN(rating * 100)) / 100
// FROM movies
// GROUP BY released - 2000
//
// Results in the following nodes:
//
// - Projection: rating * 100, rating * 100, released - 2000
// - Aggregation: max(#0), min(#1) group by #2
// - Projection: (#0 - #1) / 100
let aggregates = self.extract_aggregates(&mut select)?;
let groups = self.extract_groups(&mut select, group_by, aggregates.len())?;
if !aggregates.is_empty() || !groups.is_empty() {
node = self.build_aggregation(scope, node, groups, aggregates)?;
}
// Build the remaining non-aggregate projection.
let expressions: Vec<(Expression, Option<String>)> = select
.into_iter()
.map(|(e, l)| Ok((self.build_expression(scope, e)?, l)))
.collect::<Result<_>>()?;
scope.project(&expressions)?;
node = Node::Projection { source: Box::new(node), expressions };
};
// Build HAVING clause.
if let Some(expr) = having {
node = Node::Filter {
source: Box::new(node),
predicate: self.build_expression(scope, expr)?,
};
};
// Build ORDER clause.
if !order.is_empty() {
node = Node::Order {
source: Box::new(node),
orders: order
.into_iter()
.map(|(e, o)| {
Ok((
self.build_expression(scope, e)?,
match o {
ast::Order::Ascending => Direction::Ascending,
ast::Order::Descending => Direction::Descending,
},
))
})
.collect::<Result<_>>()?,
};
}
// Build OFFSET clause.
if let Some(expr) = offset {
node = Node::Offset {
source: Box::new(node),
offset: match self.evaluate_constant(expr)? {
Value::Integer(i) if i >= 0 => Ok(i as u64),
v => errinput!("invalid offset {v}"),
}?,
}
}
// Build LIMIT clause.
if let Some(expr) = limit {
node = Node::Limit {
source: Box::new(node),
limit: match self.evaluate_constant(expr)? {
Value::Integer(i) if i >= 0 => Ok(i as u64),
v => errinput!("invalid limit {v}"),
}?,
}
}
// Remove any hidden columns.
if hidden > 0 {
node = Node::Projection {
source: Box::new(node),
expressions: (0..(scope.len() - hidden))
.map(|i| (Expression::Field(i, None), None))
.collect(),
}
}
Plan::Select(node)
}
})
}
/// Builds a FROM clause consisting of several items. Each item is either a single table or a
/// join of an arbitrary number of tables. All of the items are joined, since e.g. 'SELECT * FROM
/// a, b' is an implicit join of a and b.
fn build_from_clause(&self, scope: &mut Scope, from: Vec<ast::FromItem>) -> Result<Node> {
let base_scope = scope.clone();
let mut items = from.into_iter();
let mut node = match items.next() {
Some(item) => self.build_from_item(scope, item)?,
None => return errinput!("no from items given"),
};
for item in items {
let mut right_scope = base_scope.clone();
let right = self.build_from_item(&mut right_scope, item)?;
node = Node::NestedLoopJoin {
left: Box::new(node),
left_size: scope.len(),
right: Box::new(right),
predicate: None,
outer: false,
};
scope.merge(right_scope)?;
}
Ok(node)
}
/// Builds FROM items, which can either be a single table or a chained join of multiple tables,
/// e.g. 'SELECT * FROM a LEFT JOIN b ON b.a_id = a.id'. Any tables will be stored in
/// self.tables keyed by their query name (i.e. alias if given, otherwise name). The table can
/// only be referenced by the query name (so if alias is given, cannot reference by name).
fn build_from_item(&self, scope: &mut Scope, item: ast::FromItem) -> Result<Node> {
Ok(match item {
ast::FromItem::Table { name, alias } => {
let table = self.catalog.must_get_table(&name)?;
scope.add_table(alias.clone().unwrap_or_else(|| name.clone()), table.clone())?;
Node::Scan { table, alias, filter: None }
}
ast::FromItem::Join { left, right, r#type, predicate } => {
// Right outer joins are built as a left outer join with an additional projection
// to swap the resulting columns.
let (left, right) = match r#type {
ast::JoinType::Right => (right, left),
_ => (left, right),
};
let left = Box::new(self.build_from_item(scope, *left)?);
let left_size = scope.len();
let right = Box::new(self.build_from_item(scope, *right)?);
let predicate = predicate.map(|e| self.build_expression(scope, e)).transpose()?;
let outer = match r#type {
ast::JoinType::Cross | ast::JoinType::Inner => false,
ast::JoinType::Left | ast::JoinType::Right => true,
};
let mut node = Node::NestedLoopJoin { left, left_size, right, predicate, outer };
if matches!(r#type, ast::JoinType::Right) {
let expressions = (left_size..scope.len())
.chain(0..left_size)
.map(|i| Ok((Expression::Field(i, scope.get_label(i)?), None)))
.collect::<Result<Vec<_>>>()?;
scope.project(&expressions)?;
node = Node::Projection { source: Box::new(node), expressions }
}
node
}
})
}
/// Builds an aggregation node. All aggregate parameters and GROUP BY expressions are evaluated
/// in a pre-projection, whose results are fed into an Aggregate node. This node computes the
/// aggregates for the given groups, passing the group values through directly.
fn build_aggregation(
&self,
scope: &mut Scope,
source: Node,
groups: Vec<(ast::Expression, Option<String>)>,
aggregations: Vec<(Aggregate, ast::Expression)>,
) -> Result<Node> {
let mut aggregates = Vec::new();
let mut expressions = Vec::new();
for (aggregate, expr) in aggregations {
aggregates.push(aggregate);
expressions.push((self.build_expression(scope, expr)?, None));
}
for (expr, label) in groups {
expressions.push((self.build_expression(scope, expr)?, label));
}
scope.project(
&expressions
.iter()
.cloned()
.enumerate()
.map(|(i, (e, l))| {
if i < aggregates.len() {
// We pass null values here since we don't want field references to hit
// the fields in scope before the aggregation.
(Expression::Constant(Value::Null), None)
} else {
(e, l)
}
})
.collect::<Vec<_>>(),
)?;
let node = Node::Aggregation {
source: Box::new(Node::Projection { source: Box::new(source), expressions }),
aggregates,
};
Ok(node)
}
/// Extracts aggregate functions from an AST expression tree. This finds the aggregate
/// function calls, replaces them with ast::Expression::Column(i), maps the aggregate functions
/// to aggregates, and returns them along with their argument expressions.
fn extract_aggregates(
&self,
exprs: &mut [(ast::Expression, Option<String>)],
) -> Result<Vec<(Aggregate, ast::Expression)>> {
let mut aggregates = Vec::new();
for (expr, _) in exprs {
expr.transform_mut(
&mut |mut e| match &mut e {
ast::Expression::Function(f, args) if args.len() == 1 => {
if let Some(aggregate) = self.aggregate_from_name(f) {
aggregates.push((aggregate, args.remove(0)));
Ok(ast::Expression::Column(aggregates.len() - 1))
} else {
Ok(e)
}
}
_ => Ok(e),
},
&mut Ok,
)?;
}
for (_, expr) in &aggregates {
if self.is_aggregate(expr) {
return errinput!("aggregate functions can't be nested");
}
}
Ok(aggregates)
}
/// Extracts group by expressions, and replaces them with column references with the given
/// offset. These can be either an arbitray expression, a reference to a SELECT column, or the
/// same expression as a SELECT column. The following are all valid:
///
/// SELECT released / 100 AS century, COUNT(*) FROM movies GROUP BY century
/// SELECT released / 100, COUNT(*) FROM movies GROUP BY released / 100
/// SELECT COUNT(*) FROM movies GROUP BY released / 100
fn extract_groups(
&self,
exprs: &mut [(ast::Expression, Option<String>)],
group_by: Vec<ast::Expression>,
offset: usize,
) -> Result<Vec<(ast::Expression, Option<String>)>> {
let mut groups = Vec::new();
for g in group_by {
// Look for references to SELECT columns with AS labels
if let ast::Expression::Field(None, label) = &g {
if let Some(i) = exprs.iter().position(|(_, l)| l.as_deref() == Some(label)) {
groups.push((
replace(&mut exprs[i].0, ast::Expression::Column(offset + groups.len())),
exprs[i].1.clone(),
));
continue;
}
}
// Look for expressions exactly equal to the group expression
if let Some(i) = exprs.iter().position(|(e, _)| e == &g) {
groups.push((
replace(&mut exprs[i].0, ast::Expression::Column(offset + groups.len())),
exprs[i].1.clone(),
));
continue;
}
// Otherwise, just use the group expression directly
groups.push((g, None))
}
// Make sure no group expressions contain Column references, which would be placed here
// during extract_aggregates().
for (expr, _) in &groups {
if self.is_aggregate(expr) {
return errinput!("group expression cannot contain aggregates");
}
}
Ok(groups)
}
/// Injects hidden expressions into SELECT expressions. This is used for ORDER BY and HAVING, in
/// order to apply these to fields or aggregates that are not present in the SELECT output, e.g.
/// to order on a column that is not selected. This is done by replacing the relevant parts of
/// the given expression with Column references to either existing columns or new, hidden
/// columns in the select expressions. Returns the number of hidden columns added.
fn inject_hidden(
&self,
expr: &mut ast::Expression,
select: &mut Vec<(ast::Expression, Option<String>)>,
) -> Result<usize> {
// Replace any identical expressions or label references with column references.
for (i, (sexpr, label)) in select.iter().enumerate() {
if expr == sexpr {
*expr = ast::Expression::Column(i);
continue;
}
if let Some(label) = label {
expr.transform_mut(
&mut |e| match e {
ast::Expression::Field(None, ref l) if l == label => {
Ok(ast::Expression::Column(i))
}
e => Ok(e),
},
&mut Ok,
)?;
}
}
// Any remaining aggregate functions and field references must be extracted as hidden
// columns.
let mut hidden = 0;
expr.transform_mut(
&mut |e| match &e {
ast::Expression::Function(f, a) if self.aggregate_from_name(f).is_some() => {
if let ast::Expression::Column(c) = a[0] {
if self.is_aggregate(&select[c].0) {
return errinput!("aggregate function cannot reference aggregate");
}
}
select.push((e, None));
hidden += 1;
Ok(ast::Expression::Column(select.len() - 1))
}
ast::Expression::Field(_, _) => {
select.push((e, None));
hidden += 1;
Ok(ast::Expression::Column(select.len() - 1))
}
_ => Ok(e),
},
&mut Ok,
)?;
Ok(hidden)
}
/// Returns the aggregate corresponding to the given aggregate function name.
fn aggregate_from_name(&self, name: &str) -> Option<Aggregate> {
match name {
"avg" => Some(Aggregate::Average),
"count" => Some(Aggregate::Count),
"max" => Some(Aggregate::Max),
"min" => Some(Aggregate::Min),
"sum" => Some(Aggregate::Sum),
_ => None,
}
}
/// Checks whether a given expression is an aggregate expression.
fn is_aggregate(&self, expr: &ast::Expression) -> bool {
expr.contains(&|e| match e {
ast::Expression::Function(f, _) => self.aggregate_from_name(f).is_some(),
_ => false,
})
}
/// Builds an expression from an AST expression
#[allow(clippy::only_used_in_recursion)]
fn build_expression(&self, scope: &mut Scope, expr: ast::Expression) -> Result<Expression> {
use Expression::*;
Ok(match expr {
ast::Expression::Literal(l) => Constant(match l {
ast::Literal::Null => Value::Null,
ast::Literal::Boolean(b) => Value::Boolean(b),
ast::Literal::Integer(i) => Value::Integer(i),
ast::Literal::Float(f) => Value::Float(f),
ast::Literal::String(s) => Value::String(s),
}),
ast::Expression::Column(i) => Field(i, scope.get_label(i)?),
ast::Expression::Field(table, name) => {
Field(scope.resolve(table.as_deref(), &name)?, Some((table, name)))
}
ast::Expression::Function(name, _) => return errinput!("unknown function {name}"),
ast::Expression::Operation(op) => match op {
// Logical operators
ast::Operation::And(lhs, rhs) => And(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::Not(expr) => Not(self.build_expression(scope, *expr)?.into()),
ast::Operation::Or(lhs, rhs) => Or(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
// Comparison operators
ast::Operation::Equal(lhs, rhs) => Equal(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::GreaterThan(lhs, rhs) => GreaterThan(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::GreaterThanOrEqual(lhs, rhs) => Or(
GreaterThan(
self.build_expression(scope, *lhs.clone())?.into(),
self.build_expression(scope, *rhs.clone())?.into(),
)
.into(),
Equal(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
)
.into(),
),
ast::Operation::IsNull(expr) => IsNull(self.build_expression(scope, *expr)?.into()),
ast::Operation::LessThan(lhs, rhs) => LessThan(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::LessThanOrEqual(lhs, rhs) => Or(
LessThan(
self.build_expression(scope, *lhs.clone())?.into(),
self.build_expression(scope, *rhs.clone())?.into(),
)
.into(),
Equal(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
)
.into(),
),
ast::Operation::Like(lhs, rhs) => Like(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::NotEqual(lhs, rhs) => Not(Equal(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
)
.into()),
// Mathematical operators
ast::Operation::Assert(expr) => Assert(self.build_expression(scope, *expr)?.into()),
ast::Operation::Add(lhs, rhs) => Add(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::Divide(lhs, rhs) => Divide(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::Exponentiate(lhs, rhs) => Exponentiate(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::Factorial(expr) => {
Factorial(self.build_expression(scope, *expr)?.into())
}
ast::Operation::Modulo(lhs, rhs) => Modulo(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::Multiply(lhs, rhs) => Multiply(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
ast::Operation::Negate(expr) => Negate(self.build_expression(scope, *expr)?.into()),
ast::Operation::Subtract(lhs, rhs) => Subtract(
self.build_expression(scope, *lhs)?.into(),
self.build_expression(scope, *rhs)?.into(),
),
},
})
}
/// Builds and evaluates a constant AST expression.
fn evaluate_constant(&self, expr: ast::Expression) -> Result<Value> {
self.build_expression(&mut Scope::constant(), expr)?.evaluate(None)
}
}
/// Manages names available to expressions and executors, and maps them onto columns/fields.
#[derive(Clone, Debug)]
pub struct Scope {
// If true, the scope is constant and cannot contain any variables.
constant: bool,
// Currently visible tables, by query name (i.e. alias or actual name).
tables: HashMap<String, Table>,
// Column labels, if any (qualified by table name when available)
columns: Vec<(Option<String>, Option<String>)>,
// Qualified names to column indexes.
qualified: HashMap<(String, String), usize>,
// Unqualified names to column indexes, if unique.
unqualified: HashMap<String, usize>,
// Unqialified ambiguous names.
ambiguous: HashSet<String>,
}
impl Scope {
/// Creates a new, empty scope.
fn new() -> Self {
Self {
constant: false,
tables: HashMap::new(),
columns: Vec::new(),
qualified: HashMap::new(),
unqualified: HashMap::new(),
ambiguous: HashSet::new(),
}
}
/// Creates a constant scope.
fn constant() -> Self {
let mut scope = Self::new();
scope.constant = true;
scope
}
/// Creates a scope from a table.
fn from_table(table: Table) -> Result<Self> {
let mut scope = Self::new();
scope.add_table(table.name.clone(), table)?;
Ok(scope)
}
/// Adds a column to the scope.
#[allow(clippy::map_entry)]
fn add_column(&mut self, table: Option<String>, label: Option<String>) {
if let Some(l) = label.clone() {
if let Some(t) = table.clone() {
self.qualified.insert((t, l.clone()), self.columns.len());
}
if !self.ambiguous.contains(&l) {
if !self.unqualified.contains_key(&l) {
self.unqualified.insert(l, self.columns.len());
} else {
self.unqualified.remove(&l);
self.ambiguous.insert(l);
}
}
}
self.columns.push((table, label));
}
/// Adds a table to the scope.
fn add_table(&mut self, label: String, table: Table) -> Result<()> {
if self.constant {
panic!("can't modify constant scope");
}
if self.tables.contains_key(&label) {
return errinput!("duplicate table name {label}");
}
for column in &table.columns {
self.add_column(Some(label.clone()), Some(column.name.clone()));
}
self.tables.insert(label, table);
Ok(())
}
/// Fetches a column from the scope by index.
fn get_column(&self, index: usize) -> Result<(Option<String>, Option<String>)> {
if self.constant {
return errinput!("expression must be constant, found column {index}");
}
self.columns.get(index).cloned().ok_or(errinput!("column index {index} not found"))
}
/// Fetches a column label by index, if any.
fn get_label(&self, index: usize) -> Result<Option<(Option<String>, String)>> {
Ok(match self.get_column(index)? {
(table, Some(name)) => Some((table, name)),
_ => None,
})
}
/// Merges two scopes, by appending the given scope to self.
fn merge(&mut self, scope: Scope) -> Result<()> {
if self.constant {
panic!("can't modify constant scope");
}
for (label, table) in scope.tables {
if self.tables.contains_key(&label) {
return errinput!("duplicate table name {label}");
}
self.tables.insert(label, table);
}
for (table, label) in scope.columns {
self.add_column(table, label);
}
Ok(())
}
/// Resolves a name, optionally qualified by a table name.
fn resolve(&self, table: Option<&str>, name: &str) -> Result<usize> {
if self.constant {
return errinput!(
"expression must be constant, found field {}",
if let Some(table) = table { format!("{}.{}", table, name) } else { name.into() }
);
}
if let Some(table) = table {
if !self.tables.contains_key(table) {
return errinput!("unknown table {table}");
}
self.qualified
.get(&(table.into(), name.into()))
.copied()
.ok_or(errinput!("Unknown field {table}.{name}"))
} else if self.ambiguous.contains(name) {
errinput!("ambiguous field {name}")
} else {
self.unqualified.get(name).copied().ok_or(errinput!("unknown field {name}"))
}
}
/// Number of columns in the current scope.
fn len(&self) -> usize {
self.columns.len()
}
/// Projects the scope. This takes a set of expressions and labels in the current scope,
/// and returns a new scope for the projection.
fn project(&mut self, projection: &[(Expression, Option<String>)]) -> Result<()> {
if self.constant {
panic!("can't modify constant scope");
}
let mut new = Self::new();
new.tables = self.tables.clone();
for (expr, label) in projection {
match (expr, label) {
(_, Some(label)) => new.add_column(None, Some(label.clone())),
(Expression::Field(_, Some((Some(table), name))), _) => {
new.add_column(Some(table.clone()), Some(name.clone()))
}
(Expression::Field(_, Some((None, name))), _) => {
if let Some(i) = self.unqualified.get(name) {
let (table, name) = self.columns[*i].clone();
new.add_column(table, name);
}
}
(Expression::Field(i, None), _) => {
let (table, label) = self.columns.get(*i).cloned().unwrap_or((None, None));
new.add_column(table, label)
}
_ => new.add_column(None, None),
}
}
*self = new;
Ok(())
}
}