LATERAL VIEW
The LATERAL VIEW clause is used in conjunction with generator functions (such as EXPLODE, POSEXPLODE, etc.) to generate a virtual table containing one or more rows. This clause allows users to operate on input arrays or maps and spread the results into separate rows.
Syntax
Parameter Description
- OUTER: Optional parameter. If the
OUTERkeyword is specified, a Null value will be returned when the input array or map is empty or Null. - generator_function: Generator function, such as EXPLODE, POSEXPLODE, etc.
- alias: Optional parameter. Alias for the generator_function.
- column_identifier: List of column aliases for the rows output by the
generator_function. The number of column identifiers must match the number of columns returned by the generator function.
Example
Prepare Data
Example 1: Using the EXPLODE Function
Assume we have a table named employees, which contains a field named skills of array type, and we want to split each employee's skills into individual rows.
Example 2: Using the POSEXPLODE Function
The POSEXPLODE function is similar to EXPLODE, but it also returns the position index of the elements in the array. Using the same employees table and inserted data, we can use POSEXPLODE:
Precautions
- When using the
LATERAL VIEWclause, make sure to correctly specify the generator function, alias, and column identifiers.
