perf: evaluate the child of `size` once when it needs no NULL guard #7473
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.42) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 328 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 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.347
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #7473"
general
weight: 0.35
rich7420
Which issue does this PR close?
Part of #5781, which stays open.
Rationale for this change
CometSizeserializes its child twice, asCASE WHEN isnotnull(child) THEN size(child) ELSE <null answer> END. DataFusion'sCASEevaluates theWHENover the batch and then theTHEN, so the child runs twice. That costs little for a column, but an expensive child pays twice. For a Scala UDF it is two dispatcher calls per row.The native
sizealready returns -1 for a NULL array or map, which is Spark's answer whenspark.sql.legacy.sizeOfNullis on and ANSI is off, the default on Spark 3.x. A child that cannot be NULL never takes the NULL branch either. In both cases the child can go straight tosize, the wayCometArrayAppendalready skips its guard for a non-nullable array.This came up in a production Spark 3.4 job. Spark's
InferFiltersFromGenerateaddedsize(x) > 0 AND isnotnull(x)for anexplode, and the plan shows that filter below an aggregate, withxreplaced by the Scala UDF call that produced it. Comet called the UDF three times per row where Spark called it twice. Estimated from the UDF's own timers, the extra call and its JVM round trip were about a fifth of that stage's task time.Evaluating the child once in these cases also keeps a nondeterministic child in step there (#5781). The guard stays for a nullable child when
legacySizeOfNullis off, which includes Spark 4's default ANSI mode, so #5781 still applies tosizein that case and to the other three expressions it lists.The condition is the one @grorge123 proposed for
CometSizein #5526. The discussion on #5781 left narrowing the guards for a follow-up, and this takes only thesizepart.What changes are included in this PR?
CometSize.convertemits the nativesizealone whenlegacySizeOfNullis on or the child is not nullable, and keeps the guard otherwise.sizeonly ever sees a null-free array in a Comet plan now say when it does.How are these changes tested?
A new test in
CometArrayExpressionSuitecounts calls to a Scala UDF undersize: withlegacySizeOfNullon and a nullable UDF, and with it off and a non-nullable UDF. It checks the answer and native execution, and that Comet calls the UDF as many times as Spark does. With the guard, Comet would call it once more for each row whose array is not NULL.size.sqlalready covers bothlegacySizeOfNullsettings for array and map columns with NULLs.The new test has not been run locally yet.
test-compilepasses on the default profile and with-Pspark-3.5 -Pstrict-warnings.