diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 9f95e75787c2..a82326df470f 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -594,6 +594,7 @@ ALL_TESTS = [ "//pkg/sql/sem/eval/eval_test:eval_test_test", "//pkg/sql/sem/eval:eval_disallowed_imports_test", "//pkg/sql/sem/eval:eval_test", + "//pkg/sql/sem/idxtype:idxtype_disallowed_imports_test", "//pkg/sql/sem/normalize:normalize_test", "//pkg/sql/sem/tree:tree_disallowed_imports_test", "//pkg/sql/sem/tree:tree_test", @@ -2211,6 +2212,8 @@ GO_TARGETS = [ "//pkg/sql/sem/eval/eval_test:eval_test_test", "//pkg/sql/sem/eval:eval", "//pkg/sql/sem/eval:eval_test", + "//pkg/sql/sem/idxtype:idxtype", + "//pkg/sql/sem/idxtype:idxtypepb", "//pkg/sql/sem/normalize:normalize", "//pkg/sql/sem/normalize:normalize_test", "//pkg/sql/sem/plpgsqltree/utils:utils", diff --git a/pkg/crosscluster/logical/BUILD.bazel b/pkg/crosscluster/logical/BUILD.bazel index d42fd7152436..02550167bee6 100644 --- a/pkg/crosscluster/logical/BUILD.bazel +++ b/pkg/crosscluster/logical/BUILD.bazel @@ -156,6 +156,7 @@ go_test( "//pkg/sql/randgen", "//pkg/sql/rowenc", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/stats", "//pkg/testutils", diff --git a/pkg/crosscluster/logical/logical_replication_job_test.go b/pkg/crosscluster/logical/logical_replication_job_test.go index a1e68110a179..9fb5ef74ee3a 100644 --- a/pkg/crosscluster/logical/logical_replication_job_test.go +++ b/pkg/crosscluster/logical/logical_replication_job_test.go @@ -34,7 +34,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/security/username" "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/execinfra" "github.com/cockroachdb/cockroach/pkg/sql/isql" @@ -42,6 +41,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/randgen" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/stats" "github.com/cockroachdb/cockroach/pkg/testutils" @@ -1491,7 +1491,7 @@ func compareReplicatedTables( descB := desctestutils.TestingGetPublicTableDescriptor(s.DB(), s.Codec(), dbB, tableName) for _, indexA := range descA.AllIndexes() { - if indexA.GetType() == descpb.IndexDescriptor_INVERTED { + if indexA.GetType() == idxtype.INVERTED { t.Logf("skipping fingerprinting of inverted index %s", indexA.GetName()) continue } diff --git a/pkg/gen/protobuf.bzl b/pkg/gen/protobuf.bzl index 6caeea0af082..db191681e922 100644 --- a/pkg/gen/protobuf.bzl +++ b/pkg/gen/protobuf.bzl @@ -70,6 +70,7 @@ PROTOBUF_SRCS = [ "//pkg/sql/protoreflect/test:protoreflecttest_go_proto", "//pkg/sql/rowenc/rowencpb:rowencpb_go_proto", "//pkg/sql/schemachanger/scpb:scpb_go_proto", + "//pkg/sql/sem/idxtype:idxtype_go_proto", "//pkg/sql/sem/semenumpb:semenumpb_go_proto", "//pkg/sql/sessiondatapb:sessiondatapb_go_proto", "//pkg/sql/sqlstats/insights:insights_go_proto", diff --git a/pkg/internal/sqlsmith/BUILD.bazel b/pkg/internal/sqlsmith/BUILD.bazel index b73a4cc0199a..a990f0941eef 100644 --- a/pkg/internal/sqlsmith/BUILD.bazel +++ b/pkg/internal/sqlsmith/BUILD.bazel @@ -29,6 +29,7 @@ go_library( "//pkg/sql/sem/cast", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/plpgsqltree", "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treebin", diff --git a/pkg/internal/sqlsmith/alter.go b/pkg/internal/sqlsmith/alter.go index 0629574dbae4..a4906475f308 100644 --- a/pkg/internal/sqlsmith/alter.go +++ b/pkg/internal/sqlsmith/alter.go @@ -13,6 +13,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/randgen" "github.com/cockroachdb/cockroach/pkg/sql/sem/cast" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treebin" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -340,7 +341,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) { } var cols tree.IndexElemList seen := map[tree.Name]bool{} - indexType := tree.IndexTypeForward + indexType := idxtype.FORWARD unique := s.coin() for len(cols) < 1 || s.coin() { col := tableRef.Columns[s.rnd.Intn(len(tableRef.Columns))] @@ -351,7 +352,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) { // If this is the first column and it's invertible (i.e., JSONB), make an inverted index. if len(cols) == 0 && colinfo.ColumnTypeIsOnlyInvertedIndexable(tree.MustBeStaticallyKnownType(col.Type)) { - indexType = tree.IndexTypeInverted + indexType = idxtype.INVERTED unique = false cols = append(cols, tree.IndexElem{ Column: col.Name, @@ -366,7 +367,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) { } } var storing tree.NameList - for indexType == tree.IndexTypeForward && s.coin() { + for indexType.SupportsStoring() && s.coin() { col := tableRef.Columns[s.rnd.Intn(len(tableRef.Columns))] if seen[col.Name] { continue diff --git a/pkg/internal/sqlsmith/schema.go b/pkg/internal/sqlsmith/schema.go index 6f5a124d03e3..9fad2d6f842c 100644 --- a/pkg/internal/sqlsmith/schema.go +++ b/pkg/internal/sqlsmith/schema.go @@ -18,6 +18,7 @@ import ( _ "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treebin" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -184,7 +185,7 @@ func (s *Smither) getRandTable() (*aliasedTableRef, bool) { var indexFlags tree.IndexFlags indexNames := make([]tree.Name, 0, len(indexes)) for _, index := range indexes { - if index.Type == tree.IndexTypeForward { + if index.Type == idxtype.FORWARD { indexNames = append(indexNames, index.Name) } } @@ -499,9 +500,9 @@ func (s *Smither) extractIndexes( return nil, err } if _, ok := indexes[idx]; !ok { - indexType := tree.IndexTypeForward + indexType := idxtype.FORWARD if inverted { - indexType = tree.IndexTypeInverted + indexType = idxtype.INVERTED } indexes[idx] = &tree.CreateIndex{ Name: idx, diff --git a/pkg/protos.bzl b/pkg/protos.bzl index 08e7cb1f951d..6248b7b2dcde 100644 --- a/pkg/protos.bzl +++ b/pkg/protos.bzl @@ -35,6 +35,7 @@ SERVER_PROTOS = [ "//pkg/sql/contentionpb:contentionpb_proto", "//pkg/sql/lex:lex_proto", "//pkg/sql/schemachanger/scpb:scpb_proto", + "//pkg/sql/sem/idxtype:idxtype_proto", "//pkg/sql/sem/semenumpb:semenumpb_proto", "//pkg/sql/sessiondatapb:sessiondatapb_proto", "//pkg/sql/sqlstats/insights:insights_proto", @@ -123,6 +124,7 @@ PROTO_FILES = [ "//pkg/sql/lex:encode.proto", "//pkg/sql/schemachanger/scpb:elements.proto", "//pkg/sql/schemachanger/scpb:scpb.proto", + "//pkg/sql/sem/idxtype:idxtype.proto", "//pkg/sql/sem/semenumpb:constraint.proto", "//pkg/sql/sem/semenumpb:trigger.proto", "//pkg/sql/sessiondatapb:local_only_session_data.proto", diff --git a/pkg/sql/BUILD.bazel b/pkg/sql/BUILD.bazel index 5b7f7b68fb6a..b44d5f7f1440 100644 --- a/pkg/sql/BUILD.bazel +++ b/pkg/sql/BUILD.bazel @@ -497,6 +497,7 @@ go_library( "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/plpgsqltree", "//pkg/sql/sem/semenumpb", "//pkg/sql/sem/transform", @@ -878,6 +879,7 @@ go_test( "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sessiondata", "//pkg/sql/sessiondatapb", diff --git a/pkg/sql/alter_primary_key.go b/pkg/sql/alter_primary_key.go index 071bc53aaa93..0959521c5842 100644 --- a/pkg/sql/alter_primary_key.go +++ b/pkg/sql/alter_primary_key.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" @@ -178,7 +179,7 @@ func (p *planner) AlterPrimaryKey( Unique: true, CreatedExplicitly: true, EncodingType: catenumpb.PrimaryIndexEncoding, - Type: descpb.IndexDescriptor_FORWARD, + Type: idxtype.FORWARD, // TODO(postamar): bump version to LatestIndexDescriptorVersion in 22.2 // This is not possible until then because of a limitation in 21.2 which // affects mixed-21.2-22.1-version clusters (issue #78426). @@ -433,7 +434,7 @@ func (p *planner) AlterPrimaryKey( return true, nil } - return !idx.IsUnique() || idx.GetType() == descpb.IndexDescriptor_INVERTED, nil + return !idx.IsUnique() || idx.GetType() == idxtype.INVERTED, nil } var indexesToRewrite []catalog.Index for _, idx := range tableDesc.PublicNonPrimaryIndexes() { @@ -809,7 +810,7 @@ func setKeySuffixAndStoredColumnIDsFromPrimary( // Second, determine the key suffix columns: add all primary key columns // which have not already been in the key columns in the secondary index. toAdd.KeySuffixColumnIDs = nil - invIdx := toAdd.Type == descpb.IndexDescriptor_INVERTED + invIdx := toAdd.Type == idxtype.INVERTED for _, colID := range primary.KeyColumnIDs { if !idxColIDs.Contains(colID) { toAdd.KeySuffixColumnIDs = append(toAdd.KeySuffixColumnIDs, colID) diff --git a/pkg/sql/alter_table.go b/pkg/sql/alter_table.go index c14f661a9960..e092e8396312 100644 --- a/pkg/sql/alter_table.go +++ b/pkg/sql/alter_table.go @@ -33,6 +33,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/semenumpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/volatility" @@ -274,7 +275,7 @@ func (n *alterTableNode) startExec(params runParams) error { n.tableDesc, tableName, columns, - tree.IndexTypeForward, + idxtype.FORWARD, false, /* isNewTable */ params.p.SemaCtx(), params.ExecCfg().Settings.Version.ActiveVersion(params.ctx), diff --git a/pkg/sql/backfill.go b/pkg/sql/backfill.go index d31b27dc600a..eae47f70fd13 100644 --- a/pkg/sql/backfill.go +++ b/pkg/sql/backfill.go @@ -39,6 +39,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" @@ -1466,9 +1467,9 @@ func (sc *SchemaChanger) validateIndexes(ctx context.Context) error { continue } switch idx.GetType() { - case descpb.IndexDescriptor_FORWARD: + case idxtype.FORWARD: forwardIndexes = append(forwardIndexes, idx) - case descpb.IndexDescriptor_INVERTED: + case idxtype.INVERTED: invertedIndexes = append(invertedIndexes, idx) } } diff --git a/pkg/sql/backfill/BUILD.bazel b/pkg/sql/backfill/BUILD.bazel index ca6955fa06c6..4de03dd1624b 100644 --- a/pkg/sql/backfill/BUILD.bazel +++ b/pkg/sql/backfill/BUILD.bazel @@ -33,6 +33,7 @@ go_library( "//pkg/sql/rowinfra", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/transform", "//pkg/sql/sem/tree", "//pkg/sql/sqlerrors", diff --git a/pkg/sql/backfill/backfill.go b/pkg/sql/backfill/backfill.go index f0fa56a05a13..6172001376c5 100644 --- a/pkg/sql/backfill/backfill.go +++ b/pkg/sql/backfill/backfill.go @@ -29,6 +29,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/rowinfra" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/transform" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" @@ -486,7 +487,7 @@ type IndexBackfiller struct { // ContainsInvertedIndex returns true if backfilling an inverted index. func (ib *IndexBackfiller) ContainsInvertedIndex() bool { for _, idx := range ib.added { - if idx.GetType() == descpb.IndexDescriptor_INVERTED { + if idx.GetType() == idxtype.INVERTED { return true } } diff --git a/pkg/sql/catalog/BUILD.bazel b/pkg/sql/catalog/BUILD.bazel index 100ba702ef71..023e720a54c5 100644 --- a/pkg/sql/catalog/BUILD.bazel +++ b/pkg/sql/catalog/BUILD.bazel @@ -40,6 +40,7 @@ go_library( "//pkg/sql/privilege", "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/catconstants", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/semenumpb", "//pkg/sql/sem/tree", "//pkg/sql/sessiondatapb", diff --git a/pkg/sql/catalog/catformat/BUILD.bazel b/pkg/sql/catalog/catformat/BUILD.bazel index a675832a3ce7..fe10da4768ad 100644 --- a/pkg/sql/catalog/catformat/BUILD.bazel +++ b/pkg/sql/catalog/catformat/BUILD.bazel @@ -13,6 +13,7 @@ go_library( "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/schemaexpr", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sessiondata", "@com_github_cockroachdb_errors//:errors", @@ -31,6 +32,7 @@ go_test( "//pkg/sql/catalog/tabledesc", "//pkg/sql/sem/catconstants", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sessiondata", "//pkg/sql/types", diff --git a/pkg/sql/catalog/catformat/index.go b/pkg/sql/catalog/catformat/index.go index e81d3f602e74..b47c2d9c5d61 100644 --- a/pkg/sql/catalog/catformat/index.go +++ b/pkg/sql/catalog/catformat/index.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/errors" @@ -101,7 +102,7 @@ func indexForDisplay( if index.Unique { f.WriteString("UNIQUE ") } - if !f.HasFlags(tree.FmtPGCatalog) && index.Type == descpb.IndexDescriptor_INVERTED { + if !f.HasFlags(tree.FmtPGCatalog) && index.Type == idxtype.INVERTED { f.WriteString("INVERTED ") } f.WriteString("INDEX ") @@ -113,7 +114,7 @@ func indexForDisplay( if f.HasFlags(tree.FmtPGCatalog) { f.WriteString(" USING") - if index.Type == descpb.IndexDescriptor_INVERTED { + if index.Type == idxtype.INVERTED { f.WriteString(" gin") } else { f.WriteString(" btree") @@ -239,7 +240,7 @@ func FormatIndexElements( } else { f.FormatNameP(&index.KeyColumnNames[i]) } - if index.Type == descpb.IndexDescriptor_INVERTED && + if index.Type == idxtype.INVERTED && col.GetID() == index.InvertedColumnID() && len(index.InvertedColumnKinds) > 0 { switch index.InvertedColumnKinds[0] { case catpb.InvertedIndexColumnKind_TRIGRAM: @@ -249,7 +250,7 @@ func FormatIndexElements( // The last column of an inverted index cannot have a DESC direction. // Since the default direction is ASC, we omit the direction entirely // for inverted index columns. - if i < n-1 || index.Type != descpb.IndexDescriptor_INVERTED { + if i < n-1 || index.Type != idxtype.INVERTED { f.WriteByte(' ') f.WriteString(index.KeyColumnDirections[i].String()) } diff --git a/pkg/sql/catalog/catformat/index_test.go b/pkg/sql/catalog/catformat/index_test.go index 91e78bcfa25d..22b67c65b5b4 100644 --- a/pkg/sql/catalog/catformat/index_test.go +++ b/pkg/sql/catalog/catformat/index_test.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -81,7 +82,7 @@ func TestIndexForDisplay(t *testing.T) { // JSONB INVERTED INDEX baz (a) jsonbInvertedIndex := baseIndex - jsonbInvertedIndex.Type = descpb.IndexDescriptor_INVERTED + jsonbInvertedIndex.Type = idxtype.INVERTED jsonbInvertedIndex.KeyColumnNames = []string{"a"} jsonbInvertedIndex.KeyColumnIDs = descpb.ColumnIDs{1} diff --git a/pkg/sql/catalog/descpb/BUILD.bazel b/pkg/sql/catalog/descpb/BUILD.bazel index 0ff4092f54d7..b920732ade1c 100644 --- a/pkg/sql/catalog/descpb/BUILD.bazel +++ b/pkg/sql/catalog/descpb/BUILD.bazel @@ -26,6 +26,7 @@ go_library( "//pkg/sql/protoreflect", "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util/errorutil/unimplemented", @@ -60,6 +61,7 @@ proto_library( "//pkg/sql/catalog/catenumpb:catenumpb_proto", "//pkg/sql/catalog/catpb:catpb_proto", "//pkg/sql/schemachanger/scpb:scpb_proto", + "//pkg/sql/sem/idxtype:idxtype_proto", "//pkg/sql/sem/semenumpb:semenumpb_proto", "//pkg/sql/types:types_proto", "//pkg/util/hlc:hlc_proto", @@ -80,6 +82,7 @@ go_proto_library( "//pkg/sql/catalog/catenumpb", "//pkg/sql/catalog/catpb", "//pkg/sql/schemachanger/scpb", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/semenumpb", "//pkg/sql/types", "//pkg/util/hlc", diff --git a/pkg/sql/catalog/descpb/index.go b/pkg/sql/catalog/descpb/index.go index 43cfcb5a9fd6..49c01f8f21ed 100644 --- a/pkg/sql/catalog/descpb/index.go +++ b/pkg/sql/catalog/descpb/index.go @@ -9,6 +9,7 @@ import ( "fmt" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/errors" @@ -99,7 +100,7 @@ func (desc *IndexDescriptor) GetName() string { // index. This is always the last column in ColumnIDs. Panics if the index is // not inverted. func (desc *IndexDescriptor) InvertedColumnID() ColumnID { - if desc.Type != IndexDescriptor_INVERTED { + if desc.Type != idxtype.INVERTED { panic(errors.AssertionFailedf("index is not inverted")) } return desc.KeyColumnIDs[len(desc.KeyColumnIDs)-1] @@ -109,7 +110,7 @@ func (desc *IndexDescriptor) InvertedColumnID() ColumnID { // index. This is always the last column in KeyColumnNames. Panics if the index is // not inverted. func (desc *IndexDescriptor) InvertedColumnName() string { - if desc.Type != IndexDescriptor_INVERTED { + if desc.Type != idxtype.INVERTED { panic(errors.AssertionFailedf("index is not inverted")) } return desc.KeyColumnNames[len(desc.KeyColumnNames)-1] @@ -120,7 +121,7 @@ func (desc *IndexDescriptor) InvertedColumnName() string { // // Panics if the index is not inverted. func (desc *IndexDescriptor) InvertedColumnKeyType() *types.T { - if desc.Type != IndexDescriptor_INVERTED { + if desc.Type != idxtype.INVERTED { panic(errors.AssertionFailedf("index is not inverted")) } return types.EncodedKey diff --git a/pkg/sql/catalog/descpb/structured.proto b/pkg/sql/catalog/descpb/structured.proto index 30dc2cebc0b6..afe66646f274 100644 --- a/pkg/sql/catalog/descpb/structured.proto +++ b/pkg/sql/catalog/descpb/structured.proto @@ -13,6 +13,7 @@ import "util/hlc/timestamp.proto"; import "sql/catalog/catenumpb/index.proto"; import "sql/catalog/catpb/catalog.proto"; import "sql/catalog/catpb/enum.proto"; +import "sql/sem/idxtype/idxtype.proto"; import "sql/sem/semenumpb/constraint.proto"; import "sql/sem/semenumpb/trigger.proto"; import "sql/catalog/catpb/privilege.proto"; @@ -322,12 +323,6 @@ message InterleaveDescriptor { message IndexDescriptor { option (gogoproto.equal) = true; - // The type of the index. - enum Type { - FORWARD = 0; - INVERTED = 1; - } - optional string name = 1 [(gogoproto.nullable) = false]; optional uint32 id = 2 [(gogoproto.nullable) = false, (gogoproto.customname) = "ID", (gogoproto.casttype) = "IndexID"]; @@ -421,8 +416,8 @@ message IndexDescriptor { // is partitioned into spans of keys each addressable by zone configs. optional cockroach.sql.catalog.catpb.PartitioningDescriptor partitioning = 15 [(gogoproto.nullable) = false]; - // Type is the type of index, inverted or forward. - optional Type type = 16 [(gogoproto.nullable)=false]; + // Type is the type of index, forward, inverted, vector, etc. + optional cockroach.sql.sem.idxtype.T type = 16 [(gogoproto.nullable)=false]; // CreatedExplicitly specifies whether this index was created explicitly // (i.e. via 'CREATE INDEX' statement). diff --git a/pkg/sql/catalog/systemschema/BUILD.bazel b/pkg/sql/catalog/systemschema/BUILD.bazel index 719196afc07b..3511cffc0991 100644 --- a/pkg/sql/catalog/systemschema/BUILD.bazel +++ b/pkg/sql/catalog/systemschema/BUILD.bazel @@ -18,6 +18,7 @@ go_library( "//pkg/sql/catalog/tabledesc", "//pkg/sql/privilege", "//pkg/sql/sem/catconstants", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util/log", diff --git a/pkg/sql/catalog/systemschema/system.go b/pkg/sql/catalog/systemschema/system.go index c2ff278c62f5..960045ac4ce0 100644 --- a/pkg/sql/catalog/systemschema/system.go +++ b/pkg/sql/catalog/systemschema/system.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/log" @@ -3168,7 +3169,7 @@ var ( KeyColumnIDs: []descpb.ColumnID{13}, KeySuffixColumnIDs: []descpb.ColumnID{11, 1, 2, 3, 4, 5, 6}, Version: descpb.StrictIndexColumnIDGuaranteesVersion, - Type: descpb.IndexDescriptor_INVERTED, + Type: idxtype.INVERTED, InvertedColumnKinds: []catpb.InvertedIndexColumnKind{catpb.InvertedIndexColumnKind_DEFAULT}, }, descpb.IndexDescriptor{ @@ -5189,7 +5190,7 @@ var ( }, descpb.IndexDescriptor{ Name: "db_name_gin", - Type: descpb.IndexDescriptor_INVERTED, + Type: idxtype.INVERTED, ID: 8, Unique: false, Version: descpb.StrictIndexColumnIDGuaranteesVersion, @@ -5201,7 +5202,7 @@ var ( }, descpb.IndexDescriptor{ Name: "table_name_gin", - Type: descpb.IndexDescriptor_INVERTED, + Type: idxtype.INVERTED, ID: 9, Unique: false, Version: descpb.StrictIndexColumnIDGuaranteesVersion, @@ -5213,7 +5214,7 @@ var ( }, descpb.IndexDescriptor{ Name: "schema_name_gin", - Type: descpb.IndexDescriptor_INVERTED, + Type: idxtype.INVERTED, ID: 10, Unique: false, Version: descpb.StrictIndexColumnIDGuaranteesVersion, @@ -5225,7 +5226,7 @@ var ( }, descpb.IndexDescriptor{ Name: "store_ids_gin", - Type: descpb.IndexDescriptor_INVERTED, + Type: idxtype.INVERTED, ID: 11, Unique: false, Version: descpb.StrictIndexColumnIDGuaranteesVersion, diff --git a/pkg/sql/catalog/table_elements.go b/pkg/sql/catalog/table_elements.go index f445cb683d2c..b0fcd25f73e9 100644 --- a/pkg/sql/catalog/table_elements.go +++ b/pkg/sql/catalog/table_elements.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/semenumpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -171,7 +172,7 @@ type Index interface { IsCreatedExplicitly() bool GetInvisibility() float64 GetPredicate() string - GetType() descpb.IndexDescriptor_Type + GetType() idxtype.T GetGeoConfig() geopb.Config GetVersion() descpb.IndexDescriptorVersion GetEncodingType() catenumpb.IndexDescriptorEncodingType diff --git a/pkg/sql/catalog/tabledesc/BUILD.bazel b/pkg/sql/catalog/tabledesc/BUILD.bazel index 29415885c6e7..3e09efc8f559 100644 --- a/pkg/sql/catalog/tabledesc/BUILD.bazel +++ b/pkg/sql/catalog/tabledesc/BUILD.bazel @@ -53,6 +53,7 @@ go_library( "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/semenumpb", "//pkg/sql/sem/tree", "//pkg/sql/sem/volatility", @@ -118,6 +119,7 @@ go_test( "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/semenumpb", "//pkg/sql/types", "//pkg/testutils", diff --git a/pkg/sql/catalog/tabledesc/column.go b/pkg/sql/catalog/tabledesc/column.go index bc2f645cd3ed..e29e9e2a07e0 100644 --- a/pkg/sql/catalog/tabledesc/column.go +++ b/pkg/sql/catalog/tabledesc/column.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/fetchpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr" "github.com/cockroachdb/cockroach/pkg/sql/parser" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/protoutil" @@ -436,7 +437,7 @@ func makeIndexColumnCache(idx *descpb.IndexDescriptor, all []catalog.Column) (ic // code needs to tolerate any descriptor state (like having no key columns, or // having uninitialized column IDs). var invertedColumnID descpb.ColumnID - if nKey > 0 && idx.Type == descpb.IndexDescriptor_INVERTED { + if nKey > 0 && idx.Type == idxtype.INVERTED { invertedColumnID = idx.InvertedColumnID() } var compositeIDs catalog.TableColSet diff --git a/pkg/sql/catalog/tabledesc/index.go b/pkg/sql/catalog/tabledesc/index.go index ce44ea649c4d..f7b7c0f82944 100644 --- a/pkg/sql/catalog/tabledesc/index.go +++ b/pkg/sql/catalog/tabledesc/index.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/catenumpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/iterutil" "github.com/cockroachdb/cockroach/pkg/util/protoutil" @@ -126,7 +127,7 @@ func (w index) GetPredicate() string { } // GetType returns the type of index, inverted or forward. -func (w index) GetType() descpb.IndexDescriptor_Type { +func (w index) GetType() idxtype.T { return w.desc.Type } @@ -211,7 +212,7 @@ func (w index) InvertedColumnKeyType() *types.T { // // Panics if the index is not inverted. func (w index) InvertedColumnKind() catpb.InvertedIndexColumnKind { - if w.desc.Type != descpb.IndexDescriptor_INVERTED { + if w.desc.Type != idxtype.INVERTED { panic(errors.AssertionFailedf("index is not inverted")) } if len(w.desc.InvertedColumnKinds) == 0 { diff --git a/pkg/sql/catalog/tabledesc/index_test.go b/pkg/sql/catalog/tabledesc/index_test.go index 3ec271ec3228..1dd2059142c2 100644 --- a/pkg/sql/catalog/tabledesc/index_test.go +++ b/pkg/sql/catalog/tabledesc/index_test.go @@ -27,6 +27,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -271,7 +272,7 @@ func TestIndexInterface(t *testing.T) { errMsgFmt, "GetPredicate", idx.GetName()) require.Equal(t, idx == s5 || idx == pk, idx.IsUnique(), errMsgFmt, "IsUnique", idx.GetName()) - require.Equal(t, idx == s2 || idx == s6, idx.GetType() == descpb.IndexDescriptor_INVERTED, + require.Equal(t, idx == s2 || idx == s6, idx.GetType() == idxtype.INVERTED, errMsgFmt, "GetType", idx.GetName()) require.Equal(t, idx == s4, idx.IsSharded(), errMsgFmt, "IsSharded", idx.GetName()) diff --git a/pkg/sql/catalog/tabledesc/structured.go b/pkg/sql/catalog/tabledesc/structured.go index fc61e24d4571..5b2d46c7f2d8 100644 --- a/pkg/sql/catalog/tabledesc/structured.go +++ b/pkg/sql/catalog/tabledesc/structured.go @@ -30,6 +30,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/rowenc" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -790,7 +791,7 @@ func (desc *Mutable) allocateIndexIDs(columnNames map[string]descpb.ColumnID) er // index encoding. It is the set difference of the primary key minus the // non-inverted columns in the index's key. colIDs := idx.CollectKeyColumnIDs() - isInverted := idx.GetType() == descpb.IndexDescriptor_INVERTED + isInverted := idx.GetType() == idxtype.INVERTED invID := catid.ColumnID(0) if isInverted { invID = idx.InvertedColumnID() @@ -1186,7 +1187,7 @@ func (desc *Mutable) AddFamily(fam descpb.ColumnFamilyDescriptor) { // AddPrimaryIndex adds a primary index to a mutable table descriptor, assuming // that none has yet been set, and performs some sanity checks. func (desc *Mutable) AddPrimaryIndex(idx descpb.IndexDescriptor) error { - if idx.Type == descpb.IndexDescriptor_INVERTED { + if idx.Type == idxtype.INVERTED { return fmt.Errorf("primary index cannot be inverted") } if err := checkColumnsValidForIndex(desc, idx.KeyColumnNames); err != nil { @@ -1226,7 +1227,7 @@ func (desc *Mutable) AddPrimaryIndex(idx descpb.IndexDescriptor) error { // AddSecondaryIndex adds a secondary index to a mutable table descriptor. func (desc *Mutable) AddSecondaryIndex(idx descpb.IndexDescriptor) error { - if idx.Type == descpb.IndexDescriptor_FORWARD { + if idx.Type == idxtype.FORWARD { if err := checkColumnsValidForIndex(desc, idx.KeyColumnNames); err != nil { return err } @@ -2084,11 +2085,11 @@ func (desc *Mutable) AddIndexMutation( func (desc *Mutable) checkValidIndex(idx *descpb.IndexDescriptor) error { switch idx.Type { - case descpb.IndexDescriptor_FORWARD: + case idxtype.FORWARD: if err := checkColumnsValidForIndex(desc, idx.KeyColumnNames); err != nil { return err } - case descpb.IndexDescriptor_INVERTED: + case idxtype.INVERTED: if err := checkColumnsValidForInvertedIndex( desc, idx.KeyColumnNames, idx.KeyColumnDirections, ); err != nil { diff --git a/pkg/sql/catalog/tabledesc/table_desc_builder.go b/pkg/sql/catalog/tabledesc/table_desc_builder.go index 0bfbf7b80273..e55c3b894141 100644 --- a/pkg/sql/catalog/tabledesc/table_desc_builder.go +++ b/pkg/sql/catalog/tabledesc/table_desc_builder.go @@ -22,6 +22,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/intsets" @@ -842,7 +843,7 @@ func maybeUpgradePrimaryIndexFormatVersion(builder *tableDescriptorBuilder) (has func maybeUpgradeSecondaryIndexFormatVersion(idx *descpb.IndexDescriptor) (hasChanged bool) { switch idx.Version { case descpb.SecondaryIndexFamilyFormatVersion: - if idx.Type == descpb.IndexDescriptor_INVERTED { + if idx.Type == idxtype.INVERTED { return false } case descpb.EmptyArraysInInvertedIndexesVersion: diff --git a/pkg/sql/colenc/BUILD.bazel b/pkg/sql/colenc/BUILD.bazel index 0660312e1593..0ef565fb1a00 100644 --- a/pkg/sql/colenc/BUILD.bazel +++ b/pkg/sql/colenc/BUILD.bazel @@ -29,6 +29,7 @@ go_library( "//pkg/sql/rowenc/valueside", "//pkg/sql/rowinfra", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sqlerrors", "//pkg/sql/types", diff --git a/pkg/sql/colenc/encode.go b/pkg/sql/colenc/encode.go index 318d12fe27f4..92f71a5552ce 100644 --- a/pkg/sql/colenc/encode.go +++ b/pkg/sql/colenc/encode.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/rowenc/valueside" "github.com/cockroachdb/cockroach/pkg/sql/rowinfra" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -427,7 +428,7 @@ func (b *BatchEncoder) encodeSecondaryIndex(ctx context.Context, ind catalog.Ind // Store nulls we encounter so we can properly make the key unique below. var nulls coldata.Nulls - if ind.GetType() == descpb.IndexDescriptor_INVERTED { + if ind.GetType() == idxtype.INVERTED { // Since the inverted indexes generate multiple keys per row just handle them // separately. return b.encodeInvertedSecondaryIndex(ctx, ind, kys, b.extraKeys) diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index 25183c13a3cb..d29586af584e 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -77,6 +77,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/cockroach/pkg/sql/sqlliveness" @@ -4270,7 +4271,7 @@ CREATE TABLE crdb_internal.table_indexes ( tree.NewDString(idx.GetName()), idxType, tree.MakeDBool(tree.DBool(idx.IsUnique())), - tree.MakeDBool(idx.GetType() == descpb.IndexDescriptor_INVERTED), + tree.MakeDBool(idx.GetType() == idxtype.INVERTED), tree.MakeDBool(tree.DBool(idx.IsSharded())), tree.MakeDBool(idxInvisibility == 0.0), tree.NewDFloat(tree.DFloat(1-idxInvisibility)), diff --git a/pkg/sql/create_index.go b/pkg/sql/create_index.go index 2742f751ef13..7b3a18f734f0 100644 --- a/pkg/sql/create_index.go +++ b/pkg/sql/create_index.go @@ -7,6 +7,7 @@ package sql import ( "context" + "strings" "time" "github.com/cockroachdb/cockroach/pkg/clusterversion" @@ -25,6 +26,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/sql/storageparam" @@ -143,7 +145,7 @@ func makeIndexDescriptor( `"bucket_count" storage param should only be set with "USING HASH" for hash sharded index`, ) } - if n.Type == tree.IndexTypeVector { + if n.Type == idxtype.VECTOR { return nil, unimplemented.NewWithIssuef(137370, "VECTOR indexes are not yet supported") } @@ -207,22 +209,25 @@ func makeIndexDescriptor( CreatedAtNanos: params.EvalContext().GetTxnTimestamp(time.Microsecond).UnixNano(), NotVisible: n.Invisibility.Value != 0.0, Invisibility: n.Invisibility.Value, + Type: n.Type, } - if n.Type == tree.IndexTypeInverted { - if n.Sharded != nil { - return nil, pgerror.New(pgcode.InvalidSQLStatementName, "inverted indexes don't support hash sharding") - } + if !n.Type.SupportsSharding() && n.Sharded != nil { + return nil, pgerror.Newf(pgcode.InvalidSQLStatementName, + "%s indexes don't support hash sharding", strings.ToLower(n.Type.String())) + } - if len(indexDesc.StoreColumnNames) > 0 { - return nil, pgerror.New(pgcode.InvalidSQLStatementName, "inverted indexes don't support stored columns") - } + if !n.Type.SupportsStoring() && len(indexDesc.StoreColumnNames) > 0 { + return nil, pgerror.Newf(pgcode.InvalidSQLStatementName, + "%s indexes don't support stored columns", strings.ToLower(n.Type.String())) + } - if n.Unique { - return nil, pgerror.New(pgcode.InvalidSQLStatementName, "inverted indexes can't be unique") - } + if !n.Type.CanBeUnique() && n.Unique { + return nil, pgerror.Newf(pgcode.InvalidSQLStatementName, + "%s indexes can't be unique", strings.ToLower(n.Type.String())) + } - indexDesc.Type = descpb.IndexDescriptor_INVERTED + if n.Type == idxtype.INVERTED { invCol := columns[len(columns)-1] column, err := catalog.MustFindColumnByTreeName(tableDesc, invCol.Column) if err != nil { @@ -286,7 +291,7 @@ func makeIndexDescriptor( } // Increment telemetry once a descriptor has been successfully created. - if indexDesc.Type == descpb.IndexDescriptor_INVERTED { + if indexDesc.Type == idxtype.INVERTED { telemetry.Inc(sqltelemetry.InvertedIndexCounter) if indexDesc.GeoConfig.IsGeometry() { telemetry.Inc(sqltelemetry.GeometryInvertedIndexCounter) @@ -318,7 +323,7 @@ func checkIndexColumns( desc catalog.TableDescriptor, columns tree.IndexElemList, storing tree.NameList, - indexType tree.IndexType, + indexType idxtype.T, version clusterversion.ClusterVersion, ) error { for i, colDef := range columns { @@ -332,7 +337,7 @@ func checkIndexColumns( "cannot index system column %v", colDef.Column, ) } - if colDef.OpClass != "" && (i < len(columns)-1 || indexType != tree.IndexTypeInverted) { + if colDef.OpClass != "" && (i < len(columns)-1 || !indexType.SupportsOpClass()) { return pgerror.New(pgcode.DatatypeMismatch, "operator classes are only allowed for the last column of an inverted index") } @@ -479,7 +484,7 @@ func replaceExpressionElemsWithVirtualCols( desc *tabledesc.Mutable, tn *tree.TableName, elems tree.IndexElemList, - indexType tree.IndexType, + indexType idxtype.T, isNewTable bool, semaCtx *tree.SemaContext, version clusterversion.ClusterVersion, @@ -544,7 +549,7 @@ func replaceExpressionElemsWithVirtualCols( ) } - if indexType != tree.IndexTypeInverted && !colinfo.ColumnTypeIsIndexable(typ) { + if indexType != idxtype.INVERTED && !colinfo.ColumnTypeIsIndexable(typ) { if colinfo.ColumnTypeIsInvertedIndexable(typ) { return errors.WithHint( pgerror.Newf( @@ -564,7 +569,7 @@ func replaceExpressionElemsWithVirtualCols( ) } - if indexType == tree.IndexTypeInverted { + if indexType == idxtype.INVERTED { if i < lastColumnIdx && !colinfo.ColumnTypeIsIndexable(typ) { return errors.WithHint( pgerror.Newf( @@ -785,7 +790,7 @@ func (n *createIndexNode) startExec(params runParams) error { return err } - if indexDesc.Type == descpb.IndexDescriptor_INVERTED && indexDesc.Partitioning.NumColumns != 0 { + if indexDesc.Type == idxtype.INVERTED && indexDesc.Partitioning.NumColumns != 0 { telemetry.Inc(sqltelemetry.PartitionedInvertedIndexCounter) } diff --git a/pkg/sql/create_stats.go b/pkg/sql/create_stats.go index 44021dcd614c..786a7df72f02 100644 --- a/pkg/sql/create_stats.go +++ b/pkg/sql/create_stats.go @@ -25,6 +25,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" "github.com/cockroachdb/cockroach/pkg/sql/stats" @@ -509,7 +510,7 @@ func createStatsDefaultColumns( // implicitly partitioned indexes. if partialStats { for _, idx := range desc.ActiveIndexes() { - if idx.GetType() != descpb.IndexDescriptor_FORWARD || + if idx.GetType() != idxtype.FORWARD || idx.IsPartial() || idx.IsSharded() || idx.ImplicitPartitioningColumnCount() > 0 { @@ -579,7 +580,7 @@ func createStatsDefaultColumns( for _, idx := range desc.PublicNonPrimaryIndexes() { for j, n := 0, idx.NumKeyColumns(); j < n; j++ { colID := idx.GetKeyColumnID(j) - isInverted := idx.GetType() == descpb.IndexDescriptor_INVERTED && colID == idx.InvertedColumnID() + isInverted := idx.GetType() == idxtype.INVERTED && colID == idx.InvertedColumnID() // Generate stats for each indexed column. if err := addIndexColumnStatsIfNotExists(colID, isInverted); err != nil { diff --git a/pkg/sql/create_table.go b/pkg/sql/create_table.go index 95dcc9220f05..ce2da5cf872b 100644 --- a/pkg/sql/create_table.go +++ b/pkg/sql/create_table.go @@ -46,6 +46,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treebin" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" @@ -1886,7 +1887,7 @@ func NewTableDesc( // pass, handled above. case *tree.IndexTableDef: - if d.Type == tree.IndexTypeVector { + if d.Type == idxtype.VECTOR { return nil, unimplemented.NewWithIssuef(137370, "VECTOR indexes are not yet supported") } // If the index is named, ensure that the name is unique. Unnamed @@ -1926,9 +1927,7 @@ func NewTableDesc( Version: indexEncodingVersion, NotVisible: d.Invisibility.Value != 0.0, Invisibility: d.Invisibility.Value, - } - if d.Type == tree.IndexTypeInverted { - idx.Type = descpb.IndexDescriptor_INVERTED + Type: d.Type, } columns := d.Columns if d.Sharded != nil { @@ -1941,7 +1940,7 @@ func NewTableDesc( if err := idx.FillColumns(columns); err != nil { return nil, err } - if d.Type == tree.IndexTypeInverted { + if d.Type == idxtype.INVERTED { column, err := catalog.MustFindColumnByName(&desc, idx.InvertedColumnName()) if err != nil { return nil, err @@ -2371,7 +2370,7 @@ func NewTableDesc( if idx.IsSharded() { telemetry.Inc(sqltelemetry.HashShardedIndexCounter) } - if idx.GetType() == descpb.IndexDescriptor_INVERTED { + if idx.GetType() == idxtype.INVERTED { telemetry.Inc(sqltelemetry.InvertedIndexCounter) geoConfig := idx.GetGeoConfig() if !geoConfig.IsEmpty() { @@ -2770,18 +2769,9 @@ func replaceLikeTableOpts(n *tree.CreateTable, params runParams) (tree.TableDefs // we'll just generate a new one. continue } - var indexType tree.IndexType - switch idx.GetType() { - case descpb.IndexDescriptor_FORWARD: - indexType = tree.IndexTypeForward - case descpb.IndexDescriptor_INVERTED: - indexType = tree.IndexTypeInverted - default: - return nil, errors.AssertionFailedf("unknown index descriptor type %v", idx.GetType()) - } indexDef := tree.IndexTableDef{ Name: tree.Name(idx.GetName()), - Type: indexType, + Type: idx.GetType(), Storing: make(tree.NameList, 0, idx.NumSecondaryStoredColumns()), Columns: make(tree.IndexElemList, 0, idx.NumKeyColumns()), Invisibility: tree.IndexInvisibility{Value: idx.GetInvisibility()}, @@ -2818,9 +2808,9 @@ func replaceLikeTableOpts(n *tree.CreateTable, params runParams) (tree.TableDefs } indexDef.Columns = append(indexDef.Columns, elem) } - // The last column of an inverted index cannot have an explicit - // direction. - if indexDef.Type == tree.IndexTypeInverted { + // The last column of an inverted or vector index cannot have an + // explicit direction. + if !indexDef.Type.AllowExplicitDirection() { indexDef.Columns[len(indexDef.Columns)-1].Direction = tree.DefaultDirection } for j := 0; j < idx.NumSecondaryStoredColumns(); j++ { diff --git a/pkg/sql/distsql_plan_stats.go b/pkg/sql/distsql_plan_stats.go index c0f3cb824565..dec4cb0032a0 100644 --- a/pkg/sql/distsql_plan_stats.go +++ b/pkg/sql/distsql_plan_stats.go @@ -26,6 +26,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/parser" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/span" "github.com/cockroachdb/cockroach/pkg/sql/stats" @@ -440,7 +441,7 @@ func (dsp *DistSQLPlanner) createPartialStatsPlan( // (i.e., with different configurations). See #50655. if len(reqStat.columns) == 1 { for _, index := range desc.PublicNonPrimaryIndexes() { - if index.GetType() == descpb.IndexDescriptor_INVERTED && index.InvertedColumnID() == column.GetID() { + if index.GetType() == idxtype.INVERTED && index.InvertedColumnID() == column.GetID() { spec.Index = index.IndexDesc() break } @@ -680,7 +681,7 @@ func (dsp *DistSQLPlanner) createStatsPlan( if len(s.columns) == 1 { col := s.columns[0] for _, index := range desc.PublicNonPrimaryIndexes() { - if index.GetType() == descpb.IndexDescriptor_INVERTED && index.InvertedColumnID() == col { + if index.GetType() == idxtype.INVERTED && index.InvertedColumnID() == col { spec.Index = index.IndexDesc() break } diff --git a/pkg/sql/indexbackfiller_test.go b/pkg/sql/indexbackfiller_test.go index f539949f531f..b523fc5611e4 100644 --- a/pkg/sql/indexbackfiller_test.go +++ b/pkg/sql/indexbackfiller_test.go @@ -33,6 +33,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/row" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" "github.com/cockroachdb/cockroach/pkg/sql/rowinfra" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" @@ -255,7 +256,7 @@ INSERT INTO foo VALUES (1, 2), (2, 3), (3, 4); KeySuffixColumnIDs: []descpb.ColumnID{ mut.Columns[0].ID, }, - Type: descpb.IndexDescriptor_FORWARD, + Type: idxtype.FORWARD, EncodingType: catenumpb.SecondaryIndexEncoding, } mut.NextIndexID++ @@ -339,7 +340,7 @@ INSERT INTO foo VALUES (1), (10), (100); columnWithDefault.ID, computedColumnNotInPrimaryIndex.ID, }, - Type: descpb.IndexDescriptor_FORWARD, + Type: idxtype.FORWARD, EncodingType: catenumpb.PrimaryIndexEncoding, } mut.NextIndexID++ diff --git a/pkg/sql/opt/cat/BUILD.bazel b/pkg/sql/opt/cat/BUILD.bazel index 4cd147613bc2..56b9b0c2c84f 100644 --- a/pkg/sql/opt/cat/BUILD.bazel +++ b/pkg/sql/opt/cat/BUILD.bazel @@ -31,6 +31,7 @@ go_library( "//pkg/sql/privilege", "//pkg/sql/roleoption", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sessiondata", "//pkg/sql/types", diff --git a/pkg/sql/opt/cat/index.go b/pkg/sql/opt/cat/index.go index c11da3cd884c..a8ef4ab467a4 100644 --- a/pkg/sql/opt/cat/index.go +++ b/pkg/sql/opt/cat/index.go @@ -9,6 +9,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/geo/geopb" "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" ) @@ -45,15 +46,12 @@ type Index interface { // Specifically idx = Table().Index(idx.Ordinal). Ordinal() IndexOrdinal + // Type returns the type of this index: forward, inverted, vector, etc. + Type() idxtype.T + // IsUnique returns true if this index is declared as UNIQUE in the schema. IsUnique() bool - // IsInverted returns true if this is an inverted index. - IsInverted() bool - - // IsVector returns true if this is a vector index. - IsVector() bool - // GetInvisibility returns index invisibility. GetInvisibility() float64 diff --git a/pkg/sql/opt/cat/utils.go b/pkg/sql/opt/cat/utils.go index 52e4fafb5b52..a8d26055628a 100644 --- a/pkg/sql/opt/cat/utils.go +++ b/pkg/sql/opt/cat/utils.go @@ -11,6 +11,7 @@ import ( "fmt" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/cockroach/pkg/util/treeprinter" @@ -117,16 +118,19 @@ func FormatTable( // formatCatalogIndex nicely formats a catalog index using a treeprinter for // debugging and testing. func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node, redactableValues bool) { - idx := tab.Index(ord) - idxType := "" - if idx.Ordinal() == PrimaryIndex { - idxType = "PRIMARY " - } else if idx.IsUnique() { - idxType = "UNIQUE " - } else if idx.IsInverted() { - idxType = "INVERTED " - } else if idx.IsVector() { - idxType = "VECTOR " + index := tab.Index(ord) + indexType := "" + if index.Ordinal() == PrimaryIndex { + indexType = "PRIMARY " + } else if index.IsUnique() { + indexType = "UNIQUE " + } else { + switch index.Type() { + case idxtype.INVERTED: + indexType = "INVERTED " + case idxtype.VECTOR: + indexType = "VECTOR " + } } mutation := "" if IsMutationIndex(tab, ord) { @@ -134,7 +138,7 @@ func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node, redactableValue } idxVisibililty := "" - if invisibility := idx.GetInvisibility(); invisibility != 0.0 { + if invisibility := index.GetInvisibility(); invisibility != 0.0 { if invisibility == 1.0 { idxVisibililty = " NOT VISIBLE" } else { @@ -142,41 +146,41 @@ func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node, redactableValue } } - child := tp.Childf("%sINDEX %s%s%s", idxType, idx.Name(), mutation, idxVisibililty) + child := tp.Childf("%sINDEX %s%s%s", indexType, index.Name(), mutation, idxVisibililty) var buf bytes.Buffer - colCount := idx.ColumnCount() + colCount := index.ColumnCount() if ord == PrimaryIndex { // Omit the "stored" columns from the primary index. - colCount = idx.KeyColumnCount() + colCount = index.KeyColumnCount() } for i := 0; i < colCount; i++ { buf.Reset() - idxCol := idx.Column(i) + idxCol := index.Column(i) formatColumn(idxCol.Column, &buf, redactableValues) if idxCol.Descending { fmt.Fprintf(&buf, " desc") } - if i >= idx.LaxKeyColumnCount() { + if i >= index.LaxKeyColumnCount() { fmt.Fprintf(&buf, " (storing)") } - if i < idx.ImplicitColumnCount() { + if i < index.ImplicitColumnCount() { fmt.Fprintf(&buf, " (implicit)") } child.Child(buf.String()) } - FormatZone(idx.Zone(), child) + FormatZone(index.Zone(), child) - if n := idx.PartitionCount(); n > 0 { + if n := index.PartitionCount(); n > 0 { c := child.Child("partitions") for i := 0; i < n; i++ { - p := idx.Partition(i) + p := index.Partition(i) part := c.Child(p.Name()) prefixes := part.Child("partition by list prefixes") for _, datums := range p.PartitionByListPrefixes() { @@ -185,7 +189,7 @@ func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node, redactableValue FormatZone(p.Zone(), part) } } - if pred, isPartial := idx.Predicate(); isPartial { + if pred, isPartial := index.Predicate(); isPartial { child.Childf("WHERE %s", MaybeMarkRedactable(pred, redactableValues)) } } diff --git a/pkg/sql/opt/exec/execbuilder/BUILD.bazel b/pkg/sql/opt/exec/execbuilder/BUILD.bazel index b13e0c745a99..9b80cbb7245d 100644 --- a/pkg/sql/opt/exec/execbuilder/BUILD.bazel +++ b/pkg/sql/opt/exec/execbuilder/BUILD.bazel @@ -39,6 +39,7 @@ go_library( "//pkg/sql/sem/builtins/builtinsregistry", "//pkg/sql/sem/catconstants", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treebin", "//pkg/sql/sem/tree/treecmp", diff --git a/pkg/sql/opt/exec/execbuilder/relational.go b/pkg/sql/opt/exec/execbuilder/relational.go index 93cca29213db..a8127c6973d0 100644 --- a/pkg/sql/opt/exec/execbuilder/relational.go +++ b/pkg/sql/opt/exec/execbuilder/relational.go @@ -33,6 +33,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/builtinsregistry" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treewindow" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" @@ -584,7 +585,7 @@ func (b *Builder) scanParams( // index in the memo. if scan.Flags.ForceIndex && scan.Flags.Index != scan.Index { idx := tab.Index(scan.Flags.Index) - isInverted := idx.IsInverted() + isInverted := idx.Type() == idxtype.INVERTED _, isPartial := idx.Predicate() var err error @@ -756,11 +757,13 @@ func (b *Builder) buildScan(scan *memo.ScanExpr) (_ execPlan, outputCols colOrdM } idx := tab.Index(scan.Index) - if idx.IsInverted() && len(scan.InvertedConstraint) == 0 && scan.Constraint == nil { - return execPlan{}, colOrdMap{}, - errors.AssertionFailedf("expected inverted index scan to have a constraint") + if idx.Type() == idxtype.INVERTED { + if len(scan.InvertedConstraint) == 0 && scan.Constraint == nil { + return execPlan{}, colOrdMap{}, + errors.AssertionFailedf("expected inverted index scan to have a constraint") + } } - if idx.IsVector() { + if idx.Type() == idxtype.VECTOR { return execPlan{}, colOrdMap{}, errors.AssertionFailedf( "only VectorSearch operators can use vector indexes") } diff --git a/pkg/sql/opt/exec/explain/BUILD.bazel b/pkg/sql/opt/exec/explain/BUILD.bazel index a03d9535682f..443681fe3ac9 100644 --- a/pkg/sql/opt/exec/explain/BUILD.bazel +++ b/pkg/sql/opt/exec/explain/BUILD.bazel @@ -32,6 +32,7 @@ go_library( "//pkg/sql/pgwire/pgerror", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sessiondatapb", "//pkg/sql/types", diff --git a/pkg/sql/opt/exec/explain/plan_gist_factory.go b/pkg/sql/opt/exec/explain/plan_gist_factory.go index 0bdb6adf6a55..1f628d7d38ad 100644 --- a/pkg/sql/opt/exec/explain/plan_gist_factory.go +++ b/pkg/sql/opt/exec/explain/plan_gist_factory.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util" @@ -681,12 +682,8 @@ func (u *unknownIndex) IsUnique() bool { return false } -func (u *unknownIndex) IsInverted() bool { - return false -} - -func (u *unknownIndex) IsVector() bool { - return false +func (u *unknownIndex) Type() idxtype.T { + return idxtype.FORWARD } func (u *unknownIndex) GetInvisibility() float64 { diff --git a/pkg/sql/opt/indexrec/BUILD.bazel b/pkg/sql/opt/indexrec/BUILD.bazel index 4b3c72d4be01..16762a2c6824 100644 --- a/pkg/sql/opt/indexrec/BUILD.bazel +++ b/pkg/sql/opt/indexrec/BUILD.bazel @@ -19,6 +19,7 @@ go_library( "//pkg/sql/opt", "//pkg/sql/opt/cat", "//pkg/sql/opt/memo", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util/intsets", diff --git a/pkg/sql/opt/indexrec/hypothetical_index.go b/pkg/sql/opt/indexrec/hypothetical_index.go index 025e79884dd0..576f15fb7a4d 100644 --- a/pkg/sql/opt/indexrec/hypothetical_index.go +++ b/pkg/sql/opt/indexrec/hypothetical_index.go @@ -11,6 +11,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/intsets" @@ -41,8 +42,8 @@ type hypotheticalIndex struct { // columns (neither index columns nor suffix key columns). storedCols []cat.IndexColumn - // inverted indicates if an index is inverted. - inverted bool + // typ indicates the type of the index - forward, inverted, or vector. + typ idxtype.T } var _ cat.Index = &hypotheticalIndex{} @@ -52,14 +53,14 @@ func (hi *hypotheticalIndex) init( name tree.Name, cols []cat.IndexColumn, indexOrd int, - inverted bool, + typ idxtype.T, zone cat.Zone, ) { hi.tab = tab hi.name = name hi.cols = cols hi.indexOrdinal = indexOrd - hi.inverted = inverted + hi.typ = typ hi.zone = zone // Build an index column ordinal set. @@ -77,8 +78,8 @@ func (hi *hypotheticalIndex) init( } } - // Build the stored cols for non-inverted indexes only. - if !inverted { + // Build the stored cols for forward indexes only. + if typ.SupportsStoring() { keyColsOrds := colsOrdSet.Union(pkColOrds) hi.storedCols = make([]cat.IndexColumn, 0, tab.ColumnCount()) for i, n := 0, tab.ColumnCount(); i < n; i++ { @@ -106,14 +107,9 @@ func (hi *hypotheticalIndex) IsUnique() bool { return false } -// IsInverted is part of the cat.Index interface. -func (hi *hypotheticalIndex) IsInverted() bool { - return hi.inverted -} - -// IsVector is part of the cat.Index interface. -func (hi *hypotheticalIndex) IsVector() bool { - return false +// Type is part of the cat.Index interface. +func (hi *hypotheticalIndex) Type() idxtype.T { + return hi.typ } // GetInvisibility is part of the cat.Index interface. @@ -150,7 +146,7 @@ func (hi *hypotheticalIndex) LaxKeyColumnCount() int { // PrefixColumnCount is part of the cat.Index interface. func (hi *hypotheticalIndex) PrefixColumnCount() int { - if !hi.IsInverted() && !hi.IsVector() { + if !hi.Type().AllowsPrefixColumns() { panic(errors.AssertionFailedf("only inverted and vector indexes have prefix columns")) } return len(hi.cols) - 1 @@ -173,7 +169,7 @@ func (hi *hypotheticalIndex) Column(i int) cat.IndexColumn { // InvertedColumn is part of the cat.Index interface. func (hi *hypotheticalIndex) InvertedColumn() cat.IndexColumn { - if !hi.IsInverted() { + if hi.Type() != idxtype.INVERTED { panic(errors.AssertionFailedf("non-inverted indexes do not have inverted columns")) } return hi.cols[len(hi.cols)-1] @@ -221,7 +217,7 @@ func (hi *hypotheticalIndex) ImplicitPartitioningColumnCount() int { // GeoConfig is part of the cat.Index interface. func (hi *hypotheticalIndex) GeoConfig() geopb.Config { - if hi.IsInverted() { + if hi.Type() == idxtype.INVERTED { srcCol := hi.tab.Column(hi.InvertedColumn().InvertedSourceColumnOrdinal()) switch srcCol.DatumType().Family() { case types.GeometryFamily: @@ -253,20 +249,18 @@ func (hi *hypotheticalIndex) Partition(i int) cat.Partition { // have the exact same list, length, and order. If the index is inverted, it // also checks to make sure that the inverted column has the same source column. // If so, it returns true. -func (hi *hypotheticalIndex) hasSameExplicitCols(existingIndex cat.Index, isInverted bool) bool { +func (hi *hypotheticalIndex) hasSameExplicitCols(existingIndex cat.Index) bool { indexCols := hi.cols if existingIndex.ExplicitColumnCount() != len(indexCols) { return false } - return hi.hasPrefixOfExplicitCols(existingIndex, isInverted) + return hi.hasPrefixOfExplicitCols(existingIndex) } // hasPrefixOfExplicitCols returns true if the explicit columns in the // hypothetical index are a prefix of the explicit columns in the given existing // index. -func (hi *hypotheticalIndex) hasPrefixOfExplicitCols( - existingIndex cat.Index, isInverted bool, -) bool { +func (hi *hypotheticalIndex) hasPrefixOfExplicitCols(existingIndex cat.Index) bool { indexCols := hi.cols if existingIndex.ExplicitColumnCount() < len(indexCols) { return false diff --git a/pkg/sql/opt/indexrec/hypothetical_table.go b/pkg/sql/opt/indexrec/hypothetical_table.go index 0ffa07fde1d4..623c9897ca1b 100644 --- a/pkg/sql/opt/indexrec/hypothetical_table.go +++ b/pkg/sql/opt/indexrec/hypothetical_table.go @@ -11,6 +11,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/intsets" @@ -39,9 +40,11 @@ func BuildOptAndHypTableMaps( lastKeyCol := indexCols[len(indexCols)-1] // TODO (Shivam): Index recommendations should not only allow JSON columns // to be part of inverted indexes since they are also forward indexable. - inverted := !colinfo.ColumnTypeIsIndexable(lastKeyCol.DatumType()) || - lastKeyCol.DatumType().Family() == types.JsonFamily - if inverted { + indexType := idxtype.FORWARD + if !colinfo.ColumnTypeIsIndexable(lastKeyCol.DatumType()) || + lastKeyCol.DatumType().Family() == types.JsonFamily { + indexType = idxtype.INVERTED + invertedCol := hypTable.addInvertedCol(lastKeyCol.Column) indexCols[len(indexCols)-1] = cat.IndexColumn{Column: invertedCol} } @@ -51,7 +54,7 @@ func BuildOptAndHypTableMaps( tree.Name(fmt.Sprintf("_hyp_%d", indexOrd)), indexCols, indexOrd, - inverted, + indexType, t.Zone(), ) @@ -59,7 +62,7 @@ func BuildOptAndHypTableMaps( // index with the same key. Inverted indexes do not have stored columns, // so we should not make a recommendation if the same index already // exists. - if !inverted || hypTable.existingRedundantIndex(&hypIndex) == nil { + if indexType != idxtype.INVERTED || hypTable.existingRedundantIndex(&hypIndex) == nil { hypIndexes = append(hypIndexes, hypIndex) } } @@ -159,7 +162,7 @@ func (ht *HypotheticalTable) FullyQualifiedName(ctx context.Context) (cat.DataSo func (ht *HypotheticalTable) existingRedundantIndex(index *hypotheticalIndex) cat.Index { for i, n := 0, ht.Table.IndexCount(); i < n; i++ { existingIndex := ht.Table.Index(i) - indexExists := index.hasSameExplicitCols(existingIndex, index.IsInverted()) + indexExists := index.hasSameExplicitCols(existingIndex) _, isPartialIndex := existingIndex.Predicate() if indexExists && !isPartialIndex && existingIndex.GetInvisibility() == 0.0 { return existingIndex diff --git a/pkg/sql/opt/indexrec/rec.go b/pkg/sql/opt/indexrec/rec.go index 5fa4451f4a15..d488758ce92c 100644 --- a/pkg/sql/opt/indexrec/rec.go +++ b/pkg/sql/opt/indexrec/rec.go @@ -13,6 +13,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt" "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" "github.com/cockroachdb/cockroach/pkg/sql/opt/memo" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/intsets" ) @@ -163,7 +164,7 @@ func findBestExistingIndexToReplace( continue } if existingIndex.GetInvisibility() != 0.0 { - if hypIndex.hasPrefixOfExplicitCols(existingIndex, hypIndex.IsInverted()) { + if hypIndex.hasPrefixOfExplicitCols(existingIndex) { existingIndexAllCols := getAllCols(existingIndex) if newStoredCols.Difference(existingIndexAllCols).Empty() { // There exists an invisible index containing every explicit column in @@ -186,7 +187,7 @@ func findBestExistingIndexToReplace( // hasSameExplicitCols returns true iff the existing index and hypIndex has // the same explicit columns. If hypIndex is inverted, it also makes sure // that their inverted column comes from the same source column. - hasSameExplicitCols := hypIndex.hasSameExplicitCols(existingIndex, hypIndex.IsInverted()) + hasSameExplicitCols := hypIndex.hasSameExplicitCols(existingIndex) if hasSameExplicitCols { // If hasSameExplicitCols, this existing index is a candidate for // potential index replacement. @@ -256,13 +257,6 @@ func (ir *indexRecommendation) constructIndexRec(ctx context.Context) (Rec, erro return Rec{}, err } - indexType := tree.IndexTypeForward - if ir.index.IsInverted() { - indexType = tree.IndexTypeInverted - } else if ir.index.IsVector() { - indexType = tree.IndexTypeVector - } - // Formats index recommendation to its final output struct Rec. switch recType { case TypeCreateIndex: @@ -271,7 +265,7 @@ func (ir *indexRecommendation) constructIndexRec(ctx context.Context) (Rec, erro Columns: indexCols, Storing: storing, Unique: false, - Type: indexType, + Type: ir.index.Type(), } sb.WriteString(createCmd.String()) sb.WriteByte(';') @@ -289,7 +283,7 @@ func (ir *indexRecommendation) constructIndexRec(ctx context.Context) (Rec, erro Storing: storing, // Maintain uniqueness and inverted if the existing index is unique. Unique: existingIndex.IsUnique(), - Type: indexType, + Type: ir.index.Type(), } sb.WriteString(createCmd.String()) sb.WriteByte(';') @@ -452,7 +446,7 @@ func (ir *indexRecommendation) indexCols() []tree.IndexElem { indexCol := ir.index.Column(i) colName := indexCol.Column.ColName() - if ir.index.IsInverted() && i == len(ir.index.cols)-1 { + if ir.index.Type() == idxtype.INVERTED && i == len(ir.index.cols)-1 { colName = ir.index.tab.Column(indexCol.InvertedSourceColumnOrdinal()).ColName() } diff --git a/pkg/sql/opt/invertedidx/BUILD.bazel b/pkg/sql/opt/invertedidx/BUILD.bazel index bbceef4d1f7b..515897d78641 100644 --- a/pkg/sql/opt/invertedidx/BUILD.bazel +++ b/pkg/sql/opt/invertedidx/BUILD.bazel @@ -30,6 +30,7 @@ go_library( "//pkg/sql/opt/props", "//pkg/sql/rowenc", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treecmp", "//pkg/sql/types", diff --git a/pkg/sql/opt/invertedidx/inverted_index_expr.go b/pkg/sql/opt/invertedidx/inverted_index_expr.go index ce719c67f99a..4f370a5bee2a 100644 --- a/pkg/sql/opt/invertedidx/inverted_index_expr.go +++ b/pkg/sql/opt/invertedidx/inverted_index_expr.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/norm" "github.com/cockroachdb/cockroach/pkg/sql/opt/props" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/encoding" @@ -449,7 +450,7 @@ func TryJoinInvertedIndex( index cat.Index, inputCols opt.ColSet, ) opt.ScalarExpr { - if !index.IsInverted() { + if index.Type() != idxtype.INVERTED { return nil } diff --git a/pkg/sql/opt/memo/BUILD.bazel b/pkg/sql/opt/memo/BUILD.bazel index df7e0ba9b5d6..9665b51d5d2f 100644 --- a/pkg/sql/opt/memo/BUILD.bazel +++ b/pkg/sql/opt/memo/BUILD.bazel @@ -43,6 +43,7 @@ go_library( "//pkg/sql/sem/cast", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treewindow", "//pkg/sql/sem/volatility", diff --git a/pkg/sql/opt/memo/expr.go b/pkg/sql/opt/memo/expr.go index 6c10ed2bae16..3d6a0896470f 100644 --- a/pkg/sql/opt/memo/expr.go +++ b/pkg/sql/opt/memo/expr.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treewindow" "github.com/cockroachdb/cockroach/pkg/sql/sem/volatility" @@ -862,7 +863,7 @@ func (s *ScanPrivate) IsFullIndexScan() bool { // IsInvertedScan returns true if the index being scanned is an inverted // index. func (s *ScanPrivate) IsInvertedScan(md *opt.Metadata) bool { - return md.Table(s.Table).Index(s.Index).IsInverted() + return md.Table(s.Table).Index(s.Index).Type() == idxtype.INVERTED } // IsVirtualTable returns true if the table being scanned is a virtual table. diff --git a/pkg/sql/opt/memo/expr_format.go b/pkg/sql/opt/memo/expr_format.go index 2029e911a3a0..f4c008263124 100644 --- a/pkg/sql/opt/memo/expr_format.go +++ b/pkg/sql/opt/memo/expr_format.go @@ -18,6 +18,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/props" "github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treewindow" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -1473,10 +1474,10 @@ func (f *ExprFmtCtx) formatIndex(tabID opt.TableID, idxOrd cat.IndexOrdinal, rev if reverse { f.Buffer.WriteString(",rev") } - if index.IsInverted() { + switch index.Type() { + case idxtype.INVERTED: f.Buffer.WriteString(",inverted") - } - if index.IsVector() { + case idxtype.VECTOR: f.Buffer.WriteString(",vector") } if _, isPartial := index.Predicate(); isPartial { diff --git a/pkg/sql/opt/memo/logical_props_builder.go b/pkg/sql/opt/memo/logical_props_builder.go index 018b98bb2b06..ad2761860a9a 100644 --- a/pkg/sql/opt/memo/logical_props_builder.go +++ b/pkg/sql/opt/memo/logical_props_builder.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/props" "github.com/cockroachdb/cockroach/pkg/sql/sem/cast" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treewindow" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -2019,13 +2020,9 @@ func MakeTableFuncDep(md *opt.Metadata, tabID opt.TableID) *props.FuncDepSet { continue } - if index.IsInverted() { - // Skip inverted indexes for now. - continue - } - - if index.IsVector() { - // Skip vector indexes for now. + switch index.Type() { + case idxtype.INVERTED, idxtype.VECTOR: + // Skip inverted and vector indexes for now. continue } diff --git a/pkg/sql/opt/memo/statistics_builder.go b/pkg/sql/opt/memo/statistics_builder.go index d050cd579bf1..01e693cab7bf 100644 --- a/pkg/sql/opt/memo/statistics_builder.go +++ b/pkg/sql/opt/memo/statistics_builder.go @@ -18,6 +18,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/constraint" "github.com/cockroachdb/cockroach/pkg/sql/opt/props" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/stats" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -635,7 +636,7 @@ func (sb *statisticsBuilder) makeTableStatistics(tabID opt.TableID) *props.Stati invertedIndexCols := make(map[int]invertedIndexColInfo) for indexI, indexN := 0, tab.IndexCount(); indexI < indexN; indexI++ { index := tab.Index(indexI) - if !index.IsInverted() { + if index.Type() != idxtype.INVERTED { continue } col := index.InvertedColumn() @@ -1947,8 +1948,8 @@ func (sb *statisticsBuilder) buildZigzagJoin( // join ends up having a higher row count and therefore higher cost than // a competing index join + constrained scan. tab := sb.md.Table(zigzag.LeftTable) - leftIndexInverted := tab.Index(zigzag.LeftIndex).IsInverted() - rightIndexInverted := tab.Index(zigzag.RightIndex).IsInverted() + leftIndexInverted := tab.Index(zigzag.LeftIndex).Type() == idxtype.INVERTED + rightIndexInverted := tab.Index(zigzag.RightIndex).Type() == idxtype.INVERTED if leftIndexInverted { unapplied.unknown += len(zigzag.LeftFixedCols) * 2 } diff --git a/pkg/sql/opt/optbuilder/BUILD.bazel b/pkg/sql/opt/optbuilder/BUILD.bazel index 14eb16e214cf..b6a08b38d163 100644 --- a/pkg/sql/opt/optbuilder/BUILD.bazel +++ b/pkg/sql/opt/optbuilder/BUILD.bazel @@ -88,6 +88,7 @@ go_library( "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/plpgsqltree", "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treebin", diff --git a/pkg/sql/opt/optbuilder/alter_table.go b/pkg/sql/opt/optbuilder/alter_table.go index de5a2fdb5aed..960122bff66f 100644 --- a/pkg/sql/opt/optbuilder/alter_table.go +++ b/pkg/sql/opt/optbuilder/alter_table.go @@ -14,6 +14,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" ) @@ -197,7 +198,7 @@ func getIndexColumnNamesAndTypes(index cat.Index) (colNames []string, colTypes [ colNames[i] = string(c.ColName()) colTypes[i] = c.DatumType() } - if index.IsInverted() && !index.GeoConfig().IsEmpty() { + if index.Type() == idxtype.INVERTED && !index.GeoConfig().IsEmpty() { // TODO(sumeer): special case Array too. JSON is harder since the split // needs to be a Datum and the JSON inverted column is not. // diff --git a/pkg/sql/opt/optbuilder/groupby.go b/pkg/sql/opt/optbuilder/groupby.go index 429e96b65d55..701918d9af9b 100644 --- a/pkg/sql/opt/optbuilder/groupby.go +++ b/pkg/sql/opt/optbuilder/groupby.go @@ -989,7 +989,7 @@ func (b *Builder) allowImplicitGroupingColumn(colID opt.ColumnID, g *groupby) bo // Check UNIQUE INDEX constraints. for i := 1; i < tab.IndexCount(); i++ { index := tab.Index(i) - if !index.IsUnique() || index.IsInverted() || index.IsVector() { + if !index.IsUnique() || !index.Type().CanBeUnique() { continue } // If any of the key columns is nullable, uniqueCols is suffixed with the diff --git a/pkg/sql/opt/optbuilder/mutation_builder.go b/pkg/sql/opt/optbuilder/mutation_builder.go index 9a03dc008606..12a689544ab5 100644 --- a/pkg/sql/opt/optbuilder/mutation_builder.go +++ b/pkg/sql/opt/optbuilder/mutation_builder.go @@ -21,6 +21,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/sem/cast" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry" @@ -1117,7 +1118,7 @@ func (mb *mutationBuilder) projectVectorIndexColsImpl(delete bool) { index := mb.tab.Index(i) // Skip non-vector indexes. - if !index.IsVector() { + if index.Type() != idxtype.VECTOR { continue } vectorColOrd := index.VectorColumn().Ordinal() @@ -1652,7 +1653,7 @@ func partialIndexCount(tab cat.Table) int { func vectorIndexCount(tab cat.Table) int { count := 0 for i, n := 0, tab.DeletableIndexCount(); i < n; i++ { - if tab.Index(i).IsVector() { + if tab.Index(i).Type() == idxtype.VECTOR { count++ } } diff --git a/pkg/sql/opt/ordering/BUILD.bazel b/pkg/sql/opt/ordering/BUILD.bazel index 8222e6d58114..f229cd710ec5 100644 --- a/pkg/sql/opt/ordering/BUILD.bazel +++ b/pkg/sql/opt/ordering/BUILD.bazel @@ -31,6 +31,7 @@ go_library( "//pkg/sql/opt/memo", "//pkg/sql/opt/props", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/util/buildutil", "@com_github_cockroachdb_errors//:errors", diff --git a/pkg/sql/opt/ordering/interesting_orderings.go b/pkg/sql/opt/ordering/interesting_orderings.go index 640a21d25b20..f2913990e0ef 100644 --- a/pkg/sql/opt/ordering/interesting_orderings.go +++ b/pkg/sql/opt/ordering/interesting_orderings.go @@ -10,6 +10,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" "github.com/cockroachdb/cockroach/pkg/sql/opt/memo" "github.com/cockroachdb/cockroach/pkg/sql/opt/props" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" ) // DeriveRestrictedInterestingOrderings calculates and returns the entry of the @@ -105,7 +106,8 @@ func interestingOrderingsForScan(scan *memo.ScanExpr) props.OrderingSet { addIndexOrdering := func(indexOrd cat.IndexOrdinal, fds *props.FuncDepSet, exactPrefix int) { index := tab.Index(indexOrd) - if index.IsInverted() { + if index.Type() != idxtype.FORWARD { + // Do not consider inverted or vector indexes. return } numIndexCols := index.KeyColumnCount() diff --git a/pkg/sql/opt/testutils/testcat/BUILD.bazel b/pkg/sql/opt/testutils/testcat/BUILD.bazel index 35ed5e5cbfcd..06235c614961 100644 --- a/pkg/sql/opt/testutils/testcat/BUILD.bazel +++ b/pkg/sql/opt/testutils/testcat/BUILD.bazel @@ -46,6 +46,7 @@ go_library( "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treecmp", "//pkg/sql/sem/volatility", diff --git a/pkg/sql/opt/testutils/testcat/create_table.go b/pkg/sql/opt/testutils/testcat/create_table.go index cce53a756dc6..f04057a26a6c 100644 --- a/pkg/sql/opt/testutils/testcat/create_table.go +++ b/pkg/sql/opt/testutils/testcat/create_table.go @@ -24,6 +24,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb" @@ -883,8 +884,7 @@ func (tt *Table) addIndexWithVersion( idx := &Index{ IdxName: tt.makeIndexName(def.Name, def.Columns, typ), Unique: typ != nonUniqueIndex, - Inverted: def.Type == tree.IndexTypeInverted, - Vector: def.Type == tree.IndexTypeVector, + Typ: def.Type, IdxZone: cat.EmptyZone(), table: tt, version: version, @@ -913,9 +913,9 @@ func (tt *Table) addIndexWithVersion( isLastIndexCol := i == len(def.Columns)-1 if isLastIndexCol { switch def.Type { - case tree.IndexTypeInverted: + case idxtype.INVERTED: idx.invertedOrd = i - case tree.IndexTypeVector: + case idxtype.VECTOR: idx.vectorOrd = i } } @@ -925,7 +925,7 @@ func (tt *Table) addIndexWithVersion( notNullIndex = false } - if isLastIndexCol && def.Type == tree.IndexTypeInverted { + if isLastIndexCol && def.Type == idxtype.INVERTED { switch tt.Columns[col.InvertedSourceColumnOrdinal()].DatumType().Family() { case types.GeometryFamily: // Don't use the default config because it creates a huge number of spans. @@ -1071,9 +1071,9 @@ func (tt *Table) addIndexWithVersion( // Add storing columns. for _, name := range def.Storing { switch def.Type { - case tree.IndexTypeInverted: + case idxtype.INVERTED: panic("inverted indexes don't support stored columns") - case tree.IndexTypeVector: + case idxtype.VECTOR: panic("vector indexes don't support stored columns") } // Only add storing columns that weren't added as part of adding implicit @@ -1235,7 +1235,7 @@ func (ti *Index) addColumn( colName = elem.Column } - if ti.Inverted && isLastIndexCol { + if ti.Typ == idxtype.INVERTED && isLastIndexCol { // The last column of an inverted index is special: the index key does // not contain values from the column itself, but contains inverted // index entries derived from that column. Create a virtual column to be @@ -1319,7 +1319,7 @@ func (ti *Index) addColumnByOrdinal( )) } } else if typ.Family() == types.PGVectorFamily { - if !ti.Vector { + if ti.Typ != idxtype.VECTOR { panic(fmt.Errorf( "column %s of type %s is not allowed in a non-vector index", col.ColName(), typ, )) diff --git a/pkg/sql/opt/testutils/testcat/test_catalog.go b/pkg/sql/opt/testutils/testcat/test_catalog.go index 977a9e2c67f8..828b4ed163e6 100644 --- a/pkg/sql/opt/testutils/testcat/test_catalog.go +++ b/pkg/sql/opt/testutils/testcat/test_catalog.go @@ -29,6 +29,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/stats" @@ -1094,11 +1095,8 @@ type Index struct { // Unique is true if this index is declared as UNIQUE in the schema. Unique bool - // Inverted is true when this index is an inverted index. - Inverted bool - - // Vector is true when this index is a vector index. - Vector bool + // Typ is the type of the index: forward, inverted, vector, etc. + Typ idxtype.T // Invisibility specifies the invisibility of an index and can be any float64 // between [0.0, 1.0]. An index with invisibility 0.0 means that the index is @@ -1170,14 +1168,9 @@ func (ti *Index) IsUnique() bool { return ti.Unique } -// IsInverted is part of the cat.Index interface. -func (ti *Index) IsInverted() bool { - return ti.Inverted -} - -// IsVector is part of the cat.Index interface. -func (ti *Index) IsVector() bool { - return ti.Vector +// Type is part of the cat.Index interface. +func (ti *Index) Type() idxtype.T { + return ti.Typ } // GetInvisibility is part of the cat.Index interface. @@ -1207,13 +1200,14 @@ func (ti *Index) LaxKeyColumnCount() int { // PrefixColumnCount is part of the cat.Index interface. func (ti *Index) PrefixColumnCount() int { - if ti.IsInverted() { + switch ti.Type() { + case idxtype.INVERTED: return ti.invertedOrd - } - if ti.IsVector() { + case idxtype.VECTOR: return ti.vectorOrd + default: + panic("only supported for inverted and vector indexes") } - panic("only supported for inverted and vector indexes") } // Column is part of the cat.Index interface. @@ -1223,7 +1217,7 @@ func (ti *Index) Column(i int) cat.IndexColumn { // InvertedColumn is part of the cat.Index interface. func (ti *Index) InvertedColumn() cat.IndexColumn { - if !ti.IsInverted() { + if ti.Type() != idxtype.INVERTED { panic("non-inverted indexes do not have inverted columns") } return ti.Column(ti.invertedOrd) @@ -1231,7 +1225,7 @@ func (ti *Index) InvertedColumn() cat.IndexColumn { // VectorColumn is part of the cat.Index interface. func (ti *Index) VectorColumn() cat.IndexColumn { - if !ti.IsVector() { + if ti.Type() != idxtype.VECTOR { panic("non-vector indexes do not have indexed vector columns") } return ti.Column(ti.vectorOrd) diff --git a/pkg/sql/opt/workloadindexrec/BUILD.bazel b/pkg/sql/opt/workloadindexrec/BUILD.bazel index f42d06a1a7a4..ec2afaff8f27 100644 --- a/pkg/sql/opt/workloadindexrec/BUILD.bazel +++ b/pkg/sql/opt/workloadindexrec/BUILD.bazel @@ -11,6 +11,7 @@ go_library( deps = [ "//pkg/sql/parser", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sessiondata", "@com_github_cockroachdb_errors//:errors", diff --git a/pkg/sql/opt/workloadindexrec/workload_indexrecs.go b/pkg/sql/opt/workloadindexrec/workload_indexrecs.go index 69fc583b6819..b535670a46f3 100644 --- a/pkg/sql/opt/workloadindexrec/workload_indexrecs.go +++ b/pkg/sql/opt/workloadindexrec/workload_indexrecs.go @@ -11,6 +11,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/parser" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/errors" @@ -125,7 +126,7 @@ func collectIndexRecs( switch stmt := stmt.AST.(type) { case *tree.CreateIndex: // Ignore all the inverted, vector, partial, sharded, etc. indexes right now. - if stmt.Type == tree.IndexTypeForward && stmt.Predicate == nil && stmt.Sharded == nil { + if stmt.Type == idxtype.FORWARD && stmt.Predicate == nil && stmt.Sharded == nil { cis = append(cis, *stmt) } case *tree.DropIndex: diff --git a/pkg/sql/opt/xform/BUILD.bazel b/pkg/sql/opt/xform/BUILD.bazel index 987994e8ef6a..c9076fc1b99f 100644 --- a/pkg/sql/opt/xform/BUILD.bazel +++ b/pkg/sql/opt/xform/BUILD.bazel @@ -48,6 +48,7 @@ go_library( "//pkg/sql/opt/props/physical", "//pkg/sql/rowinfra", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sem/volatility", "//pkg/sql/sessiondatapb", diff --git a/pkg/sql/opt/xform/general_funcs.go b/pkg/sql/opt/xform/general_funcs.go index 9eafd7fceb8f..063aa0ba227e 100644 --- a/pkg/sql/opt/xform/general_funcs.go +++ b/pkg/sql/opt/xform/general_funcs.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/ordering" "github.com/cockroachdb/cockroach/pkg/sql/opt/partialidx" "github.com/cockroachdb/cockroach/pkg/sql/opt/props" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/errors" ) @@ -59,7 +60,7 @@ func (c *CustomFuncs) HasInvertedIndexes(scanPrivate *memo.ScanPrivate) bool { // Skip the primary index because it cannot be inverted. for i := 1; i < tab.IndexCount(); i++ { - if tab.Index(i).IsInverted() { + if tab.Index(i).Type() == idxtype.INVERTED { return true } } diff --git a/pkg/sql/opt/xform/limit_funcs.go b/pkg/sql/opt/xform/limit_funcs.go index b38859b6da60..248d82ac013c 100644 --- a/pkg/sql/opt/xform/limit_funcs.go +++ b/pkg/sql/opt/xform/limit_funcs.go @@ -12,6 +12,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/ordering" "github.com/cockroachdb/cockroach/pkg/sql/opt/props" "github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/errors" @@ -190,7 +191,7 @@ func (c *CustomFuncs) ScanIsLimited(sp *memo.ScanPrivate) bool { func (c *CustomFuncs) ScanIsInverted(sp *memo.ScanPrivate) bool { md := c.e.mem.Metadata() idx := md.Table(sp.Table).Index(sp.Index) - return idx.IsInverted() + return idx.Type() == idxtype.INVERTED } // SplitLimitedScanIntoUnionScans returns a UnionAll tree of Scan operators with diff --git a/pkg/sql/opt/xform/placeholder_fast_path.go b/pkg/sql/opt/xform/placeholder_fast_path.go index 6663938a84fb..d48dafab9686 100644 --- a/pkg/sql/opt/xform/placeholder_fast_path.go +++ b/pkg/sql/opt/xform/placeholder_fast_path.go @@ -10,6 +10,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/cat" "github.com/cockroachdb/cockroach/pkg/sql/opt/memo" "github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/buildutil" "github.com/cockroachdb/cockroach/pkg/util/errorutil" @@ -121,7 +122,8 @@ func (o *Optimizer) TryPlaceholderFastPath() (_ opt.Expr, ok bool, err error) { var foundIndex cat.Index for ord, n := 0, tabMeta.Table.IndexCount(); ord < n; ord++ { index := tabMeta.Table.Index(ord) - if index.IsInverted() || index.IsVector() { + if index.Type() != idxtype.FORWARD { + // Skip inverted and vector indexes. continue } diff --git a/pkg/sql/opt/xform/scan_index_iter.go b/pkg/sql/opt/xform/scan_index_iter.go index b0a6a6ee1d8f..4f28052b7cdf 100644 --- a/pkg/sql/opt/xform/scan_index_iter.go +++ b/pkg/sql/opt/xform/scan_index_iter.go @@ -12,6 +12,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/opt/memo" "github.com/cockroachdb/cockroach/pkg/sql/opt/partialidx" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/errors" ) @@ -234,22 +235,22 @@ func (it *scanIndexIter) ForEachStartingAfter(ord int, f enumerateIndexFunc) { } // Skip over inverted indexes if rejectInvertedIndexes is set. - if it.hasRejectFlags(rejectInvertedIndexes) && index.IsInverted() { + if it.hasRejectFlags(rejectInvertedIndexes) && index.Type() == idxtype.INVERTED { continue } // Skip over non-inverted indexes if rejectNonInvertedIndexes is set. - if it.hasRejectFlags(rejectNonInvertedIndexes) && !index.IsInverted() { + if it.hasRejectFlags(rejectNonInvertedIndexes) && index.Type() != idxtype.INVERTED { continue } // Skip over vector indexes if rejectVectorIndexes is set. - if it.hasRejectFlags(rejectVectorIndexes) && index.IsVector() { + if it.hasRejectFlags(rejectVectorIndexes) && index.Type() == idxtype.VECTOR { continue } // Skip over non-vector indexes if rejectNonVectorIndexes is set. - if it.hasRejectFlags(rejectNonVectorIndexes) && !index.IsVector() { + if it.hasRejectFlags(rejectNonVectorIndexes) && index.Type() != idxtype.VECTOR { continue } diff --git a/pkg/sql/opt_catalog.go b/pkg/sql/opt_catalog.go index 93c528a888b9..0b603a2b3381 100644 --- a/pkg/sql/opt_catalog.go +++ b/pkg/sql/opt_catalog.go @@ -34,6 +34,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" @@ -853,7 +854,7 @@ func newOptTable( // Add one for each inverted index column. secondaryIndexes := ot.desc.DeletableNonPrimaryIndexes() for _, index := range secondaryIndexes { - if index.GetType() == descpb.IndexDescriptor_INVERTED { + if index.GetType() == idxtype.INVERTED { numCols++ } } @@ -1002,7 +1003,7 @@ func newOptTable( } } } - if idx.GetType() == descpb.IndexDescriptor_INVERTED { + if idx.GetType() == idxtype.INVERTED { // The inverted column of an inverted index is special: in the // descriptors, it looks as if the table column is part of the // index; in fact the key contains values *derived* from that @@ -1637,7 +1638,7 @@ func (oi *optIndex) init( } // Populate columnOrds. - inverted := oi.IsInverted() + inverted := oi.Type() == idxtype.INVERTED numKeyCols := idx.NumKeyColumns() numKeySuffixCols := idx.NumKeySuffixColumns() oi.columnOrds = make([]int, oi.numCols) @@ -1667,22 +1668,16 @@ func (oi *optIndex) Name() tree.Name { return tree.Name(oi.idx.GetName()) } +// Type is part of the cat.Index interface. +func (oi *optIndex) Type() idxtype.T { + return oi.idx.GetType() +} + // IsUnique is part of the cat.Index interface. func (oi *optIndex) IsUnique() bool { return oi.idx.IsUnique() } -// IsInverted is part of the cat.Index interface. -func (oi *optIndex) IsInverted() bool { - return oi.idx.GetType() == descpb.IndexDescriptor_INVERTED -} - -// IsVector is part of the cat.Index interface. -func (oi *optIndex) IsVector() bool { - // TODO(#137370): check the index type. - return false -} - // GetInvisibility is part of the cat.Index interface. func (oi *optIndex) GetInvisibility() float64 { return oi.idx.GetInvisibility() @@ -1710,7 +1705,7 @@ func (oi *optIndex) LaxKeyColumnCount() int { // PrefixColumnCount is part of the cat.Index interface. func (oi *optIndex) PrefixColumnCount() int { - if !oi.IsInverted() && !oi.IsVector() { + if !oi.Type().AllowsPrefixColumns() { panic(errors.AssertionFailedf("only inverted and vector indexes have prefix columns")) } return oi.idx.NumKeyColumns() - 1 @@ -1729,7 +1724,7 @@ func (oi *optIndex) Column(i int) cat.IndexColumn { // InvertedColumn is part of the cat.Index interface. func (oi *optIndex) InvertedColumn() cat.IndexColumn { - if !oi.IsInverted() { + if oi.Type() != idxtype.INVERTED { panic(errors.AssertionFailedf("non-inverted indexes do not have inverted columns")) } ord := oi.idx.NumKeyColumns() - 1 @@ -1738,7 +1733,7 @@ func (oi *optIndex) InvertedColumn() cat.IndexColumn { // VectorColumn is part of the cat.Index interface. func (oi *optIndex) VectorColumn() cat.IndexColumn { - if !oi.IsVector() { + if oi.Type() != idxtype.VECTOR { panic(errors.AssertionFailedf("non-vector indexes do not have inverted columns")) } ord := oi.idx.NumKeyColumns() - 1 @@ -2606,6 +2601,11 @@ func (oi *optVirtualIndex) Name() tree.Name { return tree.Name(oi.idx.GetName()) } +// Type is part of the cat.Index interface. +func (oi *optVirtualIndex) Type() idxtype.T { + return idxtype.FORWARD +} + // IsUnique is part of the cat.Index interface. func (oi *optVirtualIndex) IsUnique() bool { if oi.idx == nil { @@ -2615,16 +2615,6 @@ func (oi *optVirtualIndex) IsUnique() bool { return oi.idx.IsUnique() } -// IsInverted is part of the cat.Index interface. -func (oi *optVirtualIndex) IsInverted() bool { - return false -} - -// IsVector is part of the cat.Index interface. -func (oi *optVirtualIndex) IsVector() bool { - return false -} - // GetInvisibility is part of the cat.Index interface. func (oi *optVirtualIndex) GetInvisibility() float64 { return 0.0 diff --git a/pkg/sql/parser/BUILD.bazel b/pkg/sql/parser/BUILD.bazel index 70b289f3c78e..b170d31cbcc3 100644 --- a/pkg/sql/parser/BUILD.bazel +++ b/pkg/sql/parser/BUILD.bazel @@ -31,6 +31,7 @@ go_library( "//pkg/sql/privilege", # keep "//pkg/sql/scanner", "//pkg/sql/sem/builtins/builtinsregistry", + "//pkg/sql/sem/idxtype", # keep "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treebin", # keep "//pkg/sql/sem/tree/treecmp", # keep diff --git a/pkg/sql/parser/sql.y b/pkg/sql/parser/sql.y index 9dddfb4afa67..9d3f2936a8f7 100644 --- a/pkg/sql/parser/sql.y +++ b/pkg/sql/parser/sql.y @@ -40,6 +40,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treebin" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treewindow" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/vector" "github.com/cockroachdb/errors" @@ -940,8 +941,8 @@ func (u *sqlSymUnion) triggerTransitions() []*tree.TriggerTransition { func (u *sqlSymUnion) triggerForEach() tree.TriggerForEach { return u.val.(tree.TriggerForEach) } -func (u *sqlSymUnion) indexType() tree.IndexType { - return u.val.(tree.IndexType) +func (u *sqlSymUnion) indexType() idxtype.T { + return u.val.(idxtype.T) } func (u *sqlSymUnion) doBlockOptions() tree.DoBlockOptions { return u.val.(tree.DoBlockOptions) @@ -1546,7 +1547,7 @@ func (u *sqlSymUnion) doBlockOption() tree.DoBlockOption { %type <[]*tree.Order> sortby_list sortby_no_index_list %type index_params create_as_params %type opt_index_visible alter_index_visible -%type opt_index_access_method +%type opt_index_access_method %type name_list privilege_list %type <[]int32> opt_array_bounds %type <*tree.Batch> opt_batch_clause @@ -11231,7 +11232,7 @@ index_def: $$.val = &tree.IndexTableDef{ Name: "", Columns: $4.idxElems(), - Type: tree.IndexTypeInverted, + Type: idxtype.INVERTED, PartitionByIndex: $6.partitionByIndex(), StorageParams: $7.storageParams(), Predicate: $8.expr(), @@ -11243,7 +11244,7 @@ index_def: $$.val = &tree.IndexTableDef{ Name: tree.Name($3), Columns: $5.idxElems(), - Type: tree.IndexTypeInverted, + Type: idxtype.INVERTED, PartitionByIndex: $7.partitionByIndex(), StorageParams: $8.storageParams(), Predicate: $9.expr(), @@ -11255,7 +11256,7 @@ index_def: $$.val = &tree.IndexTableDef{ Name: "", Columns: $4.idxElems(), - Type: tree.IndexTypeVector, + Type: idxtype.VECTOR, PartitionByIndex: $6.partitionByIndex(), StorageParams: $7.storageParams(), Predicate: $8.expr(), @@ -11267,7 +11268,7 @@ index_def: $$.val = &tree.IndexTableDef{ Name: tree.Name($3), Columns: $5.idxElems(), - Type: tree.IndexTypeVector, + Type: idxtype.VECTOR, PartitionByIndex: $7.partitionByIndex(), StorageParams: $8.storageParams(), Predicate: $9.expr(), @@ -12236,7 +12237,7 @@ create_index_stmt: Name: tree.Name($6), Table: table, Unique: $2.bool(), - Type: tree.IndexTypeInverted, + Type: idxtype.INVERTED, Columns: $10.idxElems(), Storing: $12.nameList(), PartitionByIndex: $13.partitionByIndex(), @@ -12253,7 +12254,7 @@ create_index_stmt: Name: tree.Name($9), Table: table, Unique: $2.bool(), - Type: tree.IndexTypeInverted, + Type: idxtype.INVERTED, IfNotExists: true, Columns: $13.idxElems(), Storing: $15.nameList(), @@ -12271,7 +12272,7 @@ create_index_stmt: Name: tree.Name($6), Table: table, Unique: $2.bool(), - Type: tree.IndexTypeVector, + Type: idxtype.VECTOR, Columns: $10.idxElems(), Storing: $12.nameList(), PartitionByIndex: $13.partitionByIndex(), @@ -12288,7 +12289,7 @@ create_index_stmt: Name: tree.Name($9), Table: table, Unique: $2.bool(), - Type: tree.IndexTypeVector, + Type: idxtype.VECTOR, IfNotExists: true, Columns: $13.idxElems(), Storing: $15.nameList(), @@ -12305,14 +12306,14 @@ opt_index_access_method: USING name { /* FORCE DOC */ - var val tree.IndexType + var val idxtype.T switch $2 { case "gin", "gist": - val = tree.IndexTypeInverted + val = idxtype.INVERTED case "btree": - val = tree.IndexTypeForward + val = idxtype.FORWARD case "cspann": - val = tree.IndexTypeVector + val = idxtype.VECTOR case "hash", "spgist", "brin": return unimplemented(sqllex, "index using " + $2) default: @@ -12323,7 +12324,7 @@ opt_index_access_method: } | /* EMPTY */ { - $$.val = tree.IndexTypeForward + $$.val = idxtype.FORWARD } opt_concurrently: diff --git a/pkg/sql/pg_catalog.go b/pkg/sql/pg_catalog.go index 209439f238a4..31dccbe4cef8 100644 --- a/pkg/sql/pg_catalog.go +++ b/pkg/sql/pg_catalog.go @@ -41,6 +41,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/sem/cast" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/semenumpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" @@ -816,7 +817,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-class.html`, // Indexes. return catalog.ForEachIndex(table, catalog.IndexOpts{}, func(index catalog.Index) error { indexType := forwardIndexOid - if index.GetType() == descpb.IndexDescriptor_INVERTED { + if index.GetType() == idxtype.INVERTED { indexType = invertedIndexOid } ownerOid, err := getOwnerOID(ctx, p, table) diff --git a/pkg/sql/randgen/BUILD.bazel b/pkg/sql/randgen/BUILD.bazel index fbf3afb9198e..2751084b3049 100644 --- a/pkg/sql/randgen/BUILD.bazel +++ b/pkg/sql/randgen/BUILD.bazel @@ -31,6 +31,7 @@ go_library( "//pkg/sql/rowenc/valueside", "//pkg/sql/sem/cast", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sem/tree/treebin", "//pkg/sql/sem/tree/treecmp", diff --git a/pkg/sql/randgen/mutator.go b/pkg/sql/randgen/mutator.go index bd112ef3258e..e7a32d5bc0a5 100644 --- a/pkg/sql/randgen/mutator.go +++ b/pkg/sql/randgen/mutator.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/parser" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/stats" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -806,7 +807,7 @@ func postgresCreateTableMutator( // TODO(rafi): Postgres supports inverted indexes with a different // syntax than Cockroach. Maybe we could add it later. // The syntax is `CREATE INDEX name ON table USING gin(column)`. - if def.Type == tree.IndexTypeForward { + if def.Type == idxtype.FORWARD { mutated = append(mutated, &tree.CreateIndex{ Name: def.Name, Table: mutatedStmt.Table, @@ -1031,8 +1032,7 @@ func indexStoringMutator(rng *rand.Rand, stmts []tree.Statement) ([]tree.Stateme for _, stmt := range stmts { switch ast := stmt.(type) { case *tree.CreateIndex: - if ast.Type != tree.IndexTypeForward { - // Only forward indexes support STORING columns for now. + if !ast.Type.SupportsStoring() { continue } info, ok := tables[ast.Table.ObjectName] @@ -1063,8 +1063,7 @@ func indexStoringMutator(rng *rand.Rand, stmts []tree.Statement) ([]tree.Stateme idx = &defType.IndexTableDef } } - if idx == nil || idx.Type != tree.IndexTypeForward { - // STORING is not currently supported by non-forward indexes. + if idx == nil || !idx.Type.SupportsStoring() { continue } // If we don't have a storing list, make one with 50% chance. diff --git a/pkg/sql/randgen/schema.go b/pkg/sql/randgen/schema.go index 7918528976b3..7c05da886de3 100644 --- a/pkg/sql/randgen/schema.go +++ b/pkg/sql/randgen/schema.go @@ -22,6 +22,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/parser" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util" @@ -228,7 +229,7 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName( if opt.IsSet(TableOptPrimaryIndexRequired) || (rng.Intn(8) != 0) { for { indexDef, ok := randIndexTableDefFromCols(ctx, rng, columnDefs, tableName, true /* isPrimaryIndex */, opt) - canUseIndex := ok && indexDef.Type == tree.IndexTypeForward + canUseIndex := ok && indexDef.Type.CanBePrimary() if canUseIndex { // Although not necessary for Cockroach to function correctly, // but for ease of use for any code that introspects on the @@ -260,8 +261,9 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName( if !ok { continue } - if indexDef.Type == tree.IndexTypeInverted && pk != nil { - // Inverted indexes aren't permitted to be created on primary key columns. + if !indexDef.Type.CanBePrimary() && pk != nil { + // Inverted/vector indexes aren't permitted to be created on primary + // key columns. col := indexDef.Columns[len(indexDef.Columns)-1] foundOverlap := false for _, pkCol := range pk.Columns { @@ -274,9 +276,9 @@ func randCreateTableWithColumnIndexNumberGeneratorAndName( continue } } - // Make forward indexes unique 50% of the time. Inverted indexes cannot + // Make forward indexes unique 50% of the time. Other index types cannot // be unique. - unique := indexDef.Type == tree.IndexTypeForward && rng.Intn(2) == 0 + unique := indexDef.Type.CanBeUnique() && rng.Intn(2) == 0 if unique { defs = append(defs, &tree.UniqueConstraintTableDef{ IndexTableDef: indexDef, @@ -616,13 +618,13 @@ func randIndexTableDefFromCols( // The last index column can be inverted-indexable, which makes the // index an inverted index. if colinfo.ColumnTypeIsOnlyInvertedIndexable(semType) { - def.Type = tree.IndexTypeInverted + def.Type = idxtype.INVERTED stopPrefix = true } else if isLastCol && !stopPrefix && invertedIndexable { // With 1/4 probability, choose to use an inverted index for a column type // that is both inverted indexable and forward indexable. if rng.Intn(4) == 0 { - def.Type = tree.IndexTypeInverted + def.Type = idxtype.INVERTED stopPrefix = true if semType.Family() == types.StringFamily { elem.OpClass = "gin_trgm_ops" @@ -631,7 +633,7 @@ func randIndexTableDefFromCols( } // Last column for inverted indexes must always be ascending. - if i == nCols-1 && def.Type == tree.IndexTypeInverted { + if i == nCols-1 && def.Type == idxtype.INVERTED { elem.Direction = tree.Ascending } @@ -644,7 +646,7 @@ func randIndexTableDefFromCols( // An inverted index column cannot be DESC, so use either the default // direction or ASC. - if def.Type == tree.IndexTypeInverted { + if def.Type == idxtype.INVERTED { dir := tree.Direction(rng.Intn(int(tree.Ascending) + 1)) def.Columns[len(def.Columns)-1].Direction = dir } diff --git a/pkg/sql/row/BUILD.bazel b/pkg/sql/row/BUILD.bazel index d8d0faf01649..62f3d47e5c25 100644 --- a/pkg/sql/row/BUILD.bazel +++ b/pkg/sql/row/BUILD.bazel @@ -62,6 +62,7 @@ go_library( "//pkg/sql/scrub", "//pkg/sql/sem/builtins/builtinconstants", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/transform", "//pkg/sql/sem/tree", "//pkg/sql/sem/volatility", diff --git a/pkg/sql/row/helper.go b/pkg/sql/row/helper.go index bbc4062627b4..cc4592097fd5 100644 --- a/pkg/sql/row/helper.go +++ b/pkg/sql/row/helper.go @@ -27,6 +27,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/rowenc/rowencpb" "github.com/cockroachdb/cockroach/pkg/sql/rowenc/valueside" "github.com/cockroachdb/cockroach/pkg/sql/rowinfra" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -240,7 +241,7 @@ func (rh *RowHelper) encodeTombstonesForIndex( if !index.IsUnique() { return nil, errors.AssertionFailedf("Expected index %s to be unique", index.GetName()) } - if index.GetType() != descpb.IndexDescriptor_FORWARD { + if index.GetType() != idxtype.FORWARD { return nil, errors.AssertionFailedf("Expected index %s to be a forward index", index.GetName()) } diff --git a/pkg/sql/row/updater.go b/pkg/sql/row/updater.go index 7eed190609a1..518432dc22b2 100644 --- a/pkg/sql/row/updater.go +++ b/pkg/sql/row/updater.go @@ -18,6 +18,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" "github.com/cockroachdb/cockroach/pkg/sql/rowinfra" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/intsets" "github.com/cockroachdb/cockroach/pkg/util/log" @@ -310,7 +311,7 @@ func (ru *Updater) UpdateRow( return nil, err } } - if ru.Helper.Indexes[i].GetType() == descpb.IndexDescriptor_INVERTED && !ru.Helper.Indexes[i].IsTemporaryIndexForBackfill() { + if ru.Helper.Indexes[i].GetType() == idxtype.INVERTED && !ru.Helper.Indexes[i].IsTemporaryIndexForBackfill() { // Deduplicate the keys we're adding and removing if we're updating an // inverted index. For example, imagine a table with an inverted index on j: // @@ -379,7 +380,7 @@ func (ru *Updater) UpdateRow( // in the new and old values. var writtenIndexes intsets.Fast for i, index := range ru.Helper.Indexes { - if index.GetType() == descpb.IndexDescriptor_FORWARD { + if index.GetType() == idxtype.FORWARD { oldIdx, newIdx := 0, 0 oldEntries, newEntries := ru.oldIndexEntries[i], ru.newIndexEntries[i] // The index entries for a particular index are stored in diff --git a/pkg/sql/rowenc/BUILD.bazel b/pkg/sql/rowenc/BUILD.bazel index ecf48682638f..ca362dbba5d4 100644 --- a/pkg/sql/rowenc/BUILD.bazel +++ b/pkg/sql/rowenc/BUILD.bazel @@ -29,6 +29,7 @@ go_library( "//pkg/sql/rowenc/rowencpb", "//pkg/sql/rowenc/valueside", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/sqlerrors", "//pkg/sql/types", @@ -79,6 +80,7 @@ go_test( "//pkg/sql/randgen", "//pkg/sql/rowenc/valueside", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/testutils/datapathutils", diff --git a/pkg/sql/rowenc/index_encoding.go b/pkg/sql/rowenc/index_encoding.go index 1e1a92c41259..347cbefd633a 100644 --- a/pkg/sql/rowenc/index_encoding.go +++ b/pkg/sql/rowenc/index_encoding.go @@ -26,6 +26,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/rowenc/rowencpb" "github.com/cockroachdb/cockroach/pkg/sql/rowenc/valueside" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -1299,7 +1300,7 @@ func EncodeSecondaryIndexKey( var containsNull = false var secondaryKeys [][]byte var err error - if secondaryIndex.GetType() == descpb.IndexDescriptor_INVERTED { + if secondaryIndex.GetType() == idxtype.INVERTED { secondaryKeys, err = EncodeInvertedIndexKeys(ctx, secondaryIndex, colMap, values, secondaryIndexKeyPrefix) } else { var secondaryIndexKey []byte @@ -1360,7 +1361,7 @@ func EncodeSecondaryIndex( } if tableDesc.NumFamilies() == 1 || - secondaryIndex.GetType() == descpb.IndexDescriptor_INVERTED || + secondaryIndex.GetType() == idxtype.INVERTED || secondaryIndex.GetVersion() == descpb.BaseIndexFormatVersion { // We do all computation that affects indexes with families in a separate code path to avoid performance // regression for tables without column families. @@ -1560,7 +1561,7 @@ func GetValueColumns(index catalog.Index) []ValueEncodedColumn { id := index.GetCompositeColumnID(i) // Inverted indexes on a composite type (i.e. an array of composite types) // should not add the indexed column to the value. - if index.GetType() == descpb.IndexDescriptor_INVERTED && id == index.InvertedColumnID() { + if index.GetType() == idxtype.INVERTED && id == index.InvertedColumnID() { continue } cols = append(cols, ValueEncodedColumn{ColID: id, IsComposite: true}) diff --git a/pkg/sql/rowenc/index_encoding_test.go b/pkg/sql/rowenc/index_encoding_test.go index c29863978865..9b7049108a04 100644 --- a/pkg/sql/rowenc/index_encoding_test.go +++ b/pkg/sql/rowenc/index_encoding_test.go @@ -26,6 +26,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/randgen" . "github.com/cockroachdb/cockroach/pkg/sql/rowenc" "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util" @@ -49,7 +50,7 @@ func makeTableDescForTest( secondaryColumnIDs := make([]descpb.ColumnID, len(test.secondaryValues)) columns := make([]descpb.ColumnDescriptor, len(test.primaryValues)+len(test.secondaryValues)) var colMap catalog.TableColMap - secondaryType := descpb.IndexDescriptor_FORWARD + secondaryType := idxtype.FORWARD for i := range columns { columns[i] = descpb.ColumnDescriptor{ ID: descpb.ColumnID(i + 1), @@ -61,10 +62,10 @@ func makeTableDescForTest( } else { columns[i].Type = test.secondaryValues[i-len(test.primaryValues)].ResolvedType() if colinfo.ColumnTypeIsInvertedIndexable(columns[i].Type) { - secondaryType = descpb.IndexDescriptor_INVERTED + secondaryType = idxtype.INVERTED } if isSecondaryIndexForward && columns[i].Type.Family() == types.JsonFamily { - secondaryType = descpb.IndexDescriptor_FORWARD + secondaryType = idxtype.FORWARD } secondaryColumnIDs[i-len(test.primaryValues)] = columns[i].ID } diff --git a/pkg/sql/rowenc/index_fetch.go b/pkg/sql/rowenc/index_fetch.go index c21a03ffae35..cac40e6a80bd 100644 --- a/pkg/sql/rowenc/index_fetch.go +++ b/pkg/sql/rowenc/index_fetch.go @@ -10,6 +10,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/fetchpb" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/util/buildutil" "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/errors" @@ -72,7 +73,7 @@ func InitIndexFetchSpec( s.KeyAndSuffixColumns = table.IndexFetchSpecKeyAndSuffixColumns(index) var invertedColumnID descpb.ColumnID - if index.GetType() == descpb.IndexDescriptor_INVERTED { + if index.GetType() == idxtype.INVERTED { invertedColumnID = index.InvertedColumnID() } diff --git a/pkg/sql/scan.go b/pkg/sql/scan.go index 33ab379dc015..8708a15fb147 100644 --- a/pkg/sql/scan.go +++ b/pkg/sql/scan.go @@ -230,7 +230,7 @@ func (n *scanNode) initDescSpecificCol(colCfg scanColumnsConfig, prefixCol catal // table where prefixCol is the key column. foundIndex := false for _, idx := range indexes { - if idx.GetType() != descpb.IndexDescriptor_FORWARD || idx.IsPartial() { + if idx.GetType().AllowsPrefixColumns() || idx.IsPartial() { continue } columns := n.desc.IndexKeyColumns(idx) diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel index c6b2986e767c..3152118033fb 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/BUILD.bazel @@ -94,6 +94,7 @@ go_library( "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/transform", "//pkg/sql/sem/tree", "//pkg/sql/sem/volatility", diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go index 2bb019d92f59..0b3d15b69922 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/alter_table_add_column.go @@ -30,6 +30,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" @@ -680,7 +681,7 @@ func addSecondaryIndexTargetsForAddColumn( TableID: tbl.TableID, IndexID: desc.ID, IsUnique: desc.Unique, - IsInverted: desc.Type == descpb.IndexDescriptor_INVERTED, + IsInverted: desc.Type == idxtype.INVERTED, SourceIndexID: newPrimaryIdx.IndexID, IsNotVisible: desc.NotVisible, Invisibility: desc.Invisibility, diff --git a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/create_index.go b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/create_index.go index 6cfaf795288e..f918be835358 100644 --- a/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/create_index.go +++ b/pkg/sql/schemachanger/scbuild/internal/scbuildstmt/create_index.go @@ -29,6 +29,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/screl" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/sqlerrors" "github.com/cockroachdb/cockroach/pkg/sql/storageparam" @@ -51,7 +52,7 @@ func CreateIndex(b BuildCtx, n *tree.CreateIndex) { idxSpec.secondary = &scpb.SecondaryIndex{ Index: scpb.Index{ IsUnique: n.Unique, - IsInverted: n.Type == tree.IndexTypeInverted, + IsInverted: n.Type == idxtype.INVERTED, IsConcurrently: n.Concurrently, IsNotVisible: n.Invisibility.Value != 0.0, Invisibility: n.Invisibility.Value, @@ -144,22 +145,26 @@ func CreateIndex(b BuildCtx, n *tree.CreateIndex) { } panicIfSchemaChangeIsDisallowed(relationElements, n) - // Vector indexes are note yet supported. - if n.Type == tree.IndexTypeVector { + // Vector indexes are not yet supported. + if n.Type == idxtype.VECTOR { panic(unimplemented.NewWithIssuef(137370, "VECTOR indexes are not yet supported")) } + if !n.Type.SupportsSharding() && n.Sharded != nil { + panic(pgerror.Newf(pgcode.InvalidSQLStatementName, + "%s indexes don't support hash sharding", strings.ToLower(n.Type.String()))) + } + if !n.Type.SupportsStoring() && len(n.Storing) > 0 { + panic(pgerror.Newf(pgcode.InvalidSQLStatementName, + "%s indexes don't support stored columns", strings.ToLower(n.Type.String()))) + } + if !n.Type.CanBeUnique() && n.Unique { + panic(pgerror.Newf(pgcode.InvalidSQLStatementName, + "%s indexes can't be unique", strings.ToLower(n.Type.String()))) + } + // Inverted indexes do not support hash sharding or unique. - if n.Type == tree.IndexTypeInverted { - if n.Sharded != nil { - panic(pgerror.New(pgcode.InvalidSQLStatementName, "inverted indexes don't support hash sharding")) - } - if len(n.Storing) > 0 { - panic(pgerror.New(pgcode.InvalidSQLStatementName, "inverted indexes don't support stored columns")) - } - if n.Unique { - panic(pgerror.New(pgcode.InvalidSQLStatementName, "inverted indexes can't be unique")) - } + if n.Type == idxtype.INVERTED { b.IncrementSchemaChangeIndexCounter("inverted") if len(n.Columns) > 1 { b.IncrementSchemaChangeIndexCounter("multi_column_inverted") @@ -301,16 +306,16 @@ func processColNodeType( ) catpb.InvertedIndexColumnKind { invertedKind := catpb.InvertedIndexColumnKind_DEFAULT // OpClass are only allowed for the last column of an inverted index. - if columnNode.OpClass != "" && (!lastColIdx || n.Type != tree.IndexTypeInverted) { + if columnNode.OpClass != "" && (!lastColIdx || !n.Type.SupportsOpClass()) { panic(pgerror.New(pgcode.DatatypeMismatch, "operator classes are only allowed for the last column of an inverted index")) } // Disallow descending last columns in inverted indexes. - if n.Type == tree.IndexTypeInverted && columnNode.Direction == tree.Descending && lastColIdx { + if !n.Type.AllowExplicitDirection() && columnNode.Direction == tree.Descending && lastColIdx { panic(pgerror.New(pgcode.FeatureNotSupported, "the last column in an inverted index cannot have the DESC option")) } - if n.Type == tree.IndexTypeInverted && lastColIdx { + if n.Type.SupportsOpClass() && lastColIdx { switch columnType.Type.Family() { case types.ArrayFamily: switch columnNode.OpClass { @@ -375,7 +380,7 @@ func processColNodeType( }) } // Only certain column types are supported for inverted indexes. - if n.Type == tree.IndexTypeInverted && lastColIdx && + if n.Type == idxtype.INVERTED && lastColIdx && !colinfo.ColumnTypeIsInvertedIndexable(columnType.Type) { colNameForErr := colName if columnNode.Expr != nil { @@ -383,7 +388,7 @@ func processColNodeType( } panic(tabledesc.NewInvalidInvertedColumnError(colNameForErr, columnType.Type.String())) - } else if (n.Type != tree.IndexTypeInverted || !lastColIdx) && !colinfo.ColumnTypeIsIndexable(columnType.Type) { + } else if (n.Type != idxtype.INVERTED || !lastColIdx) && !colinfo.ColumnTypeIsIndexable(columnType.Type) { // Otherwise, check if the column type is indexable. panic(unimplemented.NewWithIssueDetailf(35730, columnType.Type.DebugString(), @@ -488,7 +493,7 @@ func maybeAddPartitionDescriptorForIndex(b BuildCtx, n *tree.CreateIndex, idxSpe columnsToPrepend = append(columnsToPrepend, newIndexColumn) } idxSpec.columns = append(columnsToPrepend, idxSpec.columns...) - if n.Type == tree.IndexTypeInverted { + if n.Type == idxtype.INVERTED { b.IncrementSchemaChangeIndexCounter("partitioned_inverted") } } @@ -793,7 +798,7 @@ func maybeCreateVirtualColumnForIndex( tn *tree.TableName, tbl *scpb.Table, expr tree.Expr, - typ tree.IndexType, + typ idxtype.T, lastColumn bool, ) string { validateColumnIndexableType := func(t *types.T) { @@ -808,7 +813,7 @@ func maybeCreateVirtualColumnForIndex( )) } // Check if the column type is indexable for forward indexes. - if typ == tree.IndexTypeForward && !colinfo.ColumnTypeIsIndexable(t) { + if typ == idxtype.FORWARD && !colinfo.ColumnTypeIsIndexable(t) { panic(pgerror.Newf( pgcode.InvalidTableDefinition, "index element %s of type %s is not indexable", @@ -816,7 +821,7 @@ func maybeCreateVirtualColumnForIndex( t.Name())) } // Check if inverted columns are invertible. - if typ == tree.IndexTypeInverted && !lastColumn && !colinfo.ColumnTypeIsIndexable(t) { + if typ == idxtype.INVERTED && !lastColumn && !colinfo.ColumnTypeIsIndexable(t) { panic(errors.WithHint( pgerror.Newf( pgcode.InvalidTableDefinition, @@ -827,7 +832,7 @@ func maybeCreateVirtualColumnForIndex( "see the documentation for more information about inverted indexes: "+docs.URL("inverted-indexes.html"), )) } - if typ == tree.IndexTypeInverted && lastColumn && !colinfo.ColumnTypeIsInvertedIndexable(t) { + if typ == idxtype.INVERTED && lastColumn && !colinfo.ColumnTypeIsInvertedIndexable(t) { panic(errors.WithHint( pgerror.Newf( pgcode.InvalidTableDefinition, @@ -908,7 +913,7 @@ func maybeAddIndexPredicate(b BuildCtx, n *tree.CreateIndex, idxSpec *indexSpec) expr := b.PartialIndexPredicateExpression(idxSpec.secondary.TableID, n.Predicate) idxSpec.secondary.EmbeddedExpr = b.WrapExpression(idxSpec.secondary.TableID, expr) b.IncrementSchemaChangeIndexCounter("partial") - if n.Type == tree.IndexTypeInverted { + if n.Type == idxtype.INVERTED { b.IncrementSchemaChangeIndexCounter("partial_inverted") } } diff --git a/pkg/sql/schemachanger/scdecomp/BUILD.bazel b/pkg/sql/schemachanger/scdecomp/BUILD.bazel index 3e13a4fd68a2..ccf39a423872 100644 --- a/pkg/sql/schemachanger/scdecomp/BUILD.bazel +++ b/pkg/sql/schemachanger/scdecomp/BUILD.bazel @@ -24,6 +24,7 @@ go_library( "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util/iterutil", diff --git a/pkg/sql/schemachanger/scdecomp/decomp.go b/pkg/sql/schemachanger/scdecomp/decomp.go index 4cd87b8406c8..7e4f90e41b12 100644 --- a/pkg/sql/schemachanger/scdecomp/decomp.go +++ b/pkg/sql/schemachanger/scdecomp/decomp.go @@ -20,6 +20,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/iterutil" "github.com/cockroachdb/cockroach/pkg/util/protoutil" @@ -635,7 +636,7 @@ func (w *walkCtx) walkIndex(tbl catalog.TableDescriptor, idx catalog.Index) { TableID: tbl.GetID(), IndexID: idx.GetID(), IsUnique: idx.IsUnique(), - IsInverted: idx.GetType() == descpb.IndexDescriptor_INVERTED, + IsInverted: idx.GetType() == idxtype.INVERTED, IsCreatedExplicitly: idx.IsCreatedExplicitly(), ConstraintID: idx.GetConstraintID(), IsNotVisible: idx.GetInvisibility() != 0.0, diff --git a/pkg/sql/schemachanger/scexec/BUILD.bazel b/pkg/sql/schemachanger/scexec/BUILD.bazel index 9258648b7aee..514a99f00b70 100644 --- a/pkg/sql/schemachanger/scexec/BUILD.bazel +++ b/pkg/sql/schemachanger/scexec/BUILD.bazel @@ -38,6 +38,7 @@ go_library( "//pkg/sql/schemachanger/scpb", "//pkg/sql/schemachanger/scplan", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sessiondata", "//pkg/util/ctxgroup", "//pkg/util/hlc", @@ -107,6 +108,7 @@ go_test( "//pkg/util/timeutil", "@com_github_golang_mock//gomock", "@com_github_stretchr_testify//require", + "//pkg/sql/sem/idxtype", ], ) diff --git a/pkg/sql/schemachanger/scexec/exec_backfill_test.go b/pkg/sql/schemachanger/scexec/exec_backfill_test.go index 44ea64afa0a9..794ada59f5f8 100644 --- a/pkg/sql/schemachanger/scexec/exec_backfill_test.go +++ b/pkg/sql/schemachanger/scexec/exec_backfill_test.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scdeps/sctestdeps" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -83,7 +84,7 @@ func TestExecBackfiller(t *testing.T) { KeyColumnDirections: dirs, KeyColumnIDs: columnIDs, KeySuffixColumnIDs: keySuffixColumnIDs, - Type: descpb.IndexDescriptor_FORWARD, + Type: idxtype.FORWARD, CreatedExplicitly: true, EncodingType: catenumpb.SecondaryIndexEncoding, UseDeletePreservingEncoding: isTempIndex, diff --git a/pkg/sql/schemachanger/scexec/exec_validation.go b/pkg/sql/schemachanger/scexec/exec_validation.go index f5b663292463..1f5fb34e3e7e 100644 --- a/pkg/sql/schemachanger/scexec/exec_validation.go +++ b/pkg/sql/schemachanger/scexec/exec_validation.go @@ -9,9 +9,9 @@ import ( "context" "github.com/cockroachdb/cockroach/pkg/sql/catalog" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scerrors" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/errors" ) @@ -34,7 +34,7 @@ func executeValidateUniqueIndex( } // Execute the validation operation as a node user. execOverride := sessiondata.NodeUserSessionDataOverride - if index.GetType() == descpb.IndexDescriptor_FORWARD { + if index.GetType() == idxtype.FORWARD { err = deps.Validator().ValidateForwardIndexes(ctx, deps.TransactionalJobRegistry().CurrentJob(), table, []catalog.Index{index}, execOverride) } else { err = deps.Validator().ValidateInvertedIndexes(ctx, deps.TransactionalJobRegistry().CurrentJob(), table, []catalog.Index{index}, execOverride) diff --git a/pkg/sql/schemachanger/scexec/scmutationexec/BUILD.bazel b/pkg/sql/schemachanger/scexec/scmutationexec/BUILD.bazel index 88605ad08c64..9c313364c25c 100644 --- a/pkg/sql/schemachanger/scexec/scmutationexec/BUILD.bazel +++ b/pkg/sql/schemachanger/scexec/scmutationexec/BUILD.bazel @@ -52,6 +52,7 @@ go_library( "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util/iterutil", diff --git a/pkg/sql/schemachanger/scexec/scmutationexec/index.go b/pkg/sql/schemachanger/scexec/scmutationexec/index.go index 660195d45261..162b5042a979 100644 --- a/pkg/sql/schemachanger/scexec/scmutationexec/index.go +++ b/pkg/sql/schemachanger/scexec/scmutationexec/index.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/errors" ) @@ -61,9 +62,9 @@ func addNewIndexMutation( } // Set up the index descriptor type. - indexType := descpb.IndexDescriptor_FORWARD + indexType := idxtype.FORWARD if opIndex.IsInverted { - indexType = descpb.IndexDescriptor_INVERTED + indexType = idxtype.INVERTED } // Set up the encoding type. encodingType := catenumpb.PrimaryIndexEncoding @@ -408,7 +409,7 @@ func (i *immediateVisitor) AddColumnToIndex(ctx context.Context, op scop.AddColu }) } // If this is an inverted column, note that. - if indexDesc.Type == descpb.IndexDescriptor_INVERTED && op.ColumnID == indexDesc.InvertedColumnID() { + if indexDesc.Type == idxtype.INVERTED && op.ColumnID == indexDesc.InvertedColumnID() { indexDesc.InvertedColumnKinds = []catpb.InvertedIndexColumnKind{op.InvertedKind} } return nil @@ -447,7 +448,7 @@ func (i *immediateVisitor) RemoveColumnFromIndex( idx.KeyColumnNames = idx.KeyColumnNames[:i] idx.KeyColumnIDs = idx.KeyColumnIDs[:i] idx.KeyColumnDirections = idx.KeyColumnDirections[:i] - if idx.Type == descpb.IndexDescriptor_INVERTED && i == len(idx.KeyColumnIDs)-1 { + if idx.Type == idxtype.INVERTED && i == len(idx.KeyColumnIDs)-1 { idx.InvertedColumnKinds = nil } } diff --git a/pkg/sql/sem/idxtype/BUILD.bazel b/pkg/sql/sem/idxtype/BUILD.bazel new file mode 100644 index 000000000000..79bfd0e3fecf --- /dev/null +++ b/pkg/sql/sem/idxtype/BUILD.bazel @@ -0,0 +1,49 @@ +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("//pkg/testutils:buildutil/buildutil.bzl", "disallowed_imports_test") + +proto_library( + name = "idxtype_proto", + srcs = ["idxtype.proto"], + strip_import_prefix = "/pkg", + visibility = ["//visibility:public"], + deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"], +) + +go_library( + name = "idxtype", + srcs = ["idxtype.go"], + embed = [":idxtype_go_proto"], + importpath = "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype", + visibility = ["//visibility:public"], +) + +go_proto_library( + name = "idxtype_go_proto", + compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"], + importpath = "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype", + proto = ":idxtype_proto", + visibility = ["//visibility:public"], + deps = ["@com_github_gogo_protobuf//gogoproto"], +) + +go_library( + name = "idxtypepb", + srcs = ["idxtype.go"], + importpath = "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtypepb", + visibility = ["//visibility:public"], +) + +# idxtype is meant to stay a leaf package. Never add a heavy-weight dependency. +# In fact, avoid any dependency at all, except protobuf-related. +disallowed_imports_test( + "idxtype", + allowlist = [ + "//pkg/sql/sem/idxtype:idxtype_go_proto", + "@com_github_gogo_protobuf//gogoproto", + "@com_github_gogo_protobuf//proto", + "@com_github_gogo_protobuf//protoc-gen-gogo/descriptor", + ], + disallow_cdeps = True, +) diff --git a/pkg/sql/sem/idxtype/idxtype.go b/pkg/sql/sem/idxtype/idxtype.go new file mode 100644 index 000000000000..2e49b2729884 --- /dev/null +++ b/pkg/sql/sem/idxtype/idxtype.go @@ -0,0 +1,56 @@ +// Copyright 2025 The Cockroach Authors. +// +// Use of this software is governed by the CockroachDB Software License +// included in the /LICENSE file. + +package idxtype + +// CanBePrimary is true if this index type can be the primary index that always +// contains unique keys sorted according to the primary ordering of the table. +// Secondary indexes refer to rows in the primary index by unique key value. +func (t T) CanBePrimary() bool { + return t == FORWARD +} + +// CanBeUnique is true if this index type can be declared as UNIQUE, meaning it +// never contains duplicate keys. +func (t T) CanBeUnique() bool { + return t == FORWARD +} + +// AllowExplicitDirection is true if this index type allows all of its columns +// to specify an explicit ascending or descending direction. For example, +// inverted and vector indexes do not allow the last column in the index to +// specify an explicit direction. +func (t T) AllowExplicitDirection() bool { + return t == FORWARD +} + +// AllowsPrefixColumns is true if this index type allows other columns from the +// table to act as a key prefix that separates indexed values into distinct +// groupings, e.g. by user or customer. +func (t T) AllowsPrefixColumns() bool { + return t == INVERTED || t == VECTOR +} + +// SupportsSharding is true if this index can be hash sharded, meaning that its +// rows are grouped according to a hash value and spread across the keyspace. +func (t T) SupportsSharding() bool { + return t == FORWARD +} + +// SupportsStoring is true if this index allows STORING values, which are +// un-indexed columns from the table that are stored directly in the index for +// faster retrieval. +func (t T) SupportsStoring() bool { + return t == FORWARD +} + +// SupportsOpClass is true if this index allows columns to specify an operator +// class, which defines an alternate set of operators used when sorting and +// querying those columns. +// NOTE: Currently, only inverted indexes support operator classes, and only on +// the last column of the index. +func (t T) SupportsOpClass() bool { + return t == INVERTED +} diff --git a/pkg/sql/sem/idxtype/idxtype.proto b/pkg/sql/sem/idxtype/idxtype.proto new file mode 100644 index 000000000000..e7b34e095bc4 --- /dev/null +++ b/pkg/sql/sem/idxtype/idxtype.proto @@ -0,0 +1,27 @@ +// Copyright 2025 The Cockroach Authors. +// +// Use of this software is governed by the CockroachDB Software License +// included in the /LICENSE file. + +syntax = "proto3"; +package cockroach.sql.sem.idxtype; +option go_package = "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype"; + +import "gogoproto/gogo.proto"; + +// T represents different types of indexes that can be defined on a table. +enum T { + option (gogoproto.goproto_enum_prefix) = false; + + // FORWARD is a standard relational index, containing at most one entry for + // each row in the table (if a partial index, a table row may not have a + // corresponding entry in the index). + FORWARD = 0; + // INVERTED indexes can contain multiple entries for each row in the table, + // which is useful for indexing collection or composite data types like JSONB, + // ARRAY, and GEOGRAPHY. + INVERTED = 1; + // VECTOR indexes high-dimensional vectors to enable rapid similarity search + // using an approximate nearest neighbor (ANN) algorithm. + VECTOR = 2; +}; diff --git a/pkg/sql/sem/tree/BUILD.bazel b/pkg/sql/sem/tree/BUILD.bazel index fb211ca30260..1fec9ed5a401 100644 --- a/pkg/sql/sem/tree/BUILD.bazel +++ b/pkg/sql/sem/tree/BUILD.bazel @@ -146,6 +146,7 @@ go_library( "//pkg/sql/sem/cast", "//pkg/sql/sem/catconstants", "//pkg/sql/sem/catid", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/semenumpb", "//pkg/sql/sem/tree/treebin", "//pkg/sql/sem/tree/treecmp", diff --git a/pkg/sql/sem/tree/create.go b/pkg/sql/sem/tree/create.go index a7938ea0796b..d069aa6b0310 100644 --- a/pkg/sql/sem/tree/create.go +++ b/pkg/sql/sem/tree/create.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/lexbase" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/collatedstring" "github.com/cockroachdb/cockroach/pkg/util/pretty" @@ -223,14 +224,6 @@ func (l *IndexElemList) doc(p *PrettyCfg) pretty.Doc { return p.commaSeparated(d...) } -type IndexType uint8 - -const ( - IndexTypeForward IndexType = iota - IndexTypeInverted - IndexTypeVector -) - type IndexInvisibility struct { Value float64 FloatProvided bool @@ -241,7 +234,7 @@ type CreateIndex struct { Name Name Table TableName Unique bool - Type IndexType + Type idxtype.T IfNotExists bool Columns IndexElemList Sharded *ShardedIndexDef @@ -265,9 +258,9 @@ func (node *CreateIndex) Format(ctx *FmtCtx) { ctx.WriteString("UNIQUE ") } switch node.Type { - case IndexTypeInverted: + case idxtype.INVERTED: ctx.WriteString("INVERTED ") - case IndexTypeVector: + case idxtype.VECTOR: ctx.WriteString("VECTOR ") } ctx.WriteString("INDEX ") @@ -1041,7 +1034,7 @@ type IndexTableDef struct { Columns IndexElemList Sharded *ShardedIndexDef Storing NameList - Type IndexType + Type idxtype.T PartitionByIndex *PartitionByIndex StorageParams StorageParams Predicate Expr @@ -1051,9 +1044,9 @@ type IndexTableDef struct { // Format implements the NodeFormatter interface. func (node *IndexTableDef) Format(ctx *FmtCtx) { switch node.Type { - case IndexTypeInverted: + case idxtype.INVERTED: ctx.WriteString("INVERTED ") - case IndexTypeVector: + case idxtype.VECTOR: ctx.WriteString("VECTOR ") } ctx.WriteString("INDEX ") diff --git a/pkg/sql/sem/tree/pretty.go b/pkg/sql/sem/tree/pretty.go index b785fcc84399..056eaed3387a 100644 --- a/pkg/sql/sem/tree/pretty.go +++ b/pkg/sql/sem/tree/pretty.go @@ -11,6 +11,7 @@ import ( "strconv" "strings" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treecmp" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree/treewindow" "github.com/cockroachdb/cockroach/pkg/sql/types" @@ -1643,9 +1644,9 @@ func (node *CreateIndex) doc(p *PrettyCfg) pretty.Doc { title = append(title, pretty.Keyword("UNIQUE")) } switch node.Type { - case IndexTypeInverted: + case idxtype.INVERTED: title = append(title, pretty.Keyword("INVERTED")) - case IndexTypeVector: + case idxtype.VECTOR: title = append(title, pretty.Keyword("VECTOR")) } title = append(title, pretty.Keyword("INDEX")) @@ -1739,9 +1740,9 @@ func (node *IndexTableDef) doc(p *PrettyCfg) pretty.Doc { title = pretty.ConcatSpace(title, p.Doc(&node.Name)) } switch node.Type { - case IndexTypeInverted: + case idxtype.INVERTED: title = pretty.ConcatSpace(pretty.Keyword("INVERTED"), title) - case IndexTypeVector: + case idxtype.VECTOR: title = pretty.ConcatSpace(pretty.Keyword("VECTOR"), title) } title = pretty.ConcatSpace(title, p.bracket("(", p.Doc(&node.Columns), ")")) diff --git a/pkg/sql/span/BUILD.bazel b/pkg/sql/span/BUILD.bazel index b8e92ecdb6b8..9d0769325176 100644 --- a/pkg/sql/span/BUILD.bazel +++ b/pkg/sql/span/BUILD.bazel @@ -21,6 +21,7 @@ go_library( "//pkg/sql/rowenc", "//pkg/sql/rowenc/keyside", "//pkg/sql/sem/eval", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util/encoding", diff --git a/pkg/sql/span/span_splitter.go b/pkg/sql/span/span_splitter.go index d2512f72519d..ccb886ca2448 100644 --- a/pkg/sql/span/span_splitter.go +++ b/pkg/sql/span/span_splitter.go @@ -11,6 +11,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/util/intsets" "github.com/cockroachdb/errors" ) @@ -86,8 +87,8 @@ func MakeSplitterBase( // If we're looking at a secondary index... if index.GetID() != table.GetPrimaryIndexID() { - // * The index cannot be inverted. - if index.GetType() != descpb.IndexDescriptor_FORWARD { + // * The index must be a forward index. + if index.GetType() != idxtype.FORWARD { return NoopSplitter() } diff --git a/pkg/workload/schemachange/BUILD.bazel b/pkg/workload/schemachange/BUILD.bazel index 1314f9e53e02..538be233ae49 100644 --- a/pkg/workload/schemachange/BUILD.bazel +++ b/pkg/workload/schemachange/BUILD.bazel @@ -32,6 +32,7 @@ go_library( "//pkg/sql/pgwire/pgerror", "//pkg/sql/randgen", "//pkg/sql/sem/catconstants", + "//pkg/sql/sem/idxtype", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util", diff --git a/pkg/workload/schemachange/operation_generator.go b/pkg/workload/schemachange/operation_generator.go index 38d12b71674b..505c906ef751 100644 --- a/pkg/workload/schemachange/operation_generator.go +++ b/pkg/workload/schemachange/operation_generator.go @@ -28,6 +28,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/randgen" "github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/sem/idxtype" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util" @@ -983,10 +984,10 @@ func (og *operationGenerator) createIndex(ctx context.Context, tx pgx.Tx) (*opSt } // TODO(andyk): Do we need to include vector indexes? - indexType := tree.IndexTypeForward + indexType := idxtype.FORWARD if og.randIntn(10) == 0 { // 10% INVERTED - indexType = tree.IndexTypeInverted + indexType = idxtype.INVERTED } def := &tree.CreateIndex{ @@ -1027,7 +1028,7 @@ func (og *operationGenerator) createIndex(ctx context.Context, tx pgx.Tx) (*opSt if columnNames[i].name == regionColumn && i != 0 { duplicateRegionColumn = true } - if def.Type == tree.IndexTypeInverted { + if def.Type == idxtype.INVERTED { // We can have an inverted index on a set of columns if the last column // is an inverted indexable type and the preceding columns are not. invertedIndexableType := colinfo.ColumnTypeIsInvertedIndexable(columnNames[i].typ) @@ -1144,10 +1145,10 @@ func (og *operationGenerator) createIndex(ctx context.Context, tx pgx.Tx) (*opSt stmt.expectedExecErrors.addAll(codesWithConditions{ {code: pgcode.DuplicateRelation, condition: indexExists}, // Inverted indexes do not support stored columns. - {code: pgcode.InvalidSQLStatementName, condition: len(def.Storing) > 0 && def.Type == tree.IndexTypeInverted}, + {code: pgcode.InvalidSQLStatementName, condition: len(def.Storing) > 0 && def.Type == idxtype.INVERTED}, // Inverted indexes cannot be unique. - {code: pgcode.InvalidSQLStatementName, condition: def.Unique && def.Type == tree.IndexTypeInverted}, - {code: pgcode.InvalidSQLStatementName, condition: def.Type == tree.IndexTypeInverted && len(def.Storing) > 0}, + {code: pgcode.InvalidSQLStatementName, condition: def.Unique && def.Type == idxtype.INVERTED}, + {code: pgcode.InvalidSQLStatementName, condition: def.Type == idxtype.INVERTED && len(def.Storing) > 0}, {code: pgcode.DuplicateColumn, condition: duplicateStore}, {code: pgcode.FeatureNotSupported, condition: nonIndexableType}, {code: pgcode.FeatureNotSupported, condition: regionColStored},