如何在 Presto 中并行执行多个数组 unnest()

How to perform multiple array unnest() in parallel in Presto

我有以下 table 这种格式

create table raw_data (

userid BIGINT,
city  VARCHAR,
campaign ARRAY <
       STRUCT <campaignid BIGINT,
               campaign_start_at TIMESTAMP,
               campaign_ends_at TIMESTAMP,
               parameters ARRAY<
                           STRUCT < goal VARCHAR,
                                    reward VARCHAR
                                  >
               campaignstatus ARRAY
                          STRUCT < seen BOOLEAN ,
                                   seen_at TIMESTAMP
                                   action VARCHAR,
                                   action_at TIMESTAMP
                                  >
                                >
                 >)

我希望最后的结果是这样的:

userid|city|campaignid|campaign_start_at|campaign_ends_at|goal|reward|seen|seen_at|action|action_at

1 | Athens | 234   | 2019-03-19 12:00 |2019-03-19 14:00| 10| 2.7 | yes |2019-03-19 10:23|null|null
1 | Athens | 234   | 2019-03-19 12:00 |2019-03-19 14:00| 10| 2.7 | yes |2019-03-17 10:23|participate|2019-03-19 11:20
2 | Athens | 234   | 2019-03-19 12:00 |2019-03-19 14:00| 10| 2.7 | yes |2019-03-19 10:23|ignore|2019-03-19 10:10
3 | Athens | 234   | 2019-03-19 12:00 |2019-03-19 14:00| 10| 2.7 | null|null|null|null
3 | Athens | 234   | 2019-03-19 12:00 |2019-03-19 14:00| 10| 2.7 | yes |2019-03-19 12:23|blocked|2019-03-19 12:24

换句话说,我想取消嵌套数据并查找用户标识级别的信息。 我尝试使用以下脚本

取消嵌套 table
select * 
FROM raw_data 
LEFT JOIN UNNEST(campaign) as t(campaigns)

但是returns错误:Table hive.default.campaign不存在

我的问题是:

是否可以在 presto 中并行取消嵌套多个数组?

所以基本上我找到了一个解决方案,虽然简单但有效。

为了取消嵌套所有嵌套数组,您需要从外部数组到内部数组。对于这个例子

  • 首先根据userid
  • 取消嵌套campaign数组
  • 其次根据 userid 和 campaignid 取消嵌套 campaignstatus 数组
  • 第三次取消嵌套 parameters 数组。重要说明:parameters 数组可以作为对象(而非数组)进行操作,因为所有数据都是字符串,并且可以使用 json 函数访问。

更具体地说,查询将是这样的:

select 
   a.userid
   ,a.city
   ,a.campaignid 
   ,a.campaign_start_at 
   ,a.campaign_ends_at TIMESTAMP
   ,cs.sseen
   ,cs.seen_at
   ,cs.action
   ,cs.action_at
   ,json_array_get(cast(parameters as json),0) goal
   ,json_array_get(cast(parameters as json),1) reward

from (
       select
       userid
      ,city
      ,campaignid 
      ,campaign_start_at 
      ,campaign_ends_at TIMESTAMP

      from raw_data
      cross join unnest(campaign) as c
   ) a

cross join unnest(campaignstatus) as cs

不过,我想阅读更复杂的解决方案。