Schema Evolution

Concepts covered: Shuffle Optimization

In a plain directory each Parquet file carries its own schema and nothing reconciles them. A table format keeps 1 schema in the log's metaData action and gives it 2 jobs. It enforces: a write whose columns or types disagree with the logged schema is rejected before it commits, so a job that starts writing amount as a string fails loudly and the table stays clean. It also evolves: a schema change is itself a commit, and the format's rules decide which changes cost only that commit and which force a rewrite.

Adding a column costs 1 commit

Add discount to orders and the format writes a new schema to the log and rewrites no files. Old files have no discount column, so readers fill null for their rows, while new files carry real values. A table of 50,000 files gains the column in the time it takes to write 1 commit.

Widening reads old bytes as a bigger type

A table whose order_id is running out of INT can widen the column to BIGINT. Old files keep their 4-byte integers and the reader upcasts each value as it decodes it, which is safe because every old value fits the new type. Formats that support widening allow changes such as INT to BIGINT, FLOAT to DOUBLE, and DECIMAL to a larger precision at the same scale. Narrowing, or a change across type families such as STRING to INT, can fail on a value already stored, so it means rewriting every file.

Rename and drop: names versus IDs

Parquet readers match columns by name by default, and that makes renames dangerous. Rename amt to amount in the logged schema, and a by-name reader opening an old file looks for amount, finds no such column, and returns null for every old row. The query succeeds, and nothing reports that the column's history is gone.

The fix gives each column a permanent ID. Iceberg assigns every column a field ID, writes the same ID into each Parquet file's schema, and matches on the ID when reading. Delta does the equivalent once column mapping is on (delta.columnMapping.mode = 'name'), keeping a fixed physical name under each logical one. With either, a rename changes only the log.

IDs make drops safe too. Drop amt, later add a different column that happens to be called amt, and a by-name reader resurrects the old values from old files under the new column. Matched by ID, the new column gets a fresh ID such as 7, old files only hold ID 2, and the new column reads null for them.

A table matches Parquet columns by name. An engineer renames amt to amount with a metadata-only change, then sums amount over the whole table. What comes back?

Schema changeFiles rewrittenOld rows read as
Add a columnNonenull
Widen INT to BIGINTNonethe old value, upcast
Rename, matched by IDNonethe old value
Rename, matched by nameNonenull, with no error
Drop a columnNonehidden from readers
Narrow, or change type familyEvery filethe rewritten value

A table reads its files the way unionByName combines 2 DataFrames: columns pair up by name in any order, and an INT meeting a BIGINT comes out as BIGINT. In the cell, the older batch stands in for files written before platform existed, so it carries platform as an explicit null, which is what a reader fills for old files. The newer batch lists its columns in a different order. Run the cell and check platform on the rows marked before.

The call makes 2 choices that decide whether the rows line up. unionByName matches columns by name, which is why the newer batch's reordered columns still land correctly. Plain union matches by position and would put platform values under notif_id. Passing allowMissingColumns=True makes unionByName fill null for a column one side lacks, the step the cell wrote out by hand. Without the flag, the call fails as soon as one side is missing a column.

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.