在 Variant (Array) Snowflake 中加入 Id
Join on Id in Variant (Array) Snowflake
我创建了两个表 t1 和 t2,如下所示:
创建表
CREATE TABLE t1(
id integer AUTOINCREMENT START 1 INCREMENT 1,
name varchar(10) DEFAULT RANDSTR(10, random()),
id_array variant,
)
CREATE TABLE t2(
id integer AUTOINCREMENT START 1 INCREMENT 1,
name varchar(10) DEFAULT RANDSTR(10, random())
)
目标
如果记录的 ID 存在于 t1 变体数组中,我希望将 t2 连接到 t1。
我试过的
我遇到了看起来很完美的 ARRAY_CONTAINS 函数。但是我没有收到以下结果:
SELECT t1.id, t2.name
FROM t1, t2
WHERE ARRAY_CONTAINS(t1.id_array, array_construct(t2.id))
我如何得到这个?
关于如何实现以下结果有什么建议吗?
t1 record:
{id: 1, name: 'A-123', ids_array: '["1", "2"]'}
t2 records:
{id: 1, name: 'test'},
{id: 2, name: 'test2'}
想要的结果
{id: 1, name: 'A-123', joined_name: 'test'},
{id: 1, name: 'A-123', joined_name: 'test2'}
ARRAY_CONTAINS 需要以下参数:变量和数组:
SELECT t1.id, t2.name
FROM t1, t2
WHERE ARRAY_CONTAINS(t1.id_array, array_construct(t2.id))
应该是:
SELECT t1.id, t2.name
FROM t1
JOIN t2
ON ARRAY_CONTAINS(t2.id::variant, t1.id_array)
所以为了调试正在发生的事情,我在下面添加了越来越多的行 SQL,但简而言之,你有 ARRAY_CONTAINS 的参数从头到尾:
WITH t1(id, name, ids_array) AS (
SELECT column1, column2, SPLIT(column3,',') FROM VALUES
(1, 'A-123', '1,2')
), t2(id, name) AS (
SELECT column1, column2 FROM VALUES
(1, 'test'),
(2, 'test2')
)
SELECT t1.id
,t2.name
,t2.id
,t1.ids_array
,ARRAY_CONTAINS('1'::variant, t1.ids_array) as const_1
,ARRAY_CONTAINS('2'::variant, t1.ids_array) as const_2
,t2.id::text::variant as v1
,ARRAY_CONTAINS(v1, t1.ids_array) as it_works
FROM t1, t2
给出:
ID
NAME
ID
IDS_ARRAY
CONST_1
CONST_2
V1
IT_WORKS
1
test
1
[ "1", "2" ]
TRUE
TRUE
"1"
TRUE
1
test2
2
[ "1", "2" ]
TRUE
TRUE
"2"
TRUE
所以现在使用正确的形式:
WITH t1(id, name, ids_array) AS (
SELECT column1, column2, SPLIT(column3,',') FROM VALUES
(1, 'A-123', '1,2')
), t2(id, name) AS (
SELECT column1, column2 FROM VALUES
(1, 'test'),
(2, 'test2')
)
SELECT t1.id
,t2.name
FROM t1, t2
WHERE ARRAY_CONTAINS(t2.id::text::variant, t1.ids_array)
我们得到:
ID
NAME
1
test
1
test2
我创建了两个表 t1 和 t2,如下所示:
创建表
CREATE TABLE t1(
id integer AUTOINCREMENT START 1 INCREMENT 1,
name varchar(10) DEFAULT RANDSTR(10, random()),
id_array variant,
)
CREATE TABLE t2(
id integer AUTOINCREMENT START 1 INCREMENT 1,
name varchar(10) DEFAULT RANDSTR(10, random())
)
目标
如果记录的 ID 存在于 t1 变体数组中,我希望将 t2 连接到 t1。
我试过的
我遇到了看起来很完美的 ARRAY_CONTAINS 函数。但是我没有收到以下结果:
SELECT t1.id, t2.name
FROM t1, t2
WHERE ARRAY_CONTAINS(t1.id_array, array_construct(t2.id))
我如何得到这个?
关于如何实现以下结果有什么建议吗?
t1 record:
{id: 1, name: 'A-123', ids_array: '["1", "2"]'}
t2 records:
{id: 1, name: 'test'},
{id: 2, name: 'test2'}
想要的结果
{id: 1, name: 'A-123', joined_name: 'test'},
{id: 1, name: 'A-123', joined_name: 'test2'}
ARRAY_CONTAINS 需要以下参数:变量和数组:
SELECT t1.id, t2.name
FROM t1, t2
WHERE ARRAY_CONTAINS(t1.id_array, array_construct(t2.id))
应该是:
SELECT t1.id, t2.name
FROM t1
JOIN t2
ON ARRAY_CONTAINS(t2.id::variant, t1.id_array)
所以为了调试正在发生的事情,我在下面添加了越来越多的行 SQL,但简而言之,你有 ARRAY_CONTAINS 的参数从头到尾:
WITH t1(id, name, ids_array) AS (
SELECT column1, column2, SPLIT(column3,',') FROM VALUES
(1, 'A-123', '1,2')
), t2(id, name) AS (
SELECT column1, column2 FROM VALUES
(1, 'test'),
(2, 'test2')
)
SELECT t1.id
,t2.name
,t2.id
,t1.ids_array
,ARRAY_CONTAINS('1'::variant, t1.ids_array) as const_1
,ARRAY_CONTAINS('2'::variant, t1.ids_array) as const_2
,t2.id::text::variant as v1
,ARRAY_CONTAINS(v1, t1.ids_array) as it_works
FROM t1, t2
给出:
ID | NAME | ID | IDS_ARRAY | CONST_1 | CONST_2 | V1 | IT_WORKS |
---|---|---|---|---|---|---|---|
1 | test | 1 | [ "1", "2" ] | TRUE | TRUE | "1" | TRUE |
1 | test2 | 2 | [ "1", "2" ] | TRUE | TRUE | "2" | TRUE |
所以现在使用正确的形式:
WITH t1(id, name, ids_array) AS (
SELECT column1, column2, SPLIT(column3,',') FROM VALUES
(1, 'A-123', '1,2')
), t2(id, name) AS (
SELECT column1, column2 FROM VALUES
(1, 'test'),
(2, 'test2')
)
SELECT t1.id
,t2.name
FROM t1, t2
WHERE ARRAY_CONTAINS(t2.id::text::variant, t1.ids_array)
我们得到:
ID | NAME |
---|---|
1 | test |
1 | test2 |