-
Notifications
You must be signed in to change notification settings - Fork 18
/
into_optd.rs
370 lines (353 loc) · 15.6 KB
/
into_optd.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
use anyhow::{bail, Result};
use datafusion::{
common::DFSchema,
logical_expr::{self, logical_plan, LogicalPlan, Operator},
scalar::ScalarValue,
};
use datafusion_expr::Expr as DFExpr;
use optd_core::rel_node::RelNode;
use optd_datafusion_repr::plan_nodes::{
BetweenExpr, BinOpExpr, BinOpType, CastExpr, ColumnRefExpr, ConstantExpr, Expr, ExprList,
FuncExpr, FuncType, JoinType, LikeExpr, LogOpExpr, LogOpType, LogicalAgg, LogicalEmptyRelation,
LogicalFilter, LogicalJoin, LogicalLimit, LogicalProjection, LogicalScan, LogicalSort,
OptRelNode, OptRelNodeRef, OptRelNodeTyp, PlanNode, SortOrderExpr, SortOrderType,
};
use crate::OptdPlanContext;
impl OptdPlanContext<'_> {
fn conv_into_optd_table_scan(&mut self, node: &logical_plan::TableScan) -> Result<PlanNode> {
let table_name = node.table_name.to_string();
if node.fetch.is_some() {
bail!("fetch")
}
if !node.filters.is_empty() {
bail!("no filters")
}
self.tables.insert(table_name.clone(), node.source.clone());
let scan = LogicalScan::new(table_name);
if let Some(ref projection) = node.projection {
let mut exprs = Vec::with_capacity(projection.len());
for &p in projection {
exprs.push(ColumnRefExpr::new(p).into_expr());
}
let projection = LogicalProjection::new(scan.into_plan_node(), ExprList::new(exprs));
return Ok(projection.into_plan_node());
}
Ok(scan.into_plan_node())
}
fn conv_into_optd_expr(
&mut self,
expr: &logical_expr::Expr,
context: &DFSchema,
) -> Result<Expr> {
use logical_expr::Expr;
match expr {
Expr::BinaryExpr(node) => {
let left = self.conv_into_optd_expr(node.left.as_ref(), context)?;
let right = self.conv_into_optd_expr(node.right.as_ref(), context)?;
let op = match node.op {
Operator::Eq => BinOpType::Eq,
Operator::NotEq => BinOpType::Neq,
Operator::LtEq => BinOpType::Leq,
Operator::Lt => BinOpType::Lt,
Operator::GtEq => BinOpType::Geq,
Operator::Gt => BinOpType::Gt,
Operator::And => BinOpType::And,
Operator::Or => BinOpType::Or,
Operator::Plus => BinOpType::Add,
Operator::Minus => BinOpType::Sub,
Operator::Multiply => BinOpType::Mul,
Operator::Divide => BinOpType::Div,
op => unimplemented!("{}", op),
};
Ok(BinOpExpr::new(left, right, op).into_expr())
}
Expr::Column(col) => {
let idx = context.index_of_column(col)?;
Ok(ColumnRefExpr::new(idx).into_expr())
}
Expr::Literal(x) => match x {
ScalarValue::UInt8(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::uint8(*x).into_expr())
}
ScalarValue::UInt16(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::uint16(*x).into_expr())
}
ScalarValue::UInt32(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::uint32(*x).into_expr())
}
ScalarValue::UInt64(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::uint64(*x).into_expr())
}
ScalarValue::Int8(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::int8(*x).into_expr())
}
ScalarValue::Int16(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::int16(*x).into_expr())
}
ScalarValue::Int32(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::int32(*x).into_expr())
}
ScalarValue::Int64(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::int64(*x).into_expr())
}
ScalarValue::Float64(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::float64(*x).into_expr())
}
ScalarValue::Utf8(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::string(x).into_expr())
}
ScalarValue::Date32(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::date(*x as i64).into_expr())
}
ScalarValue::Decimal128(x, _, _) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::decimal(*x as f64).into_expr())
}
ScalarValue::Boolean(x) => {
let x = x.as_ref().unwrap();
Ok(ConstantExpr::bool(*x).into_expr())
}
_ => bail!("{:?}", x),
},
Expr::Alias(x) => self.conv_into_optd_expr(x.expr.as_ref(), context),
Expr::ScalarFunction(x) => {
let args = self.conv_into_optd_expr_list(&x.args, context)?;
Ok(FuncExpr::new(FuncType::new_scalar(x.fun), args).into_expr())
}
Expr::AggregateFunction(x) => {
let args = self.conv_into_optd_expr_list(&x.args, context)?;
Ok(FuncExpr::new(FuncType::new_agg(x.fun.clone()), args).into_expr())
}
Expr::Case(x) => {
let when_then_expr = &x.when_then_expr;
assert_eq!(when_then_expr.len(), 1);
let (when_expr, then_expr) = &when_then_expr[0];
let when_expr = self.conv_into_optd_expr(when_expr, context)?;
let then_expr = self.conv_into_optd_expr(then_expr, context)?;
let else_expr = self.conv_into_optd_expr(x.else_expr.as_ref().unwrap(), context)?;
assert!(x.expr.is_none());
Ok(FuncExpr::new(
FuncType::Case,
ExprList::new(vec![when_expr, then_expr, else_expr]),
)
.into_expr())
}
Expr::Sort(x) => {
let expr = self.conv_into_optd_expr(x.expr.as_ref(), context)?;
Ok(SortOrderExpr::new(
if x.asc {
SortOrderType::Asc
} else {
SortOrderType::Desc
},
expr,
)
.into_expr())
}
Expr::Between(x) => {
let expr = self.conv_into_optd_expr(x.expr.as_ref(), context)?;
let low = self.conv_into_optd_expr(x.low.as_ref(), context)?;
let high = self.conv_into_optd_expr(x.high.as_ref(), context)?;
assert!(!x.negated, "unimplemented");
Ok(BetweenExpr::new(expr, low, high).into_expr())
}
Expr::Cast(x) => {
let expr = self.conv_into_optd_expr(x.expr.as_ref(), context)?;
Ok(CastExpr::new(expr, x.data_type.clone()).into_expr())
}
Expr::Like(x) => {
let expr = self.conv_into_optd_expr(x.expr.as_ref(), context)?;
let pattern = self.conv_into_optd_expr(x.pattern.as_ref(), context)?;
Ok(LikeExpr::new(x.negated, x.case_insensitive, expr, pattern).into_expr())
}
_ => bail!("Unsupported expression: {:?}", expr),
}
}
fn conv_into_optd_projection(
&mut self,
node: &logical_plan::Projection,
) -> Result<LogicalProjection> {
let input = self.conv_into_optd_plan_node(node.input.as_ref())?;
let expr_list = self.conv_into_optd_expr_list(&node.expr, node.input.schema())?;
Ok(LogicalProjection::new(input, expr_list))
}
fn conv_into_optd_filter(&mut self, node: &logical_plan::Filter) -> Result<LogicalFilter> {
let input = self.conv_into_optd_plan_node(node.input.as_ref())?;
let expr = self.conv_into_optd_expr(&node.predicate, node.input.schema())?;
Ok(LogicalFilter::new(input, expr))
}
fn conv_into_optd_expr_list(
&mut self,
exprs: &[logical_expr::Expr],
context: &DFSchema,
) -> Result<ExprList> {
let exprs = exprs
.iter()
.map(|expr| self.conv_into_optd_expr(expr, context))
.collect::<Result<Vec<_>>>()?;
Ok(ExprList::new(exprs))
}
fn conv_into_optd_sort(&mut self, node: &logical_plan::Sort) -> Result<LogicalSort> {
let input = self.conv_into_optd_plan_node(node.input.as_ref())?;
let expr_list = self.conv_into_optd_expr_list(&node.expr, node.input.schema())?;
Ok(LogicalSort::new(input, expr_list))
}
fn conv_into_optd_agg(&mut self, node: &logical_plan::Aggregate) -> Result<LogicalAgg> {
let input = self.conv_into_optd_plan_node(node.input.as_ref())?;
let agg_exprs = self.conv_into_optd_expr_list(&node.aggr_expr, node.input.schema())?;
let group_exprs = self.conv_into_optd_expr_list(&node.group_expr, node.input.schema())?;
Ok(LogicalAgg::new(input, agg_exprs, group_exprs))
}
fn add_column_offset(offset: usize, expr: Expr) -> Expr {
if expr.typ() == OptRelNodeTyp::ColumnRef {
let expr = ColumnRefExpr::from_rel_node(expr.into_rel_node()).unwrap();
return ColumnRefExpr::new(expr.index() + offset).into_expr();
}
let rel_node = expr.into_rel_node();
let children = rel_node
.children
.iter()
.map(|child| {
let child = child.clone();
let child = Expr::from_rel_node(child).unwrap();
let child = Self::add_column_offset(offset, child);
child.into_rel_node()
})
.collect();
Expr::from_rel_node(
RelNode {
typ: rel_node.typ.clone(),
children,
data: rel_node.data.clone(),
}
.into(),
)
.unwrap()
}
fn conv_into_optd_join(&mut self, node: &logical_plan::Join) -> Result<LogicalJoin> {
use logical_plan::JoinType as DFJoinType;
let left = self.conv_into_optd_plan_node(node.left.as_ref())?;
let right = self.conv_into_optd_plan_node(node.right.as_ref())?;
let join_type = match node.join_type {
DFJoinType::Inner => JoinType::Inner,
DFJoinType::Left => JoinType::LeftOuter,
DFJoinType::Right => JoinType::RightOuter,
DFJoinType::Full => JoinType::FullOuter,
DFJoinType::LeftAnti => JoinType::LeftAnti,
DFJoinType::RightAnti => JoinType::RightAnti,
DFJoinType::LeftSemi => JoinType::LeftSemi,
DFJoinType::RightSemi => JoinType::RightSemi,
};
let mut log_ops = Vec::with_capacity(node.on.len());
for (left, right) in &node.on {
let left = self.conv_into_optd_expr(left, node.left.schema())?;
let right = self.conv_into_optd_expr(right, node.right.schema())?;
let right = Self::add_column_offset(node.left.schema().fields().len(), right);
let op = BinOpType::Eq;
let expr = BinOpExpr::new(left, right, op).into_expr();
log_ops.push(expr);
}
if log_ops.is_empty() {
// optd currently only supports
// 1. normal equal condition join
// select * from a join b on a.id = b.id
// 2. join on false/true
// select * from a join b on false/true
// 3. join on other literals or other filters are not supported
// instead of converting them to a join on true, we bail out
match node.filter {
Some(DFExpr::Literal(ScalarValue::Boolean(Some(val)))) => Ok(LogicalJoin::new(
left,
right,
ConstantExpr::bool(val).into_expr(),
join_type,
)),
None => Ok(LogicalJoin::new(
left,
right,
ConstantExpr::bool(true).into_expr(),
join_type,
)),
_ => bail!("unsupported join filter: {:?}", node.filter),
}
} else if log_ops.len() == 1 {
Ok(LogicalJoin::new(left, right, log_ops.remove(0), join_type))
} else {
let expr_list = ExprList::new(log_ops);
Ok(LogicalJoin::new(
left,
right,
LogOpExpr::new(LogOpType::And, expr_list).into_expr(),
join_type,
))
}
}
fn conv_into_optd_cross_join(&mut self, node: &logical_plan::CrossJoin) -> Result<LogicalJoin> {
let left = self.conv_into_optd_plan_node(node.left.as_ref())?;
let right = self.conv_into_optd_plan_node(node.right.as_ref())?;
Ok(LogicalJoin::new(
left,
right,
ConstantExpr::bool(true).into_expr(),
JoinType::Cross,
))
}
fn conv_into_optd_empty_relation(
&mut self,
node: &logical_plan::EmptyRelation,
) -> Result<LogicalEmptyRelation> {
Ok(LogicalEmptyRelation::new(node.produce_one_row))
}
fn conv_into_optd_limit(&mut self, node: &logical_plan::Limit) -> Result<LogicalLimit> {
let input = self.conv_into_optd_plan_node(node.input.as_ref())?;
// try_into guys are converting usize to u64.
let converted_skip = node.skip.try_into().unwrap();
let converted_fetch = if let Some(x) = node.fetch {
x.try_into().unwrap()
} else {
u64::MAX // u64 MAX represents infinity (not the best way to do this)
};
Ok(LogicalLimit::new(
input,
ConstantExpr::uint64(converted_skip).into_expr(),
ConstantExpr::uint64(converted_fetch).into_expr(),
))
}
fn conv_into_optd_plan_node(&mut self, node: &LogicalPlan) -> Result<PlanNode> {
let node = match node {
LogicalPlan::TableScan(node) => self.conv_into_optd_table_scan(node)?.into_plan_node(),
LogicalPlan::Projection(node) => self.conv_into_optd_projection(node)?.into_plan_node(),
LogicalPlan::Sort(node) => self.conv_into_optd_sort(node)?.into_plan_node(),
LogicalPlan::Aggregate(node) => self.conv_into_optd_agg(node)?.into_plan_node(),
LogicalPlan::SubqueryAlias(node) => {
self.conv_into_optd_plan_node(node.input.as_ref())?
}
LogicalPlan::Join(node) => self.conv_into_optd_join(node)?.into_plan_node(),
LogicalPlan::Filter(node) => self.conv_into_optd_filter(node)?.into_plan_node(),
LogicalPlan::CrossJoin(node) => self.conv_into_optd_cross_join(node)?.into_plan_node(),
LogicalPlan::EmptyRelation(node) => {
self.conv_into_optd_empty_relation(node)?.into_plan_node()
}
LogicalPlan::Limit(node) => self.conv_into_optd_limit(node)?.into_plan_node(),
_ => bail!(
"unsupported plan node: {}",
format!("{:?}", node).split('\n').next().unwrap()
),
};
Ok(node)
}
pub fn conv_into_optd(&mut self, root_rel: &LogicalPlan) -> Result<OptRelNodeRef> {
Ok(self.conv_into_optd_plan_node(root_rel)?.into_rel_node())
}
}