INFER_SCHEMA

functionpartialData loading & unloading

docs.snowflake.com ↗

Table functions

Automatically detects the file metadata schema in a set of staged data files that contain semi-structured data and retrieves the column definitions. The GENERATE_COLUMN_DESCRIPTION function builds on the INFER_SCHEMA function output to simplify the creation of new tables, external tables, or views (using the appropriate CREATE <object> command) based on the column definitions of the staged files. You can execute the CREATE TABLE, CREATE EXTERNAL TABLE, or CREATE ICEBERG TABLE command with the USING TEMPLATE clause to create a new table or external table with the column definitions derived from the INFER_SCHEMA function output. To return structured types (OBJECT, ARRAY, or MAP) for nested data, set the INFER_NESTED_TYPES argument to TRUE. Note This function supports Apache Parquet, Apache Avro, ORC, JSON, and CSV files.

Syntax

supportedpartialnot supportednot scored

INFER_SCHEMA(
  LOCATION => '{ internalStage | externalStage }'
  , FILE_FORMAT => '<file_format_name>'
  , FILES => ( '<file_name>' [ , '<file_name>' ] [ , ... ] )
  , IGNORE_CASE => TRUE | FALSE
  , MAX_FILE_COUNT => <num>
  , MAX_RECORDS_PER_FILE => <num>
  , KIND => '<kind_name>'
  , INFER_NESTED_TYPES => TRUE | FALSE
)

Where

internalStage ::=
    @[<namespace>.]<int_stage_name>[/<path>][/<filename>]
  | @~[/<path>][/<filename>]

Where

externalStage ::=
  @[<namespace>.]<ext_stage_name>[/<path>][/<filename>]

Parameters

NameRequiredDescription
LOCATION => '...'requiredName of the internal or external stage where the files are stored. Optionally include a path to one or more files in the cloud storage location; otherwise, the INFER_SCHEMA function scans files in all subdirectories in the stage: @[namespace.]int_stage_name[/path][/filename] Files are in the specified named internal stage. @[namespace.]ext_stage_name[/path][/filename] Files are in the specified named external stage. @~[/path][/filename] Files are in the stage for the current user. This SQL function supports named stages (internal or external) and user stages only. It does not support table stages. ExpandShow lessSee more
FILES => ( 'file_name' [ , 'file_name' ] [ , ... ] )requiredSpecifies a list of one or more files (separated by commas) in a set of staged files that contain semi-structured data. The files must already have been staged in either the Snowflake internal location or external location specified in the command. If any of the specified files cannot be found, the query will be aborted. The maximum number of files names that can be specified is 1000. Note For external stages only (Amazon S3, Google Cloud Storage, or Microsoft Azure), the file path is set by concatenating the URL in the stage definition and the list of resolved file names. However, Snowflake doesn’t insert a separator implicitly between the path and file names. You must explicitly include a separator (/) either at the end of the URL in the stage definition or at the beginning of each file name specified in this parameter.
FILE_FORMAT => 'file_format_name'requiredName of the file format object that describes the data contained in the staged files. For more information, see CREATE FILE FORMAT.
IGNORE_CASE => TRUE | FALSErequiredSpecifies whether column names detected from stage files are treated as case sensitive. By default, the value is FALSE, which means that Snowflake preserves the case of alphabetic characters when retrieving column names. If you specify the value as TRUE, column names are treated as case-insensitive and all column names are retrieved as uppercase letters. This argument applies only to column names. Field names inside structured types keep the case used in the files.
MAX_FILE_COUNT => numrequiredSpecifies the maximum number of files scanned from stage. This option is recommended for large number of files that have identical schema across files. This option cannot determine which files are scanned. If you want to scan specific files, use the FILES option instead.
MAX_RECORDS_PER_FILE => numrequiredSpecifies the maximum number of records scanned per file. This option only applies to CSV and JSON files. We recommend that you use this option for large files. This option might affect the accuracy of schema detection.
KIND => 'kind_name'requiredSpecifies the kind of file metadata schema that can be scanned from the stage. By default, the value is STANDARD, which means that the file metadata schema that can be scanned from the stage is for Snowflake tables and the output is Snowflake data types. If you specify the value as ICEBERG, the schema is for Apache Iceberg tables and the output is Iceberg data types. Note If you’re inferring Parquet files to create Iceberg tables, we strongly recommend that you set KIND => 'ICEBERG'. Otherwise, the column definitions returned by the function might be incorrect.
INFER_NESTED_TYPES => TRUE | FALSErequiredSpecifies whether nested data is inferred as structured types, such as OBJECT(city TEXT, zip NUMBER(10, 0)), instead of semi-structured types, such as VARIANT. By default, the value is FALSE. This argument affects only the column definitions that the function returns. To have schema evolution add structured columns and fields when you load files, also set the INFER_NESTED_TYPES copy option to TRUE. When the argument is TRUE, note the following: Parquet files that contain maps whose keys aren’t strings or integers are skipped without a warning. With KIND => 'ICEBERG', the function returns an error instead. JSON objects are inferred as OBJECT, not MAP. ORC UNION types, and Avro UNION types that have more than one non-null type, are returned as VARIANT. ORC map data can’t be loaded into MAP columns, so change MAP types inferred from ORC files to VARIANT. When files contain different types for the same field, NUMBER types are widened to fit all values. Other conflicting types are returned as VARIANT, or as TEXT for JSON files. For JSON files, numeric types are sized from the scanned values, so review these types before you create a table. For more information, see Choose column types.

Usage notes

Output

The function returns the following columns: Column Name | Data Type | Description COLUMN_NAME | TEXT | Name of a column in the staged files. TYPE | TEXT | Data type of the column. NULLABLE | BOOLEAN | Specifies whether rows in the column can store NULL instead of a value. Currently, the inferred nullability of a column can apply to one data file but not others in the scanned set. EXPRESSION | TEXT | Expression of the column in the format $1:COLUMN_NAME::TYPE (primarily for external tables). If IGNORE_CASE is specified as TRUE, the expression of the column will be in the format GET_IGNORE_CASE ($1, COLUMN_NAME)::TYPE. FILENAMES | TEXT | Names of the files that contain the column. ORDER_ID | NUMBER | Column order in the staged files.

Known gaps

Parts not fully supported, and recorded divergences from Snowflake.