You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/25 21:13:07 UTC

[GitHub] [arrow-datafusion] andygrove opened a new pull request, #2339: Add `Expr::Exists` to represent EXISTS subquery expression

andygrove opened a new pull request, #2339:
URL: https://github.com/apache/arrow-datafusion/pull/2339

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes https://github.com/apache/arrow-datafusion/issues/2337
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   This is the first step towards supporting `EXISTS` subquery expressions. This work is partly based on work by @paveltiunov in https://github.com/cube-js/arrow-datafusion/commit/d14f0de4c042c9a14b40acd4768c6e4f6585ee93
   
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   - Add `Expr::Exists(Subquery)` expression
   - Update optimizer rules
   - Add test that builds a plan with `EXISTS` using `LogicalPlanBuilder`
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   API change.
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


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


[GitHub] [arrow-datafusion] andygrove commented on a diff in pull request #2339: Add `Expr::Exists` to represent EXISTS subquery expression

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2339:
URL: https://github.com/apache/arrow-datafusion/pull/2339#discussion_r858070915


##########
datafusion/core/src/optimizer/utils.rs:
##########
@@ -223,6 +225,17 @@ pub fn from_plan(
             let right = &inputs[1];
             LogicalPlanBuilder::from(left).cross_join(right)?.build()
         }
+        LogicalPlan::Subquery(Subquery { .. }) => {
+            // let schema = inputs[0].schema().as_ref().clone().into();
+            // let schema =
+            //     DFSchemaRef::new(DFSchema::try_from_qualified_schema(alias, &schema)?);
+            // Ok(LogicalPlan::Subquery(Subquery {
+            //     subquery
+            //     input: Arc::new(inputs[0].clone()),
+            //     schema,
+            // }))
+            Err(DataFusionError::Plan("not implemented".to_string()))

Review Comment:
   I have now implemented this section of code, although it is not exercised by any tests yet



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


[GitHub] [arrow-datafusion] andygrove merged pull request #2339: Add `Expr::Exists` to represent EXISTS subquery expression

Posted by GitBox <gi...@apache.org>.
andygrove merged PR #2339:
URL: https://github.com/apache/arrow-datafusion/pull/2339


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


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2339: Add `Expr::Exists` to represent EXISTS subquery expression

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2339:
URL: https://github.com/apache/arrow-datafusion/pull/2339#discussion_r858047148


##########
datafusion/expr/src/expr_fn.rs:
##########
@@ -180,6 +182,13 @@ pub fn approx_percentile_cont_with_weight(
     }
 }
 
+/// Create an EXISTS subquery expression
+pub fn exists(subquery: LogicalPlan) -> Expr {

Review Comment:
   I wonder if this function should also be added to the prelude: https://github.com/apache/arrow-datafusion/blob/b890190a6521ab1e572184dcf6ea0a5afb3a47af/datafusion/core/src/prelude.rs#L33-L40



##########
datafusion/core/src/logical_plan/builder.rs:
##########
@@ -1339,6 +1341,31 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn exists_subquery() -> Result<()> {
+        let foo = test_table_scan_with_name("foo")?;
+        let bar = test_table_scan_with_name("bar")?;
+
+        let subquery = LogicalPlanBuilder::from(foo)
+            .project(vec![col("a")])?
+            .filter(col("a").eq(col("bar.a")))?
+            .build()?;
+
+        let outer_query = LogicalPlanBuilder::from(bar)
+            .project(vec![col("a")])?
+            .filter(exists(subquery))?

Review Comment:
   👌 



##########
datafusion/core/src/physical_plan/planner.rs:
##########
@@ -186,6 +186,9 @@ fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result<String> {
                 Ok(format!("{} IN ({:?})", expr, list))
             }
         }
+        Expr::Exists(_) => Err(DataFusionError::NotImplemented(
+            "EXISTS is not supported in the physical plan".to_string(),

Review Comment:
   ```suggestion
               "EXISTS is not yet supported in the physical plan".to_string(),
   ```



##########
datafusion/core/src/optimizer/utils.rs:
##########
@@ -223,6 +225,17 @@ pub fn from_plan(
             let right = &inputs[1];
             LogicalPlanBuilder::from(left).cross_join(right)?.build()
         }
+        LogicalPlan::Subquery(Subquery { .. }) => {
+            // let schema = inputs[0].schema().as_ref().clone().into();
+            // let schema =
+            //     DFSchemaRef::new(DFSchema::try_from_qualified_schema(alias, &schema)?);
+            // Ok(LogicalPlan::Subquery(Subquery {
+            //     subquery
+            //     input: Arc::new(inputs[0].clone()),
+            //     schema,
+            // }))
+            Err(DataFusionError::Plan("not implemented".to_string()))

Review Comment:
   ```suggestion
               Err(DataFusionError::Plan("subquery support is not full implemented yet".to_string()))
   ```



##########
datafusion/expr/src/expr_fn.rs:
##########
@@ -180,6 +182,13 @@ pub fn approx_percentile_cont_with_weight(
     }
 }
 
+/// Create an EXISTS subquery expression
+pub fn exists(subquery: LogicalPlan) -> Expr {

Review Comment:
   Maybe this function could take an `Arc<LogicalPlan>` so if someone already has one they can reuse it rather than making a copy to pass in here



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