Athena Unnest 映射键为空的键值对
Athena Unnest Map key value pairs where key is null
假设我在 Athena 中有行并且每行中的一列可能为空,或者包含 json 和键值对,我正在尝试 select 键值对作为行使用 UNNEST
,但让我能够 select,其中 value
或 variable
在以下示例中为空。
我面临的问题是地图的键不能为空。我想知道我是否可以以某种方式将 map(varchar, varchar)
转换为元组 [varchar, varchar]
以便它不是键。
如有任何建议,我们将不胜感激。
备注
- 理想情况下,我需要 select * 而不是手动列出它们 - 这也许是我的问题?如果我愿意手动列出所有列,也许我可以做一个案例陈述?
- 合并使得交叉连接仍然包含没有
ctx
列的行(雅典娜不存在左连接不嵌套,只有 presto >=319)
- 我需要能够在结果集上编写
where variable is not null
样式查询,因为可视化软件会使用它
数据
event_id
ctx
1
{"hello": "world", "bar": "baz"}
2
预期结果
event_id
variable
value
1
hello
world
1
bar
baz
2
null
null
查询
SELECT * from events
CROSS JOIN UNNEST(
COALESCE(
TRY_CAST(
JSON_PARSE(ctx) as MAP(varchar, varchar)
),
map(array[''], array[null])
)
) AS t (variable, value)
您也可以从数组中取消嵌套,因此将 map 转换为两个数组(键和值)并将每个数组与单个空值数组合并应该可以解决问题:
WITH events (event_id, ctx) AS (
VALUES (1, '{"hello": "world", "bar": "baz"}'),
(2, null)
)
SELECT event_id, t.*
FROM (
SELECT event_id,
TRY_CAST(JSON_PARSE(ctx) as MAP(varchar, varchar)) as m
FROM events
)
CROSS JOIN UNNEST(
coalesce(map_keys(m), array [ null ]),
coalesce(map_values(m), array [ null ])
) as t (variable, value)
输出:
event_id
variable
value
1
bar
baz
1
hello
world
2
假设我在 Athena 中有行并且每行中的一列可能为空,或者包含 json 和键值对,我正在尝试 select 键值对作为行使用 UNNEST
,但让我能够 select,其中 value
或 variable
在以下示例中为空。
我面临的问题是地图的键不能为空。我想知道我是否可以以某种方式将 map(varchar, varchar)
转换为元组 [varchar, varchar]
以便它不是键。
如有任何建议,我们将不胜感激。
备注
- 理想情况下,我需要 select * 而不是手动列出它们 - 这也许是我的问题?如果我愿意手动列出所有列,也许我可以做一个案例陈述?
- 合并使得交叉连接仍然包含没有
ctx
列的行(雅典娜不存在左连接不嵌套,只有 presto >=319) - 我需要能够在结果集上编写
where variable is not null
样式查询,因为可视化软件会使用它
数据
event_id | ctx |
---|---|
1 | {"hello": "world", "bar": "baz"} |
2 |
预期结果
event_id | variable | value |
---|---|---|
1 | hello | world |
1 | bar | baz |
2 | null | null |
查询
SELECT * from events
CROSS JOIN UNNEST(
COALESCE(
TRY_CAST(
JSON_PARSE(ctx) as MAP(varchar, varchar)
),
map(array[''], array[null])
)
) AS t (variable, value)
您也可以从数组中取消嵌套,因此将 map 转换为两个数组(键和值)并将每个数组与单个空值数组合并应该可以解决问题:
WITH events (event_id, ctx) AS (
VALUES (1, '{"hello": "world", "bar": "baz"}'),
(2, null)
)
SELECT event_id, t.*
FROM (
SELECT event_id,
TRY_CAST(JSON_PARSE(ctx) as MAP(varchar, varchar)) as m
FROM events
)
CROSS JOIN UNNEST(
coalesce(map_keys(m), array [ null ]),
coalesce(map_values(m), array [ null ])
) as t (variable, value)
输出:
event_id | variable | value |
---|---|---|
1 | bar | baz |
1 | hello | world |
2 |