Skip to content

Commit 72bf6b6

Browse files
committed
jsonpath/parser: add support for string scalars
This commit adds string scalars, enabling string comparisons within jsonpath queries. Epic: None Release note (sql change): Support string comparisons within jsonpath queries.
1 parent 5350a82 commit 72bf6b6

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

pkg/sql/logictest/testdata/logic_test/jsonb_path_query

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -761,12 +761,30 @@ SELECT jsonb_path_query('{"a": "hello"}', '$.a / 2');
761761
statement error pgcode 22038 pq: right operand of jsonpath operator \+ is not a single numeric value
762762
SELECT jsonb_path_query('{"a": null}', '2 + $.a');
763763

764-
# when string literals are supported
765-
# query T rowsort
766-
# SELECT jsonb_path_query('{"data": [{"val": "a", "num": 1}, {"val": "b", "num": 2}, {"val": "a", "num": 3}]}'::jsonb, '$.data ? (@.val == "a")'::jsonpath);
767-
# ----
768-
# {"num": 1, "val": "a"}
769-
# {"num": 3, "val": "a"}
764+
query T
765+
SELECT jsonb_path_query('{}', '"a" == "b"');
766+
----
767+
false
768+
769+
query T
770+
SELECT jsonb_path_query('{}', '"a" < "b"');
771+
----
772+
true
773+
774+
query T
775+
SELECT jsonb_path_query('{}', '"a" > "b"');
776+
----
777+
false
778+
779+
statement error pgcode 22038 pq: left operand of jsonpath operator \+ is not a single numeric value
780+
SELECT jsonb_path_query('{}', '"a" + "b"');
781+
782+
query T rowsort
783+
SELECT jsonb_path_query('{"data": [{"val": "a", "num": 1}, {"val": "b", "num": 2}, {"val": "a", "num": 3}]}', '$.data ? (@.val == "a")');
784+
----
785+
{"num": 1, "val": "a"}
786+
{"num": 3, "val": "a"}
787+
770788
# select jsonb_path_query('[1, 2, 3, 4, 5]', '$[-1]');
771789
# select jsonb_path_query('[1, 2, 3, 4, 5]', 'strict $[-1]');
772790

pkg/sql/scanner/jsonpath_scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (s *JSONPathScanner) Scan(lval ScanSymType) {
4747
case identQuote:
4848
// "[^"]"
4949
if s.scanString(lval, identQuote, false /* allowEscapes */, true /* requireUTF8 */) {
50-
lval.SetID(lexbase.IDENT)
50+
lval.SetID(lexbase.STRING)
5151
}
5252
return
5353
case '=':

pkg/util/jsonpath/parser/jsonpath.y

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func unaryOp(op jsonpath.OperationType, left jsonpath.Path) jsonpath.Operation {
171171

172172
%token <str> CURRENT
173173

174+
%token <str> STRING
175+
174176
%type <jsonpath.Jsonpath> jsonpath
175177
%type <jsonpath.Path> expr_or_predicate
176178
%type <jsonpath.Path> expr
@@ -447,20 +449,22 @@ scalar_value:
447449
{
448450
$$.val = jsonpath.Scalar{Type: jsonpath.ScalarBool, Value: json.FromBool(false)}
449451
}
450-
// TODO(normanchenn): support strings, null.
452+
| STRING
453+
{
454+
$$.val = jsonpath.Scalar{Type: jsonpath.ScalarString, Value: json.FromString($1)}
455+
}
456+
// TODO(normanchenn): support null.
451457
;
452458

453459
any_identifier:
454460
IDENT
461+
| STRING
455462
| unreserved_keyword
456463
;
457464

458465
unreserved_keyword:
459-
AND
460-
| FALSE
466+
FALSE
461467
| LAX
462-
| NOT
463-
| OR
464468
| STRICT
465469
| TO
466470
| TRUE

pkg/util/jsonpath/parser/testdata/jsonpath

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,24 @@ $.c[$.b - $.a to $.d - $.b]
418418
----
419419
$."c"[($."b" - $."a") to ($."d" - $."b")] -- normalized!
420420

421+
parse
422+
"hello" == "hello"
423+
----
424+
("hello" == "hello") -- normalized!
425+
426+
parse
427+
$.a ? (@.b == "string")
428+
----
429+
$."a"?((@."b" == "string")) -- normalized!
430+
431+
error
432+
"a" && "b"
433+
----
434+
at or near "&": syntax error
435+
DETAIL: source SQL:
436+
"a" && "b"
437+
^
438+
421439
# postgres allows floats as array indexes
422440
# parse
423441
# $.abc[1.0]

0 commit comments

Comments
 (0)