Функция map_keys (Карта) возвращает неупорядоченный массив, содержащий ключи входной карты.
Пример, см. Комментарии в коде:
with mydata as (
select 1 id, map('key11','val11','key12','val12','key13','val13') as mymap
union all
select 2 id, map('key21','val21','key22','val22','key13','val13') as mymap --Key13 also exist in first row
)
select id, map_keys(d.mymap) keys
from mydata d
;
Результат:
id keys
1 ["key11","key12","key13"]
2 ["key21","key22","key13"]
Если вам нужен список уникальных ключей из всех строк, разбить массив и собрать снова, используя collect_set, он вернет массив различных ключей:
with mydata as (
select 1 id, map('key11','val11','key12','val12','key13','val13') as mymap
union all
select 2 id, map('key21','val21','key22','val22','key13','val13') as mymap --Key13 also exist in first row
)
select --id,
collect_set(key) as keys
from mydata d
lateral view outer explode(map_keys(d.mymap)) e as key
--group by id --without id in groupby you get the distinct list of keys in all rows
--with id in groupby you get list of map keys for each row
;
Результат:
["key11","key12","key13","key21","key22"]