Sub topic 2
Delta Lake tables: the foundation under every layer
This chapter assumes you know basic SQL, can use PySpark DataFrames, and have run a notebook in a Databricks workspace. It covers the storage format that every medallion layer relies on, so the later chapters on bronze, silver, and gold can focus on what each layer does.
What a Delta table is
A Delta table is a folder of Parquet data files plus a transaction log. The log is a series of JSON files in a _delta_log directory. Each file records one commit: which data files were added and which were removed. A reader builds the current table by replaying the log, not by listing the folder.
This design gives you properties that plain Parquet files on cloud storage lack:
- Atomic commits. A write either appears in full or not at all. A failed job leaves no half-written data.
- Snapshot isolation. A query sees one consistent version of the table, even while another job writes to it.
- Schema enforcement. A write whose columns or types do not match the table is rejected.
- Row-level changes.
UPDATE,DELETE, andMERGEwork on a table in object storage. - Time travel. Earlier versions stay readable until their files are cleaned up.
On Databricks, Delta is the default table format. A CREATE TABLE statement without a USING clause creates a Delta table.
CREATE TABLE IF NOT EXISTS main.bronze.orders_demo (
order_id STRING,
amount STRING,
_ingested_at TIMESTAMP
);
INSERT INTO main.bronze.orders_demo
VALUES ('A-1', '19.90', current_timestamp());
DESCRIBE HISTORY main.bronze.orders_demo;DESCRIBE HISTORY shows one row per commit, with the version number, timestamp, and operation. Each commit gets the next version number.
Why this matters for the layers
Each medallion layer leans on a different Delta feature.
| Layer | Delta feature it leans on | Why |
|---|---|---|
| Bronze | Append-only writes, history | Raw data stays intact and can be replayed |
| Silver | `MERGE`, schema enforcement | Upserts remove duplicates and bad types are caught |
| Gold | Snapshot isolation | Dashboards read a stable version while a refresh runs |
Time travel in practice
Because old files remain until cleanup, you can query an earlier version. This helps when you need to check what a layer looked like before a bad load.
SELECT COUNT(*) FROM main.bronze.orders_demo VERSION AS OF 0;
RESTORE TABLE main.bronze.orders_demo TO VERSION AS OF 0;RESTORE creates a new commit that returns the table to the older state. It does not delete history.
Common errors
- Schema mismatch on write. Appending a DataFrame with an extra column or a different type fails. This is the intended behavior. Fix the data, or evolve the schema on purpose, for example with the
mergeSchemawrite option. Do not turn on automatic evolution everywhere, because it hides upstream changes. - Concurrent write conflicts. Two jobs that change the same files at the same time can cause one to fail with a conflict error. Appends from different jobs usually succeed together. Overlapping
UPDATEorMERGEoperations may not. Run them one after another, or narrow each one with a preciseONcondition. - Treating the folder as the table. Reading the Parquet files directly ignores the log. You will see removed files and duplicate rows. Always read through the table name or
format("delta").
Exercise
A job appends a DataFrame to a silver Delta table. The DataFrame has a new column, coupon_code, that the table does not have. What happens by default, and what is one safe way to accept the column?
Show answer
The write fails because Delta enforces the table schema. To accept the column deliberately, add it with ALTER TABLE ... ADD COLUMN, or write with the mergeSchema option set to true for that one job, after confirming the change is expected.
Exercise
You ran VACUUM with the default retention, 10 days later you find a bad load from last week. Can you use RESTORE to go back to the version before it? Why or why not?
Show answer
Probably not. Files removed by the bad load's commit were older than 7 days and VACUUM deleted them, so that version's data files are gone. This is why raw source files in the landing volume should be kept.