AWS Athena unnest with left join 不工作

AWS Athena unnest with left join not working

我有一个嵌套的 JSON 结构,我想在其中取消嵌套一个 JSON 子树。填充 A 或 B,并将 event.type 标记为 'A' 或 'B'。这里有两个简化的例子:

{
  "event": {
        "event_type": "A",
        "time": 1599692445083,
        "A" : {
            "name": "item1",
            "revenue": 100
        }
      }
  }
}

{
  "event": {
        "event_type": "B",
        "time": 1599692445083,
        "B" : {
            "items" : [
                {"name": "item2", revenue" : 10},
                {"name": "item3", revenue" : 20},
            ]
        }
      }
  }
}

查询到目前为止有效,但我有一个问题,即 UNNEST 只能与交叉连接一起使用,因此我丢失了所有“A”事件,因为我需要左连接。请注意,我进行了多次聚合,因此仅以更简单的方式计算总和就足够了。

select
    from_unixtime( (floor(event.time/1000) / (60 * 60))  *60*60) as event_hour,
    count(*) filter(where event.event_type = 'A') as A_items,
    count(*) filter(where event.event_type = 'B') as B_items
FROM mydb.event_table
   left join unnest(event.B.items) as t(b)
WHERE
    year=2020 and month=9 and day=18 and hour=1
GROUP BY
    from_unixtime( (floor(event.time/1000) / (60 * 60))  *60*60),

对于 cross join unnest 查询执行,但由于 A 没有项目数组,我没有计算任何条目。对于 left join unnest,我收到一条错误消息(很有趣,它也指出 'left'):

mismatched input 'where' expecting {'join', 'cross', 'inner', 'left', 'right', 'full', 'natural', 'using', 'on', 'tablesample'}

有没有办法在 Athena 的 unnest 中使用 left join?

Athena 基于 Presto .172 LEFT JOINUNNEST 已添加到 Presto 319

Add support for INNER and OUTER joins involving UNNEST.

我假设雅典娜不支持这个,直到他们

  • 升级到较新的 Presto 版本
  • 将该功能反向移植到他们的分支

同时,您可以 运行 Presto 的最新最伟大版本:

此外,正如@GMB 指出的那样,JOIN 需要 ON 子句(可以像 ON true 一样简单)。

对于上述用例,有一种变通方法可以在使用交叉连接进行 UNNESTING 时不丢失条目。它看起来很丑陋,但在 Athena 支持更新的 Presto 功能之前可以正常工作。

而不是

   left join unnest(event.B.items) as t(b)

可以将左联接重写为具有合并的交叉联接:

   cross join unnest(coalesce(event.B.items, array[null])) as t(b)