You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "ygf11 (via GitHub)" <gi...@apache.org> on 2023/03/17 08:58:03 UTC

[GitHub] [arrow-datafusion] ygf11 commented on a diff in pull request #5593: Add OuterReferenceColumn to Expr to represent correlated expression

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


##########
datafusion/sql/src/expr/subquery.rs:
##########
@@ -30,13 +30,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         input_schema: &DFSchema,
         planner_context: &mut PlannerContext,
     ) -> Result<Expr> {
+        let old_outer_query_schema = planner_context.outer_query_schema.clone();
+        planner_context.outer_query_schema = Some(input_schema.clone());
+        let sub_plan = self.query_to_plan(subquery, planner_context)?;
+        let outer_ref_columns = sub_plan.all_out_ref_exprs();
+        planner_context.outer_query_schema = old_outer_query_schema;

Review Comment:
   Can we support the following subquery in the future? 
   ```sql
   # three level
   # the innermost(`t3`) reference outermost(`t1`) column
   SELECT *
   FROM t1
   WHERE EXISTS (
   	SELECT *
   	FROM t2
   	WHERE t1_id = t2_id
   		AND EXISTS (
   			SELECT *
   			FROM t3
   			WHERE t3_id > t1_id
   		)
   );
   ```
   
   I just think the optimizer rule you plan to add(https://github.com/apache/arrow-datafusion/issues/5492) can also optimize it.



##########
datafusion/expr/src/logical_plan/plan.rs:
##########
@@ -1919,14 +1947,11 @@ impl Join {
 pub struct Subquery {
     /// The subquery
     pub subquery: Arc<LogicalPlan>,
+    /// The outer references used in the subquery
+    pub outer_ref_columns: Vec<Expr>,

Review Comment:
   👍 This field can help to check if it is a correlated subquery.



##########
datafusion/expr/src/expr_fn.rs:
##########
@@ -267,15 +267,21 @@ pub fn approx_percentile_cont_with_weight(
 /// Create an EXISTS subquery expression
 pub fn exists(subquery: Arc<LogicalPlan>) -> Expr {
     Expr::Exists {
-        subquery: Subquery { subquery },
+        subquery: Subquery {
+            subquery,
+            outer_ref_columns: vec![],
+        },

Review Comment:
   Maybe we can add `outer_ref_columns` as argument?
   I find some tests depend it, and we can cover more test(I can help add this).



-- 
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-unsubscribe@arrow.apache.org

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