Share via


posexplode_outer

Returns a new row for each element with position in the given array or map. Unlike posexplode, if the array/map is null or empty then the row (null, null) is produced. Uses the default column name pos for position, and col for elements in the array and key and value for elements in the map unless specified otherwise.

Syntax

from pyspark.sql import functions as sf

sf.posexplode_outer(col)

Parameters

Parameter Type Description
col pyspark.sql.Column or column name Target column to work on.

Returns

pyspark.sql.Column: one row per array item or map key value including positions as a separate column.

Examples

Example 1: Using an array column

from pyspark.sql import functions as sf
df = spark.sql('SELECT * FROM VALUES (1,ARRAY(1,2,3,NULL)), (2,ARRAY()), (3,NULL) AS t(i,a)')
df.select('*', sf.posexplode_outer('a')).show()
+---+---------------+----+----+
|  i|              a| pos| col|
+---+---------------+----+----+
|  1|[1, 2, 3, NULL]|   0|   1|
|  1|[1, 2, 3, NULL]|   1|   2|
|  1|[1, 2, 3, NULL]|   2|   3|
|  1|[1, 2, 3, NULL]|   3|NULL|
|  2|             []|NULL|NULL|
|  3|           NULL|NULL|NULL|
+---+---------------+----+----+

Example 2: Using a map column

from pyspark.sql import functions as sf
df = spark.sql('SELECT * FROM VALUES (1,MAP(1,2,3,4,5,NULL)), (2,MAP()), (3,NULL) AS t(i,m)')
df.select('*', sf.posexplode_outer('m')).show(truncate=False)
+---+---------------------------+----+----+-----+
|i  |m                          |pos |key |value|
+---+---------------------------+----+----+-----+
|1  |{1 -> 2, 3 -> 4, 5 -> NULL}|0   |1   |2    |
|1  |{1 -> 2, 3 -> 4, 5 -> NULL}|1   |3   |4    |
|1  |{1 -> 2, 3 -> 4, 5 -> NULL}|2   |5   |NULL |
|2  |{}                         |NULL|NULL|NULL |
|3  |NULL                       |NULL|NULL|NULL |
+---+---------------------------+----+----+-----+