将 Athena SQL 中的元组数组转换为格式化的 table

Convert an array of tuples in Athena SQL to a formatted table

有没有办法将 Athena SQL 中的以下数据(“元组”数组)转换为以下格式?

注意:数组中的项数没有定义,可以有很多,但它们总是成对的。

发件人:

[1234, 1, 5678, 2, ..., 9012, 3]

收件人:

ID Val
1234 1
5678 2
9012 3

使用提供的数据,您可以尝试下一个方法 - unnest 具有序数的数组,使用序数分组到数组中,然后提取数组的成员:

select try(agg [ 1 ]) id,
    try(agg [ 2 ]) val
from (
        select (a - 1) / 2 grp, -- group determined from ordinality
            array_agg(n) agg -- aggregated array with up to 2 members
        from (select array[1234, 1, 5678, 2, 9012, 3] arr) -- initial data
        cross join unnest(arr) WITH ORDINALITY AS t (n, a)
        group by 1
    )
order by grp

输出:

id val
1234 1
5678 2
9012 3