ygf11 commented on code in PR #4711:
URL: https://github.com/apache/arrow-datafusion/pull/4711#discussion_r1056751207


##########
datafusion/optimizer/src/extract_equijoin_predicate.rs:
##########
@@ -0,0 +1,438 @@
+// 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.
+
+//! Optimizer rule to extract equijoin expr from filter
+use crate::utils::optimize_children;
+use crate::{OptimizerConfig, OptimizerRule};
+use datafusion_common::DFSchema;
+use datafusion_common::Result;
+use datafusion_expr::utils::{can_hash, check_all_column_from_schema};
+use datafusion_expr::{BinaryExpr, Expr, ExprSchemable, Join, LogicalPlan, 
Operator};
+use std::sync::Arc;
+
+/// Optimization rule that extract equijoin expr from the filter
+#[derive(Default)]
+pub struct ExtractEquijoinPredicate;
+
+impl ExtractEquijoinPredicate {
+    #[allow(missing_docs)]
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl OptimizerRule for ExtractEquijoinPredicate {
+    fn try_optimize(
+        &self,
+        plan: &LogicalPlan,
+        config: &dyn OptimizerConfig,
+    ) -> Result<Option<LogicalPlan>> {
+        match plan {
+            LogicalPlan::Join(Join {
+                left,
+                right,
+                on,
+                filter,
+                join_type,
+                join_constraint,
+                schema,
+                null_equals_null,
+            }) => {
+                let left_schema = left.schema();
+                let right_schema = right.schema();
+
+                let new_on_and_accumu_filter = if let Some(expr) = filter {
+                    let mut accum: Vec<(Expr, Expr)> = vec![];
+                    let mut accum_filter: Vec<Expr> = vec![];
+                    // TODO: avoding clone with split_conjunction
+                    extract_join_keys(
+                        expr.clone(),
+                        &mut accum,
+                        &mut accum_filter,
+                        left_schema,
+                        right_schema,
+                    )?;
+
+                    (!accum.is_empty()).then(|| {
+                        let mut new_on = on.clone();
+                        new_on.extend(accum);
+                        (new_on, accum_filter)
+                    })
+                } else {
+                    None
+                };
+
+                let optimized_left = self.try_optimize(left.as_ref(), config)?;
+                let optimized_right = self.try_optimize(right.as_ref(), 
config)?;
+                let plan_changed = new_on_and_accumu_filter.is_some()
+                    || optimized_left.is_some()
+                    || optimized_right.is_some();
+
+                let plan = plan_changed.then(|| {
+                    let left =
+                        optimized_left.map(Arc::new).unwrap_or_else(|| 
left.clone());
+                    let right = optimized_right
+                        .map(Arc::new)
+                        .unwrap_or_else(|| right.clone());
+                    let (new_on, new_filter) = new_on_and_accumu_filter
+                        .map(|(on, accumu_filter)| {
+                            let filter = 
accumu_filter.into_iter().reduce(Expr::and);
+                            (on, filter)
+                        })
+                        .unwrap_or_else(|| (on.clone(), filter.clone()));
+
+                    LogicalPlan::Join(Join {
+                        left,
+                        right,
+                        on: new_on,
+                        filter: new_filter,
+                        join_type: *join_type,
+                        join_constraint: *join_constraint,
+                        schema: schema.clone(),
+                        null_equals_null: *null_equals_null,
+                    })
+                });
+
+                Ok(plan)
+            }
+            _ => Ok(Some(optimize_children(self, plan, config)?)),

Review Comment:
   Yes, we can do it.
   
   Utilizing the traverse of optimizer now. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to