Skip to content

Commit

Permalink
opt: inverted-index accelerate filters of the form j = ... and j IN (…
Browse files Browse the repository at this point in the history
…...)

An inverted index can be used to accelerate queries, using the = and the IN
operator, with filters having the fetch value operator present; eg: j->0  = '{"b": "c"}'
and j->0 IN ('1', '2') where j is a JSON column. This has been completed here:

This PR aims to add support to generate inverted spans for queries not involving the
fetch val operator and having the `=` operator or the `IN` operator. This was done
by creating JSON objects from the JSON values present on the right side of the
equality or the IN expression. Moreover, in this case, no "keys" were extracted
from the left hand side of the operators while creating these JSON objects which
were useful for creating the inverted spans.

Epic: CRDB-3301

Fixes: #96658

Release note (performance improvement): The optimizer now plans
inverted index scans for queries that do not use the JSON fetch value
operator (`->`) alongside the `IN` and the `=` operators.
eg: json_col = '{"b": "c"}' OR json_col IN ('"a"', '1')
  • Loading branch information
Shivs11 committed Apr 11, 2023
1 parent b36c119 commit de6b2eb
Show file tree
Hide file tree
Showing 5 changed files with 1,277 additions and 15 deletions.
86 changes: 86 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/inverted_index
Expand Up @@ -812,6 +812,92 @@ INSERT INTO f VALUES
(42, '{"a": "a"}'),
(43, '[[0, 1, 2], [0, 1, 2], "s"]')

# Testing the equality operator without the fetch value
# operator on an inverted index.

query T
SELECT j FROM f@i WHERE j = '[[0, 1, 2]]' ORDER BY k
----
[[0, 1, 2]]

query T
SELECT j FROM f@i WHERE j = '{"a": "a"}' ORDER BY k
----
{"a": "a"}

query T
SELECT j FROM f@i WHERE j = '{"b": 2, "a": 1}' ORDER BY k
----
{"a": 1, "b": 2}

query T
SELECT j FROM f@i WHERE j = '{"a": {"b": "c"}, "d": "e"}' ORDER BY k
----
{"a": {"b": "c"}, "d": "e"}

query T
SELECT j FROM f@i WHERE j = 'null' ORDER BY k
----
null

query T
SELECT j FROM f@i WHERE j = '[]' ORDER BY k
----
[]

query T
SELECT j FROM f@i WHERE j = '{}' ORDER BY k
----
{}

query T
SELECT j FROM f@i WHERE j = '1' ORDER BY k
----

query T
SELECT j FROM f@i WHERE j = '[[]]' ORDER BY k
----
[[]]

query T
SELECT j FROM f@i WHERE j = '[[0, 1, 2], [0, 1, 2], "s"]' ORDER BY k
----
[[0, 1, 2], [0, 1, 2], "s"]

# Testing the IN operator without the fetch value operator on
# an inverted index.

query T
SELECT j FROM f@i WHERE j IN ('{}', '[]', 'null') ORDER BY k
----
null
{}
[]

query T
SELECT j FROM f@i WHERE j IN ('[1]') ORDER BY k
----

query T
SELECT j FROM f@i WHERE j IN ('{"a": "b", "x": ["c", "d", "e"]}', '{}') ORDER BY k
----
{"a": "b", "x": ["c", "d", "e"]}
{}

query T
SELECT j FROM f@i WHERE j IN ('{"a": []}', '{"a": {}}') ORDER BY k
----
{"a": []}
{"a": {}}

query T
SELECT j FROM f@i WHERE j IN ('{"a": [1, 2, null]}', '[[]]', '[[{"a": {"b": []}}]]')
ORDER BY k
----
{"a": [1, 2, null]}
[[]]
[[{"a": {"b": []}}]]

query T
SELECT j FROM f@i WHERE j->0 @> '[0, 1, 2, 3]' ORDER BY k
----
Expand Down

0 comments on commit de6b2eb

Please sign in to comment.