使用数组和对象从多个值插入雪花 table
insert into snowflake table from multiple values with arrays and objects
我正在尝试通过 SQL 语句 将包含数组和结构的多行插入 到 Snowflake 中。
要将值数组插入到列中,我使用 ARRAY_CONSTUCT
函数并插入 structures/dictionaries/objects 我使用 OBJECT_CONSTRUCT
函数。
例如
insert into "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID", "TS", "TEXT", "DATEONLY", "ARRAY", "OBJ", "BOOL", "DOUBLE", "INT", "DEC_18_9")
values
('id1', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190),
('id2', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190)
;
这导致异常:
SQL compilation error: Invalid expression [ARRAY_CONSTRUCT(0, 1, 2)] in VALUES clause
使用此语法插入一行有效:
insert into "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID", "TS", "TEXT", "DATEONLY", "ARRAY", "OBJ", "BOOL", "DOUBLE", "INT", "DEC_18_9")
select 'id1', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190
;
但是,尚不清楚这是否以及如何用于插入多行。
table定义是:
create or replace temporary table "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID" STRING NOT NULL, "TS" TIMESTAMP NOT NULL, "TEXT" STRING,
"DATEONLY" DATE, "ARRAY" ARRAY, "OBJ" OBJECT, "BOOL" BOOLEAN,
"DOUBLE" DOUBLE, "INT" BIGINT, "DEC_18_9" NUMBER (18, 9)
);
正确的做法是什么?
(我需要将数据溢出到文件中并从那里加载还是有直接的方法来做到这一点?)
我发现唯一提到的是 "answer" here。
澄清一下,我正在将数据插入到一个临时 table 中,我从中合并到另一个 table,因为我找不到通过 SQL 合并数据的方法来自值的语句(即不是来自 table)。
VALUES 不支持 ARRAY_CONSTRUCT 表达式,这就是您收到“无效表达式”错误的原因:
https://docs.snowflake.com/en/sql-reference/constructs/values.html#syntax
Most simple arithmetic expressions and string functions can be
evaluated at compile time, but most other expressions cannot.
您可以使用 SELECT + UNION ALL 而不是 VALUES:
insert into "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID", "TS", "TEXT", "DATEONLY", "ARRAY", "OBJ", "BOOL", "DOUBLE", "INT", "DEC_18_9")
SELECT 'id1', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190
UNION ALL
SELECT 'id2', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190;
通过反复试验,我找到了这个解决方案:
INSERT INTO "MY_DB"."MY_SCHEMA"."MY_TABLE"
SELECT , , , , PARSE_JSON(), PARSE_JSON(), , , ,
from values
('id1', '2020-11-26 14:01:27.868', '19', '2020-11-26', '[0, 1, 2]',
'{"this": "is", "my": "object",
"query": {"field1": "one", "field2": ["field2a", "two"], "field3": [3, 4, 5]}}',
FALSE, 178482300.96318725, 9, 12345619.876543190),
('id2', '2020-11-26 14:01:27.868', '19', '2020-11-26', '[0, 1, 2]',
'{"this": "is", "my": "object",
"query": {"field1": "one", "field2": ["field2a", "two"], "field3": [3, 4, 5]}}',
FALSE, 178482300.96318725, 9, 12345619.876543190)
;
仅供参考,通过简短的测试,Snowflake 将 JSON 中的 NaN
、-inf
和 inf
标识为浮点值,例如{"my_field": NaN}
.
我正在尝试通过 SQL 语句 将包含数组和结构的多行插入 到 Snowflake 中。
要将值数组插入到列中,我使用 ARRAY_CONSTUCT
函数并插入 structures/dictionaries/objects 我使用 OBJECT_CONSTRUCT
函数。
例如
insert into "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID", "TS", "TEXT", "DATEONLY", "ARRAY", "OBJ", "BOOL", "DOUBLE", "INT", "DEC_18_9")
values
('id1', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190),
('id2', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190)
;
这导致异常:
SQL compilation error: Invalid expression [ARRAY_CONSTRUCT(0, 1, 2)] in VALUES clause
使用此语法插入一行有效:
insert into "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID", "TS", "TEXT", "DATEONLY", "ARRAY", "OBJ", "BOOL", "DOUBLE", "INT", "DEC_18_9")
select 'id1', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190
;
但是,尚不清楚这是否以及如何用于插入多行。
table定义是:
create or replace temporary table "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID" STRING NOT NULL, "TS" TIMESTAMP NOT NULL, "TEXT" STRING,
"DATEONLY" DATE, "ARRAY" ARRAY, "OBJ" OBJECT, "BOOL" BOOLEAN,
"DOUBLE" DOUBLE, "INT" BIGINT, "DEC_18_9" NUMBER (18, 9)
);
正确的做法是什么? (我需要将数据溢出到文件中并从那里加载还是有直接的方法来做到这一点?)
我发现唯一提到的是 "answer" here。
澄清一下,我正在将数据插入到一个临时 table 中,我从中合并到另一个 table,因为我找不到通过 SQL 合并数据的方法来自值的语句(即不是来自 table)。
VALUES 不支持 ARRAY_CONSTRUCT 表达式,这就是您收到“无效表达式”错误的原因:
https://docs.snowflake.com/en/sql-reference/constructs/values.html#syntax
Most simple arithmetic expressions and string functions can be evaluated at compile time, but most other expressions cannot.
您可以使用 SELECT + UNION ALL 而不是 VALUES:
insert into "MY_DB"."MY_SCHEMA"."MY_TABLE"
("ID", "TS", "TEXT", "DATEONLY", "ARRAY", "OBJ", "BOOL", "DOUBLE", "INT", "DEC_18_9")
SELECT 'id1', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190
UNION ALL
SELECT 'id2', '2020-11-26 14:01:27.868', '19', '2020-11-26',
ARRAY_CONSTRUCT(0, 1, 2), OBJECT_CONSTRUCT('this', 'is', 'my', 'object', 'query',
OBJECT_CONSTRUCT('field1', 'one', 'field2', ARRAY_CONSTRUCT('field2a', 'two')),
'field3', ARRAY_CONSTRUCT(3, 4, 5)), FALSE, 178482300.96318725, 9, 12345619.876543190;
通过反复试验,我找到了这个解决方案:
INSERT INTO "MY_DB"."MY_SCHEMA"."MY_TABLE"
SELECT , , , , PARSE_JSON(), PARSE_JSON(), , , ,
from values
('id1', '2020-11-26 14:01:27.868', '19', '2020-11-26', '[0, 1, 2]',
'{"this": "is", "my": "object",
"query": {"field1": "one", "field2": ["field2a", "two"], "field3": [3, 4, 5]}}',
FALSE, 178482300.96318725, 9, 12345619.876543190),
('id2', '2020-11-26 14:01:27.868', '19', '2020-11-26', '[0, 1, 2]',
'{"this": "is", "my": "object",
"query": {"field1": "one", "field2": ["field2a", "two"], "field3": [3, 4, 5]}}',
FALSE, 178482300.96318725, 9, 12345619.876543190)
;
仅供参考,通过简短的测试,Snowflake 将 JSON 中的 NaN
、-inf
和 inf
标识为浮点值,例如{"my_field": NaN}
.