← Back to Pull requests

[SPARK-61913][PYTHON] Match Python on NaN ordering and short-circuit evaluation in transpiled UDFs #71

Open
unknown opened this pull request with 366 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 18, 2026 21:51 UTC
Author

What changes were proposed in this pull request?

Follow-up to SPARK-58628 for the experimental Python UDF transpiler (python/pyspark/sql/transpile.py):

  • Ordering comparisons get the NaN guard == / != already have, so a NaN operand gives False as in Python. len(...) is categorised integer, so it gets no guard.

  • and / or lower later operands into a CASE WHEN when one of them can raise (a NULL guard or arithmetic). Chains that cannot raise still lower to AND / OR.

  • Earlier and / or operands narrow later ones (x is not None and x > 0 has no NULL guard), or narrows the false branch, and arithmetic over non-null operands is non-null.

  • A bool is a valid string repeat count again (s (len(s) > 1)).

  • An and / or operand that can be None (a ternary arm) falls back to Python, as the literal None already did.
  • Why are the changes needed?

    The lowered form disagreed with Python:

    | Body | Input | Python | Lowered (master) |
    |---|---|---|---|
    | len(s) > 3 and x > 0.5 | ('abcde', NaN) | False | True |
    | x > 0 and len(s) > 3 and len(s) < 10 | (None, -1.0) | False | raises (len guard) |
    | y != 0 and x % y > 1 and x % y < 5 | (7, 0) | False | REMAINDER_BY_ZERO |
    | (True if x > 0 else None) and y is not None | (-1, None) | None | False |

    Spark orders NaN highest; Python's NaN comparisons are all False. AND / OR short-circuit, but subexpression elimination evaluates a subexpression repeated across their operands up front, on rows Python skips.

    Does this PR introduce _any_ user-facing change?

    Only with spark.sql.experimental.optimizer.transpilePyUDFs on: the cases above now match the interpreted UDF, and s (len(s) > 1) is transpiled again.

    How was this patch tested?

    One new test per behavior in test_udf_transpile_unit.py, plus the existing NaN and None-operand tests updated.

    This head has not run on a master-built JVM yet; CI here is the first run. Before pushing, the lowered expressions were compared with CPython on a Spark 4.3.0 engine.

    Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Code

    ---
    _Generated by Claude Code_

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Improved Python-compatible handling of NaN in ordering comparisons; comparisons such as NaN > 0 now return False.
    Preserved NULL results in boolean expressions and comparisons, with more accurate null handling across and and or conditions.
    Boolean expressions now respect short-circuit behavior, avoiding errors from later expressions that would not be evaluated in Python.
    * String repetition now supports boolean counts.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 366 lines (+274 / -92) across 4 files Reviewer: holdenk Review duration: 17175m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.43) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 366 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.353
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #71"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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.7% (MEDIUM) · Higher = larger blast radius
    42.7%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 22.8% · Safe to merge
    22.8%
    Repository