Sooner or later, a source team adds a column. Maybe it's a new field in a JSON event, or a CSV export that grew from 12 columns to 13. Nobody tells the data team, because to them it's a tiny change.
What happens next depends on a single Auto Loader option most of us never set on purpose: cloudFiles.schemaEvolutionMode. This post walks through what each mode does, how to pick one, and a small monitoring query so a schema change becomes a note in the morning instead of a broken pipeline.
Step 1: Know what you get by default
If you let Auto Loader infer the schema and give it a schemaLocation, the default mode is addNewColumns. When a new column shows up:
- Auto Loader adds the column to the schema it keeps in the schemaLocation folder.
- The stream stops with an UnknownFieldException.
- When it restarts, it carries on with the new schema.
So the default "fails on purpose". That's fine as long as something restarts the stream. If you run it as a Lakeflow job with retries, it recovers on its own. If you run it by hand from a notebook, it just sits there stopped.
Here's a typical setup:
source_path = "/Volumes/main/raw/landing/orders/"
checkpoint = "/Volumes/main/raw/checkpoints/orders/"
(spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", checkpoint)
.option("cloudFiles.inferColumnTypes", "true")
.load(source_path)
.writeStream
.option("checkpointLocation", checkpoint)
.option("mergeSchema", "true")
.trigger(availableNow=True)
.toTable("main.bronze.orders"))Two details worth noticing:
- mergeSchema on the write side lets the new column land in the Delta table. Without it the stream picks up the column but the write fails.
- inferColumnTypes is set to true. Without it, Auto Loader infers every JSON and CSV column as a string. That's safe, but you'll be casting everything later.
Step 2: Pick the mode that matches the table
There are five modes. In practice, three cover almost everything.
addNewColumns (the default). Good for bronze tables where you want to keep everything the source sends. Pair it with job retries.
rescue. The schema never changes and the stream never fails because of schema drift. Anything unexpected goes into the _rescued_data column as JSON. Good when downstream consumers rely on a fixed shape and you'd rather review new fields before they become real columns.
failOnNewColumns. The stream stops and stays stopped until someone updates the schema or removes the file. Good for tables under a strict contract, where a surprise column really should block the load.
The other two are none (new columns are silently ignored, which I'd avoid unless you're sure) and addNewColumnsWithTypeWidening, which is in Public Preview on DBR 16.4 and above and also widens types like int to long.
Switching mode is one line:
.option("cloudFiles.schemaEvolutionMode", "rescue")Step 3: Pin the columns you care about with schema hints
Inference is a guess based on a sample. For the columns that matter, tell Auto Loader what to expect:
.option("cloudFiles.schemaHints",
"order_id STRING, amount DECIMAL(18,2), order_ts TIMESTAMP")Hints only apply when you don't pass a full schema. If a value doesn't match the hinted type, Auto Loader doesn't drop the row. The value goes to _rescued_data and the column is left null. That brings us to the part most pipelines skip.
Step 4: Actually look at _rescued_data
When Auto Loader infers the schema, it adds _rescued_data for you. It catches three kinds of things:
- columns that aren't in the schema (in rescue mode, that's every new column)
- values with the wrong type, like "N/A" in a numeric field
- column names that only differ by case, like Amount vs amount
Each value is a JSON string that also includes the source file path, so you can trace a bad row back to the file it came from.
The data is safe, but nobody sees it unless you look. Here's a daily check:
SELECT
date(_ingested_at) AS load_date,
count(*) AS total_rows,
count_if(_rescued_data IS NOT NULL) AS rescued_rows,
round(100 * count_if(_rescued_data IS NOT NULL) / count(*), 2) AS rescued_pct
FROM main.bronze.orders
WHERE _ingested_at >= current_date() - INTERVAL 7 DAYS
GROUP BY ALL
ORDER BY load_date DESC;
That assumes you add an ingestion timestamp when you write. It's a good habit anyway:
from pyspark.sql import functions as F
df = (spark.readStream.format("cloudFiles")
# ... options from above ...
.load(source_path)
.withColumn("_ingested_at", F.current_timestamp()))To see which fields are being rescued, pull out the JSON keys:
SELECT
key AS rescued_field,
count(*) AS rows
FROM main.bronze.orders,
LATERAL explode(map_keys(from_json(_rescued_data, 'MAP<STRING,STRING>'))) AS t(key)
WHERE _rescued_data IS NOT NULL
AND _ingested_at >= current_date() - INTERVAL 1 DAY
GROUP BY key
ORDER BY rows DESC;
If you see _file_path and nothing else, ignore that row. It's the metadata Auto Loader adds. Any other key is a field that didn't fit.
Save the first query as a SQL alert that fires when rescued_pct is above zero (or above whatever level you consider normal). Now a schema change is a message you read with your coffee.
Step 5: Make restarts automatic
If you stay on addNewColumns, set retries on the job task so the stream restarts after the planned UnknownFieldException. In a bundle it looks like this:
resources:
jobs:
orders_bronze:
name: orders_bronze
tasks:
- task_key: ingest_orders
notebook_task:
notebook_path: ../src/ingest_orders.py
max_retries: 2
min_retry_interval_millis: 60000Two retries is plenty for a schema change, since the second attempt starts with the updated schema. If it still fails after that, something else is wrong and you want to hear about it.
Takeaways
- The default mode, addNewColumns, stops the stream on purpose when a column appears. Make sure a job retry is there to restart it.
- Choose the mode per table: addNewColumns for raw bronze, rescue when consumers need a fixed shape, failOnNewColumns for strict contracts.
- Use schema hints for the columns that matter, and add mergeSchema on the write so new columns can reach the Delta table.
- _rescued_data only helps if someone looks at it. A one-query alert is enough.
Docs: https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/schema
If you handle schema drift differently, for example with a contract check before the files even land, I'd like to hear how it's working for you.