-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathtable_desc.go
554 lines (485 loc) · 19 KB
/
table_desc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// Package tabledesc provides concrete implementations of catalog.TableDesc.
package tabledesc
import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/errors"
)
var _ catalog.TableDescriptor = (*immutable)(nil)
var _ catalog.TableDescriptor = (*Mutable)(nil)
var _ catalog.MutableDescriptor = (*Mutable)(nil)
var _ catalog.TableDescriptor = (*wrapper)(nil)
// wrapper is the base implementation of the catalog.Descriptor
// interface, which is overloaded by immutable and Mutable.
type wrapper struct {
descpb.TableDescriptor
// mutationCache, indexCache and columnCache, when not nil, respectively point
// to a struct containing precomputed catalog.Mutation, catalog.Index or
// catalog.Column slices.
// Those can therefore only be set when creating an immutable.
mutationCache *mutationCache
indexCache *indexCache
columnCache *columnCache
postDeserializationChanges PostDeserializationTableDescriptorChanges
}
// IsUncommittedVersion implements the catalog.Descriptor interface.
func (*wrapper) IsUncommittedVersion() bool {
return false
}
// GetPostDeserializationChanges returns the set of changes which occurred to
// this descriptor post deserialization.
func (desc *wrapper) GetPostDeserializationChanges() PostDeserializationTableDescriptorChanges {
return desc.postDeserializationChanges
}
// HasPostDeserializationChanges returns if the MutableDescriptor was changed after running
// RunPostDeserializationChanges.
func (desc *wrapper) HasPostDeserializationChanges() bool {
return desc.postDeserializationChanges.UpgradedForeignKeyRepresentation ||
desc.postDeserializationChanges.UpgradedFormatVersion ||
desc.postDeserializationChanges.UpgradedIndexFormatVersion ||
desc.postDeserializationChanges.UpgradedNamespaceName ||
desc.postDeserializationChanges.UpgradedPrivileges
}
// ActiveChecks implements the TableDescriptor interface.
func (desc *wrapper) ActiveChecks() []descpb.TableDescriptor_CheckConstraint {
checks := make([]descpb.TableDescriptor_CheckConstraint, len(desc.Checks))
for i, c := range desc.Checks {
checks[i] = *c
}
return checks
}
// immutable is a custom type for TableDescriptors
// It holds precomputed values and the underlying TableDescriptor
// should be const.
type immutable struct {
wrapper
allChecks []descpb.TableDescriptor_CheckConstraint
// isUncommittedVersion is set to true if this descriptor was created from
// a copy of a Mutable with an uncommitted version.
isUncommittedVersion bool
// TODO (lucy): populate these and use them
// inboundFKs []*ForeignKeyConstraint
// outboundFKs []*ForeignKeyConstraint
}
// IsUncommittedVersion implements the Descriptor interface.
func (desc *immutable) IsUncommittedVersion() bool {
return desc.isUncommittedVersion
}
// DescriptorProto implements the Descriptor interface.
func (desc *wrapper) DescriptorProto() *descpb.Descriptor {
return &descpb.Descriptor{
Union: &descpb.Descriptor_Table{Table: &desc.TableDescriptor},
}
}
// ByteSize implements the Descriptor interface.
func (desc *wrapper) ByteSize() int64 {
return int64(desc.Size())
}
// NewBuilder implements the catalog.Descriptor interface.
func (desc *wrapper) NewBuilder() catalog.DescriptorBuilder {
return NewBuilder(desc.TableDesc())
}
// GetPrimaryIndexID implements the TableDescriptor interface.
func (desc *wrapper) GetPrimaryIndexID() descpb.IndexID {
return desc.PrimaryIndex.ID
}
// IsTemporary implements the TableDescriptor interface.
func (desc *wrapper) IsTemporary() bool {
return desc.GetTemporary()
}
// ImmutableCopy implements the MutableDescriptor interface.
func (desc *Mutable) ImmutableCopy() catalog.Descriptor {
if desc.IsUncommittedVersion() {
return NewBuilderForUncommittedVersion(desc.TableDesc()).BuildImmutable()
}
return NewBuilder(desc.TableDesc()).BuildImmutable()
}
// IsUncommittedVersion implements the Descriptor interface.
func (desc *Mutable) IsUncommittedVersion() bool {
return desc.IsNew() || desc.GetVersion() != desc.ClusterVersion.GetVersion()
}
// SetDrainingNames implements the MutableDescriptor interface.
//
// Deprecated: Do not use.
func (desc *Mutable) SetDrainingNames(names []descpb.NameInfo) {
desc.DrainingNames = names
}
// RemovePublicNonPrimaryIndex removes a secondary index by ordinal.
// indexOrdinal must be in range [1, len(desc.Indexes)], 0 denotes the primary
// index.
func (desc *Mutable) RemovePublicNonPrimaryIndex(indexOrdinal int) {
desc.Indexes = append(desc.Indexes[:indexOrdinal-1], desc.Indexes[indexOrdinal:]...)
}
// SetPublicNonPrimaryIndexes replaces all existing secondary indexes with new
// ones passed to it.
func (desc *Mutable) SetPublicNonPrimaryIndexes(indexes []descpb.IndexDescriptor) {
desc.Indexes = append(desc.Indexes[:0], indexes...)
}
// AddPublicNonPrimaryIndex adds a new secondary index.
func (desc *Mutable) AddPublicNonPrimaryIndex(index descpb.IndexDescriptor) {
desc.Indexes = append(desc.Indexes, index)
}
// SetPrimaryIndex sets the primary index.
func (desc *Mutable) SetPrimaryIndex(index descpb.IndexDescriptor) {
desc.PrimaryIndex = index
}
// SetPublicNonPrimaryIndex sets one of the secondary indexes.
// indexOrdinal must be in range [1, len(desc.Indexes)], 0 denotes the primary
// index.
func (desc *Mutable) SetPublicNonPrimaryIndex(indexOrdinal int, index descpb.IndexDescriptor) {
desc.Indexes[indexOrdinal-1] = index
}
// SetSequenceOwner adds sequence id to the sequence id list owned by a column
// and set ownership values of sequence options.
func (desc *Mutable) SetSequenceOwner(colName tree.Name, table *Mutable) error {
if !desc.IsSequence() {
return errors.Errorf("%s is not a sequence", desc.Name)
}
col, err := table.FindColumnWithName(colName)
if err != nil {
return err
}
found := false
for _, seqID := range col.ColumnDesc().OwnsSequenceIds {
if seqID == desc.ID {
found = true
break
}
}
if !found {
col.ColumnDesc().OwnsSequenceIds = append(col.ColumnDesc().OwnsSequenceIds, desc.ID)
}
desc.SequenceOpts.SequenceOwner.OwnerTableID = table.ID
desc.SequenceOpts.SequenceOwner.OwnerColumnID = col.GetID()
return nil
}
// UpdateIndexPartitioning applies the new partition and adjusts the column info
// for the specified index descriptor. Returns false iff this was a no-op.
func UpdateIndexPartitioning(
idx *descpb.IndexDescriptor,
isIndexPrimary bool,
newImplicitCols []catalog.Column,
newPartitioning descpb.PartitioningDescriptor,
) bool {
oldNumImplicitCols := int(idx.Partitioning.NumImplicitColumns)
isNoOp := oldNumImplicitCols == len(newImplicitCols) && idx.Partitioning.Equal(newPartitioning)
numCols := len(idx.KeyColumnIDs)
newCap := numCols + len(newImplicitCols) - oldNumImplicitCols
newColumnIDs := make([]descpb.ColumnID, len(newImplicitCols), newCap)
newColumnNames := make([]string, len(newImplicitCols), newCap)
newColumnDirections := make([]descpb.IndexDescriptor_Direction, len(newImplicitCols), newCap)
for i, col := range newImplicitCols {
newColumnIDs[i] = col.GetID()
newColumnNames[i] = col.GetName()
newColumnDirections[i] = descpb.IndexDescriptor_ASC
if isNoOp &&
(idx.KeyColumnIDs[i] != newColumnIDs[i] ||
idx.KeyColumnNames[i] != newColumnNames[i] ||
idx.KeyColumnDirections[i] != newColumnDirections[i]) {
isNoOp = false
}
}
if isNoOp {
return false
}
idx.KeyColumnIDs = append(newColumnIDs, idx.KeyColumnIDs[oldNumImplicitCols:]...)
idx.KeyColumnNames = append(newColumnNames, idx.KeyColumnNames[oldNumImplicitCols:]...)
idx.KeyColumnDirections = append(newColumnDirections, idx.KeyColumnDirections[oldNumImplicitCols:]...)
idx.Partitioning = newPartitioning
if !isIndexPrimary {
return true
}
newStoreColumnIDs := make([]descpb.ColumnID, 0, len(idx.StoreColumnIDs))
newStoreColumnNames := make([]string, 0, len(idx.StoreColumnNames))
for i := range idx.StoreColumnIDs {
id := idx.StoreColumnIDs[i]
name := idx.StoreColumnNames[i]
found := false
for _, newColumnName := range newColumnNames {
if newColumnName == name {
found = true
break
}
}
if !found {
newStoreColumnIDs = append(newStoreColumnIDs, id)
newStoreColumnNames = append(newStoreColumnNames, name)
}
}
idx.StoreColumnIDs = newStoreColumnIDs
idx.StoreColumnNames = newStoreColumnNames
if len(idx.StoreColumnNames) == 0 {
idx.StoreColumnIDs = nil
idx.StoreColumnNames = nil
}
return true
}
// GetPrimaryIndex implements the TableDescriptor interface.
func (desc *wrapper) GetPrimaryIndex() catalog.Index {
return desc.getExistingOrNewIndexCache().primary
}
// getExistingOrNewIndexCache should be the only place where the indexCache
// field in wrapper is ever read.
func (desc *wrapper) getExistingOrNewIndexCache() *indexCache {
if desc.indexCache != nil {
return desc.indexCache
}
return newIndexCache(desc.TableDesc(), desc.getExistingOrNewMutationCache())
}
// AllIndexes returns a slice with all indexes, public and non-public,
// in the underlying proto, in their canonical order:
// - the primary index,
// - the public non-primary indexes in the Indexes array, in order,
// - the non-public indexes present in the Mutations array, in order.
//
// See also catalog.Index.Ordinal().
func (desc *wrapper) AllIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().all
}
// ActiveIndexes returns a slice with all public indexes in the underlying
// proto, in their canonical order:
// - the primary index,
// - the public non-primary indexes in the Indexes array, in order.
//
// See also catalog.Index.Ordinal().
func (desc *wrapper) ActiveIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().active
}
// NonDropIndexes returns a slice of all non-drop indexes in the underlying
// proto, in their canonical order. This means:
// - the primary index, if the table is a physical table,
// - the public non-primary indexes in the Indexes array, in order,
// - the non-public indexes present in the Mutations array, in order,
// if the mutation is not a drop.
//
// See also catalog.Index.Ordinal().
func (desc *wrapper) NonDropIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().nonDrop
}
// NonDropIndexes returns a slice of all partial indexes in the underlying
// proto, in their canonical order. This is equivalent to taking the slice
// produced by AllIndexes and filtering indexes with non-empty expressions.
func (desc *wrapper) PartialIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().partial
}
// PublicNonPrimaryIndexes returns a slice of all active secondary indexes,
// in their canonical order. This is equivalent to the Indexes array in the
// proto.
func (desc *wrapper) PublicNonPrimaryIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().publicNonPrimary
}
// WritableNonPrimaryIndexes returns a slice of all non-primary indexes which
// allow being written to: public + delete-and-write-only, in their canonical
// order. This is equivalent to taking the slice produced by
// DeletableNonPrimaryIndexes and removing the indexes which are in mutations
// in the delete-only state.
func (desc *wrapper) WritableNonPrimaryIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().writableNonPrimary
}
// DeletableNonPrimaryIndexes returns a slice of all non-primary indexes
// which allow being deleted from: public + delete-and-write-only +
// delete-only, in their canonical order. This is equivalent to taking
// the slice produced by AllIndexes and removing the primary index.
func (desc *wrapper) DeletableNonPrimaryIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().deletableNonPrimary
}
// DeleteOnlyNonPrimaryIndexes returns a slice of all non-primary indexes
// which allow only being deleted from, in their canonical order. This is
// equivalent to taking the slice produced by DeletableNonPrimaryIndexes and
// removing the indexes which are not in mutations or not in the delete-only
// state.
func (desc *wrapper) DeleteOnlyNonPrimaryIndexes() []catalog.Index {
return desc.getExistingOrNewIndexCache().deleteOnlyNonPrimary
}
// FindIndexWithID returns the first catalog.Index that matches the id
// in the set of all indexes, or an error if none was found. The order of
// traversal is the canonical order, see catalog.Index.Ordinal().
func (desc *wrapper) FindIndexWithID(id descpb.IndexID) (catalog.Index, error) {
if idx := catalog.FindIndex(desc, catalog.IndexOpts{
NonPhysicalPrimaryIndex: true,
DropMutations: true,
AddMutations: true,
}, func(idx catalog.Index) bool {
return idx.GetID() == id
}); idx != nil {
return idx, nil
}
for _, m := range desc.GCMutations {
if m.IndexID == id {
return nil, ErrIndexGCMutationsList
}
}
return nil, errors.Errorf("index-id \"%d\" does not exist", id)
}
// FindIndexWithName returns the first catalog.Index that matches the name in
// the set of all indexes, excluding the primary index of non-physical
// tables, or an error if none was found. The order of traversal is the
// canonical order, see catalog.Index.Ordinal().
func (desc *wrapper) FindIndexWithName(name string) (catalog.Index, error) {
if idx := catalog.FindIndex(desc, catalog.IndexOpts{
NonPhysicalPrimaryIndex: false,
DropMutations: true,
AddMutations: true,
}, func(idx catalog.Index) bool {
return idx.GetName() == name
}); idx != nil {
return idx, nil
}
return nil, errors.Errorf("index %q does not exist", name)
}
// getExistingOrNewColumnCache should be the only place where the columnCache
// field in wrapper is ever read.
func (desc *wrapper) getExistingOrNewColumnCache() *columnCache {
if desc.columnCache != nil {
return desc.columnCache
}
return newColumnCache(desc.TableDesc(), desc.getExistingOrNewMutationCache())
}
// AllColumns implements the TableDescriptor interface.
func (desc *wrapper) AllColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().all
}
// PublicColumns implements the TableDescriptor interface.
func (desc *wrapper) PublicColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().public
}
// WritableColumns implements the TableDescriptor interface.
func (desc *wrapper) WritableColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().writable
}
// DeletableColumns implements the TableDescriptor interface.
func (desc *wrapper) DeletableColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().deletable
}
// NonDropColumns implements the TableDescriptor interface.
func (desc *wrapper) NonDropColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().nonDrop
}
// VisibleColumns implements the TableDescriptor interface.
func (desc *wrapper) VisibleColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().visible
}
// AccessibleColumns implements the TableDescriptor interface.
func (desc *wrapper) AccessibleColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().accessible
}
// UserDefinedTypeColumns implements the TableDescriptor interface.
func (desc *wrapper) UserDefinedTypeColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().withUDTs
}
// ReadableColumns implements the TableDescriptor interface.
func (desc *wrapper) ReadableColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().readable
}
// SystemColumns implements the TableDescriptor interface.
func (desc *wrapper) SystemColumns() []catalog.Column {
return desc.getExistingOrNewColumnCache().system
}
// IndexColumns implements the TableDescriptor interface.
func (desc *wrapper) IndexColumns(idx catalog.Index) []catalog.Column {
if ic := desc.getExistingOrNewIndexColumnCache(idx); ic != nil {
return ic.all
}
return nil
}
// IndexKeyColumns implements the TableDescriptor interface.
func (desc *wrapper) IndexKeyColumns(idx catalog.Index) []catalog.Column {
if ic := desc.getExistingOrNewIndexColumnCache(idx); ic != nil {
return ic.key
}
return nil
}
// IndexKeyColumnDirections implements the TableDescriptor interface.
func (desc *wrapper) IndexKeyColumnDirections(
idx catalog.Index,
) []descpb.IndexDescriptor_Direction {
if ic := desc.getExistingOrNewIndexColumnCache(idx); ic != nil {
return ic.keyDirs
}
return nil
}
// IndexKeySuffixColumns implements the TableDescriptor interface.
func (desc *wrapper) IndexKeySuffixColumns(idx catalog.Index) []catalog.Column {
if ic := desc.getExistingOrNewIndexColumnCache(idx); ic != nil {
return ic.keySuffix
}
return nil
}
// IndexFullColumns implements the TableDescriptor interface.
func (desc *wrapper) IndexFullColumns(idx catalog.Index) []catalog.Column {
if ic := desc.getExistingOrNewIndexColumnCache(idx); ic != nil {
return ic.full
}
return nil
}
// IndexFullColumnDirections implements the TableDescriptor interface.
func (desc *wrapper) IndexFullColumnDirections(
idx catalog.Index,
) []descpb.IndexDescriptor_Direction {
if ic := desc.getExistingOrNewIndexColumnCache(idx); ic != nil {
return ic.fullDirs
}
return nil
}
// IndexStoredColumns implements the TableDescriptor interface.
func (desc *wrapper) IndexStoredColumns(idx catalog.Index) []catalog.Column {
if ic := desc.getExistingOrNewIndexColumnCache(idx); ic != nil {
return ic.stored
}
return nil
}
// getExistingOrNewIndexColumnCache is a convenience method for Index*Columns
// methods.
func (desc *wrapper) getExistingOrNewIndexColumnCache(idx catalog.Index) *indexColumnCache {
if idx == nil {
return nil
}
c := desc.getExistingOrNewColumnCache()
if idx.Ordinal() >= len(c.index) {
return nil
}
return &c.index[idx.Ordinal()]
}
// FindColumnWithID implements the TableDescriptor interface.
func (desc *wrapper) FindColumnWithID(id descpb.ColumnID) (catalog.Column, error) {
for _, col := range desc.AllColumns() {
if col.GetID() == id {
return col, nil
}
}
return nil, pgerror.Newf(pgcode.UndefinedColumn, "column-id \"%d\" does not exist", id)
}
// FindColumnWithName implements the TableDescriptor interface.
func (desc *wrapper) FindColumnWithName(name tree.Name) (catalog.Column, error) {
for _, col := range desc.AllColumns() {
if col.ColName() == name {
return col, nil
}
}
return nil, colinfo.NewUndefinedColumnError(string(name))
}
// getExistingOrNewMutationCache should be the only place where the
// mutationCache field in wrapper is ever read.
func (desc *wrapper) getExistingOrNewMutationCache() *mutationCache {
if desc.mutationCache != nil {
return desc.mutationCache
}
return newMutationCache(desc.TableDesc())
}
// AllMutations implements the TableDescriptor interface.
func (desc *wrapper) AllMutations() []catalog.Mutation {
return desc.getExistingOrNewMutationCache().all
}