Skip to content

Apache DataFusion SQL Query Engine

refactor: Simplify optimizer traversal with `TreeNode::exists()` (#25769)

Yongting Youe120d7cd528f
diff --git a/datafusion/optimizer/src/push_down_filter.rs b/datafusion/optimizer/src/push_down_filter.rs
index d059b2ae7d..1972174a45 100644
--- a/datafusion/optimizer/src/push_down_filter.rs
+++ b/datafusion/optimizer/src/push_down_filter.rs
@@ -26,9 +26,7 @@ use itertools::Itertools;
use log::{Level, debug, log_enabled};
use datafusion_common::instant::Instant;
-use datafusion_common::tree_node::{
- Transformed, TransformedResult, TreeNode, TreeNodeRecursion,
-};
+use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};
use datafusion_common::{
Column, DFSchema, Result, assert_eq_or_internal_err, internal_err, plan_err,
qualified_name,
@@ -233,22 +231,18 @@ impl<'a> ColumnChecker<'a> {
/// Determine whether the predicate can evaluate as the join conditions
fn can_evaluate_as_join_condition(predicate: &Expr) -> Result<bool> {
- let mut is_evaluate = true;
- predicate.apply(|expr| match expr {
- Expr::Column(_)
- | Expr::Literal(_, _)
- | Expr::Placeholder(_)
- | Expr::ScalarVariable(_, _) => Ok(TreeNodeRecursion::Jump),
+ Ok(!predicate.exists(|expr| match expr {
Expr::Exists { .. }
| Expr::InSubquery(_)
| Expr::SetComparison(_)
| Expr::ScalarSubquery(_)
| Expr::OuterReferenceColumn(_, _)
- | Expr::Unnest(_) => {
- is_evaluate = false;
- Ok(TreeNodeRecursion::Stop)
- }
- Expr::Alias(_)
+ | Expr::Unnest(_) => Ok(true),
+ Expr::Column(_)
+ | Expr::Literal(_, _)
+ | Expr::Placeholder(_)
+ | Expr::ScalarVariable(_, _)
+ | Expr::Alias(_)
| Expr::BinaryExpr(_)
| Expr::Like(_)
| Expr::SimilarTo(_)
@@ -270,15 +264,14 @@ fn can_evaluate_as_join_condition(predicate: &Expr) -> Result<bool> {
| Expr::ScalarFunction(_)
| Expr::HigherOrderFunction(_)
| Expr::Lambda(_)
- | Expr::LambdaVariable(_) => Ok(TreeNodeRecursion::Continue),
+ | Expr::LambdaVariable(_) => Ok(false),
// TODO: remove the next line after `Expr::Wildcard` is removed
#[expect(deprecated)]
Expr::AggregateFunction(_)
| Expr::WindowFunction(_)
| Expr::Wildcard { .. }
| Expr::GroupingSet(_) => internal_err!("Unsupported predicate type"),
- })?;
- Ok(is_evaluate)
+ })?)
}
/// examine OR clause to see if any useful clauses can be extracted and push down.
@@ -1512,19 +1505,10 @@ fn unalias(expr: &Expr) -> &Expr {
/// check whether the expression uses the columns in `check_map`.
fn contain<T>(e: &Expr, check_map: &HashMap<String, T>) -> bool {
- let mut is_contain = false;
- e.apply(|expr| {
- if let Expr::Column(c) = &expr
- && check_map.contains_key(&c.flat_name())
- {
- is_contain = true;
- Ok(TreeNodeRecursion::Stop)
- } else {
- Ok(TreeNodeRecursion::Continue)
- }
+ e.exists(|expr| {
+ Ok(matches!(expr, Expr::Column(c) if check_map.contains_key(&c.flat_name())))
})
- .unwrap();
- is_contain
+ .unwrap()
}
fn with_filters(predicates: Vec<Expr>, plan: LogicalPlan) -> LogicalPlan {
diff --git a/datafusion/physical-optimizer/src/projection_pushdown.rs b/datafusion/physical-optimizer/src/projection_pushdown.rs
index fe71c21176..97ec7718b8 100644
--- a/datafusion/physical-optimizer/src/projection_pushdown.rs
+++ b/datafusion/physical-optimizer/src/projection_pushdown.rs
@@ -405,26 +405,17 @@ impl<'a> JoinFilterRewriter<'a> {
Ok(new_intermediate_idx)
}
- /// Checks whether the entire expression depends on the given `join_side`.
+ /// Checks whether the expression references any columns from `join_side`.
fn depends_on_join_side(
- &mut self,
+ &self,
expr: &Arc<dyn PhysicalExpr>,
join_side: JoinSide,
) -> Result<bool> {
- let mut result = false;
- expr.apply(|expr| match expr.downcast_ref::<Column>() {
- None => Ok(TreeNodeRecursion::Continue),
- Some(c) => {
- let column_index = &self.intermediate_column_indices[c.index()];
- if column_index.side == join_side {
- result = true;
- return Ok(TreeNodeRecursion::Stop);
- }
- Ok(TreeNodeRecursion::Continue)
- }
- })?;
-
- Ok(result)
+ expr.exists(|expr| {
+ Ok(expr.downcast_ref::<Column>().is_some_and(|c| {
+ self.intermediate_column_indices[c.index()].side == join_side
+ }))
+ })
}
}