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

Commit 1fb2508

Browse files
author
Nikita Glukhov
committed
Fix jsonpath grammar: add support for $[*]
1 parent 5f29ef3 commit 1fb2508

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

src/backend/utils/adt/jsonpath_gram.y

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ makeIndexArray(List *list)
195195

196196
%type <value> result scalar_value
197197

198-
%type <elems> joined_key path absolute_path relative_path
198+
%type <elems> joined_key path absolute_path relative_path array_accessors
199199

200-
%type <value> key any_key right_expr expr jsonpath numeric
200+
%type <value> key any_key right_expr expr jsonpath numeric array_accessor
201201

202202
%type <indexs> index_elem index_list
203203

@@ -278,18 +278,27 @@ index_list:
278278
| index_list ',' index_elem { $$ = list_concat($1, $3); }
279279
;
280280

281+
array_accessor:
282+
'[' '*' ']' { $$ = makeItemType(jpiAnyArray); }
283+
| '[' index_list ']' { $$ = makeIndexArray($2); }
284+
;
285+
286+
array_accessors:
287+
array_accessor { $$ = list_make1($1); }
288+
| array_accessors array_accessor { $$ = lappend($1, $2); }
289+
;
290+
281291
any_key:
282292
key { $$ = $1; }
293+
| array_accessor { $$ = $1; }
283294
| '*' { $$ = makeItemType(jpiAnyKey); }
284-
| '[' '*' ']' { $$ = makeItemType(jpiAnyArray); }
285-
| '[' index_list ']' { $$ = makeIndexArray($2); }
286295
;
287296

288297
joined_key:
289298
any_key { $$ = list_make1($1); }
290-
| joined_key '[' '*' ']' { $$ = lappend($1, makeItemType(jpiAnyArray)); }
291-
| joined_key '[' index_list ']' { $$ = lappend($1, makeIndexArray($3)); }
299+
| joined_key array_accessor { $$ = lappend($1, $2); }
292300
;
301+
293302
key:
294303
STRING_P { $$ = makeItemKey(&$1); }
295304
| TO_P { $$ = makeItemKey(&$1); }
@@ -301,15 +310,18 @@ key:
301310

302311
absolute_path:
303312
'$' '.' { $$ = list_make1(makeItemType(jpiRoot)); }
304-
| '$' { $$ = list_make1(makeItemType(jpiRoot)); }
313+
| '$' { $$ = list_make1(makeItemType(jpiRoot)); }
305314
| '$' '.' path { $$ = lcons(makeItemType(jpiRoot), $3); }
315+
| '$' array_accessors { $$ = lcons(makeItemType(jpiRoot), $2); }
316+
| '$' array_accessors '.' path { $$ = lcons(makeItemType(jpiRoot), list_concat($2, $4)); }
306317
;
307318

308319
relative_path:
309320
joined_key '.' joined_key { $$ = list_concat($1, $3); }
310321
| '.' joined_key '.' joined_key { $$ = list_concat($2, $4); }
311322
| '@' '.' joined_key '.' joined_key { $$ = list_concat($3, $5); }
312323
| relative_path '.' joined_key { $$ = list_concat($1, $3); }
324+
;
313325

314326
path:
315327
joined_key { $$ = $1; }

src/test/regress/expected/jsonpath.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ select '$.a[*].[*]'::jsonpath;
7575
$."a"[*][*]
7676
(1 row)
7777

78+
select '$[*]'::jsonpath;
79+
jsonpath
80+
----------
81+
$[*]
82+
(1 row)
83+
84+
select '$[0]'::jsonpath;
85+
jsonpath
86+
----------
87+
$[0]
88+
(1 row)
89+
90+
select '$[*][0]'::jsonpath;
91+
jsonpath
92+
----------
93+
$[*][0]
94+
(1 row)
95+
96+
select '$[*].a'::jsonpath;
97+
jsonpath
98+
----------
99+
$[*]."a"
100+
(1 row)
101+
102+
select '$[*][0].a.b'::jsonpath;
103+
jsonpath
104+
-----------------
105+
$[*][0]."a"."b"
106+
(1 row)
107+
78108
select '$.g ? (@ = 1)'::jsonpath;
79109
jsonpath
80110
---------------

src/test/regress/sql/jsonpath.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ select '$.a.[*][*]'::jsonpath;
1313
select '$.a.[*].[*]'::jsonpath;
1414
select '$.a[*][*]'::jsonpath;
1515
select '$.a[*].[*]'::jsonpath;
16+
select '$[*]'::jsonpath;
17+
select '$[0]'::jsonpath;
18+
select '$[*][0]'::jsonpath;
19+
select '$[*].a'::jsonpath;
20+
select '$[*][0].a.b'::jsonpath;
1621

1722
select '$.g ? (@ = 1)'::jsonpath;
1823
select '$.g ? (a = 1)'::jsonpath;

0 commit comments

Comments
 (0)