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

Commit eb3ba15

Browse files
author
Nikita Glukhov
committed
Fix jsonpath parenthesized expressions
1 parent 5214cc4 commit eb3ba15

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
@@ -284,7 +284,7 @@ makeItemLikeRegex(JsonPathParseItem *expr, string *pattern, string *flags)
284284

285285
%type <result> result
286286

287-
%type <value> scalar_value path_primary expr pexpr array_accessor
287+
%type <value> scalar_value path_primary expr array_accessor
288288
any_path accessor_op key predicate delimited_predicate
289289
index_elem starts_with_initial opt_datetime_template
290290
expr_or_predicate
@@ -351,21 +351,21 @@ comp_op:
351351
;
352352

353353
delimited_predicate:
354-
'(' predicate ')' { $$ = $2; }
354+
'(' predicate ')' { $$ = $2; }
355355
| EXISTS_P '(' expr ')' { $$ = makeItemUnary(jpiExists, $3); }
356356
;
357357

358358
predicate:
359359
delimited_predicate { $$ = $1; }
360-
| pexpr comp_op pexpr { $$ = makeItemBinary($2, $1, $3); }
360+
| expr comp_op expr { $$ = makeItemBinary($2, $1, $3); }
361361
| predicate AND_P predicate { $$ = makeItemBinary(jpiAnd, $1, $3); }
362362
| predicate OR_P predicate { $$ = makeItemBinary(jpiOr, $1, $3); }
363363
| NOT_P delimited_predicate { $$ = makeItemUnary(jpiNot, $2); }
364364
| '(' predicate ')' IS_P UNKNOWN_P { $$ = makeItemUnary(jpiIsUnknown, $2); }
365-
| pexpr STARTS_P WITH_P starts_with_initial
365+
| expr STARTS_P WITH_P starts_with_initial
366366
{ $$ = makeItemBinary(jpiStartsWith, $1, $4); }
367-
| pexpr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); }
368-
| pexpr LIKE_REGEX_P STRING_P FLAG_P STRING_P
367+
| expr LIKE_REGEX_P STRING_P { $$ = makeItemLikeRegex($1, &$3, NULL); }
368+
| expr LIKE_REGEX_P STRING_P FLAG_P STRING_P
369369
{ $$ = makeItemLikeRegex($1, &$3, &$5); }
370370
;
371371

@@ -385,29 +385,25 @@ accessor_expr:
385385
path_primary { $$ = list_make1($1); }
386386
| '.' key { $$ = list_make2(makeItemType(jpiCurrent), $2); }
387387
| '(' expr ')' accessor_op { $$ = list_make2($2, $4); }
388-
| '(' predicate ')' accessor_op { $$ = list_make2($2, $4); }
388+
| '(' predicate ')' accessor_op { $$ = list_make2($2, $4); }
389389
| accessor_expr accessor_op { $$ = lappend($1, $2); }
390390
;
391391

392-
pexpr:
393-
expr { $$ = $1; }
394-
| '(' expr ')' { $$ = $2; }
395-
;
396-
397392
expr:
398-
accessor_expr { $$ = makeItemList($1); }
399-
| '+' pexpr %prec UMINUS { $$ = makeItemUnary(jpiPlus, $2); }
400-
| '-' pexpr %prec UMINUS { $$ = makeItemUnary(jpiMinus, $2); }
401-
| pexpr '+' pexpr { $$ = makeItemBinary(jpiAdd, $1, $3); }
402-
| pexpr '-' pexpr { $$ = makeItemBinary(jpiSub, $1, $3); }
403-
| pexpr '*' pexpr { $$ = makeItemBinary(jpiMul, $1, $3); }
404-
| pexpr '/' pexpr { $$ = makeItemBinary(jpiDiv, $1, $3); }
405-
| pexpr '%' pexpr { $$ = makeItemBinary(jpiMod, $1, $3); }
393+
accessor_expr { $$ = makeItemList($1); }
394+
| '(' expr ')' { $$ = $2; }
395+
| '+' expr %prec UMINUS { $$ = makeItemUnary(jpiPlus, $2); }
396+
| '-' expr %prec UMINUS { $$ = makeItemUnary(jpiMinus, $2); }
397+
| expr '+' expr { $$ = makeItemBinary(jpiAdd, $1, $3); }
398+
| expr '-' expr { $$ = makeItemBinary(jpiSub, $1, $3); }
399+
| expr '*' expr { $$ = makeItemBinary(jpiMul, $1, $3); }
400+
| expr '/' expr { $$ = makeItemBinary(jpiDiv, $1, $3); }
401+
| expr '%' expr { $$ = makeItemBinary(jpiMod, $1, $3); }
406402
;
407403

408404
index_elem:
409-
pexpr { $$ = makeItemBinary(jpiSubscript, $1, NULL); }
410-
| pexpr TO_P pexpr { $$ = makeItemBinary(jpiSubscript, $1, $3); }
405+
expr { $$ = makeItemBinary(jpiSubscript, $1, NULL); }
406+
| expr TO_P expr { $$ = makeItemBinary(jpiSubscript, $1, $3); }
411407
;
412408

413409
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)