⌘+k ctrl+k
1.1.3 (stable)
Search Shortcut cmd + k | ctrl + k
SQL to/from JSON

DuckDB 提供了将 SELECT 语句在 SQL 和 JSON 之间序列化和反序列化的函数,以及执行 JSON 序列化语句的功能。

Function Type Description
json_deserialize_sql(json) 标量 将一个或多个json序列化的语句反序列化为等效的SQL字符串。
json_execute_serialized_sql(varchar) 表格 执行json序列化的语句并返回结果行。目前只支持一次执行一个语句。
json_serialize_sql(varchar, skip_empty := boolean, skip_null := boolean, format := boolean) 标量 将一组以分号分隔的(;)选择语句序列化为等效的json序列化语句列表。
PRAGMA json_execute_serialized_sql(varchar) Pragma json_execute_serialized_sql 函数的 Pragma 版本。

json_serialize_sql(varchar) 函数接受三个可选参数,skip_emptyskip_nullformat,这些参数可用于控制序列化语句的输出。

如果你在事务中运行json_execute_serialized_sql(varchar)表函数,序列化的语句将无法看到任何事务本地的更改。这是因为这些语句是在一个单独的查询上下文中执行的。你可以使用PRAGMA json_execute_serialized_sql(varchar) pragma版本来在与pragma相同的查询上下文中执行这些语句,尽管有一个限制,即序列化的JSON必须作为常量字符串提供,也就是说,你不能执行PRAGMA json_execute_serialized_sql(json_serialize_sql(...))

请注意,这些函数不会保留诸如FROM * SELECT ...之类的语法糖,因此通过json_deserialize_sql(json_serialize_sql(...))往返的语句可能与原始语句不完全相同,但在语义上应始终等效并产生相同的输出。

Examples

简单示例:

SELECT json_serialize_sql('SELECT 2');
{"error":false,"statements":[{"node":{"type":"SELECT_NODE","modifiers":[],"cte_map":{"map":[]},"select_list":[{"class":"CONSTANT","type":"VALUE_CONSTANT","alias":"","value":{"type":{"id":"INTEGER","type_info":null},"is_null":false,"value":2}}],"from_table":{"type":"EMPTY","alias":"","sample":null},"where_clause":null,"group_expressions":[],"group_sets":[],"aggregate_handling":"STANDARD_HANDLING","having":null,"sample":null,"qualify":null}}]}

包含多个语句和跳过选项的示例:

SELECT json_serialize_sql('SELECT 1 + 2; SELECT a + b FROM tbl1', skip_empty := true, skip_null := true);
{"error":false,"statements":[{"node":{"type":"SELECT_NODE","select_list":[{"class":"FUNCTION","type":"FUNCTION","function_name":"+","children":[{"class":"CONSTANT","type":"VALUE_CONSTANT","value":{"type":{"id":"INTEGER"},"is_null":false,"value":1}},{"class":"CONSTANT","type":"VALUE_CONSTANT","value":{"type":{"id":"INTEGER"},"is_null":false,"value":2}}],"order_bys":{"type":"ORDER_MODIFIER"},"distinct":false,"is_operator":true,"export_state":false}],"from_table":{"type":"EMPTY"},"aggregate_handling":"STANDARD_HANDLING"}},{"node":{"type":"SELECT_NODE","select_list":[{"class":"FUNCTION","type":"FUNCTION","function_name":"+","children":[{"class":"COLUMN_REF","type":"COLUMN_REF","column_names":["a"]},{"class":"COLUMN_REF","type":"COLUMN_REF","column_names":["b"]}],"order_bys":{"type":"ORDER_MODIFIER"},"distinct":false,"is_operator":true,"export_state":false}],"from_table":{"type":"BASE_TABLE","table_name":"tbl1"},"aggregate_handling":"STANDARD_HANDLING"}}]}

带有语法错误的示例:

SELECT json_serialize_sql('TOTALLY NOT VALID SQL');
{"error":true,"error_type":"parser","error_message":"syntax error at or near \"TOTALLY\"","position":"0","error_subtype":"SYNTAX_ERROR"}

反序列化示例:

SELECT json_deserialize_sql(json_serialize_sql('SELECT 1 + 2'));
SELECT (1 + 2)

带有反序列化和语法糖的示例,这些在转换过程中丢失了:

SELECT json_deserialize_sql(json_serialize_sql('FROM x SELECT 1 + 2'));
SELECT (1 + 2) FROM x

使用 execute 的示例:

SELECT * FROM json_execute_serialized_sql(json_serialize_sql('SELECT 1 + 2'));
3

带有错误的示例:

SELECT * FROM json_execute_serialized_sql(json_serialize_sql('TOTALLY NOT VALID SQL'));
Parser Error: Error parsing json: parser: syntax error at or near "TOTALLY"