Skip to content

Commit 664452a

Browse files
committed
sql/jsonpath: disallow index acceleration for jsonb_path_exists with variables
We don't allow index acceleration for jsonb_path_exists filter if it takes a variable list in its parameters. For example, `jsonb_path_exists(b, '$.a ? (@.b == $x)', '{"x": "c"}')` (where `'{"x": "c"}'` is the variable list) is not allowed. Release note: None
1 parent 8eaa0da commit 664452a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pkg/sql/logictest/testdata/logic_test/jsonb_path_exists_index_acceleration

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,3 +545,14 @@ SELECT a FROM json_tab@primary WHERE jsonb_path_exists('{"a": {"b": "c"}}', '$.a
545545

546546
statement error index "foo_inv" is inverted and cannot be used for this query
547547
SELECT a FROM json_tab@foo_inv WHERE jsonb_path_exists('{"a": {"b": "c"}}', '$.a.b') ORDER BY a;
548+
549+
# We don't index accelerate jsonb_path_exists clause with variables.
550+
query I
551+
SELECT a FROM json_tab@primary WHERE jsonb_path_exists(b, '$.a ? (@.b == $x)', '{"x": "c"}') ORDER BY a;
552+
----
553+
3
554+
9
555+
19
556+
557+
statement error index "foo_inv" is inverted and cannot be used for this query
558+
SELECT a FROM json_tab@foo_inv WHERE jsonb_path_exists(b, '$.a ? (@.b == $x)', '{"x": "c"}') ORDER BY a;

pkg/sql/opt/invertedidx/json_array.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ func (j *jsonOrArrayFilterPlanner) extractInvertedFilterConditionFromLeaf(
416416
invertedExpr = j.extractArrayOverlapsCondition(ctx, evalCtx, t.Left, t.Right)
417417
case *memo.FunctionExpr:
418418
if t.Properties.Category == builtinconstants.CategoryJsonpath && t.Name == "jsonb_path_exists" {
419-
if len(t.Args) > 1 {
419+
if len(t.Args) == 2 {
420420
// The first parameter has to be a column reference.
421421
if isIndexColumn(j.tabID, j.index, t.Args[0], j.computedColumns) {
422422
if ce, ok := t.Args[1].(*memo.ConstExpr); ok {

0 commit comments

Comments
 (0)