Difference between explode and explode_outer

六眼飞鱼酱① 提交于 2021-02-05 11:39:22

问题


What is the difference between explode and explode_outer? The documentation for both functions is the same and also the examples for both functions are identical:

SELECT explode(array(10, 20));
 10
 20

and

SELECT explode_outer(array(10, 20));
 10
 20

The Spark source suggests that there is a difference between the two functions

expression[Explode]("explode"),
expressionGeneratorOuter[Explode]("explode_outer")

but what is the effect of expressionGeneratorOuter compared to expression?


回答1:


explode creates a row for each element in the array or map column by ignoring null or empty values in array whereas explode_outer returns all values in array or map including null or empty.

For example, for the following dataframe-

id | name | likes
_______________________________
1  | Luke | [baseball, soccer]
2  | Lucy | null

explode gives the following output-

id | name | likes
_______________________________
1  | Luke | baseball
1  | Luke | soccer

Whereas explode_outer gives the following output-

id | name | likes
_______________________________
1  | Luke | baseball
1  | Luke | soccer
2  | Lucy | null

SELECT explode(col1) from values (array(10,20)), (null)

returns

+---+
|col|
+---+
| 10|
| 20|
+---+

while

SELECT explode_outer(col1) from values (array(10,20)), (null)

returns

+----+
| col|
+----+
|  10|
|  20|
|null|
+----+


来源:https://stackoverflow.com/questions/64377894/difference-between-explode-and-explode-outer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!