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

John Constantine picture John Constantine · Oct 25, 2018 · Viewed 8.2k times · Source

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?

Answer

Piotr Findeisen picture Piotr Findeisen · Oct 29, 2018

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://prestosql.io/docs/current/functions/map.html#map_from_entries)