← Back to Pull requests

fix: evaluate the right operand of AND, OR and IN only for rows the left side has not decided #7326

Closed
unknown closed this pull request without merging on Oct 31, 2026 13:48 UTC
View on GitHub
unknown unknown commented on Oct 29, 2026 20:40 UTC
Author

Which issue does this PR close?

Closes #6823.

Part of #6006.

Rationale for this change

Spark evaluates AND, OR and the candidates of IN lazily: a later operand runs only for the rows that the earlier ones have not decided. Comet evaluates every operand over the whole batch, so under ANSI mode an operand that can raise throws on a row Spark never evaluates. With v holding 0 to 3 on a Parquet table and ANSI on, each of these raised DIVIDE_BY_ZERO in Comet and returned rows in Spark:

  • v != 0 AND 100 / v > 1

  • v = 0 OR 100 / v > 1

  • v IN (0, 100 / v)
  • CASE WHEN, IF and coalesce already match Spark.

    What changes are included in this PR?

  • LazyLogical (native/spark-expr/src/conditional_funcs/lazy_logical.rs) evaluates the right operand of AND and OR only for the rows the left operand has not decided, which are the rows where it is not false for AND and not true for OR. It shares the column projection and masked evaluation of NullShortCircuit, so skipping rows filters no other column. spark_comparison creates it for AND and OR.

  • When more than half the rows still need the right operand, and it is built from deterministic expressions, it is first tried over the whole batch, which is cheaper than selecting rows. A failure there falls back to selecting the rows, here and for the batches after it. The result for a row the left operand decides is ignored, so this only matters if it fails there.

  • It applies only when the right operand can fail, by the existing cannot_fail check. Otherwise the expression stays a plain BinaryExpr, so the common case has no extra cost.

  • lazy_in_list rewrites v IN (c1, c2, ...) into a chain when a candidate after the first can fail. A run of candidates that cannot fail stays one IN, keeping its hash set, and each candidate that can fail is a lazy equality. NOT IN is the negation of that chain. This applies only when the value is cheap and cannot fail, because the chain evaluates it once per term. The terms are joined in a balanced tree, so a long list is no deeper than its logarithm. Other shapes keep the existing whole-batch evaluation.
  • This does not cover the rest of #6006: aggregate FILTER, operator-level masks, and the compatibility guide section.

    How are these changes tested?

  • Rust unit tests in lazy_logical.rs cover AND and OR on rows the left side decides, a NULL left operand, a batch with no decided rows, an empty batch, a scalar left operand, a right operand that reads no column, an infallible right operand staying a BinaryExpr, and the IN and NOT IN rewrite, including floats. The datafusion-comet-spark-expr lib suite passes (1334 tests) and cargo clippy -D warnings is clean.

  • New SQL file test expressions/conditional/logical_lazy_ansi.sql runs the cases above, chains, nesting under NOT, and IN with a failing cast. It also has controls where a row Spark does evaluate must still raise DIVIDE_BY_ZERO or CAST_INVALID_INPUT. It passes with CometSqlFileTestSuite on Spark 4.1, along with the existing conditional/ and predicates SQL files.

  • native/spark-expr/benches/lazy_logical.rs compares the eager and lazy forms at 0 to 100% pending rows, and IN lists of 2, 8 and 64 candidates. Lazy evaluation is up to 3-4x faster when few rows are pending, and within noise of eager evaluation otherwise. A 64-candidate IN with one fallible candidate is 2.3x faster than DataFusion's.

  • I did not run the Spark SQL suite or the other Spark profiles.
  • Diff: 442 lines (+331 / -111) across 5 files Reviewer: sunchao Review duration: 2468m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 442 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.365
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7326"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.3% (MEDIUM) · Higher = larger blast radius
    45.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.9% · Safe to merge
    17.9%