---
title: Type widening
description: Delta Lake type widening on Databricks allows you to change column data types to a wider type without rewriting data files, supporting manual and schema-evolution-driven type changes.
canonical_url: https://docs.databricks.com/gcp/en/tables/features/type-widening
---

# Type widening

Available on Delta Lake tables in Databricks Runtime 15.4 LTS and above, type widening allows you to change column data types to a wider type without rewriting data files.

All Unity Catalog managed tables use Delta Lake by default. See [Unity Catalog managed tables for Delta Lake and Apache Iceberg](https://docs.databricks.com/gcp/en/tables/managed.md).

> [!NOTE]
>
> Enabling type widening upgrades the reader and writer protocols. This might affect compatibility with external Delta Lake clients. See [Delta Lake feature compatibility and protocols](https://docs.databricks.com/gcp/en/tables/features/feature-compatibility.md).
>
> Tables with type widening enabled can only be read by Databricks Runtime 15.4 LTS and above.

## Supported type changes

You can widen types according to the following rules:

| Source type | Supported wider types                      |
| ----------- | ------------------------------------------ |
| `BYTE`      | `SHORT`, `INT`, `BIGINT`                   |
| `SHORT`     | `INT`, `BIGINT`                            |
| `INT`       | `BIGINT`                                   |
| `FLOAT`     | `DOUBLE`                                   |
| `DECIMAL`   | `DECIMAL` with greater precision and scale |
| `DATE`      | `TIMESTAMP_NTZ`                            |
| `VOID`      | Any type                                   |

In Databricks Runtime 15.4.3 and above, you can't change `BYTE`, `SHORT`, `INT`, or `BIGINT` to `DECIMAL`, or `BYTE`, `SHORT`, or `INT` to `DOUBLE`.

Type changes are supported for top-level columns and fields nested inside structs, maps, and arrays.

> [!NOTE]
>
> `VOID` to any type does not require type widening to be enabled on the table. Any operation that updates the type of a `VOID` column succeeds without additional configuration. `VOID` type widening is available in Databricks Runtime 18.2 and above.

### Decimal behavior

When widening a `decimal` type, the total precision must be equal to or greater than the starting precision. If you also increase the scale, the total precision must increase by a corresponding amount.

If you want to add two decimal places to a field with `decimal(10,1)`, the minimum target is `decimal(12,3)`.

## Enable type widening

> [!NOTE]
>
> Enabling type widening upgrades the reader and writer protocols. This might affect compatibility with external Delta Lake clients. See [Delta Lake feature compatibility and protocols](https://docs.databricks.com/gcp/en/tables/features/feature-compatibility.md).

You can enable type widening on an existing table by setting the `delta.enableTypeWidening` table property to `true`:

```sql
  ALTER TABLE <table_name> SET TBLPROPERTIES ('delta.enableTypeWidening' = 'true')
```

You can also enable type widening during table creation:

```sql
  CREATE TABLE T(c1 INT) TBLPROPERTIES('delta.enableTypeWidening' = 'true')
```

## Manually apply a type change

Use the `ALTER COLUMN` command to manually change types:

```sql
ALTER TABLE <table_name> ALTER COLUMN <col_name> TYPE <new_type>
```

This operation updates the table schema without rewriting the underlying data files. See [ALTER TABLE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-ddl-alter-table-manage-column.md#alter-column-clause) for more details.

## <a id="widen-types-with-automatic-schema-evolution" />Widen types with automatic schema evolution

Use schema evolution with type widening to update data types in target tables to match the type of incoming data.

> [!NOTE]
>
> Without type widening enabled, schema evolution always attempts to downcast data to match column types in the target table. If you don't want to automatically widen data types in your target tables, you must turn off type widening before you run workloads with schema evolution enabled.

To use schema evolution to widen the data type of a column during ingestion, you must meet the following conditions:

* The write command runs with automatic schema evolution enabled.
* The target table has type widening enabled.
* The source column type is wider than the target column type.
* Type widening supports the type change.

Type mismatches that don't meet all of these conditions follow normal schema enforcement rules. See [Schema enforcement](https://docs.databricks.com/gcp/en/tables/schema-enforcement.md).

### Example

The following examples demonstrate how type widening works with schema evolution.

#### Python

Create a target table with an `INT` column and a source table with a `BIGINT` column:

```python
spark.sql("CREATE TABLE target_table (id INT, data STRING) TBLPROPERTIES ('delta.enableTypeWidening' = 'true')")
spark.sql("CREATE TABLE source_table (id BIGINT, data STRING)")
```

Use `saveAsTable()` with schema evolution to automatically widen the `INT` column to `BIGINT` during an append:

```python
spark.table("source_table").write.mode("append").option("mergeSchema", "true").saveAsTable("target_table")
```

Use `MERGE INTO` with schema evolution:

```python
from delta.tables import DeltaTable

source_df = spark.table("source_table")
target_table = DeltaTable.forName(spark, "target_table")

(target_table.alias("target")
  .merge(source_df.alias("source"), "target.id = source.id")
  .withSchemaEvolution()
  .whenMatchedUpdateAll()
  .whenNotMatchedInsertAll()
  .execute()
)
```

#### Scala

Create a target table with an `INT` column and a source table with a `BIGINT` column:

```scala
spark.sql("CREATE TABLE target_table (id INT, data STRING) TBLPROPERTIES ('delta.enableTypeWidening' = 'true')")
spark.sql("CREATE TABLE source_table (id BIGINT, data STRING)")
```

Use `saveAsTable()` with schema evolution to automatically widen the `INT` column to `BIGINT` during an append:

```scala
spark.table("source_table").write.mode("append").option("mergeSchema", "true").saveAsTable("target_table")
```

Use `MERGE INTO` with schema evolution:

```scala
import io.delta.tables.DeltaTable

val sourceDf = spark.table("source_table")
val targetTable = DeltaTable.forName(spark, "target_table")

targetTable.alias("target")
  .merge(sourceDf.alias("source"), "target.id = source.id")
  .withSchemaEvolution()
  .whenMatched().updateAll()
  .whenNotMatched().insertAll()
  .execute()
```

#### SQL

Create a target table with an `INT` column and a source table with a `BIGINT` column:

```sql
CREATE TABLE target_table (id INT, data STRING) TBLPROPERTIES ('delta.enableTypeWidening' = 'true');
CREATE TABLE source_table (id BIGINT, data STRING);
```

Use `INSERT INTO` with schema evolution to automatically widen the `INT` column to `BIGINT` during an append:

```sql
INSERT WITH SCHEMA EVOLUTION INTO target_table SELECT * FROM source_table;
```

Use `MERGE INTO` with schema evolution:

```sql
MERGE WITH SCHEMA EVOLUTION INTO target_table
USING source_table
ON target_table.id = source_table.id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *;
```

## Auto Loader

> [!NOTE]
> **Preview**
>
> Type widening support in Auto Loader is in [Public Preview](https://docs.databricks.com/gcp/en/release-notes/release-types.md).

Auto Loader supports type widening with automatic schema evolution. When you use Auto Loader to ingest data into a Delta Lake table with type widening and schema evolution enabled, column types are automatically widened to match the incoming data.

```python
(spark.readStream
  .format("cloudFiles")
  .option("cloudFiles.format", "json")
  .option("cloudFiles.schemaLocation", "<path-to-schema-location>")
  .load("<path-to-source-data>")
  .writeStream
  .option("mergeSchema", "true")
  .option("checkpointLocation", "<path-to-checkpoint>")
  .trigger(availableNow=True)
  .toTable("table_name")
)
```

See [Automatic type widening with Auto Loader](https://docs.databricks.com/gcp/en/ingestion/cloud-object-storage/auto-loader/type-widening.md). Also, the target table must have type widening enabled. See [Enable type widening](https://docs.databricks.com/gcp/en/tables/features/type-widening.md#enable-type-widening).

## <a id="drop" /> Disable the type widening table feature

You can prevent accidental type widening on enabled tables by setting the property to `false`:

```sql
  ALTER TABLE <table_name> SET TBLPROPERTIES ('delta.enableTypeWidening' = 'false')
```

This setting prevents future type changes to the table, but doesn't remove the type widening table feature or undo previous type changes.

If you need to completely remove the type widening table features, you can use the `DROP FEATURE` command as shown in the following example:

```sql
 ALTER TABLE <table-name> DROP FEATURE 'typeWidening' [TRUNCATE HISTORY]
```

> [!NOTE]
>
> Tables that enabled type widening using Databricks Runtime 15.4 LTS require you to drop the feature `typeWidening-preview` instead.

When dropping type widening, Databricks rewrites all data files that don't conform to the current table schema. See [Drop a Delta Lake table feature and downgrade table protocol](https://docs.databricks.com/gcp/en/tables/features/drop-feature.md).

<a id="streaming" />

## Streaming from a Delta Lake table

Support for type widening in Structured Streaming is available in Databricks Runtime 16.4 LTS and above.

When streaming from a Delta Lake table with type widening enabled, you can configure automatic type widening for streaming queries by enabling schema evolution with the `mergeSchema` option on the target table. The target table must have type widening enabled. See [Enable type widening](https://docs.databricks.com/gcp/en/tables/features/type-widening.md#enable-type-widening).

### Python

```python
(spark.readStream
  .table("delta_source_table")
  .writeStream
  .option("checkpointLocation", "/path/to/checkpointLocation")
  .option("mergeSchema", "true")
  .toTable("output_table")
)
```

### Scala

```scala
spark.readStream
  .table("delta_source_table")
  .writeStream
  .option("checkpointLocation", "/path/to/checkpointLocation")
  .option("mergeSchema", "true")
  .toTable("output_table")
```

When `mergeSchema` is enabled and the target table has type widening enabled:

* Type changes are applied automatically to the downstream table without requiring manual intervention.
* New columns are added automatically to the downstream table schema.

Without `mergeSchema` enabled, values are handled according to the `spark.sql.storeAssignmentPolicy` configuration, which by default downcasts values to match the target column type. For more information about assignment policy behavior, see [Store assignment](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-ansi-compliance.md#store-assignment).

### Handle type changes in a stream

When streaming from a Delta Lake table, you can provide a schema tracking location to track non-additive schema changes including type changes. Providing a schema tracking location is required in Databricks Runtime 18.0 and below, and it is optional in Databricks Runtime 18.1 and above.

You can't set a `schemaTrackingLocation` using SQL. See [Unsupported features](https://docs.databricks.com/gcp/en/tables/features/type-widening.md#other-limits).

`schemaTrackingLocation` must be set to a location within the same path as your streaming checkpoint. For example:

#### Python

```python
checkpoint_path = "/path/to/checkpointLocation"

(spark.readStream
  .option("schemaTrackingLocation", checkpoint_path)
  .table("delta_source_table")
  .writeStream
  .option("checkpointLocation", checkpoint_path)
  .toTable("output_table")
)
```

#### Scala

```scala
val checkpointPath = "/path/to/checkpointLocation"

spark.readStream
  .option("schemaTrackingLocation", checkpointPath)
  .table("delta_source_table")
  .writeStream
  .option("checkpointLocation", checkpointPath)
  .toTable("output_table")
```

After setting a schema tracking location, the stream evolves its tracked schema when it detects a type change and then stops. At that time, you must handle the type change, such as enabling type widening on the downstream table or updating the streaming query.

To resume processing, set the Spark configuration `spark.databricks.delta.streaming.allowSourceColumnTypeChange` or the `DataFrame` reader option `allowSourceColumnTypeChange`, as in the following example:

#### Python

```python
checkpoint_path = "/path/to/checkpointLocation"

(spark.readStream
  .option("schemaTrackingLocation", checkpoint_path)
  .option("allowSourceColumnTypeChange", "<delta_source_table_version>")
  # alternatively to allow all future type changes for this stream:
  # .option("allowSourceColumnTypeChange", "always")
  .table("delta_source_table")
  .writeStream
  .option("checkpointLocation", checkpoint_path)
  .toTable("output_table")
)
```

#### Scala

```scala
val checkpointPath = "/path/to/checkpointLocation"

spark.readStream
  .option("schemaTrackingLocation", checkpointPath)
  .option("allowSourceColumnTypeChange", "<delta_source_table_version>")
  // alternatively to allow all future type changes for this stream:
  // .option("allowSourceColumnTypeChange", "always")
  .table("delta_source_table")
  .writeStream
  .option("checkpointLocation", checkpointPath)
  .toTable("output_table")
```

#### SQL

```sql
  -- To unblock for this particular stream just for this series of schema change(s):
  SET spark.databricks.delta.streaming.allowSourceColumnTypeChange.ckpt_<checkpoint_id> = "<delta_source_table_version>"
  -- To unblock for this particular stream:
  SET spark.databricks.delta.streaming.allowSourceColumnTypeChange = "<delta_source_table_version>"
  -- To unblock for all streams:
  SET spark.databricks.delta.streaming.allowSourceColumnTypeChange = "always"
```

When the stream stops, an error message displays the checkpoint ID `<checkpoint_id>` and Delta Lake source table version `<delta_source_table_version>`.

For a complete list of streaming Delta Lake options, see [Delta Lake](https://docs.databricks.com/gcp/en/spark/api-options.md#stream-reader-delta).

## <a id="lakeflow-pipelines" />Lakeflow pipelines

You can enable type widening for Lakeflow pipelines at the pipeline level or for individual tables. Type widening allows column types to be automatically widened during pipeline execution without requiring a full refresh of streaming tables. Type changes in materialized views always trigger a full recompute, and when a type change is applied to a source table, materialized views that depend on that table require a full recompute to reflect the new types.

### Enable type widening for an entire pipeline

To enable type widening for all tables in a pipeline, set the pipeline configuration `pipelines.enableTypeWidening`:

#### JSON

```json
{
  "configuration": {
    "pipelines.enableTypeWidening": "true"
  }
}
```

#### YAML

```yaml
configuration:
  pipelines.enableTypeWidening: 'true'
```

### Enable type widening for specific tables

You can also enable type widening for individual tables by setting the table property `delta.enableTypeWidening`:

#### Python

```python
import dlt

@dlt.table(
  table_properties={"delta.enableTypeWidening": "true"}
)
def my_table():
  return spark.readStream.table("source_table")
```

#### SQL

```sql
CREATE OR REFRESH STREAMING TABLE my_table
TBLPROPERTIES ('delta.enableTypeWidening' = 'true')
AS SELECT * FROM source_table
```

### Compatibility with downstream readers

Tables with type widening enabled can only be read in Databricks Runtime 15.4 LTS and above. If you want a table with type widening enabled in your pipeline to be readable by readers on Databricks Runtime 14.3 and below, you must either:

* Turn off type widening by removing the property `delta.enableTypeWidening`/`pipelines.enableTypeWidening` or setting it to false, and trigger a full refresh of the table.
* Enable [Compatibility Mode](https://docs.databricks.com/gcp/en/external-access/compatibility-mode.md) on your table.

## OpenSharing

> [!NOTE]
>
> Support for Type Widening in OpenSharing is available in Databricks Runtime 16.1 and above.

Sharing a Delta Lake table with type widening enabled is supported in Databricks-to-Databricks OpenSharing. The provider and recipient must be on Databricks Runtime 16.1 or above.

To read change data feed from a Delta Lake table with type widening enabled using OpenSharing, you must set the response format to `delta`:

```scala
spark.read
  .format("deltaSharing")
  .option("responseFormat", "delta")
  .option("readChangeFeed", "true")
  .option("startingVersion", "<start version>")
  .option("endingVersion", "<end version>")
  .load("<table>")
```

Reading change data feed across type changes isn't supported. You must instead split the operation into two separate reads, one ending at the table version containing the type change, and the other starting at the version containing the type change.

## Limitations

### Apache Iceberg Compatibility

Apache Iceberg doesn't support all type changes covered by type widening. See [Iceberg Schema Evolution](https://iceberg.apache.org/spec/#schema-evolution).

Unsupported type changes include the following:

* `byte`, `short`, `int`, `long` to `decimal` or `double`
* decimal scale increase
* `date` to `timestampNTZ`

When you enable Iceberg reads on a Delta Lake table, applying one of the preceding type changes results in an error. See [Read Delta Lake tables with Iceberg clients](https://docs.databricks.com/gcp/en/delta/iceberg-reads.md).

If you apply one of these unsupported type changes to a Delta Lake table, you have two options:

* Regenerate Iceberg metadata: Use the following command to regenerate Iceberg metadata without the type widening table feature:

  ```sql
  ALTER TABLE <table-name> SET TBLPROPERTIES ('delta.universalFormat.config.icebergCompatVersion' = '<version>')
  ```

  This allows you to maintain compatibility with Iceberg reads after applying incompatible type changes.

* Drop the type widening table feature: See [ Disable the type widening table feature](https://docs.databricks.com/gcp/en/tables/features/type-widening.md#drop).

### Type-dependent functions

Some SQL functions return results that depend on the input data type. For example, [`hash` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/hash.md) returns different hash values for the same logical value if the argument type is different: `hash(1::INT)` returns a different result than `hash(1::BIGINT)`.

Other type dependent functions are: [`xxhash64`](https://docs.databricks.com/gcp/en/sql/language-manual/functions/xxhash64.md), [`bit_get`](https://docs.databricks.com/gcp/en/sql/language-manual/functions/bit_get.md), [`bit_reverse`](https://docs.databricks.com/gcp/en/sql/language-manual/functions/bit_reverse.md), [`typeof`](https://docs.databricks.com/gcp/en/sql/language-manual/functions/typeof.md).

For stable results in queries that use these functions, you must explicitly cast values to the desired type:

#### Python

```python
spark.read.table("table_name") \
  .selectExpr("hash(CAST(column_name AS BIGINT))")
```

#### Scala

```scala
spark.read.table("main.johan_lasperas.dlt_type_widening_bronze2")
  .selectExpr("hash(CAST(a AS BIGINT))")
```

#### SQL

```sql
-- Use explicit casting for stable hash values
SELECT hash(CAST(column_name AS BIGINT)) FROM table_name
```

### <a id="other-limits" />Unsupported features

* You can't set a schema tracking location using SQL when streaming from a Delta Lake table with a type change.
* You cannot share a table with type widening enabled to non-Databricks consumers using OpenSharing.
