<?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_UDTF</id>
	<title>Python UDTF - 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_UDTF"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Python_UDTF&amp;action=history"/>
	<updated>2026-12-06T00:13:09Z</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_UDTF&amp;diff=170&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_UDTF&amp;diff=170&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 UDTF&lt;br /&gt;
| also_known_as   = Python user-defined table function, &amp;lt;code&amp;gt;@udtf&amp;lt;/code&amp;gt;&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| introduced      = Apache Spark 3.5.0 (SPARK-43797)&lt;br /&gt;
| key_parts       = Handler class with &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;; optional &amp;lt;code&amp;gt;__init__&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;analyze&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;terminate&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;cleanup&amp;lt;/code&amp;gt;; output schema; table arguments&lt;br /&gt;
| implementations = [[Apache Spark]] (PySpark)&lt;br /&gt;
| related         = [[User-defined function (Spark)]], [[Arrow-optimized Python UDF]], [[Python data source API (Spark)]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (the PySpark UDTF tutorial, python/pyspark/sql/functions/builtin.py, python/pyspark/sql/udtf.py, python/pyspark/sql/connect/udtf.py, examples/src/main/python/sql/udtf.py, SQLConf.scala, the PySpark migration guide) and the Spark 3.5.0 and 4.1.0 release notes}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], a &amp;#039;&amp;#039;&amp;#039;Python UDTF&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;Python user-defined table function&amp;#039;&amp;#039;&amp;#039;) is a Python class, registered with PySpark, whose &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; method can return zero or more rows for each call, so that the function produces a whole table instead of a single value.&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_udtf.html |title=Python User-defined Table Functions (UDTFs) (PySpark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; A UDTF is called in the &amp;lt;code&amp;gt;FROM&amp;lt;/code&amp;gt; clause of a SQL query or directly from Python, and its arguments can be scalar expressions or one input table.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; Python UDTFs were introduced in Spark 3.5.0.&amp;lt;ref name=&amp;quot;r350&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/releases/spark-release-3-5-0.html |title=Spark Release 3.5.0 |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What is a Python UDTF used for? ==&lt;br /&gt;
The PySpark user guide recommends a Python UDTF when a function must generate several rows and columns and benefit from Python libraries.&amp;lt;ref name=&amp;quot;guide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/user_guide/udfandudtf.html |title=Chapter 5: Unleashing UDFs &amp;amp; UDTFs (PySpark 4.3.0 user guide) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; It lists these use cases:&amp;lt;ref name=&amp;quot;guide&amp;quot; /&amp;gt;&lt;br /&gt;
* Exploding nested data such as arrays or structs into multiple rows.&lt;br /&gt;
* Splitting strings into several rows or columns.&lt;br /&gt;
* Generating rows from input ranges, such as sequences of numbers, timestamps or dates.&lt;br /&gt;
* Computations that need Python libraries, such as statistical functions or machine learning inference, where a SQL table function would not be enough.&lt;br /&gt;
&lt;br /&gt;
A scalar [[User-defined function (Spark)|UDF]] returns exactly one value per input row; a UDTF can return any number of rows, each with several columns.&amp;lt;ref name=&amp;quot;guide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do you write a Python UDTF? ==&lt;br /&gt;
A Python UDTF is a class with a required &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; method that yields output rows as tuples, lists or &amp;lt;code&amp;gt;pyspark.sql.Row&amp;lt;/code&amp;gt; objects. Optional methods are &amp;lt;code&amp;gt;__init__&amp;lt;/code&amp;gt; (called once per UDTF instance on the executor), &amp;lt;code&amp;gt;terminate&amp;lt;/code&amp;gt; (called after all input rows of a partition have been processed, and able to yield more rows), &amp;lt;code&amp;gt;cleanup&amp;lt;/code&amp;gt; (always called at the end, even after an error) and a static &amp;lt;code&amp;gt;analyze&amp;lt;/code&amp;gt; method.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output schema is a &amp;lt;code&amp;gt;StructType&amp;lt;/code&amp;gt; or a DDL string, given in the &amp;lt;code&amp;gt;returnType&amp;lt;/code&amp;gt; of the &amp;lt;code&amp;gt;@udtf&amp;lt;/code&amp;gt; decorator or returned by &amp;lt;code&amp;gt;analyze&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; A one-column row must be yielded with a trailing comma, for example &amp;lt;code&amp;gt;yield x,&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following PySpark example runs on Spark 4.3.0. It defines a UDTF, calls it from Python, registers it and calls it from SQL, including with a &amp;lt;code&amp;gt;LATERAL&amp;lt;/code&amp;gt; join.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql.functions import lit, udtf&lt;br /&gt;
&lt;br /&gt;
@udtf(returnType=&amp;quot;word: string&amp;quot;)&lt;br /&gt;
class WordSplitter:&lt;br /&gt;
    def eval(self, text: str):&lt;br /&gt;
        for word in text.split(&amp;quot; &amp;quot;):&lt;br /&gt;
            yield (word.strip(),)&lt;br /&gt;
&lt;br /&gt;
WordSplitter(lit(&amp;quot;hello world&amp;quot;)).show()&lt;br /&gt;
&lt;br /&gt;
spark.udtf.register(&amp;quot;split_words&amp;quot;, WordSplitter)&lt;br /&gt;
spark.sql(&amp;quot;SELECT * FROM split_words(&amp;#039;hello world&amp;#039;)&amp;quot;).show()&lt;br /&gt;
spark.sql(&lt;br /&gt;
    &amp;quot;SELECT * FROM VALUES (&amp;#039;Hello World&amp;#039;), (&amp;#039;Apache Spark&amp;#039;) t(text), &amp;quot;&lt;br /&gt;
    &amp;quot;LATERAL split_words(text)&amp;quot;&lt;br /&gt;
).show()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The example follows the UDTF examples shipped with Spark 4.3.0.&amp;lt;ref name=&amp;quot;ex&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/examples/src/main/python/sql/udtf.py |title=examples/src/main/python/sql/udtf.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do table arguments work? ==&lt;br /&gt;
A Python UDTF can take one input table as an argument, alongside scalar arguments. In SQL the table is passed with the &amp;lt;code&amp;gt;TABLE&amp;lt;/code&amp;gt; keyword, either as &amp;lt;code&amp;gt;TABLE(t)&amp;lt;/code&amp;gt; or as a subquery such as &amp;lt;code&amp;gt;TABLE(SELECT a, b FROM t)&amp;lt;/code&amp;gt;. Each input row reaches &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; as a &amp;lt;code&amp;gt;Row&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The call can ask Spark to partition the input table: &amp;lt;code&amp;gt;PARTITION BY&amp;lt;/code&amp;gt; guarantees that all rows with the same values of the partitioning expressions are consumed by one instance of the UDTF class, and &amp;lt;code&amp;gt;WITH SINGLE PARTITION&amp;lt;/code&amp;gt; sends all rows to one instance. An &amp;lt;code&amp;gt;ORDER BY&amp;lt;/code&amp;gt; clause sets the row order within each partition.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; For example: &amp;lt;code&amp;gt;SELECT * FROM my_udtf(TABLE(events) PARTITION BY user_id ORDER BY ts)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;analyze&amp;lt;/code&amp;gt; method can request the same partitioning, ordering and column selection itself, through the &amp;lt;code&amp;gt;partitionBy&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;withSinglePartition&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;orderBy&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;select&amp;lt;/code&amp;gt; fields of &amp;lt;code&amp;gt;AnalyzeResult&amp;lt;/code&amp;gt;, so that callers can write just &amp;lt;code&amp;gt;TABLE(t)&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; &amp;lt;code&amp;gt;analyze&amp;lt;/code&amp;gt; also lets a UDTF compute its output schema from its arguments at query planning time, instead of declaring a fixed &amp;lt;code&amp;gt;returnType&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; method can raise &amp;lt;code&amp;gt;SkipRestOfInputTableException&amp;lt;/code&amp;gt; to skip the remaining rows of the current partition and go straight to &amp;lt;code&amp;gt;terminate&amp;lt;/code&amp;gt;; any other exception aborts the UDTF and fails the query.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Is a Python UDTF Arrow-optimized? ==&lt;br /&gt;
Since Spark 4.2, Python UDTFs use [[Apache Arrow]] for data transfer by default: &amp;lt;code&amp;gt;spark.sql.execution.pythonUDTF.arrow.enabled&amp;lt;/code&amp;gt; defaults to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, and setting it to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt; restores the earlier behaviour.&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;&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; The &amp;lt;code&amp;gt;useArrow&amp;lt;/code&amp;gt; argument of &amp;lt;code&amp;gt;@udtf&amp;lt;/code&amp;gt; overrides the configuration for one UDTF.&amp;lt;ref name=&amp;quot;builtin&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/functions/builtin.py |title=python/pyspark/sql/functions/builtin.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The tutorial says Arrow helps most when each input row produces a large result table.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt; Arrow optimization for UDTFs was first added in Spark 3.5.0 (SPARK-43964).&amp;lt;ref name=&amp;quot;r350&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An Arrow-optimized UDTF still runs &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; on Python objects. Spark 4.1.0 added a separate decorator, &amp;lt;code&amp;gt;arrow_udtf&amp;lt;/code&amp;gt; (SPARK-52979), whose &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt; receives PyArrow &amp;lt;code&amp;gt;RecordBatch&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;Array&amp;lt;/code&amp;gt; objects and yields PyArrow tables or record batches, for vectorized processing without row-by-row overhead.&amp;lt;ref name=&amp;quot;builtin&amp;quot; /&amp;gt;&amp;lt;ref&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;&lt;br /&gt;
&lt;br /&gt;
== Limitations ==&lt;br /&gt;
* A UDTF cannot create or reference the Spark session; trying to do so causes a serialization error.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
* A UDTF call accepts at most one table argument.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;terminate&amp;lt;/code&amp;gt; may hold a whole partition in memory if the UDTF buffers rows until the end, and the tutorial warns about memory use in that pattern.&amp;lt;ref name=&amp;quot;tut&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PySpark 4.3.0 also ships a Python UDTF implementation for [[Spark Connect]] clients (&amp;lt;code&amp;gt;pyspark.sql.connect.udtf&amp;lt;/code&amp;gt;).&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/connect/udtf.py |title=python/pyspark/sql/connect/udtf.py (v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[User-defined function (Spark)]]&lt;br /&gt;
* [[Arrow-optimized Python UDF]]&lt;br /&gt;
* [[Pandas UDF]]&lt;br /&gt;
* [[Python data source API (Spark)]]&lt;br /&gt;
* [[Apache Arrow]]&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>