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


##########
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:
   if the subquery has some nested subquery underneath, i believe this function 
won't be able to return the outer_ref_columns from lower level.
   For example
   ```
   where column1=(select count(*) from inner_table_lv1 lv1 where 
lv2.column2=lv0.column2 and exists (
     select * from inner_table_lv2 lv2 where lv2.column1=lv1.column1 and 
lv2.column2=lv0.column3
   )
   ```
   In this case, the calls to `subquery.outer_ref_columns` will only returns 
`lv0.column2`, while the general framework needs to be aware of `lv0.column3` 
as well



##########
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 {

Review Comment:
   here are more cases i can think of:
   
   1. a predicate can be a complex expressions such as 
   ```
    where column1=(scalar_subquery) or column2=(exists_subquery)
   ```
   2. In this case 2 nested dependent join will be generated
   
   and the scalar exprs sometimes is not the direct child of the predicate for 
example
   ```
   where column1 > 1 + (subquery)
   ```
   3. We can have 2 subqueries in the same binary expr
   ```
   where (subquery1) > (subquery2) + 1
   ```



##########
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)))],

Review Comment:
   nit: we have shorter syntax: 
   ```
   use datafusion_expr::lit;
   let some_exprs = vec![lit(true)];
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to