← Back to Pull requests

Do not let slice throw on rows a conditional never selects #17169

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 27, 2026 06:41 UTC
Author

Fixes #16914.

Description

slice can fail a query on the GPU that Spark completes on the CPU. When slice sits
inside a CASE WHEN or if branch whose predicate protects it, the CPU evaluates the
branch lazily and never checks the arguments of the rows the predicate skips. On the GPU,
GpuCaseWhen and GpuIf evaluate the branch over the whole batch, GpuSlice validates
the length column with min() and the start column with contains(0), and a single
unselected row with an illegal argument aborts the task. Retries see the same data, so the
stage and the job fail.

A real query of this shape is

case when size(parts) >= 13 then concat_ws('_', slice(parts, 8, size(parts) - 11)) end

The rows with fewer than 13 elements give a negative length but are never selected. The
query succeeds on the CPU and fails on the GPU.

The fix has two parts:

  • GpuSlice now reports side effects, so the conditional expressions take their

  • columnarEvalWithSideEffects path, which filters the batch down to the selected rows
    before evaluating the branch. Side effects are reported unless both start and length
    are literals already known to pass the checks, so a call such as slice(a, 1, 5) keeps
    the cheaper whole-batch path. This follows the fix for the same class of bug in #16496.
  • The zero-start check ran on the raw start column. slice is null intolerant, so only

  • the rows whose list and length are non-null are validated and sliced. This reuses the
    NullUtilities.mergeNulls approach that #13236 introduced for the negative-length check.

    The length and start validations are independent of ANSI mode; both throw with ANSI
    disabled, so the tests do not parameterize over it.

    hasSideEffects also gates other whole-batch evaluations, and each of them is a place
    where evaluating slice on rows Spark would skip is equally wrong: short-circuiting in
    GpuAnd / GpuOr, hoisting by GpuEquivalentExpressions, lambda fusion in the
    higher-order functions, materializing an earlier tier in tiered projection, and dynamic
    IN list eligibility. Dynamic IN lists containing a non-literal slice now fall back to
    the CPU for the IN, consistent with how other throwing expressions are treated.

    User-facing behaviour: no new configuration. Queries that used to fail now return what the
    CPU returns; results of queries that already succeeded are unchanged.

    Testing

    Added to integration_tests/src/main/python/array_test.py:

  • test_array_slice_invalid_args_on_rows_a_conditional_skips — a predicate that never

  • holds guards slice calls with a negative length column and a zero start column, in both
    CASE WHEN and if form; GPU and CPU must agree instead of throwing.
  • test_array_slice_invalid_args_on_rows_a_conditional_selects — the other direction: the

  • predicate holds for every row, so both engines must still throw. Filtering the batch must
    not hide invalid arguments.
  • test_array_slice_zero_start_only_on_null_list_rows — the list is null exactly on the

  • rows whose start is 0, which the CPU does not validate.
  • test_array_slice_mixed_selected_and_skipped_rows — every batch mixes selected rows with

  • valid arguments and skipped rows with a negative length or zero start, with slice in both
    the THEN and ELSE position; GPU and CPU results must match row for row.

    The python integration tests were run locally on main with Spark 3.5.2 on a Tesla T4
    (driver 535.161.07, using CUDA 12.9 forward compatibility from
    /usr/local/cuda-12.9/compat):

    mvn package -pl dist,integration_tests -am -Dbuildver=352 -DskipTests
    ./integration_tests/run_pyspark_from_build.sh -k array_slice
    

    Result: 51 passed, covering the four new tests and the existing test_array_slice*
    tests.

    To confirm the new tests catch the bug, they were also run against the pre-fix
    collectionOperations.scala with everything else unchanged.
    test_array_slice_invalid_args_on_rows_a_conditional_skips,
    test_array_slice_zero_start_only_on_null_list_rows and
    test_array_slice_mixed_selected_and_skipped_rows fail there with the same
    Unexpected value for start / length errors as #16914.
    test_array_slice_invalid_args_on_rows_a_conditional_selects passes on both, as expected,
    because invalid arguments on selected rows must raise before and after the fix.

    Because hasSideEffects changes the paths listed above, the test files that exercise them
    were also run in full in the same environment: array_test.py, conditionals_test.py,
    logic_test.py, higher_order_functions_test.py and cmp_test.py. Result: 1449 passed,
    84 skipped, 2 xfailed, 0 failed. All skips are Spark-version gates that do not apply to
    3.5.2; the two xfails are the existing test_array_distinct_neg_zero cases.

    Also verified on Spark 3.5.2 with one Tesla T4 executor on a Kubernetes cluster. That
    cluster runs 26.10, so these checks used the same two changes cherry-picked onto
    release/26.10, compared against the stock jar:

  • The four scenarios of the new tests were run as SQL in one session with the plugin

  • enabled and disabled: the skipped-row and null-list cases return identical results on
    GPU and CPU, and the selected-row cases raise on both.
  • The original failing production query went from failing every attempt to succeeding with

  • results matching its CPU run.

    Static checks: scalastyle reports 0 errors on the changed Scala file. The change touches
    only shared sql-plugin source and adds no imports. The shim preflight
    check_shim_compatibility.py coverage was run on this head (63d1a72) merged with main
    at a3b4c67 and returned PASS across 34 release profiles with no findings. The review
    and advisory modes were not run locally because they need authenticated gh and Git
    2.38+, so cross-PR evidence will come from the CI shim-compatibility and
    shim-related-prs reports. Only the spark352 profile was compiled locally; the other
    profiles rely on premerge CI.

    Checklists

    Documentation

  • Updated for new or modified user-facing features or behaviors

  • No user-facing change
  • Testing

  • Added or modified tests to cover new code paths

  • Covered by existing tests

  • (Please provide the names of the existing tests in the PR description.)
  • Not required
  • Performance

  • Tests ran and results are added in the PR description

  • Issue filed with a link in the PR description

  • Not required
  • Performance

    Reporting side effects moves conditional slice from whole-batch evaluation to the
    filter-then-evaluate path, which costs time on valid inputs. Measured with the methodology
    of #16496: 10 million cached rows of valid arguments (20-element arrays, start in [1, 5],
    length in [0, 9]), 50% branch selectivity, the expression projected ten times per query,
    two warm-up runs and nine measured runs, median reported. Environment: Tesla T4, Spark
    3.5.2, one executor, AQE off, ANSI disabled, plan asserted to run as GpuProject. Two
    rounds were run in A-B-A-B order to separate the change from cluster drift.

  • slice(a, b, c) alone: 168 ms before, 181 ms after (round deltas +16% and -9%, minima

  • 137 ms both) — no systematic change, as expected, since this path is untouched.
  • if(sel = 0, slice(a, b, c), NULL): 158 ms before, 223 ms after (+42%; rounds +43% and

  • +39%).
  • CASE WHEN sel = 0 THEN slice(a, b, c) END: 149 ms before, 188 ms after (+26%; rounds

  • +33% and +21%).

    The magnitude matches the 19-24% that #16496 measured for the same mechanism. Calls whose
    start and length are known-good literals keep the old path and are unaffected. The
    alternative — leaving the whole-batch path in place — is what fails the query, so the cost
    buys correctness on exactly the queries that cannot run today.

    AI assistance

    This change was produced with AI assistance (Claude). A human reviewed the complete diff
    and this description before the pull request was opened.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: thirtiseven Review duration: 2779m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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%
    46%
    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.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #17169"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 27.7% · Safe to merge
    27.7%