[SPARK-62323][SQL] Fix wrong results from NULLIF or BETWEEN in a lambda function #60869
All Vouch review confidence checks passed
Residual risk 0.25 = Change Risk (0.43) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 388 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.357
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #60869"
general
weight: 0.35
dongjoon-hyun
What changes were proposed in this pull request?
NULLIF and BETWEEN build a
With.RewriteWithExpressioneither evaluates its definition in aProjectunder the operator, or, when no child can evaluate it, inlines it into every reference. A lambda function may be evaluated any number of times per row, possibly none, so neither is right there.This PR handles a higher-order function as the rule already handles a conditional expression: it rewrites only the arguments that are always evaluated, and in the rest a
Withstays where it is and memoizes its definition per evaluation, unless the definition gains nothing from it. A definition over a lambda variable now counts as cheap there when it would over an attribute.zip_withandmap_zip_withevaluate their second argument only if the first one is not null, and now say so inalwaysEvaluatedArguments.Why are the changes needed?
With the default
spark.sql.alwaysInlineCommonExpr=false:Under ANSI mode,
transform(arr, x -> nullif(a / b, 1))also fails with DIVIDE_BY_ZERO on a row wherearris empty andbis 0. So dozip_withandmap_zip_withwith such a NULLIF in the second argument and a NULL first argument, andaggregatewith one in the start value and a NULL array, which all return NULL without NULLIF.NullIfbuilds aWithsince SPARK-45760 (4.0.0).Does this PR introduce _any_ user-facing change?
Yes. A NULLIF or BETWEEN in a lambda function reads its input once each time the lambda function is evaluated, so the queries above return 0 and 100, and the
transform,zip_with,map_zip_withandaggregateabove do not fail.This has costs. A deterministic definition that reads no lambda variable and is not cheap, such as a Scala UDF over a column, is now evaluated each time the lambda function is, instead of once per row. So is an expensive column below that an operand which is not cheap reads once, like
cinnullif(c + x, 1). A definition that reads a lambda variable and is not cheap, such asx + 1, keeps aWithin the lambda function, which, as in a conditional branch, stops outer-join elimination and join-condition placement.How was this patch tested?
New test in
ColumnExpressionSuite, on the interpreted, expression codegen and whole-stage codegen paths: variants of the two queries above with a seededrandand a BETWEEN with an empty window overrand, and, under ANSI mode, the four queries above that failed. It fails without this change.New plan test in
RewriteWithExpressionSuitefor what the new case keeps, inlines and hoists.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5