Sub topic 2
Every commit is a table version
This chapter assumes you can write basic SQL (SELECT, WHERE, ALTER TABLE), use a Databricks notebook or the SQL editor, and know what a Delta table is. The PySpark example needs basic DataFrame skills. The previous chapter covered the transaction log. Here we look at how that log turns each change into a numbered version.
What counts as a version
A version is the state of a Delta table after one successful commit. A commit is one atomic entry in the _delta_log directory. Each write operation that changes the table produces exactly one commit, and each commit gets the next version number.
- Version numbers start at 0 when the table is created.
- Each commit increases the number by 1. Numbers are never reused or skipped.
- A version is a complete, consistent snapshot. Readers never see a half-finished write.
The commit entries are JSON files named with a zero-padded version number, such as 00000000000000000005.json. Each file lists the actions that changed the table: files added, files removed, metadata changes, and information about the operation.
What creates a commit
Any operation that changes the table's data or metadata commits a new version. Examples:
| Operation | Typical effect on the table |
|---|---|
| `CREATE TABLE`, `CREATE OR REPLACE TABLE` | Version 0, or a new version if the table exists |
| `INSERT`, `COPY INTO`, streaming micro-batch append | Adds data files |
| `UPDATE`, `DELETE`, `MERGE` | Removes old data files and adds rewritten ones |
| `OPTIMIZE` | Compacts small files into larger ones |
| `ALTER TABLE ... SET TBLPROPERTIES` | Changes metadata only |
| `RESTORE` | Records a rollback as a new commit |
Two points are easy to miss. First, OPTIMIZE and property changes create versions even though the rows you query do not change. A table can have many versions that return identical results. Second, a statement that changes no rows may still create a commit, depending on the operation. Do not assume that version count equals count of data changes.
Files are immutable, so old versions survive
Delta never edits a Parquet data file in place. An UPDATE or DELETE writes new files with the changed rows, then records in the log that the old files are removed and the new files are added. The old files stay in storage. The log decides which files belong to which version.
This is the mechanism behind time travel. Reading version 0 means using the files the log lists as active at version 0. Reading the latest version means replaying all commits and using the files that remain active. Time travel works only while the old files still exist in storage, which is why retention and VACUUM matter later in this tutorial.
Seeing versions in practice
The following walkthrough creates a small table and produces three versions. Run it in a notebook with Unity Catalog, replacing the catalog and schema if needed.
CREATE OR REPLACE TABLE main.sales.orders_demo (
order_id INT,
status STRING
);
INSERT INTO main.sales.orders_demo VALUES (1, 'new'), (2, 'new');
UPDATE main.sales.orders_demo SET status = 'shipped' WHERE order_id = 1;These three statements give the following versions:
- Version 0:
CREATE OR REPLACE TABLE. The table exists and is empty. - Version 1:
INSERT. Two rows are added. - Version 2:
UPDATE. The file holding order 1 is replaced by a rewritten file.
If the table already existed, CREATE OR REPLACE adds a new version to the existing history instead of restarting at 0. In that case, your numbers will be higher. Check the real numbers with DESCRIBE HISTORY, covered in the next chapter.
You can also find the current version from PySpark with the Delta API:
from delta.tables import DeltaTable
history = DeltaTable.forName(spark, "main.sales.orders_demo").history(1)
latest_version = history.select("version").first()["version"]
print(latest_version)history(1) returns only the most recent commit. The version value is the number you pass to VERSION AS OF later.
Concurrent writers and version order
Delta uses optimistic concurrency control. Two writers can prepare changes at the same time, but only one can create commit file number N. The other detects the conflict. If its changes do not conflict with the winner's, it retries as commit N+1. If they do conflict, it fails with a concurrent modification error.
The result is a single, linear history. Versions have one order, so "version 5" means the same table state to every reader.
Common mistakes
- Treating a version as a timestamp. A version is a sequence number. Use
TIMESTAMP AS OFwhen you know when something happened andVERSION AS OFwhen you know which commit. - Expecting every version to differ in data. Maintenance operations and metadata changes create versions with identical rows.
- Assuming all versions are readable forever. The log entry can outlive the data files it refers to. Retention rules decide this, as a later chapter explains.
Exercise
A table was created, then received two INSERT statements, one UPDATE, and one OPTIMIZE. What is the latest version number, and does the OPTIMIZE commit change what a SELECT returns?
Show answer
The latest version is 4. CREATE is version 0, the two INSERTs are versions 1 and 2, the UPDATE is version 3, and OPTIMIZE is version 4. OPTIMIZE only rewrites files into a better layout, so a SELECT returns the same rows as at version 3.
Exercise
Why can Delta still read version 1 of a table after a DELETE created version 2?
Show answer
Delta never modifies data files in place. The DELETE commit records that some files are removed and new ones are added, but the old files stay in storage. Version 1 is read by using the files the log lists as active at that version.