From 21957eb1e21fbce55792f47b7e240199139859ad Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Tue, 2 Jul 2024 12:42:43 -0400 Subject: [PATCH v1] Document impact of datestyle on jsonpath string() Add tests demonstrating the output of the jsonpath `string()` method for the different date and time data types, and how the `datestyle` GUC determines that output. Note this relationship in the documentation for the `string()` method, as well. --- doc/src/sgml/func.sgml | 4 +- src/test/regress/expected/jsonb_jsonpath.out | 88 ++++++++++++++++++++ src/test/regress/sql/jsonb_jsonpath.sql | 17 ++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index f1f22a1960..0db4dd3f25 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -17965,7 +17965,9 @@ ERROR: jsonpath member accessor can only be applied to an object string - String value converted from a JSON boolean, number, string, or datetime + String value converted from a JSON boolean, number, string, or + datetime. Note that the string output of datetimes is determined by + the parameter. jsonb_path_query_array('[1.23, "xyz", false]', '$[*].string()') diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out index a6112e86fa..d224c5cc0f 100644 --- a/src/test/regress/expected/jsonb_jsonpath.out +++ b/src/test/regress/expected/jsonb_jsonpath.out @@ -2607,6 +2607,94 @@ select jsonb_path_query_array('[1.23, "yes", false]', '$[*].string().type()'); ["string", "string", "string"] (1 row) +select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); +ERROR: cannot convert value from timestamp to timestamptz without time zone usage +HINT: Use *_tz() function for time zone support. +select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work + jsonb_path_query_tz +-------------------------------- + "Tue Aug 15 12:34:56 2023 PDT" +(1 row) + +select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()'); + jsonb_path_query +-------------------------------- + "Tue Aug 15 00:04:56 2023 PDT" +(1 row) + +select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()'); + jsonb_path_query +---------------------------- + "Tue Aug 15 12:34:56 2023" +(1 row) + +select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()'); + jsonb_path_query +------------------ + "12:34:56+05:30" +(1 row) + +select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work + jsonb_path_query_tz +--------------------- + "12:34:56-07" +(1 row) + +select jsonb_path_query('"12:34:56"', '$.time().string()'); + jsonb_path_query +------------------ + "12:34:56" +(1 row) + +select jsonb_path_query('"2023-08-15"', '$.date().string()'); + jsonb_path_query +------------------ + "08-15-2023" +(1 row) + +set datestyle = 'ISO'; +select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work + jsonb_path_query_tz +-------------------------- + "2023-08-15 12:34:56-07" +(1 row) + +select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()'); + jsonb_path_query +-------------------------- + "2023-08-15 00:04:56-07" +(1 row) + +select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()'); + jsonb_path_query +----------------------- + "2023-08-15 12:34:56" +(1 row) + +select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()'); + jsonb_path_query +------------------ + "12:34:56+05:30" +(1 row) + +select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work + jsonb_path_query_tz +--------------------- + "12:34:56-07" +(1 row) + +select jsonb_path_query('"12:34:56"', '$.time().string()'); + jsonb_path_query +------------------ + "12:34:56" +(1 row) + +select jsonb_path_query('"2023-08-15"', '$.date().string()'); + jsonb_path_query +------------------ + "2023-08-15" +(1 row) + -- Test .time() select jsonb_path_query('null', '$.time()'); ERROR: jsonpath item method .time() can only be applied to a string diff --git a/src/test/regress/sql/jsonb_jsonpath.sql b/src/test/regress/sql/jsonb_jsonpath.sql index 5e14f7759b..a6e6a0912d 100644 --- a/src/test/regress/sql/jsonb_jsonpath.sql +++ b/src/test/regress/sql/jsonb_jsonpath.sql @@ -591,6 +591,23 @@ select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp().string()') select jsonb_path_query_tz('"2023-08-15 12:34:56 +5:30"', '$.timestamp().string()'); -- should work select jsonb_path_query_array('[1.23, "yes", false]', '$[*].string()'); select jsonb_path_query_array('[1.23, "yes", false]', '$[*].string().type()'); +select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); +select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work +select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()'); +select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()'); +select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()'); +select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work +select jsonb_path_query('"12:34:56"', '$.time().string()'); +select jsonb_path_query('"2023-08-15"', '$.date().string()'); + +set datestyle = 'ISO'; +select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work +select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()'); +select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()'); +select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()'); +select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work +select jsonb_path_query('"12:34:56"', '$.time().string()'); +select jsonb_path_query('"2023-08-15"', '$.date().string()'); -- Test .time() select jsonb_path_query('null', '$.time()'); -- 2.45.2