Skip to content

Commit

Permalink
sql/json: fix null array support in inverted index support
Browse files Browse the repository at this point in the history
We assumed all the array elements were strings w/o checking for null,
now we use AsDString to check it.

Found internally with expanded sqlsmith testing.

Fixes: #101025
Epic: None
Release note: None
  • Loading branch information
cucaroach committed Apr 10, 2023
1 parent b02b1b5 commit bc47c8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/json
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,15 @@ SELECT true FROM x WHERE j->'a' @> '2'::JSONB
----
true

query T
SELECT j FROM x WHERE j ?| ARRAY[NULL]
----

query T
SELECT j FROM x WHERE j ?& ARRAY[NULL::STRING]
----
{"a": [1, 2, 3]}

query T
SELECT '{"foo": {"bar": 1}}'::JSONB #- ARRAY['foo', 'bar']
----
Expand Down
7 changes: 5 additions & 2 deletions pkg/sql/rowenc/index_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,11 @@ func EncodeExistsInvertedIndexSpans(
}
var expr inverted.Expression
for _, d := range val.(*tree.DArray).Array {
s := string(*d.(*tree.DString))
newExpr, err := json.EncodeExistsInvertedIndexSpans(nil /* inKey */, s)
ds, ok := tree.AsDString(d)
if !ok {
continue
}
newExpr, err := json.EncodeExistsInvertedIndexSpans(nil /* inKey */, string(ds))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit bc47c8b

Please sign in to comment.