Sub topic 12
Handling schema changes in Auto Loader
This chapter assumes you can already run an Auto Loader stream into a Delta table with a checkpoint, as in the earlier ingestion chapter. Here you will learn what happens when the incoming files change shape, and how to choose a response.
How Auto Loader tracks the schema
Auto Loader samples your files and infers a schema. It saves that schema in the directory set by cloudFiles.schemaLocation. On later runs it reads the saved schema instead of inferring again. Each time the schema changes, it stores a new version there.
For JSON and CSV, inference returns every column as a string by default. Set cloudFiles.inferColumnTypes to true to get real types. Use cloudFiles.schemaHints to fix the type of specific columns.
Choosing an evolution mode
The option cloudFiles.schemaEvolutionMode controls what happens when a file contains a column the saved schema does not have.
| Mode | New column behavior |
|---|---|
| `addNewColumns` | The stream fails once. The new column is added to the saved schema. The restarted stream includes it. This is the default when you do not supply a schema. |
| `rescue` | The schema never changes. The new data goes into the `_rescued_data` column. |
| `failOnNewColumns` | The stream fails. It stays failed until you update the schema or remove the data. |
| `none` | The schema is ignored and new columns are dropped. If you also set `rescuedDataColumn`, they go there. This is the default when you supply a schema. |
A complete example
df = (
spark.readStream.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/demo/chk/orders_schema")
.option("cloudFiles.inferColumnTypes", "true")
.option("cloudFiles.schemaEvolutionMode", "addNewColumns")
.option("cloudFiles.schemaHints", "order_id BIGINT")
.load("/Volumes/main/demo/landing/orders")
)
(
df.writeStream
.option("checkpointLocation", "/Volumes/main/demo/chk/orders")
.option("mergeSchema", "true")
.trigger(availableNow=True)
.toTable("main.demo.orders_bronze")
)The mergeSchema option on the write lets the Delta target add the new column. Without it, the write fails even after Auto Loader has updated its own schema.
The rescued data column
In rescue mode, and whenever a value cannot be parsed to the expected type, Auto Loader puts the data in _rescued_data. This column is a JSON string. It holds the unmatched column names and values, plus the source file path. Nothing is lost, so you can promote a column later after you decide on its name and type.
Common errors
- The stream fails with an unknown field error. This is the normal signal in
addNewColumnsmode. Restart the query. If it fails again, check that the schema location is the same as before. - The target table rejects the new column. Add
mergeSchemato the write, or alter the table yourself. - All columns are strings. You did not set
cloudFiles.inferColumnTypes. - A type change breaks the stream. Changing a column's type, for example from integer to string, does not evolve the schema. The data goes to
_rescued_datainstead. Use a schema hint to set the type you want. - A new schema location starts over. Pointing at a fresh schema location re-infers the schema from scratch. Keep the location stable, just like the checkpoint.
Exercise
A stream uses addNewColumns. A file with a new column coupon arrives and the job fails. You restart it and the Auto Loader read now includes coupon, but the write to the Delta table still fails. What is the likely cause, and what is the fix?
Show answer
Auto Loader updated its own saved schema, but the Delta target table does not have the coupon column. Add .option("mergeSchema", "true") to the writeStream, or add the column to the table with ALTER TABLE.