Skip to content

Commit

Permalink
Merge #117846
Browse files Browse the repository at this point in the history
117846: sql: fix trigram inverted index on NAME type r=yuzefovich a=yuzefovich

Previously, we would hit an internal error when trying to use string-like datum of NAME type for the trigram inverted index. This data type is handled via the DOidWrapper around the DString, so we simply need to unwrap that.

I decided to not include a release note given that we've only seen this issue once in our own randomized testing.

Fixes: #117758.

Release note: None

Co-authored-by: Yahor Yuzefovich <[email protected]>
  • Loading branch information
craig[bot] and yuzefovich committed Jan 17, 2024
2 parents dd7d7d1 + a120fff commit 1c67f8e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 23 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/trigram_indexes
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,26 @@ CREATE TABLE public.t_112713 (
)

subtest end

# Regression test for hitting an internal error on string-like datums (e.g NAME).
statement ok
CREATE TABLE t117758 (
col1 VARCHAR NOT NULL,
col2 NAME NOT NULL,
INVERTED INDEX (col2 gin_trgm_ops)
);
SELECT
tab.col1_1
FROM
t117758 AS tab2
JOIN (
SELECT
'foo'::NAME, 'bar'::NAME
FROM
t117758 AS tab3
JOIN t117758 AS tab4 ON
(tab3.col2) = (tab4.col2)
)
AS tab (col1_1, col1_2) ON
(tab2.col1) = (tab.col1_1)
AND (tab2.col2) = (tab.col1_2);
8 changes: 4 additions & 4 deletions pkg/sql/opt/invertedidx/trigram.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/errors"
)

Expand Down Expand Up @@ -95,13 +94,14 @@ func (t *trigramFilterPlanner) extractInvertedFilterConditionFromLeaf(
// Can only accelerate with a single constant value.
return inverted.NonInvertedColExpression{}, expr, nil
}
d := memo.ExtractConstDatum(constantVal)
if d.ResolvedType() != types.String {
d := tree.UnwrapDOidWrapper(memo.ExtractConstDatum(constantVal))
ds, ok := d.(*tree.DString)
if !ok {
panic(errors.AssertionFailedf(
"trying to apply inverted index to unsupported type %s", d.ResolvedType().SQLStringForError(),
))
}
s := string(*d.(*tree.DString))
s := string(*ds)
var err error
invertedExpr, err = rowenc.EncodeTrigramSpans(s, allMustMatch)
if err != nil {
Expand Down

0 comments on commit 1c67f8e

Please sign in to comment.