alamb commented on code in PR #16930:
URL: https://github.com/apache/datafusion/pull/16930#discussion_r2249196239


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -375,7 +375,46 @@ impl PhysicalExpr for BinaryExpr {
                 // as it takes into account cases where the selection contains 
null values.
                 let batch = filter_record_batch(batch, selection)?;
                 let right_ret = self.right.evaluate(&batch)?;
-                return pre_selection_scatter(selection, right_ret);
+
+                match &right_ret {
+                    ColumnarValue::Array(array) => {
+                        // When the array on the right is all true or all 
false, skip the scatter process
+                        let boolean_array = array.as_boolean();
+                        let true_count = boolean_array.true_count();
+                        let length = boolean_array.len();
+                        if true_count == length {
+                            return Ok(lhs);
+                        } else if true_count == 0 && 
boolean_array.null_count() == 0 {
+                            // If the right-hand array is returned at this 
point,the lengths will be inconsistent;
+                            // returning a scalar can avoid this issue
+                            return 
Ok(ColumnarValue::Scalar(ScalarValue::Boolean(
+                                Some(false),
+                            )));
+                        }
+
+                        return pre_selection_scatter(selection, boolean_array);
+                    }
+                    ColumnarValue::Scalar(scalar) => {
+                        if let ScalarValue::Boolean(v) = scalar {
+                            // When the scalar is true or false, skip the 
scatter process
+                            if let Some(v) = v {
+                                if *v {
+                                    return Ok(lhs);
+                                } else {
+                                    return Ok(right_ret);
+                                }
+                            } else {
+                                let array =

Review Comment:
   I suspect anywhere we do a `BooleanArray::from(vec![..])` could be improved 
eventually if we care. It is likely not worth pursuing in this PR, but I 
figured I would bring it u



##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -106,6 +106,9 @@ pub trait PhysicalExpr: Send + Sync + Display + Debug + 
DynEq + DynHash {
             Ok(tmp_result)
         } else if let ColumnarValue::Array(a) = tmp_result {
             scatter(selection, a.as_ref()).map(ColumnarValue::Array)
+        } else if let ColumnarValue::Scalar(ScalarValue::Boolean(v)) = 
&tmp_result {
+            let a = BooleanArray::from(vec![*v; tmp_batch.num_rows()]);

Review Comment:
   we can probably make things faster if we avoid expanding out to a constant 
array here (avoid the `vec![]`).
   
   



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