Skip to content

Commit a7f9c1e

Browse files
committed
Add tests for jsonpath .* on arrays
There was no coverage for the path to unwrap an array before applying `.*` to it, so add tests that explicitly test `.*` for both objects and arrays, showing how no results are returned for an array of scalars, but results are returned when the array contains an object. Also test the behavior in strict mode with and without the silent parameter, and with the `@?` operator. Unrelated but potentially useful to future source readers: document `GetJsonPathVar` and `CountJsonPathVars`.
1 parent d537b2e commit a7f9c1e

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2978,7 +2978,8 @@ getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item,
29782978
}
29792979

29802980
/*
2981-
* Returns the computed value of a JSON path variable with given name.
2981+
* Definition of JsonPathGetVarCallback for when JsonPathExecContext.vars
2982+
* is specified as a List value.
29822983
*/
29832984
static JsonbValue *
29842985
GetJsonPathVar(void *cxt, char *varName, int varNameLen,
@@ -3025,6 +3026,10 @@ GetJsonPathVar(void *cxt, char *varName, int varNameLen,
30253026
return result;
30263027
}
30273028

3029+
/*
3030+
* Definition of JsonPathCountVarsCallback for when JsonPathExecContext.vars
3031+
* is specified as a List value.
3032+
*/
30283033
static int
30293034
CountJsonPathVars(void *cxt)
30303035
{

src/test/regress/expected/jsonb_jsonpath.out

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4394,3 +4394,53 @@ ORDER BY s1.num, s2.num;
43944394
{"s": "B"} | {"s": "B"} | false | true | true | true | false
43954395
(144 rows)
43964396

4397+
-- Test any key on arrays with and without unwrapping.
4398+
select jsonb_path_query('{"a": [1,2,3], "b": [3,4,5]}', '$.*');
4399+
jsonb_path_query
4400+
------------------
4401+
[1, 2, 3]
4402+
[3, 4, 5]
4403+
(2 rows)
4404+
4405+
select jsonb_path_query('[1,2,3]', '$.*');
4406+
jsonb_path_query
4407+
------------------
4408+
(0 rows)
4409+
4410+
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'lax $.*');
4411+
jsonb_path_query
4412+
------------------
4413+
[3, 4, 5]
4414+
(1 row)
4415+
4416+
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*');
4417+
ERROR: jsonpath wildcard member accessor can only be applied to an object
4418+
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*', NULL, true);
4419+
jsonb_path_query
4420+
------------------
4421+
(0 rows)
4422+
4423+
select jsonb '{"a": [1,2,3], "b": [3,4,5]}' @? '$.*';
4424+
?column?
4425+
----------
4426+
t
4427+
(1 row)
4428+
4429+
select jsonb '[1,2,3]' @? '$.*';
4430+
?column?
4431+
----------
4432+
f
4433+
(1 row)
4434+
4435+
select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'lax $.*';
4436+
?column?
4437+
----------
4438+
t
4439+
(1 row)
4440+
4441+
select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'strict $.*';
4442+
?column?
4443+
----------
4444+
4445+
(1 row)
4446+

src/test/regress/sql/jsonb_jsonpath.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,3 +1116,14 @@ SELECT
11161116
jsonb_path_query_first(s1.j, '$.s > $s', vars => s2.j) gt
11171117
FROM str s1, str s2
11181118
ORDER BY s1.num, s2.num;
1119+
1120+
-- Test any key on arrays with and without unwrapping.
1121+
select jsonb_path_query('{"a": [1,2,3], "b": [3,4,5]}', '$.*');
1122+
select jsonb_path_query('[1,2,3]', '$.*');
1123+
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'lax $.*');
1124+
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*');
1125+
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*', NULL, true);
1126+
select jsonb '{"a": [1,2,3], "b": [3,4,5]}' @? '$.*';
1127+
select jsonb '[1,2,3]' @? '$.*';
1128+
select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'lax $.*';
1129+
select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'strict $.*';

0 commit comments

Comments
 (0)