← Back to Pull requests

fix: evaluate guarded divisors and logarithm arguments once #7730

Closed
unknown closed this pull request without merging on Nov 30, 2026 23:47 UTC
View on GitHub
unknown unknown commented on Nov 21, 2026 09:57 UTC
Author

Which issue does this PR close?

Closes #7671.

Rationale for this change

Comet used IF(x = 0, NULL, x) for non-ANSI divisors and IF(x <= 0, NULL, x) for logarithm arguments. The condition evaluates x over the batch. The value branch evaluates it again on the rows that passed the check. For a stateful expression such as monotonically_increasing_id(), those evaluations can assign different values to the same row. A divisor that passed the check can then be zero when the arithmetic kernel uses it.

What changes are included in this PR?

  • Use a unary native function to check the evaluated operand. It masks zeros for non-ANSI /, div and %, and non-positive values for ln, log10 and log2. The math kernel consumes the values the guard checked. Both signs of zero match, and NaNs pass through.

  • Fold literal divisor checks at planning time. For arrays, the guard updates validity while sharing the values buffer.

  • Recognize the guard when checking whether an expression is safe to evaluate eagerly. This lets a guarded floating-point division qualify for short-circuit IN evaluation when its children qualify.

  • Update the custom serde guide to use CometAtan2 as its input preprocessing example.
  • #7785 tracks a separate problem on the dividend side. Spark skips the dividend when the divisor is NULL or when a zero divisor yields NULL. #7456 proposes evaluating the divisor first and is relevant to that follow-up.

    branch-1.2 still evaluates these guarded operands twice. I marked this PR as a backport-1.2 candidate. Trying the patch against 8afafe4f produced conflicts limited to imports in planner.rs and case_when.rs and a comment in case_when.rs. I suggest backporting the native guard with its regression tests after this lands on main.

    How are these changes tested?

  • A SQL fixture compares all six native operators with Spark using monotonically_increasing_id() over a single Parquet file in two-row batches. Separate queries exercise native try_divide and decimal / with the same stateful divisor. The arithmetic queries cross a zero divisor, and the logarithm inputs cycle through −1, 0 and 1.

  • A projection over three rows passes a nondeterministic counting UDF to each operator. The test checks native execution of all six operators and asserts exactly 18 calls to the UDF.

  • A Rust assertion checks that the planner recognizes guarded division over columns as safe to evaluate eagerly.
  • Local checks cover Rust and Scala formatting and git diff --check. Hosted CI runs the test suites.

    Post-merge follow-up #7807 adds the requested Rust tests for the numeric guards and the optional negative-zero input to each of the ln, log10 and log2 Parquet fixtures. The merge queue run for this PR passed Spark 4.1 Catalyst, SQL core and Hive.

    Diff: 361 lines (+270 / -91) across 4 files Reviewer: viirya Review duration: 13789m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 361 lines diff across 4 files
    0.43
    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.352
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7730"
    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: 42.6% (MEDIUM) · Higher = larger blast radius
    42.6%
    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: 16.8% · Safe to merge
    16.8%