Where Raw Parquet Runs Out

Concepts covered: Distributed Primitives

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.

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

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.

orders = spark.read.parquet("/lake/raw/orders")

(orders
    .dropDuplicates(["order_id"])
    .write
    .mode("overwrite")
    .parquet("/lake/orders"))

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

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 of it.

GuaranteeWhat a Parquet directory doesWhere it bites
Atomic commitFiles appear one by one as the job commit moves them into placeA crash mid-commit leaves part of a write visible
IsolationA reader lists the directory once and reads what is thereA query listing between a hand-run compaction's write and its delete counts rows twice
Conflict checkNothing notices a write that landed in betweenTwo overwrites of 1 partition can leave both jobs' files in it
Row-level changeFiles are immutable, so any edit means rewriting whole files yourselfDeleting 1 customer means finding and rewriting every file holding them
One schemaEach file carries its own schema in its footerA job writes amount as a string, and reads of those files fail
HistoryOverwrites and deletes destroy the previous filesLast Tuesday's report cannot be rerun on last Tuesday's data

A seventh gap: planning time

The last gap costs time. To plan a query on a partitioned path, Spark lists its partition directories, all of them when it reads the path directly, and on an object store each listing call is a network round trip. With 5 years of daily partitions, that is more than 1,800 directories to list before any task starts. Footer stats help only once a task has opened a file, so a filter on a non-partition column still opens the footer of every file.

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?

About this lesson

This section is part of the Storage Formats: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.