Skip to content

Commit 17e8fa7

Browse files
authored
Remove deprecated function OptimizerRule::try_optimize (apache#15051)
* chore: cleanup deprecated optimizer API since version <= 40 follow up of apache#15027 * chore: inlined `optimize_plan_node` And also removed out dated comment * chore: deprecate `supports_rewrite` function
1 parent 755f9a5 commit 17e8fa7

File tree

4 files changed

+10
-90
lines changed

4 files changed

+10
-90
lines changed

datafusion/optimizer/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ pub use analyzer::{Analyzer, AnalyzerRule};
6969
pub use optimizer::{
7070
ApplyOrder, Optimizer, OptimizerConfig, OptimizerContext, OptimizerRule,
7171
};
72-
#[allow(deprecated)]
73-
pub use utils::optimize_children;
7472

7573
pub(crate) mod join_key_set;
7674
mod plan_signature;

datafusion/optimizer/src/optimizer.rs

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,6 @@ use crate::utils::log_plan;
6969
/// [`AnalyzerRule`]: crate::analyzer::AnalyzerRule
7070
/// [`SessionState::add_optimizer_rule`]: https://docs.rs/datafusion/latest/datafusion/execution/session_state/struct.SessionState.html#method.add_optimizer_rule
7171
pub trait OptimizerRule: Debug {
72-
/// Try and rewrite `plan` to an optimized form, returning None if the plan
73-
/// cannot be optimized by this rule.
74-
///
75-
/// Note this API will be deprecated in the future as it requires `clone`ing
76-
/// the input plan, which can be expensive. OptimizerRules should implement
77-
/// [`Self::rewrite`] instead.
78-
#[deprecated(
79-
since = "40.0.0",
80-
note = "please implement supports_rewrite and rewrite instead"
81-
)]
82-
fn try_optimize(
83-
&self,
84-
_plan: &LogicalPlan,
85-
_config: &dyn OptimizerConfig,
86-
) -> Result<Option<LogicalPlan>> {
87-
internal_err!("Should have called rewrite")
88-
}
89-
9072
/// A human readable name for this optimizer rule
9173
fn name(&self) -> &str;
9274

@@ -99,15 +81,13 @@ pub trait OptimizerRule: Debug {
9981
}
10082

10183
/// Does this rule support rewriting owned plans (rather than by reference)?
84+
#[deprecated(since = "47.0.0", note = "This method is no longer used")]
10285
fn supports_rewrite(&self) -> bool {
10386
true
10487
}
10588

10689
/// Try to rewrite `plan` to an optimized form, returning `Transformed::yes`
10790
/// if the plan was rewritten and `Transformed::no` if it was not.
108-
///
109-
/// Note: this function is only called if [`Self::supports_rewrite`] returns
110-
/// true. Otherwise the Optimizer calls [`Self::try_optimize`]
11191
fn rewrite(
11292
&self,
11393
_plan: LogicalPlan,
@@ -304,43 +284,25 @@ impl TreeNodeRewriter for Rewriter<'_> {
304284

305285
fn f_down(&mut self, node: LogicalPlan) -> Result<Transformed<LogicalPlan>> {
306286
if self.apply_order == ApplyOrder::TopDown {
307-
optimize_plan_node(node, self.rule, self.config)
287+
{
288+
self.rule.rewrite(node, self.config)
289+
}
308290
} else {
309291
Ok(Transformed::no(node))
310292
}
311293
}
312294

313295
fn f_up(&mut self, node: LogicalPlan) -> Result<Transformed<LogicalPlan>> {
314296
if self.apply_order == ApplyOrder::BottomUp {
315-
optimize_plan_node(node, self.rule, self.config)
297+
{
298+
self.rule.rewrite(node, self.config)
299+
}
316300
} else {
317301
Ok(Transformed::no(node))
318302
}
319303
}
320304
}
321305

322-
/// Invokes the Optimizer rule to rewrite the LogicalPlan in place.
323-
fn optimize_plan_node(
324-
plan: LogicalPlan,
325-
rule: &dyn OptimizerRule,
326-
config: &dyn OptimizerConfig,
327-
) -> Result<Transformed<LogicalPlan>> {
328-
if rule.supports_rewrite() {
329-
return rule.rewrite(plan, config);
330-
}
331-
332-
#[allow(deprecated)]
333-
rule.try_optimize(&plan, config).map(|maybe_plan| {
334-
match maybe_plan {
335-
Some(new_plan) => {
336-
// if the node was rewritten by the optimizer, replace the node
337-
Transformed::yes(new_plan)
338-
}
339-
None => Transformed::no(plan),
340-
}
341-
})
342-
}
343-
344306
impl Optimizer {
345307
/// Optimizes the logical plan by applying optimizer rules, and
346308
/// invoking observer function after each call
@@ -386,7 +348,9 @@ impl Optimizer {
386348
&mut Rewriter::new(apply_order, rule.as_ref(), config),
387349
),
388350
// rule handles recursion itself
389-
None => optimize_plan_node(new_plan, rule.as_ref(), config),
351+
None => {
352+
rule.rewrite(new_plan, config)
353+
},
390354
}
391355
.and_then(|tnr| {
392356
// run checks optimizer invariant checks, per optimizer rule applied

datafusion/optimizer/src/simplify_expressions/simplify_exprs.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ impl OptimizerRule for SimplifyExpressions {
6262
true
6363
}
6464

65-
/// if supports_owned returns true, the Optimizer calls
66-
/// [`Self::rewrite`] instead of [`Self::try_optimize`]
6765
fn rewrite(
6866
&self,
6967
plan: LogicalPlan,

datafusion/optimizer/src/utils.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
2020
use std::collections::{BTreeSet, HashMap, HashSet};
2121

22-
use crate::{OptimizerConfig, OptimizerRule};
23-
2422
use crate::analyzer::type_coercion::TypeCoercionRewriter;
2523
use arrow::array::{new_null_array, Array, RecordBatch};
2624
use arrow::datatypes::{DataType, Field, Schema};
@@ -38,44 +36,6 @@ use std::sync::Arc;
3836
/// as it was initially placed here and then moved elsewhere.
3937
pub use datafusion_expr::expr_rewriter::NamePreserver;
4038

41-
/// Convenience rule for writing optimizers: recursively invoke
42-
/// optimize on plan's children and then return a node of the same
43-
/// type. Useful for optimizer rules which want to leave the type
44-
/// of plan unchanged but still apply to the children.
45-
/// This also handles the case when the `plan` is a [`LogicalPlan::Explain`].
46-
///
47-
/// Returning `Ok(None)` indicates that the plan can't be optimized by the `optimizer`.
48-
#[deprecated(
49-
since = "40.0.0",
50-
note = "please use OptimizerRule::apply_order with ApplyOrder::BottomUp instead"
51-
)]
52-
pub fn optimize_children(
53-
optimizer: &impl OptimizerRule,
54-
plan: &LogicalPlan,
55-
config: &dyn OptimizerConfig,
56-
) -> Result<Option<LogicalPlan>> {
57-
let mut new_inputs = Vec::with_capacity(plan.inputs().len());
58-
let mut plan_is_changed = false;
59-
for input in plan.inputs() {
60-
if optimizer.supports_rewrite() {
61-
let new_input = optimizer.rewrite(input.clone(), config)?;
62-
plan_is_changed = plan_is_changed || new_input.transformed;
63-
new_inputs.push(new_input.data);
64-
} else {
65-
#[allow(deprecated)]
66-
let new_input = optimizer.try_optimize(input, config)?;
67-
plan_is_changed = plan_is_changed || new_input.is_some();
68-
new_inputs.push(new_input.unwrap_or_else(|| input.clone()))
69-
}
70-
}
71-
if plan_is_changed {
72-
let exprs = plan.expressions();
73-
plan.with_new_exprs(exprs, new_inputs).map(Some)
74-
} else {
75-
Ok(None)
76-
}
77-
}
78-
7939
/// Returns true if `expr` contains all columns in `schema_cols`
8040
pub(crate) fn has_all_column_refs(expr: &Expr, schema_cols: &HashSet<Column>) -> bool {
8141
let column_refs = expr.column_refs();

0 commit comments

Comments
 (0)