BatchEvalPython in the Plan
A Python UDF is opaque to Catalyst, yet the physical plan still marks exactly where the border sits. Catalyst lifts each Python UDF out of the expression you wrote and evaluates it in a dedicated operator, BatchEvalPython, which takes rows from the operator below, sends the UDF's inputs to the workers and appends the results as new columns. Find that node and you can read off which rows and columns cross, and how many times.
def clean_name(s):
return s.strip().lower() if s is not None else None
clean_udf = F.udf(clean_name, "string")
named = (
spark.read.parquet("/data/products")
.filter(F.col("in_stock") == 1)
.select("product_id", clean_udf("product_name").alias("clean_name"))
)
named.explain()Its plan, trimmed to operator names and columns. Read it from the bottom up:
| Plan line | What it tells you |
|---|---|
| `Project [product_id#0, pythonUDF0#12 AS clean_name#9]` | Names the returned column, back in the JVM |
| `+- BatchEvalPython [clean_name(product_name#1)#8], [pythonUDF0#12]` | The border: the call with its 1 input column, then the result column it appends |
| `+- Project [product_id#0, product_name#1]` | Only 2 columns continue, since `in_stock` has done its job |
| `+- Filter (isnotnull(in_stock#5) AND (in_stock#5 = 1))` | A built-in filter below the border: rows it drops never cross |
| `+- FileScan parquet [product_id#0,product_name#1,in_stock#5] PushedFilters: [IsNotNull(in_stock), EqualTo(in_stock,1)]` | The same condition pushed into the Parquet read |
The call inside the node carries the Python function's name, so a function defined with def and a clear name keeps plans readable, while a lambda shows only a generic label. The columns inside the call are what gets pickled. No Exchange appears, which agrees with the Spark UI: the crossing happens inside 1 stage and adds no shuffle.
A UDF inside a filter
Catalyst cannot push a predicate it cannot read into the scan, so a UDF condition is evaluated in BatchEvalPython and a Filter above it keeps the rows where it returned true. Here the condition also holds a built-in comparison:
valid = (
spark.read.parquet("/data/users")
.filter(is_valid_udf("email") & (F.col("account_status") == "active"))
.select("user_id", "email")
)| Plan line | What it tells you |
|---|---|
| `Project [user_id#0, email#2]` | Keeps the 2 selected columns |
| `+- Filter pythonUDF0#20: boolean` | Keeps rows where the UDF returned true, after they crossed |
| `+- BatchEvalPython [is_valid(email#2)#19], [pythonUDF0#20]` | Every row that passed the filter below crosses here |
| `+- Filter (isnotnull(account_status#4) AND (account_status#4 = active))` | The built-in half of the condition, below the border |
| `+- FileScan parquet [user_id#0,email#2,account_status#4] PushedFilters: [IsNotNull(account_status), EqualTo(account_status,active)]` | That half also reached the Parquet read, and the UDF half could not |
Catalyst split the &. The built-in comparison moved below the border and into the scan, so only active users cross, and that split happens whichever half you write first. What matters is that a selective built-in condition exists. If the UDF were the only condition, every row in users would cross.
2 crossings in one plan
UDFs whose inputs are plain columns run together in a single BatchEvalPython. When a built-in expression sits between 2 UDFs, as in score_udf(F.concat(clean_udf("name"), F.lit("|"))), the first result has to come back to the JVM before the second input can be built. The plan shows 2 BatchEvalPython nodes stacked on each other, and every row crosses twice.
Pricing a plan before it runs
From plan to price
- Count the BatchEvalPython nodes: each is a round trip for every row that reaches it.
- Read the input columns inside each call and estimate their bytes per row.
- Estimate the rows reaching each node from the filters and scan beneath it.
- Multiply rows by the per-row core cost your benchmark measured, then divide by the cores.
Suppose users holds 300 million rows, 10% of them are active, and a benchmark of is_valid measured 5 microseconds of core time per row. With the built-in filter below the border, 30 million rows cross: 150 core-seconds, about 1.5 seconds of wall time on 100 cores. With the UDF as the only condition, all 300 million cross for 1,500 core-seconds. A single built-in condition cut 90% of the UDF's cost before any rewrite.
A nightly job reads 400 million page_views rows, 20% of them from mobile. Which change cuts the UDF's cost the most?
views = spark.read.parquet("/data/page_views")
checkout = (
views.withColumn("path", path_udf("page_url"))
.filter(F.col("path") == "/checkout")
.filter(F.col("device") == "mobile")
)- Move the device filter above withColumn in the code, so fewer rows reach the UDF
- Add repartition(400) before the UDF so more Python workers share the work
- Replace path_udf with F.expr("parse_url(page_url, 'PATH')"), deleting the crossing
- Filter on path first, so Catalyst can push the UDF condition into the scan
Some UDFs leave nothing to price, because a built-in does their whole job. A UDF filter that compares a column to a constant becomes F.col("event_type") == "purchase", which Catalyst can push into the FileScan as a PushedFilters entry. A UDF that fills a missing value becomes F.coalesce inside the Project. Run explain() after the rewrite: a plan with no BatchEvalPython node sends no row to Python.
About this lesson
This section is part of the UDFs and Serialization: Intermediate 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.