【问题标题】:Unnest array of structure in PrestoPresto中的非嵌套结构数组
【发布时间】:2020-04-12 19:06:33
【问题描述】:

我有以下 json 格式的数据

create table events (
id_user bigint,
experiments ARRAY <
    STRUCT <
        id: BIGINT,
        impressed: BOOLEAN,
        variantId: BIGINT
    >
>
)

describe events 返回:

columns               | types
id_user               | bigint
experiments           | array(row(id bigint, impressed boolean, variantid bigint)) 

我想用下面的命令解除数组结构的嵌套

select CAST(ROW(array[experiments]) AS ROW(id BIGINT, impressed boolean, variantid bigint)) as test

from events

并且 presto 返回以下错误: 失败:fromType 和 toType 的大小必须匹配

当我在数组中输入虚拟数据时,命令运行顺利。

什么是问题以及我如何克服它?

【问题讨论】:

    标签: arrays json presto complex-data-types


    【解决方案1】:

    presto 返回如下错误:失败:fromType 和 toType 的大小必须匹配

    这个:

    ROW(array[experiments])
    

    构造一个带有一个字段的ROW,类型为“array of row

    我想取消嵌套数组

    如果你想unnest 数组,你想要这样的东西:

    SELECT *
    FROM events
    LEFT JOIN UNNEST(experiments) AS t(experiment) ON true
    

    (对于旧的 Presto 版本,使用 CROSS JOIN 而不是 LEFT JOIN .. ON true;请注意这会改变语义)

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 2015-08-14
      • 2019-03-07
      • 2018-12-05
      • 2021-06-21
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2020-05-07
      相关资源
      最近更新 更多