如何在 Clickhouse 中使用数组连接

问题描述

我正在尝试使用 arrayJoin() 拆分 2 个数组

我的桌子:

create table test_array(
    col1 Array(INT),col2 Array(INT),col3 String
)
engine = TinyLog;

然后我插入这些值:

insert into test_array values ([1,2],[11,22],'Text');
insert into test_array values ([5,6],[55,66],'Text');

当我在 col1 中拆分第一个数组时,结果将是这样的:

enter image description here

但我需要的是拆分 col1 和 col2 并将它们添加到选择中。

我试过这个查询,但没有用。

select arrayJoin(col1),arrayJoin(col2),col1,col2,col3 from test_array;

enter image description here

如何编辑查询删除图片突出显示的行

谢谢。

解决方法

arrayJoin 的串行调用产生笛卡尔积,以避免使用 ARRAY JOIN

SELECT
    c1,c2,col1,col2,col3
FROM test_array
ARRAY JOIN
    col1 AS c1,col2 AS c2

/*
┌─c1─┬─c2─┬─col1──┬─col2────┬─col3─┐
│  1 │ 11 │ [1,2] │ [11,22] │ Text │
│  2 │ 22 │ [1,22] │ Text │
│  5 │ 55 │ [5,6] │ [55,66] │ Text │
│  6 │ 66 │ [5,66] │ Text │
└────┴────┴───────┴─────────┴──────┘
*/
,

另一种方式——tuple()

SELECT
    untuple(arrayJoin(arrayZip(col1,col2))),col3
FROM test_array

┌─_ut_1─┬─_ut_2─┬─col3─┐
│     1 │    11 │ Text │
│     2 │    22 │ Text │
│     5 │    55 │ Text │
│     6 │    66 │ Text │
└───────┴───────┴──────┘