TableValuedFunction.inline
Transforma um array de structs em uma tabela.
Essa função recebe uma coluna de entrada contendo um array de structs e retorna uma nova coluna onde cada struct no array é dividido em uma linha separada.
Sintaxe
Python
spark.tvf.inline(input)
Parâmetros
Parâmetro | Tipo | Descrição |
|---|---|---|
|
| Coluna de entrada com os valores a serem explodidos. |
Devoluções
pyspark.sql.DataFrameUm DataFrame com linhas de estrutura expandidas.
Exemplos
Exemplo 1 : Usando `inline` com um único array de structs
Python
import pyspark.sql.functions as sf
spark.tvf.inline(sf.array(
sf.named_struct(sf.lit("a"), sf.lit(1), sf.lit("b"), sf.lit(2)),
sf.named_struct(sf.lit("a"), sf.lit(3), sf.lit("b"), sf.lit(4))
)).show()
Output
+---+---+
| a| b|
+---+---+
| 1| 2|
| 3| 4|
+---+---+
Exemplo 2 : Usando `inline` com uma coluna de array de structs vazia
Python
import pyspark.sql.functions as sf
spark.tvf.inline(sf.array().astype("array<struct<a:int,b:int>>")).show()
Output
+---+---+
| a| b|
+---+---+
+---+---+
Exemplo 3 : Usando `inline` com uma coluna de array de structs contendo valores nulos.
Python
import pyspark.sql.functions as sf
spark.tvf.inline(sf.array(
sf.named_struct(sf.lit("a"), sf.lit(1), sf.lit("b"), sf.lit(2)),
sf.lit(None),
sf.named_struct(sf.lit("a"), sf.lit(3), sf.lit("b"), sf.lit(4))
)).show()
Output
+----+----+
| a| b|
+----+----+
| 1| 2|
|NULL|NULL|
| 3| 4|
+----+----+