<?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=Arrow-optimized_Python_UDF</id>
	<title>Arrow-optimized Python UDF - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Arrow-optimized_Python_UDF"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Arrow-optimized_Python_UDF&amp;action=history"/>
	<updated>2026-12-06T00:07:20Z</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=Arrow-optimized_Python_UDF&amp;diff=124&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=Arrow-optimized_Python_UDF&amp;diff=124&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:34Z</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            = Arrow-optimized Python UDF&lt;br /&gt;
| also_known_as   = Arrow Python UDF, &amp;lt;code&amp;gt;useArrow=True&amp;lt;/code&amp;gt; UDF&lt;br /&gt;
| field           = Distributed data processing&lt;br /&gt;
| introduced      = Apache Spark 3.5.0 (SPARK-40307); default since Spark 4.2.0&lt;br /&gt;
| key_parts       = &amp;lt;code&amp;gt;udf(useArrow=...)&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;spark.sql.execution.pythonUDF.arrow.enabled&amp;lt;/code&amp;gt;, Arrow record batches, Python worker&lt;br /&gt;
| implementations = [[Apache Spark]] (PySpark)&lt;br /&gt;
| related         = [[User-defined function (Spark)]], [[Pandas UDF]], [[Apache Arrow]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (SQLConf.scala, python/pyspark/sql/functions/builtin.py, the &amp;quot;Apache Arrow in PySpark&amp;quot; tutorial, the PySpark UDF user guide, the PySpark migration guide), the Spark 3.5.0, 4.0.0 and 4.2.0 release notes and the 2023 Databricks blog post on the feature}}&lt;br /&gt;
&lt;br /&gt;
In PySpark, an &amp;#039;&amp;#039;&amp;#039;Arrow-optimized Python UDF&amp;#039;&amp;#039;&amp;#039; is a regular row-at-a-time Python [[User-defined function (Spark)|user-defined function]] whose input and output move between the JVM and the Python worker as [[Apache Arrow]] batches instead of pickled Python objects.&amp;lt;ref name=&amp;quot;arrow&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/api/python/tutorial/sql/arrow_pandas.html |title=Apache Arrow in PySpark (PySpark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The function code is the same as for a pickled UDF; only the transfer and the type coercion rules change.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; [[Apache Spark]] introduced Arrow-optimized Python UDFs in version 3.5.0 and made them the default for &amp;lt;code&amp;gt;pyspark.sql.functions.udf&amp;lt;/code&amp;gt; in version 4.2.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;&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;
== What changed compared with pickled Python UDFs? ==&lt;br /&gt;
A pickled Python UDF serializes every row with cloudpickle on its way to and from the Python worker, and the PySpark user guide says this causes performance bottlenecks with large inputs and outputs.&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; An Arrow-optimized Python UDF still calls the function once per row, but Spark ships the rows in Arrow batches, a columnar format shared by the JVM and Python.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;guide&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Arrow-optimized Python UDF !! Pickled Python UDF&lt;br /&gt;
|-&lt;br /&gt;
! Serialization between JVM and Python&lt;br /&gt;
| Apache Arrow batches || Pickle (cloudpickle)&lt;br /&gt;
|-&lt;br /&gt;
! Function receives&lt;br /&gt;
| Python objects, one row per call || Python objects, one row per call&lt;br /&gt;
|-&lt;br /&gt;
! Type coercion of results&lt;br /&gt;
| Arrow type system; errors on unsafe conversions since Spark 4.1 || Falls back to &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; on many type mismatches&lt;br /&gt;
|-&lt;br /&gt;
! Default in Spark 4.3.0&lt;br /&gt;
| Yes (since 4.2.0) || No; set &amp;lt;code&amp;gt;useArrow=False&amp;lt;/code&amp;gt; or the configuration to &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
Sources for the table: the PySpark Arrow guide and the migration guide.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;upgrade&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a Databricks benchmark for Spark 3.5, an Arrow-optimized Python UDF ran about 1.6 times faster than a pickled Python UDF for a single transformation, and about 1.9 times faster when UDFs were chained on a 32 GB dataset, on a cluster of three workers with 16 vCPUs and 122 GiB each.&amp;lt;ref name=&amp;quot;blog&amp;quot;&amp;gt;{{Cite web |url=https://www.databricks.com/blog/arrow-optimized-python-udfs-apache-sparktm-35 |title=Arrow-optimized Python UDFs in Apache Spark 3.5 |publisher=Databricks |date=2023-11-06 |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How is Arrow optimization turned on or off? ==&lt;br /&gt;
The &amp;lt;code&amp;gt;useArrow&amp;lt;/code&amp;gt; argument of &amp;lt;code&amp;gt;pyspark.sql.functions.udf&amp;lt;/code&amp;gt; controls a single UDF. When &amp;lt;code&amp;gt;useArrow&amp;lt;/code&amp;gt; is not set (&amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt;), the session configuration &amp;lt;code&amp;gt;spark.sql.execution.pythonUDF.arrow.enabled&amp;lt;/code&amp;gt; decides.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&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;&lt;br /&gt;
&lt;br /&gt;
In Spark 4.3.0, &amp;lt;code&amp;gt;spark.sql.execution.pythonUDF.arrow.enabled&amp;lt;/code&amp;gt; defaults to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;, and the optimization applies only when the function takes at least one argument.&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 default changed to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; in Spark 4.2.0 (SPARK-54555); before that, Arrow optimization had to be requested explicitly.&amp;lt;ref name=&amp;quot;upgrade&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;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from pyspark.sql.functions import udf&lt;br /&gt;
&lt;br /&gt;
@udf(returnType=&amp;quot;int&amp;quot;)                  # Arrow-optimized by default in Spark 4.2+&lt;br /&gt;
def slen(s: str) -&amp;gt; int:&lt;br /&gt;
    return len(s)&lt;br /&gt;
&lt;br /&gt;
@udf(returnType=&amp;quot;int&amp;quot;, useArrow=False)  # pickled Python UDF&lt;br /&gt;
def slen_pickled(s: str) -&amp;gt; int:&lt;br /&gt;
    return len(s)&lt;br /&gt;
&lt;br /&gt;
# Restore pickled UDFs for every UDF in the session that does not set useArrow:&lt;br /&gt;
spark.conf.set(&amp;quot;spark.sql.execution.pythonUDF.arrow.enabled&amp;quot;, &amp;quot;false&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does type coercion differ? ==&lt;br /&gt;
Type coercion matters when the Python value a UDF returns does not match its declared return type. The PySpark guide says pickled UDFs often fall back to &amp;lt;code&amp;gt;None&amp;lt;/code&amp;gt; on a mismatch, which can lose data silently, and that converting dates, datetimes and tuples to strings can give ambiguous results; Arrow-optimized UDFs use Arrow&amp;#039;s type system to make the coercion consistent.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt; The full coercion tables are in the pull request that the guide links (apache/spark#41706).&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Several behaviour changes in Spark 4.x affect Arrow-optimized UDFs:&amp;lt;ref name=&amp;quot;upgrade&amp;quot; /&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 4.1&amp;#039;&amp;#039;&amp;#039; removed an unnecessary conversion to pandas objects in the serializer, which changes coercion when the output type differs from the declared schema. &amp;lt;code&amp;gt;spark.sql.legacy.execution.pythonUDF.pandas.conversion.enabled=true&amp;lt;/code&amp;gt; restores the old behaviour.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 4.1&amp;#039;&amp;#039;&amp;#039; supports user-defined types (UDTs) as input and output instead of falling back to a pickled UDF; &amp;lt;code&amp;gt;spark.sql.execution.pythonUDF.arrow.legacy.fallbackOnUDT=true&amp;lt;/code&amp;gt; restores the fallback.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 4.1&amp;#039;&amp;#039;&amp;#039; turned on &amp;lt;code&amp;gt;spark.sql.execution.pandas.convertToArrowArraySafely&amp;lt;/code&amp;gt; by default, so integer overflow, floating point truncation and loss of precision in returned data raise errors.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Spark 4.2&amp;#039;&amp;#039;&amp;#039; made Arrow optimization the default, as described above.&lt;br /&gt;
&lt;br /&gt;
== Other settings ==&lt;br /&gt;
&amp;lt;code&amp;gt;spark.sql.execution.pythonUDF.arrow.concurrency.level&amp;lt;/code&amp;gt;, added in Spark 4.0.0 (SPARK-50446), runs an Arrow-optimized Python UDF with more than one level of concurrency, which the configuration description says can help UDFs that do a lot of I/O. It has no default and must be greater than 1 when set.&amp;lt;ref name=&amp;quot;sqlconf&amp;quot; /&amp;gt;&amp;lt;ref&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;
The Arrow batch size is set by &amp;lt;code&amp;gt;spark.sql.execution.arrow.maxRecordsPerBatch&amp;lt;/code&amp;gt;, default 10,000 rows.&amp;lt;ref name=&amp;quot;arrow&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Arrow-optimized Python UDF vs pandas UDF and Arrow UDF ==&lt;br /&gt;
An Arrow-optimized Python UDF changes only the transfer; the function still handles one row at a time. A [[Pandas UDF]] or an Arrow UDF (&amp;lt;code&amp;gt;arrow_udf&amp;lt;/code&amp;gt;, Spark 4.1.0 and later) is vectorized: the function receives a whole batch as pandas or PyArrow objects.&amp;lt;ref name=&amp;quot;guide&amp;quot; /&amp;gt; Python UDTFs have the same switch, &amp;lt;code&amp;gt;spark.sql.execution.pythonUDTF.arrow.enabled&amp;lt;/code&amp;gt;, which also defaults to &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; since Spark 4.2; see [[Python UDTF]].&amp;lt;ref name=&amp;quot;upgrade&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[User-defined function (Spark)]]&lt;br /&gt;
* [[Pandas UDF]]&lt;br /&gt;
* [[Python UDTF]]&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>