← Back to Pull requests

[SPARK-60627][SQL] Do not slice an Arrow batch whose slice positions overflow int in `SliceRecordsArrowOutputProcessorImpl` #59871

Closed
unknown closed this pull request without merging on Oct 29, 2026 01:31 UTC
View on GitHub
unknown unknown commented on Oct 28, 2026 16:52 UTC
Author

What changes were proposed in this pull request?

This PR fixes SliceRecordsArrowOutputProcessorImpl, which slices the Arrow batches returned from Python when the internal config spark.sql.execution.arrow.maxRecordsPerOutputBatch is set, so that it outputs a batch whole when Arrow cannot slice it, instead of failing the task.

  • Add BaseSliceArrowOutputProcessor.canSlice, which returns whether VectorSchemaRoot.slice can 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.getRoot checks 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.

  • The doc of spark.sql.execution.arrow.maxRecordsPerOutputBatch now 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 BaseSliceArrowOutputProcessor so that SliceBytesArrowOutputProcessorImpl can share it. SPARK-60395 proposes the same check for the bytes slicing.

  • https://github.com/apache/spark/pull/59786
  • Why are the changes needed?

    In Arrow Java 19.0.0 (and 18.3.0 in branch-4.1), the transfer pairs that VectorSchemaRoot.slice uses compute some buffer positions in int:

  • BaseFixedWidthVector.splitAndTransferValueBuffer: startIndex typeWidth and length typeWidth

  • ListVector's transfer pair: startIndex OFFSET_WIDTH and (startIndex + length) OFFSET_WIDTH

  • BaseVariableWidthViewVector.splitAndTransferViewBufferAndDataBuffer: i * ELEMENT_SIZE
  • Once a slice reaches 2 GiB into such a buffer, the position wraps around to a negative value and ArrowBuf.slice throws IndexOutOfBoundsException. VectorSchemaRoot.slice does 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., mapInArrow writes each batch that its function yields as is. For example, with maxRecordsPerOutputBatch=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:

    java.lang.IndexOutOfBoundsException: index (-2147483648) must not be negative
      at org.apache.arrow.util.Preconditions.checkPositionIndex(Preconditions.java:1128)
      at org.apache.arrow.util.Preconditions.checkPositionIndex(Preconditions.java:1111)
      at org.apache.arrow.memory.ArrowBuf.slice(ArrowBuf.java:185)
      at org.apache.arrow.vector.BaseFixedWidthVector.splitAndTransferValueBuffer(BaseFixedWidthVector.java:653)
      at org.apache.arrow.vector.BaseFixedWidthVector.splitAndTransferTo(BaseFixedWidthVector.java:644)
      at org.apache.arrow.vector.BigIntVector$TransferImpl.splitAndTransfer(BigIntVector.java:346)
      at org.apache.arrow.vector.VectorSchemaRoot.lambda$slice$1(VectorSchemaRoot.java:347)
    

    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 with IndexOutOfBoundsException and leaking the batch.

    How was this patch tested?

  • Added a test to PythonArrowOutputSuite that runs SliceRecordsArrowOutputProcessorImpl over a struct column, like the output of mapInArrow, 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.

  • Added a test of BaseSliceArrowOutputProcessor.canSlice at the boundaries of an 8-byte fixed-width vector and of a list vector.

  • Manually ran SliceRecordsArrowOutputProcessorImpl over 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.

  • Manually ran the following on local[1] with --conf spark.sql.execution.arrow.maxRecordsPerOutputBatch=16777216. With this fix, it returns the right count, sum and max, and the runner logs Batches: 1, Data: 2176.00 MB, i.e., Python sends the 2.28 GB batch as one batch, which is output whole.
  • import numpy as np
    import pyarrow as pa
    
    rows = (1 << 28) + (1 << 24)
    
    def f(it):
        for _ in it:
            pass
        yield pa.RecordBatch.from_arrays([pa.array(np.arange(rows, dtype=np.int64))], ["id"])
    
    df = spark.range(1, numPartitions=1).mapInArrow(f, "id long")
    df.selectExpr("count(*)", "sum(id)", "max(id)").collect()
    # [Row(count(1)=285212672, sum(id)=40673133992083456, max(id)=285212671)]
    

    build/sbt 'sql/testOnly *PythonArrowOutputSuite'
    

    Was this patch authored or co-authored using generative AI tooling?

    Generated-by: Claude Opus 5.5

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: HyukjinKwon Review duration: 519m
    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
    0.52
    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%
    60%
    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.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #59871"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 20.5% · Safe to merge
    20.5%
    Repository