Skip to content

Commit 2d5e154

Browse files
committed
Rewrite Optimizer to use TreeNode API
1 parent 12d4a8c commit 2d5e154

26 files changed

+527
-561
lines changed

datafusion-examples/examples/rewrite_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn main() -> Result<()> {
5959

6060
// then run the optimizer with our custom rule
6161
let optimizer = Optimizer::with_rules(vec![Arc::new(MyOptimizerRule {})]);
62-
let optimized_plan = optimizer.optimize(&analyzed_plan, &config, observe)?;
62+
let optimized_plan = optimizer.optimize(analyzed_plan, &config, observe)?;
6363
println!(
6464
"Optimized Logical Plan:\n\n{}\n",
6565
optimized_plan.display_indent()

datafusion/common/src/tree_node.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub trait TreeNode: Sized {
5656
/// Visit the tree node using the given [`TreeNodeVisitor`], performing a
5757
/// depth-first walk of the node and its children.
5858
///
59+
/// See also:
60+
/// * [`Self::rewrite`] to rewrite owned `TreeNode`s
61+
///
5962
/// Consider the following tree structure:
6063
/// ```text
6164
/// ParentNode
@@ -93,6 +96,9 @@ pub trait TreeNode: Sized {
9396
/// Implements the [visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) for
9497
/// recursively transforming [`TreeNode`]s.
9598
///
99+
/// See also:
100+
/// * [`Self::visit`] for inspecting (without modification) `TreeNode`s
101+
///
96102
/// Consider the following tree structure:
97103
/// ```text
98104
/// ParentNode
@@ -293,13 +299,15 @@ pub trait TreeNode: Sized {
293299
}
294300

295301
/// Apply the closure `F` to the node's children.
302+
///
303+
/// See `mutate_children` for rewriting in place
296304
fn apply_children<F: FnMut(&Self) -> Result<TreeNodeRecursion>>(
297305
&self,
298306
f: F,
299307
) -> Result<TreeNodeRecursion>;
300308

301-
/// Apply transform `F` to the node's children. Note that the transform `F`
302-
/// might have a direction (pre-order or post-order).
309+
/// Apply transform `F` to potentially rewrite the node's children. Note
310+
/// that the transform `F` might have a direction (pre-order or post-order).
303311
fn map_children<F: FnMut(Self) -> Result<Transformed<Self>>>(
304312
self,
305313
f: F,

datafusion/core/src/execution/context/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ impl SessionState {
18811881

18821882
// optimize the child plan, capturing the output of each optimizer
18831883
let optimized_plan = self.optimizer.optimize(
1884-
&analyzed_plan,
1884+
analyzed_plan,
18851885
self,
18861886
|optimized_plan, optimizer| {
18871887
let optimizer_name = optimizer.name().to_string();
@@ -1911,7 +1911,7 @@ impl SessionState {
19111911
let analyzed_plan =
19121912
self.analyzer
19131913
.execute_and_check(plan, self.options(), |_, _| {})?;
1914-
self.optimizer.optimize(&analyzed_plan, self, |_, _| {})
1914+
self.optimizer.optimize(analyzed_plan, self, |_, _| {})
19151915
}
19161916
}
19171917

datafusion/core/tests/optimizer_integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn test_sql(sql: &str) -> Result<LogicalPlan> {
110110
let optimizer = Optimizer::new();
111111
// analyze and optimize the logical plan
112112
let plan = analyzer.execute_and_check(&plan, config.options(), |_, _| {})?;
113-
optimizer.optimize(&plan, &config, |_, _| {})
113+
optimizer.optimize(plan, &config, |_, _| {})
114114
}
115115

116116
#[derive(Default)]

0 commit comments

Comments
 (0)