posexplode table-valued generator function
Applies to: Databricks SQL
Databricks Runtime
Returns a set of rows by un-nesting expr with numbering of positions.
In Databricks SQL and Databricks Runtime 16.1 and above this function supports named parameter invocation.
Syntax
posexplode(collection)
Arguments
collection: AnARRAYorMAPexpression.
Returns
A set of rows composed of the position and the elements of the array or the keys and values of the map.
The columns produced by posexplode of an array are named pos and col.
The columns for a map are called pos, key and value.
If collection is NULL no rows are produced.
-
Applies to:
Databricks Runtime 12.1 and earlier:
posexplodecan only be placed in theSELECTlist as the root of an expression or following a LATERAL VIEW. When placing the function in theSELECTlist there must be no other generator function in the sameSELECTlist or UNSUPPORTED_GENERATOR.MULTI_GENERATOR is raised. -
Applies to:
Databricks SQL
Databricks Runtime 12.2 LTS and above:
Invocation from the LATERAL VIEW clause or the
SELECTlist is deprecated. Instead, invokeposexplodeas a table_reference.
Examples
Applies to: Databricks Runtime 12.1 and earlier:
> SELECT posexplode(array(10, 20)) AS elem, 'Spark';
0 10 Spark
1 20 Spark
> SELECT posexplode(map(1, 'a', 2, 'b')) AS (num, val), 'Spark';
0 1 a Spark
1 2 b Spark
> SELECT posexplode(array(1, 2)), posexplode(array(3, 4));
Error: UNSUPPORTED_GENERATOR.MULTI_GENERATOR
Applies to: Databricks SQL
Databricks Runtime 12.2 LTS and above:
> SELECT pos, col FROM posexplode(array(10, 20));
0 10
1 20
> SELECT pos, key, value FROM posexplode(map(10, 'a', 20, 'b'));
0 10 a
1 22 b
> SELECT p1.*, p2.* FROM posexplode(array(1, 2)) AS p1, posexplode(array(3, 4)) AS p2;
0 1 0 3
0 1 1 4
1 2 0 3
1 2 1 4
-- Using lateral correlation in Databricks 12.2 and above
> SELECT p1.*, p2.* FROM posexplode(array(1, 2)) AS p1, LATERAL posexplode(array(3 * p1.col, 4 * p1.col)) AS p2;
0 1 0 3
0 1 1 4
1 2 0 6
1 2 1 8