-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtable.go
525 lines (492 loc) · 17.9 KB
/
table.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
// Copyright 2015 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
import (
"context"
"fmt"
"sort"
"strings"
"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/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// MakeColumnDefDescs creates the column descriptor for a column, as well as the
// index descriptor if the column is a primary key or unique.
//
// If the column type *may* be SERIAL (or SERIAL-like), it is the
// caller's responsibility to call sql.processSerialInColumnDef() and
// sql.doCreateSequence() before MakeColumnDefDescs() to remove the
// SERIAL type and replace it with a suitable integer type and default
// expression.
//
// semaCtx can be nil if no default expression is used for the
// column or during cluster bootstrapping.
//
// The DEFAULT expression is returned in TypedExpr form for analysis (e.g. recording
// sequence dependencies).
func MakeColumnDefDescs(
ctx context.Context, d *tree.ColumnTableDef, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext,
) (*descpb.ColumnDescriptor, *descpb.IndexDescriptor, tree.TypedExpr, error) {
if d.IsSerial {
// To the reader of this code: if control arrives here, this means
// the caller has not suitably called processSerialInColumnDef()
// prior to calling MakeColumnDefDescs. The dependent sequences
// must be created, and the SERIAL type eliminated, prior to this
// point.
return nil, nil, nil, pgerror.New(pgcode.FeatureNotSupported,
"SERIAL cannot be used in this context")
}
if len(d.CheckExprs) > 0 {
// Should never happen since `HoistConstraints` moves these to table level
return nil, nil, nil, errors.New("unexpected column CHECK constraint")
}
if d.HasFKConstraint() {
// Should never happen since `HoistConstraints` moves these to table level
return nil, nil, nil, errors.New("unexpected column REFERENCED constraint")
}
col := &descpb.ColumnDescriptor{
Name: string(d.Name),
Nullable: d.Nullable.Nullability != tree.NotNull && !d.PrimaryKey.IsPrimaryKey,
Virtual: d.IsVirtual(),
Hidden: d.Hidden,
}
// Validate and assign column type.
resType, err := tree.ResolveType(ctx, d.Type, semaCtx.GetTypeResolver())
if err != nil {
return nil, nil, nil, err
}
if err := colinfo.ValidateColumnDefType(resType); err != nil {
return nil, nil, nil, err
}
col.Type = resType
var typedExpr tree.TypedExpr
if d.HasDefaultExpr() {
// Verify the default expression type is compatible with the column type
// and does not contain invalid functions.
var err error
if typedExpr, err = schemaexpr.SanitizeVarFreeExpr(
ctx, d.DefaultExpr.Expr, resType, "DEFAULT", semaCtx, tree.VolatilityVolatile,
); err != nil {
return nil, nil, nil, err
}
// Keep the type checked expression so that the type annotation gets
// properly stored, only if the default expression is not NULL.
// Otherwise we want to keep the default expression nil.
if typedExpr != tree.DNull {
d.DefaultExpr.Expr = typedExpr
s := tree.Serialize(d.DefaultExpr.Expr)
col.DefaultExpr = &s
}
}
if d.IsComputed() {
s := tree.Serialize(d.Computed.Expr)
col.ComputeExpr = &s
}
var idx *descpb.IndexDescriptor
if d.PrimaryKey.IsPrimaryKey || (d.Unique.IsUnique && !d.Unique.WithoutIndex) {
if !d.PrimaryKey.Sharded {
idx = &descpb.IndexDescriptor{
Unique: true,
ColumnNames: []string{string(d.Name)},
ColumnDirections: []descpb.IndexDescriptor_Direction{descpb.IndexDescriptor_ASC},
}
} else {
buckets, err := EvalShardBucketCount(ctx, semaCtx, evalCtx, d.PrimaryKey.ShardBuckets)
if err != nil {
return nil, nil, nil, err
}
shardColName := GetShardColumnName([]string{string(d.Name)}, buckets)
idx = &descpb.IndexDescriptor{
Unique: true,
ColumnNames: []string{shardColName, string(d.Name)},
ColumnDirections: []descpb.IndexDescriptor_Direction{descpb.IndexDescriptor_ASC, descpb.IndexDescriptor_ASC},
Sharded: descpb.ShardedDescriptor{
IsSharded: true,
Name: shardColName,
ShardBuckets: buckets,
ColumnNames: []string{string(d.Name)},
},
}
}
if d.Unique.ConstraintName != "" {
idx.Name = string(d.Unique.ConstraintName)
}
}
return col, idx, typedExpr, nil
}
// EvalShardBucketCount evaluates and checks the integer argument to a `USING HASH WITH
// BUCKET_COUNT` index creation query.
func EvalShardBucketCount(
ctx context.Context, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, shardBuckets tree.Expr,
) (int32, error) {
const invalidBucketCountMsg = `BUCKET_COUNT must be an integer greater than 1`
typedExpr, err := schemaexpr.SanitizeVarFreeExpr(
ctx, shardBuckets, types.Int, "BUCKET_COUNT", semaCtx, tree.VolatilityVolatile,
)
if err != nil {
return 0, err
}
d, err := typedExpr.Eval(evalCtx)
if err != nil {
return 0, pgerror.Wrap(err, pgcode.InvalidParameterValue, invalidBucketCountMsg)
}
buckets := tree.MustBeDInt(d)
if buckets < 2 {
return 0, pgerror.New(pgcode.InvalidParameterValue, invalidBucketCountMsg)
}
return int32(buckets), nil
}
// GetShardColumnName generates a name for the hidden shard column to be used to create a
// hash sharded index.
func GetShardColumnName(colNames []string, buckets int32) string {
// We sort the `colNames` here because we want to avoid creating a duplicate shard
// column if one already exists for the set of columns in `colNames`.
sort.Strings(colNames)
return strings.Join(
append(append([]string{`crdb_internal`}, colNames...), fmt.Sprintf(`shard_%v`, buckets)), `_`,
)
}
// GetConstraintInfo returns a summary of all constraints on the table.
func (desc *wrapper) GetConstraintInfo() (map[string]descpb.ConstraintDetail, error) {
return desc.collectConstraintInfo(nil)
}
// GetConstraintInfoWithLookup returns a summary of all constraints on the
// table using the provided function to fetch a TableDescriptor from an ID.
func (desc *wrapper) GetConstraintInfoWithLookup(
tableLookup catalog.TableLookupFn,
) (map[string]descpb.ConstraintDetail, error) {
return desc.collectConstraintInfo(tableLookup)
}
// CheckUniqueConstraints returns a non-nil error if a descriptor contains two
// constraints with the same name.
func (desc *wrapper) CheckUniqueConstraints() error {
_, err := desc.collectConstraintInfo(nil)
return err
}
// if `tableLookup` is non-nil, provide a full summary of constraints, otherwise just
// check that constraints have unique names.
func (desc *wrapper) collectConstraintInfo(
tableLookup catalog.TableLookupFn,
) (map[string]descpb.ConstraintDetail, error) {
info := make(map[string]descpb.ConstraintDetail)
// Indexes provide PK and Unique constraints that are enforced by an index.
for _, indexI := range desc.NonDropIndexes() {
index := indexI.IndexDesc()
if index.ID == desc.PrimaryIndex.ID {
if _, ok := info[index.Name]; ok {
return nil, pgerror.Newf(pgcode.DuplicateObject,
"duplicate constraint name: %q", index.Name)
}
colHiddenMap := make(map[descpb.ColumnID]bool, len(desc.Columns))
for i := range desc.Columns {
col := &desc.Columns[i]
colHiddenMap[col.ID] = col.Hidden
}
// Don't include constraints against only hidden columns.
// This prevents the auto-created rowid primary key index from showing up
// in show constraints.
hidden := true
for _, id := range index.ColumnIDs {
if !colHiddenMap[id] {
hidden = false
break
}
}
if hidden {
continue
}
detail := descpb.ConstraintDetail{Kind: descpb.ConstraintTypePK}
detail.Columns = index.ColumnNames
detail.Index = index
info[index.Name] = detail
} else if index.Unique {
if _, ok := info[index.Name]; ok {
return nil, pgerror.Newf(pgcode.DuplicateObject,
"duplicate constraint name: %q", index.Name)
}
detail := descpb.ConstraintDetail{Kind: descpb.ConstraintTypeUnique}
detail.Columns = index.ColumnNames
detail.Index = index
info[index.Name] = detail
}
}
// Get the unique constraints that are not enforced by an index.
ucs := desc.AllActiveAndInactiveUniqueWithoutIndexConstraints()
for _, uc := range ucs {
if _, ok := info[uc.Name]; ok {
return nil, pgerror.Newf(pgcode.DuplicateObject,
"duplicate constraint name: %q", uc.Name)
}
detail := descpb.ConstraintDetail{Kind: descpb.ConstraintTypeUnique}
// Constraints in the Validating state are considered Unvalidated for this
// purpose.
detail.Unvalidated = uc.Validity != descpb.ConstraintValidity_Validated
var err error
detail.Columns, err = desc.NamesForColumnIDs(uc.ColumnIDs)
if err != nil {
return nil, err
}
detail.UniqueWithoutIndexConstraint = uc
info[uc.Name] = detail
}
fks := desc.AllActiveAndInactiveForeignKeys()
for _, fk := range fks {
if _, ok := info[fk.Name]; ok {
return nil, pgerror.Newf(pgcode.DuplicateObject,
"duplicate constraint name: %q", fk.Name)
}
detail := descpb.ConstraintDetail{Kind: descpb.ConstraintTypeFK}
// Constraints in the Validating state are considered Unvalidated for this
// purpose.
detail.Unvalidated = fk.Validity != descpb.ConstraintValidity_Validated
var err error
detail.Columns, err = desc.NamesForColumnIDs(fk.OriginColumnIDs)
if err != nil {
return nil, err
}
detail.FK = fk
if tableLookup != nil {
other, err := tableLookup(fk.ReferencedTableID)
if err != nil {
return nil, errors.NewAssertionErrorWithWrappedErrf(err,
"error resolving table %d referenced in foreign key",
log.Safe(fk.ReferencedTableID))
}
referencedColumnNames, err := other.NamesForColumnIDs(fk.ReferencedColumnIDs)
if err != nil {
return nil, err
}
detail.Details = fmt.Sprintf("%s.%v", other.GetName(), referencedColumnNames)
detail.ReferencedTable = other.TableDesc()
}
info[fk.Name] = detail
}
for _, c := range desc.AllActiveAndInactiveChecks() {
if _, ok := info[c.Name]; ok {
return nil, pgerror.Newf(pgcode.DuplicateObject,
"duplicate constraint name: %q", c.Name)
}
detail := descpb.ConstraintDetail{Kind: descpb.ConstraintTypeCheck}
// Constraints in the Validating state are considered Unvalidated for this
// purpose.
detail.Unvalidated = c.Validity != descpb.ConstraintValidity_Validated
detail.CheckConstraint = c
detail.Details = c.Expr
if tableLookup != nil {
colsUsed, err := desc.ColumnsUsed(c)
if err != nil {
return nil, errors.NewAssertionErrorWithWrappedErrf(err,
"error computing columns used in check constraint %q", c.Name)
}
for _, colID := range colsUsed {
col, err := desc.FindColumnWithID(colID)
if err != nil {
return nil, errors.NewAssertionErrorWithWrappedErrf(err,
"error finding column %d in table %s", log.Safe(colID), desc.Name)
}
detail.Columns = append(detail.Columns, col.GetName())
}
}
info[c.Name] = detail
}
return info, nil
}
// FindFKReferencedUniqueConstraint finds the first index in the supplied
// referencedTable that can satisfy a foreign key of the supplied column ids.
// If no such index exists, attempts to find a unique constraint on the supplied
// column ids. If neither an index nor unique constraint is found, returns an
// error.
func FindFKReferencedUniqueConstraint(
referencedTable catalog.TableDescriptor, referencedColIDs descpb.ColumnIDs,
) (descpb.UniqueConstraint, error) {
// Search for a unique index on the referenced table that matches our foreign
// key columns.
primaryIndex := referencedTable.GetPrimaryIndex()
if primaryIndex.IsValidReferencedUniqueConstraint(referencedColIDs) {
return primaryIndex.IndexDesc(), nil
}
// If the PK doesn't match, find the index corresponding to the referenced column.
for _, idx := range referencedTable.PublicNonPrimaryIndexes() {
if idx.IsValidReferencedUniqueConstraint(referencedColIDs) {
return idx.IndexDesc(), nil
}
}
// As a last resort, try to find a unique constraint with matching columns.
uniqueWithoutIndexConstraints := referencedTable.GetUniqueWithoutIndexConstraints()
for i := range uniqueWithoutIndexConstraints {
c := &uniqueWithoutIndexConstraints[i]
// A partial unique constraint cannot be a reference constraint for a
// FK.
if c.IsPartial() {
continue
}
// TODO(rytaft): We should allow out-of-order unique constraints, as long
// as they have the same columns.
if descpb.ColumnIDs(c.ColumnIDs).Equals(referencedColIDs) {
return c, nil
}
}
return nil, pgerror.Newf(
pgcode.ForeignKeyViolation,
"there is no unique constraint matching given keys for referenced table %s",
referencedTable.GetName(),
)
}
// FindFKOriginIndex finds the first index in the supplied originTable
// that can satisfy an outgoing foreign key of the supplied column ids.
func FindFKOriginIndex(
originTable catalog.TableDescriptor, originColIDs descpb.ColumnIDs,
) (*descpb.IndexDescriptor, error) {
// Search for an index on the origin table that matches our foreign
// key columns.
if primaryIndex := originTable.GetPrimaryIndex(); primaryIndex.IsValidOriginIndex(originColIDs) {
return primaryIndex.IndexDesc(), nil
}
// If the PK doesn't match, find the index corresponding to the origin column.
for _, idx := range originTable.PublicNonPrimaryIndexes() {
if idx.IsValidOriginIndex(originColIDs) {
return idx.IndexDesc(), nil
}
}
return nil, pgerror.Newf(
pgcode.ForeignKeyViolation,
"there is no index matching given keys for referenced table %s",
originTable.GetName(),
)
}
// FindFKOriginIndexInTxn finds the first index in the supplied originTable
// that can satisfy an outgoing foreign key of the supplied column ids.
// It returns either an index that is active, or an index that was created
// in the same transaction that is currently running.
func FindFKOriginIndexInTxn(
originTable *Mutable, originColIDs descpb.ColumnIDs,
) (*descpb.IndexDescriptor, error) {
// Search for an index on the origin table that matches our foreign
// key columns.
if originTable.PrimaryIndex.IsValidOriginIndex(originColIDs) {
return &originTable.PrimaryIndex, nil
}
// If the PK doesn't match, find the index corresponding to the origin column.
for i := range originTable.Indexes {
idx := &originTable.Indexes[i]
if idx.IsValidOriginIndex(originColIDs) {
return idx, nil
}
}
currentMutationID := originTable.ClusterVersion.NextMutationID
for i := range originTable.Mutations {
mut := &originTable.Mutations[i]
if idx := mut.GetIndex(); idx != nil &&
mut.MutationID == currentMutationID &&
mut.Direction == descpb.DescriptorMutation_ADD {
if idx.IsValidOriginIndex(originColIDs) {
return idx, nil
}
}
}
return nil, pgerror.Newf(
pgcode.ForeignKeyViolation,
"there is no index matching given keys for referenced table %s",
originTable.Name,
)
}
// InitTableDescriptor returns a blank TableDescriptor.
func InitTableDescriptor(
id, parentID, parentSchemaID descpb.ID,
name string,
creationTime hlc.Timestamp,
privileges *descpb.PrivilegeDescriptor,
persistence tree.Persistence,
) Mutable {
return Mutable{
wrapper: wrapper{
TableDescriptor: descpb.TableDescriptor{
ID: id,
Name: name,
ParentID: parentID,
UnexposedParentSchemaID: parentSchemaID,
FormatVersion: descpb.InterleavedFormatVersion,
Version: 1,
ModificationTime: creationTime,
Privileges: privileges,
CreateAsOfTime: creationTime,
Temporary: persistence.IsTemporary(),
},
},
}
}
// FindPublicColumnsWithNames is a convenience function which behaves exactly
// like FindPublicColumnWithName applied repeatedly to the names in the
// provided list, returning early at the first encountered error.
func FindPublicColumnsWithNames(
desc catalog.TableDescriptor, names tree.NameList,
) ([]catalog.Column, error) {
cols := make([]catalog.Column, len(names))
for i, name := range names {
c, err := FindPublicColumnWithName(desc, name)
if err != nil {
return nil, err
}
cols[i] = c
}
return cols, nil
}
// FindPublicColumnWithName is a convenience function which behaves exactly
// like desc.FindColumnWithName except it ignores column mutations.
func FindPublicColumnWithName(
desc catalog.TableDescriptor, name tree.Name,
) (catalog.Column, error) {
col, err := desc.FindColumnWithName(name)
if err != nil {
return nil, err
}
if !col.Public() {
return nil, colinfo.NewUndefinedColumnError(string(name))
}
return col, nil
}
// FindPublicColumnWithID is a convenience function which behaves exactly
// like desc.FindColumnWithID except it ignores column mutations.
func FindPublicColumnWithID(
desc catalog.TableDescriptor, id descpb.ColumnID,
) (catalog.Column, error) {
col, err := desc.FindColumnWithID(id)
if err != nil {
return nil, err
}
if !col.Public() {
return nil, fmt.Errorf("column-id \"%d\" does not exist", id)
}
return col, nil
}
// FindVirtualColumn returns a catalog.Column matching the virtual column
// descriptor in `spec` if not nil, nil otherwise.
func FindVirtualColumn(
desc catalog.TableDescriptor, virtualColDesc *descpb.ColumnDescriptor,
) catalog.Column {
if virtualColDesc == nil {
return nil
}
found, err := desc.FindColumnWithID(virtualColDesc.ID)
if err != nil {
panic(errors.HandleAsAssertionFailure(err))
}
virtualColumn := found.DeepCopy()
*virtualColumn.ColumnDesc() = *virtualColDesc
return virtualColumn
}