Presto: cast array<struct<key:string,value:array<string>>> into map<string,array<string>>

Viewed 13035

I have a table like

name            string                                      
address         string                                      
timezone        string                                      
one_key_value   array<struct<key:string,value:array<string>>                    
two_key_value   array<struct<key:string,value:array<string>>

and want to convert it to

name            string                                      
address         string                                      
timezone        string                                      
one_key_value   map<string,array<string>>                       
two_key_value   map<string,array<string>>

using presto. There is lateral view inline but it doesn't really work in presto. How can I do this?

2 Answers

I have not tested thought, but below expression should help:

map( transform(one_key_value, e -> e.key), transform(one_key_value, e -> e.value))
map( transform(two_key_value, e -> e.key), transform(two_key_value, e -> e.value))

AS per Presto 0.175 docs:

map(array, array) → map Returns a map created using the given key/value arrays.

SELECT map(ARRAY[1,3], ARRAY[2,4]); -- {1 -> 2, 3 -> 4}

We can use array transform function to build the array of keys and values from input field ( array<struct<key:string,value:array<string>>)

transform(array, function) → ARRAY Returns an array that applies function to each element of array

Related