DataEng Hub
Delta LakeintermediateLearning · 1 min read

MERGE needs its own schema evolution switch

Delta's mergeSchema option does nothing for MERGE statements — automatic schema evolution for MERGE is a separate session setting.

Balaji S.
Lead Data Engineer

The takeaway

option("mergeSchema", "true") only applies to writes (append/overwrite). A MERGE INTO that should add new source columns to the target needs:

SET spark.databricks.delta.schema.autoMerge.enabled = true;

or, on newer runtimes, the explicit WHEN NOT MATCHED THEN INSERT * with schema evolution enabled on the table operation.

Context

A CDC feed added a discount_code column upstream. The nightly MERGE kept succeeding — and silently dropping the new column, because UPDATE SET * and INSERT * resolve against the target schema.

Example

SET spark.databricks.delta.schema.autoMerge.enabled = true;
 
MERGE INTO silver.orders t
USING staged_updates s
ON t.order_id = s.order_id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;
-- discount_code now lands as a new nullable column

Why it matters

Silently dropped columns are the worst kind of failure: no error, wrong data, discovered weeks later. If your source schemas drift, decide explicitly — auto-merge on, or a schema check that fails loudly.

Go deeper

#merge#schema-evolution

Keep going

One practical note a week

Get new articles, problems and guides in your inbox.