Sub topic 3
What Photon is and what it accelerates
Prerequisites
This chapter assumes you know basic SQL (SELECT, JOIN, GROUP BY) and have worked with Databricks clusters or SQL warehouses. You should also know the Spark vocabulary of DataFrames, stages and tasks, and understand that Delta Lake tables are stored as Parquet files plus a transaction log. The previous chapter on columnar and vectorized execution explains why processing batches of column values is faster than processing one row at a time. This chapter builds on that idea.
What Photon is
Photon is a query engine written in C++ that Databricks built to run Spark SQL and DataFrame operations. It runs inside the Databricks Runtime and replaces parts of the standard Spark execution path. It does not replace Spark itself.
Spark still plans the query, schedules stages, assigns tasks to executors and handles shuffles between them. Photon takes over the work inside the tasks. The standard path runs those operators on the JVM, mostly in generated Java code. Photon runs them as native, vectorized code that processes batches of column values.
Because Photon plugs in below the planner, your queries do not change. You write the same SQL or DataFrame code and Spark decides which operators Photon can run.
What Photon accelerates
Photon speeds up CPU-bound work on structured data. The main areas are:
- Scans of Parquet and Delta files. Photon decodes columnar data and applies filters on batches of values.
- Filters and projections. Expressions such as comparisons, arithmetic, string functions and date functions run over whole column batches.
- Aggregations. Hash aggregations behind GROUP BY, including SUM, COUNT, MIN, MAX and AVG, are a common win.
- Joins. Hash joins and broadcast hash joins have native implementations.
- Sorts and window functions. Many sort and window operators run natively.
- Writes to Delta tables. Photon speeds up parts of the write path, such as MERGE, UPDATE and DELETE on Delta, and large inserts.
Databricks reports the largest gains on queries that read and process large volumes of data, join large tables, aggregate many rows, or use many expressions per row.
What Photon does not speed up
Photon changes how fast a task computes. It does not change how much data must move or how well the data is laid out. Expect little or no gain in these cases:
- Queries that finish in a few seconds. Startup and scheduling time dominate, and there is little compute to speed up.
- I/O-bound queries. If a query waits on slow storage or reads far more files than it needs, faster CPU code does not help. Later chapters on Delta file layout and data skipping address this.
- Shuffle-heavy queries with data skew. One slow task still holds up the stage.
- Python UDFs. These run in a Python process outside the native engine.
- Unsupported operators. Spark runs these on the JVM path (see the next chapter).
Photon does not fix a badly laid-out table or a bad join order. It makes a well-planned query cheaper to compute.
Photon compared with the standard Spark path
| Aspect | Standard Spark (JVM) | Photon |
|---|---|---|
| Implementation | Scala and Java, with generated code | C++ |
| Processing unit | Row-oriented in most operators | Batches of column values |
| Planning and scheduling | Spark | Spark (unchanged) |
| Memory management | JVM heap, subject to garbage collection | Native memory outside the JVM heap |
| Python UDFs | Run in a Python worker | Not accelerated |
| Result semantics | Spark SQL semantics | Matches Spark SQL semantics |
The last row matters in practice. Photon is built to return the same results as Spark. Switching it on should not change your query output, and you do not need to rewrite queries.
How to see Photon at work
Photon operators appear in the physical plan with a Photon prefix. Run EXPLAIN FORMATTED on a query and look at the operator names.
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;On Photon-enabled compute, you can expect to see operators such as PhotonScan, PhotonBroadcastHashJoin, PhotonGroupingAgg and PhotonShuffleExchangeSink. Operators without the prefix run on the standard path. The exact names and plan layout vary by runtime version, so treat these as examples and not as a fixed list.
A plan can mix both kinds of operator. Where a Photon operator feeds a non-Photon operator, the data must be converted between formats at that boundary. The more often a plan crosses that boundary, the less benefit you get.
A worked reading of a query
Consider the query above. It has four parts:
- Scan
ordersand filter onorder_date. - Scan
customers. - Join the two on
customer_id. - Aggregate by
region.
On Photon compute, all four parts can run natively. The scan decodes only the needed columns (customer_id, amount, order_date, region). The filter runs on batches of dates. The join builds a hash table, and the aggregate updates sums per region in batches.
If you replaced the filter with a Python UDF, for example WHERE my_udf(o.order_date), that predicate would run row by row in Python. The rest of the query could still use Photon, but you would pay for moving data between the engines, and the UDF would likely dominate the runtime.
Common errors and misconceptions
- "Photon makes every query faster." It speeds up CPU-bound work in supported operators. A query limited by I/O, skew or a poor plan sees little change.
- "Photon replaces Spark." Spark still plans, schedules and shuffles. Photon only runs operators inside tasks.
- "A Photon cluster means a Photon query." Enabling Photon on compute makes it available. Each operator still has to be supported. Check the plan.
- "Photon needs code changes." It does not. If you are rewriting working queries just to enable Photon, stop and check the plan first.
- Judging speedup from one run. Caches, cluster warm-up and data size all affect timing. The benchmarking chapter covers fair comparison.
Key points
- Photon is a native, vectorized C++ engine that runs inside Spark tasks.
- Spark keeps planning, scheduling and shuffling.
- It helps most with scans, filters, aggregations, joins and Delta writes over large data.
- It does not help with Python UDFs, I/O-bound reads, skew or very short queries.
- You verify Photon use by reading operator names in the plan.
Exercise
A query on a Photon-enabled cluster shows PhotonScan and PhotonGroupingAgg in its plan, but one Filter step has no Photon prefix and uses a Python UDF. The query is still slow. Give two reasons the Photon engine does not remove the slowness, and name the first thing you would try.
Show answer
First, the UDF runs row by row in a Python process outside Photon, so that step stays slow, and data must be converted between formats at the boundary with the Photon operators. Second, if the query is slow because it reads too many files or has skewed shuffles, faster native code does not change how much data is read or how unevenly work is split. The first thing to try is replacing the UDF with built-in functions, such as F.upper(F.trim("region")) in place of a Python lambda, so the filter can run in Photon and Spark can optimize it.
Exercise
Your team enables Photon on a cluster and a 3-second query shows no improvement. Is Photon broken? Explain.
Show answer
Not necessarily. For a query that short, startup and scheduling time dominate, so there is little compute for Photon to speed up. Check the plan to confirm the operators are Photon operators, then test with a longer query on larger data before drawing a conclusion.