Do not let slice throw on rows a conditional never selects #17169
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
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.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #17169"
general
weight: 0.35
thirtiseven
Fixes #16914.
Description
slicecan fail a query on the GPU that Spark completes on the CPU. Whenslicesitsinside a
CASE WHENorifbranch whose predicate protects it, the CPU evaluates thebranch lazily and never checks the arguments of the rows the predicate skips. On the GPU,
GpuCaseWhenandGpuIfevaluate the branch over the whole batch,GpuSlicevalidatesthe
lengthcolumn withmin()and thestartcolumn withcontains(0), and a singleunselected 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
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:
GpuSlicenow reports side effects, so the conditional expressions take theircolumnarEvalWithSideEffectspath, which filters the batch down to the selected rowsbefore evaluating the branch. Side effects are reported unless both
startandlengthare literals already known to pass the checks, so a call such as
slice(a, 1, 5)keepsthe cheaper whole-batch path. This follows the fix for the same class of bug in #16496.
startcolumn.sliceis null intolerant, so onlythe rows whose list and length are non-null are validated and sliced. This reuses the
NullUtilities.mergeNullsapproach that #13236 introduced for the negative-length check.The
lengthandstartvalidations are independent of ANSI mode; both throw with ANSIdisabled, so the tests do not parameterize over it.
hasSideEffectsalso gates other whole-batch evaluations, and each of them is a placewhere evaluating
sliceon rows Spark would skip is equally wrong: short-circuiting inGpuAnd/GpuOr, hoisting byGpuEquivalentExpressions, lambda fusion in thehigher-order functions, materializing an earlier tier in tiered projection, and dynamic
INlist eligibility. DynamicINlists containing a non-literalslicenow fall back tothe 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 neverholds guards
slicecalls with a negative length column and a zero start column, in bothCASE WHENandifform; GPU and CPU must agree instead of throwing.test_array_slice_invalid_args_on_rows_a_conditional_selects— the other direction: thepredicate 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 therows whose start is 0, which the CPU does not validate.
test_array_slice_mixed_selected_and_skipped_rows— every batch mixes selected rows withvalid arguments and skipped rows with a negative length or zero start, with
slicein boththe
THENandELSEposition; GPU and CPU results must match row for row.The python integration tests were run locally on
mainwith 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):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.scalawith everything else unchanged.test_array_slice_invalid_args_on_rows_a_conditional_skips,test_array_slice_zero_start_only_on_null_list_rowsandtest_array_slice_mixed_selected_and_skipped_rowsfail there with the sameUnexpected value for start/lengtherrors as #16914.test_array_slice_invalid_args_on_rows_a_conditional_selectspasses on both, as expected,because invalid arguments on selected rows must raise before and after the fix.
Because
hasSideEffectschanges the paths listed above, the test files that exercise themwere also run in full in the same environment:
array_test.py,conditionals_test.py,logic_test.py,higher_order_functions_test.pyandcmp_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_zerocases.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: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.
results matching its CPU run.
Static checks: scalastyle reports 0 errors on the changed Scala file. The change touches
only shared
sql-pluginsource and adds no imports. The shim preflightcheck_shim_compatibility.py coveragewas run on this head (63d1a72) merged withmainat
a3b4c67and returned PASS across 34 release profiles with no findings. Thereviewand
advisorymodes were not run locally because they need authenticatedghand Git2.38+, so cross-PR evidence will come from the CI
shim-compatibilityandshim-related-prsreports. Only the spark352 profile was compiled locally; the otherprofiles rely on premerge CI.
Checklists
Documentation
Testing
(Please provide the names of the existing tests in the PR description.)
Performance
Performance
Reporting side effects moves conditional
slicefrom whole-batch evaluation to thefilter-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. Tworounds 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%, minima137 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
startandlengthare known-good literals keep the old path and are unaffected. Thealternative — 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.