Skip to content

Commit 430fb70

Browse files
committed
sql/jsonpath: unwrap array with root key for index acceleration
Fixes: #156741 Previously, we didn't consider the case where the queried JSON is an array and the root key can unwrap one layer of the array. For example: ``` INSERT INTO json_tab VALUES (1,'[{"b": {"x": "y"}, "a": "x"}, null]'); CREATE INVERTED INDEX foo_inv ON json_tab(b); SELECT a FROM json_tab@foo_inv WHERE jsonb_path_exists(b, '$.b'); ``` This previously returned an empty result but should return 1. This commit fixes the issue by adding array-unwrapping for the root key (`$`). Release note (bug fix): Fix JSON path index acceleration with JSON array objects. Only 25.4.0 releases are affected.
1 parent 7a515db commit 430fb70

File tree

4 files changed

+195
-71
lines changed

4 files changed

+195
-71
lines changed

pkg/sql/logictest/testdata/logic_test/jsonb_path_exists_index_acceleration

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,31 @@ SELECT a FROM json_tab@primary WHERE jsonb_path_exists(b, '$.a ? (@.b == $x)', '
557557
statement error index "foo_inv" is inverted and cannot be used for this query
558558
SELECT a FROM json_tab@foo_inv WHERE jsonb_path_exists(b, '$.a ? (@.b == $x)', '{"x": "c"}') ORDER BY a;
559559

560+
statement ok
561+
INSERT INTO json_tab VALUES
562+
(20,'[{"b": {"x": "y"}, "a": "x"}, null]'),
563+
(21,'[[{"b": {"x": "y"}, "a": "x"}], null]');
564+
565+
query I
566+
SELECT a FROM json_tab@primary WHERE jsonb_path_exists(b, '$."b"[*]');
567+
----
568+
20
569+
570+
query I
571+
SELECT a FROM json_tab@foo_inv WHERE jsonb_path_exists(b, '$.b');
572+
----
573+
20
574+
575+
query I
576+
SELECT a FROM json_tab@primary WHERE jsonb_path_exists(b, '$."b"[*]');
577+
----
578+
20
579+
580+
query I
581+
SELECT a FROM json_tab@foo_inv WHERE jsonb_path_exists(b, '$."b"[*]');
582+
----
583+
20
584+
560585
subtest anykey
561586

562587
statement ok

0 commit comments

Comments
 (0)