Sub topic 5
When queries fall back from Photon to Spark
This chapter assumes you know what Photon is and how to turn it on (see the earlier chapters). You should also be comfortable with basic SQL and with reading a Spark query plan.
Photon runs per operator, not per query
Photon does not take over a whole query. It replaces individual operators in the physical plan, such as scans, filters, joins, and aggregations. If Photon does not support an operator or expression, that part runs on the standard Spark engine. Photon then hands its data to Spark, and Spark continues the query.
This is called fallback. The query still returns correct results. The cost is speed, because data must be converted between Photon's columnar format and Spark's row-based processing at each boundary.
Common causes of fallback
- Python and Scala UDFs. These run row by row in a separate process or the JVM, so Photon cannot execute them.
- Unsupported expressions or functions. Photon covers many built-in functions, but not all of them.
- Unsupported data sources or formats. Photon is built for Delta and Parquet. Other sources may be read by Spark.
- Operators with no Photon version. Some operators in a plan have no Photon implementation.
Photon's coverage grows with each Databricks Runtime release. Check the documentation for your runtime version rather than relying on a fixed list.
Spotting fallback in a plan
Run EXPLAIN FORMATTED and look at the operator names. Photon operators carry a Photon prefix, for example PhotonScan or PhotonGroupingAgg. Operators without the prefix run in Spark.
EXPLAIN FORMATTED
SELECT c.region, SUM(o.amount) AS total
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE o.order_date >= '2024-01-01'
GROUP BY c.region;Read the plan from the bottom up. A healthy plan has Photon operators in an unbroken run. Look for a non-Photon operator sitting between Photon ones. That is usually where the fallback happens. The Query Profile (covered in the next chapter) also marks which operators ran in Photon.
Fixing the common case: UDFs
Replacing a Python UDF with built-in functions is the most frequent fix.
from pyspark.sql import functions as F
from pyspark.sql.types import StringType
# Slower: a Python UDF runs row by row outside the native engine.
normalize_udf = F.udf(lambda s: s.strip().upper() if s is not None else None, StringType())
slow = df.withColumn("region_norm", normalize_udf("region"))
# Preferred: built-in functions stay inside the optimized engine.
fast = df.withColumn("region_norm", F.upper(F.trim("region")))Both versions give the same output. Only the second can run in Photon.
A practical checklist
- Run
EXPLAIN FORMATTEDand find operators without thePhotonprefix. - Identify the expression or source that caused each one.
- Replace it with a built-in equivalent if one exists.
- Re-run the plan and confirm that the operator is now a Photon one.
- Compare runtimes, since removing fallback does not always change them much.
Exercise
A plan shows PhotonScan, then a non-Photon Project that calls a Python UDF, then PhotonGroupingAgg. What is the likely cause of the slowdown, and what is the first fix to try?
Show answer
The Python UDF cannot run in Photon, so the Project runs in Spark and the data must be converted at both boundaries. Rewrite the UDF logic with built-in functions, such as F.upper and F.trim, so the whole chain can stay in Photon.