POSEXPLODE_OUTER Function
Description
The POSEXPLODE_OUTER function is used to expand array or map type expressions into multiple rows and adds a position column for each row. Unlike POSEXPLODE, when the input is NULL or an empty array/empty map, POSEXPLODE_OUTER retains the original row and outputs NULL values, whereas POSEXPLODE directly discards that row.
This function can be used directly or in combination with LATERAL VIEW.
Syntax
Parameters
expr: The input array (ARRAY<T>) or map (MAP<K, V>) expression.
Return Results
- For array type input, returns two columns:
pos(INT) andcol(T). - For map type input, returns three columns:
pos(INT),key(K), andvalue(V). - When the input is NULL or an empty array/empty map, outputs one row with all columns being NULL.
Examples
-
Expand an array, including NULL input cases:
-
Comparison with
POSEXPLODE(discards NULL and empty array rows): -
Expand a map type, including NULL input:
Notes
- When the input is NULL,
POSEXPLODE_OUTERretains the original row and outputs NULL (including the pos column), whereasPOSEXPLODEdiscards that row. - When the input is an empty array
array()or empty mapmap(), the behavior is the same as NULL: retains the original row and outputs NULL. POSEXPLODE_OUTERis commonly used in LEFT JOIN semantic scenarios, ensuring that the main table rows are not lost even if the array/map is empty or NULL.- This function can be used directly in SELECT or in combination with
LATERAL VIEW.
