通过


inline_outer

将结构数组分解为一个表。 与内联不同,如果数组为 null 或为空,则为每个嵌套列生成 null。

Syntax

from pyspark.sql import functions as sf

sf.inline_outer(col)

参数

参数 类型 Description
col pyspark.sql.Column 或列名 要分解的值的输入列。

退货

pyspark.sql.Column:内联分解结果的生成器表达式。

例子

from pyspark.sql import functions as sf
df = spark.sql('SELECT * FROM VALUES (1,ARRAY(NAMED_STRUCT("a",1,"b",2), NULL, NAMED_STRUCT("a",3,"b",4))), (2,ARRAY()), (3,NULL) AS t(i,s)')
df.printSchema()
root
 |-- i: integer (nullable = false)
 |-- s: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- a: integer (nullable = false)
 |    |    |-- b: integer (nullable = false)
df.select('*', sf.inline_outer('s')).show(truncate=False)
+---+----------------------+----+----+
|i  |s                     |a   |b   |
+---+----------------------+----+----+
|1  |[{1, 2}, NULL, {3, 4}]|1   |2   |
|1  |[{1, 2}, NULL, {3, 4}]|NULL|NULL|
|1  |[{1, 2}, NULL, {3, 4}]|3   |4   |
|2  |[]                    |NULL|NULL|
|3  |NULL                  |NULL|NULL|
+---+----------------------+----+----+