From f3c78664b0cdff76f1bc25286fb7185a9f496220 Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Mon, 18 Jul 2022 15:37:16 -0700 Subject: [PATCH] rowenc: fix recent bugs with DOidWrappers and inverted indexes Previously, we were using the passed-in datum asserting that it is a `DString` whereas it could be a `DOidWrapper`, and this is now fixed. Release note: None (no stable release with the bugs) --- pkg/sql/logictest/testdata/logic_test/inverted_index | 5 +++++ pkg/sql/rowenc/index_encoding.go | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/inverted_index b/pkg/sql/logictest/testdata/logic_test/inverted_index index 79df6712a0bb..c2525e760bd4 100644 --- a/pkg/sql/logictest/testdata/logic_test/inverted_index +++ b/pkg/sql/logictest/testdata/logic_test/inverted_index @@ -1686,3 +1686,8 @@ SELECT primes FROM cb WHERE primes && numbers ORDER BY primes {2,3} {2,3} {2,3,5} + +# Regression test for incorrectly unwrapping a DOidWrapper (#84569). +statement ok +CREATE TABLE t84569 (name_col NAME NOT NULL, INVERTED INDEX (name_col gin_trgm_ops DESC)); +INSERT INTO t84569 (name_col) VALUES ('X'::NAME) diff --git a/pkg/sql/rowenc/index_encoding.go b/pkg/sql/rowenc/index_encoding.go index 4fefad75c8a7..c08c138a8e5a 100644 --- a/pkg/sql/rowenc/index_encoding.go +++ b/pkg/sql/rowenc/index_encoding.go @@ -587,7 +587,9 @@ func EncodeInvertedIndexTableKeys( // need to pass in the inverted index column kind to this function. // We pad the keys when writing them to the index. // TODO(jordan): why are we doing this padding at all? Postgres does it. - return encodeTrigramInvertedIndexTableKeys(string(*val.(*tree.DString)), inKey, version, true /* pad */) + // val could be a DOidWrapper, so we need to use the unwrapped datum + // here. + return encodeTrigramInvertedIndexTableKeys(string(*datum.(*tree.DString)), inKey, version, true /* pad */) } return nil, errors.AssertionFailedf("trying to apply inverted index to unsupported type %s", datum.ResolvedType()) } @@ -672,7 +674,9 @@ func EncodeExistsInvertedIndexSpans( datum := eval.UnwrapDatum(evalCtx, val) switch val.ResolvedType().Family() { case types.StringFamily: - s := string(*val.(*tree.DString)) + // val could be a DOidWrapper, so we need to use the unwrapped datum + // here. + s := string(*datum.(*tree.DString)) return json.EncodeExistsInvertedIndexSpans(nil /* inKey */, s) case types.ArrayFamily: if val.ResolvedType().ArrayContents().Family() != types.StringFamily {