Skip to content

Commit 52f3a54

Browse files
committed
Rewrite Optimizer to use TreeNode API
1 parent e570e89 commit 52f3a54

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
@@ -57,6 +57,9 @@ pub trait TreeNode: Sized {
5757
/// Visit the tree node using the given [`TreeNodeVisitor`], performing a
5858
/// depth-first walk of the node and its children.
5959
///
60+
/// See also:
61+
/// * [`Self::rewrite`] to rewrite owned `TreeNode`s
62+
///
6063
/// Consider the following tree structure:
6164
/// ```text
6265
/// ParentNode
@@ -94,6 +97,9 @@ pub trait TreeNode: Sized {
9497
/// Implements the [visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern) for
9598
/// recursively transforming [`TreeNode`]s.
9699
///
100+
/// See also:
101+
/// * [`Self::visit`] for inspecting (without modification) `TreeNode`s
102+
///
97103
/// Consider the following tree structure:
98104
/// ```text
99105
/// ParentNode
@@ -294,13 +300,15 @@ pub trait TreeNode: Sized {
294300
}
295301

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

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

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

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

18971897
// optimize the child plan, capturing the output of each optimizer
18981898
let optimized_plan = self.optimizer.optimize(
1899-
&analyzed_plan,
1899+
analyzed_plan,
19001900
self,
19011901
|optimized_plan, optimizer| {
19021902
let optimizer_name = optimizer.name().to_string();
@@ -1926,7 +1926,7 @@ impl SessionState {
19261926
let analyzed_plan =
19271927
self.analyzer
19281928
.execute_and_check(plan, self.options(), |_, _| {})?;
1929-
self.optimizer.optimize(&analyzed_plan, self, |_, _| {})
1929+
self.optimizer.optimize(analyzed_plan, self, |_, _| {})
19301930
}
19311931
}
19321932

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)