The posexplode table function expands the <array> column into multiple rows and adds a column indicating the position, returning a STRUCT type. It should be used together with Lateral View and supports multiple Lateral Views. The main difference between posexplode and posexplode_outer is how they handle null values.
POSEXPLODE(<array>)
<array> Array type, NULL is not supported.<array>.<array> is NULL or an empty array (number of elements is 0), 0 rows are returned.<array> cannot be NULL or other types, otherwise an error will be reported.create table example( k1 int ) properties( "replication_num" = "1" ); insert into example values(1);
select * from (select 1 as k1) t1 lateral view posexplode([1, 2, null, 4, 5]) t2 as c;
+------+-----------------------+ | k1 | c | +------+-----------------------+ | 1 | {"pos":0, "col":1} | | 1 | {"pos":1, "col":2} | | 1 | {"pos":2, "col":null} | | 1 | {"pos":3, "col":4} | | 1 | {"pos":4, "col":5} | +------+-----------------------+
select * from (select 1 as k1) t1 lateral view posexplode([1, 2, null, 4, 5]) t2 as pos, value;
+------+------+-------+ | k1 | pos | value | +------+------+-------+ | 1 | 0 | 1 | | 1 | 1 | 2 | | 1 | 2 | NULL | | 1 | 3 | 4 | | 1 | 4 | 5 | +------+------+-------+
select * from (select 1 as k1) t1 lateral view posexplode([]) t2 as c;
Empty set (0.03 sec)
select * from (select 1 as k1) t1 lateral view posexplode(NULL) t2 as c;
ERROR 1105 (HY000): errCode = 2, detailMessage = only support array type for posexplode function but got NULL
select * from (select 1 as k1) t1 lateral view posexplode('abc') t2 as c;
ERROR 1105 (HY000): errCode = 2, detailMessage = only support array type for posexplode function but got VARCHAR(3)