Skip to content

Commit 47aea3c

Browse files
committed
jsonpath: add parenthesized expressions in jsonpath accessors
This commit adds support for parenthesized expressions in JSONPath accessors. Release note (sql change): Add support for parenthesized expressions in JSONPath queries. For example, (`SELECT jsonb_path_query('3', '($ > 2) ? (@ == true)');`)
1 parent 74f9ec4 commit 47aea3c

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

pkg/sql/logictest/testdata/logic_test/jsonb_path_query

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,3 +1375,34 @@ SELECT jsonb_path_query('{}', '$var', '{}', true);
13751375

13761376
statement error pgcode 42704 pq: could not find jsonpath variable "var"
13771377
SELECT jsonb_path_query('{}', '$var', '{}', false);
1378+
1379+
query T
1380+
SELECT jsonb_path_query('[1, 2, 3]', '($[*] > 2) ? (@ == true)');
1381+
----
1382+
true
1383+
1384+
query T
1385+
SELECT jsonb_path_query('[1, 2, 3]', '((($[*] > (2)))) ? ((@ == true))');
1386+
----
1387+
true
1388+
1389+
query T
1390+
SELECT jsonb_path_query('{"a": {"b": {"c": 1}}}', '($.a.b).c');
1391+
----
1392+
1
1393+
1394+
query T rowsort
1395+
SELECT jsonb_path_query('{"a": {"b": {"c": 1, "d": 2}}}', '($.a.b).*');
1396+
----
1397+
1
1398+
2
1399+
1400+
query T
1401+
SELECT jsonb_path_query('{"a": [10, 20, 30]}', '($.a)[1]');
1402+
----
1403+
20
1404+
1405+
query T
1406+
SELECT jsonb_path_query('{"a": {"b": [{"c": 1}, {"c": 2}]}}', '($.a.b[1]).c');
1407+
----
1408+
2

pkg/util/jsonpath/parser/jsonpath.y

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ accessor_expr:
304304
{
305305
$$.val = []jsonpath.Path{$1.path()}
306306
}
307+
| '(' expr ')' accessor_op
308+
{
309+
$$.val = []jsonpath.Path{$2.path(), $4.path()}
310+
}
311+
| '(' predicate ')' accessor_op
312+
{
313+
$$.val = []jsonpath.Path{$2.path(), $4.path()}
314+
}
307315
| accessor_expr accessor_op
308316
{
309317
$$.val = append($1.pathArr(), $2.path())

pkg/util/jsonpath/parser/testdata/jsonpath

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,34 @@ $.a starts with $var
556556
----
557557
($."a" starts with $"var") -- normalized!
558558

559+
parse
560+
($.a).b
561+
----
562+
$."a"."b" -- normalized!
563+
564+
parse
565+
(($.a.b).c.d).e.f
566+
----
567+
$."a"."b"."c"."d"."e"."f" -- normalized!
568+
569+
parse
570+
(1 == 1).abc
571+
----
572+
(1 == 1)."abc" -- normalized!
573+
574+
parse
575+
(((1 == 1).a.b == 2).c.d == 3).e.f
576+
----
577+
(((1 == 1)."a"."b" == 2)."c"."d" == 3)."e"."f" -- normalized!
578+
579+
error
580+
($[*] > 2 ? (@ == true)
581+
----
582+
at or near "EOF": syntax error
583+
DETAIL: source SQL:
584+
($[*] > 2 ? (@ == true)
585+
^
586+
559587
# postgres allows floats as array indexes
560588
# parse
561589
# $.abc[1.0]

0 commit comments

Comments
 (0)