Description
UNNEST
is a function used to expand arrays or nested data structures, commonly used to convert array-type columns into multiple rows of data for row-level analysis.
Syntax
Parameter Description
Array Expression
: The array or nested data structure (such as multidimensional arrays, structs) that needs to be expanded.
Alias (Column Name)
: Optional, specify an alias and column name for the expanded columns.
LEFT JOIN
: Optional, retain all rows of the left table even if there is no matching data in the right table (UNNEST result).
** Example**
- Basic Array Expansion Expand a one-dimensional array into multiple rows, with each row corresponding to an element in the array. Example:
- Multiple Array Expansion
Supports expanding multiple arrays simultaneously, aligned by rows. If the array lengths are inconsistent, missing values are filled with
NULL
.
Example:
- Nested Array Expansion
Supports expanding multidimensional arrays (recursive expansion) to generate flattened results.
Example:
- Using with JOIN
Can be associated with other tables through
JOIN
orCROSS JOIN
to expand array columns.
Example:
- Handling NULL and Empty Arrays
- If the array is
NULL
, there will be no result after expansion. - If the array is empty (
[]
), there will also be no result after expansion.
Example:
- :Filter Expansion Results
Precautions
- Parameter Type Restrictions
UNNEST
only accepts arrays or nested structures as input, passing non-array types will result in an error.