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?
Here is the basic example of an Array having a Struct within another Struct such as (Array [Struct<columns, Struct<>]). This is another example of an Array having another Array and Struct within Struct such as (Array [Struct<Struct<>, Array []>]).
The String Array works similarly to other data types of Array. In Array, only a fixed set of elements can be stored. It is an index-based data structure, which starts from the 0 th position. The first element will take place in Index 0, and the 2 nd element will take place in Index 1, and so on.
But if you want to select partial values from the Struct data type, you can do that by using “.” such as address_history.status In the case of Array of Structs, the column can be selected directly and it will result in only one row: As above, there is only one row with multiple values for each Struct key.
The main method {Public static void main [ String [] args]; } in Java is also an String Array. It is an object of the Array. It can be declared by the two methods; by specifying the size or without specifying the size. It can be initialized either at the time of declaration or by populating the values after the declaration.
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
Based on the provided information, you basically need two things:
map_from_entries(one_key_value)
(docs: https://trino.io/docs/current/functions/map.html#map_from_entries)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With