← Back to Pull requests

[SPARK-60726][SQL] Add an API to read a partition of a native data source as an Arrow C stream #59934

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 29, 2026 19:25 UTC
Author

What changes were proposed in this pull request?

A follow-up to SPARK-60101 (#59424), which @zhouyuan asked for in https://github.com/apache/spark/pull/59424#issuecomment-6196064644. The design questions were listed in the reply https://github.com/apache/spark/pull/59424#issuecomment-6204557249. The change is an @Evolving API, like the other mix-ins of org.apache.spark.sql.connector.read, to read a partition of a native scan as an Arrow C stream:

@Evolving
public interface SupportsArrowStreamRead extends PartitionReaderFactory {
  void openArrowStream(InputPartition partition, long streamAddress);
}

The reader factories of the batch and micro-batch scans of native data sources implement it, with the stream that read of the library initializes. This PR is based on #59931 (version 1 of the interface), whose commit it includes until that PR is merged. The questions of the reply are settled as follows:

  • Which object. The engine gets the reader factory of the scan from its plan, BatchScanExec.readerFactory, and the partitions with it. The factory of a batch scan holds the location of the library, the name of the data source, the read schema, its time zone and the state of the reader.

  • Who owns the task context. openArrowStream is called on the thread of the running task that reads the partition, and fails otherwise. The engine owns the stream, which it may move, and releases it, also after the task ended and on another thread. Spark frees the SparkDsTaskContext once the library released its stream: it wraps the four callbacks of the stream with its own, which call those of the library, keyed by a private_data of Spark. Once the task failed or completed, is_interrupted of the context returns true, and the context only holds the task weakly. A stream that is never released leaks. The stream has to be zero-initialized, and if openArrowStream fails, the stream is released. The library gets the index of the partition in the scan as partition_id, since the task of an engine may read several, and so does Spark's own reader now.

  • What the engine checks. Spark does not check the arrays of such a stream, while it checks those that it imports itself, because what to check depends on the importer. Arrow Java ignores the offsets of the arrays, for example, while Arrow C++ and arrow-rs do not. Spark still checks that the library initialized the stream, with non-NULL callbacks. The callbacks can be called from any thread, but never concurrently, as the header already says, and they call into the JVM, so the engine releases the stream before the JVM exits.
  • NativePartitionReader opens its streams with the same code. The release of an engine's stream closes its NativeLibrary.Task on whichever thread the engine releases it, and only the release that removes the stream from the wrapped streams does, so the task is closed once. The docs describe the API under "Native Execution Engines".

    The reader factories of micro-batch scans implement it too: ColumnarStreamReader.readerFactory() lets a stream reader return its own factory, NativeMicroBatchPartitionReaderFactory for native data sources. The partitions of each micro-batch hold a broadcast of the state that the stream reader returned with them, so that a factory that an engine keeps reads them with that state, also once Spark planned the next micro-batch, while each executor fetches the state once for the micro-batch rather than with each task. openArrowStream checks the task and the stream before the partition, and rejects a partition of the other kind of scan with an IllegalArgumentException.

    Why are the changes needed?

    Without it, a query on a native data source under a native engine such as Gluten imports the data into Arrow Java. A ColumnarToRowExec then converts it to rows, which the engine converts back to its own columnar format:

    read -> ArrowArrayStream -> Arrow Java -> ArrowColumnVector -> ColumnarToRow -> RowToVeloxColumnar
    

    With the API, the engine imports the stream of the library directly. Library loading, the checksums of the packages and spark_ds_init stay in Spark, so the same library runs on Spark alone and under the engine.

    Does this PR introduce _any_ user-facing change?

    It adds an API for engines, SupportsArrowStreamRead, in 4.4.0. Nothing changes for queries. For a library, the partition_id of a read with Spark's own reader is now the index of the partition in the scan, rather than the partition of the task, which differs after a union or a coalesce; native data sources are new in the unreleased 4.4.0.

    How was this patch tested?

    "open the partitions of batch and micro-batch scans as Arrow C streams" in NativeDataSourceSuite acts as an engine: in a single task, it opens the streams of both partitions, reads them on another thread, and checks the data and the partitions that the library got. It also reads a stream to its end in its task and releases it after the task ended, from the driver, and checks that the context stays alive until then. It checks the preconditions of openArrowStream, including a completed task, a stream that is already open, which it leaves as it is, and a partition of the other kind of scan, and that a failing read, a library that does not initialize the stream, and a stream that Spark rejects raise NATIVE_DATA_SOURCE_ERROR and leave the stream released; the library's own release is called for the stream that Spark rejects. After a stream is released, a copy of it gets an error from its callbacks, and releasing the copy does nothing. It reads the partitions of two micro-batches with the reader factory of the first, through the stream and through Spark's reader, and the test library checks that read gets the state of the micro-batch of its partition. It checks that a serialized partition of a micro-batch does not contain the state, and that a micro-batch without a state is not broadcast. The reads of micro-batches check the partition_id that the library gets, in a task of another partition, and that every task context is closed. "pass the task to reads and data writers" checks the partition_id of reads after a union and a coalesce, and of a union of two streams. The layout test also checks the offsets of ArrowArrayStream.

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

    Generated-by: Claude Code (Claude Opus 5.5)

    This pull request and its description were written by Isaac.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: HyukjinKwon Review duration: 6720m
    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 #59934"
    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%
    Repository