Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

hive

presto

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?

like image 843
John Constantine Avatar asked Oct 25 '18 15:10

John Constantine


People also ask

Can an array have a struct within a struct?

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 []>]).

How does a string array work?

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.

How to select partial values from an array of structs?

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.

What is the main method of string array in Java?

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.


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

like image 59
skadya Avatar answered Sep 22 '22 19:09

skadya


Based on the provided information, you basically need two things:

  1. Upgrade to something more recent -- latest from Maven Central / Github, or a Starburst-supported (and free) distribution from https://www.starburstdata.com/starburst-presto-sql/ (disclaimer: I am from Starburst). 0.175 is really a bit out of date.
  2. Use map_from_entries(one_key_value) (docs: https://trino.io/docs/current/functions/map.html#map_from_entries)
like image 43
Piotr Findeisen Avatar answered Sep 24 '22 19:09

Piotr Findeisen