<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Python_data_source_API_(Spark)</id>
	<title>Python data source API (Spark) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Python_data_source_API_(Spark)"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Python_data_source_API_(Spark)&amp;action=history"/>
	<updated>2026-12-06T00:25:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Python_data_source_API_(Spark)&amp;diff=169&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Python_data_source_API_(Spark)&amp;diff=169&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:48Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Python data source API (Spark)&lt;br /&gt;
| also_known_as   = Python Data Source API, PySpark data source, &amp;lt;code&amp;gt;pyspark.sql.datasource&amp;lt;/code&amp;gt;&lt;br /&gt;
| field           = Data integration, distributed data processing&lt;br /&gt;
| introduced      = Apache Spark 4.0.0 (SPIP SPARK-44076)&lt;br /&gt;
| key_parts       = &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DataSourceReader&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DataSourceWriter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DataSourceStreamReader&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SimpleDataSourceStreamReader&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DataSourceStreamWriter&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;InputPartition&amp;lt;/code&amp;gt;&lt;br /&gt;
| implementations = [[Apache Spark]] (PySpark)&lt;br /&gt;
| related         = [[Structured Streaming]], [[Predicate pushdown]], [[Apache Arrow]], [[Python UDTF]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (the PySpark Python Data Source tutorial, python/pyspark/sql/datasource.py, SQLConf.scala, the PySpark migration guide) and the Spark 4.0.0, 4.1.0 and 4.2.0 release notes}}&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Python data source API&amp;#039;&amp;#039;&amp;#039; of [[Apache Spark]] is a set of PySpark base classes, in the module &amp;lt;code&amp;gt;pyspark.sql.datasource&amp;lt;/code&amp;gt;, for writing a custom data source or sink entirely in Python and using it with &amp;lt;code&amp;gt;spark.read&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;df.write&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spark.readStream&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;writeStream&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/tutorial/sql/python_data_source.html |title=Python Data Source API (PySpark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The API was introduced in Spark 4.0.0 under the improvement proposal SPARK-44076.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;r400&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-0-0.html |title=Spark Release 4.0.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does a Python data source work? ==&lt;br /&gt;
A Python data source is a subclass of &amp;lt;code&amp;gt;DataSource&amp;lt;/code&amp;gt;. It gives a short &amp;lt;code&amp;gt;name()&amp;lt;/code&amp;gt; used in &amp;lt;code&amp;gt;format(...)&amp;lt;/code&amp;gt;, an optional default &amp;lt;code&amp;gt;schema()&amp;lt;/code&amp;gt;, and one factory method per capability.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Source (read) !! Sink (write)&lt;br /&gt;
|-&lt;br /&gt;
! Batch&lt;br /&gt;
| &amp;lt;code&amp;gt;reader()&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;DataSourceReader&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;writer()&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;DataSourceWriter&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Streaming&lt;br /&gt;
| &amp;lt;code&amp;gt;streamReader()&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;DataSourceStreamReader&amp;lt;/code&amp;gt;, or &amp;lt;code&amp;gt;simpleStreamReader()&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;SimpleDataSourceStreamReader&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;streamWriter()&amp;lt;/code&amp;gt; returns a &amp;lt;code&amp;gt;DataSourceStreamWriter&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
Source for the table: the PySpark 4.3.0 Python Data Source tutorial.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A batch reader can implement &amp;lt;code&amp;gt;partitions()&amp;lt;/code&amp;gt; to return a list of &amp;lt;code&amp;gt;InputPartition&amp;lt;/code&amp;gt; objects; if it returns N partitions, Spark plans N tasks that run &amp;lt;code&amp;gt;read(partition)&amp;lt;/code&amp;gt; in parallel, and by default there is a single partition.&amp;lt;ref name=&amp;quot;api&amp;quot; /&amp;gt; &amp;lt;code&amp;gt;read&amp;lt;/code&amp;gt; yields tuples or &amp;lt;code&amp;gt;Row&amp;lt;/code&amp;gt; objects, or PyArrow &amp;lt;code&amp;gt;RecordBatch&amp;lt;/code&amp;gt; objects.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; The tutorial says that yielding Arrow record batches avoids row-by-row processing and can improve performance by up to an order of magnitude on large datasets.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The data source, reader and writer classes and their methods must be picklable, because Spark ships them to executors. Libraries used inside a method must be imported inside that method.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Example: a batch reader ==&lt;br /&gt;
The following PySpark example works on Spark 4.3.0. It defines a source named &amp;lt;code&amp;gt;simple&amp;lt;/code&amp;gt; that returns two rows.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql.datasource import DataSource, DataSourceReader&lt;br /&gt;
from pyspark.sql.types import StructType&lt;br /&gt;
&lt;br /&gt;
class SimpleDataSource(DataSource):&lt;br /&gt;
    @classmethod&lt;br /&gt;
    def name(cls):&lt;br /&gt;
        return &amp;quot;simple&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def schema(self):&lt;br /&gt;
        return &amp;quot;name string, age int&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def reader(self, schema: StructType):&lt;br /&gt;
        return SimpleReader()&lt;br /&gt;
&lt;br /&gt;
class SimpleReader(DataSourceReader):&lt;br /&gt;
    def read(self, partition):&lt;br /&gt;
        yield (&amp;quot;Alice&amp;quot;, 20)&lt;br /&gt;
        yield (&amp;quot;Bob&amp;quot;, 30)&lt;br /&gt;
&lt;br /&gt;
spark.dataSource.register(SimpleDataSource)&lt;br /&gt;
spark.read.format(&amp;quot;simple&amp;quot;).load().show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Options passed with &amp;lt;code&amp;gt;.option(...)&amp;lt;/code&amp;gt; reach the source as &amp;lt;code&amp;gt;self.options&amp;lt;/code&amp;gt;, a dictionary whose values are all strings. A caller can override the default schema with &amp;lt;code&amp;gt;.schema(...)&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do writers commit data? ==&lt;br /&gt;
A &amp;lt;code&amp;gt;DataSourceWriter&amp;lt;/code&amp;gt; implements &amp;lt;code&amp;gt;write(rows)&amp;lt;/code&amp;gt;, which runs in each task and returns a &amp;lt;code&amp;gt;WriterCommitMessage&amp;lt;/code&amp;gt;. After all tasks succeed, Spark calls &amp;lt;code&amp;gt;commit(messages)&amp;lt;/code&amp;gt; on the driver with the messages from every task; if any task fails, it calls &amp;lt;code&amp;gt;abort(messages)&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; Batch writes support the &amp;lt;code&amp;gt;append&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;overwrite&amp;lt;/code&amp;gt; modes.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; Spark 4.0.0 also added &amp;lt;code&amp;gt;DataSourceArrowWriter&amp;lt;/code&amp;gt;, which receives PyArrow record batches instead of rows (SPARK-50471).&amp;lt;ref name=&amp;quot;api&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/datasource.py |title=python/pyspark/sql/datasource.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;r400&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do streaming readers work? ==&lt;br /&gt;
A Python data source can act as a [[Structured Streaming]] source. A &amp;lt;code&amp;gt;DataSourceStreamReader&amp;lt;/code&amp;gt; tracks progress with offsets, which are dictionaries:&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;initialOffset()&amp;lt;/code&amp;gt; returns the starting offset.&lt;br /&gt;
* &amp;lt;code&amp;gt;latestOffset()&amp;lt;/code&amp;gt; returns the offset that the next micro-batch reads up to.&lt;br /&gt;
* &amp;lt;code&amp;gt;partitions(start, end)&amp;lt;/code&amp;gt; splits the range between two offsets into &amp;lt;code&amp;gt;InputPartition&amp;lt;/code&amp;gt; objects.&lt;br /&gt;
* &amp;lt;code&amp;gt;read(partition)&amp;lt;/code&amp;gt; yields the rows of one partition on an executor.&lt;br /&gt;
* &amp;lt;code&amp;gt;commit(end)&amp;lt;/code&amp;gt; is called after the query has processed data up to &amp;lt;code&amp;gt;end&amp;lt;/code&amp;gt;, and can release resources.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;SimpleDataSourceStreamReader&amp;lt;/code&amp;gt; is a lighter interface for low-throughput sources that do not need partitioning: &amp;lt;code&amp;gt;read(start)&amp;lt;/code&amp;gt; returns both the rows and the next offset, and &amp;lt;code&amp;gt;readBetweenOffsets(start, end)&amp;lt;/code&amp;gt; must return the same rows deterministically when Spark replays a batch after a restart or failure.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; Since Spark 4.2, a simple reader that returns rows without advancing the offset fails with &amp;lt;code&amp;gt;SIMPLE_STREAM_READER_OFFSET_DID_NOT_ADVANCE&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;upgrade&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/migration_guide/pyspark_upgrade.html |title=Upgrading PySpark (PySpark 4.3.0 migration guide) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Spark 4.2.0 added admission control and &amp;lt;code&amp;gt;Trigger.AvailableNow&amp;lt;/code&amp;gt; support to Python streaming readers (SPARK-55304). A reader implements &amp;lt;code&amp;gt;getDefaultReadLimit()&amp;lt;/code&amp;gt;, for example returning &amp;lt;code&amp;gt;ReadMaxRows(20)&amp;lt;/code&amp;gt;, and its &amp;lt;code&amp;gt;latestOffset(start, limit)&amp;lt;/code&amp;gt; must honour the &amp;lt;code&amp;gt;ReadLimit&amp;lt;/code&amp;gt; that Spark passes in.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;r420&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4-2-0.html |title=Spark Release 4.2.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A &amp;lt;code&amp;gt;DataSourceStreamWriter&amp;lt;/code&amp;gt; has &amp;lt;code&amp;gt;write&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;commit(messages, batchId)&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;abort(messages, batchId)&amp;lt;/code&amp;gt;, so a sink can commit each micro-batch as a unit.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; An Arrow-based variant, &amp;lt;code&amp;gt;DataSourceStreamArrowWriter&amp;lt;/code&amp;gt;, was added in Spark 4.1.0.&amp;lt;ref name=&amp;quot;api&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Does the Python data source API support filter pushdown? ==&lt;br /&gt;
Spark 4.1.0 added a filter pushdown API (SPARK-51271).&amp;lt;ref name=&amp;quot;r410&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-4.1.0.html |title=Spark Release 4.1.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; A &amp;lt;code&amp;gt;DataSourceReader&amp;lt;/code&amp;gt; can override &amp;lt;code&amp;gt;pushFilters(filters)&amp;lt;/code&amp;gt;, which Spark calls once during planning with filter objects such as &amp;lt;code&amp;gt;EqualTo&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GreaterThan&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;In&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;IsNull&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;StringStartsWith&amp;lt;/code&amp;gt;; the method returns the filters that Spark must still evaluate after the scan. The API documentation recommends it only for sources that filter natively, such as databases.&amp;lt;ref name=&amp;quot;api&amp;quot; /&amp;gt; In Spark 4.3.0 the pushdown is controlled by the internal configuration &amp;lt;code&amp;gt;spark.sql.python.filterPushdown.enabled&amp;lt;/code&amp;gt;, which defaults to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; because it runs the Python worker one more time during planning.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; See [[Predicate pushdown]].&lt;br /&gt;
&lt;br /&gt;
== How is a Python data source registered? ==&lt;br /&gt;
&amp;lt;code&amp;gt;spark.dataSource.register(MyDataSource)&amp;lt;/code&amp;gt; registers a source for the current session (SPARK-45600).&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;r400&amp;quot; /&amp;gt; Registering a second source with the same name overwrites the first. Built-in and Scala or Java sources take precedence over a Python source with the same name.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A package can register a source automatically by exporting it as &amp;lt;code&amp;gt;DefaultSource&amp;lt;/code&amp;gt; in a top-level module whose name starts with &amp;lt;code&amp;gt;pyspark_&amp;lt;/code&amp;gt;; the tutorial cites the &amp;lt;code&amp;gt;pyspark_huggingface&amp;lt;/code&amp;gt; package as an example.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; Spark 4.0.0 also added Python data sources for [[Spark Connect]] clients (SPARK-47367), creating tables with a Python data source in SQL (SPARK-45597), and Python metrics (SPARK-46424).&amp;lt;ref name=&amp;quot;r400&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Spark 4.2, a source whose returned Arrow data has column types that do not match its declared schema fails with &amp;lt;code&amp;gt;DATA_SOURCE_RETURN_SCHEMA_MISMATCH&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;upgrade&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Structured Streaming]]&lt;br /&gt;
* [[Predicate pushdown]]&lt;br /&gt;
* [[Python UDTF]]&lt;br /&gt;
* [[User-defined function (Spark)]]&lt;br /&gt;
* [[Apache Arrow]]&lt;br /&gt;
* [[Spark Connect]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>