Skip to content
Silver 6 min read · Delta, Iceberg 2 practice problems ↓

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

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:

WriteResult
A column the table does not haveRejected (unless schema merging is enabled)
A column with an incompatible type, e.g. string into intRejected
A missing nullable columnAccepted; the column is null for these rows
Column names differing only in caseRejected 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

PySparkSpark SQL · Two ways to add a column
# 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

ChangeDelta LakeApache Iceberg
Add columnYesYes
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 columnYes, with column mapping enabledYes
Drop columnYes, with column mapping enabledYes
Reorder columnsYesYes
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:

PySparkSpark SQL
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;
Watch out: enabling column mapping or type widening upgrades the table protocol. Older readers and some other engines may no longer be able to read the table. Check every consumer before changing protocol features.

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.

Production patternPySpark · Enforcement, an explicit mergeSchema, and a rename with column mapping
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 no currency column.
  • With mergeSchema the column was added; the old row has NULL currency.
  • After turning on column mapping, amount was renamed to total. 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?"

Schema enforcement rejected an unknown column. Options: explicitly ALTER TABLE ADD COLUMNS (preferred, reviewed), or enable mergeSchema for that write; or select only known columns to ignore it. Explain why enforcement is good, and mention renames/drops need column mapping (Delta) while Iceberg tracks columns by ID.

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?"
Adding a nullable column is safe. Widening some types (int to long) is safe where supported. Renaming or dropping columns needs column mapping in Delta (or Iceberg, which tracks columns by id). Narrowing types or changing a column's meaning breaks readers.
Scenario"A source changed a column from int to string. What happens on append, and what do you do?"
Schema enforcement rejects the write. Decide whether the new type is legitimate: if so, add a new column or rewrite the table with the new type; if not, cast and validate in the pipeline, quarantine bad rows, and talk to the producer.
Trap"Iceberg and Delta handle a column rename the same way as plain Parquet."
Plain Parquet matches columns by name, so a rename looks like a dropped column plus a new empty one. Iceberg tracks columns by id, and Delta with column mapping does too, so renames are metadata-only.

Check yourself

3 questions

A 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 read
Constraints and generated columns
NOT NULL and CHECK constraints, generated columns and identity columns in Delta tables.

Related lessons

Previous: Time travel and RESTORE

Primary sources: Delta: schema validation and evolution · Delta: column mapping · Iceberg: schema evolution