Schema enforcement and evolution
Tables change shape over time: a new column arrives, a type needs widening. A table format refuses writes that do not match the table, and lets you evolve it on purpose. This lesson shows what gets rejected, how to add columns safely, and which changes need care.
Step 3 · Data lake & lakehouse · Silver · Delta day to day, lesson 3 of 4
On this page
Show code in
Every code block on the page follows this.
Read first: Open table formats (skip if you know it)
In plain words first
A form with fixed boxes refuses an answer written in the wrong box, which protects everyone who reads it later. When you need a new question, you print a new version of the form with an extra box, and old forms simply have that box empty.
That is enforcement (refuse what does not fit) and evolution (change the form on purpose).
Enforcement
A Delta or Iceberg write is checked against the table schema before commitcommit: One atomic change to a table: a new version that lists which files were added and removed. Learn more →. A plain ParquetParquet: A columnar file format: values of each column are stored together, so queries read only the columns they need. Learn more → folder accepts anything; a table formattable format: A metadata layer (Delta Lake, Iceberg, Hudi) that turns a folder of data files into a table with commits, versions and a schema. Learn more → refuses:
| Write | Result |
|---|---|
| A column the table does not have | Rejected (unless schema merging is enabled) |
| A column with an incompatible type, e.g. string into int | Rejected |
| A missing nullable column | Accepted; the column is null for these rows |
| Column names differing only in case | Rejected in Delta (case-insensitive names must be unique) |
This is a feature: a producer that suddenly sends amount as text fails loudly at write time instead of corrupting months of data.
Adding columns
# Let this write add new columns (Delta) ( df.write.format("delta") .mode("append") .option("mergeSchema", "true") .saveAsTable("events") )
ALTER TABLE events ADD COLUMNS (device STRING, app_version STRING);
For MERGE, Delta supports automatic schema evolution with spark.databricks.delta.schema.autoMerge.enabled, and newer versions accept MERGE WITH SCHEMA EVOLUTION. Prefer explicit ALTER TABLE in production: schema changes become reviewed decisions instead of side effects of a write.
Which changes are safe
| Change | Delta Lake | Apache Iceberg |
|---|---|---|
| Add column | Yes | Yes |
| Widen type (int → long, float → double) | With type widening (preview in Delta 3.2, GA in 4.0) | Yes (int → long, float → double, decimal precision increase) |
| Rename column | Yes, with column mapping enabled | Yes |
| Drop column | Yes, with column mapping enabled | Yes |
| Reorder columns | Yes | Yes |
| Narrow or change type (long → int, string → int) | Rewrite the table (overwriteSchema) | Not allowed; rewrite |
Why renames are hard, and how IDs fix them
Parquet files identify columns by name. If a table renames email to contact_email by metadata only, old files still say email, and a name-based reader would see the new column as null. Iceberg solved this from the start by giving every column a permanent field ID stored in the Parquet files; readers match by ID, so names can change freely. Delta added the same idea as column mapping:
spark.sql(""" ALTER TABLE events SET TBLPROPERTIES ( 'delta.columnMapping.mode' = 'name', 'delta.minReaderVersion' = '2', 'delta.minWriterVersion' = '5') """) spark.sql("ALTER TABLE events RENAME COLUMN email TO contact_email") spark.sql("ALTER TABLE events DROP COLUMN legacy_flag")
ALTER TABLE events SET TBLPROPERTIES ( 'delta.columnMapping.mode' = 'name', 'delta.minReaderVersion' = '2', 'delta.minWriterVersion' = '5'); ALTER TABLE events RENAME COLUMN email TO contact_email; ALTER TABLE events DROP COLUMN legacy_flag;
Replacing the schema entirely
For incompatible changes, rewrite: df.write.mode("overwrite").option("overwriteSchema", "true").saveAsTable("events") in Delta, or CREATE OR REPLACE TABLE ... AS SELECT. This rewrites all data and keeps history, so time traveltime travel: Reading a table as it was at an earlier version or time. Learn more → to the old schema still works within retention.
Try it yourself
Each example says what it shows, then the output from a real run and what to notice in it, so you can follow without running anything. To try one yourself, paste it into a local Spark session with Delta Lake, with spark already created: each builds its own data.
Goal: See Delta reject an unexpected column, accept it when you ask, and rename a column without rewriting data.
import os path = os.path.abspath("lab/schema/orders") (spark.createDataFrame([(1, 10.0)], ["id", "amount"]) .write.format("delta").mode("overwrite").save(path)) with_currency = spark.createDataFrame([(2, 20.0, "INR")], ["id", "amount", "currency"]) try: with_currency.write.format("delta").mode("append").save(path) except Exception as e: print("rejected:", str(e).split("]")[0] + "]") # Accepted deliberately, for this write only: (with_currency.write.format("delta").mode("append") .option("mergeSchema", "true").save(path)) # old rows: null currency spark.read.format("delta").load(path).orderBy("id").show() spark.sql(f"""ALTER TABLE delta.`{path}` SET TBLPROPERTIES ( 'delta.columnMapping.mode' = 'name', 'delta.minReaderVersion' = '2', 'delta.minWriterVersion' = '5')""") # metadata only spark.sql(f"ALTER TABLE delta.`{path}` RENAME COLUMN amount TO total") print(spark.read.format("delta").load(path).columns)
Output from a real run
rejected: [DELTA_METADATA_MISMATCH] +---+------+--------+ | id|amount|currency| +---+------+--------+ | 1| 10.0| NULL| | 2| 20.0| INR| +---+------+--------+ ['id', 'total', 'currency']
What to notice:
- The first append was rejected (
DELTA_METADATA_MISMATCH): the table had nocurrencycolumn. - With
mergeSchemathe column was added; the old row hasNULLcurrency. - After turning on column mapping,
amountwas renamed tototal. Only the metadata changed; no file was rewritten.
Common mistakes
- Turning on mergeSchema everywhere — Any upstream typo becomes a permanent new column.
- Renaming in Delta without column mapping — Not supported without it; enabling it changes the protocol.
- Assuming all engines read evolved tables — Protocol upgrades can lock out older readers.
Key takeaways
- Table formats reject writes that do not match the schema.
- Add columns with ALTER TABLE or mergeSchema; prefer explicit changes.
- Iceberg tracks columns by ID; Delta needs column mapping for renames and drops.
- Protocol upgrades for evolution features can break older readers.
Interview prep
THE QUESTION
"An upstream team added a column and our Delta append job started failing. What happened and what are the options?"
Avoid: "turn on mergeSchema everywhere". That lets any upstream typo create a new column silently.
Follow-ups · answer out loud, then open
Follow-up"Which schema changes are safe, and which break readers?"
Scenario"A source changed a column from int to string. What happens on append, and what do you do?"
Trap"Iceberg and Delta handle a column rename the same way as plain Parquet."
Check yourself
3 questionsA Delta append contains a column the table lacks, without mergeSchema. What happens?
Show the answer
The write fails. Schema enforcement rejects unknown columns.
How does Iceberg allow renaming a column without rewriting files?
Show the answer
Columns are tracked by permanent field IDs, not names. Readers match columns by ID, so names in metadata can change.
What does Delta need to drop a column without rewriting data?
Show the answer
Column mapping. Column mapping decouples logical names from physical Parquet column names.
Practise this
Problems that use this, graded on hidden tests
Keep going
Up next · Delta day to day · lesson 4 of 4 · 6 min readConstraints and generated columns
NOT NULL and CHECK constraints, generated columns and identity columns in Delta tables.
Related lessons
unionByNameData lake & lakehouse · 5 min read
Delta Lake 4.0Data lake & lakehouse · 6 min read
Iceberg metadata tree
Previous: Time travel and RESTORE
Primary sources: Delta: schema validation and evolution · Delta: column mapping · Iceberg: schema evolution