Skip to content

Commit

Permalink
sql: add Index interface and TableDescriptor methods in catalog
Browse files Browse the repository at this point in the history
Previously, the catalog.TableDescriptor interface and its implementing
types would liberally return descpb.IndexDescriptor values, pointers and
slices. In an effort to stop manipulating such protos directly, this
patch introduces a catalog.Index interface to encapsulate it. In order
to enventually propagate this change throughout the code base, this
patch marks some existing catalog.TableDescriptor methods as deprecated
and introduces new ones to eventually replace them.

Partially addresses #57465.

Release note: None
  • Loading branch information
Marius Posta committed Dec 24, 2020
1 parent 735da60 commit 76b3641
Show file tree
Hide file tree
Showing 15 changed files with 977 additions and 328 deletions.
1 change: 1 addition & 0 deletions pkg/sql/catalog/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog",
visibility = ["//visibility:public"],
deps = [
"//pkg/geo/geoindex",
"//pkg/keys",
"//pkg/kv",
"//pkg/roachpb",
Expand Down
110 changes: 97 additions & 13 deletions pkg/sql/catalog/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ package catalog
import (
"context"

"github.com/cockroachdb/cockroach/pkg/geo/geoindex"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
Expand Down Expand Up @@ -112,22 +112,51 @@ type TableDescriptor interface {
GetFormatVersion() descpb.FormatVersion

GetPrimaryIndexID() descpb.IndexID
GetPrimaryIndex() *descpb.IndexDescriptor
GetPrimaryIndex() *descpb.IndexDescriptor // deprecated
PrimaryIndexSpan(codec keys.SQLCodec) roachpb.Span
GetPublicNonPrimaryIndexes() []descpb.IndexDescriptor
ForeachIndex(opts IndexOpts, f func(idxDesc *descpb.IndexDescriptor, isPrimary bool) error) error
AllNonDropIndexes() []*descpb.IndexDescriptor
ForeachNonDropIndex(f func(idxDesc *descpb.IndexDescriptor) error) error
GetPublicNonPrimaryIndexes() []descpb.IndexDescriptor // deprecated
ForeachIndex(opts IndexOpts, f func(idxDesc *descpb.IndexDescriptor, isPrimary bool) error) error // deprecated
AllNonDropIndexes() []*descpb.IndexDescriptor // deprecated
ForeachNonDropIndex(f func(idxDesc *descpb.IndexDescriptor) error) error // deprecated
IndexSpan(codec keys.SQLCodec, id descpb.IndexID) roachpb.Span
FindIndexByID(id descpb.IndexID) (*descpb.IndexDescriptor, error)
FindIndexByName(name string) (_ *descpb.IndexDescriptor, dropped bool, _ error)
FindIndexesWithPartition(name string) []*descpb.IndexDescriptor
FindIndexByID(id descpb.IndexID) (*descpb.IndexDescriptor, error) // deprecated
FindIndexByName(name string) (_ *descpb.IndexDescriptor, dropped bool, _ error) // deprecated
GetIndexMutationCapabilities(id descpb.IndexID) (isMutation, isWriteOnly bool)
KeysPerRow(id descpb.IndexID) (int, error)
PartialIndexOrds() util.FastIntSet
WritableIndexes() []descpb.IndexDescriptor
DeletableIndexes() []descpb.IndexDescriptor
DeleteOnlyIndexes() []descpb.IndexDescriptor
WritableIndexes() []descpb.IndexDescriptor // deprecated
DeletableIndexes() []descpb.IndexDescriptor // deprecated
DeleteOnlyIndexes() []descpb.IndexDescriptor // deprecated

PrimaryIndexInterface() Index
AllIndexes() []Index
ActiveIndexes() []Index
NonDropIndexes() []Index
PartialIndexes() []Index
PublicNonPrimaryIndexes() []Index
WritableNonPrimaryIndexes() []Index
DeletableNonPrimaryIndexes() []Index
DeleteOnlyNonPrimaryIndexes() []Index

ForEachIndex(opts IndexOpts, f func(idx Index) error) error
ForEachActiveIndex(f func(idx Index) error) error
ForEachNonDropIndex(f func(idx Index) error) error
ForEachPartialIndex(f func(idx Index) error) error
ForEachPublicNonPrimaryIndex(f func(idx Index) error) error
ForEachWritableNonPrimaryIndex(f func(idx Index) error) error
ForEachDeletableNonPrimaryIndex(f func(idx Index) error) error
ForEachDeleteOnlyNonPrimaryIndex(f func(idx Index) error) error

FindIndex(opts IndexOpts, test func(idx Index) bool) Index
FindActiveIndex(test func(idx Index) bool) Index
FindNonDropIndex(test func(idx Index) bool) Index
FindPartialIndex(test func(idx Index) bool) Index
FindPublicNonPrimaryIndex(test func(idx Index) bool) Index
FindWritableNonPrimaryIndex(test func(idx Index) bool) Index
FindDeletableNonPrimaryIndex(test func(idx Index) bool) Index
FindDeleteOnlyNonPrimaryIndex(test func(idx Index) bool) Index

FindIndexWithID(id descpb.IndexID) (Index, error)
FindIndexWithName(name string) (Index, error)

HasPrimaryKey() bool
PrimaryKeyString() string
Expand Down Expand Up @@ -187,6 +216,61 @@ type TableDescriptor interface {
WritableColumns() []descpb.ColumnDescriptor
}

// Index is an interface around the index descriptor types.
type Index interface {
IndexDesc() *descpb.IndexDescriptor
IndexDescDeepCopy() descpb.IndexDescriptor

Ordinal() int
Primary() bool
Public() bool
WriteAndDeleteOnly() bool
DeleteOnly() bool
Adding() bool
Dropped() bool

GetID() descpb.IndexID
GetName() string
IsInterleaved() bool
IsPartial() bool
IsUnique() bool
IsDisabled() bool
IsSharded() bool
IsCreatedExplicitly() bool
GetPredicate() string
GetType() descpb.IndexDescriptor_Type
IsValidOriginIndex(originColIDs descpb.ColumnIDs) bool
IsValidReferencedIndex(referencedColIDs descpb.ColumnIDs) bool
GetGeoConfig() geoindex.Config
GetSharded() descpb.ShardedDescriptor
GetShardColumnName() string
GetVersion() descpb.IndexDescriptorVersion
GetEncodingType() descpb.IndexDescriptorEncodingType
GetPartitioning() descpb.PartitioningDescriptor
FindPartitionByName(name string) descpb.PartitioningDescriptor
PartitionNames() []string
NumInterleaveAncestors() int
GetInterleaveAncestor(ancestorOrdinal int) descpb.InterleaveDescriptor_Ancestor
NumInterleavedBy() int
GetInterleavedBy(interleavedByOrdinal int) descpb.ForeignKeyReference
NumColumns() int
GetColumnID(columnOrdinal int) descpb.ColumnID
GetColumnName(columnOrdinal int) string
GetColumnDirection(columnOrdinal int) descpb.IndexDescriptor_Direction
ContainsColumnID(colID descpb.ColumnID) bool
InvertedColumnID() descpb.ColumnID
InvertedColumnName() string
ForEachColumnID(func(id descpb.ColumnID) error) error
NumStoredColumns() int
GetStoredColumnID(storedColumnOrdinal int) descpb.ColumnID
GetStoredColumnName(storedColumnOrdinal int) string
HasOldStoredColumns() bool
NumExtraColumns() int
GetExtraColumnID(extraColumnOrdinal int) descpb.ColumnID
NumCompositeColumns() int
GetCompositeColumnID(compositeColumnOrdinal int) descpb.ColumnID
}

// TypeDescriptor will eventually be called typedesc.Descriptor.
// It is implemented by (Imm|M)utableTypeDescriptor.
type TypeDescriptor interface {
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/catalog/tabledesc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "tabledesc",
srcs = [
"index.go",
"safe_format.go",
"structured.go",
"table.go",
Expand All @@ -11,6 +12,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc",
visibility = ["//visibility:public"],
deps = [
"//pkg/geo/geoindex",
"//pkg/keys",
"//pkg/roachpb",
"//pkg/settings/cluster",
Expand Down
Loading

0 comments on commit 76b3641

Please sign in to comment.