Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix trigram inverted index on NAME type #117846

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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