irenjj commented on code in PR #16174:
URL: https://github.com/apache/datafusion/pull/16174#discussion_r2105792100


##########
datafusion/optimizer/src/create_dependent_join.rs:
##########
@@ -0,0 +1,163 @@
+// 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.
+
+use datafusion_common::tree_node::Transformed;
+use datafusion_common::{Result, ScalarValue};
+use datafusion_expr::{Expr, JoinType, LogicalPlan, LogicalPlanBuilder, 
Subquery};
+
+use crate::{ApplyOrder, OptimizerConfig, OptimizerRule};
+
+/// (temporary) OPtimizer rule for rewriting current plan with
+/// DependentJoin to jj
+#[derive(Default, Debug)]
+pub struct CreateDependentJoin {}
+
+impl CreateDependentJoin {
+    #[allow(missing_docs)]
+    pub fn new() -> Self {
+        Self::default()
+    }
+}
+
+impl OptimizerRule for CreateDependentJoin {
+    fn supports_rewrite(&self) -> bool {
+        true
+    }
+
+    fn name(&self) -> &str {
+        "create_dependent_join"
+    }
+
+    fn apply_order(&self) -> Option<ApplyOrder> {
+        Some(ApplyOrder::TopDown)
+    }
+
+    fn rewrite(
+        &self,
+        plan: LogicalPlan,
+        _config: &dyn OptimizerConfig,
+    ) -> Result<Transformed<LogicalPlan>> {
+        if let LogicalPlan::Filter(ref filter) = plan {
+            match &filter.predicate {
+                Expr::BinaryExpr(binary) => {
+                    // Check if right hand side is a scalar subquery
+                    if let Expr::ScalarSubquery(subquery) = 
binary.right.as_ref() {
+                        let new_plan = build_dependent_join(
+                            subquery,
+                            filter.input.as_ref().clone(),
+                            JoinType::Left,
+                        )?;
+                        return Ok(Transformed::yes(new_plan));
+                    }
+                    // Continue searching in children if no subquery found
+                    return Ok(Transformed::no(plan));
+                }
+                _ => {
+                    // TODO: add other type of subqueries.
+                    return Ok(Transformed::no(plan));
+                }
+            }
+        }
+
+        // No Filter found, continue searching in children
+        Ok(Transformed::no(plan))
+    }
+}
+
+fn build_dependent_join(
+    subquery: &Subquery,
+    root: LogicalPlan,
+    join_type: JoinType,
+) -> Result<LogicalPlan> {
+    let subquery_plan = (subquery.subquery).as_ref().clone();
+
+    let new_plan = LogicalPlanBuilder::from(root)
+        .dependent_join_on(
+            subquery_plan,
+            join_type,
+            vec![Expr::Literal(ScalarValue::Boolean(Some(true)))],
+            subquery.outer_ref_columns.clone(),

Review Comment:
   > i wonder would it be more simple to let the decorrelation optimizor aware 
of the depth and handle recursion itself 🤔
   
   Since there are multiple optimizer rules, I'm wondering if the depth will 
change because of other priority rules rewrite.🤔



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to