Arrow-optimized Python UDF
| Also known as | Arrow Python UDF, useArrow=True UDF
|
|---|---|
| Field | Distributed data processing |
| Introduced | Apache Spark 3.5.0 (SPARK-40307); default since Spark 4.2.0 |
| Key parts | udf(useArrow=...), spark.sql.execution.pythonUDF.arrow.enabled, Arrow record batches, Python worker
|
| Open source implementations | Apache Spark (PySpark) |
| Related | User-defined function (Spark), Pandas UDF, Apache Arrow |
In PySpark, an Arrow-optimized Python UDF is a regular row-at-a-time Python user-defined function whose input and output move between the JVM and the Python worker as Apache Arrow batches instead of pickled Python objects.[1] The function code is the same as for a pickled UDF; only the transfer and the type coercion rules change.[1] Apache Spark introduced Arrow-optimized Python UDFs in version 3.5.0 and made them the default for pyspark.sql.functions.udf in version 4.2.0.[2][3]
What changed compared with pickled Python UDFs?
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.[4] 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.[1][4]
| Arrow-optimized Python UDF | Pickled Python UDF | |
|---|---|---|
| Serialization between JVM and Python | Apache Arrow batches | Pickle (cloudpickle) |
| Function receives | Python objects, one row per call | Python objects, one row per call |
| Type coercion of results | Arrow type system; errors on unsafe conversions since Spark 4.1 | Falls back to None on many type mismatches
|
| Default in Spark 4.3.0 | Yes (since 4.2.0) | No; set useArrow=False or the configuration to false
|
Sources for the table: the PySpark Arrow guide and the migration guide.[1][3]
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.[5]
How is Arrow optimization turned on or off?
The useArrow argument of pyspark.sql.functions.udf controls a single UDF. When useArrow is not set (None), the session configuration spark.sql.execution.pythonUDF.arrow.enabled decides.[1][6]
In Spark 4.3.0, spark.sql.execution.pythonUDF.arrow.enabled defaults to true, and the optimization applies only when the function takes at least one argument.[7] The default changed to true in Spark 4.2.0 (SPARK-54555); before that, Arrow optimization had to be requested explicitly.[3][8]
from pyspark.sql.functions import udf
@udf(returnType="int") # Arrow-optimized by default in Spark 4.2+
def slen(s: str) -> int:
return len(s)
@udf(returnType="int", useArrow=False) # pickled Python UDF
def slen_pickled(s: str) -> int:
return len(s)
# Restore pickled UDFs for every UDF in the session that does not set useArrow:
spark.conf.set("spark.sql.execution.pythonUDF.arrow.enabled", "false")
How does type coercion differ?
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 None 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's type system to make the coercion consistent.[1] The full coercion tables are in the pull request that the guide links (apache/spark#41706).[1]
Several behaviour changes in Spark 4.x affect Arrow-optimized UDFs:[3]
- Spark 4.1 removed an unnecessary conversion to pandas objects in the serializer, which changes coercion when the output type differs from the declared schema.
spark.sql.legacy.execution.pythonUDF.pandas.conversion.enabled=truerestores the old behaviour. - Spark 4.1 supports user-defined types (UDTs) as input and output instead of falling back to a pickled UDF;
spark.sql.execution.pythonUDF.arrow.legacy.fallbackOnUDT=truerestores the fallback. - Spark 4.1 turned on
spark.sql.execution.pandas.convertToArrowArraySafelyby default, so integer overflow, floating point truncation and loss of precision in returned data raise errors. - Spark 4.2 made Arrow optimization the default, as described above.
Other settings
spark.sql.execution.pythonUDF.arrow.concurrency.level, 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.[7][9]
The Arrow batch size is set by spark.sql.execution.arrow.maxRecordsPerBatch, default 10,000 rows.[1]
Arrow-optimized Python UDF vs pandas UDF and Arrow UDF
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 (arrow_udf, Spark 4.1.0 and later) is vectorized: the function receives a whole batch as pandas or PyArrow objects.[4] Python UDTFs have the same switch, spark.sql.execution.pythonUDTF.arrow.enabled, which also defaults to true since Spark 4.2; see Python UDTF.[3]
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 Apache Arrow in PySpark (PySpark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 3.5.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 3.0 3.1 3.2 3.3 3.4 Upgrading PySpark (PySpark 4.3.0 migration guide). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 4.0 4.1 4.2 Chapter 5: Unleashing UDFs & UDTFs (PySpark 4.3.0 user guide). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Arrow-optimized Python UDFs in Apache Spark 3.5. Databricks, 2023-11-06.Retrieved 2026-11-23.
- ↑ python/pyspark/sql/functions/builtin.py (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 7.0 7.1 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 4.2.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 4.0.0. Apache Software Foundation.Retrieved 2026-11-23.