Skip to content

Commit e2b9f77

Browse files
committed
refactor: add count_boolean_values helper function and optimize check_short_circuit logic
- Introduced a new helper function `count_boolean_values` to count true and false values in a BooleanArray, improving readability and performance. - Updated `check_short_circuit` to utilize the new helper function for counting, reducing redundant operations and enhancing clarity in the evaluation logic for AND/OR operations. - Adjusted comments for better understanding of the short-circuiting conditions based on the new counting mechanism.
1 parent 8b910e1 commit e2b9f77

1 file changed

Lines changed: 34 additions & 20 deletions

File tree

  • datafusion/physical-expr/src/expressions

‎datafusion/physical-expr/src/expressions/binary.rs‎

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,23 @@ enum ShortCircuitStrategy {
822822
ReturnRight,
823823
None,
824824
}
825+
/// Helper function to count the number of true and false values in a BooleanArray.
826+
/// Assumes the array does not contain nulls.
827+
#[inline(always)]
828+
fn count_boolean_values(array: &BooleanArray) -> (usize, usize) {
829+
let mut true_count = 0;
830+
let mut false_count = 0;
831+
for i in 0..array.len() {
832+
// Since we assume there are no nulls (null_count() == 0), we can directly call value(i)
833+
if array.value(i) {
834+
true_count += 1;
835+
} else {
836+
false_count += 1;
837+
}
838+
}
839+
(true_count, false_count)
840+
}
841+
825842
/// Checks if a logical operator (`AND`/`OR`) can short-circuit evaluation based on the left-hand side (lhs) result.
826843
///
827844
/// Short-circuiting occurs under these circumstances:
@@ -831,14 +848,15 @@ enum ShortCircuitStrategy {
831848
/// - For `OR`:
832849
/// - if LHS is all true => short-circuit → return LHS
833850
/// - if LHS is all false => short-circuit → return RHS
851+
///
834852
/// # Arguments
835853
/// * `lhs` - The left-hand side (lhs) columnar value (array or scalar)
836854
/// * `op` - The logical operator (`AND` or `OR`)
837855
///
838856
/// # Implementation Notes
839-
/// 1. Only works with Boolean-typed arguments (other types automatically return `false`)
857+
/// 1. Only works with Boolean-typed arguments (other types automatically return `None`)
840858
/// 2. Handles both scalar values and array values
841-
/// 3. For arrays, uses optimized bit counting techniques for boolean arrays
859+
/// 3. For arrays, uses a single-pass count to acquire both true and false counts
842860
fn check_short_circuit(lhs: &ColumnarValue, op: &Operator) -> ShortCircuitStrategy {
843861
// Only apply short-circuiting for logical operators And/Or.
844862
if !matches!(op, Operator::And | Operator::Or) {
@@ -854,37 +872,32 @@ fn check_short_circuit(lhs: &ColumnarValue, op: &Operator) -> ShortCircuitStrate
854872

855873
match lhs {
856874
ColumnarValue::Array(array) => {
857-
if let Ok(array) = as_boolean_array(&array) {
858-
// For both operations, check if we have nulls
859-
if array.null_count() > 0 {
875+
if let Ok(bool_array) = as_boolean_array(&array) {
876+
// If there are any nulls, we cannot safely short-circuit.
877+
if bool_array.null_count() > 0 {
860878
return ShortCircuitStrategy::None;
861879
}
862880

863-
let len = array.len();
881+
let len = bool_array.len();
882+
// Use the helper to count true and false in one pass.
883+
let (true_count, false_count) = count_boolean_values(bool_array);
864884

865-
// Optimize evaluation order based on operator type
866-
// This avoids redundant counting operations
867885
if is_and {
868-
// For AND, check for all false values first (most common short-circuit case)
869-
let false_count = array.false_count();
886+
// For AND, if every value is false, we can return LHS.
870887
if false_count == len {
871888
return ShortCircuitStrategy::ReturnLeft; // All false for AND
872889
}
873-
874-
if false_count == 0 {
875-
// This means all true
890+
// If every value is true, we must evaluate the RHS.
891+
if true_count == len {
876892
return ShortCircuitStrategy::ReturnRight; // All true for AND
877893
}
878894
} else {
879-
// is OR
880-
// For OR, check for all true values first (most common short-circuit case)
881-
let true_count = array.true_count();
895+
// For OR, if every value is true, we can return LHS.
882896
if true_count == len {
883897
return ShortCircuitStrategy::ReturnLeft; // All true for OR
884898
}
885-
886-
if true_count == 0 {
887-
// This means all false
899+
// If every value is false, we must evaluate the RHS.
900+
if false_count == len {
888901
return ShortCircuitStrategy::ReturnRight; // All false for OR
889902
}
890903
}
@@ -893,7 +906,8 @@ fn check_short_circuit(lhs: &ColumnarValue, op: &Operator) -> ShortCircuitStrate
893906
ColumnarValue::Scalar(scalar) => {
894907
if let ScalarValue::Boolean(Some(value)) = scalar {
895908
let is_true = *value;
896-
909+
// For AND, a false scalar immediately determines the result,
910+
// and for OR, a true scalar immediately determines the result.
897911
if (is_and && !is_true) || (!is_and && is_true) {
898912
return ShortCircuitStrategy::ReturnLeft;
899913
} else {

0 commit comments

Comments
 (0)