Skip to content

Commit 9b71f36

Browse files
committed
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.
1 parent 057ee91 commit 9b71f36

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

doc/src/sgml/func.sgml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17971,7 +17971,9 @@ ERROR: jsonpath member accessor can only be applied to an object
1797117971
<returnvalue><replaceable>string</replaceable></returnvalue>
1797217972
</para>
1797317973
<para>
17974-
String value converted from a JSON boolean, number, string, or datetime
17974+
String value converted from a JSON boolean, number, string, or
17975+
datetime. Note that the string output of datetimes is determined by
17976+
the <xref linkend="guc-datestyle"/> parameter.
1797517977
</para>
1797617978
<para>
1797717979
<literal>jsonb_path_query_array('[1.23, "xyz", false]', '$[*].string()')</literal>

src/test/regress/expected/jsonb_jsonpath.out

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,95 @@ select jsonb_path_query_array('[1.23, "yes", false]', '$[*].string().type()');
26572657
["string", "string", "string"]
26582658
(1 row)
26592659

2660+
select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp_tz().string()');
2661+
ERROR: cannot convert value from timestamp to timestamptz without time zone usage
2662+
HINT: Use *_tz() function for time zone support.
2663+
select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work
2664+
jsonb_path_query_tz
2665+
--------------------------------
2666+
"Tue Aug 15 12:34:56 2023 PDT"
2667+
(1 row)
2668+
2669+
select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()');
2670+
jsonb_path_query
2671+
--------------------------------
2672+
"Tue Aug 15 00:04:56 2023 PDT"
2673+
(1 row)
2674+
2675+
select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()');
2676+
jsonb_path_query
2677+
----------------------------
2678+
"Tue Aug 15 12:34:56 2023"
2679+
(1 row)
2680+
2681+
select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()');
2682+
jsonb_path_query
2683+
------------------
2684+
"12:34:56+05:30"
2685+
(1 row)
2686+
2687+
select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work
2688+
jsonb_path_query_tz
2689+
---------------------
2690+
"12:34:56-07"
2691+
(1 row)
2692+
2693+
select jsonb_path_query('"12:34:56"', '$.time().string()');
2694+
jsonb_path_query
2695+
------------------
2696+
"12:34:56"
2697+
(1 row)
2698+
2699+
select jsonb_path_query('"2023-08-15"', '$.date().string()');
2700+
jsonb_path_query
2701+
------------------
2702+
"08-15-2023"
2703+
(1 row)
2704+
2705+
set datestyle = 'ISO';
2706+
select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work
2707+
jsonb_path_query_tz
2708+
--------------------------
2709+
"2023-08-15 12:34:56-07"
2710+
(1 row)
2711+
2712+
select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()');
2713+
jsonb_path_query
2714+
--------------------------
2715+
"2023-08-15 00:04:56-07"
2716+
(1 row)
2717+
2718+
select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()');
2719+
jsonb_path_query
2720+
-----------------------
2721+
"2023-08-15 12:34:56"
2722+
(1 row)
2723+
2724+
select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()');
2725+
jsonb_path_query
2726+
------------------
2727+
"12:34:56+05:30"
2728+
(1 row)
2729+
2730+
select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work
2731+
jsonb_path_query_tz
2732+
---------------------
2733+
"12:34:56-07"
2734+
(1 row)
2735+
2736+
select jsonb_path_query('"12:34:56"', '$.time().string()');
2737+
jsonb_path_query
2738+
------------------
2739+
"12:34:56"
2740+
(1 row)
2741+
2742+
select jsonb_path_query('"2023-08-15"', '$.date().string()');
2743+
jsonb_path_query
2744+
------------------
2745+
"2023-08-15"
2746+
(1 row)
2747+
2748+
reset datestyle;
26602749
-- Test .time()
26612750
select jsonb_path_query('null', '$.time()');
26622751
ERROR: jsonpath item method .time() can only be applied to a string

src/test/regress/sql/jsonb_jsonpath.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,24 @@ select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp().string()')
602602
select jsonb_path_query_tz('"2023-08-15 12:34:56 +5:30"', '$.timestamp().string()'); -- should work
603603
select jsonb_path_query_array('[1.23, "yes", false]', '$[*].string()');
604604
select jsonb_path_query_array('[1.23, "yes", false]', '$[*].string().type()');
605+
select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp_tz().string()');
606+
select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work
607+
select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()');
608+
select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()');
609+
select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()');
610+
select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work
611+
select jsonb_path_query('"12:34:56"', '$.time().string()');
612+
select jsonb_path_query('"2023-08-15"', '$.date().string()');
613+
614+
set datestyle = 'ISO';
615+
select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz().string()'); -- should work
616+
select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp_tz().string()');
617+
select jsonb_path_query('"2023-08-15 12:34:56"', '$.timestamp().string()');
618+
select jsonb_path_query('"12:34:56 +5:30"', '$.time_tz().string()');
619+
select jsonb_path_query_tz('"12:34:56"', '$.time_tz().string()'); -- should work
620+
select jsonb_path_query('"12:34:56"', '$.time().string()');
621+
select jsonb_path_query('"2023-08-15"', '$.date().string()');
622+
reset datestyle;
605623

606624
-- Test .time()
607625
select jsonb_path_query('null', '$.time()');

0 commit comments

Comments
 (0)