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 19:32:54 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2342: Add `Expr::InSubquery` and `Expr::ScalarSubquery`

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