Sub topic 6
Keeping unexpected data in _rescued_data
This chapter assumes you can read a PySpark readStream and writeStream pipeline, you know what a Delta table is, and you have seen the schema location from the previous chapter. Here you will learn what the rescued data column holds, when Auto Loader fills it, and how to use it so that no source data is silently lost.
The problem the column solves
Files in a landing folder come from systems you do not control. A producer renames a field, sends a string where you expected a number, or adds a column without telling you. A reader that enforces a fixed schema has two bad options. It can fail the whole job, or it can drop the value and give you null.
Auto Loader adds a third option. It keeps the data it could not fit into your schema in a column named _rescued_data. The row still arrives in the table, the expected columns hold what matched, and the leftover content sits in one place where you can inspect it.
With schema inference on, Auto Loader adds this column to the schema by default.
What counts as rescued
Auto Loader puts a value in _rescued_data in these cases:
- The column is missing from the schema. A field exists in the file but not in the schema you have, so its data is rescued.
- The type does not match. A value like
"abc"in a column inferred asBIGINTcannot be converted. - The case does not match. A file has
Order_IDwhile the schema hasorder_id.
The column is a string that holds a JSON document. Its keys are the original column names and its values are the original data. Auto Loader also adds a _file_path key that records the file the data came from.
A row with no rescued content has null in _rescued_data. That makes the column easy to filter on.
A worked example
Suppose the schema was inferred from earlier files and has order_id, amount, and order_ts. A new file arrives with this content:
{"order_id": 1001, "amount": "12.50", "order_ts": "2026-12-01T10:00:00", "coupon": "WELCOME"}
{"order_id": 1002, "amount": "oops", "order_ts": "2026-12-01T10:05:00"}The coupon field is new. The value "oops" in amount cannot become a number when amount is typed as DOUBLE. Here is a stream that reads the file with a fixed schema and the default rescue behavior.
from pyspark.sql.types import StructType, StructField, LongType, DoubleType, TimestampType
schema = StructType([
StructField("order_id", LongType()),
StructField("amount", DoubleType()),
StructField("order_ts", TimestampType()),
])
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaEvolutionMode", "rescue")
.schema(schema)
.load("/Volumes/main/raw/landing/orders"))
(df.writeStream
.option("checkpointLocation", "/Volumes/main/raw/_checkpoints/orders_rescue")
.trigger(availableNow=True)
.toTable("main.bronze.orders_rescue"))Because the stream sets a schema explicitly, no schema location is needed. The rescue mode tells Auto Loader never to change the schema and to keep unmatched data in the rescued column instead.
After the run, the table looks roughly like this:
| order_id | amount | order_ts | _rescued_data |
|---|---|---|---|
| 1001 | 12.5 | 2026-12-01 10:00:00 | {"coupon":"WELCOME","_file_path":"/Volumes/main/raw/landing/orders/part-1.json"} |
| 1002 | null | 2026-12-01 10:05:00 | {"amount":"oops","_file_path":"/Volumes/main/raw/landing/orders/part-1.json"} |
The first row keeps its valid columns and carries the new field in _rescued_data. The second row has null in amount, and the bad value is preserved in the rescued column. You can still recover "oops" later.
How the column interacts with schema evolution
The rescued data column and schema evolution are two answers to the same question: what happens when the data changes shape. The mode you choose decides which one applies.
| Mode | New column in the data | Where the new data goes |
|---|---|---|
| `addNewColumns` | The stream stops with an error, then picks up the new column on restart | Added to the schema. Type mismatches still go to `_rescued_data` |
| `rescue` | The schema never changes | The new column's data goes to `_rescued_data` |
| `failOnNewColumns` | The stream fails and stays failed until you change the schema | Not applicable until you update the schema or remove the file |
| `none` | The schema is not evolved | New columns are ignored. With the rescued data column off, their data is dropped |
The default is addNewColumns when you provide no schema, and none when you do provide one. In the example above, the explicit rescue setting avoids the none default.
Even in addNewColumns mode, rescue matters. Evolution adds columns for new fields, but it does not change the type of an existing column. A value that does not fit the existing type still lands in _rescued_data.
Using the column in a pipeline
Treat _rescued_data as a signal. A healthy pipeline has mostly null values in it. When the share of non-null values grows, something upstream changed.
This query counts rescued rows per day:
SELECT
date(order_ts) AS order_date,
count(*) AS total_rows,
count_if(_rescued_data IS NOT NULL) AS rescued_rows
FROM main.bronze.orders_rescue
GROUP BY date(order_ts)
ORDER BY order_date;To read keys out of the JSON string, use get_json_object or from_json.
from pyspark.sql.functions import col, get_json_object
rescued = (spark.table("main.bronze.orders_rescue")
.where(col("_rescued_data").isNotNull())
.select(
"order_id",
get_json_object("_rescued_data", "$.coupon").alias("coupon"),
get_json_object("_rescued_data", "$.amount").alias("raw_amount"),
get_json_object("_rescued_data", "$._file_path").alias("file_path"),
))
rescued.show(truncate=False)Once you decide a rescued field deserves a real column, you have two paths. You can add the column to the schema with addNewColumns or a schema hint, and the field will populate from then on. Or you can backfill from _rescued_data for the rows already ingested.
Controlling the column
You can rename the column with rescuedDataColumn. This helps when a source already has a field named _rescued_data, or when your naming standard differs.
df = (spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/raw/_schemas/orders")
.option("rescuedDataColumn", "_rescued")
.load("/Volumes/main/raw/landing/orders"))If you read with an explicit schema and want the column, you must request it. Setting a schema without a rescue option, and with the default none evolution mode, means unmatched data is not captured. Setting cloudFiles.schemaEvolutionMode to rescue, as in the worked example, or naming rescuedDataColumn turns it on.
Common errors and gotchas
- Ignoring the column. The table loads without errors, so nobody looks. Add a check on the non-null rate and alert when it rises.
- Expecting a type change. Rescue keeps the bad value. It does not widen
BIGINTtoSTRING. To change the type, use a schema hint and handle existing data yourself. - Parsing the JSON in every query. Downstream users then each write their own parsing. Promote recurring fields to real columns in a silver step.
- Case mismatches. Names are matched case-insensitively by default in many setups, but a mismatch in a case-sensitive configuration pushes the data to the rescued column. Confirm your
spark.sql.caseSensitivesetting. - Dropping the column early. Selecting only known columns in the bronze write discards your safety net. Keep
_rescued_datain bronze tables. - Treating
nullamounts as true nulls. In the example, row 1002 hasamount = nullbecause of a failed conversion, not because the source was empty. Join the null with the rescued column to tell them apart.
Exercise
A file arrives with the record {"orderid": 7, "amount": 9.99, "region": "EU"}. Your schema has only orderid and amount, and the stream uses schemaEvolutionMode "rescue". What do the three columns orderid, amount, and rescued_data contain for this row, and what changes if you switch the mode to addNewColumns?
Show answer
With rescue, orderid is 7, amount is 9.99, and rescueddata holds JSON with the region value ("EU") and the filepath key. The schema does not change. With addNewColumns, the stream stops with an error when it finds the new field, and after a restart the schema includes a region column, so later records populate region directly instead of using rescued_data.
Exercise
Name two cases where a value ends up in rescueddata even when the schema evolution mode is addNewColumns.
Show answer
A value whose type cannot be converted to the existing column type, such as the string "oops" in a numeric column, and a value whose column name differs from the schema only in case when matching is case sensitive. Evolution adds new column names but does not change the type of an existing column.