Sub topic 2
Delta Lake Tables: The Foundation Under Every Layer
This chapter assumes you know basic SQL, basic PySpark DataFrames, and what a data lake is. You should also know that Parquet and JSON are file formats, and you should have used a Databricks notebook. It explains the storage layer that bronze, silver, and gold tables all share. Later chapters rely on the properties described here.
Why the layers need a table format
A plain data lake stores files. Files have no transactions, no schema enforcement, and no history. If a job fails halfway through writing 200 Parquet files, readers see a partial result. If two jobs write at once, one can overwrite the other. Medallion pipelines rerun, retry, and update data constantly, so they need more than files.
Delta Lake is an open table format that adds these guarantees on top of Parquet files in cloud storage. On Databricks, a table created without a format clause is a Delta table by default. Every layer in this tutorial (bronze, silver, and gold) is a Delta table.
How a Delta table is stored
A Delta table is a directory that holds two things:
- Data files, which are ordinary Parquet files.
- A transaction log in a
_delta_logsubdirectory. It is an ordered record of every change made to the table.
Each commit to the table writes a new JSON file to the log, such as 00000000000000000003.json. A commit records which data files were added and which were removed. Periodically, Delta writes a checkpoint file that summarizes the log so readers do not replay every commit.
To read a table, an engine reads the log, works out which data files make up the current version, and reads only those files. Files that were removed by a later commit stay in storage until they are cleaned up, which is what makes history possible.
ACID transactions
ACID stands for atomicity, consistency, isolation, and durability. Delta Lake provides these for operations on a single table.
- Atomicity. A commit is a single log file. Either it exists and all its changes are visible, or it does not and none are. A failed job leaves no half-written table.
- Consistency. A write that violates a constraint or the table schema is rejected, so the table stays valid.
- Isolation. Readers see a consistent snapshot of one version. A reader that starts during a write sees the version from before the write.
- Durability. Once committed, the log entry lives in cloud object storage.
For medallion pipelines, atomicity matters most. A silver MERGE that fails in the middle leaves silver unchanged, so you can fix the problem and rerun it.
Two writers can still conflict. Delta uses optimistic concurrency control: each writer assumes it is alone, then checks at commit time whether another commit touched the same files. If so, one writer gets a conflict exception such as ConcurrentAppendException and must retry. Appends to different partitions or to a table that no one is rewriting usually succeed. Two MERGE statements that update the same rows usually do not.
Schema enforcement and evolution
Delta stores the schema in the log and checks every write against it. If you append a DataFrame whose column types do not match, the write fails. This is schema enforcement, and it protects silver and gold from malformed input.
Bronze often needs the opposite: it must accept new columns when the source changes. Delta supports schema evolution when you ask for it explicitly. In PySpark you set mergeSchema on the write:
from pyspark.sql import Row
df = spark.createDataFrame([
Row(order_id=1, amount=10.5, coupon="SPRING"),
])
(
df.write.format("delta")
.mode("append")
.option("mergeSchema", "true")
.saveAsTable("main.bronze.orders_demo")
)If main.bronze.orders_demo does not yet have a coupon column, this adds it. Existing rows show NULL for the new column. Without mergeSchema, the same write would fail.
A common rule: allow evolution at bronze, and make changes to silver and gold deliberate. A new source column should not silently reach a gold table that dashboards read.
Time travel
Because old data files stay until cleaned up, you can query earlier versions of a table. Each commit creates a version number, and each has a timestamp.
-- Show the commit history
DESCRIBE HISTORY main.silver.orders;
-- Query an earlier version
SELECT * FROM main.silver.orders VERSION AS OF 12;
-- Query the table as it was at a time
SELECT * FROM main.silver.orders TIMESTAMP AS OF '2026-12-01 00:00:00';Time travel supports three jobs in a medallion setup:
- Auditing:
DESCRIBE HISTORYshows which operation changed a table, when, and with what metrics. - Debugging: you can compare a table before and after a bad run.
- Recovery:
RESTORE TABLE main.silver.orders TO VERSION AS OF 12rolls the table back.
Time travel is not a long-term backup. History is kept only as long as the log and data files are retained, which is controlled by table properties (delta.logRetentionDuration, default 30 days, and delta.deletedFileRetentionDuration, default 7 days). VACUUM deletes unreferenced files older than the retention threshold, and after that the old versions cannot be read. Keep bronze as your replayable source of truth, as the next chapters explain, instead of relying on history alone.
Common write operations
Delta supports the operations the layers use most. This table maps each to where it appears in the pipeline.
| Operation | What it does | Typical layer |
|---|---|---|
| `INSERT INTO` / append | Adds new rows | Bronze |
| `MERGE INTO` | Updates, inserts, or deletes rows by a match condition | Silver |
| `INSERT OVERWRITE` / `CREATE OR REPLACE TABLE` | Replaces table contents atomically | Gold |
| `UPDATE` / `DELETE` | Changes or removes rows matching a predicate | Corrections, compliance deletes |
CREATE OR REPLACE TABLE is safe for gold rebuilds because the swap is a single commit. A reader sees either the old version or the new one, never an empty table in between.
Row-level UPDATE, DELETE, and MERGE rewrite the data files that contain affected rows. Parquet files are immutable, so Delta writes new files and marks the old ones removed. Changing one row in a very large file rewrites that whole file. Deletion vectors, an optional feature, record deleted rows in a side file and defer the rewrite, which makes frequent small updates cheaper.
The change data feed
By default, a downstream job cannot ask which rows changed in a Delta table. You can enable the change data feed (CDF) to record row-level changes:
ALTER TABLE main.silver.orders
SET TBLPROPERTIES (delta.enableChangeDataFeed = true);After that, you can read inserts, updates, and deletes between versions with table_changes('main.silver.orders', 13) or by setting readChangeFeed on a read. Changes made before you enabled the feed are not recorded. CDF is optional and adds some storage overhead. Use it when a downstream layer needs only the changed rows, not a full re-scan.
Delta tables as streaming sources and sinks
A Delta table can be the target of a stream and also the source of the next one. This is what lets silver read incrementally from bronze: each append to bronze is a new commit, and a streaming query tracks which commits it has already read. The incremental processing chapter covers the mechanics. For now, note that a streaming read of a Delta table expects the source to be append-only. If you run UPDATE, DELETE, or MERGE on the source table, the stream fails by default, because it cannot treat rewritten files as new data. Bronze is append-only by design, so it fits this pattern well.
Managed and external tables
In Unity Catalog, you choose where table data lives.
- A managed table stores its files in a location Unity Catalog controls. Dropping the table deletes the data (after a retention period for recovery). This is the default and the recommended choice for most medallion tables.
- An external table points to files at a path you manage. Dropping it removes only the metadata.
You can create a managed Delta table with plain SQL:
CREATE TABLE IF NOT EXISTS main.silver.orders (
order_id BIGINT NOT NULL,
customer_id BIGINT,
order_ts TIMESTAMP,
amount DECIMAL(12,2),
status STRING
);The NOT NULL constraint is enforced on every write. You can also add CHECK constraints, for example ALTER TABLE main.silver.orders ADD CONSTRAINT positive_amount CHECK (amount >= 0). A write containing a row that violates the constraint fails the whole commit. That is stricter than the warn and drop behaviors covered in the data quality chapter, so use constraints for rules that must never be broken.
Common errors
DELTA_FAILED_TO_MERGE_FIELDSor schema mismatch on append. The incoming DataFrame has different types than the table. Fix the types, or setmergeSchemaif the change is intended.ConcurrentAppendExceptionorConcurrentDeleteReadException. Two jobs touched the same files. Retry, and avoid running overlapping jobs against the same table.- Time travel fails with a missing-file error.
VACUUMremoved the files for that version. Query a newer version or restore from bronze. - A streaming query from a table fails after an update. The source was modified in place. Keep streaming sources append-only, or use the change data feed.
- Running
VACUUMwith a very short retention. This can delete files that a running query or a stream still needs. Keep the default retention unless you have a specific reason.
Exercise
A silver job fails halfway through a MERGE into main.silver.orders. What state is the table in, and what could you do afterwards to check that it is unchanged?
Show answer
The table is unchanged. A Delta commit is atomic, so a failed MERGE never writes its log entry and readers never see partial results. To confirm, run DESCRIBE HISTORY main.silver.orders and check that no new version appears for the failed run. You can then fix the cause and rerun the MERGE.
Exercise
Why is allowing mergeSchema at bronze and requiring deliberate changes at silver and gold a reasonable policy?
Show answer
Bronze should accept whatever the source sends so that no data is lost. Silver and gold are consumed by other jobs and dashboards, so an unplanned new column could break them or change results. Deliberate schema changes at those layers let you update downstream logic at the same time.