What you will be able to do
- Save parsing rules in a named FILE FORMAT and know which options belong to which TYPE
- Use FIELD_OPTIONALLY_ENCLOSED_BY, NULL_IF, EMPTY_FIELD_AS_NULL, TRIM_SPACE and SKIP_HEADER to fix CSV values during the load
- Load Parquet, Avro or ORC either as a single $1 VARIANT or as typed columns via INFER_SCHEMA, USING TEMPLATE and MATCH_BY_COLUMN_NAME
1.Saving parsing rules in a named file format
A file format tells Snowflake how to read the bytes in a staged file: which type it is, how fields are separated and enclosed, and which strings count as NULL. You can write these options inline in each COPY INTO, but a named object made with CREATE FILE FORMAT stores them once. Every load, stage and INFER_SCHEMA call can then reference it by name with FORMAT_NAME = 'my_csv_format', so a fix to the parsing rules reaches every pipeline that uses the format.
TYPE decides which other options are valid. The default is CSV, which covers any delimited text file, not only comma-separated ones. JSON and Parquet can be loaded and unloaded. Avro, ORC and XML can only be loaded. Each type has its own option list, and an option from another type's list does not apply. CREATE OR ALTER FILE FORMAT can change the options and comment of an existing format but never its TYPE.
| TYPE | Options specific to this type | Shared options |
|---|---|---|
| CSV | SKIP_HEADER, PARSE_HEADER, FIELD_DELIMITER, FIELD_OPTIONALLY_ENCLOSED_BY, EMPTY_FIELD_AS_NULL, DATE_FORMAT, ERROR_ON_COLUMN_COUNT_MISMATCH | NULL_IF, TRIM_SPACE |
| JSON | STRIP_OUTER_ARRAY, ALLOW_DUPLICATE, STRIP_NULL_VALUES, DATE_FORMAT | NULL_IF, TRIM_SPACE |
| PARQUET | BINARY_AS_TEXT, USE_LOGICAL_TYPE, USE_VECTORIZED_SCANNER, SNAPPY_COMPRESSION | NULL_IF, TRIM_SPACE |
| XML | STRIP_OUTER_ELEMENT, PRESERVE_SPACE, DISABLE_AUTO_CONVERT | none of the two |
Header handling is the clearest example. SKIP_HEADER and PARSE_HEADER appear only in the CSV list, because only delimited text has a header row. A Parquet file stores its column names in the file itself, so adding either option to a Parquet format does nothing useful.
Checkpoint 1 of 4· Match them up
Each named file format accepts only the options listed for its TYPE. Match each option to the TYPE values that accept it.
Tap a term, then the definition that fits it.
TYPE decides which other options are valid, and an option from another type's list does not apply. That is why SKIP_HEADER or PARSE_HEADER on a Parquet format does nothing useful: Parquet stores its column names inside the file.
“Depending on the format type, you can specify additional format-specific options.”Source: docs.snowflake.com
Sources1
2.CSV options that fix values while parsing
Most CSV problems come from values that look wrong to a typed column: quotes around strings, placeholder text in numeric fields, stray spaces, a header row read as data. Each has a file format option that fixes it as the file is parsed, before anything reaches the table.
| Option | Fixes | Behaviour |
|---|---|---|
| FIELD_OPTIONALLY_ENCLOSED_BY | Strings wrapped in quotes | Sets the enclosing character: NONE (default), single quote or double quote |
| NULL_IF | Placeholders such as 'N/A' or 'NULL' | Listed strings become SQL NULL on load; default is \N |
| EMPTY_FIELD_AS_NULL | Empty fields (,,) | TRUE (default) loads SQL NULL; FALSE tries to cast the empty field to the column type |
| TRIM_SPACE | Leading or trailing spaces around fields | TRUE removes them; default FALSE |
| SKIP_HEADER | Header lines read as data | An integer number of lines skipped at the start of the file |
| DATE_FORMAT | Dates in a non-default layout | A format string, or AUTO |
NULL_IF matches values regardless of data type. If 2 is in the list, every 2 in the file becomes NULL, whether in a text column or a number column. That is convenient for 'N/A' and dangerous for anything that could be a real value. The list can include the empty string, NULL_IF = ('\N', 'NULL', 'NUL', ''). When unloading, Snowflake writes SQL NULL as the first string in the list.
TRIM_SPACE and FIELD_OPTIONALLY_ENCLOSED_BY often need to be set together. Suppose an export writes | "Hello world" |, with spaces outside the quotes. Snowflake then sees the leading space as the start of the field, and the quotes become part of the data. TRIM_SPACE = TRUE removes the outer spaces and FIELD_OPTIONALLY_ENCLOSED_BY = '"' removes the quotes, so the value loads as Hello world. Spaces inside the quotes are kept: " Hello world " loads with its inner spaces.
Checkpoint 2 of 4· Check yourself
A vendor's CSV export writes the text N/A or NULL wherever a value is missing. You want those placeholders to load as SQL NULL without a cleanup step after the load. Which file format option should you add?
NULL_IF takes a list of strings and turns every match into SQL NULL as the file is parsed. EMPTY_FIELD_AS_NULL only covers fields with nothing in them (,,), not placeholder text. TRIM_SPACE and FIELD_OPTIONALLY_ENCLOSED_BY strip spaces and quotes, but they keep the value.
“NULL_IF = ( '<string>' [ , '<string>' ... ] )”Source: docs.snowflake.com
Sources2
3.Parquet, Avro and ORC: one VARIANT or typed columns
Parquet is a compressed, columnar binary format that supports nested data, and Avro and ORC are also binary. Snowflake converts all three into the same VARIANT, OBJECT and ARRAY hierarchy it uses for JSON. They don't have to stay in that form, though, and the main decision for these files is which of two shapes to land them in.
The first option is a single VARIANT. A Parquet record arrives as one column, referenced as $1, and you query it with the same path syntax as JSON. This fits schemas that change often or data you are still exploring. To get typed columns from it, use a transforming COPY or a CTAS that selects paths and casts them, such as $1:o_custkey::number and $1:o_orderdate::date. Loading into a table with one VARCHAR column is not one of these options, because it just stores the records as text.
The second option is a typed table built from the file's own schema. INFER_SCHEMA reads staged Parquet, Avro, ORC, JSON or CSV files and returns each column's name, type, nullability and the $1:...::TYPE expression that extracts it. CREATE TABLE ... USING TEMPLATE takes that output, aggregated with ARRAY_AGG(OBJECT_CONSTRUCT(*)), and creates the columns. A COPY INTO with MATCH_BY_COLUMN_NAME then loads each file column into the table column with the same name, whatever order the columns are in. This works best for stable, wide files that BI tools will query directly.
CREATE TABLE mytable USING TEMPLATE ( SELECT ARRAY_AGG(OBJECT_CONSTRUCT(*)) FROM TABLE( INFER_SCHEMA( LOCATION=>'@mystage/json/', FILE_FORMAT=>'my_json_format' ) ));COPY INTO mytable FROM @mystage/csv/
FILE_FORMAT = (
FORMAT_NAME= 'my_csv_format'
)
MATCH_BY_COLUMN_NAME=CASE_INSENSITIVE;Mixed-case column names in the files call for CASE_INSENSITIVE. Parquet columns such as CustNum then match a table column created as custnum or CUSTNUM. INFER_SCHEMA also accepts IGNORE_CASE => TRUE, which returns every column name in uppercase. The aggregated template has a size limit: for very wide files, select only COLUMN_NAME, TYPE and NULLABLE in place of *.
Checkpoint 3 of 4· Check yourself
Your team runs a plain COPY INTO to load Parquet order files into a table whose only column is a VARIANT. Each file's schema includes o_custkey and o_orderdate. Analysts now need o_orderdate as a DATE column. Which statement is correct?
A plain load like this puts each Parquet record into one VARIANT column, $1. You query it with JSON-style paths and cast those paths to get typed columns. Typed columns appear only if you build the table from INFER_SCHEMA and load with MATCH_BY_COLUMN_NAME. PARSE_HEADER is a CSV-only option.
“Depending on your loading use case, Snowflake either reads Parquet data into a single VARIANT column or directly into table columns”Source: docs.snowflake.com
Checkpoint 4 of 4· Exam question
Parquet files with columns named in mixed case are staged in @lake_stage, and no target table exists yet. The analyst wants the table to be built from the Parquet schema and then loaded by column name. Select TWO steps that accomplish this.(Select 2)
Correct answers: A, B — Run CREATE TABLE sales USING TEMPLATE (SELECT ARRAY_AGG(OBJECT_CONSTRUCT(*)) FROM TABLE(INFER_SCHEMA(LOCATION=>'@lake_stage', FILE_FORMAT=>'pq_fmt'))); Run COPY INTO sales FROM @lake_stage FILE_FORMAT=(FORMAT_NAME='pq_fmt') MATCH_BY_COLUMN_NAME=CASE_INSENSITIVE to map file columns to table columns
- A. Correct. INFER_SCHEMA reads the Parquet metadata and USING TEMPLATE creates a table with matching column names and types. This avoids hand-writing the DDL.
- B. Correct. MATCH_BY_COLUMN_NAME maps each Parquet column to the table column of the same name, ignoring case. Column order in the files then no longer matters.
- C. SKIP_HEADER is a CSV file format option, and Parquet files carry their schema in metadata rather than in a header row. Supplying it to a Parquet format is not a valid way to map columns.
- D. Each Parquet record is loaded as an object, which cannot be placed in a VARCHAR column without a transformation. This approach also discards the per-column typing the analyst wants.
- E. PARSE_HEADER belongs to the CSV file format and tells Snowflake to read column names from a header line. Parquet already stores column names in its metadata, so the option does not apply.
Sources
(7)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Creates a named file format that describes a set of staged data to access or load into Snowflake tables.”
↩︎ Saving parsing rules in a named file format“Supported alterations include changes to the formatTypeOptions and COMMENT properties.”
↩︎ Saving parsing rules in a named file format“Although the name (CSV) suggests comma-separated values, you can use any valid character as a field separator.”
↩︎ Exam trap 1“Depending on the format type, you can specify additional format-specific options.”
↩︎ Checkpoint“NULL_IF = ( '<string>' [ , '<string>' ... ] )”
↩︎ Checkpoint - 2.
“When loading data, Snowflake replaces these values in the data load source with SQL NULL.”
↩︎ CSV options that fix values while parsing“which are represented by two successive delimiters (For example, ,,)”
↩︎ CSV options that fix values while parsing“you can remove the surrounding spaces using this option and the quote character using the FIELD_OPTIONALLY_ENCLOSED_BY option”
↩︎ CSV options that fix values while parsing“Note that Snowflake converts all instances of the value to NULL, regardless of the data type.”
↩︎ Exam trap 2“If set to FALSE, Snowflake attempts to cast an empty field to the corresponding column type.”
↩︎ Prediction - 3.
“Depending on your loading use case, Snowflake either reads Parquet data into a single VARIANT column or directly into table columns”
↩︎ Parquet, Avro and ORC: one VARIANT or typed columns - 4.
“the data is converted to an internal data format that uses Snowflake VARIANT, ARRAY, and OBJECT data types.”
↩︎ Parquet, Avro and ORC: one VARIANT or typed columns - 5.
“The USING TEMPLATE clause accepts an expression that calls the INFER_SCHEMA SQL function to detect the column definitions in the files.”
↩︎ Parquet, Avro and ORC: one VARIANT or typed columns“After the table is created, you can then use a COPY statement with the MATCH_BY_COLUMN_NAME option to load files directly into the structured table.”
↩︎ Parquet, Avro and ORC: one VARIANT or typed columns - 6.
“In the returned output, all column names are retrieved as uppercase letters.”
↩︎ Parquet, Avro and ORC: one VARIANT or typed columns
Also cited
“Note that all Parquet data is stored in a single column ($1)”
↩︎ Exam trap 3