← Back to Pull requests

[SPARK-62323][SQL] Fix wrong results from NULLIF or BETWEEN in a lambda function #60869

Open
unknown opened this pull request with 388 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 25, 2026 04:17 UTC
Author

What changes were proposed in this pull request?

NULLIF and BETWEEN build a With. RewriteWithExpression either evaluates its definition in a Project under 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 With stays 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_with and map_zip_with evaluate their second argument only if the first one is not null, and now say so in alwaysEvaluatedArguments.

Why are the changes needed?

With the default spark.sql.alwaysInlineCommonExpr=false:

-- A definition that reads a lambda variable is evaluated once per reference.
SELECT size(filter(sequence(1, 200), x -> nullif(rand() + x * 0 > 0.5, true)));
-- about 50, although nullif(_, true) never returns true

-- A definition that reads no lambda variable is evaluated once per row.
SELECT size(array_distinct(transform(sequence(1, 100), x -> nullif(rand(), 2))));
-- 1 instead of 100

Under ANSI mode, transform(arr, x -> nullif(a / b, 1)) also fails with DIVIDE_BY_ZERO on a row where arr is empty and b is 0. So do zip_with and map_zip_with with such a NULLIF in the second argument and a NULL first argument, and aggregate with one in the start value and a NULL array, which all return NULL without NULLIF. NullIf builds a With since 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_with and aggregate above 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 c in nullif(c + x, 1). A definition that reads a lambda variable and is not cheap, such as x + 1, keeps a With in 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 seeded rand and a BETWEEN with an empty window over rand, and, under ANSI mode, the four queries above that failed. It fails without this change.

New plan test in RewriteWithExpressionSuite for 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

Diff: 388 lines (+291 / -97) across 4 files Reviewer: dongjoon-hyun Review duration: 9639m
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
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%
42%
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.357
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #60869"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 43.0% 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: 43.1% (MEDIUM) · Higher = larger blast radius
43.1%
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: 43.0% · Lower = rushed or shallow review
43.0%
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: 24.6% · Safe to merge
24.6%
Repository