From bc47c8b3901aa274bcacaa6c0579ea378cbbb790 Mon Sep 17 00:00:00 2001 From: Tommy Reilly Date: Mon, 10 Apr 2023 09:43:01 -0400 Subject: [PATCH] sql/json: fix null array support in inverted index support 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 --- pkg/sql/logictest/testdata/logic_test/json | 9 +++++++++ pkg/sql/rowenc/index_encoding.go | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/json b/pkg/sql/logictest/testdata/logic_test/json index da291dc6de93..6f645ebb22af 100644 --- a/pkg/sql/logictest/testdata/logic_test/json +++ b/pkg/sql/logictest/testdata/logic_test/json @@ -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'] ---- diff --git a/pkg/sql/rowenc/index_encoding.go b/pkg/sql/rowenc/index_encoding.go index 033ba11c6cfe..2abddbf8a37e 100644 --- a/pkg/sql/rowenc/index_encoding.go +++ b/pkg/sql/rowenc/index_encoding.go @@ -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 }