Skip to content

Add tests for jsonpath .* on arrays #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/backend/utils/adt/jsonpath_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -2978,7 +2978,8 @@ getJsonPathItem(JsonPathExecContext *cxt, JsonPathItem *item,
}

/*
* Returns the computed value of a JSON path variable with given name.
* Definition of JsonPathGetVarCallback for when JsonPathExecContext.vars
* is specified as a List value.
*/
static JsonbValue *
GetJsonPathVar(void *cxt, char *varName, int varNameLen,
Expand Down Expand Up @@ -3025,6 +3026,10 @@ GetJsonPathVar(void *cxt, char *varName, int varNameLen,
return result;
}

/*
* Definition of JsonPathCountVarsCallback for when JsonPathExecContext.vars
* is specified as a List value.
*/
static int
CountJsonPathVars(void *cxt)
{
Expand Down
50 changes: 50 additions & 0 deletions src/test/regress/expected/jsonb_jsonpath.out
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,56 @@ select jsonb_path_query('{"a": [1, 2]}', 'lax $.a * 3', silent => true);
------------------
(0 rows)

-- any key on arrays with and without unwrapping.
select jsonb_path_query('{"a": [1,2,3], "b": [3,4,5]}', '$.*');
jsonb_path_query
------------------
[1, 2, 3]
[3, 4, 5]
(2 rows)

select jsonb_path_query('[1,2,3]', '$.*');
jsonb_path_query
------------------
(0 rows)

select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'lax $.*');
jsonb_path_query
------------------
[3, 4, 5]
(1 row)

select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*');
ERROR: jsonpath wildcard member accessor can only be applied to an object
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*', NULL, true);
jsonb_path_query
------------------
(0 rows)

select jsonb '{"a": [1,2,3], "b": [3,4,5]}' @? '$.*';
?column?
----------
t
(1 row)

select jsonb '[1,2,3]' @? '$.*';
?column?
----------
f
(1 row)

select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'lax $.*';
?column?
----------
t
(1 row)

select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'strict $.*';
?column?
----------

(1 row)

-- extension: boolean expressions
select jsonb_path_query('2', '$ > 1');
jsonb_path_query
Expand Down
11 changes: 11 additions & 0 deletions src/test/regress/sql/jsonb_jsonpath.sql
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,17 @@ select jsonb_path_query('{"a": [2, 3, 4]}', 'lax -$.a');
select jsonb_path_query('{"a": [1, 2]}', 'lax $.a * 3');
select jsonb_path_query('{"a": [1, 2]}', 'lax $.a * 3', silent => true);

-- any key on arrays with and without unwrapping.
select jsonb_path_query('{"a": [1,2,3], "b": [3,4,5]}', '$.*');
select jsonb_path_query('[1,2,3]', '$.*');
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'lax $.*');
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*');
select jsonb_path_query('[1,2,3,{"b": [3,4,5]}]', 'strict $.*', NULL, true);
select jsonb '{"a": [1,2,3], "b": [3,4,5]}' @? '$.*';
select jsonb '[1,2,3]' @? '$.*';
select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'lax $.*';
select jsonb '[1,2,3,{"b": [3,4,5]}]' @? 'strict $.*';

-- extension: boolean expressions
select jsonb_path_query('2', '$ > 1');
select jsonb_path_query('2', '$ <= 1');
Expand Down