Sub topic 1
How a Delta table is stored
A Delta table is a folder of Parquet data files plus a transaction log. Later chapters on OPTIMIZE, Z-ordering, and liquid clustering all work by changing which files exist and which rows sit in them. You need this storage model first.
Prerequisites
This chapter assumes you have:
- Basic SQL (CREATE TABLE, SELECT, WHERE).
- Access to a Databricks notebook or SQL warehouse.
- A basic idea of Parquet files and columnar storage.
- Awareness of what a Spark job and a partition are.
The two parts of a table
A Delta table has two parts in cloud storage:
- Data files. These are immutable Parquet files that hold the rows.
- The transaction log. This is the
_delta_logfolder. It records which data files make up each version of the table.
A table folder looks like this:
events/
_delta_log/
00000000000000000000.json
00000000000000000001.json
00000000000000000010.checkpoint.parquet
part-00000-a1b2.snappy.parquet
part-00001-c3d4.snappy.parquetThe data files alone are not the table. A Parquet file in the folder counts only if the log says it belongs to the current version.
What the log records
Each write creates one numbered JSON file in _delta_log. This file is a commit. It holds a list of actions, including:
add: a data file joined the table. It also carries the file's size and statistics.remove: a data file left the table.metaData: the schema and the table properties.commitInfo: which operation ran, and when.
To find the current table, a reader replays the commits in order. Files that were added and not removed make up the table. Every 10 commits, Delta writes a checkpoint, which is a Parquet summary of the state so far. Readers start from the latest checkpoint, so they do not replay the whole history.
flowchart TDThe diagram below shows how a reader resolves a query.
Updates are rewrites
Parquet files are never edited in place. An UPDATE, DELETE, or MERGE that touches rows in a file works in two steps:
- It writes a new file containing the changed rows and the unchanged rows.
- It commits a
removefor the old file and anaddfor the new one.
The old file stays in storage, but no current version references it. This is why time travel works. It is also why deleted files take up space until you run VACUUM, which a later chapter covers.
Every INSERT or append adds new files. Many small writes therefore produce many small files. That is the small-files problem, and the next chapter covers it.
Statistics in the log
Each add action stores per-file statistics: the number of records, and the minimum value, maximum value, and null count for columns. By default these cover the first 32 columns of the table. Delta compares these ranges with your WHERE clause and skips files that cannot match. This is data skipping, and it depends on how rows are spread across files.
Looking at a table
Two commands show the log without opening any files by hand:
DESCRIBE DETAIL events;This returns the table's location, numFiles, sizeInBytes, and partition columns.
DESCRIBE HISTORY events;This returns one row per commit, with the version, timestamp, operation, and operation metrics such as the number of files added and removed.
Summary of the model
- Data files are immutable Parquet.
- The log is the source of truth for which files are current.
- Writes add and remove files. They never edit them.
- Per-file statistics in the log enable data skipping.
Exercise
You run DELETE FROM events WHERE user_id = 7. The rows are in one data file. What does Delta write to the log, and what happens to the old file in storage?
Show answer
Delta writes a new data file with the remaining rows from that file. It commits a remove action for the old file and an add action for the new one. The old file stays in storage, because older versions and time travel can still use it, until VACUUM deletes it after the retention period.