Skip to content

Commit

Permalink
Merge pull request #116071 from chrisseto/backport23.2-115707
Browse files Browse the repository at this point in the history
release-23.2: sql: prevent creating indexes on arrays of non-indexable types
  • Loading branch information
rafiss committed Dec 26, 2023
2 parents b1b23cd + fe5d17a commit 4ac5719
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
43 changes: 43 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/refcursor
Expand Up @@ -596,6 +596,49 @@ CREATE INDEX ON t112099_no_index (r);
statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor and thus is not indexable
CREATE INDEX ON t112099_no_index (x, r, y);

# Regression test for #115701 - REFCURSOR[] is not a valid index column.
subtest refcursor[]_array_index

statement ok
CREATE TABLE t115701_no_index (x INT NOT NULL, y TEXT NOT NULL, r REFCURSOR[] NOT NULL);

# REFCURSOR[] is not allowed in a primary key.
statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
CREATE TABLE t115701 (r REFCURSOR[] PRIMARY KEY);

statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
CREATE TABLE t115701 (x INT, y TEXT, r REFCURSOR[], PRIMARY KEY (x, r, y));

statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
ALTER TABLE t115701_no_index ADD PRIMARY KEY (r);

statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
ALTER TABLE t115701_no_index ADD PRIMARY KEY (x, r, y);

# REFCURSOR[] is not allowed in a secondary index.
statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
CREATE TABLE t115701 (r REFCURSOR[], INDEX (r));

statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
CREATE TABLE t115701 (x INT, y TEXT, r REFCURSOR[], INDEX (x, r, y));

statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
CREATE INDEX ON t115701_no_index (r);

statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
CREATE INDEX ON t115701_no_index (x, r, y);

statement error pgcode 0A000 pq: column r of type refcursor\[\] is not allowed as the last column in an inverted index
CREATE INDEX ON t115701_no_index USING GIN (r)

skipif config local-legacy-schema-changer
statement error pgcode 0A000 pq: unimplemented: column r is of type refcursor\[\] and thus is not indexable
CREATE INDEX ON t115701_no_index USING GIN (x, r, y gin_trgm_ops)

onlyif config local-legacy-schema-changer
statement error pgcode 0A000 pq: column r of type refcursor\[\] is only allowed as the last column in an inverted index
CREATE INDEX ON t115701_no_index USING GIN (x, r, y gin_trgm_ops)

# REFCURSOR is allowed as a STORING column.
statement ok
CREATE TABLE t112099 (x INT, r REFCURSOR, INDEX (x) STORING (r));
Expand Down
13 changes: 12 additions & 1 deletion pkg/sql/catalog/colinfo/col_type_info.go
Expand Up @@ -150,9 +150,18 @@ func ValidateColumnDefType(ctx context.Context, version clusterversion.Handle, t

// ColumnTypeIsIndexable returns whether the type t is valid as an indexed column.
func ColumnTypeIsIndexable(t *types.T) bool {
// NB: .IsAmbiguous checks the content type of array types.
if t.IsAmbiguous() || t.Family() == types.TupleFamily || t.Family() == types.RefCursorFamily {
return false
}

// If the type is an array, check its content type as well.
if unwrapped := t.ArrayContents(); unwrapped != nil {
if unwrapped.Family() == types.TupleFamily || unwrapped.Family() == types.RefCursorFamily {
return false
}
}

// Some inverted index types also have a key encoding, but we don't
// want to support those yet. See #50659.
return !MustBeValueEncoded(t) && !ColumnTypeIsOnlyInvertedIndexable(t)
Expand All @@ -162,7 +171,9 @@ func ColumnTypeIsIndexable(t *types.T) bool {
// using an inverted index.
func ColumnTypeIsInvertedIndexable(t *types.T) bool {
switch t.Family() {
case types.JsonFamily, types.ArrayFamily, types.StringFamily:
case types.ArrayFamily:
return t.ArrayContents().Family() != types.RefCursorFamily
case types.JsonFamily, types.StringFamily:
return true
}
return ColumnTypeIsOnlyInvertedIndexable(t)
Expand Down

0 comments on commit 4ac5719

Please sign in to comment.