fn check_short_circuit()

in datafusion/physical-expr/src/expressions/binary.rs [855:937]


fn check_short_circuit<'a>(
    lhs: &'a ColumnarValue,
    op: &Operator,
) -> ShortCircuitStrategy<'a> {
    // Quick reject for non-logical operators,and quick judgment when op is and
    let is_and = match op {
        Operator::And => true,
        Operator::Or => false,
        _ => return ShortCircuitStrategy::None,
    };

    // Non-boolean types can't be short-circuited
    if lhs.data_type() != DataType::Boolean {
        return ShortCircuitStrategy::None;
    }

    match lhs {
        ColumnarValue::Array(array) => {
            // Fast path for arrays - try to downcast to boolean array
            if let Ok(bool_array) = as_boolean_array(array) {
                // Arrays with nulls can't be short-circuited
                if bool_array.null_count() > 0 {
                    return ShortCircuitStrategy::None;
                }

                let len = bool_array.len();
                if len == 0 {
                    return ShortCircuitStrategy::None;
                }

                let true_count = bool_array.values().count_set_bits();
                if is_and {
                    // For AND, prioritize checking for all-false (short circuit case)
                    // Uses optimized false_count() method provided by Arrow

                    // Short circuit if all values are false
                    if true_count == 0 {
                        return ShortCircuitStrategy::ReturnLeft;
                    }

                    // If no false values, then all must be true
                    if true_count == len {
                        return ShortCircuitStrategy::ReturnRight;
                    }

                    // determine if we can pre-selection
                    if true_count as f32 / len as f32 <= PRE_SELECTION_THRESHOLD {
                        return ShortCircuitStrategy::PreSelection(bool_array);
                    }
                } else {
                    // For OR, prioritize checking for all-true (short circuit case)
                    // Uses optimized true_count() method provided by Arrow

                    // Short circuit if all values are true
                    if true_count == len {
                        return ShortCircuitStrategy::ReturnLeft;
                    }

                    // If no true values, then all must be false
                    if true_count == 0 {
                        return ShortCircuitStrategy::ReturnRight;
                    }
                }
            }
        }
        ColumnarValue::Scalar(scalar) => {
            // Fast path for scalar values
            if let ScalarValue::Boolean(Some(is_true)) = scalar {
                // Return Left for:
                // - AND with false value
                // - OR with true value
                if (is_and && !is_true) || (!is_and && *is_true) {
                    return ShortCircuitStrategy::ReturnLeft;
                } else {
                    return ShortCircuitStrategy::ReturnRight;
                }
            }
        }
    }

    // If we can't short-circuit, indicate that normal evaluation should continue
    ShortCircuitStrategy::None
}