github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sqlbase/structured.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sqlbase
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"sort"
    17  	"strconv"
    18  	"strings"
    19  
    20  	"github.com/cockroachdb/cockroach/pkg/clusterversion"
    21  	"github.com/cockroachdb/cockroach/pkg/keys"
    22  	"github.com/cockroachdb/cockroach/pkg/kv"
    23  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    24  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    25  	"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
    26  	"github.com/cockroachdb/cockroach/pkg/sql/parser"
    27  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
    28  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
    29  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    30  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    31  	"github.com/cockroachdb/cockroach/pkg/util"
    32  	"github.com/cockroachdb/cockroach/pkg/util/encoding"
    33  	"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
    34  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    35  	"github.com/cockroachdb/cockroach/pkg/util/interval"
    36  	"github.com/cockroachdb/cockroach/pkg/util/log"
    37  	"github.com/cockroachdb/cockroach/pkg/util/protoutil"
    38  	"github.com/cockroachdb/errors"
    39  	"github.com/lib/pq/oid"
    40  )
    41  
    42  // ID, ColumnID, FamilyID, and IndexID are all uint32, but are each given a
    43  // type alias to prevent accidental use of one of the types where
    44  // another is expected.
    45  
    46  // ID is a custom type for {Database,Table}Descriptor IDs.
    47  type ID tree.ID
    48  
    49  // InvalidID is the uninitialised descriptor id.
    50  const InvalidID ID = 0
    51  
    52  // IDs is a sortable list of IDs.
    53  type IDs []ID
    54  
    55  func (ids IDs) Len() int           { return len(ids) }
    56  func (ids IDs) Less(i, j int) bool { return ids[i] < ids[j] }
    57  func (ids IDs) Swap(i, j int)      { ids[i], ids[j] = ids[j], ids[i] }
    58  
    59  // TableDescriptors is a sortable list of *TableDescriptors.
    60  type TableDescriptors []*TableDescriptor
    61  
    62  // TablesByID is a shorthand for the common map of tables keyed by ID.
    63  type TablesByID map[ID]*TableDescriptor
    64  
    65  func (t TableDescriptors) Len() int           { return len(t) }
    66  func (t TableDescriptors) Less(i, j int) bool { return t[i].ID < t[j].ID }
    67  func (t TableDescriptors) Swap(i, j int)      { t[i], t[j] = t[j], t[i] }
    68  
    69  // ColumnID is a custom type for ColumnDescriptor IDs.
    70  type ColumnID tree.ColumnID
    71  
    72  // ColumnIDs is a slice of ColumnDescriptor IDs.
    73  type ColumnIDs []ColumnID
    74  
    75  func (c ColumnIDs) Len() int           { return len(c) }
    76  func (c ColumnIDs) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
    77  func (c ColumnIDs) Less(i, j int) bool { return c[i] < c[j] }
    78  
    79  // HasPrefix returns true if the input list is a prefix of this list.
    80  func (c ColumnIDs) HasPrefix(input ColumnIDs) bool {
    81  	if len(input) > len(c) {
    82  		return false
    83  	}
    84  	for i := range input {
    85  		if input[i] != c[i] {
    86  			return false
    87  		}
    88  	}
    89  	return true
    90  }
    91  
    92  // Equals returns true if the input list is equal to this list.
    93  func (c ColumnIDs) Equals(input ColumnIDs) bool {
    94  	if len(input) != len(c) {
    95  		return false
    96  	}
    97  	for i := range input {
    98  		if input[i] != c[i] {
    99  			return false
   100  		}
   101  	}
   102  	return true
   103  }
   104  
   105  // FamilyID is a custom type for ColumnFamilyDescriptor IDs.
   106  type FamilyID uint32
   107  
   108  // IndexID is a custom type for IndexDescriptor IDs.
   109  type IndexID tree.IndexID
   110  
   111  // DescriptorVersion is a custom type for TableDescriptor Versions.
   112  type DescriptorVersion uint32
   113  
   114  // FormatVersion is a custom type for TableDescriptor versions of the sql to
   115  // key:value mapping.
   116  //go:generate stringer -type=FormatVersion
   117  type FormatVersion uint32
   118  
   119  const (
   120  	_ FormatVersion = iota
   121  	// BaseFormatVersion corresponds to the encoding described in
   122  	// https://www.cockroachlabs.com/blog/sql-in-cockroachdb-mapping-table-data-to-key-value-storage/.
   123  	BaseFormatVersion
   124  	// FamilyFormatVersion corresponds to the encoding described in
   125  	// https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20151214_sql_column_families.md
   126  	FamilyFormatVersion
   127  	// InterleavedFormatVersion corresponds to the encoding described in
   128  	// https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20160624_sql_interleaved_tables.md
   129  	InterleavedFormatVersion
   130  )
   131  
   132  // IndexDescriptorVersion is a custom type for IndexDescriptor Versions.
   133  type IndexDescriptorVersion uint32
   134  
   135  const (
   136  	// BaseIndexFormatVersion corresponds to the original encoding of secondary indexes that
   137  	// don't respect table level column family definitions. We allow the 0 value of the type to
   138  	// have a value so that existing index descriptors are denoted as having the base format.
   139  	BaseIndexFormatVersion IndexDescriptorVersion = iota
   140  	// SecondaryIndexFamilyFormatVersion corresponds to the encoding of secondary indexes that
   141  	// use table level column family definitions.
   142  	SecondaryIndexFamilyFormatVersion
   143  )
   144  
   145  // IndexDescriptorEncodingType is a custom type to represent different encoding types
   146  // for secondary indexes.
   147  type IndexDescriptorEncodingType uint32
   148  
   149  const (
   150  	// SecondaryIndexEncoding corresponds to the standard way of encoding secondary indexes
   151  	// as described in docs/tech-notes/encoding.md. We allow the 0 value of this type
   152  	// to have a value so that existing descriptors are encoding using this encoding.
   153  	SecondaryIndexEncoding IndexDescriptorEncodingType = iota
   154  	// PrimaryIndexEncoding corresponds to when a secondary index is encoded using the
   155  	// primary index encoding as described in docs/tech-notes/encoding.md.
   156  	PrimaryIndexEncoding
   157  )
   158  
   159  // Remove unused warning.
   160  var _ = SecondaryIndexEncoding
   161  
   162  // MutationID is a custom type for TableDescriptor mutations.
   163  type MutationID uint32
   164  
   165  // MutableTableDescriptor is a custom type for TableDescriptors
   166  // going through schema mutations.
   167  type MutableTableDescriptor struct {
   168  	TableDescriptor
   169  
   170  	// ClusterVersion represents the version of the table descriptor read from the store.
   171  	ClusterVersion TableDescriptor
   172  }
   173  
   174  // ImmutableTableDescriptor is a custom type for TableDescriptors
   175  // It holds precomputed values and the underlying TableDescriptor
   176  // should be const.
   177  type ImmutableTableDescriptor struct {
   178  	TableDescriptor
   179  
   180  	// publicAndNonPublicCols is a list of public and non-public columns.
   181  	// It is partitioned by the state of the column: public, write-only, delete-only
   182  	publicAndNonPublicCols []ColumnDescriptor
   183  
   184  	// publicAndNonPublicCols is a list of public and non-public indexes.
   185  	// It is partitioned by the state of the index: public, write-only, delete-only
   186  	publicAndNonPublicIndexes []IndexDescriptor
   187  
   188  	writeOnlyColCount   int
   189  	writeOnlyIndexCount int
   190  
   191  	allChecks []TableDescriptor_CheckConstraint
   192  
   193  	// ReadableColumns is a list of columns (including those undergoing a schema change)
   194  	// which can be scanned. Columns in the process of a schema change
   195  	// are all set to nullable while column backfilling is still in
   196  	// progress, as mutation columns may have NULL values.
   197  	ReadableColumns []ColumnDescriptor
   198  
   199  	// TODO (lucy): populate these and use them
   200  	// inboundFKs  []*ForeignKeyConstraint
   201  	// outboundFKs []*ForeignKeyConstraint
   202  }
   203  
   204  // InvalidMutationID is the uninitialised mutation id.
   205  const InvalidMutationID MutationID = 0
   206  
   207  const (
   208  	// PrimaryKeyIndexName is the name of the index for the primary key.
   209  	PrimaryKeyIndexName = "primary"
   210  	// SequenceColumnID is the ID of the sole column in a sequence.
   211  	SequenceColumnID = 1
   212  	// SequenceColumnName is the name of the sole column in a sequence.
   213  	SequenceColumnName = "value"
   214  )
   215  
   216  // ErrMissingColumns indicates a table with no columns.
   217  var ErrMissingColumns = errors.New("table must contain at least 1 column")
   218  
   219  // ErrMissingPrimaryKey indicates a table with no primary key.
   220  var ErrMissingPrimaryKey = errors.New("table must contain a primary key")
   221  
   222  func validateName(name, typ string) error {
   223  	if len(name) == 0 {
   224  		return pgerror.Newf(pgcode.Syntax, "empty %s name", typ)
   225  	}
   226  	// TODO(pmattis): Do we want to be more restrictive than this?
   227  	return nil
   228  }
   229  
   230  // ToEncodingDirection converts a direction from the proto to an encoding.Direction.
   231  func (dir IndexDescriptor_Direction) ToEncodingDirection() (encoding.Direction, error) {
   232  	switch dir {
   233  	case IndexDescriptor_ASC:
   234  		return encoding.Ascending, nil
   235  	case IndexDescriptor_DESC:
   236  		return encoding.Descending, nil
   237  	default:
   238  		return encoding.Ascending, errors.Errorf("invalid direction: %s", dir)
   239  	}
   240  }
   241  
   242  // ErrDescriptorNotFound is returned by GetTableDescFromID to signal that a
   243  // descriptor could not be found with the given id.
   244  var ErrDescriptorNotFound = errors.New("descriptor not found")
   245  
   246  // ErrIndexGCMutationsList is returned by FindIndexByID to signal that the
   247  // index with the given ID does not have a descriptor and is in the garbage
   248  // collected mutations list.
   249  var ErrIndexGCMutationsList = errors.New("index in GC mutations list")
   250  
   251  // NewMutableCreatedTableDescriptor returns a MutableTableDescriptor from the
   252  // given TableDescriptor with the cluster version being the zero table. This
   253  // is for a table that is created in the transaction.
   254  func NewMutableCreatedTableDescriptor(tbl TableDescriptor) *MutableTableDescriptor {
   255  	return &MutableTableDescriptor{TableDescriptor: tbl}
   256  }
   257  
   258  // NewMutableExistingTableDescriptor returns a MutableTableDescriptor from the
   259  // given TableDescriptor with the cluster version also set to the descriptor.
   260  // This is for an existing table.
   261  func NewMutableExistingTableDescriptor(tbl TableDescriptor) *MutableTableDescriptor {
   262  	return &MutableTableDescriptor{TableDescriptor: tbl, ClusterVersion: tbl}
   263  }
   264  
   265  // NewImmutableTableDescriptor returns a ImmutableTableDescriptor from the
   266  // given TableDescriptor.
   267  func NewImmutableTableDescriptor(tbl TableDescriptor) *ImmutableTableDescriptor {
   268  	publicAndNonPublicCols := tbl.Columns
   269  	publicAndNonPublicIndexes := tbl.Indexes
   270  
   271  	readableCols := tbl.Columns
   272  
   273  	desc := &ImmutableTableDescriptor{TableDescriptor: tbl}
   274  
   275  	if len(tbl.Mutations) > 0 {
   276  		publicAndNonPublicCols = make([]ColumnDescriptor, 0, len(tbl.Columns)+len(tbl.Mutations))
   277  		publicAndNonPublicIndexes = make([]IndexDescriptor, 0, len(tbl.Indexes)+len(tbl.Mutations))
   278  		readableCols = make([]ColumnDescriptor, 0, len(tbl.Columns)+len(tbl.Mutations))
   279  
   280  		publicAndNonPublicCols = append(publicAndNonPublicCols, tbl.Columns...)
   281  		publicAndNonPublicIndexes = append(publicAndNonPublicIndexes, tbl.Indexes...)
   282  		readableCols = append(readableCols, tbl.Columns...)
   283  
   284  		// Fill up mutations into the column/index lists by placing the writable columns/indexes
   285  		// before the delete only columns/indexes.
   286  		for _, m := range tbl.Mutations {
   287  			switch m.State {
   288  			case DescriptorMutation_DELETE_AND_WRITE_ONLY:
   289  				if idx := m.GetIndex(); idx != nil {
   290  					publicAndNonPublicIndexes = append(publicAndNonPublicIndexes, *idx)
   291  					desc.writeOnlyIndexCount++
   292  				} else if col := m.GetColumn(); col != nil {
   293  					publicAndNonPublicCols = append(publicAndNonPublicCols, *col)
   294  					desc.writeOnlyColCount++
   295  				}
   296  			}
   297  		}
   298  
   299  		for _, m := range tbl.Mutations {
   300  			switch m.State {
   301  			case DescriptorMutation_DELETE_ONLY:
   302  				if idx := m.GetIndex(); idx != nil {
   303  					publicAndNonPublicIndexes = append(publicAndNonPublicIndexes, *idx)
   304  				} else if col := m.GetColumn(); col != nil {
   305  					publicAndNonPublicCols = append(publicAndNonPublicCols, *col)
   306  				}
   307  			}
   308  		}
   309  
   310  		// Iterate through all mutation columns.
   311  		for _, c := range publicAndNonPublicCols[len(tbl.Columns):] {
   312  			// Mutation column may need to be fetched, but may not be completely backfilled
   313  			// and have be null values (even though they may be configured as NOT NULL).
   314  			c.Nullable = true
   315  			readableCols = append(readableCols, c)
   316  		}
   317  	}
   318  
   319  	desc.ReadableColumns = readableCols
   320  	desc.publicAndNonPublicCols = publicAndNonPublicCols
   321  	desc.publicAndNonPublicIndexes = publicAndNonPublicIndexes
   322  
   323  	desc.allChecks = make([]TableDescriptor_CheckConstraint, len(tbl.Checks))
   324  	for i, c := range tbl.Checks {
   325  		desc.allChecks[i] = *c
   326  	}
   327  
   328  	return desc
   329  }
   330  
   331  // protoGetter is a sub-interface of client.Txn that can fetch protobufs in a
   332  // transaction.
   333  type protoGetter interface {
   334  	// GetProtoTs retrieves a protoutil.Message that's stored at key, storing it
   335  	// into the input msg parameter. If the key doesn't exist, the input proto
   336  	// will be reset.
   337  	GetProtoTs(ctx context.Context, key interface{}, msg protoutil.Message) (hlc.Timestamp, error)
   338  }
   339  
   340  // GetDatabaseDescFromID retrieves the database descriptor for the database
   341  // ID passed in using an existing proto getter. Returns an error if the
   342  // descriptor doesn't exist or if it exists and is not a database.
   343  func GetDatabaseDescFromID(
   344  	ctx context.Context, protoGetter protoGetter, codec keys.SQLCodec, id ID,
   345  ) (*DatabaseDescriptor, error) {
   346  	desc := &Descriptor{}
   347  	descKey := MakeDescMetadataKey(codec, id)
   348  	_, err := protoGetter.GetProtoTs(ctx, descKey, desc)
   349  	if err != nil {
   350  		return nil, err
   351  	}
   352  	db := desc.GetDatabase()
   353  	if db == nil {
   354  		return nil, ErrDescriptorNotFound
   355  	}
   356  	return db, nil
   357  }
   358  
   359  // GetTypeDescFromID retrieves the type descriptor for the type ID passed
   360  // in using an existing proto getter. It returns an error if the descriptor
   361  // doesn't exist or if it exists and is not a type descriptor.
   362  func GetTypeDescFromID(
   363  	ctx context.Context, protoGetter protoGetter, codec keys.SQLCodec, id ID,
   364  ) (*TypeDescriptor, error) {
   365  	descKey := MakeDescMetadataKey(codec, id)
   366  	desc := &Descriptor{}
   367  	_, err := protoGetter.GetProtoTs(ctx, descKey, desc)
   368  	if err != nil {
   369  		return nil, err
   370  	}
   371  	typ := desc.GetType()
   372  	if typ == nil {
   373  		return nil, ErrDescriptorNotFound
   374  	}
   375  	return typ, nil
   376  }
   377  
   378  // GetTableDescFromID retrieves the table descriptor for the table
   379  // ID passed in using an existing proto getter. Returns an error if the
   380  // descriptor doesn't exist or if it exists and is not a table.
   381  // NB: If this function changes, make sure to update GetTableDescFromIDWithFKsChanged
   382  // in a similar way.
   383  func GetTableDescFromID(
   384  	ctx context.Context, protoGetter protoGetter, codec keys.SQLCodec, id ID,
   385  ) (*TableDescriptor, error) {
   386  	table, err := getTableDescFromIDRaw(ctx, protoGetter, codec, id)
   387  	if err != nil {
   388  		return nil, err
   389  	}
   390  
   391  	if err := table.MaybeFillInDescriptor(ctx, protoGetter, codec); err != nil {
   392  		return nil, err
   393  	}
   394  
   395  	return table, nil
   396  }
   397  
   398  // GetTableDescFromIDWithFKsChanged retrieves the table descriptor for the table
   399  // ID passed in using an existing proto getter. It returns the same things as
   400  // GetTableDescFromID but additionally returns whether or not the table descriptor
   401  // was changed during the foreign key upgrade process.
   402  func GetTableDescFromIDWithFKsChanged(
   403  	ctx context.Context, protoGetter protoGetter, codec keys.SQLCodec, id ID,
   404  ) (*TableDescriptor, bool, error) {
   405  	table, err := getTableDescFromIDRaw(ctx, protoGetter, codec, id)
   406  	if err != nil {
   407  		return nil, false, err
   408  	}
   409  	table.maybeUpgradeFormatVersion()
   410  	table.Privileges.MaybeFixPrivileges(table.ID)
   411  	changed, err := table.MaybeUpgradeForeignKeyRepresentation(ctx, protoGetter, codec, false /* skipFKsWithNoMatchingTable */)
   412  	if err != nil {
   413  		return nil, false, err
   414  	}
   415  	return table, changed, err
   416  }
   417  
   418  // getTableDescFromIDRaw retrieves the table descriptor for the table
   419  // ID passed in using an existing proto getter. Returns an error if the
   420  // descriptor doesn't exist or if it exists and is not a table. Note that it
   421  // does not "fill in" the descriptor, which performs various upgrade steps for
   422  // migrations and is *required* before ordinary presentation to other code. This
   423  // method is for internal use only and shouldn't get exposed.
   424  func getTableDescFromIDRaw(
   425  	ctx context.Context, protoGetter protoGetter, codec keys.SQLCodec, id ID,
   426  ) (*TableDescriptor, error) {
   427  	desc := &Descriptor{}
   428  	descKey := MakeDescMetadataKey(codec, id)
   429  	ts, err := protoGetter.GetProtoTs(ctx, descKey, desc)
   430  	if err != nil {
   431  		return nil, err
   432  	}
   433  	table := desc.Table(ts)
   434  	if table == nil {
   435  		return nil, ErrDescriptorNotFound
   436  	}
   437  	return table, nil
   438  }
   439  
   440  // GetMutableTableDescFromID retrieves the table descriptor for the table
   441  // ID passed in using an existing proto getter. Returns an error if the
   442  // descriptor doesn't exist or if it exists and is not a table.
   443  // Otherwise a mutable copy of the table is returned.
   444  func GetMutableTableDescFromID(
   445  	ctx context.Context, protoGetter protoGetter, codec keys.SQLCodec, id ID,
   446  ) (*MutableTableDescriptor, error) {
   447  	table, err := GetTableDescFromID(ctx, protoGetter, codec, id)
   448  	if err != nil {
   449  		return nil, err
   450  	}
   451  	return NewMutableExistingTableDescriptor(*table), nil
   452  }
   453  
   454  // RunOverAllColumns applies its argument fn to each of the column IDs in desc.
   455  // If there is an error, that error is returned immediately.
   456  func (desc *IndexDescriptor) RunOverAllColumns(fn func(id ColumnID) error) error {
   457  	for _, colID := range desc.ColumnIDs {
   458  		if err := fn(colID); err != nil {
   459  			return err
   460  		}
   461  	}
   462  	for _, colID := range desc.ExtraColumnIDs {
   463  		if err := fn(colID); err != nil {
   464  			return err
   465  		}
   466  	}
   467  	for _, colID := range desc.StoreColumnIDs {
   468  		if err := fn(colID); err != nil {
   469  			return err
   470  		}
   471  	}
   472  	return nil
   473  }
   474  
   475  // FindPartitionByName searches this partitioning descriptor for a partition
   476  // whose name is the input and returns it, or nil if no match is found.
   477  func (desc *PartitioningDescriptor) FindPartitionByName(name string) *PartitioningDescriptor {
   478  	for _, l := range desc.List {
   479  		if l.Name == name {
   480  			return desc
   481  		}
   482  		if s := l.Subpartitioning.FindPartitionByName(name); s != nil {
   483  			return s
   484  		}
   485  	}
   486  	for _, r := range desc.Range {
   487  		if r.Name == name {
   488  			return desc
   489  		}
   490  	}
   491  	return nil
   492  
   493  }
   494  
   495  // FindPartitionByName searches this index descriptor for a partition whose name
   496  // is the input and returns it, or nil if no match is found.
   497  func (desc *IndexDescriptor) FindPartitionByName(name string) *PartitioningDescriptor {
   498  	return desc.Partitioning.FindPartitionByName(name)
   499  }
   500  
   501  // allocateName sets desc.Name to a value that is not EqualName to any
   502  // of tableDesc's indexes. allocateName roughly follows PostgreSQL's
   503  // convention for automatically-named indexes.
   504  func (desc *IndexDescriptor) allocateName(tableDesc *MutableTableDescriptor) {
   505  	segments := make([]string, 0, len(desc.ColumnNames)+2)
   506  	segments = append(segments, tableDesc.Name)
   507  	segments = append(segments, desc.ColumnNames...)
   508  	if desc.Unique {
   509  		segments = append(segments, "key")
   510  	} else {
   511  		segments = append(segments, "idx")
   512  	}
   513  
   514  	baseName := strings.Join(segments, "_")
   515  	name := baseName
   516  
   517  	exists := func(name string) bool {
   518  		_, _, err := tableDesc.FindIndexByName(name)
   519  		return err == nil
   520  	}
   521  	for i := 1; exists(name); i++ {
   522  		name = fmt.Sprintf("%s%d", baseName, i)
   523  	}
   524  
   525  	desc.Name = name
   526  }
   527  
   528  // FillColumns sets the column names and directions in desc.
   529  func (desc *IndexDescriptor) FillColumns(elems tree.IndexElemList) error {
   530  	desc.ColumnNames = make([]string, 0, len(elems))
   531  	desc.ColumnDirections = make([]IndexDescriptor_Direction, 0, len(elems))
   532  	for _, c := range elems {
   533  		desc.ColumnNames = append(desc.ColumnNames, string(c.Column))
   534  		switch c.Direction {
   535  		case tree.Ascending, tree.DefaultDirection:
   536  			desc.ColumnDirections = append(desc.ColumnDirections, IndexDescriptor_ASC)
   537  		case tree.Descending:
   538  			desc.ColumnDirections = append(desc.ColumnDirections, IndexDescriptor_DESC)
   539  		default:
   540  			return fmt.Errorf("invalid direction %s for column %s", c.Direction, c.Column)
   541  		}
   542  	}
   543  	return nil
   544  }
   545  
   546  type returnTrue struct{}
   547  
   548  func (returnTrue) Error() string { panic("unimplemented") }
   549  
   550  var returnTruePseudoError error = returnTrue{}
   551  
   552  // ContainsColumnID returns true if the index descriptor contains the specified
   553  // column ID either in its explicit column IDs, the extra column IDs, or the
   554  // stored column IDs.
   555  func (desc *IndexDescriptor) ContainsColumnID(colID ColumnID) bool {
   556  	return desc.RunOverAllColumns(func(id ColumnID) error {
   557  		if id == colID {
   558  			return returnTruePseudoError
   559  		}
   560  		return nil
   561  	}) != nil
   562  }
   563  
   564  // FullColumnIDs returns the index column IDs including any extra (implicit or
   565  // stored (old STORING encoding)) column IDs for non-unique indexes. It also
   566  // returns the direction with which each column was encoded.
   567  func (desc *IndexDescriptor) FullColumnIDs() ([]ColumnID, []IndexDescriptor_Direction) {
   568  	if desc.Unique {
   569  		return desc.ColumnIDs, desc.ColumnDirections
   570  	}
   571  	// Non-unique indexes have some of the primary-key columns appended to
   572  	// their key.
   573  	columnIDs := append([]ColumnID(nil), desc.ColumnIDs...)
   574  	columnIDs = append(columnIDs, desc.ExtraColumnIDs...)
   575  	dirs := append([]IndexDescriptor_Direction(nil), desc.ColumnDirections...)
   576  	for range desc.ExtraColumnIDs {
   577  		// Extra columns are encoded in ascending order.
   578  		dirs = append(dirs, IndexDescriptor_ASC)
   579  	}
   580  	return columnIDs, dirs
   581  }
   582  
   583  // ColNamesFormat writes a string describing the column names and directions
   584  // in this index to the given buffer.
   585  func (desc *IndexDescriptor) ColNamesFormat(ctx *tree.FmtCtx) {
   586  	start := 0
   587  	if desc.IsSharded() {
   588  		start = 1
   589  	}
   590  	for i := start; i < len(desc.ColumnNames); i++ {
   591  		if i > start {
   592  			ctx.WriteString(", ")
   593  		}
   594  		ctx.FormatNameP(&desc.ColumnNames[i])
   595  		if desc.Type != IndexDescriptor_INVERTED {
   596  			ctx.WriteByte(' ')
   597  			ctx.WriteString(desc.ColumnDirections[i].String())
   598  		}
   599  	}
   600  }
   601  
   602  // TODO (tyler): Issue #39771 This method needs more thorough testing, probably
   603  // in structured_test.go. Or possibly replace it with a format method taking
   604  // a format context as argument.
   605  
   606  // ColNamesString returns a string describing the column names and directions
   607  // in this index.
   608  func (desc *IndexDescriptor) ColNamesString() string {
   609  	f := tree.NewFmtCtx(tree.FmtSimple)
   610  	desc.ColNamesFormat(f)
   611  	return f.CloseAndGetString()
   612  }
   613  
   614  // TODO (tyler): Issue #39771 Same comment as ColNamesString above.
   615  
   616  // SQLString returns the SQL string describing this index. If non-empty,
   617  // "ON tableName" is included in the output in the correct place.
   618  func (desc *IndexDescriptor) SQLString(tableName *tree.TableName) string {
   619  	f := tree.NewFmtCtx(tree.FmtSimple)
   620  	if desc.Unique {
   621  		f.WriteString("UNIQUE ")
   622  	}
   623  	if desc.Type == IndexDescriptor_INVERTED {
   624  		f.WriteString("INVERTED ")
   625  	}
   626  	f.WriteString("INDEX ")
   627  	f.FormatNameP(&desc.Name)
   628  	if *tableName != AnonymousTable {
   629  		f.WriteString(" ON ")
   630  		f.FormatNode(tableName)
   631  	}
   632  	f.WriteString(" (")
   633  	desc.ColNamesFormat(f)
   634  	f.WriteByte(')')
   635  
   636  	if desc.IsSharded() {
   637  		fmt.Fprintf(f, " USING HASH WITH BUCKET_COUNT = %v",
   638  			desc.Sharded.ShardBuckets)
   639  	}
   640  
   641  	if len(desc.StoreColumnNames) > 0 {
   642  		f.WriteString(" STORING (")
   643  		for i := range desc.StoreColumnNames {
   644  			if i > 0 {
   645  				f.WriteString(", ")
   646  			}
   647  			f.FormatNameP(&desc.StoreColumnNames[i])
   648  		}
   649  		f.WriteByte(')')
   650  	}
   651  
   652  	if desc.IsPartial() {
   653  		f.WriteString(" WHERE ")
   654  		f.WriteString(desc.Predicate)
   655  	}
   656  
   657  	return f.CloseAndGetString()
   658  }
   659  
   660  // GetEncodingType returns the encoding type of this index. For backward
   661  // compatibility reasons, this might not match what is stored in
   662  // desc.EncodingType. The primary index's ID must be passed so we can check if
   663  // this index is primary or secondary.
   664  func (desc *IndexDescriptor) GetEncodingType(primaryIndexID IndexID) IndexDescriptorEncodingType {
   665  	if desc.ID == primaryIndexID {
   666  		// Primary indexes always use the PrimaryIndexEncoding, regardless of what
   667  		// desc.EncodingType indicates.
   668  		return PrimaryIndexEncoding
   669  	}
   670  	return desc.EncodingType
   671  }
   672  
   673  // IsInterleaved returns whether the index is interleaved or not.
   674  func (desc *IndexDescriptor) IsInterleaved() bool {
   675  	return len(desc.Interleave.Ancestors) > 0 || len(desc.InterleavedBy) > 0
   676  }
   677  
   678  // IsSharded returns whether the index is hash sharded or not.
   679  func (desc *IndexDescriptor) IsSharded() bool {
   680  	return desc.Sharded.IsSharded
   681  }
   682  
   683  // IsPartial returns true if the index is a partial index.
   684  func (desc *IndexDescriptor) IsPartial() bool {
   685  	return desc.Predicate != ""
   686  }
   687  
   688  // SetID implements the DescriptorProto interface.
   689  func (desc *TableDescriptor) SetID(id ID) {
   690  	desc.ID = id
   691  }
   692  
   693  // TypeName returns the plain type of this descriptor.
   694  func (desc *TableDescriptor) TypeName() string {
   695  	return "relation"
   696  }
   697  
   698  // SetName implements the DescriptorProto interface.
   699  func (desc *TableDescriptor) SetName(name string) {
   700  	desc.Name = name
   701  }
   702  
   703  // IsTable returns true if the TableDescriptor actually describes a
   704  // Table resource, as opposed to a different resource (like a View).
   705  func (desc *TableDescriptor) IsTable() bool {
   706  	return !desc.IsView() && !desc.IsSequence()
   707  }
   708  
   709  // IsView returns true if the TableDescriptor actually describes a
   710  // View resource rather than a Table.
   711  func (desc *TableDescriptor) IsView() bool {
   712  	return desc.ViewQuery != ""
   713  }
   714  
   715  // IsAs returns true if the TableDescriptor actually describes
   716  // a Table resource with an As source.
   717  func (desc *TableDescriptor) IsAs() bool {
   718  	return desc.CreateQuery != ""
   719  }
   720  
   721  // IsSequence returns true if the TableDescriptor actually describes a
   722  // Sequence resource rather than a Table.
   723  func (desc *TableDescriptor) IsSequence() bool {
   724  	return desc.SequenceOpts != nil
   725  }
   726  
   727  // IsVirtualTable returns true if the TableDescriptor describes a
   728  // virtual Table (like the information_schema tables) and thus doesn't
   729  // need to be physically stored.
   730  func (desc *TableDescriptor) IsVirtualTable() bool {
   731  	return IsVirtualTable(desc.ID)
   732  }
   733  
   734  // GetParentSchemaID returns the ParentSchemaID if the descriptor has
   735  // one. If the descriptor was created before the field was added, then the
   736  // descriptor belongs to a table under the `public` physical schema. The static
   737  // public schema ID is returned in that case.
   738  func (desc *TableDescriptor) GetParentSchemaID() ID {
   739  	parentSchemaID := desc.GetUnexposedParentSchemaID()
   740  	if parentSchemaID == InvalidID {
   741  		parentSchemaID = keys.PublicSchemaID
   742  	}
   743  	return parentSchemaID
   744  }
   745  
   746  // IsVirtualTable returns true if the TableDescriptor describes a
   747  // virtual Table (like the informationgi_schema tables) and thus doesn't
   748  // need to be physically stored.
   749  func IsVirtualTable(id ID) bool {
   750  	return MinVirtualID <= id
   751  }
   752  
   753  // IsPhysicalTable returns true if the TableDescriptor actually describes a
   754  // physical Table that needs to be stored in the kv layer, as opposed to a
   755  // different resource like a view or a virtual table. Physical tables have
   756  // primary keys, column families, and indexes (unlike virtual tables).
   757  // Sequences count as physical tables because their values are stored in
   758  // the KV layer.
   759  func (desc *TableDescriptor) IsPhysicalTable() bool {
   760  	return desc.IsSequence() || (desc.IsTable() && !desc.IsVirtualTable())
   761  }
   762  
   763  // KeysPerRow returns the maximum number of keys used to encode a row for the
   764  // given index. If a secondary index doesn't store any columns, then it only
   765  // has one k/v pair, but if it stores some columns, it can return up to one
   766  // k/v pair per family in the table, just like a primary index.
   767  func (desc *TableDescriptor) KeysPerRow(indexID IndexID) (int, error) {
   768  	if desc.PrimaryIndex.ID == indexID {
   769  		return len(desc.Families), nil
   770  	}
   771  	idx, err := desc.FindIndexByID(indexID)
   772  	if err != nil {
   773  		return 0, err
   774  	}
   775  	if len(idx.StoreColumnIDs) == 0 {
   776  		return 1, nil
   777  	}
   778  	return len(desc.Families), nil
   779  }
   780  
   781  // AllNonDropColumns returns all the columns, including those being added
   782  // in the mutations.
   783  func (desc *TableDescriptor) AllNonDropColumns() []ColumnDescriptor {
   784  	cols := make([]ColumnDescriptor, 0, len(desc.Columns)+len(desc.Mutations))
   785  	cols = append(cols, desc.Columns...)
   786  	for _, m := range desc.Mutations {
   787  		if col := m.GetColumn(); col != nil {
   788  			if m.Direction == DescriptorMutation_ADD {
   789  				cols = append(cols, *col)
   790  			}
   791  		}
   792  	}
   793  	return cols
   794  }
   795  
   796  // AllNonDropIndexes returns all the indexes, including those being added
   797  // in the mutations.
   798  func (desc *TableDescriptor) AllNonDropIndexes() []*IndexDescriptor {
   799  	indexes := make([]*IndexDescriptor, 0, 1+len(desc.Indexes)+len(desc.Mutations))
   800  	if desc.IsPhysicalTable() {
   801  		indexes = append(indexes, &desc.PrimaryIndex)
   802  	}
   803  	for i := range desc.Indexes {
   804  		indexes = append(indexes, &desc.Indexes[i])
   805  	}
   806  	for _, m := range desc.Mutations {
   807  		if idx := m.GetIndex(); idx != nil {
   808  			if m.Direction == DescriptorMutation_ADD {
   809  				indexes = append(indexes, idx)
   810  			}
   811  		}
   812  	}
   813  	return indexes
   814  }
   815  
   816  // AllActiveAndInactiveChecks returns all check constraints, including both
   817  // "active" ones on the table descriptor which are being enforced for all
   818  // writes, and "inactive" ones queued in the mutations list.
   819  func (desc *TableDescriptor) AllActiveAndInactiveChecks() []*TableDescriptor_CheckConstraint {
   820  	// A check constraint could be both on the table descriptor and in the
   821  	// list of mutations while the constraint is validated for existing rows. In
   822  	// that case, the constraint is in the Validating state, and we avoid
   823  	// including it twice. (Note that even though unvalidated check constraints
   824  	// cannot be added as of 19.1, they can still exist if they were created under
   825  	// previous versions.)
   826  	checks := make([]*TableDescriptor_CheckConstraint, 0, len(desc.Checks))
   827  	for _, c := range desc.Checks {
   828  		// While a constraint is being validated for existing rows or being dropped,
   829  		// the constraint is present both on the table descriptor and in the
   830  		// mutations list in the Validating or Dropping state, so those constraints
   831  		// are excluded here to avoid double-counting.
   832  		if c.Validity != ConstraintValidity_Validating && c.Validity != ConstraintValidity_Dropping {
   833  			checks = append(checks, c)
   834  		}
   835  	}
   836  	for _, m := range desc.Mutations {
   837  		if c := m.GetConstraint(); c != nil && c.ConstraintType == ConstraintToUpdate_CHECK {
   838  			checks = append(checks, &c.Check)
   839  		}
   840  	}
   841  	return checks
   842  }
   843  
   844  // ReplaceColumnVarsInExprWithDummies replaces the occurrences of column names in an expression with
   845  // dummies containing their type, so that they may be typechecked. It returns
   846  // this new expression tree alongside a set containing the ColumnID of each
   847  // column seen in the expression.
   848  func (desc *TableDescriptor) ReplaceColumnVarsInExprWithDummies(
   849  	rootExpr tree.Expr,
   850  ) (tree.Expr, util.FastIntSet, error) {
   851  	var colIDs util.FastIntSet
   852  	newExpr, err := tree.SimpleVisit(rootExpr, func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
   853  		vBase, ok := expr.(tree.VarName)
   854  		if !ok {
   855  			// Not a VarName, don't do anything to this node.
   856  			return true, expr, nil
   857  		}
   858  
   859  		v, err := vBase.NormalizeVarName()
   860  		if err != nil {
   861  			return false, nil, err
   862  		}
   863  
   864  		c, ok := v.(*tree.ColumnItem)
   865  		if !ok {
   866  			return true, expr, nil
   867  		}
   868  
   869  		col, dropped, err := desc.FindColumnByName(c.ColumnName)
   870  		if err != nil || dropped {
   871  			return false, nil, pgerror.Newf(pgcode.UndefinedColumn,
   872  				"column %q does not exist, referenced in %q", c.ColumnName, rootExpr.String())
   873  		}
   874  		colIDs.Add(int(col.ID))
   875  		// Convert to a dummy node of the correct type.
   876  		return false, &dummyColumnItem{typ: col.Type, name: c.ColumnName}, nil
   877  	})
   878  	return newExpr, colIDs, err
   879  }
   880  
   881  // dummyColumnItem is used in makeCheckConstraint and validateIndexPredicate to
   882  // construct an expression that can be both type-checked and examined for
   883  // variable expressions. It can also be used to format typed expressions
   884  // containing column references.
   885  type dummyColumnItem struct {
   886  	typ  *types.T
   887  	name tree.Name
   888  }
   889  
   890  // String implements the Stringer interface.
   891  func (d *dummyColumnItem) String() string {
   892  	return tree.AsString(d)
   893  }
   894  
   895  // Format implements the NodeFormatter interface.
   896  // It should be kept in line with ColumnItem.Format.
   897  func (d *dummyColumnItem) Format(ctx *tree.FmtCtx) {
   898  	ctx.FormatNode(&d.name)
   899  }
   900  
   901  // Walk implements the Expr interface.
   902  func (d *dummyColumnItem) Walk(_ tree.Visitor) tree.Expr {
   903  	return d
   904  }
   905  
   906  // TypeCheck implements the Expr interface.
   907  func (d *dummyColumnItem) TypeCheck(
   908  	_ context.Context, _ *tree.SemaContext, desired *types.T,
   909  ) (tree.TypedExpr, error) {
   910  	return d, nil
   911  }
   912  
   913  // Eval implements the TypedExpr interface.
   914  func (*dummyColumnItem) Eval(_ *tree.EvalContext) (tree.Datum, error) {
   915  	panic("dummyColumnItem.Eval() is undefined")
   916  }
   917  
   918  // ResolvedType implements the TypedExpr interface.
   919  func (d *dummyColumnItem) ResolvedType() *types.T {
   920  	return d.typ
   921  }
   922  
   923  // GetColumnFamilyForShard returns the column family that a newly added shard column
   924  // should be assigned to, given the set of columns it's computed from.
   925  //
   926  // This is currently the column family of the first column in the set of index columns.
   927  func GetColumnFamilyForShard(desc *MutableTableDescriptor, idxColumns []string) string {
   928  	for _, f := range desc.Families {
   929  		for _, fCol := range f.ColumnNames {
   930  			if fCol == idxColumns[0] {
   931  				return f.Name
   932  			}
   933  		}
   934  	}
   935  	return ""
   936  }
   937  
   938  // AllActiveAndInactiveForeignKeys returns all foreign keys, including both
   939  // "active" ones on the index descriptor which are being enforced for all
   940  // writes, and "inactive" ones queued in the mutations list. An error is
   941  // returned if multiple foreign keys (including mutations) are found for the
   942  // same index.
   943  func (desc *TableDescriptor) AllActiveAndInactiveForeignKeys() []*ForeignKeyConstraint {
   944  	fks := make([]*ForeignKeyConstraint, 0, len(desc.OutboundFKs))
   945  	for i := range desc.OutboundFKs {
   946  		fk := &desc.OutboundFKs[i]
   947  		// While a constraint is being validated for existing rows or being dropped,
   948  		// the constraint is present both on the table descriptor and in the
   949  		// mutations list in the Validating or Dropping state, so those constraints
   950  		// are excluded here to avoid double-counting.
   951  		if fk.Validity != ConstraintValidity_Validating && fk.Validity != ConstraintValidity_Dropping {
   952  			fks = append(fks, fk)
   953  		}
   954  	}
   955  	for i := range desc.Mutations {
   956  		if c := desc.Mutations[i].GetConstraint(); c != nil && c.ConstraintType == ConstraintToUpdate_FOREIGN_KEY {
   957  			fks = append(fks, &c.ForeignKey)
   958  		}
   959  	}
   960  	return fks
   961  }
   962  
   963  // ForeachNonDropIndex runs a function on all indexes, including those being
   964  // added in the mutations.
   965  func (desc *TableDescriptor) ForeachNonDropIndex(f func(*IndexDescriptor) error) error {
   966  	if desc.IsPhysicalTable() {
   967  		if err := f(&desc.PrimaryIndex); err != nil {
   968  			return err
   969  		}
   970  	}
   971  	for i := range desc.Indexes {
   972  		if err := f(&desc.Indexes[i]); err != nil {
   973  			return err
   974  		}
   975  	}
   976  	for _, m := range desc.Mutations {
   977  		if idx := m.GetIndex(); idx != nil && m.Direction == DescriptorMutation_ADD {
   978  			if err := f(idx); err != nil {
   979  				return err
   980  			}
   981  		}
   982  	}
   983  	return nil
   984  }
   985  
   986  func generatedFamilyName(familyID FamilyID, columnNames []string) string {
   987  	var buf strings.Builder
   988  	fmt.Fprintf(&buf, "fam_%d", familyID)
   989  	for _, n := range columnNames {
   990  		buf.WriteString(`_`)
   991  		buf.WriteString(n)
   992  	}
   993  	return buf.String()
   994  }
   995  
   996  // MaybeFillInDescriptor performs any modifications needed to the table descriptor.
   997  // This includes format upgrades and optional changes that can be handled by all version
   998  // (for example: additional default privileges).
   999  // Returns true if any changes were made.
  1000  // NB: If this function changes, make sure to update GetTableDescFromIDWithFKsChanged
  1001  // in a similar way.
  1002  func (desc *TableDescriptor) MaybeFillInDescriptor(
  1003  	ctx context.Context, protoGetter protoGetter, codec keys.SQLCodec,
  1004  ) error {
  1005  	desc.maybeUpgradeFormatVersion()
  1006  	desc.Privileges.MaybeFixPrivileges(desc.ID)
  1007  
  1008  	if protoGetter != nil {
  1009  		if _, err := desc.MaybeUpgradeForeignKeyRepresentation(ctx, protoGetter, codec, false /* skipFKsWithNoMatchingTable*/); err != nil {
  1010  			return err
  1011  		}
  1012  	}
  1013  	return nil
  1014  }
  1015  
  1016  // MapProtoGetter is a protoGetter that has a hard-coded map of keys to proto
  1017  // messages.
  1018  type MapProtoGetter struct {
  1019  	Protos map[interface{}]protoutil.Message
  1020  }
  1021  
  1022  // getProto implements the protoGetter interface.
  1023  func (m MapProtoGetter) getProto(
  1024  	ctx context.Context, key interface{}, msg protoutil.Message,
  1025  ) error {
  1026  	msg.Reset()
  1027  	if other, ok := m.Protos[string(key.(roachpb.Key))]; ok {
  1028  		bytes := make([]byte, other.Size())
  1029  		if _, err := other.MarshalTo(bytes); err != nil {
  1030  			return err
  1031  		}
  1032  		if err := protoutil.Unmarshal(bytes, msg); err != nil {
  1033  			return err
  1034  		}
  1035  	}
  1036  	return nil
  1037  }
  1038  
  1039  // GetProtoTs implements the protoGetter interface.
  1040  func (m MapProtoGetter) GetProtoTs(
  1041  	ctx context.Context, key interface{}, msg protoutil.Message,
  1042  ) (hlc.Timestamp, error) {
  1043  	return hlc.Timestamp{}, m.getProto(ctx, key, msg)
  1044  }
  1045  
  1046  // MaybeUpgradeForeignKeyRepresentation destructively modifies the input table
  1047  // descriptor by replacing all old-style foreign key references (the ForeignKey
  1048  // and ReferencedBy fields on IndexDescriptor) with new-style foreign key
  1049  // references (the InboundFKs and OutboundFKs fields on TableDescriptor). It
  1050  // uses the supplied proto getter to look up the referenced descriptor on
  1051  // outgoing FKs and the origin descriptor on incoming FKs. It returns true in
  1052  // the first position if the descriptor was upgraded at all (i.e. had old-style
  1053  // references on it) and an error if the descriptor was unable to be upgraded
  1054  // for some reason.
  1055  // If skipFKsWithNoMatchingTable is set to true, if a *table* that's supposed to
  1056  // contain the matching forward/back-reference for an FK is not found, the FK
  1057  // is dropped from the table and no error is returned.
  1058  // TODO(lucy): Write tests for when skipFKsWithNoMatchingTable is true.
  1059  func (desc *TableDescriptor) MaybeUpgradeForeignKeyRepresentation(
  1060  	ctx context.Context,
  1061  	protoGetter protoGetter,
  1062  	codec keys.SQLCodec,
  1063  	skipFKsWithNoMatchingTable bool,
  1064  ) (bool, error) {
  1065  	if desc.Dropped() {
  1066  		// If the table has been dropped, it's permitted to have corrupted foreign
  1067  		// keys, so we have no chance to properly upgrade it. Just return as-is.
  1068  		return false, nil
  1069  	}
  1070  	otherUnupgradedTables := make(map[ID]*TableDescriptor)
  1071  	changed := false
  1072  	// No need to process mutations, since only descriptors written on a 19.2
  1073  	// cluster (after finalizing the upgrade) have foreign key mutations.
  1074  	for i := range desc.Indexes {
  1075  		newChanged, err := maybeUpgradeForeignKeyRepOnIndex(
  1076  			ctx, protoGetter, codec, otherUnupgradedTables, desc, &desc.Indexes[i], skipFKsWithNoMatchingTable,
  1077  		)
  1078  		if err != nil {
  1079  			return false, err
  1080  		}
  1081  		changed = changed || newChanged
  1082  	}
  1083  	newChanged, err := maybeUpgradeForeignKeyRepOnIndex(
  1084  		ctx, protoGetter, codec, otherUnupgradedTables, desc, &desc.PrimaryIndex, skipFKsWithNoMatchingTable,
  1085  	)
  1086  	if err != nil {
  1087  		return false, err
  1088  	}
  1089  	changed = changed || newChanged
  1090  
  1091  	return changed, nil
  1092  }
  1093  
  1094  // maybeUpgradeForeignKeyRepOnIndex is the meat of the previous function - it
  1095  // tries to upgrade a particular index's foreign key representation.
  1096  func maybeUpgradeForeignKeyRepOnIndex(
  1097  	ctx context.Context,
  1098  	protoGetter protoGetter,
  1099  	codec keys.SQLCodec,
  1100  	otherUnupgradedTables map[ID]*TableDescriptor,
  1101  	desc *TableDescriptor,
  1102  	idx *IndexDescriptor,
  1103  	skipFKsWithNoMatchingTable bool,
  1104  ) (bool, error) {
  1105  	var changed bool
  1106  	if idx.ForeignKey.IsSet() {
  1107  		ref := &idx.ForeignKey
  1108  		if _, ok := otherUnupgradedTables[ref.Table]; !ok {
  1109  			tbl, err := getTableDescFromIDRaw(ctx, protoGetter, codec, ref.Table)
  1110  			if err != nil {
  1111  				if errors.Is(err, ErrDescriptorNotFound) && skipFKsWithNoMatchingTable {
  1112  					// Ignore this FK and keep going.
  1113  				} else {
  1114  					return false, err
  1115  				}
  1116  			} else {
  1117  				otherUnupgradedTables[ref.Table] = tbl
  1118  			}
  1119  		}
  1120  		if tbl, ok := otherUnupgradedTables[ref.Table]; ok {
  1121  			referencedIndex, err := tbl.FindIndexByID(ref.Index)
  1122  			if err != nil {
  1123  				return false, err
  1124  			}
  1125  			numCols := ref.SharedPrefixLen
  1126  			outFK := ForeignKeyConstraint{
  1127  				OriginTableID:         desc.ID,
  1128  				OriginColumnIDs:       idx.ColumnIDs[:numCols],
  1129  				ReferencedTableID:     ref.Table,
  1130  				ReferencedColumnIDs:   referencedIndex.ColumnIDs[:numCols],
  1131  				Name:                  ref.Name,
  1132  				Validity:              ref.Validity,
  1133  				OnDelete:              ref.OnDelete,
  1134  				OnUpdate:              ref.OnUpdate,
  1135  				Match:                 ref.Match,
  1136  				LegacyOriginIndex:     idx.ID,
  1137  				LegacyReferencedIndex: referencedIndex.ID,
  1138  			}
  1139  			desc.OutboundFKs = append(desc.OutboundFKs, outFK)
  1140  		}
  1141  		changed = true
  1142  		idx.ForeignKey = ForeignKeyReference{}
  1143  	}
  1144  
  1145  	for refIdx := range idx.ReferencedBy {
  1146  		ref := &(idx.ReferencedBy[refIdx])
  1147  		if _, ok := otherUnupgradedTables[ref.Table]; !ok {
  1148  			tbl, err := getTableDescFromIDRaw(ctx, protoGetter, codec, ref.Table)
  1149  			if err != nil {
  1150  				if errors.Is(err, ErrDescriptorNotFound) && skipFKsWithNoMatchingTable {
  1151  					// Ignore this FK and keep going.
  1152  				} else {
  1153  					return false, err
  1154  				}
  1155  			} else {
  1156  				otherUnupgradedTables[ref.Table] = tbl
  1157  			}
  1158  		}
  1159  
  1160  		if otherTable, ok := otherUnupgradedTables[ref.Table]; ok {
  1161  			originIndex, err := otherTable.FindIndexByID(ref.Index)
  1162  			if err != nil {
  1163  				return false, err
  1164  			}
  1165  			// There are two cases. Either the other table is old (not upgraded yet),
  1166  			// or it's new (already upgraded).
  1167  			var inFK ForeignKeyConstraint
  1168  			if !originIndex.ForeignKey.IsSet() {
  1169  				// The other table has either no foreign key, indicating a corrupt
  1170  				// reference, or the other table was upgraded. Assume the second for now.
  1171  				// If we also find no matching reference in the new-style foreign keys,
  1172  				// that indicates a corrupt reference.
  1173  				var forwardFK *ForeignKeyConstraint
  1174  				for i := range otherTable.OutboundFKs {
  1175  					otherFK := &otherTable.OutboundFKs[i]
  1176  					// To find a match, we find a foreign key reference that has the same
  1177  					// referenced table ID, and that the index we point to is a valid
  1178  					// index to satisfy the columns in the foreign key.
  1179  					// TODO (rohany): I'm unsure about this... Could there be multiple FK's?
  1180  					if otherFK.ReferencedTableID == desc.ID &&
  1181  						ColumnIDs(originIndex.ColumnIDs).HasPrefix(otherFK.OriginColumnIDs) {
  1182  						// Found a match.
  1183  						forwardFK = otherFK
  1184  						break
  1185  					}
  1186  				}
  1187  				if forwardFK == nil {
  1188  					// Corrupted foreign key - there was no forward reference for the back
  1189  					// reference.
  1190  					return false, errors.AssertionFailedf(
  1191  						"error finding foreign key on table %d for backref %+v",
  1192  						otherTable.ID, ref)
  1193  				}
  1194  				inFK = ForeignKeyConstraint{
  1195  					OriginTableID:         ref.Table,
  1196  					OriginColumnIDs:       forwardFK.OriginColumnIDs,
  1197  					ReferencedTableID:     desc.ID,
  1198  					ReferencedColumnIDs:   forwardFK.ReferencedColumnIDs,
  1199  					Name:                  forwardFK.Name,
  1200  					Validity:              forwardFK.Validity,
  1201  					OnDelete:              forwardFK.OnDelete,
  1202  					OnUpdate:              forwardFK.OnUpdate,
  1203  					Match:                 forwardFK.Match,
  1204  					LegacyOriginIndex:     originIndex.ID,
  1205  					LegacyReferencedIndex: idx.ID,
  1206  				}
  1207  			} else {
  1208  				// We have an old (not upgraded yet) table, with a matching forward
  1209  				// foreign key.
  1210  				numCols := originIndex.ForeignKey.SharedPrefixLen
  1211  				inFK = ForeignKeyConstraint{
  1212  					OriginTableID:         ref.Table,
  1213  					OriginColumnIDs:       originIndex.ColumnIDs[:numCols],
  1214  					ReferencedTableID:     desc.ID,
  1215  					ReferencedColumnIDs:   idx.ColumnIDs[:numCols],
  1216  					Name:                  originIndex.ForeignKey.Name,
  1217  					Validity:              originIndex.ForeignKey.Validity,
  1218  					OnDelete:              originIndex.ForeignKey.OnDelete,
  1219  					OnUpdate:              originIndex.ForeignKey.OnUpdate,
  1220  					Match:                 originIndex.ForeignKey.Match,
  1221  					LegacyOriginIndex:     originIndex.ID,
  1222  					LegacyReferencedIndex: idx.ID,
  1223  				}
  1224  			}
  1225  			desc.InboundFKs = append(desc.InboundFKs, inFK)
  1226  		}
  1227  		changed = true
  1228  	}
  1229  	idx.ReferencedBy = nil
  1230  	return changed, nil
  1231  }
  1232  
  1233  // maybeUpgradeFormatVersion transforms the TableDescriptor to the latest
  1234  // FormatVersion (if it's not already there) and returns true if any changes
  1235  // were made.
  1236  // This method should be called through MaybeFillInDescriptor, not directly.
  1237  func (desc *TableDescriptor) maybeUpgradeFormatVersion() bool {
  1238  	if desc.FormatVersion >= InterleavedFormatVersion {
  1239  		return false
  1240  	}
  1241  	desc.maybeUpgradeToFamilyFormatVersion()
  1242  	desc.FormatVersion = InterleavedFormatVersion
  1243  	return true
  1244  }
  1245  
  1246  func (desc *TableDescriptor) maybeUpgradeToFamilyFormatVersion() bool {
  1247  	if desc.FormatVersion >= FamilyFormatVersion {
  1248  		return false
  1249  	}
  1250  
  1251  	primaryIndexColumnIds := make(map[ColumnID]struct{}, len(desc.PrimaryIndex.ColumnIDs))
  1252  	for _, colID := range desc.PrimaryIndex.ColumnIDs {
  1253  		primaryIndexColumnIds[colID] = struct{}{}
  1254  	}
  1255  
  1256  	desc.Families = []ColumnFamilyDescriptor{
  1257  		{ID: 0, Name: "primary"},
  1258  	}
  1259  	desc.NextFamilyID = desc.Families[0].ID + 1
  1260  	addFamilyForCol := func(col *ColumnDescriptor) {
  1261  		if _, ok := primaryIndexColumnIds[col.ID]; ok {
  1262  			desc.Families[0].ColumnNames = append(desc.Families[0].ColumnNames, col.Name)
  1263  			desc.Families[0].ColumnIDs = append(desc.Families[0].ColumnIDs, col.ID)
  1264  			return
  1265  		}
  1266  		colNames := []string{col.Name}
  1267  		family := ColumnFamilyDescriptor{
  1268  			ID:              FamilyID(col.ID),
  1269  			Name:            generatedFamilyName(FamilyID(col.ID), colNames),
  1270  			ColumnNames:     colNames,
  1271  			ColumnIDs:       []ColumnID{col.ID},
  1272  			DefaultColumnID: col.ID,
  1273  		}
  1274  		desc.Families = append(desc.Families, family)
  1275  		if family.ID >= desc.NextFamilyID {
  1276  			desc.NextFamilyID = family.ID + 1
  1277  		}
  1278  	}
  1279  
  1280  	for i := range desc.Columns {
  1281  		addFamilyForCol(&desc.Columns[i])
  1282  	}
  1283  	for i := range desc.Mutations {
  1284  		m := &desc.Mutations[i]
  1285  		if c := m.GetColumn(); c != nil {
  1286  			addFamilyForCol(c)
  1287  		}
  1288  	}
  1289  
  1290  	desc.FormatVersion = FamilyFormatVersion
  1291  
  1292  	return true
  1293  }
  1294  
  1295  func (desc *MutableTableDescriptor) initIDs() {
  1296  	if desc.NextColumnID == 0 {
  1297  		desc.NextColumnID = 1
  1298  	}
  1299  	if desc.Version == 0 {
  1300  		desc.Version = 1
  1301  	}
  1302  	if desc.NextMutationID == InvalidMutationID {
  1303  		desc.NextMutationID = 1
  1304  	}
  1305  }
  1306  
  1307  // MaybeFillColumnID assigns a column ID to the given column if the said column has an ID
  1308  // of 0.
  1309  func (desc *MutableTableDescriptor) MaybeFillColumnID(
  1310  	c *ColumnDescriptor, columnNames map[string]ColumnID,
  1311  ) {
  1312  	desc.initIDs()
  1313  
  1314  	columnID := c.ID
  1315  	if columnID == 0 {
  1316  		columnID = desc.NextColumnID
  1317  		desc.NextColumnID++
  1318  	}
  1319  	columnNames[c.Name] = columnID
  1320  	c.ID = columnID
  1321  }
  1322  
  1323  // AllocateIDs allocates column, family, and index ids for any column, family,
  1324  // or index which has an ID of 0.
  1325  func (desc *MutableTableDescriptor) AllocateIDs() error {
  1326  	// Only physical tables can have / need a primary key.
  1327  	if desc.IsPhysicalTable() {
  1328  		if err := desc.ensurePrimaryKey(); err != nil {
  1329  			return err
  1330  		}
  1331  	}
  1332  
  1333  	desc.initIDs()
  1334  	columnNames := map[string]ColumnID{}
  1335  	for i := range desc.Columns {
  1336  		desc.MaybeFillColumnID(&desc.Columns[i], columnNames)
  1337  	}
  1338  	for _, m := range desc.Mutations {
  1339  		if c := m.GetColumn(); c != nil {
  1340  			desc.MaybeFillColumnID(c, columnNames)
  1341  		}
  1342  	}
  1343  
  1344  	// Only tables can have / need indexes and column families.
  1345  	if desc.IsTable() {
  1346  		if err := desc.allocateIndexIDs(columnNames); err != nil {
  1347  			return err
  1348  		}
  1349  		// Virtual tables don't have column families.
  1350  		if desc.IsPhysicalTable() {
  1351  			desc.allocateColumnFamilyIDs(columnNames)
  1352  		}
  1353  	}
  1354  
  1355  	// This is sort of ugly. If the descriptor does not have an ID, we hack one in
  1356  	// to pass the table ID check. We use a non-reserved ID, reserved ones being set
  1357  	// before AllocateIDs.
  1358  	savedID := desc.ID
  1359  	if desc.ID == 0 {
  1360  		desc.ID = keys.MinUserDescID
  1361  	}
  1362  	err := desc.ValidateTable()
  1363  	desc.ID = savedID
  1364  	return err
  1365  }
  1366  
  1367  func (desc *MutableTableDescriptor) ensurePrimaryKey() error {
  1368  	if len(desc.PrimaryIndex.ColumnNames) == 0 && desc.IsPhysicalTable() {
  1369  		// Ensure a Primary Key exists.
  1370  		nameExists := func(name string) bool {
  1371  			_, _, err := desc.FindColumnByName(tree.Name(name))
  1372  			return err == nil
  1373  		}
  1374  		s := "unique_rowid()"
  1375  		col := &ColumnDescriptor{
  1376  			Name:        GenerateUniqueConstraintName("rowid", nameExists),
  1377  			Type:        types.Int,
  1378  			DefaultExpr: &s,
  1379  			Hidden:      true,
  1380  			Nullable:    false,
  1381  		}
  1382  		desc.AddColumn(col)
  1383  		idx := IndexDescriptor{
  1384  			Unique:           true,
  1385  			ColumnNames:      []string{col.Name},
  1386  			ColumnDirections: []IndexDescriptor_Direction{IndexDescriptor_ASC},
  1387  		}
  1388  		if err := desc.AddIndex(idx, true); err != nil {
  1389  			return err
  1390  		}
  1391  	}
  1392  	return nil
  1393  }
  1394  
  1395  // HasCompositeKeyEncoding returns true if key columns of the given kind can
  1396  // have a composite encoding. For such types, it can be decided on a
  1397  // case-by-base basis whether a given Datum requires the composite encoding.
  1398  //
  1399  // As an example of a composite encoding, collated string key columns are
  1400  // encoded partly as a key and partly as a value. The key part is the collation
  1401  // key, so that different strings that collate equal cannot both be used as
  1402  // keys. The value part is the usual UTF-8 encoding of the string, stored so
  1403  // that it can be recovered later for inspection/display.
  1404  func HasCompositeKeyEncoding(typ *types.T) bool {
  1405  	switch typ.Family() {
  1406  	case types.CollatedStringFamily,
  1407  		types.FloatFamily,
  1408  		types.DecimalFamily:
  1409  		return true
  1410  	case types.ArrayFamily:
  1411  		return HasCompositeKeyEncoding(typ.ArrayContents())
  1412  	}
  1413  	return false
  1414  }
  1415  
  1416  // MustBeValueEncoded returns true if columns of the given kind can only be value
  1417  // encoded.
  1418  func MustBeValueEncoded(semanticType *types.T) bool {
  1419  	switch semanticType.Family() {
  1420  	case types.ArrayFamily:
  1421  		switch semanticType.Oid() {
  1422  		case oid.T_int2vector, oid.T_oidvector:
  1423  			return true
  1424  		default:
  1425  			return MustBeValueEncoded(semanticType.ArrayContents())
  1426  		}
  1427  	case types.JsonFamily, types.TupleFamily, types.GeographyFamily, types.GeometryFamily:
  1428  		return true
  1429  	}
  1430  	return false
  1431  }
  1432  
  1433  // HasOldStoredColumns returns whether the index has stored columns in the old
  1434  // format (data encoded the same way as if they were in an implicit column).
  1435  func (desc *IndexDescriptor) HasOldStoredColumns() bool {
  1436  	return len(desc.ExtraColumnIDs) > 0 && len(desc.StoreColumnIDs) < len(desc.StoreColumnNames)
  1437  }
  1438  
  1439  func (desc *MutableTableDescriptor) allocateIndexIDs(columnNames map[string]ColumnID) error {
  1440  	if desc.NextIndexID == 0 {
  1441  		desc.NextIndexID = 1
  1442  	}
  1443  
  1444  	// Keep track of unnamed indexes.
  1445  	anonymousIndexes := make([]*IndexDescriptor, 0, len(desc.Indexes)+len(desc.Mutations))
  1446  
  1447  	// Create a slice of modifiable index descriptors.
  1448  	indexes := make([]*IndexDescriptor, 0, 1+len(desc.Indexes)+len(desc.Mutations))
  1449  	indexes = append(indexes, &desc.PrimaryIndex)
  1450  	collectIndexes := func(index *IndexDescriptor) {
  1451  		if len(index.Name) == 0 {
  1452  			anonymousIndexes = append(anonymousIndexes, index)
  1453  		}
  1454  		indexes = append(indexes, index)
  1455  	}
  1456  	for i := range desc.Indexes {
  1457  		collectIndexes(&desc.Indexes[i])
  1458  	}
  1459  	for _, m := range desc.Mutations {
  1460  		if index := m.GetIndex(); index != nil {
  1461  			collectIndexes(index)
  1462  		}
  1463  	}
  1464  
  1465  	for _, index := range anonymousIndexes {
  1466  		index.allocateName(desc)
  1467  	}
  1468  
  1469  	isCompositeColumn := make(map[ColumnID]struct{})
  1470  	for i := range desc.Columns {
  1471  		col := &desc.Columns[i]
  1472  		if HasCompositeKeyEncoding(col.Type) {
  1473  			isCompositeColumn[col.ID] = struct{}{}
  1474  		}
  1475  	}
  1476  
  1477  	// Populate IDs.
  1478  	for _, index := range indexes {
  1479  		if index.ID != 0 {
  1480  			// This index has already been populated. Nothing to do.
  1481  			continue
  1482  		}
  1483  		index.ID = desc.NextIndexID
  1484  		desc.NextIndexID++
  1485  
  1486  		for j, colName := range index.ColumnNames {
  1487  			if len(index.ColumnIDs) <= j {
  1488  				index.ColumnIDs = append(index.ColumnIDs, 0)
  1489  			}
  1490  			if index.ColumnIDs[j] == 0 {
  1491  				index.ColumnIDs[j] = columnNames[colName]
  1492  			}
  1493  		}
  1494  
  1495  		if index != &desc.PrimaryIndex && index.EncodingType == SecondaryIndexEncoding {
  1496  			indexHasOldStoredColumns := index.HasOldStoredColumns()
  1497  			// Need to clear ExtraColumnIDs and StoreColumnIDs because they are used
  1498  			// by ContainsColumnID.
  1499  			index.ExtraColumnIDs = nil
  1500  			index.StoreColumnIDs = nil
  1501  			var extraColumnIDs []ColumnID
  1502  			for _, primaryColID := range desc.PrimaryIndex.ColumnIDs {
  1503  				if !index.ContainsColumnID(primaryColID) {
  1504  					extraColumnIDs = append(extraColumnIDs, primaryColID)
  1505  				}
  1506  			}
  1507  			index.ExtraColumnIDs = extraColumnIDs
  1508  
  1509  			for _, colName := range index.StoreColumnNames {
  1510  				col, _, err := desc.FindColumnByName(tree.Name(colName))
  1511  				if err != nil {
  1512  					return err
  1513  				}
  1514  				if desc.PrimaryIndex.ContainsColumnID(col.ID) {
  1515  					// If the primary index contains a stored column, we don't need to
  1516  					// store it - it's already part of the index.
  1517  					err = pgerror.Newf(
  1518  						pgcode.DuplicateColumn, "index %q already contains column %q", index.Name, col.Name)
  1519  					err = errors.WithDetailf(err, "column %q is part of the primary index and therefore implicit in all indexes", col.Name)
  1520  					return err
  1521  				}
  1522  				if index.ContainsColumnID(col.ID) {
  1523  					return pgerror.Newf(
  1524  						pgcode.DuplicateColumn,
  1525  						"index %q already contains column %q", index.Name, col.Name)
  1526  				}
  1527  				if indexHasOldStoredColumns {
  1528  					index.ExtraColumnIDs = append(index.ExtraColumnIDs, col.ID)
  1529  				} else {
  1530  					index.StoreColumnIDs = append(index.StoreColumnIDs, col.ID)
  1531  				}
  1532  			}
  1533  		}
  1534  
  1535  		index.CompositeColumnIDs = nil
  1536  		for _, colID := range index.ColumnIDs {
  1537  			if _, ok := isCompositeColumn[colID]; ok {
  1538  				index.CompositeColumnIDs = append(index.CompositeColumnIDs, colID)
  1539  			}
  1540  		}
  1541  		for _, colID := range index.ExtraColumnIDs {
  1542  			if _, ok := isCompositeColumn[colID]; ok {
  1543  				index.CompositeColumnIDs = append(index.CompositeColumnIDs, colID)
  1544  			}
  1545  		}
  1546  	}
  1547  	return nil
  1548  }
  1549  
  1550  func (desc *MutableTableDescriptor) allocateColumnFamilyIDs(columnNames map[string]ColumnID) {
  1551  	if desc.NextFamilyID == 0 {
  1552  		if len(desc.Families) == 0 {
  1553  			desc.Families = []ColumnFamilyDescriptor{
  1554  				{ID: 0, Name: "primary"},
  1555  			}
  1556  		}
  1557  		desc.NextFamilyID = 1
  1558  	}
  1559  
  1560  	columnsInFamilies := make(map[ColumnID]struct{}, len(desc.Columns))
  1561  	for i := range desc.Families {
  1562  		family := &desc.Families[i]
  1563  		if family.ID == 0 && i != 0 {
  1564  			family.ID = desc.NextFamilyID
  1565  			desc.NextFamilyID++
  1566  		}
  1567  
  1568  		for j, colName := range family.ColumnNames {
  1569  			if len(family.ColumnIDs) <= j {
  1570  				family.ColumnIDs = append(family.ColumnIDs, 0)
  1571  			}
  1572  			if family.ColumnIDs[j] == 0 {
  1573  				family.ColumnIDs[j] = columnNames[colName]
  1574  			}
  1575  			columnsInFamilies[family.ColumnIDs[j]] = struct{}{}
  1576  		}
  1577  
  1578  		desc.Families[i] = *family
  1579  	}
  1580  
  1581  	primaryIndexColIDs := make(map[ColumnID]struct{}, len(desc.PrimaryIndex.ColumnIDs))
  1582  	for _, colID := range desc.PrimaryIndex.ColumnIDs {
  1583  		primaryIndexColIDs[colID] = struct{}{}
  1584  	}
  1585  
  1586  	ensureColumnInFamily := func(col *ColumnDescriptor) {
  1587  		if _, ok := columnsInFamilies[col.ID]; ok {
  1588  			return
  1589  		}
  1590  		if _, ok := primaryIndexColIDs[col.ID]; ok {
  1591  			// Primary index columns are required to be assigned to family 0.
  1592  			desc.Families[0].ColumnNames = append(desc.Families[0].ColumnNames, col.Name)
  1593  			desc.Families[0].ColumnIDs = append(desc.Families[0].ColumnIDs, col.ID)
  1594  			return
  1595  		}
  1596  		var familyID FamilyID
  1597  		if desc.ParentID == keys.SystemDatabaseID {
  1598  			// TODO(dan): This assigns families such that the encoding is exactly the
  1599  			// same as before column families. It's used for all system tables because
  1600  			// reads of them don't go through the normal sql layer, which is where the
  1601  			// knowledge of families lives. Fix that and remove this workaround.
  1602  			familyID = FamilyID(col.ID)
  1603  			desc.Families = append(desc.Families, ColumnFamilyDescriptor{
  1604  				ID:          familyID,
  1605  				ColumnNames: []string{col.Name},
  1606  				ColumnIDs:   []ColumnID{col.ID},
  1607  			})
  1608  		} else {
  1609  			idx, ok := fitColumnToFamily(desc, *col)
  1610  			if !ok {
  1611  				idx = len(desc.Families)
  1612  				desc.Families = append(desc.Families, ColumnFamilyDescriptor{
  1613  					ID:          desc.NextFamilyID,
  1614  					ColumnNames: []string{},
  1615  					ColumnIDs:   []ColumnID{},
  1616  				})
  1617  			}
  1618  			familyID = desc.Families[idx].ID
  1619  			desc.Families[idx].ColumnNames = append(desc.Families[idx].ColumnNames, col.Name)
  1620  			desc.Families[idx].ColumnIDs = append(desc.Families[idx].ColumnIDs, col.ID)
  1621  		}
  1622  		if familyID >= desc.NextFamilyID {
  1623  			desc.NextFamilyID = familyID + 1
  1624  		}
  1625  	}
  1626  	for i := range desc.Columns {
  1627  		ensureColumnInFamily(&desc.Columns[i])
  1628  	}
  1629  	for _, m := range desc.Mutations {
  1630  		if c := m.GetColumn(); c != nil {
  1631  			ensureColumnInFamily(c)
  1632  		}
  1633  	}
  1634  
  1635  	for i := range desc.Families {
  1636  		family := &desc.Families[i]
  1637  		if len(family.Name) == 0 {
  1638  			family.Name = generatedFamilyName(family.ID, family.ColumnNames)
  1639  		}
  1640  
  1641  		if family.DefaultColumnID == 0 {
  1642  			defaultColumnID := ColumnID(0)
  1643  			for _, colID := range family.ColumnIDs {
  1644  				if _, ok := primaryIndexColIDs[colID]; !ok {
  1645  					if defaultColumnID == 0 {
  1646  						defaultColumnID = colID
  1647  					} else {
  1648  						defaultColumnID = ColumnID(0)
  1649  						break
  1650  					}
  1651  				}
  1652  			}
  1653  			family.DefaultColumnID = defaultColumnID
  1654  		}
  1655  
  1656  		desc.Families[i] = *family
  1657  	}
  1658  }
  1659  
  1660  // MaybeIncrementVersion increments the version of a descriptor if necessary.
  1661  func (desc *MutableTableDescriptor) MaybeIncrementVersion(
  1662  	ctx context.Context, txn *kv.Txn, settings *cluster.Settings,
  1663  ) error {
  1664  	// Already incremented, no-op.
  1665  	if desc.Version == desc.ClusterVersion.Version+1 {
  1666  		return nil
  1667  	}
  1668  	desc.Version++
  1669  
  1670  	// Before 19.2 we needed to observe the transaction CommitTimestamp to ensure
  1671  	// that ModificationTime reflected the timestamp at which the transaction
  1672  	// committed. Starting in 19.2 we use a zero-valued ModificationTime when
  1673  	// incrementing the version and then upon reading use the MVCC timestamp to
  1674  	// populate the ModificationTime.
  1675  	//
  1676  	// TODO(ajwerner): remove this check in 20.1.
  1677  	var modTime hlc.Timestamp
  1678  	if !settings.Version.IsActive(ctx, clusterversion.VersionTableDescModificationTimeFromMVCC) {
  1679  		modTime = txn.CommitTimestamp()
  1680  	}
  1681  	desc.ModificationTime = modTime
  1682  	log.Infof(ctx, "publish: descID=%d (%s) version=%d mtime=%s",
  1683  		desc.ID, desc.Name, desc.Version, modTime.GoTime())
  1684  	return nil
  1685  }
  1686  
  1687  // Validate validates that the table descriptor is well formed. Checks include
  1688  // both single table and cross table invariants.
  1689  func (desc *TableDescriptor) Validate(ctx context.Context, txn *kv.Txn, codec keys.SQLCodec) error {
  1690  	err := desc.ValidateTable()
  1691  	if err != nil {
  1692  		return err
  1693  	}
  1694  	if desc.Dropped() {
  1695  		return nil
  1696  	}
  1697  	return desc.validateCrossReferences(ctx, txn, codec)
  1698  }
  1699  
  1700  // validateCrossReferences validates that each reference to another table is
  1701  // resolvable and that the necessary back references exist.
  1702  func (desc *TableDescriptor) validateCrossReferences(
  1703  	ctx context.Context, txn *kv.Txn, codec keys.SQLCodec,
  1704  ) error {
  1705  	// Check that parent DB exists.
  1706  	{
  1707  		res, err := txn.Get(ctx, MakeDescMetadataKey(codec, desc.ParentID))
  1708  		if err != nil {
  1709  			return err
  1710  		}
  1711  		if !res.Exists() {
  1712  			return errors.AssertionFailedf("parentID %d does not exist", errors.Safe(desc.ParentID))
  1713  		}
  1714  	}
  1715  
  1716  	tablesByID := map[ID]*TableDescriptor{desc.ID: desc}
  1717  	getTable := func(id ID) (*TableDescriptor, error) {
  1718  		if table, ok := tablesByID[id]; ok {
  1719  			return table, nil
  1720  		}
  1721  		table, err := GetTableDescFromID(ctx, txn, codec, id)
  1722  		if err != nil {
  1723  			return nil, err
  1724  		}
  1725  		tablesByID[id] = table
  1726  		return table, nil
  1727  	}
  1728  
  1729  	findTargetIndex := func(tableID ID, indexID IndexID) (*TableDescriptor, *IndexDescriptor, error) {
  1730  		targetTable, err := getTable(tableID)
  1731  		if err != nil {
  1732  			return nil, nil, errors.Wrapf(err,
  1733  				"missing table=%d index=%d", errors.Safe(tableID), errors.Safe(indexID))
  1734  		}
  1735  		targetIndex, err := targetTable.FindIndexByID(indexID)
  1736  		if err != nil {
  1737  			return nil, nil, errors.Wrapf(err,
  1738  				"missing table=%s index=%d", targetTable.Name, errors.Safe(indexID))
  1739  		}
  1740  		return targetTable, targetIndex, nil
  1741  	}
  1742  
  1743  	// Check foreign keys.
  1744  	for i := range desc.OutboundFKs {
  1745  		fk := &desc.OutboundFKs[i]
  1746  		referencedTable, err := getTable(fk.ReferencedTableID)
  1747  		if err != nil {
  1748  			return errors.Wrapf(err,
  1749  				"invalid foreign key: missing table=%d", errors.Safe(fk.ReferencedTableID))
  1750  		}
  1751  		found := false
  1752  		for i := range referencedTable.InboundFKs {
  1753  			backref := &referencedTable.InboundFKs[i]
  1754  			if backref.OriginTableID == desc.ID && backref.Name == fk.Name {
  1755  				found = true
  1756  				break
  1757  			}
  1758  		}
  1759  		if !found {
  1760  			return errors.AssertionFailedf("missing fk back reference %q to %q from %q",
  1761  				fk.Name, desc.Name, referencedTable.Name)
  1762  		}
  1763  	}
  1764  	for i := range desc.InboundFKs {
  1765  		backref := &desc.InboundFKs[i]
  1766  		originTable, err := getTable(backref.OriginTableID)
  1767  		if err != nil {
  1768  			return errors.Wrapf(err,
  1769  				"invalid foreign key backreference: missing table=%d", errors.Safe(backref.OriginTableID))
  1770  		}
  1771  		found := false
  1772  		for i := range originTable.OutboundFKs {
  1773  			fk := &originTable.OutboundFKs[i]
  1774  			if fk.ReferencedTableID == desc.ID && fk.Name == backref.Name {
  1775  				found = true
  1776  				break
  1777  			}
  1778  		}
  1779  		if !found {
  1780  			return errors.AssertionFailedf("missing fk forward reference %q to %q from %q",
  1781  				backref.Name, desc.Name, originTable.Name)
  1782  		}
  1783  	}
  1784  
  1785  	for _, index := range desc.AllNonDropIndexes() {
  1786  		// Check interleaves.
  1787  		if len(index.Interleave.Ancestors) > 0 {
  1788  			// Only check the most recent ancestor, the rest of them don't point
  1789  			// back.
  1790  			ancestor := index.Interleave.Ancestors[len(index.Interleave.Ancestors)-1]
  1791  			targetTable, targetIndex, err := findTargetIndex(ancestor.TableID, ancestor.IndexID)
  1792  			if err != nil {
  1793  				return errors.Wrapf(err, "invalid interleave")
  1794  			}
  1795  			found := false
  1796  			for _, backref := range targetIndex.InterleavedBy {
  1797  				if backref.Table == desc.ID && backref.Index == index.ID {
  1798  					found = true
  1799  					break
  1800  				}
  1801  			}
  1802  			if !found {
  1803  				return errors.AssertionFailedf(
  1804  					"missing interleave back reference to %q@%q from %q@%q",
  1805  					desc.Name, index.Name, targetTable.Name, targetIndex.Name)
  1806  			}
  1807  		}
  1808  		interleaveBackrefs := make(map[ForeignKeyReference]struct{})
  1809  		for _, backref := range index.InterleavedBy {
  1810  			if _, ok := interleaveBackrefs[backref]; ok {
  1811  				return errors.AssertionFailedf("duplicated interleave backreference %+v", backref)
  1812  			}
  1813  			interleaveBackrefs[backref] = struct{}{}
  1814  			targetTable, err := getTable(backref.Table)
  1815  			if err != nil {
  1816  				return errors.Wrapf(err,
  1817  					"invalid interleave backreference table=%d index=%d",
  1818  					backref.Table, backref.Index)
  1819  			}
  1820  			targetIndex, err := targetTable.FindIndexByID(backref.Index)
  1821  			if err != nil {
  1822  				return errors.Wrapf(err,
  1823  					"invalid interleave backreference table=%s index=%d",
  1824  					targetTable.Name, backref.Index)
  1825  			}
  1826  			if len(targetIndex.Interleave.Ancestors) == 0 {
  1827  				return errors.AssertionFailedf(
  1828  					"broken interleave backward reference from %q@%q to %q@%q",
  1829  					desc.Name, index.Name, targetTable.Name, targetIndex.Name)
  1830  			}
  1831  			// The last ancestor is required to be a backreference.
  1832  			ancestor := targetIndex.Interleave.Ancestors[len(targetIndex.Interleave.Ancestors)-1]
  1833  			if ancestor.TableID != desc.ID || ancestor.IndexID != index.ID {
  1834  				return errors.AssertionFailedf(
  1835  					"broken interleave backward reference from %q@%q to %q@%q",
  1836  					desc.Name, index.Name, targetTable.Name, targetIndex.Name)
  1837  			}
  1838  		}
  1839  	}
  1840  	// TODO(dan): Also validate SharedPrefixLen in the interleaves.
  1841  	return nil
  1842  }
  1843  
  1844  // ValidateIndexNameIsUnique validates that the index name does not exist.
  1845  func (desc *TableDescriptor) ValidateIndexNameIsUnique(indexName string) error {
  1846  	for _, index := range desc.AllNonDropIndexes() {
  1847  		if indexName == index.Name {
  1848  			return NewRelationAlreadyExistsError(indexName)
  1849  		}
  1850  	}
  1851  	return nil
  1852  }
  1853  
  1854  // ValidateTable validates that the table descriptor is well formed. Checks
  1855  // include validating the table, column and index names, verifying that column
  1856  // names and index names are unique and verifying that column IDs and index IDs
  1857  // are consistent. Use Validate to validate that cross-table references are
  1858  // correct.
  1859  // If version is supplied, the descriptor is checked for version incompatibilities.
  1860  func (desc *TableDescriptor) ValidateTable() error {
  1861  	if err := validateName(desc.Name, "table"); err != nil {
  1862  		return err
  1863  	}
  1864  	if desc.ID == 0 {
  1865  		return errors.AssertionFailedf("invalid table ID %d", errors.Safe(desc.ID))
  1866  	}
  1867  
  1868  	// TODO(dt, nathan): virtual descs don't validate (missing privs, PK, etc).
  1869  	if desc.IsVirtualTable() {
  1870  		return nil
  1871  	}
  1872  
  1873  	if desc.IsSequence() {
  1874  		return nil
  1875  	}
  1876  
  1877  	// ParentID is the ID of the database holding this table.
  1878  	// It is often < ID, except when a table gets moved across databases.
  1879  	if desc.ParentID == 0 {
  1880  		return errors.AssertionFailedf("invalid parent ID %d", errors.Safe(desc.ParentID))
  1881  	}
  1882  
  1883  	// We maintain forward compatibility, so if you see this error message with a
  1884  	// version older that what this client supports, then there's a
  1885  	// MaybeFillInDescriptor missing from some codepath.
  1886  	if v := desc.GetFormatVersion(); v != FamilyFormatVersion && v != InterleavedFormatVersion {
  1887  		// TODO(dan): We're currently switching from FamilyFormatVersion to
  1888  		// InterleavedFormatVersion. After a beta is released with this dual version
  1889  		// support, then:
  1890  		// - Upgrade the bidirectional reference version to that beta
  1891  		// - Start constructing all TableDescriptors with InterleavedFormatVersion
  1892  		// - Change maybeUpgradeFormatVersion to output InterleavedFormatVersion
  1893  		// - Change this check to only allow InterleavedFormatVersion
  1894  		return errors.AssertionFailedf(
  1895  			"table %q is encoded using using version %d, but this client only supports version %d and %d",
  1896  			desc.Name, errors.Safe(desc.GetFormatVersion()),
  1897  			errors.Safe(FamilyFormatVersion), errors.Safe(InterleavedFormatVersion))
  1898  	}
  1899  
  1900  	if len(desc.Columns) == 0 {
  1901  		return ErrMissingColumns
  1902  	}
  1903  
  1904  	if err := desc.CheckUniqueConstraints(); err != nil {
  1905  		return err
  1906  	}
  1907  
  1908  	columnNames := make(map[string]ColumnID, len(desc.Columns))
  1909  	columnIDs := make(map[ColumnID]string, len(desc.Columns))
  1910  	for _, column := range desc.AllNonDropColumns() {
  1911  		if err := validateName(column.Name, "column"); err != nil {
  1912  			return err
  1913  		}
  1914  		if column.ID == 0 {
  1915  			return errors.AssertionFailedf("invalid column ID %d", errors.Safe(column.ID))
  1916  		}
  1917  
  1918  		if _, columnNameExists := columnNames[column.Name]; columnNameExists {
  1919  			for i := range desc.Columns {
  1920  				if desc.Columns[i].Name == column.Name {
  1921  					return pgerror.Newf(pgcode.DuplicateColumn,
  1922  						"duplicate column name: %q", column.Name)
  1923  				}
  1924  			}
  1925  			return pgerror.Newf(pgcode.DuplicateColumn,
  1926  				"duplicate: column %q in the middle of being added, not yet public", column.Name)
  1927  		}
  1928  		columnNames[column.Name] = column.ID
  1929  
  1930  		if other, ok := columnIDs[column.ID]; ok {
  1931  			return fmt.Errorf("column %q duplicate ID of column %q: %d",
  1932  				column.Name, other, column.ID)
  1933  		}
  1934  		columnIDs[column.ID] = column.Name
  1935  
  1936  		if column.ID >= desc.NextColumnID {
  1937  			return errors.AssertionFailedf("column %q invalid ID (%d) >= next column ID (%d)",
  1938  				column.Name, errors.Safe(column.ID), errors.Safe(desc.NextColumnID))
  1939  		}
  1940  	}
  1941  
  1942  	for _, m := range desc.Mutations {
  1943  		unSetEnums := m.State == DescriptorMutation_UNKNOWN || m.Direction == DescriptorMutation_NONE
  1944  		switch desc := m.Descriptor_.(type) {
  1945  		case *DescriptorMutation_Column:
  1946  			col := desc.Column
  1947  			if unSetEnums {
  1948  				return errors.AssertionFailedf(
  1949  					"mutation in state %s, direction %s, col %q, id %v",
  1950  					errors.Safe(m.State), errors.Safe(m.Direction), col.Name, errors.Safe(col.ID))
  1951  			}
  1952  			columnIDs[col.ID] = col.Name
  1953  		case *DescriptorMutation_Index:
  1954  			if unSetEnums {
  1955  				idx := desc.Index
  1956  				return errors.AssertionFailedf(
  1957  					"mutation in state %s, direction %s, index %s, id %v",
  1958  					errors.Safe(m.State), errors.Safe(m.Direction), idx.Name, errors.Safe(idx.ID))
  1959  			}
  1960  		case *DescriptorMutation_Constraint:
  1961  			if unSetEnums {
  1962  				return errors.AssertionFailedf(
  1963  					"mutation in state %s, direction %s, constraint %v",
  1964  					errors.Safe(m.State), errors.Safe(m.Direction), desc.Constraint.Name)
  1965  			}
  1966  		case *DescriptorMutation_PrimaryKeySwap:
  1967  			if m.Direction == DescriptorMutation_NONE {
  1968  				return errors.AssertionFailedf(
  1969  					"primary key swap mutation in state %s, direction %s", errors.Safe(m.State), errors.Safe(m.Direction))
  1970  			}
  1971  		case *DescriptorMutation_ComputedColumnSwap:
  1972  			if m.Direction == DescriptorMutation_NONE {
  1973  				return errors.AssertionFailedf(
  1974  					"computed column swap mutation in state %s, direction %s", errors.Safe(m.State), errors.Safe(m.Direction))
  1975  			}
  1976  		default:
  1977  			return errors.AssertionFailedf(
  1978  				"mutation in state %s, direction %s, and no column/index descriptor",
  1979  				errors.Safe(m.State), errors.Safe(m.Direction))
  1980  		}
  1981  	}
  1982  
  1983  	// TODO(dt): Validate each column only appears at-most-once in any FKs.
  1984  
  1985  	// Only validate column families and indexes if this is actually a table, not
  1986  	// if it's just a view.
  1987  	if desc.IsPhysicalTable() {
  1988  		if err := desc.validateColumnFamilies(columnIDs); err != nil {
  1989  			return err
  1990  		}
  1991  
  1992  		if err := desc.validateTableIndexes(columnNames); err != nil {
  1993  			return err
  1994  		}
  1995  		if err := desc.validatePartitioning(); err != nil {
  1996  			return err
  1997  		}
  1998  	}
  1999  
  2000  	// Fill in any incorrect privileges that may have been missed due to mixed-versions.
  2001  	// TODO(mberhault): remove this in 2.1 (maybe 2.2) when privilege-fixing migrations have been
  2002  	// run again and mixed-version clusters always write "good" descriptors.
  2003  	desc.Privileges.MaybeFixPrivileges(desc.GetID())
  2004  
  2005  	// Ensure that mutations cannot be queued if a primary key change or
  2006  	// an alter column type schema change has either been started in
  2007  	// this transaction, or is currently in progress.
  2008  	var alterPKMutation MutationID
  2009  	var alterColumnTypeMutation MutationID
  2010  	var foundAlterPK bool
  2011  	var foundAlterColumnType bool
  2012  
  2013  	for _, m := range desc.Mutations {
  2014  		// If we have seen an alter primary key mutation, then
  2015  		// m we are considering right now is invalid.
  2016  		if foundAlterPK {
  2017  			if alterPKMutation == m.MutationID {
  2018  				return unimplemented.NewWithIssue(
  2019  					45615,
  2020  					"cannot perform other schema changes in the same transaction as a primary key change",
  2021  				)
  2022  			}
  2023  			return unimplemented.NewWithIssue(
  2024  				45615,
  2025  				"cannot perform a schema change operation while a primary key change is in progress",
  2026  			)
  2027  		}
  2028  		if foundAlterColumnType {
  2029  			if alterColumnTypeMutation == m.MutationID {
  2030  				return unimplemented.NewWithIssue(
  2031  					47137,
  2032  					"cannot perform other schema changes in the same transaction as an ALTER COLUMN TYPE schema change",
  2033  				)
  2034  			}
  2035  			return unimplemented.NewWithIssue(
  2036  				47137,
  2037  				"cannot perform a schema change operation while an ALTER COLUMN TYPE schema change is in progress",
  2038  			)
  2039  		}
  2040  		if m.GetPrimaryKeySwap() != nil {
  2041  			foundAlterPK = true
  2042  			alterPKMutation = m.MutationID
  2043  		}
  2044  		if m.GetComputedColumnSwap() != nil {
  2045  			foundAlterColumnType = true
  2046  			alterColumnTypeMutation = m.MutationID
  2047  		}
  2048  	}
  2049  
  2050  	// Validate the privilege descriptor.
  2051  	return desc.Privileges.Validate(desc.GetID())
  2052  }
  2053  
  2054  func (desc *TableDescriptor) validateColumnFamilies(columnIDs map[ColumnID]string) error {
  2055  	if len(desc.Families) < 1 {
  2056  		return fmt.Errorf("at least 1 column family must be specified")
  2057  	}
  2058  	if desc.Families[0].ID != FamilyID(0) {
  2059  		return fmt.Errorf("the 0th family must have ID 0")
  2060  	}
  2061  
  2062  	familyNames := map[string]struct{}{}
  2063  	familyIDs := map[FamilyID]string{}
  2064  	colIDToFamilyID := map[ColumnID]FamilyID{}
  2065  	for i := range desc.Families {
  2066  		family := &desc.Families[i]
  2067  		if err := validateName(family.Name, "family"); err != nil {
  2068  			return err
  2069  		}
  2070  
  2071  		if i != 0 {
  2072  			prevFam := desc.Families[i-1]
  2073  			if family.ID < prevFam.ID {
  2074  				return errors.Newf(
  2075  					"family %s at index %d has id %d less than family %s at index %d with id %d",
  2076  					family.Name, i, family.ID, prevFam.Name, i-1, prevFam.ID)
  2077  			}
  2078  		}
  2079  
  2080  		if _, ok := familyNames[family.Name]; ok {
  2081  			return fmt.Errorf("duplicate family name: %q", family.Name)
  2082  		}
  2083  		familyNames[family.Name] = struct{}{}
  2084  
  2085  		if other, ok := familyIDs[family.ID]; ok {
  2086  			return fmt.Errorf("family %q duplicate ID of family %q: %d",
  2087  				family.Name, other, family.ID)
  2088  		}
  2089  		familyIDs[family.ID] = family.Name
  2090  
  2091  		if family.ID >= desc.NextFamilyID {
  2092  			return fmt.Errorf("family %q invalid family ID (%d) > next family ID (%d)",
  2093  				family.Name, family.ID, desc.NextFamilyID)
  2094  		}
  2095  
  2096  		if len(family.ColumnIDs) != len(family.ColumnNames) {
  2097  			return fmt.Errorf("mismatched column ID size (%d) and name size (%d)",
  2098  				len(family.ColumnIDs), len(family.ColumnNames))
  2099  		}
  2100  
  2101  		for i, colID := range family.ColumnIDs {
  2102  			name, ok := columnIDs[colID]
  2103  			if !ok {
  2104  				return fmt.Errorf("family %q contains unknown column \"%d\"", family.Name, colID)
  2105  			}
  2106  			if name != family.ColumnNames[i] {
  2107  				return fmt.Errorf("family %q column %d should have name %q, but found name %q",
  2108  					family.Name, colID, name, family.ColumnNames[i])
  2109  			}
  2110  		}
  2111  
  2112  		for _, colID := range family.ColumnIDs {
  2113  			if famID, ok := colIDToFamilyID[colID]; ok {
  2114  				return fmt.Errorf("column %d is in both family %d and %d", colID, famID, family.ID)
  2115  			}
  2116  			colIDToFamilyID[colID] = family.ID
  2117  		}
  2118  	}
  2119  	for colID := range columnIDs {
  2120  		if _, ok := colIDToFamilyID[colID]; !ok {
  2121  			return fmt.Errorf("column %d is not in any column family", colID)
  2122  		}
  2123  	}
  2124  	return nil
  2125  }
  2126  
  2127  // validateTableIndexes validates that indexes are well formed. Checks include
  2128  // validating the columns involved in the index, verifying the index names and
  2129  // IDs are unique, and the family of the primary key is 0. This does not check
  2130  // if indexes are unique (i.e. same set of columns, direction, and uniqueness)
  2131  // as there are practical uses for them.
  2132  func (desc *TableDescriptor) validateTableIndexes(columnNames map[string]ColumnID) error {
  2133  	if len(desc.PrimaryIndex.ColumnIDs) == 0 {
  2134  		return ErrMissingPrimaryKey
  2135  	}
  2136  
  2137  	indexNames := map[string]struct{}{}
  2138  	indexIDs := map[IndexID]string{}
  2139  	for _, index := range desc.AllNonDropIndexes() {
  2140  		if err := validateName(index.Name, "index"); err != nil {
  2141  			return err
  2142  		}
  2143  		if index.ID == 0 {
  2144  			return fmt.Errorf("invalid index ID %d", index.ID)
  2145  		}
  2146  
  2147  		if _, indexNameExists := indexNames[index.Name]; indexNameExists {
  2148  			for i := range desc.Indexes {
  2149  				if desc.Indexes[i].Name == index.Name {
  2150  					// This error should be caught in MakeIndexDescriptor.
  2151  					return errors.HandleAsAssertionFailure(fmt.Errorf("duplicate index name: %q", index.Name))
  2152  				}
  2153  			}
  2154  			// This error should be caught in MakeIndexDescriptor.
  2155  			return errors.HandleAsAssertionFailure(fmt.Errorf(
  2156  				"duplicate: index %q in the middle of being added, not yet public", index.Name))
  2157  		}
  2158  		indexNames[index.Name] = struct{}{}
  2159  
  2160  		if other, ok := indexIDs[index.ID]; ok {
  2161  			return fmt.Errorf("index %q duplicate ID of index %q: %d",
  2162  				index.Name, other, index.ID)
  2163  		}
  2164  		indexIDs[index.ID] = index.Name
  2165  
  2166  		if index.ID >= desc.NextIndexID {
  2167  			return fmt.Errorf("index %q invalid index ID (%d) > next index ID (%d)",
  2168  				index.Name, index.ID, desc.NextIndexID)
  2169  		}
  2170  
  2171  		if len(index.ColumnIDs) != len(index.ColumnNames) {
  2172  			return fmt.Errorf("mismatched column IDs (%d) and names (%d)",
  2173  				len(index.ColumnIDs), len(index.ColumnNames))
  2174  		}
  2175  		if len(index.ColumnIDs) != len(index.ColumnDirections) {
  2176  			return fmt.Errorf("mismatched column IDs (%d) and directions (%d)",
  2177  				len(index.ColumnIDs), len(index.ColumnDirections))
  2178  		}
  2179  
  2180  		if len(index.ColumnIDs) == 0 {
  2181  			return fmt.Errorf("index %q must contain at least 1 column", index.Name)
  2182  		}
  2183  
  2184  		validateIndexDup := make(map[ColumnID]struct{})
  2185  		for i, name := range index.ColumnNames {
  2186  			colID, ok := columnNames[name]
  2187  			if !ok {
  2188  				return fmt.Errorf("index %q contains unknown column %q", index.Name, name)
  2189  			}
  2190  			if colID != index.ColumnIDs[i] {
  2191  				return fmt.Errorf("index %q column %q should have ID %d, but found ID %d",
  2192  					index.Name, name, colID, index.ColumnIDs[i])
  2193  			}
  2194  			if _, ok := validateIndexDup[colID]; ok {
  2195  				return fmt.Errorf("index %q contains duplicate column %q", index.Name, name)
  2196  			}
  2197  			validateIndexDup[colID] = struct{}{}
  2198  		}
  2199  		if index.IsSharded() {
  2200  			if err := desc.ensureShardedIndexNotComputed(index); err != nil {
  2201  				return err
  2202  			}
  2203  			if _, exists := columnNames[index.Sharded.Name]; !exists {
  2204  				return fmt.Errorf("index %q refers to non-existent shard column %q",
  2205  					index.Name, index.Sharded.Name)
  2206  			}
  2207  		}
  2208  	}
  2209  
  2210  	return nil
  2211  }
  2212  
  2213  // ensureShardedIndexNotComputed ensures that the sharded index is not based on a computed
  2214  // column. This is because the sharded index is based on a hidden computed shard column
  2215  // under the hood and we don't support transitively computed columns (computed column A
  2216  // based on another computed column B).
  2217  func (desc *TableDescriptor) ensureShardedIndexNotComputed(index *IndexDescriptor) error {
  2218  	for _, colName := range index.Sharded.ColumnNames {
  2219  		col, _, err := desc.FindColumnByName(tree.Name(colName))
  2220  		if err != nil {
  2221  			return err
  2222  		}
  2223  		if col.IsComputed() {
  2224  			return pgerror.Newf(pgcode.InvalidTableDefinition,
  2225  				"cannot create a sharded index on a computed column")
  2226  		}
  2227  	}
  2228  	return nil
  2229  }
  2230  
  2231  // PrimaryKeyString returns the pretty-printed primary key declaration for a
  2232  // table descriptor.
  2233  func (desc *TableDescriptor) PrimaryKeyString() string {
  2234  	var primaryKeyString strings.Builder
  2235  	primaryKeyString.WriteString("PRIMARY KEY (%s)")
  2236  	if desc.PrimaryIndex.IsSharded() {
  2237  		fmt.Fprintf(&primaryKeyString, " USING HASH WITH BUCKET_COUNT = %v",
  2238  			desc.PrimaryIndex.Sharded.ShardBuckets)
  2239  	}
  2240  	return fmt.Sprintf(primaryKeyString.String(),
  2241  		desc.PrimaryIndex.ColNamesString(),
  2242  	)
  2243  }
  2244  
  2245  // validatePartitioningDescriptor validates that a PartitioningDescriptor, which
  2246  // may represent a subpartition, is well-formed. Checks include validating the
  2247  // index-level uniqueness of all partition names, validating that the encoded
  2248  // tuples match the corresponding column types, and that range partitions are
  2249  // stored sorted by upper bound. colOffset is non-zero for subpartitions and
  2250  // indicates how many index columns to skip over.
  2251  func (desc *TableDescriptor) validatePartitioningDescriptor(
  2252  	a *DatumAlloc,
  2253  	idxDesc *IndexDescriptor,
  2254  	partDesc *PartitioningDescriptor,
  2255  	colOffset int,
  2256  	partitionNames map[string]string,
  2257  ) error {
  2258  	if partDesc.NumColumns == 0 {
  2259  		return nil
  2260  	}
  2261  
  2262  	// TODO(dan): The sqlccl.GenerateSubzoneSpans logic is easier if we disallow
  2263  	// setting zone configs on indexes that are interleaved into another index.
  2264  	// InterleavedBy is fine, so using the root of the interleave hierarchy will
  2265  	// work. It is expected that this is sufficient for real-world use cases.
  2266  	// Revisit this restriction if that expectation is wrong.
  2267  	if len(idxDesc.Interleave.Ancestors) > 0 {
  2268  		return errors.Errorf("cannot set a zone config for interleaved index %s; "+
  2269  			"set it on the root of the interleaved hierarchy instead", idxDesc.Name)
  2270  	}
  2271  
  2272  	// We don't need real prefixes in the DecodePartitionTuple calls because we're
  2273  	// only using it to look for collisions and the prefix would be the same for
  2274  	// all of them. Faking them out with DNull allows us to make O(list partition)
  2275  	// calls to DecodePartitionTuple instead of O(list partition entry).
  2276  	fakePrefixDatums := make([]tree.Datum, colOffset)
  2277  	for i := range fakePrefixDatums {
  2278  		fakePrefixDatums[i] = tree.DNull
  2279  	}
  2280  
  2281  	if len(partDesc.List) == 0 && len(partDesc.Range) == 0 {
  2282  		return fmt.Errorf("at least one of LIST or RANGE partitioning must be used")
  2283  	}
  2284  	if len(partDesc.List) > 0 && len(partDesc.Range) > 0 {
  2285  		return fmt.Errorf("only one LIST or RANGE partitioning may used")
  2286  	}
  2287  
  2288  	checkName := func(name string) error {
  2289  		if len(name) == 0 {
  2290  			return fmt.Errorf("PARTITION name must be non-empty")
  2291  		}
  2292  		if indexName, exists := partitionNames[name]; exists {
  2293  			if indexName == idxDesc.Name {
  2294  				return fmt.Errorf("PARTITION %s: name must be unique (used twice in index %q)",
  2295  					name, indexName)
  2296  			}
  2297  		}
  2298  		partitionNames[name] = idxDesc.Name
  2299  		return nil
  2300  	}
  2301  
  2302  	// Use the system-tenant SQL codec when validating the keys in the partition
  2303  	// descriptor. We just want to know how the partitions relate to one another,
  2304  	// so it's fine to ignore the tenant ID prefix.
  2305  	codec := keys.SystemSQLCodec
  2306  
  2307  	if len(partDesc.List) > 0 {
  2308  		listValues := make(map[string]struct{}, len(partDesc.List))
  2309  		for _, p := range partDesc.List {
  2310  			if err := checkName(p.Name); err != nil {
  2311  				return err
  2312  			}
  2313  
  2314  			if len(p.Values) == 0 {
  2315  				return fmt.Errorf("PARTITION %s: must contain values", p.Name)
  2316  			}
  2317  			// NB: key encoding is used to check uniqueness because it has
  2318  			// to match the behavior of the value when indexed.
  2319  			for _, valueEncBuf := range p.Values {
  2320  				tuple, keyPrefix, err := DecodePartitionTuple(
  2321  					a, codec, desc, idxDesc, partDesc, valueEncBuf, fakePrefixDatums)
  2322  				if err != nil {
  2323  					return fmt.Errorf("PARTITION %s: %v", p.Name, err)
  2324  				}
  2325  				if _, exists := listValues[string(keyPrefix)]; exists {
  2326  					return fmt.Errorf("%s cannot be present in more than one partition", tuple)
  2327  				}
  2328  				listValues[string(keyPrefix)] = struct{}{}
  2329  			}
  2330  
  2331  			newColOffset := colOffset + int(partDesc.NumColumns)
  2332  			if err := desc.validatePartitioningDescriptor(
  2333  				a, idxDesc, &p.Subpartitioning, newColOffset, partitionNames,
  2334  			); err != nil {
  2335  				return err
  2336  			}
  2337  		}
  2338  	}
  2339  
  2340  	if len(partDesc.Range) > 0 {
  2341  		tree := interval.NewTree(interval.ExclusiveOverlapper)
  2342  		for _, p := range partDesc.Range {
  2343  			if err := checkName(p.Name); err != nil {
  2344  				return err
  2345  			}
  2346  
  2347  			// NB: key encoding is used to check uniqueness because it has to match
  2348  			// the behavior of the value when indexed.
  2349  			fromDatums, fromKey, err := DecodePartitionTuple(
  2350  				a, codec, desc, idxDesc, partDesc, p.FromInclusive, fakePrefixDatums)
  2351  			if err != nil {
  2352  				return fmt.Errorf("PARTITION %s: %v", p.Name, err)
  2353  			}
  2354  			toDatums, toKey, err := DecodePartitionTuple(
  2355  				a, codec, desc, idxDesc, partDesc, p.ToExclusive, fakePrefixDatums)
  2356  			if err != nil {
  2357  				return fmt.Errorf("PARTITION %s: %v", p.Name, err)
  2358  			}
  2359  			pi := partitionInterval{p.Name, fromKey, toKey}
  2360  			if overlaps := tree.Get(pi.Range()); len(overlaps) > 0 {
  2361  				return fmt.Errorf("partitions %s and %s overlap",
  2362  					overlaps[0].(partitionInterval).name, p.Name)
  2363  			}
  2364  			if err := tree.Insert(pi, false /* fast */); errors.Is(err, interval.ErrEmptyRange) {
  2365  				return fmt.Errorf("PARTITION %s: empty range: lower bound %s is equal to upper bound %s",
  2366  					p.Name, fromDatums, toDatums)
  2367  			} else if errors.Is(err, interval.ErrInvertedRange) {
  2368  				return fmt.Errorf("PARTITION %s: empty range: lower bound %s is greater than upper bound %s",
  2369  					p.Name, fromDatums, toDatums)
  2370  			} else if err != nil {
  2371  				return errors.Wrapf(err, "PARTITION %s", p.Name)
  2372  			}
  2373  		}
  2374  	}
  2375  
  2376  	return nil
  2377  }
  2378  
  2379  // FindIndexesWithPartition returns all IndexDescriptors (potentially including
  2380  // the primary index) which have a partition with the given name.
  2381  func (desc *TableDescriptor) FindIndexesWithPartition(name string) []*IndexDescriptor {
  2382  	var indexes []*IndexDescriptor
  2383  	for _, idx := range desc.AllNonDropIndexes() {
  2384  		if idx.FindPartitionByName(name) != nil {
  2385  			indexes = append(indexes, idx)
  2386  		}
  2387  	}
  2388  	return indexes
  2389  }
  2390  
  2391  // validatePartitioning validates that any PartitioningDescriptors contained in
  2392  // table indexes are well-formed. See validatePartitioningDesc for details.
  2393  func (desc *TableDescriptor) validatePartitioning() error {
  2394  	partitionNames := make(map[string]string)
  2395  
  2396  	a := &DatumAlloc{}
  2397  	return desc.ForeachNonDropIndex(func(idxDesc *IndexDescriptor) error {
  2398  		return desc.validatePartitioningDescriptor(
  2399  			a, idxDesc, &idxDesc.Partitioning, 0 /* colOffset */, partitionNames,
  2400  		)
  2401  	})
  2402  }
  2403  
  2404  // FamilyHeuristicTargetBytes is the target total byte size of columns that the
  2405  // current heuristic will assign to a family.
  2406  const FamilyHeuristicTargetBytes = 256
  2407  
  2408  // fitColumnToFamily attempts to fit a new column into the existing column
  2409  // families. If the heuristics find a fit, true is returned along with the
  2410  // index of the selected family. Otherwise, false is returned and the column
  2411  // should be put in a new family.
  2412  //
  2413  // Current heuristics:
  2414  // - Put all columns in family 0.
  2415  func fitColumnToFamily(desc *MutableTableDescriptor, col ColumnDescriptor) (int, bool) {
  2416  	// Fewer column families means fewer kv entries, which is generally faster.
  2417  	// On the other hand, an update to any column in a family requires that they
  2418  	// all are read and rewritten, so large (or numerous) columns that are not
  2419  	// updated at the same time as other columns in the family make things
  2420  	// slower.
  2421  	//
  2422  	// The initial heuristic used for family assignment tried to pack
  2423  	// fixed-width columns into families up to a certain size and would put any
  2424  	// variable-width column into its own family. This was conservative to
  2425  	// guarantee that we avoid the worst-case behavior of a very large immutable
  2426  	// blob in the same family as frequently updated columns.
  2427  	//
  2428  	// However, our initial customers have revealed that this is backward.
  2429  	// Repeatedly, they have recreated existing schemas without any tuning and
  2430  	// found lackluster performance. Each of these has turned out better as a
  2431  	// single family (sometimes 100% faster or more), the most aggressive tuning
  2432  	// possible.
  2433  	//
  2434  	// Further, as the WideTable benchmark shows, even the worst-case isn't that
  2435  	// bad (33% slower with an immutable 1MB blob, which is the upper limit of
  2436  	// what we'd recommend for column size regardless of families). This
  2437  	// situation also appears less frequent than we feared.
  2438  	//
  2439  	// The result is that we put all columns in one family and require the user
  2440  	// to manually specify family assignments when this is incorrect.
  2441  	return 0, true
  2442  }
  2443  
  2444  // ColumnTypeIsIndexable returns whether the type t is valid as an indexed column.
  2445  func ColumnTypeIsIndexable(t *types.T) bool {
  2446  	return !MustBeValueEncoded(t)
  2447  }
  2448  
  2449  // ColumnTypeIsInvertedIndexable returns whether the type t is valid to be indexed
  2450  // using an inverted index.
  2451  func ColumnTypeIsInvertedIndexable(t *types.T) bool {
  2452  	family := t.Family()
  2453  	return family == types.JsonFamily || family == types.ArrayFamily ||
  2454  		family == types.GeographyFamily || family == types.GeometryFamily
  2455  }
  2456  
  2457  func notIndexableError(cols []ColumnDescriptor, inverted bool) error {
  2458  	if len(cols) == 0 {
  2459  		return nil
  2460  	}
  2461  	var msg string
  2462  	var typInfo string
  2463  	if len(cols) == 1 {
  2464  		col := &cols[0]
  2465  		msg = "column %s is of type %s and thus is not indexable"
  2466  		if inverted {
  2467  			msg += " with an inverted index"
  2468  		}
  2469  		typInfo = col.Type.DebugString()
  2470  		msg = fmt.Sprintf(msg, col.Name, col.Type.Name())
  2471  	} else {
  2472  		msg = "the following columns are not indexable due to their type: "
  2473  		for i := range cols {
  2474  			col := &cols[i]
  2475  			msg += fmt.Sprintf("%s (type %s)", col.Name, col.Type.Name())
  2476  			typInfo += col.Type.DebugString()
  2477  			if i != len(cols)-1 {
  2478  				msg += ", "
  2479  				typInfo += ","
  2480  			}
  2481  		}
  2482  	}
  2483  	return unimplemented.NewWithIssueDetailf(35730, typInfo, "%s", msg)
  2484  }
  2485  
  2486  func checkColumnsValidForIndex(tableDesc *MutableTableDescriptor, indexColNames []string) error {
  2487  	invalidColumns := make([]ColumnDescriptor, 0, len(indexColNames))
  2488  	for _, indexCol := range indexColNames {
  2489  		for _, col := range tableDesc.AllNonDropColumns() {
  2490  			if col.Name == indexCol {
  2491  				if !ColumnTypeIsIndexable(col.Type) {
  2492  					invalidColumns = append(invalidColumns, col)
  2493  				}
  2494  			}
  2495  		}
  2496  	}
  2497  	if len(invalidColumns) > 0 {
  2498  		return notIndexableError(invalidColumns, false)
  2499  	}
  2500  	return nil
  2501  }
  2502  
  2503  func checkColumnsValidForInvertedIndex(
  2504  	tableDesc *MutableTableDescriptor, indexColNames []string,
  2505  ) error {
  2506  	if len((indexColNames)) > 1 {
  2507  		return unimplemented.NewWithIssue(48100,
  2508  			"indexing more than one column with an inverted index is not supported")
  2509  	}
  2510  	invalidColumns := make([]ColumnDescriptor, 0, len(indexColNames))
  2511  	for _, indexCol := range indexColNames {
  2512  		for _, col := range tableDesc.AllNonDropColumns() {
  2513  			if col.Name == indexCol {
  2514  				if !ColumnTypeIsInvertedIndexable(col.Type) {
  2515  					invalidColumns = append(invalidColumns, col)
  2516  				}
  2517  			}
  2518  		}
  2519  	}
  2520  	if len(invalidColumns) > 0 {
  2521  		return notIndexableError(invalidColumns, true)
  2522  	}
  2523  	return nil
  2524  }
  2525  
  2526  // AddColumn adds a column to the table.
  2527  func (desc *MutableTableDescriptor) AddColumn(col *ColumnDescriptor) {
  2528  	desc.Columns = append(desc.Columns, *col)
  2529  }
  2530  
  2531  // AddFamily adds a family to the table.
  2532  func (desc *MutableTableDescriptor) AddFamily(fam ColumnFamilyDescriptor) {
  2533  	desc.Families = append(desc.Families, fam)
  2534  }
  2535  
  2536  // AddIndex adds an index to the table.
  2537  func (desc *MutableTableDescriptor) AddIndex(idx IndexDescriptor, primary bool) error {
  2538  	if idx.Type == IndexDescriptor_FORWARD {
  2539  		if err := checkColumnsValidForIndex(desc, idx.ColumnNames); err != nil {
  2540  			return err
  2541  		}
  2542  
  2543  		if primary {
  2544  			// PrimaryIndex is unset.
  2545  			if desc.PrimaryIndex.Name == "" {
  2546  				if idx.Name == "" {
  2547  					// Only override the index name if it hasn't been set by the user.
  2548  					idx.Name = PrimaryKeyIndexName
  2549  				}
  2550  				desc.PrimaryIndex = idx
  2551  			} else {
  2552  				return fmt.Errorf("multiple primary keys for table %q are not allowed", desc.Name)
  2553  			}
  2554  		} else {
  2555  			desc.Indexes = append(desc.Indexes, idx)
  2556  		}
  2557  
  2558  	} else {
  2559  		if err := checkColumnsValidForInvertedIndex(desc, idx.ColumnNames); err != nil {
  2560  			return err
  2561  		}
  2562  		desc.Indexes = append(desc.Indexes, idx)
  2563  	}
  2564  
  2565  	return nil
  2566  }
  2567  
  2568  // AddColumnToFamilyMaybeCreate adds the specified column to the specified
  2569  // family. If it doesn't exist and create is true, creates it. If it does exist
  2570  // adds it unless "strict" create (`true` for create but `false` for
  2571  // ifNotExists) is specified.
  2572  //
  2573  // AllocateIDs must be called before the TableDescriptor will be valid.
  2574  func (desc *MutableTableDescriptor) AddColumnToFamilyMaybeCreate(
  2575  	col string, family string, create bool, ifNotExists bool,
  2576  ) error {
  2577  	idx := int(-1)
  2578  	if len(family) > 0 {
  2579  		for i := range desc.Families {
  2580  			if desc.Families[i].Name == family {
  2581  				idx = i
  2582  				break
  2583  			}
  2584  		}
  2585  	}
  2586  
  2587  	if idx == -1 {
  2588  		if create {
  2589  			// NB: When AllocateIDs encounters an empty `Name`, it'll generate one.
  2590  			desc.AddFamily(ColumnFamilyDescriptor{Name: family, ColumnNames: []string{col}})
  2591  			return nil
  2592  		}
  2593  		return fmt.Errorf("unknown family %q", family)
  2594  	}
  2595  
  2596  	if create && !ifNotExists {
  2597  		return fmt.Errorf("family %q already exists", family)
  2598  	}
  2599  	desc.Families[idx].ColumnNames = append(desc.Families[idx].ColumnNames, col)
  2600  	return nil
  2601  }
  2602  
  2603  // RemoveColumnFromFamily removes a colID from the family it's assigned to.
  2604  func (desc *MutableTableDescriptor) RemoveColumnFromFamily(colID ColumnID) {
  2605  	for i := range desc.Families {
  2606  		for j, c := range desc.Families[i].ColumnIDs {
  2607  			if c == colID {
  2608  				desc.Families[i].ColumnIDs = append(
  2609  					desc.Families[i].ColumnIDs[:j], desc.Families[i].ColumnIDs[j+1:]...)
  2610  				desc.Families[i].ColumnNames = append(
  2611  					desc.Families[i].ColumnNames[:j], desc.Families[i].ColumnNames[j+1:]...)
  2612  				// Due to a complication with allowing primary key columns to not be restricted
  2613  				// to family 0, we might end up deleting all the columns from family 0. We will
  2614  				// allow empty column families now, but will disallow this in the future.
  2615  				// TODO (rohany): remove this once the reliance on sentinel family 0 has been removed.
  2616  				if len(desc.Families[i].ColumnIDs) == 0 && desc.Families[i].ID != 0 {
  2617  					desc.Families = append(desc.Families[:i], desc.Families[i+1:]...)
  2618  				}
  2619  				return
  2620  			}
  2621  		}
  2622  	}
  2623  }
  2624  
  2625  // RenameColumnDescriptor updates all references to a column name in
  2626  // a table descriptor including indexes and families.
  2627  func (desc *MutableTableDescriptor) RenameColumnDescriptor(
  2628  	column *ColumnDescriptor, newColName string,
  2629  ) {
  2630  	colID := column.ID
  2631  	column.Name = newColName
  2632  
  2633  	for i := range desc.Families {
  2634  		for j := range desc.Families[i].ColumnIDs {
  2635  			if desc.Families[i].ColumnIDs[j] == colID {
  2636  				desc.Families[i].ColumnNames[j] = newColName
  2637  			}
  2638  		}
  2639  	}
  2640  
  2641  	renameColumnInIndex := func(idx *IndexDescriptor) {
  2642  		for i, id := range idx.ColumnIDs {
  2643  			if id == colID {
  2644  				idx.ColumnNames[i] = newColName
  2645  			}
  2646  		}
  2647  		for i, id := range idx.StoreColumnIDs {
  2648  			if id == colID {
  2649  				idx.StoreColumnNames[i] = newColName
  2650  			}
  2651  		}
  2652  	}
  2653  	renameColumnInIndex(&desc.PrimaryIndex)
  2654  	for i := range desc.Indexes {
  2655  		renameColumnInIndex(&desc.Indexes[i])
  2656  	}
  2657  	for _, m := range desc.Mutations {
  2658  		if idx := m.GetIndex(); idx != nil {
  2659  			renameColumnInIndex(idx)
  2660  		}
  2661  	}
  2662  }
  2663  
  2664  // FindActiveColumnsByNames finds all requested columns (in the requested order)
  2665  // or returns an error.
  2666  func (desc *TableDescriptor) FindActiveColumnsByNames(
  2667  	names tree.NameList,
  2668  ) ([]ColumnDescriptor, error) {
  2669  	cols := make([]ColumnDescriptor, len(names))
  2670  	for i := range names {
  2671  		c, err := desc.FindActiveColumnByName(string(names[i]))
  2672  		if err != nil {
  2673  			return nil, err
  2674  		}
  2675  		cols[i] = *c
  2676  	}
  2677  	return cols, nil
  2678  }
  2679  
  2680  // FindColumnByName finds the column with the specified name. It returns
  2681  // an active column or a column from the mutation list. It returns true
  2682  // if the column is being dropped.
  2683  func (desc *TableDescriptor) FindColumnByName(name tree.Name) (*ColumnDescriptor, bool, error) {
  2684  	for i := range desc.Columns {
  2685  		c := &desc.Columns[i]
  2686  		if c.Name == string(name) {
  2687  			return c, false, nil
  2688  		}
  2689  	}
  2690  	for i := range desc.Mutations {
  2691  		m := &desc.Mutations[i]
  2692  		if c := m.GetColumn(); c != nil {
  2693  			if c.Name == string(name) {
  2694  				return c, m.Direction == DescriptorMutation_DROP, nil
  2695  			}
  2696  		}
  2697  	}
  2698  	return nil, false, NewUndefinedColumnError(string(name))
  2699  }
  2700  
  2701  // FindActiveOrNewColumnByName finds the column with the specified name.
  2702  // It returns either an active column or a column that was added in the
  2703  // same transaction that is currently running.
  2704  func (desc *MutableTableDescriptor) FindActiveOrNewColumnByName(
  2705  	name tree.Name,
  2706  ) (*ColumnDescriptor, error) {
  2707  	for i := range desc.Columns {
  2708  		c := &desc.Columns[i]
  2709  		if c.Name == string(name) {
  2710  			return c, nil
  2711  		}
  2712  	}
  2713  	currentMutationID := desc.ClusterVersion.NextMutationID
  2714  	for i := range desc.Mutations {
  2715  		mut := &desc.Mutations[i]
  2716  		if col := mut.GetColumn(); col != nil &&
  2717  			mut.MutationID == currentMutationID &&
  2718  			mut.Direction == DescriptorMutation_ADD {
  2719  			return col, nil
  2720  		}
  2721  	}
  2722  	return nil, NewUndefinedColumnError(string(name))
  2723  }
  2724  
  2725  // FindColumnMutationByName finds the mutation on the specified column.
  2726  func (desc *TableDescriptor) FindColumnMutationByName(name tree.Name) *DescriptorMutation {
  2727  	for i := range desc.Mutations {
  2728  		m := &desc.Mutations[i]
  2729  		if c := m.GetColumn(); c != nil {
  2730  			if c.Name == string(name) {
  2731  				return m
  2732  			}
  2733  		}
  2734  	}
  2735  	return nil
  2736  }
  2737  
  2738  // ColumnIdxMap returns a map from Column ID to the ordinal position of that
  2739  // column.
  2740  func (desc *TableDescriptor) ColumnIdxMap() map[ColumnID]int {
  2741  	return desc.ColumnIdxMapWithMutations(false)
  2742  }
  2743  
  2744  // ColumnIdxMapWithMutations returns a map from Column ID to the ordinal
  2745  // position of that column, optionally including mutation columns if the input
  2746  // bool is true.
  2747  func (desc *TableDescriptor) ColumnIdxMapWithMutations(mutations bool) map[ColumnID]int {
  2748  	colIdxMap := make(map[ColumnID]int, len(desc.Columns))
  2749  	for i := range desc.Columns {
  2750  		id := desc.Columns[i].ID
  2751  		colIdxMap[id] = i
  2752  	}
  2753  	if mutations {
  2754  		idx := len(desc.Columns)
  2755  		for i := range desc.Mutations {
  2756  			col := desc.Mutations[i].GetColumn()
  2757  			if col != nil {
  2758  				colIdxMap[col.ID] = idx
  2759  				idx++
  2760  			}
  2761  		}
  2762  	}
  2763  	return colIdxMap
  2764  }
  2765  
  2766  // FindActiveColumnByName finds an active column with the specified name.
  2767  func (desc *TableDescriptor) FindActiveColumnByName(name string) (*ColumnDescriptor, error) {
  2768  	for i := range desc.Columns {
  2769  		c := &desc.Columns[i]
  2770  		if c.Name == name {
  2771  			return c, nil
  2772  		}
  2773  	}
  2774  	return nil, NewUndefinedColumnError(name)
  2775  }
  2776  
  2777  // FindColumnByID finds the column with specified ID.
  2778  func (desc *TableDescriptor) FindColumnByID(id ColumnID) (*ColumnDescriptor, error) {
  2779  	for i := range desc.Columns {
  2780  		c := &desc.Columns[i]
  2781  		if c.ID == id {
  2782  			return c, nil
  2783  		}
  2784  	}
  2785  	for i := range desc.Mutations {
  2786  		if c := desc.Mutations[i].GetColumn(); c != nil {
  2787  			if c.ID == id {
  2788  				return c, nil
  2789  			}
  2790  		}
  2791  	}
  2792  	return nil, fmt.Errorf("column-id \"%d\" does not exist", id)
  2793  }
  2794  
  2795  // FindActiveColumnByID finds the active column with specified ID.
  2796  func (desc *TableDescriptor) FindActiveColumnByID(id ColumnID) (*ColumnDescriptor, error) {
  2797  	for i := range desc.Columns {
  2798  		c := &desc.Columns[i]
  2799  		if c.ID == id {
  2800  			return c, nil
  2801  		}
  2802  	}
  2803  	return nil, fmt.Errorf("column-id \"%d\" does not exist", id)
  2804  }
  2805  
  2806  // FindReadableColumnByID finds the readable column with specified ID. The
  2807  // column may be undergoing a schema change and is marked nullable regardless
  2808  // of its configuration. It returns true if the column is undergoing a
  2809  // schema change.
  2810  func (desc *ImmutableTableDescriptor) FindReadableColumnByID(
  2811  	id ColumnID,
  2812  ) (*ColumnDescriptor, bool, error) {
  2813  	for i := range desc.ReadableColumns {
  2814  		c := &desc.ReadableColumns[i]
  2815  		if c.ID == id {
  2816  			return c, i >= len(desc.Columns), nil
  2817  		}
  2818  	}
  2819  	return nil, false, fmt.Errorf("column-id \"%d\" does not exist", id)
  2820  }
  2821  
  2822  // FindFamilyByID finds the family with specified ID.
  2823  func (desc *TableDescriptor) FindFamilyByID(id FamilyID) (*ColumnFamilyDescriptor, error) {
  2824  	for i := range desc.Families {
  2825  		family := &desc.Families[i]
  2826  		if family.ID == id {
  2827  			return family, nil
  2828  		}
  2829  	}
  2830  	return nil, fmt.Errorf("family-id \"%d\" does not exist", id)
  2831  }
  2832  
  2833  // FindIndexByName finds the index with the specified name in the active
  2834  // list or the mutations list. It returns true if the index is being dropped.
  2835  func (desc *TableDescriptor) FindIndexByName(name string) (*IndexDescriptor, bool, error) {
  2836  	if desc.IsPhysicalTable() && desc.PrimaryIndex.Name == name {
  2837  		return &desc.PrimaryIndex, false, nil
  2838  	}
  2839  	for i := range desc.Indexes {
  2840  		idx := &desc.Indexes[i]
  2841  		if idx.Name == name {
  2842  			return idx, false, nil
  2843  		}
  2844  	}
  2845  	for _, m := range desc.Mutations {
  2846  		if idx := m.GetIndex(); idx != nil {
  2847  			if idx.Name == name {
  2848  				return idx, m.Direction == DescriptorMutation_DROP, nil
  2849  			}
  2850  		}
  2851  	}
  2852  	return nil, false, fmt.Errorf("index %q does not exist", name)
  2853  }
  2854  
  2855  // FindCheckByName finds the check constraint with the specified name.
  2856  func (desc *TableDescriptor) FindCheckByName(
  2857  	name string,
  2858  ) (*TableDescriptor_CheckConstraint, error) {
  2859  	for _, c := range desc.Checks {
  2860  		if c.Name == name {
  2861  			return c, nil
  2862  		}
  2863  	}
  2864  	return nil, fmt.Errorf("check %q does not exist", name)
  2865  }
  2866  
  2867  // NamesForColumnIDs returns the names for the given column ids, or an error
  2868  // if one or more column ids was missing. Note - this allocates! It's not for
  2869  // hot path code.
  2870  func (desc *TableDescriptor) NamesForColumnIDs(ids ColumnIDs) ([]string, error) {
  2871  	names := make([]string, len(ids))
  2872  	for i, id := range ids {
  2873  		col, err := desc.FindColumnByID(id)
  2874  		if err != nil {
  2875  			return nil, err
  2876  		}
  2877  		names[i] = col.Name
  2878  	}
  2879  	return names, nil
  2880  }
  2881  
  2882  // RenameIndexDescriptor renames an index descriptor.
  2883  func (desc *MutableTableDescriptor) RenameIndexDescriptor(
  2884  	index *IndexDescriptor, name string,
  2885  ) error {
  2886  	id := index.ID
  2887  	if id == desc.PrimaryIndex.ID {
  2888  		desc.PrimaryIndex.Name = name
  2889  		return nil
  2890  	}
  2891  	for i := range desc.Indexes {
  2892  		if desc.Indexes[i].ID == id {
  2893  			desc.Indexes[i].Name = name
  2894  			return nil
  2895  		}
  2896  	}
  2897  	for _, m := range desc.Mutations {
  2898  		if idx := m.GetIndex(); idx != nil && idx.ID == id {
  2899  			idx.Name = name
  2900  			return nil
  2901  		}
  2902  	}
  2903  	return fmt.Errorf("index with id = %d does not exist", id)
  2904  }
  2905  
  2906  // DropConstraint drops a constraint, either by removing it from the table
  2907  // descriptor or by queuing a mutation for a schema change.
  2908  func (desc *MutableTableDescriptor) DropConstraint(
  2909  	ctx context.Context,
  2910  	name string,
  2911  	detail ConstraintDetail,
  2912  	removeFK func(*MutableTableDescriptor, *ForeignKeyConstraint) error,
  2913  	settings *cluster.Settings,
  2914  ) error {
  2915  	switch detail.Kind {
  2916  	case ConstraintTypePK:
  2917  		desc.PrimaryIndex.Disabled = true
  2918  		return nil
  2919  
  2920  	case ConstraintTypeUnique:
  2921  		return unimplemented.NewWithIssueDetailf(42840, "drop-constraint-unique",
  2922  			"cannot drop UNIQUE constraint %q using ALTER TABLE DROP CONSTRAINT, use DROP INDEX CASCADE instead",
  2923  			tree.ErrNameStringP(&detail.Index.Name))
  2924  
  2925  	case ConstraintTypeCheck:
  2926  		if detail.CheckConstraint.Validity == ConstraintValidity_Validating {
  2927  			return unimplemented.NewWithIssueDetailf(42844, "drop-constraint-check-mutation",
  2928  				"constraint %q in the middle of being added, try again later", name)
  2929  		}
  2930  		if detail.CheckConstraint.Validity == ConstraintValidity_Dropping {
  2931  			return unimplemented.NewWithIssueDetailf(42844, "drop-constraint-check-mutation",
  2932  				"constraint %q in the middle of being dropped", name)
  2933  		}
  2934  		for i, c := range desc.Checks {
  2935  			if c.Name == name {
  2936  				// If the constraint is unvalidated, there's no assumption that it must
  2937  				// hold for all rows, so it can be dropped immediately.
  2938  				// We also drop the constraint immediately instead of queuing a mutation
  2939  				// unless the cluster is fully upgraded to 19.2, for backward
  2940  				// compatibility.
  2941  				if detail.CheckConstraint.Validity == ConstraintValidity_Unvalidated {
  2942  					desc.Checks = append(desc.Checks[:i], desc.Checks[i+1:]...)
  2943  					return nil
  2944  				}
  2945  				c.Validity = ConstraintValidity_Dropping
  2946  				desc.AddCheckMutation(c, DescriptorMutation_DROP)
  2947  				return nil
  2948  			}
  2949  		}
  2950  		return errors.Errorf("constraint %q not found on table %q", name, desc.Name)
  2951  
  2952  	case ConstraintTypeFK:
  2953  		if detail.FK.Validity == ConstraintValidity_Validating {
  2954  			return unimplemented.NewWithIssueDetailf(42844,
  2955  				"drop-constraint-fk-validating",
  2956  				"constraint %q in the middle of being added, try again later", name)
  2957  		}
  2958  		if detail.FK.Validity == ConstraintValidity_Dropping {
  2959  			return unimplemented.NewWithIssueDetailf(42844,
  2960  				"drop-constraint-fk-mutation",
  2961  				"constraint %q in the middle of being dropped", name)
  2962  		}
  2963  		// Search through the descriptor's foreign key constraints and delete the
  2964  		// one that we're supposed to be deleting.
  2965  		for i := range desc.OutboundFKs {
  2966  			ref := &desc.OutboundFKs[i]
  2967  			if ref.Name == name {
  2968  				// If the constraint is unvalidated, there's no assumption that it must
  2969  				// hold for all rows, so it can be dropped immediately.
  2970  				if detail.FK.Validity == ConstraintValidity_Unvalidated {
  2971  					// Remove the backreference.
  2972  					if err := removeFK(desc, detail.FK); err != nil {
  2973  						return err
  2974  					}
  2975  					desc.OutboundFKs = append(desc.OutboundFKs[:i], desc.OutboundFKs[i+1:]...)
  2976  					return nil
  2977  				}
  2978  				ref.Validity = ConstraintValidity_Dropping
  2979  				desc.AddForeignKeyMutation(ref, DescriptorMutation_DROP)
  2980  				return nil
  2981  			}
  2982  		}
  2983  		return errors.AssertionFailedf("constraint %q not found on table %q", name, desc.Name)
  2984  
  2985  	default:
  2986  		return unimplemented.Newf(fmt.Sprintf("drop-constraint-%s", detail.Kind),
  2987  			"constraint %q has unsupported type", tree.ErrNameString(name))
  2988  	}
  2989  
  2990  }
  2991  
  2992  // RenameConstraint renames a constraint.
  2993  func (desc *MutableTableDescriptor) RenameConstraint(
  2994  	detail ConstraintDetail,
  2995  	oldName, newName string,
  2996  	dependentViewRenameError func(string, ID) error,
  2997  	renameFK func(*MutableTableDescriptor, *ForeignKeyConstraint, string) error,
  2998  ) error {
  2999  	switch detail.Kind {
  3000  	case ConstraintTypePK, ConstraintTypeUnique:
  3001  		for _, tableRef := range desc.DependedOnBy {
  3002  			if tableRef.IndexID != detail.Index.ID {
  3003  				continue
  3004  			}
  3005  			return dependentViewRenameError("index", tableRef.ID)
  3006  		}
  3007  		return desc.RenameIndexDescriptor(detail.Index, newName)
  3008  
  3009  	case ConstraintTypeFK:
  3010  		if detail.FK.Validity == ConstraintValidity_Validating {
  3011  			return unimplemented.NewWithIssueDetailf(42844,
  3012  				"rename-constraint-fk-mutation",
  3013  				"constraint %q in the middle of being added, try again later",
  3014  				tree.ErrNameStringP(&detail.FK.Name))
  3015  		}
  3016  		// Update the name on the referenced table descriptor.
  3017  		if err := renameFK(desc, detail.FK, newName); err != nil {
  3018  			return err
  3019  		}
  3020  		// Update the name on this table descriptor.
  3021  		fk, err := desc.FindFKByName(detail.FK.Name)
  3022  		if err != nil {
  3023  			return err
  3024  		}
  3025  		fk.Name = newName
  3026  		return nil
  3027  
  3028  	case ConstraintTypeCheck:
  3029  		if detail.CheckConstraint.Validity == ConstraintValidity_Validating {
  3030  			return unimplemented.NewWithIssueDetailf(42844,
  3031  				"rename-constraint-check-mutation",
  3032  				"constraint %q in the middle of being added, try again later",
  3033  				tree.ErrNameStringP(&detail.CheckConstraint.Name))
  3034  		}
  3035  		detail.CheckConstraint.Name = newName
  3036  		return nil
  3037  
  3038  	default:
  3039  		return unimplemented.Newf(fmt.Sprintf("rename-constraint-%s", detail.Kind),
  3040  			"constraint %q has unsupported type", tree.ErrNameString(oldName))
  3041  	}
  3042  }
  3043  
  3044  // FindIndexByID finds an index (active or inactive) with the specified ID.
  3045  // Must return a pointer to the IndexDescriptor in the TableDescriptor, so that
  3046  // callers can use returned values to modify the TableDesc.
  3047  func (desc *TableDescriptor) FindIndexByID(id IndexID) (*IndexDescriptor, error) {
  3048  	if desc.PrimaryIndex.ID == id {
  3049  		return &desc.PrimaryIndex, nil
  3050  	}
  3051  	for i := range desc.Indexes {
  3052  		idx := &desc.Indexes[i]
  3053  		if idx.ID == id {
  3054  			return idx, nil
  3055  		}
  3056  	}
  3057  	for _, m := range desc.Mutations {
  3058  		if idx := m.GetIndex(); idx != nil && idx.ID == id {
  3059  			return idx, nil
  3060  		}
  3061  	}
  3062  	for _, m := range desc.GCMutations {
  3063  		if m.IndexID == id {
  3064  			return nil, ErrIndexGCMutationsList
  3065  		}
  3066  	}
  3067  	return nil, fmt.Errorf("index-id \"%d\" does not exist", id)
  3068  }
  3069  
  3070  // FindActiveIndexByID returns the index with the specified ID, or nil if it
  3071  // does not exist. It only searches active indexes.
  3072  func (desc *TableDescriptor) FindActiveIndexByID(id IndexID) *IndexDescriptor {
  3073  	if desc.PrimaryIndex.ID == id {
  3074  		return &desc.PrimaryIndex
  3075  	}
  3076  	for i := range desc.Indexes {
  3077  		idx := &desc.Indexes[i]
  3078  		if idx.ID == id {
  3079  			return idx
  3080  		}
  3081  	}
  3082  	return nil
  3083  }
  3084  
  3085  // FindIndexByIndexIdx returns an active index with the specified
  3086  // index's index which has a domain of [0, # of secondary indexes] and whether
  3087  // the index is a secondary index.
  3088  // The primary index has an index of 0 and the first secondary index
  3089  // (if it exists) has an index of 1.
  3090  func (desc *TableDescriptor) FindIndexByIndexIdx(
  3091  	indexIdx int,
  3092  ) (index *IndexDescriptor, isSecondary bool, err error) {
  3093  	// indexIdx is 0 for the primary index, or 1 to <num-indexes> for a
  3094  	// secondary index.
  3095  	if indexIdx < 0 || indexIdx > len(desc.Indexes) {
  3096  		return nil, false, errors.Errorf("invalid indexIdx %d", indexIdx)
  3097  	}
  3098  
  3099  	if indexIdx > 0 {
  3100  		return &desc.Indexes[indexIdx-1], true, nil
  3101  	}
  3102  
  3103  	return &desc.PrimaryIndex, false, nil
  3104  }
  3105  
  3106  // GetIndexMutationCapabilities returns:
  3107  // 1. Whether the index is a mutation
  3108  // 2. if so, is it in state DELETE_AND_WRITE_ONLY
  3109  func (desc *TableDescriptor) GetIndexMutationCapabilities(id IndexID) (bool, bool) {
  3110  	for _, mutation := range desc.Mutations {
  3111  		if mutationIndex := mutation.GetIndex(); mutationIndex != nil {
  3112  			if mutationIndex.ID == id {
  3113  				return true,
  3114  					mutation.State == DescriptorMutation_DELETE_AND_WRITE_ONLY
  3115  			}
  3116  		}
  3117  	}
  3118  	return false, false
  3119  }
  3120  
  3121  // FindFKByName returns the FK constraint on the table with the given name.
  3122  // Must return a pointer to the FK in the TableDescriptor, so that
  3123  // callers can use returned values to modify the TableDesc.
  3124  func (desc *TableDescriptor) FindFKByName(name string) (*ForeignKeyConstraint, error) {
  3125  	for i := range desc.OutboundFKs {
  3126  		fk := &desc.OutboundFKs[i]
  3127  		if fk.Name == name {
  3128  			return fk, nil
  3129  		}
  3130  	}
  3131  	return nil, fmt.Errorf("fk %q does not exist", name)
  3132  }
  3133  
  3134  // FindFKForBackRef searches the table descriptor for the foreign key constraint
  3135  // that matches the supplied backref, which is present on the supplied table id.
  3136  // Our current restriction that each column in a table can be on the referencing
  3137  // side of at most one FK relationship means that there's no possibility of
  3138  // ambiguity when finding the forward FK reference for a given backreference,
  3139  // since ambiguity would require an identical list of referencing columns. This
  3140  // would continue to hold even if we were to lift that restriction (see #38850), as long as
  3141  // it were still prohibited to create multiple foreign key relationships with
  3142  // exactly identical lists of referenced and referencing columns, which we think
  3143  // is a reasonable restriction (even though Postgres does allow doing this).
  3144  func (desc *TableDescriptor) FindFKForBackRef(
  3145  	referencedTableID ID, backref *ForeignKeyConstraint,
  3146  ) (*ForeignKeyConstraint, error) {
  3147  	for i := range desc.OutboundFKs {
  3148  		fk := &desc.OutboundFKs[i]
  3149  		if fk.ReferencedTableID == referencedTableID && fk.Name == backref.Name {
  3150  			return fk, nil
  3151  		}
  3152  	}
  3153  	return nil, errors.AssertionFailedf("could not find fk for backref %v", backref)
  3154  }
  3155  
  3156  // IsInterleaved returns true if any part of this this table is interleaved with
  3157  // another table's data.
  3158  func (desc *TableDescriptor) IsInterleaved() bool {
  3159  	for _, index := range desc.AllNonDropIndexes() {
  3160  		if index.IsInterleaved() {
  3161  			return true
  3162  		}
  3163  	}
  3164  	return false
  3165  }
  3166  
  3167  // IsPrimaryIndexDefaultRowID returns whether or not the table's primary
  3168  // index is the default primary key on the hidden rowid column.
  3169  func (desc *TableDescriptor) IsPrimaryIndexDefaultRowID() bool {
  3170  	if len(desc.PrimaryIndex.ColumnIDs) != 1 {
  3171  		return false
  3172  	}
  3173  	col, err := desc.FindColumnByID(desc.PrimaryIndex.ColumnIDs[0])
  3174  	if err != nil {
  3175  		// Should never be in this case.
  3176  		panic(err)
  3177  	}
  3178  	return col.Hidden
  3179  }
  3180  
  3181  // MakeMutationComplete updates the descriptor upon completion of a mutation.
  3182  // There are three Validity types for the mutations:
  3183  // Validated   - The constraint has already been added and validated, should
  3184  //               never be the case for a validated constraint to enter this
  3185  //               method.
  3186  // Validating  - The constraint has already been added, and just needs to be
  3187  //               marked as validated.
  3188  // Unvalidated - The constraint has not yet been added, and needs to be added
  3189  //               for the first time.
  3190  func (desc *MutableTableDescriptor) MakeMutationComplete(m DescriptorMutation) error {
  3191  	switch m.Direction {
  3192  	case DescriptorMutation_ADD:
  3193  		switch t := m.Descriptor_.(type) {
  3194  		case *DescriptorMutation_Column:
  3195  			desc.AddColumn(t.Column)
  3196  
  3197  		case *DescriptorMutation_Index:
  3198  			if err := desc.AddIndex(*t.Index, false); err != nil {
  3199  				return err
  3200  			}
  3201  
  3202  		case *DescriptorMutation_Constraint:
  3203  			switch t.Constraint.ConstraintType {
  3204  			case ConstraintToUpdate_CHECK:
  3205  				switch t.Constraint.Check.Validity {
  3206  				case ConstraintValidity_Validating:
  3207  					// Constraint already added, just mark it as Validated
  3208  					for _, c := range desc.Checks {
  3209  						if c.Name == t.Constraint.Name {
  3210  							c.Validity = ConstraintValidity_Validated
  3211  							break
  3212  						}
  3213  					}
  3214  				case ConstraintValidity_Unvalidated:
  3215  					// add the constraint to the list of check constraints on the table
  3216  					// descriptor
  3217  					desc.Checks = append(desc.Checks, &t.Constraint.Check)
  3218  				default:
  3219  					return errors.AssertionFailedf("invalid constraint validity state: %d", t.Constraint.Check.Validity)
  3220  				}
  3221  			case ConstraintToUpdate_FOREIGN_KEY:
  3222  				switch t.Constraint.ForeignKey.Validity {
  3223  				case ConstraintValidity_Validating:
  3224  					// Constraint already added, just mark it as Validated
  3225  					for i := range desc.OutboundFKs {
  3226  						fk := &desc.OutboundFKs[i]
  3227  						if fk.Name == t.Constraint.Name {
  3228  							fk.Validity = ConstraintValidity_Validated
  3229  							break
  3230  						}
  3231  					}
  3232  				case ConstraintValidity_Unvalidated:
  3233  					// Takes care of adding the Foreign Key to the table index. Adding the
  3234  					// backreference to the referenced table index must be taken care of
  3235  					// in another call.
  3236  					// TODO (tyler): Combine both of these tasks in the same place.
  3237  					desc.OutboundFKs = append(desc.OutboundFKs, t.Constraint.ForeignKey)
  3238  				}
  3239  			case ConstraintToUpdate_NOT_NULL:
  3240  				// Remove the dummy check constraint that was in place during validation
  3241  				for i, c := range desc.Checks {
  3242  					if c.Name == t.Constraint.Check.Name {
  3243  						desc.Checks = append(desc.Checks[:i], desc.Checks[i+1:]...)
  3244  					}
  3245  				}
  3246  				col, err := desc.FindColumnByID(t.Constraint.NotNullColumn)
  3247  				if err != nil {
  3248  					return err
  3249  				}
  3250  				col.Nullable = false
  3251  			default:
  3252  				return errors.Errorf("unsupported constraint type: %d", t.Constraint.ConstraintType)
  3253  			}
  3254  		case *DescriptorMutation_PrimaryKeySwap:
  3255  			args := t.PrimaryKeySwap
  3256  			getIndexIdxByID := func(id IndexID) (int, error) {
  3257  				for i, idx := range desc.Indexes {
  3258  					if idx.ID == id {
  3259  						return i, nil
  3260  					}
  3261  				}
  3262  				return 0, errors.New("index was not in list of indexes")
  3263  			}
  3264  
  3265  			// Update the old primary index's descriptor to denote that it uses the primary
  3266  			// index encoding and stores all columns. This ensures that it will be properly
  3267  			// encoded and decoded when it is accessed after it is no longer the primary key
  3268  			// but before it is dropped entirely during the index drop process.
  3269  			primaryIndexCopy := protoutil.Clone(&desc.PrimaryIndex).(*IndexDescriptor)
  3270  			primaryIndexCopy.EncodingType = PrimaryIndexEncoding
  3271  			for _, col := range desc.Columns {
  3272  				containsCol := false
  3273  				for _, colID := range primaryIndexCopy.ColumnIDs {
  3274  					if colID == col.ID {
  3275  						containsCol = true
  3276  						break
  3277  					}
  3278  				}
  3279  				if !containsCol {
  3280  					primaryIndexCopy.StoreColumnIDs = append(primaryIndexCopy.StoreColumnIDs, col.ID)
  3281  					primaryIndexCopy.StoreColumnNames = append(primaryIndexCopy.StoreColumnNames, col.Name)
  3282  				}
  3283  			}
  3284  			// Move the old primary index from the table descriptor into the mutations queue
  3285  			// to schedule it for deletion.
  3286  			if err := desc.AddIndexMutation(primaryIndexCopy, DescriptorMutation_DROP); err != nil {
  3287  				return err
  3288  			}
  3289  
  3290  			// Promote the new primary index into the primary index position on the descriptor,
  3291  			// and remove it from the secondary indexes list.
  3292  			newIndex, err := desc.FindIndexByID(args.NewPrimaryIndexId)
  3293  			if err != nil {
  3294  				return err
  3295  			}
  3296  			newIndex.Name = "primary"
  3297  			desc.PrimaryIndex = *protoutil.Clone(newIndex).(*IndexDescriptor)
  3298  			// The primary index "implicitly" stores all columns in the table.
  3299  			// Explicitly including them in the stored columns list is incorrect.
  3300  			desc.PrimaryIndex.StoreColumnNames, desc.PrimaryIndex.StoreColumnIDs = nil, nil
  3301  			idx, err := getIndexIdxByID(newIndex.ID)
  3302  			if err != nil {
  3303  				return err
  3304  			}
  3305  			desc.Indexes = append(desc.Indexes[:idx], desc.Indexes[idx+1:]...)
  3306  
  3307  			// Swap out the old indexes with their rewritten versions.
  3308  			for j := range args.OldIndexes {
  3309  				oldID := args.OldIndexes[j]
  3310  				newID := args.NewIndexes[j]
  3311  				// All our new indexes have been inserted into the table descriptor by now, since the primary key swap
  3312  				// is the last mutation processed in a group of mutations under the same mutation ID.
  3313  				newIndex, err := desc.FindIndexByID(newID)
  3314  				if err != nil {
  3315  					return err
  3316  				}
  3317  				oldIndexIndex, err := getIndexIdxByID(oldID)
  3318  				if err != nil {
  3319  					return err
  3320  				}
  3321  				oldIndex := protoutil.Clone(&desc.Indexes[oldIndexIndex]).(*IndexDescriptor)
  3322  				newIndex.Name = oldIndex.Name
  3323  				// Splice out old index from the indexes list.
  3324  				desc.Indexes = append(desc.Indexes[:oldIndexIndex], desc.Indexes[oldIndexIndex+1:]...)
  3325  				// Add a drop mutation for the old index. The code that calls this function will schedule
  3326  				// a schema change job to pick up all of these index drop mutations.
  3327  				if err := desc.AddIndexMutation(oldIndex, DescriptorMutation_DROP); err != nil {
  3328  					return err
  3329  				}
  3330  			}
  3331  		case *DescriptorMutation_ComputedColumnSwap:
  3332  			if err := desc.performComputedColumnSwap(t.ComputedColumnSwap); err != nil {
  3333  				return err
  3334  			}
  3335  		}
  3336  
  3337  	case DescriptorMutation_DROP:
  3338  		switch t := m.Descriptor_.(type) {
  3339  		// Nothing else to be done. The column/index was already removed from the
  3340  		// set of column/index descriptors at mutation creation time.
  3341  		// Constraints to be dropped are dropped before column/index backfills.
  3342  		case *DescriptorMutation_Column:
  3343  			desc.RemoveColumnFromFamily(t.Column.ID)
  3344  		}
  3345  	}
  3346  	return nil
  3347  }
  3348  
  3349  func (desc *MutableTableDescriptor) performComputedColumnSwap(swap *ComputedColumnSwap) error {
  3350  	// Get the old and new columns from the descriptor.
  3351  	oldCol, err := desc.FindColumnByID(swap.OldColumnId)
  3352  	if err != nil {
  3353  		return err
  3354  	}
  3355  	newCol, err := desc.FindColumnByID(swap.NewColumnId)
  3356  	if err != nil {
  3357  		return err
  3358  	}
  3359  
  3360  	// Mark newCol as no longer a computed column.
  3361  	newCol.ComputeExpr = nil
  3362  
  3363  	// oldCol still needs to have values written to it in case nodes read it from
  3364  	// it with a TableDescriptor version from before the swap.
  3365  	// To achieve this, we make oldCol a computed column of newCol.
  3366  	oldColComputeExpr := tree.CastExpr{
  3367  		Expr:       &tree.ColumnItem{ColumnName: tree.Name(oldCol.Name)},
  3368  		Type:       oldCol.DatumType(),
  3369  		SyntaxMode: tree.CastShort,
  3370  	}
  3371  	s := tree.Serialize(&oldColComputeExpr)
  3372  	oldCol.ComputeExpr = &s
  3373  
  3374  	// Generate unique name for old column.
  3375  	nameExists := func(name string) bool {
  3376  		_, _, err := desc.FindColumnByName(tree.Name(name))
  3377  		return err == nil
  3378  	}
  3379  
  3380  	uniqueName := GenerateUniqueConstraintName(newCol.Name, nameExists)
  3381  
  3382  	// Remember the name of oldCol, because newCol will take it.
  3383  	oldColName := oldCol.Name
  3384  
  3385  	// Rename old column to this new name, and rename newCol to oldCol's name.
  3386  	desc.RenameColumnDescriptor(oldCol, uniqueName)
  3387  	desc.RenameColumnDescriptor(newCol, oldColName)
  3388  
  3389  	// Swap Column Family ordering for oldCol and newCol.
  3390  	// Both columns must be in the same family since the new column is
  3391  	// created explicitly with the same column family as the old column.
  3392  	// This preserves the ordering of column families when querying
  3393  	// for column families.
  3394  	oldColColumnFamily, err := desc.GetFamilyOfColumn(oldCol.ID)
  3395  	if err != nil {
  3396  		return err
  3397  	}
  3398  	newColColumnFamily, err := desc.GetFamilyOfColumn(newCol.ID)
  3399  	if err != nil {
  3400  		return err
  3401  	}
  3402  
  3403  	if oldColColumnFamily.ID != newColColumnFamily.ID {
  3404  		return errors.Newf("expected the column families of the old and new columns to match,"+
  3405  			"oldCol column family: %v, newCol column family: %v",
  3406  			oldColColumnFamily.ID, newColColumnFamily.ID)
  3407  	}
  3408  
  3409  	for i := range oldColColumnFamily.ColumnIDs {
  3410  		if oldColColumnFamily.ColumnIDs[i] == oldCol.ID {
  3411  			oldColColumnFamily.ColumnIDs[i] = newCol.ID
  3412  			oldColColumnFamily.ColumnNames[i] = newCol.Name
  3413  		} else if oldColColumnFamily.ColumnIDs[i] == newCol.ID {
  3414  			oldColColumnFamily.ColumnIDs[i] = oldCol.ID
  3415  			oldColColumnFamily.ColumnNames[i] = oldCol.Name
  3416  		}
  3417  	}
  3418  
  3419  	// Set newCol's LogicalColumnID to oldCol's ID. This makes
  3420  	// newCol display like oldCol in catalog tables.
  3421  	newCol.LogicalColumnID = oldCol.ID
  3422  	oldCol.LogicalColumnID = 0
  3423  
  3424  	// Mark oldCol as being the result of an AlterColumnType. This allows us
  3425  	// to generate better errors for failing inserts.
  3426  	oldCol.AlterColumnTypeInProgress = true
  3427  
  3428  	// Clone oldColDesc so that we can queue it up as a mutation.
  3429  	// Use oldColCopy to queue mutation in case oldCol's memory address
  3430  	// gets overwritten during mutation.
  3431  	oldColCopy := protoutil.Clone(oldCol).(*ColumnDescriptor)
  3432  	newColCopy := protoutil.Clone(newCol).(*ColumnDescriptor)
  3433  	desc.AddColumnMutation(oldColCopy, DescriptorMutation_DROP)
  3434  
  3435  	// Remove the new column from the TableDescriptor first so we can reinsert
  3436  	// it into the position where the old column is.
  3437  	for i := range desc.Columns {
  3438  		if desc.Columns[i].ID == newCol.ID {
  3439  			desc.Columns = append(desc.Columns[:i:i], desc.Columns[i+1:]...)
  3440  			break
  3441  		}
  3442  	}
  3443  
  3444  	// Replace the old column with the new column.
  3445  	for i := range desc.Columns {
  3446  		if desc.Columns[i].ID == oldCol.ID {
  3447  			desc.Columns[i] = *newColCopy
  3448  		}
  3449  	}
  3450  
  3451  	return nil
  3452  }
  3453  
  3454  // AddCheckMutation adds a check constraint mutation to desc.Mutations.
  3455  func (desc *MutableTableDescriptor) AddCheckMutation(
  3456  	ck *TableDescriptor_CheckConstraint, direction DescriptorMutation_Direction,
  3457  ) {
  3458  	m := DescriptorMutation{
  3459  		Descriptor_: &DescriptorMutation_Constraint{
  3460  			Constraint: &ConstraintToUpdate{
  3461  				ConstraintType: ConstraintToUpdate_CHECK, Name: ck.Name, Check: *ck,
  3462  			},
  3463  		},
  3464  		Direction: direction,
  3465  	}
  3466  	desc.addMutation(m)
  3467  }
  3468  
  3469  // AddForeignKeyMutation adds a foreign key constraint mutation to desc.Mutations.
  3470  func (desc *MutableTableDescriptor) AddForeignKeyMutation(
  3471  	fk *ForeignKeyConstraint, direction DescriptorMutation_Direction,
  3472  ) {
  3473  	m := DescriptorMutation{
  3474  		Descriptor_: &DescriptorMutation_Constraint{
  3475  			Constraint: &ConstraintToUpdate{
  3476  				ConstraintType: ConstraintToUpdate_FOREIGN_KEY,
  3477  				Name:           fk.Name,
  3478  				ForeignKey:     *fk,
  3479  			},
  3480  		},
  3481  		Direction: direction,
  3482  	}
  3483  	desc.addMutation(m)
  3484  }
  3485  
  3486  // MakeNotNullCheckConstraint creates a dummy check constraint equivalent to a
  3487  // NOT NULL constraint on a column, so that NOT NULL constraints can be added
  3488  // and dropped correctly in the schema changer. This function mutates inuseNames
  3489  // to add the new constraint name.
  3490  // TODO(mgartner): Move this to schemaexpr.CheckConstraintBuilder.
  3491  func MakeNotNullCheckConstraint(
  3492  	colName string, colID ColumnID, inuseNames map[string]struct{}, validity ConstraintValidity,
  3493  ) *TableDescriptor_CheckConstraint {
  3494  	name := fmt.Sprintf("%s_auto_not_null", colName)
  3495  	// If generated name isn't unique, attempt to add a number to the end to
  3496  	// get a unique name, as in generateNameForCheckConstraint().
  3497  	if _, ok := inuseNames[name]; ok {
  3498  		i := 1
  3499  		for {
  3500  			appended := fmt.Sprintf("%s%d", name, i)
  3501  			if _, ok := inuseNames[appended]; !ok {
  3502  				name = appended
  3503  				break
  3504  			}
  3505  			i++
  3506  		}
  3507  	}
  3508  	if inuseNames != nil {
  3509  		inuseNames[name] = struct{}{}
  3510  	}
  3511  
  3512  	expr := &tree.IsNotNullExpr{
  3513  		Expr: &tree.ColumnItem{ColumnName: tree.Name(colName)},
  3514  	}
  3515  
  3516  	return &TableDescriptor_CheckConstraint{
  3517  		Name:                name,
  3518  		Expr:                tree.Serialize(expr),
  3519  		Validity:            validity,
  3520  		ColumnIDs:           []ColumnID{colID},
  3521  		IsNonNullConstraint: true,
  3522  	}
  3523  }
  3524  
  3525  // AddNotNullMutation adds a not null constraint mutation to desc.Mutations.
  3526  // Similarly to other schema elements, adding or dropping a non-null
  3527  // constraint requires a multi-state schema change, including a bulk validation
  3528  // step, before the Nullable flag can be set to false on the descriptor. This is
  3529  // done by adding a dummy check constraint of the form "x IS NOT NULL" that is
  3530  // treated like other check constraints being added, until the completion of the
  3531  // schema change, at which the check constraint is deleted. This function
  3532  // mutates inuseNames to add the new constraint name.
  3533  func (desc *MutableTableDescriptor) AddNotNullMutation(
  3534  	ck *TableDescriptor_CheckConstraint, direction DescriptorMutation_Direction,
  3535  ) {
  3536  	m := DescriptorMutation{
  3537  		Descriptor_: &DescriptorMutation_Constraint{
  3538  			Constraint: &ConstraintToUpdate{
  3539  				ConstraintType: ConstraintToUpdate_NOT_NULL,
  3540  				Name:           ck.Name,
  3541  				NotNullColumn:  ck.ColumnIDs[0],
  3542  				Check:          *ck,
  3543  			},
  3544  		},
  3545  		Direction: direction,
  3546  	}
  3547  	desc.addMutation(m)
  3548  }
  3549  
  3550  // AddColumnMutation adds a column mutation to desc.Mutations. Callers must take
  3551  // care not to further mutate the column descriptor, since this method retains
  3552  // a pointer to it.
  3553  func (desc *MutableTableDescriptor) AddColumnMutation(
  3554  	c *ColumnDescriptor, direction DescriptorMutation_Direction,
  3555  ) {
  3556  	m := DescriptorMutation{Descriptor_: &DescriptorMutation_Column{Column: c}, Direction: direction}
  3557  	desc.addMutation(m)
  3558  }
  3559  
  3560  // AddIndexMutation adds an index mutation to desc.Mutations.
  3561  func (desc *MutableTableDescriptor) AddIndexMutation(
  3562  	idx *IndexDescriptor, direction DescriptorMutation_Direction,
  3563  ) error {
  3564  
  3565  	switch idx.Type {
  3566  	case IndexDescriptor_FORWARD:
  3567  		if err := checkColumnsValidForIndex(desc, idx.ColumnNames); err != nil {
  3568  			return err
  3569  		}
  3570  	case IndexDescriptor_INVERTED:
  3571  		if err := checkColumnsValidForInvertedIndex(desc, idx.ColumnNames); err != nil {
  3572  			return err
  3573  		}
  3574  	}
  3575  
  3576  	m := DescriptorMutation{Descriptor_: &DescriptorMutation_Index{Index: idx}, Direction: direction}
  3577  	desc.addMutation(m)
  3578  	return nil
  3579  }
  3580  
  3581  // AddPrimaryKeySwapMutation adds a PrimaryKeySwap mutation to the table descriptor.
  3582  func (desc *MutableTableDescriptor) AddPrimaryKeySwapMutation(swap *PrimaryKeySwap) {
  3583  	m := DescriptorMutation{Descriptor_: &DescriptorMutation_PrimaryKeySwap{PrimaryKeySwap: swap}, Direction: DescriptorMutation_ADD}
  3584  	desc.addMutation(m)
  3585  }
  3586  
  3587  // AddComputedColumnSwapMutation adds a ComputedColumnSwap mutation to the table descriptor.
  3588  func (desc *MutableTableDescriptor) AddComputedColumnSwapMutation(swap *ComputedColumnSwap) {
  3589  	m := DescriptorMutation{Descriptor_: &DescriptorMutation_ComputedColumnSwap{ComputedColumnSwap: swap}, Direction: DescriptorMutation_ADD}
  3590  	desc.addMutation(m)
  3591  }
  3592  
  3593  func (desc *MutableTableDescriptor) addMutation(m DescriptorMutation) {
  3594  	switch m.Direction {
  3595  	case DescriptorMutation_ADD:
  3596  		m.State = DescriptorMutation_DELETE_ONLY
  3597  
  3598  	case DescriptorMutation_DROP:
  3599  		m.State = DescriptorMutation_DELETE_AND_WRITE_ONLY
  3600  	}
  3601  	// For tables created in the same transaction the next mutation ID will
  3602  	// not have been allocated and the added mutation will use an invalid ID.
  3603  	// This is fine because the mutation will be processed immediately.
  3604  	m.MutationID = desc.ClusterVersion.NextMutationID
  3605  	desc.NextMutationID = desc.ClusterVersion.NextMutationID + 1
  3606  	desc.Mutations = append(desc.Mutations, m)
  3607  }
  3608  
  3609  // IgnoreConstraints is used in MakeFirstMutationPublic to indicate that the
  3610  // table descriptor returned should not include newly added constraints, which
  3611  // is useful when passing the returned table descriptor to be used in
  3612  // validating constraints to be added.
  3613  const IgnoreConstraints = false
  3614  
  3615  // IncludeConstraints is used in MakeFirstMutationPublic to indicate that the
  3616  // table descriptor returned should include newly added constraints.
  3617  const IncludeConstraints = true
  3618  
  3619  // MakeFirstMutationPublic creates a MutableTableDescriptor from the
  3620  // ImmutableTableDescriptor by making the first mutation public.
  3621  // This is super valuable when trying to run SQL over data associated
  3622  // with a schema mutation that is still not yet public: Data validation,
  3623  // error reporting.
  3624  func (desc *ImmutableTableDescriptor) MakeFirstMutationPublic(
  3625  	includeConstraints bool,
  3626  ) (*MutableTableDescriptor, error) {
  3627  	// Clone the ImmutableTable descriptor because we want to create an Immutable one.
  3628  	table := NewMutableExistingTableDescriptor(*protoutil.Clone(desc.TableDesc()).(*TableDescriptor))
  3629  	mutationID := desc.Mutations[0].MutationID
  3630  	i := 0
  3631  	for _, mutation := range desc.Mutations {
  3632  		if mutation.MutationID != mutationID {
  3633  			// Mutations are applied in a FIFO order. Only apply the first set
  3634  			// of mutations if they have the mutation ID we're looking for.
  3635  			break
  3636  		}
  3637  		if includeConstraints || mutation.GetConstraint() == nil {
  3638  			if err := table.MakeMutationComplete(mutation); err != nil {
  3639  				return nil, err
  3640  			}
  3641  		}
  3642  		i++
  3643  	}
  3644  	table.Mutations = table.Mutations[i:]
  3645  	table.Version++
  3646  	return table, nil
  3647  }
  3648  
  3649  // ColumnNeedsBackfill returns true if adding the given column requires a
  3650  // backfill (dropping a column always requires a backfill).
  3651  func ColumnNeedsBackfill(desc *ColumnDescriptor) bool {
  3652  	if desc.HasNullDefault() {
  3653  		return false
  3654  	}
  3655  	return desc.HasDefault() || !desc.Nullable || desc.IsComputed()
  3656  }
  3657  
  3658  // HasPrimaryKey returns true if the table has a primary key.
  3659  func (desc *TableDescriptor) HasPrimaryKey() bool {
  3660  	return !desc.PrimaryIndex.Disabled
  3661  }
  3662  
  3663  // HasColumnBackfillMutation returns whether the table has any queued column
  3664  // mutations that require a backfill.
  3665  func (desc *TableDescriptor) HasColumnBackfillMutation() bool {
  3666  	for _, m := range desc.Mutations {
  3667  		col := m.GetColumn()
  3668  		if col == nil {
  3669  			// Index backfills don't affect changefeeds.
  3670  			continue
  3671  		}
  3672  		// It's unfortunate that there's no one method we can call to check if a
  3673  		// mutation will be a backfill or not, but this logic was extracted from
  3674  		// backfill.go.
  3675  		if m.Direction == DescriptorMutation_DROP || ColumnNeedsBackfill(col) {
  3676  			return true
  3677  		}
  3678  	}
  3679  	return false
  3680  }
  3681  
  3682  // GoingOffline returns true if the table is being dropped or is importing.
  3683  func (desc *TableDescriptor) GoingOffline() bool {
  3684  	return desc.Dropped() || desc.State == TableDescriptor_OFFLINE
  3685  }
  3686  
  3687  // Dropped returns true if the table is being dropped.
  3688  func (desc *TableDescriptor) Dropped() bool {
  3689  	return desc.State == TableDescriptor_DROP
  3690  }
  3691  
  3692  // Adding returns true if the table is being added.
  3693  func (desc *TableDescriptor) Adding() bool {
  3694  	return desc.State == TableDescriptor_ADD
  3695  }
  3696  
  3697  // IsNewTable returns true if the table was created in the current
  3698  // transaction.
  3699  func (desc *MutableTableDescriptor) IsNewTable() bool {
  3700  	return desc.ClusterVersion.ID == InvalidID
  3701  }
  3702  
  3703  // HasDrainingNames returns true if a draining name exists.
  3704  func (desc *TableDescriptor) HasDrainingNames() bool {
  3705  	return len(desc.DrainingNames) > 0
  3706  }
  3707  
  3708  // VisibleColumns returns all non hidden columns.
  3709  func (desc *TableDescriptor) VisibleColumns() []ColumnDescriptor {
  3710  	var cols []ColumnDescriptor
  3711  	for i := range desc.Columns {
  3712  		col := &desc.Columns[i]
  3713  		if !col.Hidden {
  3714  			cols = append(cols, *col)
  3715  		}
  3716  	}
  3717  	return cols
  3718  }
  3719  
  3720  // ColumnTypes returns the types of all columns.
  3721  func (desc *TableDescriptor) ColumnTypes() []*types.T {
  3722  	return desc.ColumnTypesWithMutations(false)
  3723  }
  3724  
  3725  // ColumnsWithMutations returns all column descriptors, optionally including
  3726  // mutation columns.
  3727  func (desc *TableDescriptor) ColumnsWithMutations(mutations bool) []ColumnDescriptor {
  3728  	n := len(desc.Columns)
  3729  	columns := desc.Columns[:n:n] // immutable on append
  3730  	if mutations {
  3731  		for i := range desc.Mutations {
  3732  			if col := desc.Mutations[i].GetColumn(); col != nil {
  3733  				columns = append(columns, *col)
  3734  			}
  3735  		}
  3736  	}
  3737  	return columns
  3738  }
  3739  
  3740  // ColumnTypesWithMutations returns the types of all columns, optionally
  3741  // including mutation columns, which will be returned if the input bool is true.
  3742  func (desc *TableDescriptor) ColumnTypesWithMutations(mutations bool) []*types.T {
  3743  	columns := desc.ColumnsWithMutations(mutations)
  3744  	types := make([]*types.T, len(columns))
  3745  	for i := range columns {
  3746  		types[i] = columns[i].Type
  3747  	}
  3748  	return types
  3749  }
  3750  
  3751  // ColumnsSelectors generates Select expressions for cols.
  3752  func ColumnsSelectors(cols []ColumnDescriptor) tree.SelectExprs {
  3753  	exprs := make(tree.SelectExprs, len(cols))
  3754  	colItems := make([]tree.ColumnItem, len(cols))
  3755  	for i, col := range cols {
  3756  		colItems[i].ColumnName = tree.Name(col.Name)
  3757  		exprs[i].Expr = &colItems[i]
  3758  	}
  3759  	return exprs
  3760  }
  3761  
  3762  // SetID implements the DescriptorProto interface.
  3763  func (desc *DatabaseDescriptor) SetID(id ID) {
  3764  	desc.ID = id
  3765  }
  3766  
  3767  // TypeName returns the plain type of this descriptor.
  3768  func (desc *DatabaseDescriptor) TypeName() string {
  3769  	return "database"
  3770  }
  3771  
  3772  // SetName implements the DescriptorProto interface.
  3773  func (desc *DatabaseDescriptor) SetName(name string) {
  3774  	desc.Name = name
  3775  }
  3776  
  3777  // DatabaseDesc implements the ObjectDescriptor interface.
  3778  func (desc *DatabaseDescriptor) DatabaseDesc() *DatabaseDescriptor {
  3779  	return desc
  3780  }
  3781  
  3782  // SchemaDesc implements the ObjectDescriptor interface.
  3783  func (desc *DatabaseDescriptor) SchemaDesc() *SchemaDescriptor {
  3784  	return nil
  3785  }
  3786  
  3787  // TableDesc implements the ObjectDescriptor interface.
  3788  func (desc *DatabaseDescriptor) TableDesc() *TableDescriptor {
  3789  	return nil
  3790  }
  3791  
  3792  // TypeDesc implements the ObjectDescriptor interface.
  3793  func (desc *DatabaseDescriptor) TypeDesc() *TypeDescriptor {
  3794  	return nil
  3795  }
  3796  
  3797  // NameResolutionResult implements the ObjectDescriptor interface.
  3798  func (desc *DatabaseDescriptor) NameResolutionResult() {}
  3799  
  3800  // Validate validates that the database descriptor is well formed.
  3801  // Checks include validate the database name, and verifying that there
  3802  // is at least one read and write user.
  3803  func (desc *DatabaseDescriptor) Validate() error {
  3804  	if err := validateName(desc.Name, "descriptor"); err != nil {
  3805  		return err
  3806  	}
  3807  	if desc.ID == 0 {
  3808  		return fmt.Errorf("invalid database ID %d", desc.ID)
  3809  	}
  3810  
  3811  	// Fill in any incorrect privileges that may have been missed due to mixed-versions.
  3812  	// TODO(mberhault): remove this in 2.1 (maybe 2.2) when privilege-fixing migrations have been
  3813  	// run again and mixed-version clusters always write "good" descriptors.
  3814  	desc.Privileges.MaybeFixPrivileges(desc.GetID())
  3815  
  3816  	// Validate the privilege descriptor.
  3817  	return desc.Privileges.Validate(desc.GetID())
  3818  }
  3819  
  3820  // GetID returns the ID of the descriptor.
  3821  func (desc *Descriptor) GetID() ID {
  3822  	switch t := desc.Union.(type) {
  3823  	case *Descriptor_Table:
  3824  		return t.Table.ID
  3825  	case *Descriptor_Database:
  3826  		return t.Database.ID
  3827  	case *Descriptor_Type:
  3828  		return t.Type.ID
  3829  	default:
  3830  		return 0
  3831  	}
  3832  }
  3833  
  3834  // GetName returns the Name of the descriptor.
  3835  func (desc *Descriptor) GetName() string {
  3836  	switch t := desc.Union.(type) {
  3837  	case *Descriptor_Table:
  3838  		return t.Table.Name
  3839  	case *Descriptor_Database:
  3840  		return t.Database.Name
  3841  	case *Descriptor_Type:
  3842  		return t.Type.Name
  3843  	default:
  3844  		return ""
  3845  	}
  3846  }
  3847  
  3848  // Table is a replacement for GetTable() which seeks to ensure that clients
  3849  // which unmarshal Descriptor structs properly set the ModificationTime on
  3850  // tables based on the MVCC timestamp at which the descriptor was read.
  3851  //
  3852  // A linter should ensure that GetTable() is not called.
  3853  func (desc *Descriptor) Table(ts hlc.Timestamp) *TableDescriptor {
  3854  	t := desc.GetTable()
  3855  	if t != nil {
  3856  		t.maybeSetTimeFromMVCCTimestamp(ts)
  3857  	}
  3858  	return t
  3859  }
  3860  
  3861  // maybeSetTimeFromMVCCTimestamp will update ModificationTime and possible
  3862  // CreateAsOfTime with the provided timestamp. If desc.ModificationTime is
  3863  // non-zero it must be the case that it is not after the provided timestamp.
  3864  //
  3865  // When table descriptor versions are incremented they are written with a
  3866  // zero-valued ModificationTime. This is done to avoid the need to observe
  3867  // the commit timestamp for the writing transaction which would prevent
  3868  // pushes. This method is used in the read path to set the modification time
  3869  // based on the MVCC timestamp of row which contained this descriptor. If
  3870  // the ModificationTime is non-zero then we know that either this table
  3871  // descriptor was written by older version of cockroach which included the
  3872  // exact commit timestamp or it was re-written in which case it will include
  3873  // a timestamp which was set by this method.
  3874  //
  3875  // It is vital that users which read table descriptor values from the KV store
  3876  // call this method.
  3877  func (desc *TableDescriptor) maybeSetTimeFromMVCCTimestamp(ts hlc.Timestamp) {
  3878  	// CreateAsOfTime is used for CREATE TABLE ... AS ... and was introduced in
  3879  	// v19.1. In general it is not critical to set except for tables in the ADD
  3880  	// ADD state which were created from CTAS so we should not assert on its not
  3881  	// being set. It's not always sensical to set it from the passed MVCC
  3882  	// timestamp. However, starting in 19.2 the CreateAsOfTime and
  3883  	// ModificationTime fields are both unset for the first Version of a
  3884  	// TableDescriptor and the code relies on the value being set based on the
  3885  	// MVCC timestamp.
  3886  	if !ts.IsEmpty() &&
  3887  		desc.ModificationTime.IsEmpty() &&
  3888  		desc.CreateAsOfTime.IsEmpty() &&
  3889  		desc.Version == 1 {
  3890  		desc.CreateAsOfTime = ts
  3891  	}
  3892  
  3893  	// Ensure that if the table is in the process of being added and relies on
  3894  	// CreateAsOfTime that it is now set.
  3895  	if desc.Adding() && desc.IsAs() && desc.CreateAsOfTime.IsEmpty() {
  3896  		log.Fatalf(context.TODO(), "table descriptor for %q (%d.%d) is in the "+
  3897  			"ADD state and was created with CREATE TABLE ... AS but does not have a "+
  3898  			"CreateAsOfTime set", desc.Name, desc.ParentID, desc.ID)
  3899  	}
  3900  
  3901  	// Set the ModificationTime based on the passed ts if we should.
  3902  	// Table descriptors can be updated in place after their version has been
  3903  	// incremented (e.g. to include a schema change lease).
  3904  	// When this happens we permit the ModificationTime to be written explicitly
  3905  	// with the value that lives on the in-memory copy. That value should contain
  3906  	// a timestamp set by this method. Thus if the ModificationTime is set it
  3907  	// must not be after the MVCC timestamp we just read it at.
  3908  	if desc.ModificationTime.IsEmpty() && ts.IsEmpty() {
  3909  		log.Fatalf(context.TODO(), "read table descriptor for %q (%d.%d) without ModificationTime "+
  3910  			"with zero MVCC timestamp", desc.Name, desc.ParentID, desc.ID)
  3911  	} else if desc.ModificationTime.IsEmpty() {
  3912  		desc.ModificationTime = ts
  3913  	} else if !ts.IsEmpty() && ts.Less(desc.ModificationTime) {
  3914  		log.Fatalf(context.TODO(), "read table descriptor %q (%d.%d) which has a ModificationTime "+
  3915  			"after its MVCC timestamp: has %v, expected %v",
  3916  			desc.Name, desc.ParentID, desc.ID, desc.ModificationTime, ts)
  3917  	}
  3918  }
  3919  
  3920  // IsSet returns whether or not the foreign key actually references a table.
  3921  func (f ForeignKeyReference) IsSet() bool {
  3922  	return f.Table != 0
  3923  }
  3924  
  3925  // InvalidateFKConstraints sets all FK constraints to un-validated.
  3926  func (desc *TableDescriptor) InvalidateFKConstraints() {
  3927  	// We don't use GetConstraintInfo because we want to edit the passed desc.
  3928  	for i := range desc.OutboundFKs {
  3929  		fk := &desc.OutboundFKs[i]
  3930  		fk.Validity = ConstraintValidity_Unvalidated
  3931  	}
  3932  }
  3933  
  3934  // AllIndexSpans returns the Spans for each index in the table, including those
  3935  // being added in the mutations.
  3936  func (desc *TableDescriptor) AllIndexSpans(codec keys.SQLCodec) roachpb.Spans {
  3937  	var spans roachpb.Spans
  3938  	err := desc.ForeachNonDropIndex(func(index *IndexDescriptor) error {
  3939  		spans = append(spans, desc.IndexSpan(codec, index.ID))
  3940  		return nil
  3941  	})
  3942  	if err != nil {
  3943  		panic(err)
  3944  	}
  3945  	return spans
  3946  }
  3947  
  3948  // PrimaryIndexSpan returns the Span that corresponds to the entire primary
  3949  // index; can be used for a full table scan.
  3950  func (desc *TableDescriptor) PrimaryIndexSpan(codec keys.SQLCodec) roachpb.Span {
  3951  	return desc.IndexSpan(codec, desc.PrimaryIndex.ID)
  3952  }
  3953  
  3954  // IndexSpan returns the Span that corresponds to an entire index; can be used
  3955  // for a full index scan.
  3956  func (desc *TableDescriptor) IndexSpan(codec keys.SQLCodec, indexID IndexID) roachpb.Span {
  3957  	prefix := roachpb.Key(MakeIndexKeyPrefix(codec, desc, indexID))
  3958  	return roachpb.Span{Key: prefix, EndKey: prefix.PrefixEnd()}
  3959  }
  3960  
  3961  // TableSpan returns the Span that corresponds to the entire table.
  3962  func (desc *TableDescriptor) TableSpan(codec keys.SQLCodec) roachpb.Span {
  3963  	// TODO(jordan): Why does IndexSpan consider interleaves but TableSpan does
  3964  	// not? Should it?
  3965  	prefix := codec.TablePrefix(uint32(desc.ID))
  3966  	return roachpb.Span{Key: prefix, EndKey: prefix.PrefixEnd()}
  3967  }
  3968  
  3969  // SQLString returns the SQL statement describing the column.
  3970  func (desc *ColumnDescriptor) SQLString() string {
  3971  	f := tree.NewFmtCtx(tree.FmtSimple)
  3972  	f.FormatNameP(&desc.Name)
  3973  	f.WriteByte(' ')
  3974  	f.WriteString(desc.Type.SQLString())
  3975  	if desc.Nullable {
  3976  		f.WriteString(" NULL")
  3977  	} else {
  3978  		f.WriteString(" NOT NULL")
  3979  	}
  3980  	if desc.DefaultExpr != nil {
  3981  		f.WriteString(" DEFAULT ")
  3982  		f.WriteString(*desc.DefaultExpr)
  3983  	}
  3984  	if desc.IsComputed() {
  3985  		f.WriteString(" AS (")
  3986  		f.WriteString(*desc.ComputeExpr)
  3987  		f.WriteString(") STORED")
  3988  	}
  3989  	return f.CloseAndGetString()
  3990  }
  3991  
  3992  // ColumnsUsed returns the IDs of the columns used in the check constraint's
  3993  // expression. v2.0 binaries will populate this during table creation, but older
  3994  // binaries will not, in which case this needs to be computed when requested.
  3995  //
  3996  // TODO(nvanbenschoten): we can remove this in v2.1 and replace it with a sql
  3997  // migration to backfill all TableDescriptor_CheckConstraint.ColumnIDs slices.
  3998  // See #22322.
  3999  func (cc *TableDescriptor_CheckConstraint) ColumnsUsed(desc *TableDescriptor) ([]ColumnID, error) {
  4000  	if len(cc.ColumnIDs) > 0 {
  4001  		// Already populated.
  4002  		return cc.ColumnIDs, nil
  4003  	}
  4004  
  4005  	parsed, err := parser.ParseExpr(cc.Expr)
  4006  	if err != nil {
  4007  		return nil, pgerror.Wrapf(err, pgcode.Syntax,
  4008  			"could not parse check constraint %s", cc.Expr)
  4009  	}
  4010  
  4011  	colIDsUsed := make(map[ColumnID]struct{})
  4012  	visitFn := func(expr tree.Expr) (recurse bool, newExpr tree.Expr, err error) {
  4013  		if vBase, ok := expr.(tree.VarName); ok {
  4014  			v, err := vBase.NormalizeVarName()
  4015  			if err != nil {
  4016  				return false, nil, err
  4017  			}
  4018  			if c, ok := v.(*tree.ColumnItem); ok {
  4019  				col, dropped, err := desc.FindColumnByName(c.ColumnName)
  4020  				if err != nil || dropped {
  4021  					return false, nil, pgerror.Newf(pgcode.UndefinedColumn,
  4022  						"column %q not found for constraint %q",
  4023  						c.ColumnName, parsed.String())
  4024  				}
  4025  				colIDsUsed[col.ID] = struct{}{}
  4026  			}
  4027  			return false, v, nil
  4028  		}
  4029  		return true, expr, nil
  4030  	}
  4031  	if _, err := tree.SimpleVisit(parsed, visitFn); err != nil {
  4032  		return nil, err
  4033  	}
  4034  
  4035  	cc.ColumnIDs = make([]ColumnID, 0, len(colIDsUsed))
  4036  	for colID := range colIDsUsed {
  4037  		cc.ColumnIDs = append(cc.ColumnIDs, colID)
  4038  	}
  4039  	sort.Sort(ColumnIDs(cc.ColumnIDs))
  4040  	return cc.ColumnIDs, nil
  4041  }
  4042  
  4043  // UsesColumn returns whether the check constraint uses the specified column.
  4044  func (cc *TableDescriptor_CheckConstraint) UsesColumn(
  4045  	desc *TableDescriptor, colID ColumnID,
  4046  ) (bool, error) {
  4047  	colsUsed, err := cc.ColumnsUsed(desc)
  4048  	if err != nil {
  4049  		return false, err
  4050  	}
  4051  	i := sort.Search(len(colsUsed), func(i int) bool {
  4052  		return colsUsed[i] >= colID
  4053  	})
  4054  	return i < len(colsUsed) && colsUsed[i] == colID, nil
  4055  }
  4056  
  4057  // CompositeKeyMatchMethodValue allows the conversion from a
  4058  // tree.ReferenceCompositeKeyMatchMethod to a ForeignKeyReference_Match.
  4059  var CompositeKeyMatchMethodValue = [...]ForeignKeyReference_Match{
  4060  	tree.MatchSimple:  ForeignKeyReference_SIMPLE,
  4061  	tree.MatchFull:    ForeignKeyReference_FULL,
  4062  	tree.MatchPartial: ForeignKeyReference_PARTIAL,
  4063  }
  4064  
  4065  // ForeignKeyReferenceMatchValue allows the conversion from a
  4066  // ForeignKeyReference_Match to a tree.ReferenceCompositeKeyMatchMethod.
  4067  // This should match CompositeKeyMatchMethodValue.
  4068  var ForeignKeyReferenceMatchValue = [...]tree.CompositeKeyMatchMethod{
  4069  	ForeignKeyReference_SIMPLE:  tree.MatchSimple,
  4070  	ForeignKeyReference_FULL:    tree.MatchFull,
  4071  	ForeignKeyReference_PARTIAL: tree.MatchPartial,
  4072  }
  4073  
  4074  // String implements the fmt.Stringer interface.
  4075  func (x ForeignKeyReference_Match) String() string {
  4076  	switch x {
  4077  	case ForeignKeyReference_SIMPLE:
  4078  		return "MATCH SIMPLE"
  4079  	case ForeignKeyReference_FULL:
  4080  		return "MATCH FULL"
  4081  	case ForeignKeyReference_PARTIAL:
  4082  		return "MATCH PARTIAL"
  4083  	default:
  4084  		return strconv.Itoa(int(x))
  4085  	}
  4086  }
  4087  
  4088  // ForeignKeyReferenceActionType allows the conversion between a
  4089  // tree.ReferenceAction and a ForeignKeyReference_Action.
  4090  var ForeignKeyReferenceActionType = [...]tree.ReferenceAction{
  4091  	ForeignKeyReference_NO_ACTION:   tree.NoAction,
  4092  	ForeignKeyReference_RESTRICT:    tree.Restrict,
  4093  	ForeignKeyReference_SET_DEFAULT: tree.SetDefault,
  4094  	ForeignKeyReference_SET_NULL:    tree.SetNull,
  4095  	ForeignKeyReference_CASCADE:     tree.Cascade,
  4096  }
  4097  
  4098  // ForeignKeyReferenceActionValue allows the conversion between a
  4099  // ForeignKeyReference_Action and a tree.ReferenceAction.
  4100  var ForeignKeyReferenceActionValue = [...]ForeignKeyReference_Action{
  4101  	tree.NoAction:   ForeignKeyReference_NO_ACTION,
  4102  	tree.Restrict:   ForeignKeyReference_RESTRICT,
  4103  	tree.SetDefault: ForeignKeyReference_SET_DEFAULT,
  4104  	tree.SetNull:    ForeignKeyReference_SET_NULL,
  4105  	tree.Cascade:    ForeignKeyReference_CASCADE,
  4106  }
  4107  
  4108  // String implements the fmt.Stringer interface.
  4109  func (x ForeignKeyReference_Action) String() string {
  4110  	switch x {
  4111  	case ForeignKeyReference_RESTRICT:
  4112  		return "RESTRICT"
  4113  	case ForeignKeyReference_SET_DEFAULT:
  4114  		return "SET DEFAULT"
  4115  	case ForeignKeyReference_SET_NULL:
  4116  		return "SET NULL"
  4117  	case ForeignKeyReference_CASCADE:
  4118  		return "CASCADE"
  4119  	default:
  4120  		return strconv.Itoa(int(x))
  4121  	}
  4122  }
  4123  
  4124  var _ cat.Column = &ColumnDescriptor{}
  4125  
  4126  // IsNullable is part of the cat.Column interface.
  4127  func (desc *ColumnDescriptor) IsNullable() bool {
  4128  	return desc.Nullable
  4129  }
  4130  
  4131  // HasNullDefault checks that the column descriptor has a default of NULL.
  4132  func (desc *ColumnDescriptor) HasNullDefault() bool {
  4133  	if !desc.HasDefault() {
  4134  		return false
  4135  	}
  4136  	defaultExpr, err := parser.ParseExpr(*desc.DefaultExpr)
  4137  	if err != nil {
  4138  		panic(errors.NewAssertionErrorWithWrappedErrf(err,
  4139  			"failed to parse default expression %s", *desc.DefaultExpr))
  4140  	}
  4141  	return defaultExpr == tree.DNull
  4142  }
  4143  
  4144  // ColID is part of the cat.Column interface.
  4145  func (desc *ColumnDescriptor) ColID() cat.StableID {
  4146  	return cat.StableID(desc.ID)
  4147  }
  4148  
  4149  // ColName is part of the cat.Column interface.
  4150  func (desc *ColumnDescriptor) ColName() tree.Name {
  4151  	return tree.Name(desc.Name)
  4152  }
  4153  
  4154  // DatumType is part of the cat.Column interface.
  4155  func (desc *ColumnDescriptor) DatumType() *types.T {
  4156  	return desc.Type
  4157  }
  4158  
  4159  // ColTypePrecision is part of the cat.Column interface.
  4160  func (desc *ColumnDescriptor) ColTypePrecision() int {
  4161  	if desc.Type.Family() == types.ArrayFamily {
  4162  		if desc.Type.ArrayContents().Family() == types.ArrayFamily {
  4163  			panic(errors.AssertionFailedf("column type should never be a nested array"))
  4164  		}
  4165  		return int(desc.Type.ArrayContents().Precision())
  4166  	}
  4167  	return int(desc.Type.Precision())
  4168  }
  4169  
  4170  // ColTypeWidth is part of the cat.Column interface.
  4171  func (desc *ColumnDescriptor) ColTypeWidth() int {
  4172  	if desc.Type.Family() == types.ArrayFamily {
  4173  		if desc.Type.ArrayContents().Family() == types.ArrayFamily {
  4174  			panic(errors.AssertionFailedf("column type should never be a nested array"))
  4175  		}
  4176  		return int(desc.Type.ArrayContents().Width())
  4177  	}
  4178  	return int(desc.Type.Width())
  4179  }
  4180  
  4181  // ColTypeStr is part of the cat.Column interface.
  4182  func (desc *ColumnDescriptor) ColTypeStr() string {
  4183  	return desc.Type.SQLString()
  4184  }
  4185  
  4186  // IsHidden is part of the cat.Column interface.
  4187  func (desc *ColumnDescriptor) IsHidden() bool {
  4188  	return desc.Hidden
  4189  }
  4190  
  4191  // HasDefault is part of the cat.Column interface.
  4192  func (desc *ColumnDescriptor) HasDefault() bool {
  4193  	return desc.DefaultExpr != nil
  4194  }
  4195  
  4196  // IsComputed is part of the cat.Column interface.
  4197  func (desc *ColumnDescriptor) IsComputed() bool {
  4198  	return desc.ComputeExpr != nil
  4199  }
  4200  
  4201  // DefaultExprStr is part of the cat.Column interface.
  4202  func (desc *ColumnDescriptor) DefaultExprStr() string {
  4203  	return *desc.DefaultExpr
  4204  }
  4205  
  4206  // ComputedExprStr is part of the cat.Column interface.
  4207  func (desc *ColumnDescriptor) ComputedExprStr() string {
  4208  	return *desc.ComputeExpr
  4209  }
  4210  
  4211  // CheckCanBeFKRef returns whether the given column is computed.
  4212  func (desc *ColumnDescriptor) CheckCanBeFKRef() error {
  4213  	if desc.IsComputed() {
  4214  		return unimplemented.NewWithIssuef(
  4215  			46672, "computed column %q cannot be a foreign key reference",
  4216  			desc.Name,
  4217  		)
  4218  	}
  4219  	return nil
  4220  }
  4221  
  4222  // GetFamilyOfColumn returns the ColumnFamilyDescriptor for the
  4223  // the family the column is part of.
  4224  func (desc *TableDescriptor) GetFamilyOfColumn(colID ColumnID) (*ColumnFamilyDescriptor, error) {
  4225  	for _, fam := range desc.Families {
  4226  		for _, id := range fam.ColumnIDs {
  4227  			if id == colID {
  4228  				return &fam, nil
  4229  			}
  4230  		}
  4231  	}
  4232  
  4233  	return nil, errors.Newf("no column family found for column id %v", colID)
  4234  }
  4235  
  4236  // PartitionNames returns a slice containing the name of every partition and
  4237  // subpartition in an arbitrary order.
  4238  func (desc *TableDescriptor) PartitionNames() []string {
  4239  	var names []string
  4240  	for _, index := range desc.AllNonDropIndexes() {
  4241  		names = append(names, index.Partitioning.PartitionNames()...)
  4242  	}
  4243  	return names
  4244  }
  4245  
  4246  // PartitionNames returns a slice containing the name of every partition and
  4247  // subpartition in an arbitrary order.
  4248  func (desc *PartitioningDescriptor) PartitionNames() []string {
  4249  	var names []string
  4250  	for _, l := range desc.List {
  4251  		names = append(names, l.Name)
  4252  		names = append(names, l.Subpartitioning.PartitionNames()...)
  4253  	}
  4254  	for _, r := range desc.Range {
  4255  		names = append(names, r.Name)
  4256  	}
  4257  	return names
  4258  }
  4259  
  4260  // SetAuditMode configures the audit mode on the descriptor.
  4261  func (desc *TableDescriptor) SetAuditMode(mode tree.AuditMode) (bool, error) {
  4262  	prev := desc.AuditMode
  4263  	switch mode {
  4264  	case tree.AuditModeDisable:
  4265  		desc.AuditMode = TableDescriptor_DISABLED
  4266  	case tree.AuditModeReadWrite:
  4267  		desc.AuditMode = TableDescriptor_READWRITE
  4268  	default:
  4269  		return false, pgerror.Newf(pgcode.InvalidParameterValue,
  4270  			"unknown audit mode: %s (%d)", mode, mode)
  4271  	}
  4272  	return prev != desc.AuditMode, nil
  4273  }
  4274  
  4275  // GetAuditMode is part of the DescriptorProto interface.
  4276  // This is a stub until per-database auditing is enabled.
  4277  func (desc *DatabaseDescriptor) GetAuditMode() TableDescriptor_AuditMode {
  4278  	return TableDescriptor_DISABLED
  4279  }
  4280  
  4281  // FindAllReferences returns all the references from a table.
  4282  func (desc *TableDescriptor) FindAllReferences() (map[ID]struct{}, error) {
  4283  	refs := map[ID]struct{}{}
  4284  	for i := range desc.OutboundFKs {
  4285  		fk := &desc.OutboundFKs[i]
  4286  		refs[fk.ReferencedTableID] = struct{}{}
  4287  	}
  4288  	for i := range desc.InboundFKs {
  4289  		fk := &desc.InboundFKs[i]
  4290  		refs[fk.OriginTableID] = struct{}{}
  4291  	}
  4292  	if err := desc.ForeachNonDropIndex(func(index *IndexDescriptor) error {
  4293  		for _, a := range index.Interleave.Ancestors {
  4294  			refs[a.TableID] = struct{}{}
  4295  		}
  4296  		for _, c := range index.InterleavedBy {
  4297  			refs[c.Table] = struct{}{}
  4298  		}
  4299  		return nil
  4300  	}); err != nil {
  4301  		return nil, err
  4302  	}
  4303  
  4304  	for _, c := range desc.AllNonDropColumns() {
  4305  		for _, id := range c.UsesSequenceIds {
  4306  			refs[id] = struct{}{}
  4307  		}
  4308  	}
  4309  
  4310  	for _, dest := range desc.DependsOn {
  4311  		refs[dest] = struct{}{}
  4312  	}
  4313  
  4314  	for _, c := range desc.DependedOnBy {
  4315  		refs[c.ID] = struct{}{}
  4316  	}
  4317  	return refs, nil
  4318  }
  4319  
  4320  // ActiveChecks returns a list of all check constraints that should be enforced
  4321  // on writes (including constraints being added/validated). The columns
  4322  // referenced by the returned checks are writable, but not necessarily public.
  4323  func (desc *ImmutableTableDescriptor) ActiveChecks() []TableDescriptor_CheckConstraint {
  4324  	return desc.allChecks
  4325  }
  4326  
  4327  // WritableColumns returns a list of public and write-only mutation columns.
  4328  func (desc *ImmutableTableDescriptor) WritableColumns() []ColumnDescriptor {
  4329  	return desc.publicAndNonPublicCols[:len(desc.Columns)+desc.writeOnlyColCount]
  4330  }
  4331  
  4332  // DeletableColumns returns a list of public and non-public columns.
  4333  func (desc *ImmutableTableDescriptor) DeletableColumns() []ColumnDescriptor {
  4334  	return desc.publicAndNonPublicCols
  4335  }
  4336  
  4337  // MutationColumns returns a list of mutation columns.
  4338  func (desc *ImmutableTableDescriptor) MutationColumns() []ColumnDescriptor {
  4339  	return desc.publicAndNonPublicCols[len(desc.Columns):]
  4340  }
  4341  
  4342  // WritableIndexes returns a list of public and write-only mutation indexes.
  4343  func (desc *ImmutableTableDescriptor) WritableIndexes() []IndexDescriptor {
  4344  	return desc.publicAndNonPublicIndexes[:len(desc.Indexes)+desc.writeOnlyIndexCount]
  4345  }
  4346  
  4347  // DeletableIndexes returns a list of public and non-public indexes.
  4348  func (desc *ImmutableTableDescriptor) DeletableIndexes() []IndexDescriptor {
  4349  	return desc.publicAndNonPublicIndexes
  4350  }
  4351  
  4352  // DeleteOnlyIndexes returns a list of delete-only mutation indexes.
  4353  func (desc *ImmutableTableDescriptor) DeleteOnlyIndexes() []IndexDescriptor {
  4354  	return desc.publicAndNonPublicIndexes[len(desc.Indexes)+desc.writeOnlyIndexCount:]
  4355  }
  4356  
  4357  // DatabaseDesc implements the ObjectDescriptor interface.
  4358  func (desc *MutableTableDescriptor) DatabaseDesc() *DatabaseDescriptor {
  4359  	return nil
  4360  }
  4361  
  4362  // SchemaDesc implements the ObjectDescriptor interface.
  4363  func (desc *MutableTableDescriptor) SchemaDesc() *SchemaDescriptor {
  4364  	return nil
  4365  }
  4366  
  4367  // TableDesc implements the ObjectDescriptor interface.
  4368  func (desc *MutableTableDescriptor) TableDesc() *TableDescriptor {
  4369  	return &desc.TableDescriptor
  4370  }
  4371  
  4372  // IsShardColumn returns true if col corresponds to a non-dropped hash sharded
  4373  // index. This method assumes that col is currently a member of desc.
  4374  func (desc *MutableTableDescriptor) IsShardColumn(col *ColumnDescriptor) bool {
  4375  	for _, idx := range desc.AllNonDropIndexes() {
  4376  		if idx.Sharded.IsSharded && idx.Sharded.Name == col.Name {
  4377  			return true
  4378  		}
  4379  	}
  4380  	return false
  4381  }
  4382  
  4383  // TypeDesc implements the ObjectDescriptor interface.
  4384  func (desc *MutableTableDescriptor) TypeDesc() *TypeDescriptor {
  4385  	return nil
  4386  }
  4387  
  4388  // DatabaseDesc implements the ObjectDescriptor interface.
  4389  func (desc *ImmutableTableDescriptor) DatabaseDesc() *DatabaseDescriptor {
  4390  	return nil
  4391  }
  4392  
  4393  // SchemaDesc implements the ObjectDescriptor interface.
  4394  func (desc *ImmutableTableDescriptor) SchemaDesc() *SchemaDescriptor {
  4395  	return nil
  4396  }
  4397  
  4398  // TableDesc implements the ObjectDescriptor interface.
  4399  func (desc *ImmutableTableDescriptor) TableDesc() *TableDescriptor {
  4400  	return &desc.TableDescriptor
  4401  }
  4402  
  4403  // TypeDesc implements the ObjectDescriptor interface.
  4404  func (desc *ImmutableTableDescriptor) TypeDesc() *TypeDescriptor {
  4405  	return nil
  4406  }
  4407  
  4408  // MutableTypeDescriptor is a custom type for TypeDescriptors undergoing
  4409  // any types of modifications.
  4410  type MutableTypeDescriptor struct {
  4411  	TypeDescriptor
  4412  
  4413  	// ClusterVersion represents the version of the type descriptor read
  4414  	// from the store.
  4415  	ClusterVersion TypeDescriptor
  4416  }
  4417  
  4418  // ImmutableTypeDescriptor is a custom type for wrapping TypeDescriptors
  4419  // when used in a read only way.
  4420  type ImmutableTypeDescriptor struct {
  4421  	TypeDescriptor
  4422  }
  4423  
  4424  // Avoid linter unused warnings.
  4425  var _ = NewMutableCreatedTypeDescriptor
  4426  
  4427  // NewMutableCreatedTypeDescriptor returns a MutableTypeDescriptor from the
  4428  // given type descriptor with the cluster version being the zero type. This
  4429  // is for a type that is created in the same transaction.
  4430  func NewMutableCreatedTypeDescriptor(desc TypeDescriptor) *MutableTypeDescriptor {
  4431  	return &MutableTypeDescriptor{TypeDescriptor: desc}
  4432  }
  4433  
  4434  // NewMutableExistingTypeDescriptor returns a MutableTypeDescriptor from the
  4435  // given type descriptor with the cluster version also set to the descriptor.
  4436  // This is for types that already exist.
  4437  func NewMutableExistingTypeDescriptor(desc TypeDescriptor) *MutableTypeDescriptor {
  4438  	return &MutableTypeDescriptor{TypeDescriptor: desc, ClusterVersion: desc}
  4439  }
  4440  
  4441  // NewImmutableTypeDescriptor returns an ImmutableTypeDescriptor from the
  4442  // given TypeDescriptor.
  4443  func NewImmutableTypeDescriptor(desc TypeDescriptor) *ImmutableTypeDescriptor {
  4444  	return &ImmutableTypeDescriptor{TypeDescriptor: desc}
  4445  }
  4446  
  4447  // DatabaseDesc implements the ObjectDescriptor interface.
  4448  func (desc *TypeDescriptor) DatabaseDesc() *DatabaseDescriptor {
  4449  	return nil
  4450  }
  4451  
  4452  // SchemaDesc implements the ObjectDescriptor interface.
  4453  func (desc *TypeDescriptor) SchemaDesc() *SchemaDescriptor {
  4454  	return nil
  4455  }
  4456  
  4457  // TableDesc implements the ObjectDescriptor interface.
  4458  func (desc *TypeDescriptor) TableDesc() *TableDescriptor {
  4459  	return nil
  4460  }
  4461  
  4462  // TypeDesc implements the ObjectDescriptor interface.
  4463  func (desc *TypeDescriptor) TypeDesc() *TypeDescriptor {
  4464  	return desc
  4465  }
  4466  
  4467  // GetAuditMode implements the DescriptorProto interface.
  4468  func (desc *TypeDescriptor) GetAuditMode() TableDescriptor_AuditMode {
  4469  	return TableDescriptor_DISABLED
  4470  }
  4471  
  4472  // GetPrivileges implements the DescriptorProto interface.
  4473  func (desc *TypeDescriptor) GetPrivileges() *PrivilegeDescriptor {
  4474  	return nil
  4475  }
  4476  
  4477  // SetID implements the DescriptorProto interface.
  4478  func (desc *TypeDescriptor) SetID(id ID) {
  4479  	desc.ID = id
  4480  }
  4481  
  4482  // TypeName implements the DescriptorProto interface.
  4483  func (desc *TypeDescriptor) TypeName() string {
  4484  	return "type"
  4485  }
  4486  
  4487  // SetName implements the DescriptorProto interface.
  4488  func (desc *TypeDescriptor) SetName(name string) {
  4489  	desc.Name = name
  4490  }
  4491  
  4492  // HydrateTypeInfoWithName fills in user defined type metadata for
  4493  // a type and sets the name in the metadata to the passed in name.
  4494  // This is used when hydrating a type with a known qualified name.
  4495  // TODO (rohany): This method should eventually be defined on an
  4496  //  ImmutableTypeDescriptor so that pointers to the cached info
  4497  //  can be shared among callers.
  4498  func (desc *TypeDescriptor) HydrateTypeInfoWithName(
  4499  	typ *types.T, name *tree.TypeName, typeLookup TypeLookupFunc,
  4500  ) error {
  4501  	typ.TypeMeta.Name = types.MakeUserDefinedTypeName(name.Catalog(), name.Schema(), name.Object())
  4502  	switch desc.Kind {
  4503  	case TypeDescriptor_ENUM:
  4504  		if typ.Family() != types.EnumFamily {
  4505  			return errors.New("cannot hydrate a non-enum type with an enum type descriptor")
  4506  		}
  4507  		logical := make([]string, len(desc.EnumMembers))
  4508  		physical := make([][]byte, len(desc.EnumMembers))
  4509  		for i := range desc.EnumMembers {
  4510  			member := &desc.EnumMembers[i]
  4511  			logical[i] = member.LogicalRepresentation
  4512  			physical[i] = member.PhysicalRepresentation
  4513  		}
  4514  		typ.TypeMeta.EnumData = &types.EnumMetadata{
  4515  			LogicalRepresentations:  logical,
  4516  			PhysicalRepresentations: physical,
  4517  		}
  4518  		return nil
  4519  	case TypeDescriptor_ALIAS:
  4520  		if typ.UserDefined() {
  4521  			switch typ.Family() {
  4522  			case types.ArrayFamily:
  4523  				// Hydrate the element type.
  4524  				elemType := typ.ArrayContents()
  4525  				elemTypName, elemTypDesc, err := typeLookup(ID(elemType.StableTypeID()))
  4526  				if err != nil {
  4527  					return err
  4528  				}
  4529  				if err := elemTypDesc.HydrateTypeInfoWithName(elemType, elemTypName, typeLookup); err != nil {
  4530  					return err
  4531  				}
  4532  				return nil
  4533  			default:
  4534  				return errors.AssertionFailedf("only array types aliases can be user defined")
  4535  			}
  4536  		}
  4537  		return nil
  4538  	default:
  4539  		return errors.AssertionFailedf("unknown type descriptor kind %s", desc.Kind)
  4540  	}
  4541  }
  4542  
  4543  // TypeLookupFunc is a type alias for a function that looks up a type by ID.
  4544  type TypeLookupFunc func(id ID) (*tree.TypeName, *TypeDescriptor, error)
  4545  
  4546  // MakeTypesT creates a types.T from the input type descriptor.
  4547  func (desc *TypeDescriptor) MakeTypesT(
  4548  	name *tree.TypeName, typeLookup TypeLookupFunc,
  4549  ) (*types.T, error) {
  4550  	switch t := desc.Kind; t {
  4551  	case TypeDescriptor_ENUM:
  4552  		typ := types.MakeEnum(uint32(desc.ID), uint32(desc.ArrayTypeID))
  4553  		if err := desc.HydrateTypeInfoWithName(typ, name, typeLookup); err != nil {
  4554  			return nil, err
  4555  		}
  4556  		return typ, nil
  4557  	case TypeDescriptor_ALIAS:
  4558  		// Hydrate the alias and return it.
  4559  		if err := desc.HydrateTypeInfoWithName(desc.Alias, name, typeLookup); err != nil {
  4560  			return nil, err
  4561  		}
  4562  		return desc.Alias, nil
  4563  	default:
  4564  		return nil, errors.AssertionFailedf("unknown type kind %s", t.String())
  4565  	}
  4566  }
  4567  
  4568  // HydrateTypesInTableDescriptor uses typeLookup to install metadata in the
  4569  // types present in a table descriptor. typeLookup retrieves the fully
  4570  // qualified name and descriptor for a particular ID.
  4571  func HydrateTypesInTableDescriptor(desc *TableDescriptor, typeLookup TypeLookupFunc) error {
  4572  	for i := range desc.Columns {
  4573  		col := &desc.Columns[i]
  4574  		if col.Type.UserDefined() {
  4575  			// Look up its type descriptor.
  4576  			name, typDesc, err := typeLookup(ID(col.Type.StableTypeID()))
  4577  			if err != nil {
  4578  				return err
  4579  			}
  4580  			// TODO (rohany): This should be a noop if the hydrated type
  4581  			//  information present in the descriptor has the same version as
  4582  			//  the resolved type descriptor we found here.
  4583  			if err := typDesc.HydrateTypeInfoWithName(col.Type, name, typeLookup); err != nil {
  4584  				return err
  4585  			}
  4586  		}
  4587  	}
  4588  	return nil
  4589  }
  4590  
  4591  // MakeSimpleAliasTypeDescriptor creates a type descriptor that is an alias
  4592  // for the input type. It is intended to be used as an intermediate for name
  4593  // resolution, and should not be serialized and stored on disk.
  4594  func MakeSimpleAliasTypeDescriptor(typ *types.T) *TypeDescriptor {
  4595  	return &TypeDescriptor{
  4596  		ParentID:       InvalidID,
  4597  		ParentSchemaID: InvalidID,
  4598  		Name:           typ.Name(),
  4599  		ID:             InvalidID,
  4600  		Kind:           TypeDescriptor_ALIAS,
  4601  		Alias:          typ,
  4602  	}
  4603  }
  4604  
  4605  // MakeTypeDescriptor creates a type descriptor. It does not fill in kind
  4606  // specific information about the type.
  4607  func MakeTypeDescriptor(parentID, parentSchemaID, id ID, name string) TypeDescriptor {
  4608  	return TypeDescriptor{
  4609  		ParentID:       parentID,
  4610  		ParentSchemaID: parentSchemaID,
  4611  		Name:           name,
  4612  		ID:             id,
  4613  	}
  4614  }
  4615  
  4616  // NameResolutionResult implements the NameResolutionResult interface.
  4617  func (desc *TypeDescriptor) NameResolutionResult() {}
  4618  
  4619  // GetAuditMode implements the DescriptorProto interface.
  4620  func (desc *SchemaDescriptor) GetAuditMode() TableDescriptor_AuditMode {
  4621  	return TableDescriptor_DISABLED
  4622  }
  4623  
  4624  // SetID implements the DescriptorProto interface.
  4625  func (desc *SchemaDescriptor) SetID(id ID) {
  4626  	desc.ID = id
  4627  }
  4628  
  4629  // TypeName implements the DescriptorProto interface.
  4630  func (desc *SchemaDescriptor) TypeName() string {
  4631  	return "schema"
  4632  }
  4633  
  4634  // SetName implements the DescriptorProto interface.
  4635  func (desc *SchemaDescriptor) SetName(name string) {
  4636  	desc.Name = name
  4637  }
  4638  
  4639  // DatabaseDesc implements the ObjectDescriptor interface.
  4640  func (desc *SchemaDescriptor) DatabaseDesc() *DatabaseDescriptor {
  4641  	return nil
  4642  }
  4643  
  4644  // SchemaDesc implements the ObjectDescriptor interface.
  4645  func (desc *SchemaDescriptor) SchemaDesc() *SchemaDescriptor {
  4646  	return desc
  4647  }
  4648  
  4649  // TableDesc implements the ObjectDescriptor interface.
  4650  func (desc *SchemaDescriptor) TableDesc() *TableDescriptor {
  4651  	return nil
  4652  }
  4653  
  4654  // TypeDesc implements the ObjectDescriptor interface.
  4655  func (desc *SchemaDescriptor) TypeDesc() *TypeDescriptor {
  4656  	return nil
  4657  }
  4658  
  4659  // NameResolutionResult implements the ObjectDescriptor interface.
  4660  func (desc *SchemaDescriptor) NameResolutionResult() {}
  4661  
  4662  // DatabaseKey implements DescriptorKey.
  4663  type DatabaseKey struct {
  4664  	name string
  4665  }
  4666  
  4667  // NewDatabaseKey returns a new DatabaseKey.
  4668  func NewDatabaseKey(name string) DatabaseKey {
  4669  	return DatabaseKey{name: name}
  4670  }
  4671  
  4672  // Key implements DescriptorKey interface.
  4673  func (dk DatabaseKey) Key(codec keys.SQLCodec) roachpb.Key {
  4674  	return MakeNameMetadataKey(codec, keys.RootNamespaceID, keys.RootNamespaceID, dk.name)
  4675  }
  4676  
  4677  // Name implements DescriptorKey interface.
  4678  func (dk DatabaseKey) Name() string {
  4679  	return dk.name
  4680  }
  4681  
  4682  // TableKey implements DescriptorKey interface.
  4683  type TableKey struct {
  4684  	parentID       ID
  4685  	parentSchemaID ID
  4686  	name           string
  4687  }
  4688  
  4689  // NewPublicTableKey returns a new TableKey scoped under the public schema.
  4690  func NewPublicTableKey(parentID ID, name string) TableKey {
  4691  	return TableKey{parentID: parentID, parentSchemaID: keys.PublicSchemaID, name: name}
  4692  }
  4693  
  4694  // NewTableKey returns a new TableKey
  4695  func NewTableKey(parentID ID, parentSchemaID ID, name string) TableKey {
  4696  	return TableKey{parentID: parentID, parentSchemaID: parentSchemaID, name: name}
  4697  }
  4698  
  4699  // Key implements DescriptorKey interface.
  4700  func (tk TableKey) Key(codec keys.SQLCodec) roachpb.Key {
  4701  	return MakeNameMetadataKey(codec, tk.parentID, tk.parentSchemaID, tk.name)
  4702  }
  4703  
  4704  // Name implements DescriptorKey interface.
  4705  func (tk TableKey) Name() string {
  4706  	return tk.name
  4707  }
  4708  
  4709  // SchemaKey implements DescriptorKey interface.
  4710  type SchemaKey struct {
  4711  	parentID ID
  4712  	name     string
  4713  }
  4714  
  4715  // NewSchemaKey returns a new SchemaKey
  4716  func NewSchemaKey(parentID ID, name string) SchemaKey {
  4717  	return SchemaKey{parentID: parentID, name: name}
  4718  }
  4719  
  4720  // NewPublicSchemaKey returns a new SchemaKey specific to the public schema.
  4721  func NewPublicSchemaKey(parentID ID) SchemaKey {
  4722  	return SchemaKey{parentID: parentID, name: tree.PublicSchema}
  4723  }
  4724  
  4725  // Key implements DescriptorKey interface.
  4726  func (sk SchemaKey) Key(codec keys.SQLCodec) roachpb.Key {
  4727  	return MakeNameMetadataKey(codec, sk.parentID, keys.RootNamespaceID, sk.name)
  4728  }
  4729  
  4730  // Name implements DescriptorKey interface.
  4731  func (sk SchemaKey) Name() string {
  4732  	return sk.name
  4733  }
  4734  
  4735  // DeprecatedTableKey implements DescriptorKey interface.
  4736  type DeprecatedTableKey struct {
  4737  	parentID ID
  4738  	name     string
  4739  }
  4740  
  4741  // NewDeprecatedTableKey returns a new DeprecatedTableKey
  4742  func NewDeprecatedTableKey(parentID ID, name string) DeprecatedTableKey {
  4743  	return DeprecatedTableKey{parentID, name}
  4744  }
  4745  
  4746  // Key implements DescriptorKey interface.
  4747  func (dtk DeprecatedTableKey) Key(codec keys.SQLCodec) roachpb.Key {
  4748  	return MakeDeprecatedNameMetadataKey(codec, dtk.parentID, dtk.name)
  4749  }
  4750  
  4751  // Name implements DescriptorKey interface.
  4752  func (dtk DeprecatedTableKey) Name() string {
  4753  	return dtk.name
  4754  }
  4755  
  4756  // DeprecatedDatabaseKey implements DescriptorKey interface.
  4757  type DeprecatedDatabaseKey struct {
  4758  	name string
  4759  }
  4760  
  4761  // NewDeprecatedDatabaseKey returns a new DeprecatedDatabaseKey
  4762  func NewDeprecatedDatabaseKey(name string) DeprecatedDatabaseKey {
  4763  	return DeprecatedDatabaseKey{name: name}
  4764  }
  4765  
  4766  // Key implements DescriptorKey interface.
  4767  func (ddk DeprecatedDatabaseKey) Key(codec keys.SQLCodec) roachpb.Key {
  4768  	return MakeDeprecatedNameMetadataKey(codec, keys.RootNamespaceID, ddk.name)
  4769  }
  4770  
  4771  // Name implements DescriptorKey interface.
  4772  func (ddk DeprecatedDatabaseKey) Name() string {
  4773  	return ddk.name
  4774  }
  4775  
  4776  // GenerateUniqueConstraintName attempts to generate a unique constraint name
  4777  // with the given prefix.
  4778  // It will first try prefix by itself, then it will subsequently try
  4779  // adding numeric digits at the end, starting from 1.
  4780  func GenerateUniqueConstraintName(prefix string, nameExistsFunc func(name string) bool) string {
  4781  	name := prefix
  4782  	for i := 1; nameExistsFunc(name); i++ {
  4783  		name = fmt.Sprintf("%s_%d", prefix, i)
  4784  	}
  4785  	return name
  4786  }
  4787  
  4788  // GetLogicalColumnID returns the LogicalColumnID of the ColumnDescriptor
  4789  // if the LogicalColumnID is set (non-zero). Returns the ID of the
  4790  // ColumnDescriptor if the LogicalColumnID is not set.
  4791  func (desc ColumnDescriptor) GetLogicalColumnID() ColumnID {
  4792  	if desc.LogicalColumnID != 0 {
  4793  		return desc.LogicalColumnID
  4794  	}
  4795  
  4796  	return desc.ID
  4797  }