-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathmod.rs
292 lines (257 loc) · 10.2 KB
/
mod.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
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0.
use egg::{define_language, Id, Symbol};
use crate::binder::copy::ExtSource;
use crate::binder::{CreateFunction, CreateIndex, CreateTable};
use crate::catalog::{ColumnRefId, TableRefId};
use crate::parser::{BinaryOperator, UnaryOperator};
use crate::types::{ColumnIndex, DataType, DataValue, DateTimeField};
mod cost;
mod explain;
mod optimizer;
mod rules;
pub use explain::Explain;
pub use optimizer::{Config, Optimizer};
pub use rules::{ExprAnalysis, Statistics, TypeError, TypeSchemaAnalysis};
// Alias types for our language.
type EGraph = egg::EGraph<Expr, ExprAnalysis>;
type Rewrite = egg::Rewrite<Expr, ExprAnalysis>;
type Pattern = egg::Pattern<Expr>;
pub type RecExpr = egg::RecExpr<Expr>;
define_language! {
pub enum Expr {
// values
Constant(DataValue), // null, true, 1, 1.0, "hello", ...
Type(DataType), // BOOLEAN, INT, DECIMAL(5), ...
Column(ColumnRefId), // $1.2, $2.1, ...
Table(TableRefId), // $1, $2, ...
ColumnIndex(ColumnIndex), // #0, #1, ...
// utilities
"ref" = Ref(Id), // (ref expr)
// refer the expr as a column
// it can also prevent optimization
"list" = List(Box<[Id]>), // (list ...)
// binary operations
"+" = Add([Id; 2]),
"-" = Sub([Id; 2]),
"*" = Mul([Id; 2]),
"/" = Div([Id; 2]),
"%" = Mod([Id; 2]),
"||" = StringConcat([Id; 2]),
">" = Gt([Id; 2]),
"<" = Lt([Id; 2]),
">=" = GtEq([Id; 2]),
"<=" = LtEq([Id; 2]),
"=" = Eq([Id; 2]),
"<>" = NotEq([Id; 2]),
"and" = And([Id; 2]),
"or" = Or([Id; 2]),
"xor" = Xor([Id; 2]),
"like" = Like([Id; 2]),
// unary operations
"-" = Neg(Id),
"not" = Not(Id),
"isnull" = IsNull(Id),
"if" = If([Id; 3]), // (if cond then else)
// functions
"extract" = Extract([Id; 2]), // (extract field expr)
Field(DateTimeField),
"replace" = Replace([Id; 3]), // (replace expr pattern replacement)
"substring" = Substring([Id; 3]), // (substring expr start length)
// vector functions
"<->" = VectorL2Distance([Id; 2]),
"<#>" = VectorNegtiveInnerProduct([Id; 2]),
"<=>" = VectorCosineDistance([Id; 2]),
// aggregations
"max" = Max(Id),
"min" = Min(Id),
"sum" = Sum(Id),
"avg" = Avg(Id),
"count" = Count(Id),
"count-distinct" = CountDistinct(Id),
"rowcount" = RowCount,
"first" = First(Id),
"last" = Last(Id),
// window functions
"over" = Over([Id; 3]), // (over window_function [partition_key..] [order_key..])
// TODO: support frame clause
// "range" = Range([Id; 2]), // (range start end)
"row_number" = RowNumber,
// subquery related
"exists" = Exists(Id), // (exists plan)
"in" = In([Id; 2]), // (in expr plan)
"cast" = Cast([Id; 2]), // (cast type expr)
// plans
"scan" = Scan([Id; 3]), // (scan table [column..] filter)
"values" = Values(Box<[Id]>), // (values [expr..]..)
"proj" = Proj([Id; 2]), // (proj [expr..] child)
"filter" = Filter([Id; 2]), // (filter expr child)
"order" = Order([Id; 2]), // (order [order_key..] child)
"desc" = Desc(Id), // (desc key)
"limit" = Limit([Id; 3]), // (limit limit offset child)
"topn" = TopN([Id; 4]), // (topn limit offset [order_key..] child)
"join" = Join([Id; 4]), // (join join_type cond left right)
"hashjoin" = HashJoin([Id; 6]), // (hashjoin join_type cond [lkey..] [rkey..] left right)
"mergejoin" = MergeJoin([Id; 6]), // (mergejoin join_type cond [lkey..] [rkey..] left right)
"apply" = Apply([Id; 3]), // (apply type left right)
"inner" = Inner,
"left_outer" = LeftOuter,
"right_outer" = RightOuter,
"full_outer" = FullOuter,
"semi" = Semi,
"anti" = Anti,
"agg" = Agg([Id; 2]), // (agg aggs=[expr..] child)
// expressions must be aggregate functions
"hashagg" = HashAgg([Id; 3]), // (hashagg keys=[expr..] aggs=[expr..] child)
// output = keys || aggs
"sortagg" = SortAgg([Id; 3]), // (sortagg keys=[expr..] aggs=[expr..] child)
// child must be ordered by keys
"window" = Window([Id; 2]), // (window [over..] child)
// output = child || exprs
CreateTable(Box<CreateTable>),
CreateIndex(Box<CreateIndex>),
"create_view" = CreateView([Id; 2]), // (create_view create_table child)
CreateFunction(CreateFunction),
"drop" = Drop(Id), // (drop [table..])
"insert" = Insert([Id; 3]), // (insert table [column..] child)
"delete" = Delete([Id; 2]), // (delete table child)
"copy_from" = CopyFrom([Id; 2]), // (copy_from dest types)
"copy_to" = CopyTo([Id; 2]), // (copy_to dest child)
ExtSource(Box<ExtSource>),
"explain" = Explain(Id), // (explain child)
"analyze" = Analyze(Id), // (analyze child)
"pragma" = Pragma([Id; 2]), // (pragma name value)
"set" = Set([Id; 2]), // (set name value)
// internal functions
"empty" = Empty(Id), // (empty child)
// returns empty chunk
// with the same schema as `child`
"max1row" = Max1Row(Id), // (max1row child)
// convert table to scalar
Symbol(Symbol),
}
}
impl Expr {
pub const fn true_() -> Self {
Self::Constant(DataValue::Bool(true))
}
pub const fn null() -> Self {
Self::Constant(DataValue::Null)
}
pub const fn zero() -> Self {
Self::Constant(DataValue::Int32(0))
}
pub fn as_const(&self) -> DataValue {
let Self::Constant(v) = self else {
panic!("not a constant: {self}")
};
v.clone()
}
pub fn as_list(&self) -> &[Id] {
let Self::List(l) = self else {
panic!("not a list: {self}")
};
l
}
pub fn as_column(&self) -> ColumnRefId {
let Self::Column(c) = self else {
panic!("not a columnn: {self}")
};
*c
}
pub fn as_table(&self) -> TableRefId {
let Self::Table(t) = self else {
panic!("not a table: {self}")
};
*t
}
pub fn as_type(&self) -> &DataType {
let Self::Type(t) = self else {
panic!("not a type: {self}")
};
t
}
pub fn as_create_table(&self) -> Box<CreateTable> {
let Self::CreateTable(v) = self else {
panic!("not a create table: {self}")
};
v.clone()
}
pub fn as_ext_source(&self) -> ExtSource {
let Self::ExtSource(v) = self else {
panic!("not an external source: {self}")
};
*v.clone()
}
pub const fn binary_op(&self) -> Option<(BinaryOperator, Id, Id)> {
use BinaryOperator as Op;
#[allow(clippy::match_ref_pats)]
Some(match self {
&Self::Add([a, b]) => (Op::Plus, a, b),
&Self::Sub([a, b]) => (Op::Minus, a, b),
&Self::Mul([a, b]) => (Op::Multiply, a, b),
&Self::Div([a, b]) => (Op::Divide, a, b),
&Self::Mod([a, b]) => (Op::Modulo, a, b),
&Self::StringConcat([a, b]) => (Op::StringConcat, a, b),
&Self::Gt([a, b]) => (Op::Gt, a, b),
&Self::Lt([a, b]) => (Op::Lt, a, b),
&Self::GtEq([a, b]) => (Op::GtEq, a, b),
&Self::LtEq([a, b]) => (Op::LtEq, a, b),
&Self::Eq([a, b]) => (Op::Eq, a, b),
&Self::NotEq([a, b]) => (Op::NotEq, a, b),
&Self::And([a, b]) => (Op::And, a, b),
&Self::Or([a, b]) => (Op::Or, a, b),
&Self::Xor([a, b]) => (Op::Xor, a, b),
_ => return None,
})
}
pub const fn unary_op(&self) -> Option<(UnaryOperator, Id)> {
use UnaryOperator as Op;
#[allow(clippy::match_ref_pats)]
Some(match self {
&Self::Neg(a) => (Op::Minus, a),
&Self::Not(a) => (Op::Not, a),
_ => return None,
})
}
pub const fn is_aggregate_function(&self) -> bool {
use Expr::*;
matches!(
self,
RowCount
| Max(_)
| Min(_)
| Sum(_)
| Avg(_)
| Count(_)
| CountDistinct(_)
| First(_)
| Last(_)
)
}
pub const fn is_window_function(&self) -> bool {
use Expr::*;
matches!(self, RowNumber) || self.is_aggregate_function()
}
}
trait ExprExt {
fn as_list(&self) -> &[Id];
fn as_column(&self) -> ColumnRefId;
}
impl<D> ExprExt for egg::EClass<Expr, D> {
fn as_list(&self) -> &[Id] {
self.iter()
.find_map(|e| match e {
Expr::List(list) => Some(list),
_ => None,
})
.expect("not a list")
}
fn as_column(&self) -> ColumnRefId {
self.iter()
.find_map(|e| match e {
Expr::Column(cid) => Some(*cid),
_ => None,
})
.expect("not a column")
}
}