[SPARK-60627][SQL] Do not slice an Arrow batch whose slice positions overflow int in `SliceRecordsArrowOutputProcessorImpl` #59871
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
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.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.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #59871"
general
weight: 0.35
HyukjinKwon
What changes were proposed in this pull request?
This PR fixes
SliceRecordsArrowOutputProcessorImpl, which slices the Arrow batches returned from Python when the internal configspark.sql.execution.arrow.maxRecordsPerOutputBatchis set, so that it outputs a batch whole when Arrow cannot slice it, instead of failing the task.BaseSliceArrowOutputProcessor.canSlice, which returns whetherVectorSchemaRoot.slicecan slice every vector of a batch, including nested ones. It rejects a fixed-width vector of 2 GiB or more, a view vector with 2^27 rows or more, and a list (or map) vector with 2^29 - 1 rows or more.SliceRecordsArrowOutputProcessorImpl.getRootchecks it before any row of a batch is output, and outputs the batch whole if the check fails, as it does for a batch within the limit.spark.sql.execution.arrow.maxRecordsPerOutputBatchnow says that a batch that Arrow cannot slice, e.g. one with a fixed-width column of 2 GiB or more, is sent as a whole.The check lives in
BaseSliceArrowOutputProcessorso thatSliceBytesArrowOutputProcessorImplcan share it. SPARK-60395 proposes the same check for the bytes slicing.Why are the changes needed?
In Arrow Java 19.0.0 (and 18.3.0 in
branch-4.1), the transfer pairs thatVectorSchemaRoot.sliceuses compute some buffer positions in int:BaseFixedWidthVector.splitAndTransferValueBuffer:startIndex typeWidthandlength typeWidthListVector's transfer pair:startIndex OFFSET_WIDTHand(startIndex + length) OFFSET_WIDTHBaseVariableWidthViewVector.splitAndTransferViewBufferAndDataBuffer:i * ELEMENT_SIZEOnce a slice reaches 2 GiB into such a buffer, the position wraps around to a negative value and
ArrowBuf.slicethrowsIndexOutOfBoundsException.VectorSchemaRoot.slicedoes not close the vectors it has sliced so far when this happens, so they keep holding the buffers of the batch, and the batch is leaked as well.The records slicing receives such a batch when Python returns one large batch, e.g.,
mapInArrowwrites each batch that its function yields as is. For example, withmaxRecordsPerOutputBatch=16777216, one batch of 285,212,672 rows (2^28 + 2^24) of one bigint column (2.28 GB) is output as 16 slices, and then the slice from row 268,435,456 (2^28) fails:All 2,317,352,960 bytes of that batch then stay allocated after the processor is closed.
This has existed since the records slicing was added by SPARK-51769 in Apache Spark 4.1.0 (2025-12-11).
Does this PR introduce _any_ user-facing change?
Yes, only for users who set the internal config
spark.sql.execution.arrow.maxRecordsPerOutputBatch, which is disabled by default. A batch from Python that Arrow cannot slice is now output whole, with more records than the limit, instead of failing the task withIndexOutOfBoundsExceptionand leaking the batch.How was this patch tested?
PythonArrowOutputSuitethat runsSliceRecordsArrowOutputProcessorImplover a struct column, like the output ofmapInArrow, holding an array column that reports 2^29 + 2^20 rows without allocating them, and checks that the batch is output whole instead of sliced. A real overflow needs buffers over 2 GiB, so the manual runs below cover the overflow and the leak. It uses a list vector because the fixed-width vectors that Spark reads are final classes.BaseSliceArrowOutputProcessor.canSliceat the boundaries of an 8-byte fixed-width vector and of a list vector.SliceRecordsArrowOutputProcessorImplover a real 2.28 GB bigint vector of 285,212,672 rows with a limit of 16,777,216 records. Without this fix, it fails and leaks as shown above. With this fix, the batch is output whole and the allocator closes without a leak.local[1]with--conf spark.sql.execution.arrow.maxRecordsPerOutputBatch=16777216. With this fix, it returns the right count, sum and max, and the runner logsBatches: 1, Data: 2176.00 MB, i.e., Python sends the 2.28 GB batch as one batch, which is output whole.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5