Sub topic 8
Choosing a schema evolution mode
This chapter assumes you can already run an Auto Loader stream with a schemaLocation, and that you know what _rescued_data holds. Here you decide what the stream does when a new file contains a column it has not seen before.
What a schema evolution mode controls
Source systems add columns over time. The schema evolution mode tells Auto Loader how to react. You set it with cloudFiles.schemaEvolutionMode. Auto Loader stores the current schema in the schemaLocation folder, so any change is recorded there.
The four modes
| Mode | New column appears | Schema changes | Typical use |
|---|---|---|---|
| `addNewColumns` | Stream fails once, then continues after restart | Yes, new column is added | Bronze tables that should grow with the source |
| `rescue` | Stream keeps running | No, value goes to `_rescued_data` | Pipelines that must never stop |
| `failOnNewColumns` | Stream fails | No, until you update the schema | Strict contracts with the producer |
| `none` | Stream keeps running | No, new columns are ignored | Fixed schemas you supply yourself |
addNewColumns is the default when you do not provide a schema. none is the default when you do provide one. Auto Loader does not allow addNewColumns together with an explicit schema.
How addNewColumns behaves
When a file with a new column arrives, Auto Loader stops the stream with an UnknownFieldException. Before it stops, it writes the extended schema to the schema location. On the next start, the stream reads the new schema and ingests the file with the new column included.
The failure is intentional. Because the stream stops, no batch is processed under the old schema. Your job must therefore restart automatically. Databricks Jobs can retry a failed task, and this is the usual setup.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("cloudFiles.schemaEvolutionMode", "addNewColumns")
.load("/Volumes/main/raw/landing/orders"))The target Delta table must accept the new column too. With toTable, you can set .option("mergeSchema", "true") on the writer so the table gains the column. Without it, the write fails.
Choosing a mode
Use rescue when the pipeline must never stop. You keep the data in _rescued_data and can promote it to a real column later.
Common errors
- Job stays failed after a schema change. The stream stops once under
addNewColumns. If nothing restarts it, the data stays unprocessed. Add task retries. - Write fails after restart. The new column is in the stream, but the table lacks it. Enable
mergeSchemaon the writer. - New columns never appear. You may have supplied a schema, so the mode is
none. UseschemaHintsinstead of a full schema if you want evolution. - Changed
schemaLocationby accident. A new location means a new inferred schema from scratch. Keep the path stable.
Exercise
A nightly job uses an explicit schema and the default evolution mode. The producer adds a column called coupon_code. What happens to the column, and how would you get it into the table without writing the schema by hand?
Show answer
With an explicit schema the default mode is none, so the stream keeps running and ignores coupon_code. To capture it, drop the explicit schema, let Auto Loader infer the schema (pinning key types with cloudFiles.schemaHints), and use addNewColumns. Set mergeSchema on the writer and enable task retries so the one-time failure restarts the stream.