← Back to Pull requests

perf: evaluate the child of `size` once when it needs no NULL guard #7473

Closed
unknown closed this pull request without merging on Nov 06, 2026 21:48 UTC
View on GitHub
unknown unknown commented on Nov 04, 2026 03:14 UTC
Author

Which issue does this PR close?

Part of #5781, which stays open.

Rationale for this change

CometSize serializes its child twice, as CASE WHEN isnotnull(child) THEN size(child) ELSE <null answer> END. DataFusion's CASE evaluates the WHEN over the batch and then the THEN, 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 size already returns -1 for a NULL array or map, which is Spark's answer when spark.sql.legacy.sizeOfNull is 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 to size, the way CometArrayAppend already skips its guard for a non-nullable array.

This came up in a production Spark 3.4 job. Spark's InferFiltersFromGenerate added size(x) > 0 AND isnotnull(x) for an explode, and the plan shows that filter below an aggregate, with x replaced 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 legacySizeOfNull is off, which includes Spark 4's default ANSI mode, so #5781 still applies to size in that case and to the other three expressions it lists.

The condition is the one @grorge123 proposed for CometSize in #5526. The discussion on #5781 left narrowing the guards for a follow-up, and this takes only the size part.

What changes are included in this PR?

  • CometSize.convert emits the native size alone when legacySizeOfNull is on or the child is not nullable, and keeps the guard otherwise.

  • Two native comments that said size only ever sees a null-free array in a Comet plan now say when it does.
  • How are these changes tested?

    A new test in CometArrayExpressionSuite counts calls to a Scala UDF under size: with legacySizeOfNull on 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.sql already covers both legacySizeOfNull settings for array and map columns with NULLs.

    The new test has not been run locally yet. test-compile passes on the default profile and with -Pspark-3.5 -Pstrict-warnings.

    Diff: 328 lines (+246 / -82) across 4 files Reviewer: rich7420 Review duration: 3994m
    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
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    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.347
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #7473"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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.1% (MEDIUM) · Higher = larger blast radius
    42.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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 18.2% · Safe to merge
    18.2%