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