Sub topic 5
When queries fall back from Photon to Spark
This chapter assumes you know basic SQL, can create a Databricks cluster or SQL warehouse, and understand Spark stages and tasks. It also assumes you have read the earlier chapter on what Photon is. Here you will learn why a query does not always run fully in Photon, how to spot the parts that don't, and what you can do about them.
Photon accelerates operators, not whole queries
Photon is a native, vectorized engine that replaces parts of the Spark execution path. It does not replace the whole query. Spark still plans the query, schedules the stages, and manages the tasks. Photon takes over individual operators in the physical plan, such as scans, filters, projections, hash joins, and aggregations.
When Photon supports an operator, that operator runs natively. When it does not, that operator runs in the standard Spark engine on the JVM. A single query can therefore mix both. This mixing is called fallback.
Fallback does not cause an error. The query returns the same results. The only visible effect is that part of the work runs at the slower, row-oriented speed you would get without Photon.
Why mixed execution costs more than you might expect
You might assume that a query with 90% of its operators in Photon gets 90% of the benefit. That is often not true, for two reasons.
- Transitions between engines add work. Photon works on columnar batches. A Spark operator that is not Photon-enabled works on its own row-based representation. Where the plan crosses from one to the other, the data must be converted. Conversion costs CPU time and memory.
- The slowest operator often dominates. If one unsupported operator sits on the critical path, such as a Python UDF applied to every row, it sets the runtime no matter how fast the other operators are.
The goal is not to reach 100% Photon coverage at any price. The goal is to make sure that the expensive operators in your query are the ones running in Photon.
Common causes of fallback
The exact list of supported operators and expressions grows with each Databricks Runtime release. Treat the list below as typical patterns, and check the Databricks documentation for your runtime version.
| Cause | What happens | Typical fix |
|---|---|---|
| Python or Scala UDFs | The function runs outside the native engine, row by row or in a separate Python process. | Replace with built-in functions. |
| Unsupported expressions or data types | The operator that contains the expression runs in Spark. | Rewrite the expression, or accept the fallback if the operator is cheap. |
| Unsupported operators or file formats | The scan or the operator runs in Spark. | Use Delta or Parquet where possible. |
| Unsupported compute | The cluster was created without Photon, or its runtime version or instance type does not support it. | Choose a supported runtime and node type. |
| Short queries | Photon is active, but the query is so small that there is little to speed up. | Do not tune these. |
The last row is a reminder that "not faster" is not always "fell back". A query that runs in 200 milliseconds has little to gain, and the planning and scheduling overhead is the same either way.
Check the compute first
Before you inspect any plan, confirm Photon is actually on. If it is off, every operator runs in Spark, and the plan will show no Photon operators at all.
On a cluster, the setting is runtime_engine in the Clusters API:
{
"cluster_name": "photon-demo",
"spark_version": "<a Databricks Runtime version string>",
"node_type_id": "<a node type available in your workspace>",
"num_workers": 2,
"runtime_engine": "PHOTON"
}Replace the placeholders with values valid in your workspace. SQL warehouses use Photon by default, so a plan with no Photon operators on a warehouse points to something other than a missing setting, usually an unsupported operation.
Finding fallback in a query plan
Run EXPLAIN FORMATTED on the query and read the physical plan:
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;In the output, Photon operators carry a Photon prefix, for example PhotonScan, PhotonBroadcastHashJoin, or PhotonGroupingAgg. Operators without that prefix, such as a plain HashAggregate or Project, ran in standard Spark.
When you read the plan, look for these signs:
- A non-Photon operator sitting between two Photon operators. This is the pattern in the diagram above, and it means two engine transitions.
- A non-Photon operator at the top of the plan with Photon operators below it. This is often harmless, because the expensive scan and aggregation work already ran natively.
- Operators such as
BatchEvalPythonorArrowEvalPython. These indicate a Python UDF.
Exact operator names vary by runtime version, so rely on the prefix convention rather than a memorized list.
Confirming in the Query Profile
The Query Profile for a completed query shows each operator as a node. Photon operators are marked, and the profile reports how much task time was spent in Photon. A low share of Photon time on a query that should be dominated by scans and aggregations is a sign that something is falling back.
Use the plan to learn which operator fell back, and the profile to learn whether it matters. An operator that accounts for 1% of the runtime is not worth rewriting.
A worked example: a UDF in the middle of a query
Suppose a table has messy region values, and you clean them with a Python UDF before aggregating:
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 compute the same column. In the plan for slow, you will see a Python evaluation operator between the scan and the aggregation. Every row must be sent to a Python process and the result sent back. In the plan for fast, the projection can stay in Photon, so the scan, projection, and aggregation remain native.
The built-in version is also visible to the Spark optimizer, which can push filters and prune columns around it. A UDF is a black box, so the optimizer cannot reason about it. A later chapter covers replacing UDFs in more detail.
Common mistakes
- Assuming Photon is on because the cluster is new. Check
runtime_engineor the cluster's Photon setting. Do not guess. - Chasing 100% coverage. Rewrite only the operators that take a meaningful share of the runtime.
- Judging by one run. The first run reads cold data and compiles code paths. Compare repeated runs, as the benchmarking chapter explains.
- Blaming Photon for a slow scan. If the query reads far too many files, the problem is table layout, not the engine. Photon makes each byte cheaper to process, but it does not reduce how many bytes you read.
- Reading the plan from the wrong end. Plans are printed with the final result at the top and the scans at the bottom. Data flows from bottom to top.
A practical checklist
- Confirm Photon is enabled on the cluster or that you are using a SQL warehouse.
- Run
EXPLAIN FORMATTEDand list the operators without thePhotonprefix. - Open the Query Profile and find which of those operators take the most time.
- Replace UDFs and unsupported expressions on the expensive path with built-in functions.
- Re-run and compare the plan and the timings.
Exercise
A query plan shows PhotonScan at the bottom, a BatchEvalPython operator in the middle, and PhotonGroupingAgg at the top. The Query Profile shows the BatchEvalPython step takes 70% of the runtime. What is the most likely cause, and what should you try first?
Show answer
The Python UDF is the cause. It runs outside Photon, and the plan has two engine transitions around it. Replace the UDF with built-in Spark SQL or DataFrame functions that do the same work, then re-run and check that the middle operator now has a Photon prefix and that the runtime dropped.
Exercise
You run EXPLAIN FORMATTED on a SQL warehouse and see no Photon operators. Why is "I forgot to turn on Photon" an unlikely explanation, and what should you look for instead?
Show answer
SQL warehouses use Photon by default, so the setting is not the usual cause. Look for operators or expressions that Photon does not support, such as a UDF or an unsupported data type or file format, and check whether the query is so small that the plan is not informative.