Sub topic 2
Why columnar, vectorized execution is faster
This chapter assumes you know basic SQL, how Spark splits a query into stages and tasks, and that Delta tables are stored as Parquet files. It explains the execution ideas that Photon builds on, so the next chapter can focus on Photon itself.
Row-at-a-time execution
Classic Spark execution on the JVM (the Java Virtual Machine) processes data one row at a time. For each row, the generated code reads the fields it needs, applies the expression, and passes the result to the next operator. Spark's whole-stage code generation fuses operators into a single loop, which removes much of the call overhead. The loop still handles one row per iteration, and it still creates JVM objects and depends on garbage collection.
Columnar layout
Parquet already stores data by column. All values of amount are stored together, separate from region. A columnar execution engine keeps that shape in memory instead of rebuilding rows.
This helps in three ways:
- A query that touches 3 of 50 columns reads and holds only those 3.
- Values of one type sit next to each other, so they compress and encode well (for example dictionary and run-length encoding).
- Memory access is sequential, which suits CPU caches.
Vectorized execution
Vectorized execution processes a batch of values, often a few thousand, in one operator call. The per-call overhead is paid once per batch instead of once per row. Tight loops over arrays of one type also let the CPU use SIMD instructions (single instruction, multiple data), which apply one operation to several values at once.
A small comparison
The two functions below compute the same total. The first mimics a row loop. The second works on whole columns.
import numpy as np
amounts = np.random.rand(1_000_000)
regions = np.random.randint(0, 5, size=1_000_000)
def row_at_a_time():
total = 0.0
for i in range(len(amounts)):
if regions[i] == 3:
total += amounts[i]
return total
def vectorized():
return amounts[regions == 3].sum()
assert abs(row_at_a_time() - vectorized()) < 1e-6If you time both, the vectorized version is far faster. NumPy is not Spark, but the principle is the same: do the work per batch in native, type-specific loops.
Where this fits in Databricks
The standard Spark engine mixes these ideas with JVM row processing. Photon is a native engine written in C++ that is vectorized and columnar throughout. Native code avoids JVM object overhead and garbage collection pauses on the hot path.
Gains are largest on scan-heavy work, such as filters, aggregations and joins over many rows. They are smaller when the time goes to something batches cannot fix, such as network shuffle, a few huge files, or user code that runs row by row.
Exercise
A query reads 3 columns of a 50-column table and sums one of them. Give two reasons a columnar, vectorized engine handles this better than a row-at-a-time engine.
Show answer
Only the 3 needed columns are read and kept in memory, so less data moves. Values of one type are processed in batches with tight loops (and SIMD), so the per-row call and object overhead is paid once per batch.