【问题标题】:Presto Query of array of structs returns individual sttruct elements as columns instead of rows of structs结构数组的 Presto 查询返回单个结构元素作为列而不是结构行
【发布时间】:2020-12-04 20:50:57
【问题描述】:

这里是示例数据,并带有实际结果和期望结果的 presto 查询。

WITH
dataset AS (
  SELECT 
      ARRAY[
        CAST(ROW('Sally', 'engineering') AS ROW(name VARCHAR, department VARCHAR)),
        CAST(ROW('John', 'finance') AS ROW(name VARCHAR, department VARCHAR))
      ] AS users
)
select t.*
from dataset
cross join unnest(users) as t

输出返回 2 列: 姓名和部门以及 2 行。

所需的输出: 1 列 struct 类型和 2 行。

如何在 presto 中获得所需的结果?有可能吗?

提前致谢,

【问题讨论】:

    标签: presto


    【解决方案1】:

    您观察到的行为是 SQL 标准行为。

    你可以得到想要的输出

    SELECT ROW(t.name, t. department)
    

    或者,通过防止unnest 使用transform()“解包”行:

    CROSS JOIN UNNEST(transform(users, u -> ROW(u)) as t
    

    【讨论】:

    • 感谢 Piotr 的快速响应。 unnest 转换解决方案很整洁。欣赏它。
    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2023-03-27
    • 2021-09-20
    • 2018-12-05
    相关资源
    最近更新 更多