Athena/Presto - UNNEST MAP to columns

狂风中的少年 提交于 2020-12-26 12:26:28

问题


Assume i have a table like this,

table: qa_list

    id   |  question_id |  question  | answer    |  
---------+--------------+------------+-------------
    1    |  100         |  question1 | answer    |  
    2    |  101         |  question2 | answer    |  
    3    |  102         |  question3 | answer    |  
    4    |  ... 
    ...  |  ...

and a query that gives below result (since I couldn't find a direct way to transpose the table),

table: qa_map

   id   |  qa_map
--------+---------
    1   | {question1=answer,question2=answer,question3=answer, ....}

Where qa_map is the result of a map_agg of arbitrary number of questions and answers.

Is there a way to UNNEST qa_map to an arbitrary number of columns as shown below?

    id   |  Question_1 |  Answer_1 |  Question_2 |  Answer_2 |  Question_3 | ....
---------+-------------+-----------+-------------+-----------+-------------+
    1    |  question   | answer    |  question   | answer    |  question   | ....

AWS Athena/Presto-0.172


回答1:


No, there is no way to write a query that results in different number of columns depending on the data. The columns must be known before query execution starts. The map you have is as close as you are going to get.

If you include your motivation for wanting to do this there may be other ways we can help you achieve your end goal.



来源:https://stackoverflow.com/questions/62876977/athena-presto-unnest-map-to-columns

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