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/26 03:22:58 UTC

[GitHub] [arrow-datafusion] andygrove opened a new pull request, #2342: WIP: Add `Expr::InSubquery` and `Expr::ScalarSubquery`

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

   # 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.
   -->
   Part of https://github.com/apache/arrow-datafusion/issues/2248
   
    # 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.  
   -->
   
   Add `InSubquery` and `ScalarSubquery` to `Expr` as a step towards supporting SQL queries that use these expressions.
   
   # 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 `InSubquery` and `ScalarSubquery` to `Expr`
   - Add functions to create these expressions
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   Yes, 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] alamb commented on a diff in pull request #2342: Add `Expr::InSubquery` and `Expr::ScalarSubquery`

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


##########
datafusion/expr/src/expr.rs:
##########
@@ -434,7 +450,26 @@ impl fmt::Debug for Expr {
             Expr::Negative(expr) => write!(f, "(- {:?})", expr),
             Expr::IsNull(expr) => write!(f, "{:?} IS NULL", expr),
             Expr::IsNotNull(expr) => write!(f, "{:?} IS NOT NULL", expr),
-            Expr::Exists(subquery) => write!(f, "EXISTS ({:?})", subquery),
+            Expr::Exists {
+                subquery,
+                negated: true,
+            } => write!(f, "NOT EXISTS ({:?})", subquery),
+            Expr::Exists {
+                subquery,
+                negated: false,
+            } => write!(f, "EXISTS ({:?})", subquery),
+            Expr::InSubquery {
+                expr,
+                subquery,
+                negated,
+            } => {
+                if *negated {
+                    write!(f, "{:?} NOT IN ({:?})", expr, subquery)
+                } else {
+                    write!(f, "{:?} IN ({:?})", expr, subquery)
+                }
+            }

Review Comment:
   The same `negated: true` / `negated: false` could be applied to `Expr::InSubquery` as was applied to `Expr::Exists`



-- 
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 #2342: Add `Expr::InSubquery` and `Expr::ScalarSubquery`

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


##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -173,7 +177,13 @@ impl ExprSchemable for Expr {
             | Expr::WindowFunction { .. }
             | Expr::AggregateFunction { .. }
             | Expr::AggregateUDF { .. } => Ok(true),
-            Expr::IsNull(_) | Expr::IsNotNull(_) | Expr::Exists(_) => Ok(false),
+            Expr::IsNull(_)
+            | Expr::IsNotNull(_)
+            | Expr::Exists { .. }
+            | Expr::InSubquery { .. } => Ok(false),

Review Comment:
   Thanks. Fixed.



-- 
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 #2342: Add `Expr::InSubquery` and `Expr::ScalarSubquery`

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


##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -173,7 +177,13 @@ impl ExprSchemable for Expr {
             | Expr::WindowFunction { .. }
             | Expr::AggregateFunction { .. }
             | Expr::AggregateUDF { .. } => Ok(true),
-            Expr::IsNull(_) | Expr::IsNotNull(_) | Expr::Exists(_) => Ok(false),
+            Expr::IsNull(_)
+            | Expr::IsNotNull(_)
+            | Expr::Exists { .. }
+            | Expr::InSubquery { .. } => Ok(false),

Review Comment:
   I think a`IN` can be nullable if its exprs are nullable:
   
   ```sql
   alamb=# select null IN (select * from a);
    ?column? 
   ----------
    
   (1 row)
   ```
   
   So maybe  `Expr::InSubquery` should return `expr.nullable(input_schema)` too
   
   



##########
datafusion/core/src/sql/utils.rs:
##########
@@ -371,7 +371,17 @@ where
             Expr::Column { .. }
             | Expr::Literal(_)
             | Expr::ScalarVariable(_, _)
-            | Expr::Exists(_) => Ok(expr.clone()),
+            | Expr::Exists { .. }
+            | Expr::ScalarSubquery(_) => Ok(expr.clone()),
+            Expr::InSubquery {
+                expr: nested_expr,
+                subquery,
+                negated,
+            } => Ok(Expr::InSubquery {
+                expr: Box::new(clone_with_replacement(&**nested_expr, replacement_fn)?),

Review Comment:
   👍 



##########
datafusion/expr/src/expr.rs:
##########
@@ -622,7 +656,11 @@ fn create_name(e: &Expr, input_schema: &DFSchema) -> Result<String> {
             let expr = create_name(expr, input_schema)?;
             Ok(format!("{} IS NOT NULL", expr))
         }
-        Expr::Exists(_) => Ok("EXISTS".to_string()),
+        Expr::Exists { .. } => Ok("EXISTS".to_string()),
+        Expr::InSubquery { .. } => Ok("IN".to_string()),

Review Comment:
   should this also take into account `negated`? 
   
   ```suggestion
           Expr::Exists { negated: true, .. } => Ok("NOT EXISTS".to_string()),
           Expr::Exists { negated: false, .. } => Ok("EXISTS".to_string()),
           Expr::InSubquery { negated: true, .. } => Ok("NOT IN".to_string()),
           Expr::InSubquery { negated: false, .. } => Ok("IN".to_string()),
   ```



##########
datafusion/core/src/optimizer/simplify_expressions.rs:
##########
@@ -375,7 +375,9 @@ impl<'a> ConstEvaluator<'a> {
             | Expr::AggregateUDF { .. }
             | Expr::ScalarVariable(_, _)
             | Expr::Column(_)
-            | Expr::Exists(_)
+            | Expr::Exists { .. }
+            | Expr::InSubquery { .. }
+            | Expr::ScalarSubquery(_)

Review Comment:
   at some point it would be cool to be able to evaluate scalar subqueries at plan time, "but not now" 😆 



##########
datafusion/expr/src/expr.rs:
##########
@@ -434,7 +450,25 @@ impl fmt::Debug for Expr {
             Expr::Negative(expr) => write!(f, "(- {:?})", expr),
             Expr::IsNull(expr) => write!(f, "{:?} IS NULL", expr),
             Expr::IsNotNull(expr) => write!(f, "{:?} IS NOT NULL", expr),
-            Expr::Exists(subquery) => write!(f, "EXISTS ({:?})", subquery),
+            Expr::Exists { subquery, negated } => {
+                if *negated {
+                    write!(f, "NOT EXISTS ({:?})", subquery)
+                } else {
+                    write!(f, "EXISTS ({:?})", subquery)
+                }
+            }

Review Comment:
   I am not sure if you think this is cleaner, but you could potentially use multiple pattern matches like this (untested)
   
   ```suggestion
               Expr::Exists { subquery, negated: true } => write!(f, "NOT EXISTS ({:?})", subquery),
               Expr::Exists { subquery, negated: false } => write!(f, "EXISTS ({:?})", subquery),
   ```
   
   The same comment applies for `Expr::InSubquery`



-- 
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 pull request #2342: Add `Expr::InSubquery` and `Expr::ScalarSubquery`

Posted by GitBox <gi...@apache.org>.
alamb commented on PR #2342:
URL: https://github.com/apache/arrow-datafusion/pull/2342#issuecomment-1110838834

   🎉 


-- 
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 #2342: Add `Expr::InSubquery` and `Expr::ScalarSubquery`

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


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