Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 653d396

Browse files
committed
Teach jsonpath string() to unwrap in lax mode
This was an ommission in commit 66ea94e, and brings it into compliance with both other methods and the standard. Per complaint from David Wheeler. Author: David Wheeler, Jeevan Chalke Reviewed-by: Chapman Flack Discussion: https://postgr.es/m/A64AE04F-4410-42B7-A141-7A7349260F4D@justatheory.com
1 parent 81d20fb commit 653d396

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

doc/src/sgml/func.sgml

+4-1
Original file line numberDiff line numberDiff line change
@@ -17792,7 +17792,10 @@ ERROR: jsonpath member accessor can only be applied to an object
1779217792
methods available in <type>jsonpath</type>. Note that while the unary
1779317793
operators and methods can be applied to multiple values resulting from a
1779417794
preceding path step, the binary operators (addition etc.) can only be
17795-
applied to single values.
17795+
applied to single values. In lax mode, methods applied to an array will be
17796+
executed for each value in the array. The exceptions are
17797+
<literal>.type()</literal> and <literal>.size()</literal>, which apply to
17798+
the array itself.
1779617799
</para>
1779717800

1779817801
<table id="functions-sqljson-op-table">

src/backend/utils/adt/jsonpath_exec.c

+3
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,9 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
16061606
JsonbValue jbv;
16071607
char *tmp = NULL;
16081608

1609+
if (unwrap && JsonbType(jb) == jbvArray)
1610+
return executeItemUnwrapTargetArray(cxt, jsp, jb, found, false);
1611+
16091612
switch (JsonbType(jb))
16101613
{
16111614
case jbvString:

src/test/regress/expected/jsonb_jsonpath.out

+11-1
Original file line numberDiff line numberDiff line change
@@ -2525,7 +2525,10 @@ select jsonb_path_query('null', '$.string()', silent => true);
25252525
(0 rows)
25262526

25272527
select jsonb_path_query('[]', '$.string()');
2528-
ERROR: jsonpath item method .string() can only be applied to a bool, string, numeric, or datetime value
2528+
jsonb_path_query
2529+
------------------
2530+
(0 rows)
2531+
25292532
select jsonb_path_query('[]', 'strict $.string()');
25302533
ERROR: jsonpath item method .string() can only be applied to a bool, string, numeric, or datetime value
25312534
select jsonb_path_query('{}', '$.string()');
@@ -2576,6 +2579,13 @@ select jsonb_path_query('1234', '$.string().type()');
25762579
"string"
25772580
(1 row)
25782581

2582+
select jsonb_path_query('[2, true]', '$.string()');
2583+
jsonb_path_query
2584+
------------------
2585+
"2"
2586+
"true"
2587+
(2 rows)
2588+
25792589
select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp().string()');
25802590
ERROR: cannot convert value from timestamptz to timestamp without time zone usage
25812591
HINT: Use *_tz() function for time zone support.

src/test/regress/sql/jsonb_jsonpath.sql

+1
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ select jsonb_path_query('"1.23aaa"', '$.string()');
586586
select jsonb_path_query('1234', '$.string()');
587587
select jsonb_path_query('true', '$.string()');
588588
select jsonb_path_query('1234', '$.string().type()');
589+
select jsonb_path_query('[2, true]', '$.string()');
589590
select jsonb_path_query('"2023-08-15 12:34:56 +5:30"', '$.timestamp().string()');
590591
select jsonb_path_query_tz('"2023-08-15 12:34:56 +5:30"', '$.timestamp().string()'); -- should work
591592
select jsonb_path_query_array('[1.23, "yes", false]', '$[*].string()');

0 commit comments

Comments
 (0)