alamb commented on a change in pull request #1208:
URL: https://github.com/apache/arrow-datafusion/pull/1208#discussion_r740501402



##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -210,6 +210,74 @@ impl<'a> ExprRewriter for Simplifier<'a> {
                         right,
                     },
                 },
+                Operator::Or => match (left.as_ref(), right.as_ref()) {
+                    (
+                        Expr::Literal(ScalarValue::Boolean(l)),
+                        Expr::Literal(ScalarValue::Boolean(r)),
+                    ) => match (l, r) {
+                        (Some(l), Some(r)) => {
+                            Expr::Literal(ScalarValue::Boolean(Some(*l || *r)))
+                        }
+                        _ => Expr::Literal(ScalarValue::Boolean(None)),
+                    },
+                    (Expr::Literal(ScalarValue::Boolean(b)), _)
+                        if self.is_boolean_type(&right) =>

Review comment:
       One example is if someone writes `col_a OR true` and `col_a` has type of 
`int64` or something -- I don't know how far such an expression will get (it 
will likely error out when trying to to actually run the `PhysicalExpr`)

##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -210,6 +210,74 @@ impl<'a> ExprRewriter for Simplifier<'a> {
                         right,
                     },
                 },
+                Operator::Or => match (left.as_ref(), right.as_ref()) {
+                    (
+                        Expr::Literal(ScalarValue::Boolean(l)),
+                        Expr::Literal(ScalarValue::Boolean(r)),
+                    ) => match (l, r) {
+                        (Some(l), Some(r)) => {
+                            Expr::Literal(ScalarValue::Boolean(Some(*l || *r)))
+                        }
+                        _ => Expr::Literal(ScalarValue::Boolean(None)),
+                    },
+                    (Expr::Literal(ScalarValue::Boolean(b)), _)
+                        if self.is_boolean_type(&right) =>
+                    {
+                        match b {
+                            Some(true) => 
Expr::Literal(ScalarValue::Boolean(Some(true))),

Review comment:
       so cool!

##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -210,6 +210,74 @@ impl<'a> ExprRewriter for Simplifier<'a> {
                         right,
                     },
                 },
+                Operator::Or => match (left.as_ref(), right.as_ref()) {
+                    (
+                        Expr::Literal(ScalarValue::Boolean(l)),
+                        Expr::Literal(ScalarValue::Boolean(r)),

Review comment:
       The  `ScalarValue` == `ScalarValue` case is covered by `ConstEvaluator` 
-- so while it is not incorrect to add it here, I also don't think it is 
necessary (and might be confusing as it would imply `ConstEvaluator` can't 
handle this.

##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -210,6 +210,74 @@ impl<'a> ExprRewriter for Simplifier<'a> {
                         right,
                     },
                 },
+                Operator::Or => match (left.as_ref(), right.as_ref()) {
+                    (
+                        Expr::Literal(ScalarValue::Boolean(l)),
+                        Expr::Literal(ScalarValue::Boolean(r)),
+                    ) => match (l, r) {
+                        (Some(l), Some(r)) => {
+                            Expr::Literal(ScalarValue::Boolean(Some(*l || *r)))
+                        }
+                        _ => Expr::Literal(ScalarValue::Boolean(None)),
+                    },
+                    (Expr::Literal(ScalarValue::Boolean(b)), _)
+                        if self.is_boolean_type(&right) =>
+                    {
+                        match b {
+                            Some(true) => 
Expr::Literal(ScalarValue::Boolean(Some(true))),

Review comment:
       I think this can match `null` as well -- aka  
`ScalarValue::Boolean(None)` 
   
   because `null OR true` == `true`
   
   ```
   alamb=# select null or true;
    ?column? 
   ----------
    t
   (1 row)
   
   ```

##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -210,6 +210,74 @@ impl<'a> ExprRewriter for Simplifier<'a> {
                         right,
                     },
                 },
+                Operator::Or => match (left.as_ref(), right.as_ref()) {
+                    (
+                        Expr::Literal(ScalarValue::Boolean(l)),
+                        Expr::Literal(ScalarValue::Boolean(r)),
+                    ) => match (l, r) {
+                        (Some(l), Some(r)) => {
+                            Expr::Literal(ScalarValue::Boolean(Some(*l || *r)))
+                        }
+                        _ => Expr::Literal(ScalarValue::Boolean(None)),
+                    },
+                    (Expr::Literal(ScalarValue::Boolean(b)), _)
+                        if self.is_boolean_type(&right) =>
+                    {
+                        match b {
+                            Some(true) => 
Expr::Literal(ScalarValue::Boolean(Some(true))),
+                            _ => *right,
+                        }
+                    }
+                    (_, Expr::Literal(ScalarValue::Boolean(b)))
+                        if self.is_boolean_type(&left) =>
+                    {
+                        match b {
+                            Some(true) => 
Expr::Literal(ScalarValue::Boolean(Some(true))),
+                            _ => *left,
+                        }
+                    }
+                    _ => Expr::BinaryExpr {
+                        left,
+                        op: Operator::Or,
+                        right,
+                    },
+                },
+                Operator::And => match (left.as_ref(), right.as_ref()) {

Review comment:
       The rules for null and AND are `NULL AND true` --> `NULL`, and `NULL AND 
false` --> `false`
   
   ```
   alamb=# select null and true;
    ?column? 
   ----------
    
   (1 row)
   
   alamb=# select null and false;
    ?column? 
   ----------
    f
   (1 row)
   ```

##########
File path: datafusion/src/optimizer/constant_folding.rs
##########
@@ -210,6 +210,74 @@ impl<'a> ExprRewriter for Simplifier<'a> {
                         right,
                     },
                 },
+                Operator::Or => match (left.as_ref(), right.as_ref()) {
+                    (
+                        Expr::Literal(ScalarValue::Boolean(l)),
+                        Expr::Literal(ScalarValue::Boolean(r)),
+                    ) => match (l, r) {
+                        (Some(l), Some(r)) => {
+                            Expr::Literal(ScalarValue::Boolean(Some(*l || *r)))
+                        }
+                        _ => Expr::Literal(ScalarValue::Boolean(None)),
+                    },
+                    (Expr::Literal(ScalarValue::Boolean(b)), _)
+                        if self.is_boolean_type(&right) =>
+                    {
+                        match b {
+                            Some(true) => 
Expr::Literal(ScalarValue::Boolean(Some(true))),
+                            _ => *right,
+                        }
+                    }
+                    (_, Expr::Literal(ScalarValue::Boolean(b)))
+                        if self.is_boolean_type(&left) =>
+                    {
+                        match b {
+                            Some(true) => 
Expr::Literal(ScalarValue::Boolean(Some(true))),

Review comment:
       We could also add the rules that ` null OR false --> null`
   
   ```
   alamb=# select null or false;
    ?column? 
   ----------
    
   (1 row)
   ```




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


Reply via email to