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

Commit 8fa4e08

Browse files
author
Nikita Glukhov
committed
Fix jsonpath parenthesized expressions
1 parent 9cad7ae commit 8fa4e08

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/backend/utils/adt/jsonpath_gram.y

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ makeItemLikeRegex(JsonPathParseItem *expr, string *pattern, string *flags)
281281

282282
%type <result> result
283283

284-
%type <value> scalar_value path_primary expr pexpr array_accessor
284+
%type <value> scalar_value path_primary expr array_accessor
285285
any_path accessor_op key predicate delimited_predicate
286286
index_elem starts_with_initial opt_datetime_template
287287
expr_or_predicate
@@ -348,21 +348,21 @@ comp_op:
348348
;
349349

350350
delimited_predicate:
351-
'(' predicate ')' { $$ = $2; }
351+
'(' predicate ')' { $$ = $2; }
352352
| EXISTS_P '(' expr ')' { $$ = makeItemUnary(jpiExists, $3); }
353353
;
354354

355355
predicate:
356356
delimited_predicate { $$ = $1; }
357-
| pexpr comp_op pexpr { $$ = makeItemBinary($2, $1, $3); }
357+
| expr comp_op expr { $$ = makeItemBinary($2, $1, $3); }
358358
| predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1, $3); }
359359
| predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1, $3); }
360360
| NOT_P delimited_predicate { $$ = makeItemUnary(jpiNot, $2); }
361361
| '(' predicate ')' IS_P UNKNOWN_P { $$ = makeItemUnary(jpiIsUnknown, $2); }
362-
| pexpr STARTS_P WITH_P starts_with_initial
362+
| expr STARTS_P WITH_P starts_with_initial
363363
{ $$ = makeItemBinary(jpiStartsWith, $1, $4); }
364-
| pexpr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); };
365-
| pexpr LIKE_REGEX_P STRING_P FLAG_P STRING_P
364+
| expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); };
365+
| expr LIKE_REGEX_P STRING_P FLAG_P STRING_P
366366
{ $$ = makeItemLikeRegex($1, &$3, &$5); };
367367
;
368368

@@ -382,29 +382,25 @@ accessor_expr:
382382
path_primary { $$ = list_make1($1); }
383383
| '.' key { $$ = list_make2(makeItemType(jpiCurrent), $2); }
384384
| '(' expr ')' accessor_op { $$ = list_make2($2, $4); }
385-
| '(' predicate ')' accessor_op { $$ = list_make2($2, $4); }
385+
| '(' predicate ')' accessor_op { $$ = list_make2($2, $4); }
386386
| accessor_expr accessor_op { $$ = lappend($1, $2); }
387387
;
388388

389-
pexpr:
390-
expr { $$ = $1; }
391-
| '(' expr ')' { $$ = $2; }
392-
;
393-
394389
expr:
395-
accessor_expr { $$ = makeItemList($1); }
396-
| '+' pexpr %prec UMINUS { $$ = makeItemUnary(jpiPlus, $2); }
397-
| '-' pexpr %prec UMINUS { $$ = makeItemUnary(jpiMinus, $2); }
398-
| pexpr '+' pexpr { $$ = makeItemBinary(jpiAdd, $1, $3); }
399-
| pexpr '-' pexpr { $$ = makeItemBinary(jpiSub, $1, $3); }
400-
| pexpr '*' pexpr { $$ = makeItemBinary(jpiMul, $1, $3); }
401-
| pexpr '/' pexpr { $$ = makeItemBinary(jpiDiv, $1, $3); }
402-
| pexpr '%' pexpr { $$ = makeItemBinary(jpiMod, $1, $3); }
390+
accessor_expr { $$ = makeItemList($1); }
391+
| '(' expr ')' { $$ = $2; }
392+
| '+' expr %prec UMINUS { $$ = makeItemUnary(jpiPlus, $2); }
393+
| '-' expr %prec UMINUS { $$ = makeItemUnary(jpiMinus, $2); }
394+
| expr '+' expr { $$ = makeItemBinary(jpiAdd, $1, $3); }
395+
| expr '-' expr { $$ = makeItemBinary(jpiSub, $1, $3); }
396+
| expr '*' expr { $$ = makeItemBinary(jpiMul, $1, $3); }
397+
| expr '/' expr { $$ = makeItemBinary(jpiDiv, $1, $3); }
398+
| expr '%' expr { $$ = makeItemBinary(jpiMod, $1, $3); }
403399
;
404400

405401
index_elem:
406-
pexpr { $$ = makeItemBinary(jpiSubscript, $1, NULL); }
407-
| pexpr TO_P pexpr { $$ = makeItemBinary(jpiSubscript, $1, $3); }
402+
expr { $$ = makeItemBinary(jpiSubscript, $1, NULL); }
403+
| expr TO_P expr { $$ = makeItemBinary(jpiSubscript, $1, $3); }
408404
;
409405

410406
index_list:

src/test/regress/expected/jsonpath.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,24 @@ select '1 + ($.a.b > 2).c.d'::jsonpath;
462462
(1 + ($."a"."b" > 2)."c"."d")
463463
(1 row)
464464

465+
select '($)'::jsonpath;
466+
jsonpath
467+
----------
468+
$
469+
(1 row)
470+
471+
select '(($))'::jsonpath;
472+
jsonpath
473+
----------
474+
$
475+
(1 row)
476+
477+
select '((($ + 1)).a + ((2)).b ? ((((@ > 1)) || (exists(@.c)))))'::jsonpath;
478+
jsonpath
479+
-------------------------------------------------
480+
(($ + 1)."a" + 2."b"?(@ > 1 || exists (@."c")))
481+
(1 row)
482+
465483
select '$ ? (@.a < 1)'::jsonpath;
466484
jsonpath
467485
---------------

src/test/regress/sql/jsonpath.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ select '($.a.b + -$.x.y).c.d'::jsonpath;
8686
select '(-+$.a.b).c.d'::jsonpath;
8787
select '1 + ($.a.b + 2).c.d'::jsonpath;
8888
select '1 + ($.a.b > 2).c.d'::jsonpath;
89+
select '($)'::jsonpath;
90+
select '(($))'::jsonpath;
91+
select '((($ + 1)).a + ((2)).b ? ((((@ > 1)) || (exists(@.c)))))'::jsonpath;
8992

9093
select '$ ? (@.a < 1)'::jsonpath;
9194
select '$ ? (@.a < -1)'::jsonpath;

0 commit comments

Comments
 (0)