Opaque to Catalyst

What Catalyst sees

Catalyst optimizes a query by reading its expressions. Given F.col("price") >= 200, it can see which column is compared with which constant, and by what operator, so it can work out which rows survive and where else the test could run. Given price_band_udf(F.col("price")) == "premium", it knows that a Python function takes price and returns a string, and nothing more. The comparisons inside the function never reach the optimizer, so it cannot tell which prices end up premium.

Built-in filter: price >= 200
  • The column it reads: price
  • The operator and the constant 200
  • That a NULL price never passes
  • That the test can run in the file reader
UDF filter: band == premium
  • The column it reads: price
  • That the function returns a string
  • Nothing about which prices pass
  • Nothing it can hand to the file reader

The filter that cannot move

Pushdown is the rewrite that moves a filter as close to the data source as it can go. When products is stored as Parquet, Catalyst hands each pushable filter to the file reader, which skips data that cannot match instead of returning every row to Spark. A filter is pushable when it compares a raw column with a value. A test on a UDF's output never qualifies, because the reader cannot run your Python.

from pyspark.sql import functions as F

products = spark.read.parquet("/data/products")

premium_builtin = products.where(F.col("price") >= 200)
premium_udf = products.where(price_band_udf(F.col("price")) == "premium")

premium_builtin.explain()
premium_udf.explain()

In each plan, find the scan line and read its PushedFilters list.

FilterPushedFilters on the scanWhat reaches the filter
`F.col("price") >= 200``[IsNotNull(price), GreaterThanOrEqual(price,200.0)]`Only data the reader could not rule out
`price_band_udf("price") == "premium"``[]`Every row, each price serialized for the function

On a table where every price is set, both DataFrames keep the same products. The built-in version lets the reader drop data before Spark handles it, and Catalyst added the IsNotNull test itself because a NULL price can never satisfy >= 200. The UDF version reads all of products, converts every price for the function, converts every answer back, and only then discards the rows that are not premium. If any price is NULL, this price_band_udf raises its TypeError at the action instead, a failure the Nulls and Types section fixes.

The Spark UI shows the gap on the scan's stage as input size. The UDF version's input matches the whole table, while the built-in version's can be far smaller when the files let the reader skip data.

Wrapping a column in a built-in, as in F.lower(F.col("category")) == "toys", also keeps that comparison out of the file reader. The built-in still avoids the conversion and stays readable to Catalyst, so a selective filter is best written on the raw column.

Which of the 2 filters can Catalyst hand to the Parquet reader?

@F.udf(returnType="string")
def normalize(term):
    return term.strip().lower()

searches = spark.read.parquet("/data/search_queries")

a = searches.where(F.col("results_count") == 0)
b = searches.where(normalize(F.col("search_term")) == "shoes")

A fast function still costs

None of this depends on how quickly the function runs. A UDF whose body returns its input unchanged still blocks pushdown on its result and still pays the conversion on every row. Rewriting the body to run 10 times faster changes the time inside the function and nothing around it.

What a UDF takes from the plan

About this lesson

This section is part of the UDFs and Serialization: Beginner 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.