How to cast varchar to MAP(VARCHAR,VARCHAR) in presto

与世无争的帅哥 提交于 2020-01-04 05:36:52

问题


I have table in presto, one column named ("mappings") have key-value pair as string

select mappings from hello;

Ex: {"foo": "baar", "foo1": "bar1" }

I want to cast "mappings" column into a MAP

like select CAST("mappings" as MAP) from hello;

This will throw error in presto. How can we translate this to map?


回答1:


There is no canonical string representation for a MAP in Presto, so so there's no way to cast it directly to MAP(VARCHAR, VARCHAR). But, if your string contains a JSON map, you can use the json_parse function to convert the string into a value of JSON type and convert that to a SQL MAP via a cast.

Example:

WITH
data(c) AS (
    VALUES '{"foo": "baar", "foo1": "bar1"}'
),
parsed AS (
    SELECT cast(json_parse(c) as map(varchar, varchar)) AS m
    FROM data
)
SELECT m['foo'], m['foo1']
FROM parsed

produces:

 _col0 | _col1
-------+-------
 baar  | bar1



回答2:


select cast( json_parse(mappings) as MAP(VARCHAR,VARCHAR)) from hello1;



来源:https://stackoverflow.com/questions/55279784/how-to-cast-varchar-to-mapvarchar-varchar-in-presto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!