[SPARK-61913][PYTHON] Match Python on NaN ordering and short-circuit evaluation in transpiled UDFs #71
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
holdenk
What changes were proposed in this pull request?
Follow-up to SPARK-58628 for the experimental Python UDF transpiler (
python/pyspark/sql/transpile.py):==/!=already have, so a NaN operand givesFalseas in Python.len(...)is categorisedinteger, so it gets no guard.and/orlower later operands into aCASE WHENwhen one of them can raise (a NULL guard or arithmetic). Chains that cannot raise still lower toAND/OR.and/oroperands narrow later ones (x is not None and x > 0has no NULL guard),ornarrows the false branch, and arithmetic over non-null operands is non-null.s (len(s) > 1)).and/oroperand that can beNone(a ternary arm) falls back to Python, as the literalNonealready 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/ORshort-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.transpilePyUDFson: the cases above now match the interpreted UDF, ands (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
NaNin ordering comparisons; comparisons such asNaN > 0now returnFalse.Preserved
NULLresults in boolean expressions and comparisons, with more accurate null handling acrossandandorconditions.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 -->