Enable automatic table schema evolution¶
Semi-structured data tends to evolve over time. Systems that generate data add new columns to accommodate additional information, which requires downstream tables to evolve accordingly.
The structure of tables in Snowflake can evolve automatically to support the structure of new data received from the data sources. Snowflake supports the following:
- Automatically adding new columns.
- Automatically dropping the NOT NULL constraint from columns that are missing in new data files.
- Automatically adding new nested columns as structured columns, and adding fields to existing structured columns (requires the
INFER_NESTED_TYPEScopy option). For more information, see Detect and evolve structured columns.
To enable table schema evolution, do the following:
- If you’re creating a new table, set the
ENABLE_SCHEMA_EVOLUTIONparameter toTRUEwhen you use the CREATE TABLE or CREATE ICEBERG TABLE command.- For an existing table, modify the table using the ALTER TABLE or ALTER ICEBERG TABLE command and set the
ENABLE_SCHEMA_EVOLUTIONparameter toTRUE.
Loading data from files evolves the table columns when all of the following are true:
- The Snowflake table has the
ENABLE_SCHEMA_EVOLUTIONparameter set toTRUE.- The COPY INTO <table> statement uses the
MATCH_BY_COLUMN_NAMEoption.- The role used to load the data has the
EVOLVE SCHEMAorOWNERSHIPprivilege on the table. For Snowpipe, the role that owns the pipe loads the data.
Additionally, for schema evolution with CSV, when used with MATCH_BY_COLUMN_NAME and PARSE_HEADER, ERROR_ON_COLUMN_COUNT_MISMATCH must be set to false.
Schema evolution is a standalone feature but can be used in conjunction with the schema detection support for retrieving the column definitions from a set of files in cloud storage. In combination, these features enable continuous data pipelines to create new tables from a set of data files in cloud storage and then modify columns of the tables as the schema of new source data files evolves with column additions or deletions.
Usage notes¶
This feature supports Apache Avro, Apache Parquet, CSV, JSON, and ORC files.
This feature is limited to COPY INTO <table> statements and Snowpipe data loads. INSERT operations cannot evolve the target table schema automatically.
Snowpipe Streaming with high-performance architecture supports schema evolution for standard Snowflake tables and Snowflake-managed Iceberg tables. Managed Iceberg support is limited to adding new top-level columns with supported data types. For details, see Table support and schema. The Kafka connector with Snowpipe Streaming Classic also supports schema detection and evolution.
By default, this feature is limited to adding a maximum of 100 columns or applying one schema change per
COPYoperation. Snowpipe doesn’t have the one-change limit because it applies each schema change and then continues loading automatically. To request more than 100 added columns or more than one schema change perCOPYoperation, contact Snowflake Support.There is no limit on dropping NOT NULL column constraints.
Schema evolution is tracked by the
SchemaEvolutionRecordoutput in the following views and commands: INFORMATION_SCHEMA COLUMNS View, ACCOUNT_USAGE COLUMNS View, DESCRIBE TABLE command, and SHOW COLUMNS command.However, for the Kafka connector with Snowpipe Streaming, schema evolution is not tracked by the
SchemaEvolutionRecordoutput. TheSchemaEvolutionRecordoutput always shows NULL.When a column is manually renamed or modified after a schema evolution, the schema evolution record will be cleared.
Schema evolution isn’t supported by tasks.
These usage notes apply to Snowflake-managed Iceberg tables as well.
Schema evolution support: Ingestion method comparison¶
The specific metadata field SchemaEvolutionRecord is used to track schema evolution. You can view this field with the INFORMATION_SCHEMA.COLUMNS View, DESCRIBE TABLE command, and SHOW COLUMNS command.
The following table summarizes schema evolution support and the corresponding SchemaEvolutionRecord tracking behavior across different Snowflake ingestion methods:
| Ingestion method | Architecture or context | Schema evolution support status | SchemaEvolutionRecord tracking behavior |
|---|---|---|---|
| File-based (batch/micro-batch) | COPY INTO <table> command | Fully supported | Visible in tracking views/commands. |
| File-based (batch/micro-batch) | Snowpipe, using automated loading | Fully supported | Visible in tracking views or commands. |
| Streaming at the row level | Snowpipe Streaming (High-performance architecture) | Supported, but doesn’t add structured columns or fields. | Visible in tracking views or commands. |
| Streaming at the row level | Snowpipe Streaming with classic architecture; for example, Kafka connector | Only the classic architecture with Kafka connector is supported, and tracking is limited. | Always shows NULL in tracking views or commands. |
Detect and evolve structured columns¶
With INFER_NESTED_TYPES => TRUE, INFER_SCHEMA
returns structured OBJECT, ARRAY, and MAP types for nested data. With the
INFER_NESTED_TYPES copy option set to TRUE, schema
evolution adds new nested columns as structured columns instead of semi-structured columns, and adds new fields to existing
structured columns:
The INFER_NESTED_TYPES copy option works for Parquet, JSON, Avro, and ORC files that you load into standard tables or
Snowflake-managed Iceberg tables with COPY INTO or Snowpipe. It can’t be used with SELECT transforms, Snowpipe Streaming,
dynamic tables, hybrid tables, or external tables.
Note the following:
- Existing
VARIANTcolumns aren’t converted. To move their data, see Move existing data to structured columns. - Schema evolution doesn’t change the types of existing columns or fields, so a record fails to load if a value doesn’t fit.
- Some queries behave differently on structured columns. For example, querying a field that a structured
OBJECTcolumn doesn’t declare returns an error instead ofNULL, and functions such asTYPEOFfail unless you cast the value toVARIANT. For more information, see Structured data types. - Field names inside structured columns are case-sensitive, even with
MATCH_BY_COLUMN_NAME = CASE_INSENSITIVE. For example,zipandZipare added as separate fields. - By default, a
COPYoperation can add up to 100 fields to structured columns.
Create a table with structured columns from your files¶
To create a table with structured columns that evolve as you load files, complete the following steps:
- Run INFER_SCHEMA with
INFER_NESTED_TYPES => TRUEon files that represent the full range of your data, and check the inferred types, as described in Choose column types. - Create the table with
CREATE TABLE ... USING TEMPLATEor your edited column definitions, and setENABLE_SCHEMA_EVOLUTION = TRUE. For a Snowflake-managed Iceberg table, see Create and evolve Snowflake-managed Iceberg tables. - If a role other than the table owner loads the data, grant that role the
EVOLVE SCHEMAprivilege on the table. For Snowpipe, grant the privilege to the role that owns the pipe. - Load files with
COPY INTOor a pipe. In every load into the table, setMATCH_BY_COLUMN_NAMEandINFER_NESTED_TYPES = TRUE. - To see the columns and fields that schema evolution added, run DESCRIBE TABLE. The
schema evolution recordcolumn showsADD_COLUMNorADD_FIELDfor each column that changed.
For a complete example, see Add a field to a structured OBJECT column.
Choose column types¶
INFER_SCHEMA infers types from the files that it scans, and schema evolution doesn’t change those types later. Before you create
the table, check for DATE or TIMESTAMP_NTZ types inferred from JSON strings, and for numeric types sized from the scanned
values (for example, NUMBER(1, 0) when every scanned value has one digit). Records with values that don’t fit these types fail
to load.
To change a type, generate the column definitions with GENERATE_COLUMN_DESCRIPTION, edit them, and create the table from the edited list.
For JSON files, schema evolution also sizes new columns and fields from the values in each loaded file. To control a column’s type, add the column before you load files that contain it.
Structured columns enforce their types, and VARIANT columns accept any value. Use a VARIANT column for the following data:
- ORC maps, and Parquet maps whose keys aren’t strings or integers. This data can’t be loaded into
MAPcolumns. - JSON objects that use data values, such as customer IDs, as keys. Schema evolution adds a field for each new key, and a load that would add more than 100 fields fails.
- Data that a dynamic table selects, because dynamic tables don’t support structured types.
To keep a column as VARIANT, declare it as VARIANT when you create the table, or add it to the table before you load files that
contain it. ORC UNION columns, and Avro UNION columns that have more than one non-null type, are always added as VARIANT.
Create and evolve Snowflake-managed Iceberg tables¶
To create a Snowflake-managed Iceberg table from your files, complete the steps in
Create a table with structured columns from your files with these changes: set KIND => 'ICEBERG' in the INFER_SCHEMA calls, and in
step 2, use CREATE ICEBERG TABLE ... USING TEMPLATE, as shown in the
INFER_SCHEMA example, and then set
ENABLE_SCHEMA_EVOLUTION to TRUE with ALTER ICEBERG TABLE.
Iceberg tables don’t support semi-structured ARRAY columns, and Iceberg v2 tables (the default) don’t support VARIANT columns.
Loads that would add these columns fail. To use VARIANT columns, create an Iceberg v3 table, or
upgrade the table to Iceberg v3. The upgrade is permanent.
If any column would be VARIANT, INFER_SCHEMA with KIND => 'ICEBERG' fails with
INFER_SCHEMA cannot produce an Iceberg column definition for type 'VARIANT'. To create an Iceberg v3 table that has VARIANT
columns, run INFER_SCHEMA with KIND => 'STANDARD', and create the table from an edited column list, as described in
Choose column types. In the edited list, use precision 6 for time types, and precision 6 or 9
for timestamp types.
Add INFER_ NESTED_ TYPES to an existing load¶
When you add INFER_NESTED_TYPES = TRUE to an existing load, data that’s already loaded doesn’t change. Later loads change in the
following ways:
- New nested columns are added as structured columns. Records whose values don’t fit a structured column’s type fail to load, and
Snowpipe skips the files that contain them. To control a new
column’s type, or to keep it as
VARIANT, add it to the table first. - For Parquet files loaded without
USE_VECTORIZED_SCANNER = TRUE, new rows store maps inVARIANTcolumns as objects ({"1": "a"}) instead ofkey_valuearrays ({"key_value": [{"key": 1, "value": "a"}]}). Queries that readmy_map:key_valuereturnNULLfor new rows.
If a pipe loads the table, pause the pipe and save its definition first, as described in steps 1 to 3 of Recreate a pipe safely. Then complete the following steps:
- Update queries that read
my_map:key_value, if any, so that they handle both layouts. - If the load doesn’t set
MATCH_BY_COLUMN_NAME, set it, and remove anySELECTtransform. Before you do, check that the table’s column names match the column names in the files, because schema evolution adds each unmatched column as a new column. To keep loading file metadata, such asMETADATA$FILENAME, use theINCLUDE_METADATAcopy option. - Set
ENABLE_SCHEMA_EVOLUTION = TRUEon the table. If a role other than the table owner loads the data, grant that role theEVOLVE SCHEMAprivilege on the table. For Snowpipe, grant the privilege to the role that owns the pipe. - Set
INFER_NESTED_TYPES = TRUEin every load into the table. Without it, records that have new fields inside structured columns fail to load. If you paused a pipe, complete steps 4 to 9 of Recreate a pipe safely. When you recreate the pipe, includeINFER_NESTED_TYPES = TRUEand any changes that you made in step 2 of this section.
Move existing data to structured columns¶
If a pipe loads the original table, pause the pipe and save its definition first, as described in steps 1 to 3 of Recreate a pipe safely. Then use one of the following methods:
-
Reload the staged files: If the files are still in the stage, create a table and load the files, as described in Create a table with structured columns from your files. Use the same file format as the original load.
-
Copy the rows: Create a table that has structured columns, as described in steps 2 and 3 of Create a table with structured columns from your files, and use
INSERT ... SELECTwith a cast to the structured type. The cast fails if a value omits a field that the type declares. To storeNULLfor these fields, build each value with OBJECT_CONSTRUCT_KEEP_NULL, and list every field:The new table has no load metadata for these rows, so a later
COPY INTOstatement from the same stage location might load the files again. To load only new files, use theFILESorPATTERNparameter.
If you paused a pipe, complete steps 4 to 9 of Recreate a pipe safely
after the move. When you recreate the pipe, have it load the new table, and include INFER_NESTED_TYPES = TRUE and the changes
from step 2 of Add INFER_NESTED_TYPES to an existing load.
Examples¶
The following examples show how schema evolution changes a table when you load files with COPY INTO.
Add columns and drop NOT NULL constraints¶
The following example creates a table with column definitions derived from a set of Parquet data. With automatic table schema evolution enabled for the table, further data loads from Parquet files with additional name/value pairs automatically add columns to the table:
Note that the mystage stage and my_parquet_format file format referenced in the statement must already exist. A set of files must
already be staged in the cloud storage location referenced in the stage definition.
This example builds on an example in the INFER_SCHEMA topic:
Add a field to a structured OBJECT column¶
The following example creates a table whose address column has a city field, and then loads a file that adds a zip field to
address. Like the previous example, it uses the mystage stage and the my_parquet_format file format:
Troubleshoot schema detection and evolution¶
The following problems might occur when you create tables from staged files or load files with schema evolution:
-
template must be a non-null JSON array:INFER_SCHEMAreturned no rows. Run theINFER_SCHEMAquery by itself to check theLOCATIONand file format. WithINFER_NESTED_TYPES => TRUE, the function also skips Parquet files that contain maps whose keys aren’t strings or integers. For these files, create a table that has aVARIANTcolumn for each of these maps, setENABLE_SCHEMA_EVOLUTION = TRUE, and load the files withMATCH_BY_COLUMN_NAMEandINFER_NESTED_TYPES = TRUE. Schema evolution adds the other columns. -
A new nested column isn’t added, and Snowflake doesn’t return an error: Check that the table has
ENABLE_SCHEMA_EVOLUTION = TRUE, that the statement setsMATCH_BY_COLUMN_NAME, and that the role that loads the data has theEVOLVE SCHEMAorOWNERSHIPprivilege on the table. For Snowpipe, check the role that owns the pipe. Correcting these settings affects only later loads. To get the column’s values from files that were already loaded, reload the files into a new table, as described in Move existing data to structured columns. -
A new nested column is added as
VARIANTor semi-structuredARRAY: The load didn’t setINFER_NESTED_TYPES = TRUE, or the column hasUNIONtypes that are always added as VARIANT. To move the column’s data to a structured column, see Move existing data to structured columns. -
Typed object schema mismatch in conversion: A record doesn’t match a structured column’s type. The error doesn’t identify the field. The following causes are common:- The record has a new field that schema evolution didn’t add. Check that the statement sets
INFER_NESTED_TYPES = TRUEand meets the requirements in Create a table with structured columns from your files. - A value doesn’t fit a field’s type. Create a table with wider types and reload the files. In a Snowflake-managed Iceberg table, you can widen the field instead.
- The record has ORC map data for a
MAPcolumn, or a new field inside aMAPvalue in a JSON file. Use aVARIANTcolumn.
To find the field, run
INFER_SCHEMAwithINFER_NESTED_TYPES => TRUEon the file that failed, and compare the result with the output of DESCRIBE TABLE. - The record has a new field that schema evolution didn’t add. Check that the statement sets
-
Unsupported data type 'VARIANT' for iceberg tablesorUnsupported data type 'ARRAY' for iceberg tables: A load into a Snowflake-managed Iceberg table would add a column type that the table doesn’t support. SetINFER_NESTED_TYPES = TRUE. If the column must beVARIANT, use an Iceberg v3 table. For more information, see Create and evolve Snowflake-managed Iceberg tables. -
infer_nested_types is not supported with copy transform: The statement combinesINFER_NESTED_TYPES, set to any value, with aSELECTtransform. Remove the option, or replace the transform withMATCH_BY_COLUMN_NAME, as described in Add INFER_NESTED_TYPES to an existing load. -
adding too many structured fields: The load would add more than 100 fields, often because JSON objects use data values as keys. Create a table that has aVARIANTcolumn for the data, and then reload the files. To request a higher limit, contact Snowflake Support. -
Schema evolution is incomplete: The files in the statement need more than one schema change, and aCOPYoperation applies one change by default. Run the statement again until it succeeds. Each run applies one more change, and only the run that succeeds loads the files. -
A
MAPcolumn contains a singlekey_valueentry, or the load fails withNumeric value 'key_value' is not recognized: The load sets neitherINFER_NESTED_TYPES = TRUEnorUSE_VECTORIZED_SCANNER = TRUE. SetINFER_NESTED_TYPES = TRUE. If the load uses aSELECTtransform, setUSE_VECTORIZED_SCANNER = TRUEinstead. Rows that were already loaded don’t change. -
Cannot load Parquet MAP field: The file has a map whose values are lists. Set the USE_VECTORIZED_SCANNER file format option toTRUE. -
Invalid Map Key found in parquet fileorUsing a non-primitive Map Key is not supported:MAPcolumns can’t store the map’s keys. Use aVARIANTcolumn: add one before you load the files, or create a table that has one and reload the files. In a Snowflake-managed Iceberg table,VARIANTcolumns require Iceberg v3.INFER_SCHEMAwithKIND => 'ICEBERG'returns the same errors. -
Snowpipe doesn’t load a file again after you fix the problem: With the default
ON_ERROR = SKIP_FILEsetting, Snowpipe skips each file that contains a record that fails to load. To find these files, look for theLoad failedstatus in the output of the COPY_HISTORY function. For files that a pipe skipped before you recreated it, use the COPY_HISTORY view. To load the files, see Reload a file that Snowpipe already processed.