Skip to content

Commit a62df47

Browse files
committed
refactor: enhance short-circuit evaluation strategy in BinaryExpr to optimize logical operations
1 parent 308d7b4 commit a62df47

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

  • datafusion/physical-expr/src/expressions

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,24 @@ impl PhysicalExpr for BinaryExpr {
360360

361361
let lhs = self.left.evaluate(batch)?;
362362

363-
// Delay evaluating RHS unless we really need it
364-
let rhs_lazy = || self.right.evaluate(batch);
365-
366363
// Optimize for short-circuiting `Operator::And` or `Operator::Or` operations and return early.
367-
if let Some(result) = get_short_circuit_result(&lhs, &self.op, None) {
368-
return Ok(result);
364+
let strategy = get_short_circuit_strategy(&lhs, &self.op);
365+
match strategy {
366+
ShortCircuitStrategy::ReturnLeft => return Ok(lhs),
367+
ShortCircuitStrategy::ReturnRight(value) => return Ok(value),
368+
// Currently, we can't utilize the PreSelection strategy without changing the API
369+
// but in the future we could add evaluate_selection to PhysicalExpr
370+
ShortCircuitStrategy::PreSelection(_) | ShortCircuitStrategy::None => {
371+
// Continue with existing logic
372+
if let Some(result) = get_short_circuit_result(&lhs, &self.op, None) {
373+
return Ok(result);
374+
}
375+
}
369376
}
370377

378+
// Delay evaluating RHS unless we really need it
379+
let rhs_lazy = || self.right.evaluate(batch);
380+
371381
// Only evaluate RHS if short-circuit evaluation doesn't apply
372382
let rhs = rhs_lazy()?;
373383

0 commit comments

Comments
 (0)