comphead commented on code in PR #27471:
URL: https://github.com/apache/datafusion/pull/27471#discussion_r4654271547


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -556,69 +583,36 @@ impl PhysicalExpr for BinaryExpr {
         let lhs = self.left.evaluate(batch)?;
 
         // Check if we can apply short-circuit evaluation.
-        match check_short_circuit(&lhs, &self.op) {
-            ShortCircuitStrategy::None => {}
-            ShortCircuitStrategy::ReturnLeft => return Ok(lhs),
-            ShortCircuitStrategy::ReturnRight => {
-                let rhs = self.right.evaluate(batch)?;
-                return Ok(rhs);
-            }
-            ShortCircuitStrategy::PreSelection { mask, fill_value } => {
-                // `mask` selects the rows whose result depends on the RHS; the
-                // unselected rows are all `fill_value` (see 
`ShortCircuitStrategy`).
-                //
-                // Use `filter_record_batch` directly because 
`evaluate_selection`
-                // scatters the RHS back to the original batch length.
-                let selection_batch = filter_record_batch(batch, &mask)?;
-                let right_ret = self.right.evaluate(&selection_batch)?;
-
-                match &right_ret {
-                    ColumnarValue::Array(array) => {
-                        let boolean_array = array.as_boolean();
-                        // If the RHS is uniform on the selected rows, the 
whole
-                        // expression collapses and no scatter is needed.
-                        if boolean_array.null_count() == 0 {
-                            let rhs_value = if !boolean_array.has_false() {
-                                Some(true)
-                            } else if !boolean_array.has_true() {
-                                Some(false)
-                            } else {
-                                None
-                            };
-                            if let Some(rhs_value) = rhs_value {
-                                return Ok(uniform_pre_selection_result(
-                                    rhs_value, fill_value, lhs,
-                                ));
-                            }
-                        }
-
-                        return pre_selection_scatter(
-                            &mask,
-                            Some(boolean_array),
-                            fill_value,
-                        );
-                    }
-                    ColumnarValue::Scalar(scalar) => {
-                        if let ScalarValue::Boolean(v) = scalar {
-                            // A scalar RHS applies uniformly to all selected 
rows.
-                            if let Some(v) = v {
-                                return Ok(uniform_pre_selection_result(
-                                    *v, fill_value, lhs,
-                                ));
-                            } else {
-                                return pre_selection_scatter(&mask, None, 
fill_value);
-                            }
-                        } else {
-                            return internal_err!(
-                                "Expected boolean scalar value, found: 
{right_ret:?}"
-                            );
-                        }
-                    }
+        if self.op == Operator::Or {
+            match check_short_circuit_for_or(&lhs, self.right_is_volatile()) {
+                ShortCircuitStrategy::None => {}
+                ShortCircuitStrategy::ReturnLeft => return Ok(lhs),
+                ShortCircuitStrategy::ReturnRight => {
+                    let rhs = self.right.evaluate(batch)?;
+                    return Ok(rhs);
+                }
+                ShortCircuitStrategy::PreSelection { mask, fill_value } => {
+                    return self.evaluate_pre_selection(batch, lhs, &mask, 
fill_value);
                 }
             }
         }
 
-        let rhs = self.right.evaluate(batch)?;
+        let rhs = match self.right.evaluate(batch) {
+            Ok(rhs) => rhs,
+            // A deterministic RHS may fail on rows the LHS already decided
+            // (e.g. `x = 0 OR 1 / x > 1`), so retry on the undecided rows 
only.
+            Err(e) if self.op == Operator::Or => {

Review Comment:
   On a genuine error this retry compounds through right-nested `OR`s. Each 
level whose LHS leaves more than 20% of rows undecided evaluates its RHS 
subtree once on its full input and again on its undecided rows. In `a OR (b OR 
(c OR f))`, where `f` fails on a needed row, `f` can run up to 2^3 = 8 times 
before the error surfaces (this is from reading the code, not from running it). 
Left-deep chains like `a OR b OR c` are not affected, because only the RHS is 
retried.
   
   `evaluate_conjunction` avoids the same nesting cost for `AND` by flattening 
with `for_each_conjunct` and narrowing a single mask. A disjunct version would 
limit this to one retry per disjunct. It would also drop the repeated 
`filter_record_batch` copies that a nested volatile RHS now pays at every 
level. This is fine as a follow-up.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to