SUPER 配置 - Amazon Redshift

SUPER 配置

当您使用 Amazon Redshift SUPER 数据类型和 PartiQL 时,请注意 SUPER 配置的以下注意事项。

宽松和严格的 SUPER 模式

当您查询 SUPER 数据时,路径表达式可能不匹配实际的 SUPER 数据结构。当您尝试访问对象或数组元素的不存在成员时,如果您的查询在默认宽松模式下运行,Amazon Redshift 将返回 NULL 值。如果您在严格模式下运行查询,Amazon Redshift 将返回错误。可以将以下会话参数设置为打开或关闭宽松模式。

以下示例使用会话参数启用宽松模式。

SET navigate_super_null_on_error=ON; --default lax mode for navigation SET cast_super_null_on_error=ON; --default lax mode for casting SET parse_super_null_on_error=OFF; --default strict mode for ingestion

访问具有大写和混合大小写字段名称或属性的 JSON 字段

当 JSON 属性名称采用大写或混合大小写时,您必须能够以区分大小写的方式处理 SUPER 类型结构。为此,您可以将 enable_case_sensitive_identifier 配置为 TRUE,并将大写和混合大小写的属性名称用双引号括起来。您也可以将 enable_case_sensitive_super_attribute 配置为 TRUE。在这种情况下,您可以在查询中使用大写和混合大小写的属性名称,而不必用双引号将其括起来。

以下示例说明如何设置 enable_case_sensitive_identifier 来查询数据。

SET enable_case_sensitive_identifier to TRUE; -- Accessing JSON attribute names with uppercase and mixedcase names SELECT json_table.data."ITEMS"."Name", json_table.data."price" FROM (SELECT json_parse('{"ITEMS":{"Name":"TV"}, "price": 345}') AS data) AS json_table; Name | price ------+------- "TV" | 345 (1 row) RESET enable_case_sensitive_identifier; -- After resetting the above configuration, the following query accessing JSON attribute names with uppercase and mixedcase names should return null (if in lax mode). SELECT json_table.data."ITEMS"."Name", json_table.data."price" FROM (SELECT json_parse('{"ITEMS":{"Name":"TV"}, "price": 345}') AS data) AS json_table; name | price ------+------- | 345 (1 row)

以下示例说明如何设置 enable_case_sensitive_super_attribute 来查询数据。

SET enable_case_sensitive_super_attribute to TRUE; -- Accessing JSON attribute names with uppercase and mixedcase names SELECT json_table.data.ITEMS.Name, json_table.data.price FROM (SELECT json_parse('{"ITEMS":{"Name":"TV"}, "price": 345}') AS data) AS json_table; name | price -----+------- "TV" | 345 (1 row) RESET enable_case_sensitive_super_attribute; -- After resetting enable_case_sensitive_super_attribute, the query now returns NULL for ITEMS.Name (if in lax mode). SELECT json_table.data.ITEMS.Name, json_table.data.price FROM (SELECT json_parse('{"ITEMS":{"Name":"TV"}, "price": 345}') AS data) AS json_table; name | price -----+------- | 345 (1 row)

解析 SUPER 的选项

当您使用 JSON_PARSE 函数将 JSON 字符串解析为 SUPER 值时,某些限制适用:

  • 相同的属性名称不能出现在同一个对象中,但可以出现在嵌套对象中。json_parse_dedup_attributes 配置选项允许 JSON_PARSE 仅保留最后一次出现的重复属性,而不是返回错误。

  • 字符串值不能超过 65535 个字节的系统最大 varchar 大小。json_parse_truncate_strings 配置选项允许 JSON_PARSE() 自动截断超过此限制的字符串,而不会返回错误。此行为仅影响字符串值,而不影响属性名称。

有关 JSON_PARSE 函数的更多信息,请参阅 JSON_PARSE 函数

以下示例显示如何将 json_parse_dedup_attributes 配置选项设置为默认行为,即针对重复属性返回错误。

SET json_parse_dedup_attributes=OFF; --default behavior of returning error instead of de-duplicating attributes

下面的示例显示如何设置 json_parse_truncate_strings 配置选项以实现默认行为,即针对长度超过此限制的字符串返回错误。

SET json_parse_truncate_strings=OFF; --default behavior of returning error instead of truncating strings