-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
optimizer_rule.rs
225 lines (202 loc) · 7.24 KB
/
optimizer_rule.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
// 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 arrow::array::{ArrayRef, Int32Array, RecordBatch, StringArray};
use arrow_schema::DataType;
use datafusion::prelude::SessionContext;
use datafusion_common::tree_node::{Transformed, TreeNode};
use datafusion_common::{assert_batches_eq, Result, ScalarValue};
use datafusion_expr::{
BinaryExpr, ColumnarValue, Expr, LogicalPlan, Operator, ScalarUDF, ScalarUDFImpl,
Signature, Volatility,
};
use datafusion_optimizer::optimizer::ApplyOrder;
use datafusion_optimizer::{OptimizerConfig, OptimizerRule};
use std::any::Any;
use std::sync::Arc;
/// This example demonstrates how to add your own [`OptimizerRule`]
/// to DataFusion.
///
/// [`OptimizerRule`]s transform [`LogicalPlan`]s into an equivalent (but
/// hopefully faster) form.
///
/// See [analyzer_rule.rs] for an example of AnalyzerRules, which are for
/// changing plan semantics.
#[tokio::main]
pub async fn main() -> Result<()> {
// DataFusion includes many built in OptimizerRules for tasks such as outer
// to inner join conversion and constant folding.
//
// Note you can change the order of optimizer rules using the lower level
// `SessionState` API
let ctx = SessionContext::new();
ctx.add_optimizer_rule(Arc::new(MyOptimizerRule {}));
// Now, let's plan and run queries with the new rule
ctx.register_batch("person", person_batch())?;
let sql = "SELECT * FROM person WHERE age = 22";
let plan = ctx.sql(sql).await?.into_optimized_plan()?;
// We can see the effect of our rewrite on the output plan that the filter
// has been rewritten to `my_eq`
assert_eq!(
plan.display_indent().to_string(),
"Filter: my_eq(person.age, Int32(22))\
\n TableScan: person projection=[name, age]"
);
// The query below doesn't respect a filter `where age = 22` because
// the plan has been rewritten using UDF which returns always true
//
// And the output verifies the predicates have been changed (as the my_eq
// function always returns true)
assert_batches_eq!(
[
"+--------+-----+",
"| name | age |",
"+--------+-----+",
"| Andy | 11 |",
"| Andrew | 22 |",
"| Oleks | 33 |",
"+--------+-----+",
],
&ctx.sql(sql).await?.collect().await?
);
// however we can see the rule doesn't trigger for queries with predicates
// other than `=`
assert_batches_eq!(
[
"+-------+-----+",
"| name | age |",
"+-------+-----+",
"| Andy | 11 |",
"| Oleks | 33 |",
"+-------+-----+",
],
&ctx.sql("SELECT * FROM person WHERE age <> 22")
.await?
.collect()
.await?
);
Ok(())
}
/// An example OptimizerRule that replaces all `col = <const>` predicates with a
/// user defined function
#[derive(Default, Debug)]
struct MyOptimizerRule {}
impl OptimizerRule for MyOptimizerRule {
fn name(&self) -> &str {
"my_optimizer_rule"
}
// New OptimizerRules should use the "rewrite" api as it is more efficient
fn supports_rewrite(&self) -> bool {
true
}
/// Ask the optimizer to handle the plan recursion. `rewrite` will be called
/// on each plan node.
fn apply_order(&self) -> Option<ApplyOrder> {
Some(ApplyOrder::BottomUp)
}
fn rewrite(
&self,
plan: LogicalPlan,
_config: &dyn OptimizerConfig,
) -> Result<Transformed<LogicalPlan>> {
plan.map_expressions(|expr| {
// This closure is called for all expressions in the current plan
//
// For example, given a plan like `SELECT a + b, 5 + 10`
//
// The closure would be called twice:
// 1. once for `a + b`
// 2. once for `5 + 10`
self.rewrite_expr(expr)
})
}
}
impl MyOptimizerRule {
/// Rewrites an Expr replacing all `<col> = <const>` expressions with
/// a call to my_eq udf
fn rewrite_expr(&self, expr: Expr) -> Result<Transformed<Expr>> {
// do a bottom up rewrite of the expression tree
expr.transform_up(|expr| {
// Closure called for each sub tree
match expr {
Expr::BinaryExpr(binary_expr) if is_binary_eq(&binary_expr) => {
// destructure the expression
let BinaryExpr { left, op: _, right } = binary_expr;
// rewrite to `my_eq(left, right)`
let udf = ScalarUDF::new_from_impl(MyEq::new());
let call = udf.call(vec![*left, *right]);
Ok(Transformed::yes(call))
}
_ => Ok(Transformed::no(expr)),
}
})
// Note that the TreeNode API handles propagating the transformed flag
// and errors up the call chain
}
}
/// return true of the expression is an equality expression for a literal or
/// column reference
fn is_binary_eq(binary_expr: &BinaryExpr) -> bool {
binary_expr.op == Operator::Eq
&& is_lit_or_col(binary_expr.left.as_ref())
&& is_lit_or_col(binary_expr.right.as_ref())
}
/// Return true if the expression is a literal or column reference
fn is_lit_or_col(expr: &Expr) -> bool {
matches!(expr, Expr::Column(_) | Expr::Literal(_))
}
/// A simple user defined filter function
#[derive(Debug, Clone)]
struct MyEq {
signature: Signature,
}
impl MyEq {
fn new() -> Self {
Self {
signature: Signature::any(2, Volatility::Stable),
}
}
}
impl ScalarUDFImpl for MyEq {
fn as_any(&self) -> &dyn Any {
self
}
fn name(&self) -> &str {
"my_eq"
}
fn signature(&self) -> &Signature {
&self.signature
}
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::Boolean)
}
fn invoke_batch(
&self,
_args: &[ColumnarValue],
_number_rows: usize,
) -> Result<ColumnarValue> {
// this example simply returns "true" which is not what a real
// implementation would do.
Ok(ColumnarValue::Scalar(ScalarValue::from(true)))
}
}
/// Return a RecordBatch with made up data
fn person_batch() -> RecordBatch {
let name: ArrayRef =
Arc::new(StringArray::from_iter_values(["Andy", "Andrew", "Oleks"]));
let age: ArrayRef = Arc::new(Int32Array::from(vec![11, 22, 33]));
RecordBatch::try_from_iter(vec![("name", name), ("age", age)]).unwrap()
}