Skip to content

Commit

Permalink
idxtype: add idxtype package
Browse files Browse the repository at this point in the history
Add new sql/sem/idxtype package that defines an index type enum with values
like forward, inverted, and vector. Code across SQL needs to check the index
type and previously it used multiple different enums and bools. This commit
continues the process of unifying these mechanisms (future commits will do
even more).

Epic: CRDB-42943

Release note: None
  • Loading branch information
andy-kimball committed Jan 24, 2025
1 parent 6ff1a78 commit 63009ad
Show file tree
Hide file tree
Showing 64 changed files with 218 additions and 103 deletions.
2 changes: 2 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,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",
Expand Down
1 change: 1 addition & 0 deletions pkg/crosscluster/logical/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/crosscluster/logical/logical_replication_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ 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"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"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"
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions pkg/internal/sqlsmith/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 5 additions & 4 deletions pkg/internal/sqlsmith/alter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))]
Expand All @@ -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,
Expand All @@ -366,7 +367,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) {
}
}
var storing tree.NameList
for indexType == tree.IndexTypeForward && s.coin() {
for idxtype.SupportsStoring(indexType) && s.coin() {
col := tableRef.Columns[s.rnd.Intn(len(tableRef.Columns))]
if seen[col.Name] {
continue
Expand All @@ -390,7 +391,7 @@ func makeCreateIndex(s *Smither) (tree.Statement, bool) {
Unique: unique,
Columns: cols,
Storing: storing,
Type: indexType,
Type: tree.IndexType(indexType),
Concurrently: s.coin(),
Invisibility: invisibility,
}, true
Expand Down
7 changes: 4 additions & 3 deletions pkg/internal/sqlsmith/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -499,14 +500,14 @@ 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,
Table: *t.TableName,
Type: indexType,
Type: tree.IndexType(indexType),
}
}
create := indexes[idx]
Expand Down
2 changes: 2 additions & 0 deletions pkg/protos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/alter_primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/backfill/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/backfill/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/catalog/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/catalog/catformat/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/catalog/catformat/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ")
Expand All @@ -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")
Expand Down Expand Up @@ -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:
Expand All @@ -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())
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/catalog/catformat/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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}

Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/catalog/descpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/catalog/descpb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand Down
Loading

0 comments on commit 63009ad

Please sign in to comment.