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
// 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.
//! Trait to make LogicalPlan rewritable
use datafusion_common::Result;
use datafusion_expr::LogicalPlan;
/// a Trait for marking tree node types that are rewritable
pub trait TreeNodeRewritable: Clone {
/// Transform the tree node using the given [TreeNodeRewriter]
/// It performs a depth first walk of an node and its children.
///
/// For an node tree such as
/// ```text
/// ParentNode
/// left: ChildNode1
/// right: ChildNode2
/// ```
///
/// The nodes are visited using the following order
/// ```text
/// pre_visit(ParentNode)
/// pre_visit(ChildNode1)
/// mutate(ChildNode1)
/// pre_visit(ChildNode2)
/// mutate(ChildNode2)
/// mutate(ParentNode)
/// ```
///
/// If an Err result is returned, recursion is stopped immediately
///
/// If [`false`] is returned on a call to pre_visit, no
/// children of that node are visited, nor is mutate
/// called on that node
///
fn transform_using<R: TreeNodeRewriter<Self>>(
self,
rewriter: &mut R,
) -> Result<Self> {
let need_mutate = match rewriter.pre_visit(&self)? {
RewriteRecursion::Mutate => return rewriter.mutate(self),
RewriteRecursion::Stop => return Ok(self),
RewriteRecursion::Continue => true,
RewriteRecursion::Skip => false,
};
let after_op_children =
self.map_children(|node| node.transform_using(rewriter))?;
// now rewrite this node itself
if need_mutate {
rewriter.mutate(after_op_children)
} else {
Ok(after_op_children)
}
}
/// Convenience utils for writing optimizers rule: recursively apply the given `op` to the node tree.
/// When `op` does not apply to a given node, it is left unchanged.
/// The default tree traversal direction is transform_up(Postorder Traversal).
fn transform<F>(self, op: &F) -> Result<Self>
where
F: Fn(Self) -> Result<Option<Self>>,
{
self.transform_up(op)
}
/// Convenience utils for writing optimizers rule: recursively apply the given 'op' to the node and all of its
/// children(Preorder Traversal).
/// When the `op` does not apply to a given node, it is left unchanged.
fn transform_down<F>(self, op: &F) -> Result<Self>
where
F: Fn(Self) -> Result<Option<Self>>,
{
let node_cloned = self.clone();
let after_op = match op(node_cloned)? {
Some(value) => value,
None => self,
};
after_op.map_children(|node| node.transform_down(op))
}
/// Convenience utils for writing optimizers rule: recursively apply the given 'op' first to all of its
/// children and then itself(Postorder Traversal).
/// When the `op` does not apply to a given node, it is left unchanged.
fn transform_up<F>(self, op: &F) -> Result<Self>
where
F: Fn(Self) -> Result<Option<Self>>,
{
let after_op_children = self.map_children(|node| node.transform_up(op))?;
let after_op_children_clone = after_op_children.clone();
let new_node = match op(after_op_children)? {
Some(value) => value,
None => after_op_children_clone,
};
Ok(new_node)
}
/// Apply transform `F` to the node's children, the transform `F` might have a direction(Preorder or Postorder)
fn map_children<F>(self, transform: F) -> Result<Self>
where
F: FnMut(Self) -> Result<Self>;
/// Apply the given function `func` to this node and recursively apply to the node's children
fn for_each<F>(&self, func: &F) -> Result<()>
where
F: Fn(&Self) -> Result<()>,
{
func(self)?;
self.apply_children(|node| node.for_each(func))
}
/// Recursively apply the given function `func` to the node's children and to this node
fn for_each_up<F>(&self, func: &F) -> Result<()>
where
F: Fn(&Self) -> Result<()>,
{
self.apply_children(|node| node.for_each_up(func))?;
func(self)
}
/// Apply the given function `func` to the node's children
fn apply_children<F>(&self, func: F) -> Result<()>
where
F: Fn(&Self) -> Result<()>;
}
/// Trait for potentially recursively transform an [`TreeNodeRewritable`] node
/// tree. When passed to `TreeNodeRewritable::transform_using`, `TreeNodeRewriter::mutate` is
/// invoked recursively on all nodes of a tree.
pub trait TreeNodeRewriter<N: TreeNodeRewritable>: Sized {
/// Invoked before (Preorder) any children of `node` are rewritten /
/// visited. Default implementation returns `Ok(RewriteRecursion::Continue)`
fn pre_visit(&mut self, _node: &N) -> Result<RewriteRecursion> {
Ok(RewriteRecursion::Continue)
}
/// Invoked after (Postorder) all children of `node` have been mutated and
/// returns a potentially modified node.
fn mutate(&mut self, node: N) -> Result<N>;
}
/// Controls how the [TreeNodeRewriter] recursion should proceed.
#[allow(dead_code)]
pub enum RewriteRecursion {
/// Continue rewrite / visit this node tree.
Continue,
/// Call 'op' immediately and return.
Mutate,
/// Do not rewrite / visit the children of this node.
Stop,
/// Keep recursive but skip apply op on this node
Skip,
}
impl TreeNodeRewritable for LogicalPlan {
fn map_children<F>(self, transform: F) -> Result<Self>
where
F: FnMut(Self) -> Result<Self>,
{
let children = self.inputs().into_iter().cloned().collect::<Vec<_>>();
if !children.is_empty() {
let new_children: Result<Vec<_>> =
children.into_iter().map(transform).collect();
self.with_new_inputs(new_children?.as_slice())
} else {
Ok(self)
}
}
fn apply_children<F>(&self, func: F) -> Result<()>
where
F: Fn(&Self) -> Result<()>,
{
let children = self.inputs();
if !children.is_empty() {
children.into_iter().try_for_each(func)
} else {
Ok(())
}
}
}