[SPARK-60726][SQL] Add an API to read a partition of a native data source as an Arrow C stream #59934
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 #59934"
general
weight: 0.35
HyukjinKwon
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
@EvolvingAPI, like the other mix-ins oforg.apache.spark.sql.connector.read, to read a partition of a native scan as an Arrow C stream:The reader factories of the batch and micro-batch scans of native data sources implement it, with the stream that
readof 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: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.openArrowStreamis 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 theSparkDsTaskContextonce the library released its stream: it wraps the four callbacks of the stream with its own, which call those of the library, keyed by aprivate_dataof Spark. Once the task failed or completed,is_interruptedof 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 ifopenArrowStreamfails, the stream is released. The library gets the index of the partition in the scan aspartition_id, since the task of an engine may read several, and so does Spark's own reader now.NativePartitionReaderopens its streams with the same code. The release of an engine's stream closes itsNativeLibrary.Taskon 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,NativeMicroBatchPartitionReaderFactoryfor 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.openArrowStreamchecks the task and the stream before the partition, and rejects a partition of the other kind of scan with anIllegalArgumentException.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
ColumnarToRowExecthen converts it to rows, which the engine converts back to its own columnar format:With the API, the engine imports the stream of the library directly. Library loading, the checksums of the packages and
spark_ds_initstay 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, thepartition_idof 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
NativeDataSourceSuiteacts 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 ofopenArrowStream, 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 failingread, a library that does not initialize the stream, and a stream that Spark rejects raiseNATIVE_DATA_SOURCE_ERRORand leave the stream released; the library's ownreleaseis 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 thatreadgets 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 thepartition_idthat 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 thepartition_idof reads after a union and a coalesce, and of a union of two streams. The layout test also checks the offsets ofArrowArrayStream.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.