Difference between explode and explode_outer

Viewed 8704

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 Answers

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|
+----+
Related