if you have a table with one or more column with array datatype and if you want it to expand into multiple rows, you can use lateral view explode function.
Let’s consider we have following table, where one employee has multiple phone numbers which are stores as part of array (list).
emp_name | phone_numbers |
user1 | [“546-487-3384″,”383-767-2238”] |
user2 | [“373-384-1192″,”374-282-1289″,”332-453-5566”] |
However as a output if we want to convert this Array (list) into multiple rows, we can use lateral view explode function a mentioned below
select emp_name, phone_number from temp.test_laterla_view_explode lateral view explode(phone_numbers) p as phone_number
This will generate the output as mentioned below
emp_name | phone_number |
user2 | 373-384-1192 |
user2 | 374-282-1289 |
user2 | 332-453-5566 |
user1 | 546-487-3384 |
user1 | 383-767-2238 |
LATERAL VIEW is often used with explode, but explode is just one UDTF of many, a full list is available in the