Sub topic 3
What Photon is and what it accelerates
This chapter assumes you know basic SQL, have used Databricks clusters or SQL warehouses, and understand Spark stages, tasks and shuffles. It also assumes you have read the previous chapter on columnar, vectorized execution.
What Photon is
Photon is a native vectorized query engine in Databricks. It is written in C++ rather than running on the JVM (Java Virtual Machine), which is where standard Spark executes. Photon plugs into the Spark runtime, so you keep the same APIs, SQL syntax and Delta tables. It replaces the code that executes parts of a query's physical plan.
Photon does not replace Spark's planning. Spark still parses the query, optimizes it and splits it into stages and tasks. Photon takes over execution of the operators it supports inside those tasks.
Where the speedup comes from
Photon processes data in columnar batches and uses vectorized code. It handles many values per instruction and avoids per-row JVM overhead such as object creation and garbage collection. It also uses compiled native code for scans, expressions and hash tables.
What Photon accelerates
Photon helps most on CPU-bound work over Delta or Parquet data:
- Scans and filters on columnar files.
- Aggregations such as
SUM,COUNTandGROUP BYover many rows. - Joins, especially hash joins on large tables.
- Writes to Delta tables, including
MERGE,UPDATEandDELETEoperations. - Common expressions on strings, numbers and dates.
The gain is largest when a query touches many rows and spends its time computing, not waiting. A query that aggregates billions of rows benefits a lot. A query that returns ten rows from a tiny table gains little, because it was never slow.
What Photon does not change
Photon does not fix a bad data layout. If a query reads too many files, Photon reads them faster, but it still reads them. Photon also does not speed up work that runs outside the engine, such as Python UDFs. Later chapters cover layout, statistics and UDFs.
| Situation | Photon effect |
|---|---|
| Large aggregation over Delta | Large speedup |
| Join of two large tables | Large speedup |
| Selective query on a tiny table | Little change |
| Python UDF in the query | That step does not run in Photon |
| Query limited by too many small files | Partial gain only |
Checking that Photon ran
Use EXPLAIN FORMATTED to see 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;Look for operator names that start with Photon, such as PhotonScan, PhotonBroadcastHashJoin or PhotonGroupingAgg. Operators without that prefix ran in standard Spark.
Exercise
A query joins two large Delta tables and then calls a Python UDF on one output column. Which parts can run in Photon, and which cannot?
Show answer
The scan, join and aggregation operators can run in Photon if they are supported. The Python UDF cannot, because it runs row by row outside the native engine. Replacing it with built-in functions lets more of the query run in Photon.