Arrow at the Border
A plain @udf runs under BatchEvalPython, and you pay for it value by value. The executor converts each field of an UnsafeRow into a Java object, a pickler on the JVM side turns those objects into bytes, and the Python worker unpickles every value into a fresh Python object before your function sees it. The result makes the same trip in reverse. A function that adds 1 to a number still costs several object allocations per field and 1 interpreted function call per row, and on a table of billions of rows those fixed costs are the job.
A layout both runtimes share
Apache Arrow is a columnar memory format with a published specification. Its Java and Python libraries read and write the same bytes. A record batch stores each column as a few contiguous buffers. Fixed-width values sit back to back in a values buffer, and a validity bitmap marks nulls with 1 bit per row. Strings and other variable-length types add an offsets buffer that records where each value starts. Since the executor and the Python worker agree on that layout, the executor streams buffers into a socket and the worker uses them as they arrive, with no decoding step per value.
| Buffer | product_name (string) | price (double) |
|---|---|---|
| Validity bitmap | 1 1 1 1 | 1 0 1 1 |
| Offsets | 0, 6, 9, 13, 17 | None, the width is fixed |
| Values | kettlemuglampvase | 19.99, unused, 5.00, 42.50 |
Read the table as 4 rows: a kettle at 19.99, a mug with no price, a lamp at 5.00 and a vase at 42.50. The mug's validity bit is 0, so its 8-byte slot in the values buffer holds a placeholder that nothing reads. The lamp's name is the characters between offsets 9 and 13 of a single run of text. Summing prices means walking a single block of memory, 8 bytes per row, which is the access pattern numpy and the CPU's vector instructions are built for.
- Executor task (transform, JVM, UnsafeRow)
- ArrowWriter (transform, JVM, rows to columns)
- Socket (queue, Arrow stream)
- Python worker (transform, buffers to pandas)
- Your function (transform, 1 call per batch)
- Next operator (consumer, JVM, result rows)
- Executor task -> ArrowWriter
- ArrowWriter -> Socket
- Socket -> Python worker
- Python worker -> Your function
- Your function -> Next operator
What changes per row
- Pickled path: BatchEvalPython
- Every field becomes a Java object, then pickle bytes, then a Python object
- Your function runs once for every row
- A null arrives as None, a number as a boxed Python int or float
- Cost scales with rows times columns, however short the function
- Arrow path: ArrowEvalPython
- Each column is copied once into Arrow buffers on the JVM side
- Your function runs once per batch, on whole pandas Series
- A numeric column becomes a numpy array in one bulk conversion
- Python call overhead is paid per batch, so cost tracks bytes moved
- 10,000
- 100
A setting, spark.sql.execution.arrow.maxRecordsPerBatch, caps the rows in a batch, and a batch never spans 2 partitions, so a partition under the cap crosses as a single batch. Bigger batches spread the per-call overhead across more rows, but the worker holds the whole batch as pandas objects at once. That memory belongs to the Python process, outside the JVM heap, and counts against spark.executor.memoryOverhead, so a wide table with long strings and a raised cap is a direct route to the memoryOverhead OOM.
Arrow does not make the crossing free. The JVM still copies every value out of its rows into column vectors on the way out, and copies each result back into a row for the next operator in the stage. What it removes is the per-value object churn in Python and the per-row function call, which is where the pickled path spends most of its time on a short function.
What the plan and the Spark UI show
In explain(), a vectorized UDF appears as ArrowEvalPython where a plain one showed BatchEvalPython. Both are narrow operators: the round trip to Python happens inside the current stage, with no exchange, so the stage count and the shuffle read and write columns in the Spark UI stay put when you switch. The number that moves is task duration for the stage holding the UDF, and that is the one to record before and after.
A table has 200 partitions of 250,000 rows each, and a Python UDF reads one numeric column. How many times does Python invoke the function body as a plain @udf, and as a Series-to-Series @pandas_udf with the default batch cap?
- 500,000 calls as a plain UDF, because rows are pickled 100 at a time, and 5,000 as a pandas UDF
- 50 million calls as a plain UDF and 50 million as a pandas UDF, because Arrow changes only the wire format
- 50 million calls as a plain UDF, 1 per row, and 5,000 as a pandas UDF, 25 batches per partition
- 200 calls as a plain UDF and 200 as a pandas UDF, because each task opens 1 Python worker
About this lesson
This section is part of the UDFs and Serialization: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.