cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to handle changing column data types in Auto Loader?

angelinak
New Contributor

How do you handle a situation where an upstream system suddenly changes the data type of an existing column?

For example, a column that was originally an integer starts arriving as a string in new Parquet files.

What is the best way to handle these files without stopping the entire ingestion pipeline? Can schema hints or rescued data columns help in this situation?

2 ACCEPTED SOLUTIONS

Accepted Solutions

SumeshKashyap
Honored Contributor

Hi @angelinak 

Auto Loader's schema evolution only adds new columns. It doesn't change an existing column's type. With the rescued data column (on by default when Auto Loader infers the schema), a value whose type doesn't match the schema is set to null in its column and saved in _rescued_data, so the stream keeps running. Keep the column at its intended type with a schema hint, then repair or quarantine the rescued values downstream.

bronze = (spark.readStream.format("cloudFiles")

   .option("cloudFiles.format", "parquet")

   .option("cloudFiles.schemaLocation", "/Volumes/.../_schemas/orders")

   .option("cloudFiles.schemaHints", "quantity INT")

   .option("rescuedDataColumn", "_rescued_data")

   .load("/Volumes/.../landing/orders/"))

Downstream (silver), recover what you can and quarantine the rest:

from pyspark.sql import functions as F

rescued = F.get_json_object("_rescued_data", "$.quantity")

silver = (bronze

   .withColumn("quantity", F.coalesce(F.col("quantity"), rescued.try_cast("int")))

   .withColumn("_bad_quantity", F.col("quantity").isNull() & rescued.isNotNull()))

Rows where _bad_quantity is true (for example "12 units") can go to a quarantine table instead of failing the load. _rescued_data also records the source file path, so you can tell the upstream team exactly which files changed.

Answers to your two questions:

- Schema hints: yes. They pin the expected type so inference doesn't flip the column to string. On their own they don't fix the bad values.

- Rescued data column: yes. It's what keeps the pipeline running. Without it, a type mismatch fails the stream.

If the change is permanent (the column really is a string now), change the target column on purpose: add a new column or rewrite the table. Delta type widening only covers safe changes like INT to BIGINT, not INT to STRING.

Docs: https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/schema#rescued-data-co...

View solution in original post

tajamulkhan
Esteemed Contributor
Yes, schema hints and rescued data columns can help, but it is important to distinguish schema evolution from data type conversion.

Changing an existing column from integer to string is not a supported automatic type-widening operation.

1. Use rescue mode to maintain uninterrupted ingestion

df = (
    spark.readStream
    .format("cloudFiles")
    .option("cloudFiles.format", "parquet")
    .option("cloudFiles.schemaLocation", schema_path)
    .option("cloudFiles.schemaEvolutionMode", "rescue")
    .option("rescuedDataColumn", "_rescued_data")
    .load(source_path)
)
 

Incompatible fields can be captured in _rescued_data without automatically changing the existing schema.

2. Standardize data types in the Silver layer

For example, if customer_id was originally an integer but starts arriving as a string such as CUST101, you can recover and standardize the value:

from pyspark.sql import functions as F

df = df.withColumn(
    "customer_id",
    F.coalesce(
        F.col("customer_id").cast("string"),
        F.get_json_object(
            "_rescued_data", "$.customer_id"
        )
    )
)
 

3. Understand schema hint limitations

Schema hints for Parquet instruct the reader how to interpret a column. They do not automatically cast incompatible physical data types.

My recommendation is to preserve unexpected data in the Bronze layer, standardize data types in the Silver layer and configure alerts whenever _rescued_data contains values.

This keeps ingestion resilient while maintaining downstream data quality. The example assumes the rescued JSON contains the incompatible field.

Official references:

 

View solution in original post

3 REPLIES 3

SumeshKashyap
Honored Contributor

Hi @angelinak 

Auto Loader's schema evolution only adds new columns. It doesn't change an existing column's type. With the rescued data column (on by default when Auto Loader infers the schema), a value whose type doesn't match the schema is set to null in its column and saved in _rescued_data, so the stream keeps running. Keep the column at its intended type with a schema hint, then repair or quarantine the rescued values downstream.

bronze = (spark.readStream.format("cloudFiles")

   .option("cloudFiles.format", "parquet")

   .option("cloudFiles.schemaLocation", "/Volumes/.../_schemas/orders")

   .option("cloudFiles.schemaHints", "quantity INT")

   .option("rescuedDataColumn", "_rescued_data")

   .load("/Volumes/.../landing/orders/"))

Downstream (silver), recover what you can and quarantine the rest:

from pyspark.sql import functions as F

rescued = F.get_json_object("_rescued_data", "$.quantity")

silver = (bronze

   .withColumn("quantity", F.coalesce(F.col("quantity"), rescued.try_cast("int")))

   .withColumn("_bad_quantity", F.col("quantity").isNull() & rescued.isNotNull()))

Rows where _bad_quantity is true (for example "12 units") can go to a quarantine table instead of failing the load. _rescued_data also records the source file path, so you can tell the upstream team exactly which files changed.

Answers to your two questions:

- Schema hints: yes. They pin the expected type so inference doesn't flip the column to string. On their own they don't fix the bad values.

- Rescued data column: yes. It's what keeps the pipeline running. Without it, a type mismatch fails the stream.

If the change is permanent (the column really is a string now), change the target column on purpose: add a new column or rewrite the table. Delta type widening only covers safe changes like INT to BIGINT, not INT to STRING.

Docs: https://docs.databricks.com/aws/en/ingestion/cloud-object-storage/auto-loader/schema#rescued-data-co...

tajamulkhan
Esteemed Contributor
Yes, schema hints and rescued data columns can help, but it is important to distinguish schema evolution from data type conversion.

Changing an existing column from integer to string is not a supported automatic type-widening operation.

1. Use rescue mode to maintain uninterrupted ingestion

df = (
    spark.readStream
    .format("cloudFiles")
    .option("cloudFiles.format", "parquet")
    .option("cloudFiles.schemaLocation", schema_path)
    .option("cloudFiles.schemaEvolutionMode", "rescue")
    .option("rescuedDataColumn", "_rescued_data")
    .load(source_path)
)
 

Incompatible fields can be captured in _rescued_data without automatically changing the existing schema.

2. Standardize data types in the Silver layer

For example, if customer_id was originally an integer but starts arriving as a string such as CUST101, you can recover and standardize the value:

from pyspark.sql import functions as F

df = df.withColumn(
    "customer_id",
    F.coalesce(
        F.col("customer_id").cast("string"),
        F.get_json_object(
            "_rescued_data", "$.customer_id"
        )
    )
)
 

3. Understand schema hint limitations

Schema hints for Parquet instruct the reader how to interpret a column. They do not automatically cast incompatible physical data types.

My recommendation is to preserve unexpected data in the Bronze layer, standardize data types in the Silver layer and configure alerts whenever _rescued_data contains values.

This keeps ingestion resilient while maintaining downstream data quality. The example assumes the rescued JSON contains the incompatible field.

Official references:

 

balajij8
Esteemed Contributor III

Parquet files carry their own schema inside them - every file knows what type each column is. When Auto Loader started the stream, it sampled the first files it saw, found id as INT and locked that schema in (stores it under a _schemas directory in the schema location so it can track changes over time). Every file after that gets read against that locked schema. So when new Parquet files show up with id as a string, the reader finds it as a mismatch. The key thing is - Auto Loader treats a type mismatch as rescuable data not as a corrupt record or an error. Hence the pipeline generally does not crash - the value in the typed column becomes NULL and the original value gets safely tucked into _rescued_data as a JSON blob (also records which file it came from). Note that int to string is not a widening change (unlike int to long or float to double), so even the newest automatic type widening feature cannot silently evolve the column - it would also route those values to _rescued_data.

You can follow below

Confirm with the upstream team that the change is intentional and permanent - if it's a bug on their side, fixing it there is the right option. If it's permanent, two things need to happen. On the read side, add a schema hint so Auto Loader reads the column as a string going forward - like .option("cloudFiles.schemaHints", "id string"). Note that with Parquet, a hint is not a cast - it tells the Parquet reader to read the column as that type. On the write side, the Delta table still has the column as INT and since int to string is an incompatible type change for a Delta table, you need to handle the table side deliberately - by writing to a fresh table with the new schema and switching over, or backfilling after recreating the table or casting in a downstream layer if you would like to keep the raw table as is. Restart the stream (schema changes are picked up at stream start). Lastly, don't forget the data that was rescued while the mismatch was live - those rows can be found in _rescued_data and you can reprocess or backfill them from the source files so nothing is lost.