Skip to content

Commit

Permalink
Merge #101178
Browse files Browse the repository at this point in the history
101178: opt: inverted-index accelerate filters of the form j = ... and j IN (...) r=Shivs11 a=Shivs11

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 done completed here: #96471 and #94666

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')`

Co-authored-by: Shivam Saraf <shivam.saraf@cockroachlabs.com>
  • Loading branch information
craig[bot] and Shivs11 committed Apr 14, 2023
2 parents f24b893 + 85400d3 commit 14615c5
Show file tree
Hide file tree
Showing 5 changed files with 1,276 additions and 15 deletions.
86 changes: 86 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/inverted_index
Expand Up @@ -804,6 +804,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 14615c5

Please sign in to comment.