FLATTEN

functionsupportedCore SQL functions

docs.snowflake.com ↗

Table functionsSemi-structured and structured data functions

Flattens (explodes) compound values into multiple rows. FLATTEN is a table function that takes a VARIANT, OBJECT, or ARRAY column and produces a lateral view — an inline view that contains correlations to other tables that precede it in the FROM clause. FLATTEN can be used to convert semi-structured data to a relational representation.

Syntax

supportedpartialnot supportednot scored

FLATTEN( INPUT => <expr> [ , PATH => <constant_expr> ]
                         [ , OUTER => TRUE | FALSE ]
                         [ , RECURSIVE => TRUE | FALSE ]
                         [ , MODE => 'OBJECT' | 'ARRAY' | 'BOTH' ] )

Parameters

NameRequiredDescription
INPUT => exprrequiredThe expression that will be flattened into rows. The expression must be of data type VARIANT, OBJECT, or ARRAY.
PATH => constant_exproptionalThe path to the element within a VARIANT data structure that needs to be flattened. Can be a zero-length string (that is, an empty path) if the outermost element is to be flattened. Default: Zero-length string (empty path)
OUTER => TRUE | FALSEoptionalIf FALSE, any input rows that can’t be expanded, either because they can’t be accessed in the path or because they have zero fields or entries, are completely omitted from the output. If TRUE, exactly one row is generated for zero-row expansions (with NULL in the KEY, INDEX, and VALUE columns). Default: FALSE Note A zero-row expansion of an empty compound displays NULL in the THIS output column, distinguishing it from an attempt to expand a non-existing or wrong kind of compound.
RECURSIVE => TRUE | FALSEoptionalIf FALSE, only the element referenced by PATH is expanded. If TRUE, the expansion is performed for all sub-elements recursively. Default: FALSE
MODE => 'OBJECT' | 'ARRAY' | 'BOTH'optionalSpecifies whether only objects, arrays, or both should be flattened. Default: BOTH

Usage notes

Output

The returned rows consist of a fixed set of columns: SEQ: A unique sequence number associated with the input record; the sequence is not guaranteed to be gap-free or ordered in any particular way. KEY: For maps or objects, this column contains the key to the exploded value. PATH: The path to the element within a data structure that needs to be flattened. INDEX: The index of the element, if it is an array; otherwise NULL. VALUE: The value of the element of the flattened array/object. THIS: The element being flattened (useful in recursive flattening). Note The columns of the original (correlated) table that was used as the source of data for FLATTEN are also accessible. If a single row from the original table resulted in multiple rows in the flattened view, the values in this input row are replicated to match the number of rows produced by FLATTEN.