Storage Formats: Advanced
A Parquet file is well built: columns stored together, row groups carrying min and max stats, and a footer that lets Spark skip whatever a query does not need. A table makes a bigger promise than a file, and a directory cannot keep it. After df.write.parquet("/lake/orders"), the table later readers see is whatever a listing of that directory returns at the moment they look. Nothing records which files belong together, which write produced them, or whether that write finished.
The overwrite that deletes first
Take a job that rebuilds the whole orders directory with mode("overwrite"). Spark runs a file overwrite in 2 steps: it deletes the files already at the path, then runs the write job. That job stages each task's output under a hidden _temporary directory and moves it into place only after every task has finished. Spark skips paths whose names begin with _ when it reads a directory, so staged files stay invisible.
Now let the job fail near the end, after 1 task has used up its retries. The old files are already gone. The new ones sit under _temporary, which readers skip. Every dashboard on /lake/orders reads an empty table until someone reruns the job, and nothing in the directory says which run was the last good one.
6 gaps between a folder and a table
| Guarantee | What a Parquet directory does | Where it bites |
|---|---|---|
| Atomic commit | Files appear one by one as the job commit moves them into place | A crash mid-commit leaves part of a write visible |
| Isolation | A reader lists the directory once and reads what is there | A query listing between a hand-run compaction's write and its delete counts rows twice |
| Conflict check | Nothing notices a write that landed in between | Two overwrites of 1 partition can leave both jobs' files in it |
| Row-level change | Files are immutable, so any edit means rewriting whole files yourself | Deleting 1 customer means finding and rewriting every file holding them |
| One schema | Each file carries its own schema in its footer | A job writes amount as a string, and reads of those files fail |
| History | Overwrites and deletes destroy the previous files | Last Tuesday's report cannot be rerun on last Tuesday's data |
A seventh gap: planning time
The rebuild above gets further this time: every task finishes, and the job commit has moved 30 of 80 staged files into /lake/orders when the driver crashes. What does a query on the path return?
A table format adds 1 directory beside the data files: a log of numbered commits. In Delta the log lives in _delta_log/, and each commit is a JSON file named for its version: 00000000000000000000.json is version 0, 00000000000000000001.json is version 1. The log defines the table. A data file that no commit adds is invisible, whatever a directory listing says.
What a commit holds
A commit is a list of actions. An add names a data file joining the table, with its size and per-column stats. A remove names a file leaving it. A metaData action carries the schema and partition columns, and commitInfo records the operation. Below is version 2 of the running example, a DELETE that dropped 10 rows from part-001.parquet:
The DELETE left part-001.parquet untouched on disk. It wrote part-003.parquet holding the 340 surviving rows and recorded the swap. Every edit to the table works this way, as new files plus log entries, because a Parquet file is never modified after it is written.
The directory versus the table
| path | in the log |
|---|---|
| part-000.parquet | added v0, removed v3 |
| part-001.parquet | added v0, removed v2 |
| part-002.parquet | added v1, removed v3 |
| part-003.parquet | added v2 |
| part-004.parquet | added v3 |
| part-005.parquet | never |
| path | records |
|---|---|
| part-003.parquet | 340 |
| part-004.parquet | 520 |
Of the 6 files on disk, 2 make up the table. part-005.parquet came from a job that crashed before committing, so no reader ever sees it. This is what makes a write atomic: data files go down first and stay invisible, and creating the next commit file publishes all of them at once.
One writer wins each version
Replaying the log
A reader rebuilds the table's state by keeping, for each file path, its newest action across all commits: a path whose newest action is add is live, and one whose newest action is remove is gone. The Delta protocol calls this action reconciliation. The animation replays the 4 commits of the running example: a groupBy on path finds each file's newest version, and a join brings back the action recorded there.
from pyspark.sql import functions as Flatest = log_actions.groupBy("path").agg(F.max("version").alias("version"))live = (latest.join(F.broadcast(log_actions), ["path", "version"]).filter(F.col("action") == "add"))live.collect()
The groupBy is the only shuffle, since every action for a path has to reach 1 task before its newest version is known. The join wraps log_actions in F.broadcast, a few rows here, so it adds no shuffle. On a table with millions of files that shuffle is real work, which is why the log gets checkpointed.
Every 10 commits by default (delta.checkpointInterval), the writer saves the reconciled state as a Parquet checkpoint, and a _last_checkpoint file points at the newest one. A reader loads that checkpoint and replays only the JSON commits after it: at version 1,234, the checkpoint from version 1,230 plus 4 commits.
The stats in each add pay off at planning. For a filter such as order_id = 1500, Spark compares the value against every live file's min and max from the log and drops files that cannot match, with no directory listing and before any task is scheduled. Delta collects these stats for the first 32 columns by default. In the SQL tab of the Spark UI, the scan node's number of files read shows how many files survived.
delta.dataSkippingNumIndexedCols. A filter on column 40 gets no skipping from the log, only the row-group stats inside each file Spark still opens.The same latest-wins rule rebuilds current state from any change history. listing_attribute_changes records each edit to a listing's attributes with an increasing change_seq. Run the cell: it keeps each listing and attribute pair's highest change_seq, joins back for the value, and shows the result.
from pyspark.sql import functions as F
changes = spark.table("listing_attribute_changes").where(
F.col("listing_ref").isNotNull() & F.col("attr_name").isNotNull()
)
newest = changes.groupBy("listing_ref", "attr_name").agg(
F.max("change_seq").alias("change_seq")
)
current = newest.join(changes, ["listing_ref", "attr_name", "change_seq"])
(current
.select("listing_ref", "attr_name", "attr_value", "op")
.orderBy("listing_ref", "attr_name")
.show())The join is where the latest-wins rule is enforced. F.max("change_seq") finds the newest change for each pair, but the aggregate keeps no attr_value, so a join has to bring the value back. Joining on listing_ref and attr_name alone would match every change ever made to the pair and return its whole history. Adding change_seq to the join columns lets only the newest change's row through. The log replay above does the same thing with path and version.
Suppose the cell above joins newest back to changes on listing_ref and attr_name only, dropping change_seq. What does current hold?
Readers pin a snapshot
Follow it through the running example. A report plans against version 2 and is handed part-000.parquet, part-002.parquet and part-003.parquet. Midway, a compaction commits version 3, removing part-000.parquet and part-002.parquet and adding part-004.parquet. The report keeps reading part-000.parquet without error, because a remove only writes a log entry. The file stays on disk until a cleanup deletes it, and the cleanup waits out a retention window first.
| file | records |
|---|---|
| part-000.parquet | 400 |
| part-002.parquet | 120 |
| part-003.parquet | 340 |
| total | 860 |
| file | records |
|---|---|
| part-003.parquet | 340 |
| part-004.parquet | 520 |
| total | 860 |
Both versions hold 860 rows: version 3 packed 400 and 120 rows into 1 file of 520 without changing a value. The log flags such commits with dataChange: false.
Writers race optimistically
Writers hold no lock while they work. Each records the version it started from, writes its data files, and then tries to create the next commit. If that version is taken, it reads the commits that beat it and runs 2 checks. Did any of them remove or rewrite a file it read? Did any of them add files inside the data it read, such as a partition its filter selected? If both answers are no, it retries the commit at the following version, reusing the data files it already wrote. If either is yes, committing would act on a table it never saw, and it fails with a concurrent modification error. So a delete of order_date = '2024-03-01' fails when another job appends into that partition first, because the new rows match its filter and the delete would skip them. When 2 blind appends hit 1 table, both commit, since neither reads anything.
Two jobs start from version 7 of orders, partitioned by order_date. Job A appends files to order_date=2024-03-15 while Job B deletes rows where order_date is 2024-03-01, and A commits version 8 first. What happens to Job B?
Reading an old version on purpose
timestampAsOf takes a time instead and resolves to the newest version committed at or before it. Use either to rerun last Tuesday's report on last Tuesday's data, to audit what a bad job changed, or to roll back by committing an old version's file list as a new version.
How far back you can go
Time travel needs 2 pieces to survive: the log entries for the version and the data files they name. Delta keeps log entries for 30 days by default (delta.logRetentionDuration), and its VACUUM command deletes data files that were removed from the table more than 7 days ago (delta.deletedFileRetentionDuration). Retention is a storage budget: a table fully rewritten every day keeps about 1 extra copy of itself per retained day, and once a version's files are vacuumed, reading it fails even while its log entries remain.
from pyspark.sql import functions as F
key = ["listing_ref", "attr_name"]
history = spark.table("listing_attribute_changes").dropna(subset=key)
span = history.agg(
F.min("change_seq").alias("lo"), F.max("change_seq").alias("hi")
)
cut = span.select(F.floor((F.col("lo") + F.col("hi")) / 2).alias("cut"))
def replay(changes, label):
newest = changes.groupBy(key).agg(F.max("change_seq").alias("change_seq"))
return newest.join(changes, key + ["change_seq"]).select(
*key, F.col("attr_value").alias(label)
)
upto_mid = history.crossJoin(cut).where(F.col("change_seq") <= F.col("cut"))
then = replay(upto_mid, "value_at_mid")
now = replay(history, "value_now")
now.join(then, key, "left").orderBy(*key).show()In a plain directory each Parquet file carries its own schema and nothing reconciles them. A table format keeps 1 schema in the log's metaData action and gives it 2 jobs. It enforces: a write whose columns or types disagree with the logged schema is rejected before it commits, so a job that starts writing amount as a string fails loudly and the table stays clean. It also evolves: a schema change is itself a commit, and the format's rules decide which changes cost only that commit and which force a rewrite.
Adding a column costs 1 commit
Add discount to orders and the format writes a new schema to the log and rewrites no files. Old files have no discount column, so readers fill null for their rows, while new files carry real values. A table of 50,000 files gains the column in the time it takes to write 1 commit.
Widening reads old bytes as a bigger type
A table whose order_id is running out of INT can widen the column to BIGINT. Old files keep their 4-byte integers and the reader upcasts each value as it decodes it, which is safe because every old value fits the new type. Formats that support widening allow changes such as INT to BIGINT, FLOAT to DOUBLE, and DECIMAL to a larger precision at the same scale. Narrowing, or a change across type families such as STRING to INT, can fail on a value already stored, so it means rewriting every file.
Rename and drop: names versus IDs
Parquet readers match columns by name by default, and that makes renames dangerous. Rename amt to amount in the logged schema, and a by-name reader opening an old file looks for amount, finds no such column, and returns null for every old row. The query succeeds, and nothing reports that the column's history is gone.
The fix gives each column a permanent ID. Iceberg assigns every column a field ID, writes the same ID into each Parquet file's schema, and matches on the ID when reading. Delta does the equivalent once column mapping is on (delta.columnMapping.mode = 'name'), keeping a fixed physical name under each logical one. With either, a rename changes only the log.
| column asked for | column in file | value read |
|---|---|---|
| order_id | order_id | 1201 |
| amount | no match | null |
| column asked for | column in file | value read |
|---|---|---|
| order_id (ID 1) | order_id (ID 1) | 1201 |
| amount (ID 2) | amt (ID 2) | 18.50 |
IDs make drops safe too. Drop amt, later add a different column that happens to be called amt, and a by-name reader resurrects the old values from old files under the new column. Matched by ID, the new column gets a fresh ID such as 7, old files only hold ID 2, and the new column reads null for them.
A table matches Parquet columns by name. An engineer renames amt to amount with a metadata-only change, then sums amount over the whole table. What comes back?
| Schema change | Files rewritten | Old rows read as |
|---|---|---|
| Add a column | None | null |
| Widen INT to BIGINT | None | the old value, upcast |
| Rename, matched by ID | None | the old value |
| Rename, matched by name | None | null, with no error |
| Drop a column | None | hidden from readers |
| Narrow, or change type family | Every file | the rewritten value |
A table reads its files the way unionByName combines 2 DataFrames: columns pair up by name in any order, and an INT meeting a BIGINT comes out as BIGINT. In the cell, the older batch stands in for files written before platform existed, so it carries platform as an explicit null, which is what a reader fills for old files. The newer batch lists its columns in a different order. Run the cell and check platform on the rows marked before.
from pyspark.sql import functions as F
older = (spark.table("push_notifs")
.select(
"notif_id",
"user_id",
"status",
F.lit(None).cast("string").alias("platform"),
F.lit("before").alias("batch"))
.limit(3))
newer = (spark.table("push_notifs_2fa")
.select(
"platform",
"status",
F.col("user_id").cast("bigint").alias("user_id"),
"notif_id",
F.lit("after").alias("batch"))
.limit(3))
older.unionByName(newer).show()The call makes 2 choices that decide whether the rows line up. unionByName matches columns by name, which is why the newer batch's reordered columns still land correctly. Plain union matches by position and would put platform values under notif_id. Passing allowMissingColumns=True makes unionByName fill null for a column one side lacks, the step the cell wrote out by hand. Without the flag, the call fails as soon as one side is missing a column.
3 ways to lay out metadata
| Aspect | Delta | Iceberg | Hudi |
|---|---|---|---|
| Metadata | Ordered JSON commits plus Parquet checkpoints | A tree: metadata file, manifest list, manifests | A timeline of instants in a .hoodie folder |
| A version is | An integer commit number | A snapshot ID | An instant, keyed by commit time |
| A commit is | Creating the next numbered file | Swapping the catalog's pointer to a new metadata file | Completing an instant on the timeline |
| Row updates | File rewrites, or deletion vectors that mark rows removed | File rewrites, or delete files (format v2) | Copy-on-write or merge-on-read, set per table |
| Built for | Spark-centric batch and merge work | Huge tables shared by several engines | Keyed upserts and pulling only changed rows |
Iceberg's tree and why it scales
A query on 2024-03-14 follows the pointer down, skips Manifest A, and plans only Data files B.
The tree also allows partition evolution. Iceberg partitions by transforms of a column, such as days(order_ts), and each manifest records the spec its files were written with. Switch from daily to hourly and new files use the new spec while old files keep theirs; a filter on order_ts prunes both, and nothing is rewritten. Queries filter on order_ts itself and never name a partition column, which Iceberg calls hidden partitioning.
Copy-on-write versus merge-on-read
from pyspark.sql import functions as Ftouched = (target.join(F.broadcast(updates), "order_id").select("file").distinct())touched.collect()
The join broadcasts updates, so the only shuffle is the distinct on file. The 4 changed rows hit 3 of the 4 files, and copy-on-write rewrites all 9 rows in them. Scaled up, 1,000 updated keys scattered over 200 files of 128MB each is 25.6GB rewritten for a few kilobytes of change, on every run.
Choosing and defending it
| If the platform... | Lean toward | Because |
|---|---|---|
| Runs nearly all reads and writes on Spark, with appends and periodic merges | Delta | Simplest log, and checkpoints keep replay short |
| Shares tables across several query engines, or holds millions of files | Iceberg | Engine-neutral spec, manifests that prune at planning |
| Changes its partitioning as data grows | Iceberg | Partition evolution with no rewrite of old files |
| Upserts keyed rows every few minutes and feeds jobs that want only changes | Hudi | Record index, merge-on-read, incremental reads by commit time |
A team runs 3 engines on the same tables: Spark writes them hourly, a SQL engine serves dashboards, and a separate service reads features for model training. The largest table holds 4 million files, rows are appended and rarely updated, and the team plans to move from daily to hourly partitions. Which format fits best?
> Uber built Hudi to bring upserts and incremental processing to its data lake: tables such as trips keep changing after their rows first land, and downstream jobs needed to consume only the changed rows instead of rescanning whole partitions on every run.
A transaction log turns a folder of Parquet files into a table with atomic commits, history and safe schema change.
- Category
- Spark
- Difficulty
- advanced
- Duration
- 15 minutes
- Challenges
- 14 hands-on challenges
Topics covered: Where Raw Parquet Runs Out, The Transaction Log, ACID and Time Travel, Schema Evolution, Delta, Iceberg, or Hudi
Lesson Sections
- Where Raw Parquet Runs Out (concepts: Distributed Primitives)
That arrangement holds while 1 job writes and nobody reads during the write. Production breaks both conditions every day: a nightly load runs while dashboards query, a backfill overlaps the regular load, and a privacy request asks you to delete 1 customer from 3 years of files. The overwrite that deletes first 6 gaps between a folder and a table That failure is one case of a general shortage. Each row below names a guarantee a database table gives and how a directory of Parquet files falls short
- The Transaction Log (concepts: Distributed Primitives)
What a commit holds The directory versus the table One writer wins each version Nothing stops 2 writers from finishing their data files at the same moment and both trying to create version 4. Exactly 1 must succeed, which takes an atomic create-if-absent on the commit file's name. Where the storage cannot do that, the format routes commits through a lock or a catalog service that hands out versions. What the loser does next is the subject of the next section. Replaying the log
- ACID and Time Travel (concepts: Distributed Primitives)
With the log as the table, each property a database promises becomes a rule about commits. Atomicity comes from the single commit file and durability from storing it durably. Isolation and consistency between writers come from how readers pick a version and how writers claim the next one. Readers pin a snapshot A query resolves the table once, at planning: it reads the newest commit, say version 2, and fixes the list of files that version names. Every task in the query reads from that list for a
- Schema Evolution (concepts: Shuffle Optimization)
Adding a column costs 1 commit Widening reads old bytes as a bigger type Rename and drop: names versus IDs
- Delta, Iceberg, or Hudi (concepts: Cost Optimization)
All 3 formats give the previous sections' guarantees: atomic commits over immutable Parquet files, snapshot reads, time travel and schema evolution. They differ in how the metadata is laid out, which decides 3 properties: how planning scales with file count, how row updates get written, and how many engines can share a table. 3 ways to lay out metadata Iceberg's tree and why it scales Iceberg's metadata is a tree, so it has no log to replay. The catalog holds a pointer to the current metadata fi