Dandandan commented on code in PR #11534:
URL: https://github.com/apache/datafusion/pull/11534#discussion_r1683928562


##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -256,6 +300,36 @@ impl CaseExpr {
 
         Ok(ColumnarValue::Array(current_value))
     }
+
+    /// This function evaluates the specialized case of:
+    ///
+    /// CASE WHEN condition THEN column
+    ///      [ELSE NULL]
+    /// END
+    fn case_column_or_null(&self, batch: &RecordBatch) -> 
Result<ColumnarValue> {
+        let when_expr = &self.when_then_expr[0].0;
+        let then_expr = &self.when_then_expr[0].1;
+        if let ColumnarValue::Array(bit_mask) = when_expr.evaluate(batch)? {
+            let bit_mask = bit_mask
+                .as_any()
+                .downcast_ref::<BooleanArray>()
+                .expect("predicate should evaluate to a boolean array");
+            // invert the bitmask
+            let bit_mask = not(bit_mask)?;

Review Comment:
   As a further optimization I think the when predicate can be optimized to be 
`not(condition)` so it's possible to use `bit_mask` directly instead of 
`not(bit_mask)`. 
   This makes it possible to optimize/simplify a condition like `x=1` to `x!=1` 
instead of `not(x=1)`, saving the invert. 



##########
datafusion/physical-expr/src/expressions/case.rs:
##########
@@ -256,6 +300,36 @@ impl CaseExpr {
 
         Ok(ColumnarValue::Array(current_value))
     }
+
+    /// This function evaluates the specialized case of:
+    ///
+    /// CASE WHEN condition THEN column
+    ///      [ELSE NULL]
+    /// END
+    fn case_column_or_null(&self, batch: &RecordBatch) -> 
Result<ColumnarValue> {
+        let when_expr = &self.when_then_expr[0].0;
+        let then_expr = &self.when_then_expr[0].1;
+        if let ColumnarValue::Array(bit_mask) = when_expr.evaluate(batch)? {
+            let bit_mask = bit_mask
+                .as_any()
+                .downcast_ref::<BooleanArray>()
+                .expect("predicate should evaluate to a boolean array");
+            // invert the bitmask
+            let bit_mask = not(bit_mask)?;

Review Comment:
   As a further optimization I think the when predicate can be transformed to 
be `not(condition)` so it's possible to use `bit_mask` directly instead of 
`not(bit_mask)`. 
   This makes it possible to optimize/simplify a condition like `x=1` to `x!=1` 
instead of `not(x=1)`, saving the invert. 



-- 
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-unsubscr...@datafusion.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to