-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
table.go
599 lines (549 loc) · 19.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// 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 sqlbase
import (
"context"
"fmt"
"sort"
"strings"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"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/log"
"github.com/cockroachdb/errors"
"golang.org/x/text/language"
)
// SanitizeVarFreeExpr verifies that an expression is valid, has the correct
// type and contains no variable expressions. It returns the type-checked and
// constant-folded expression.
func SanitizeVarFreeExpr(
expr tree.Expr,
expectedType *types.T,
context string,
semaCtx *tree.SemaContext,
allowImpure bool,
) (tree.TypedExpr, error) {
if tree.ContainsVars(expr) {
return nil, pgerror.Newf(pgcode.Syntax,
"variable sub-expressions are not allowed in %s", context)
}
// We need to save and restore the previous value of the field in
// semaCtx in case we are recursively called from another context
// which uses the properties field.
defer semaCtx.Properties.Restore(semaCtx.Properties)
// Ensure that the expression doesn't contain special functions.
flags := tree.RejectSpecial
if !allowImpure {
flags |= tree.RejectImpureFunctions
}
semaCtx.Properties.Require(context, flags)
typedExpr, err := tree.TypeCheck(expr, semaCtx, expectedType)
if err != nil {
return nil, err
}
actualType := typedExpr.ResolvedType()
if !expectedType.Equivalent(actualType) && typedExpr != tree.DNull {
// The expression must match the column type exactly unless it is a constant
// NULL value.
return nil, fmt.Errorf("expected %s expression to have type %s, but '%s' has type %s",
context, expectedType, expr, actualType)
}
return typedExpr, nil
}
// ValidateColumnDefType returns an error if the type of a column definition is
// not valid. It is checked when a column is created or altered.
func ValidateColumnDefType(t *types.T) error {
switch t.Family() {
case types.StringFamily, types.CollatedStringFamily:
if t.Family() == types.CollatedStringFamily {
if _, err := language.Parse(t.Locale()); err != nil {
return pgerror.Newf(pgcode.Syntax, `invalid locale %s`, t.Locale())
}
}
case types.DecimalFamily:
switch {
case t.Precision() == 0 && t.Scale() > 0:
// TODO (seif): Find right range for error message.
return errors.New("invalid NUMERIC precision 0")
case t.Precision() < t.Scale():
return fmt.Errorf("NUMERIC scale %d must be between 0 and precision %d",
t.Scale(), t.Precision())
}
case types.ArrayFamily:
if t.ArrayContents().Family() == types.ArrayFamily {
// Nested arrays are not supported as a column type.
return errors.Errorf("nested array unsupported as column type: %s", t.String())
}
if err := types.CheckArrayElementType(t.ArrayContents()); err != nil {
return err
}
return ValidateColumnDefType(t.ArrayContents())
case types.BitFamily, types.IntFamily, types.FloatFamily, types.BoolFamily, types.BytesFamily, types.DateFamily,
types.INetFamily, types.IntervalFamily, types.JsonFamily, types.OidFamily, types.TimeFamily,
types.TimestampFamily, types.TimestampTZFamily, types.UuidFamily, types.TimeTZFamily,
types.GeographyFamily, types.GeometryFamily:
// These types are OK.
default:
return pgerror.Newf(pgcode.InvalidTableDefinition,
"value type %s cannot be used for table columns", t.String())
}
return nil
}
// 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.
//
// The DEFAULT expression is returned in TypedExpr form for analysis (e.g. recording
// sequence dependencies).
func MakeColumnDefDescs(
d *tree.ColumnTableDef, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext,
) (*ColumnDescriptor, *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 := &ColumnDescriptor{
Name: string(d.Name),
Nullable: d.Nullable.Nullability != tree.NotNull && !d.PrimaryKey.IsPrimaryKey,
}
// Validate and assign column type.
err := ValidateColumnDefType(d.Type)
if err != nil {
return nil, nil, nil, err
}
col.Type = *d.Type
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 = SanitizeVarFreeExpr(
d.DefaultExpr.Expr, d.Type, "DEFAULT", semaCtx, true, /* allowImpure */
); 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 *IndexDescriptor
if d.PrimaryKey.IsPrimaryKey || d.Unique {
if !d.PrimaryKey.Sharded {
idx = &IndexDescriptor{
Unique: true,
ColumnNames: []string{string(d.Name)},
ColumnDirections: []IndexDescriptor_Direction{IndexDescriptor_ASC},
}
} else {
buckets, err := EvalShardBucketCount(semaCtx, evalCtx, d.PrimaryKey.ShardBuckets)
if err != nil {
return nil, nil, nil, err
}
shardColName := GetShardColumnName([]string{string(d.Name)}, buckets)
idx = &IndexDescriptor{
Unique: true,
ColumnNames: []string{shardColName, string(d.Name)},
ColumnDirections: []IndexDescriptor_Direction{IndexDescriptor_ASC, IndexDescriptor_ASC},
Sharded: ShardedDescriptor{
IsSharded: true,
Name: shardColName,
ShardBuckets: buckets,
ColumnNames: []string{string(d.Name)},
},
}
}
if d.UniqueConstraintName != "" {
idx.Name = string(d.UniqueConstraintName)
}
}
return col, idx, typedExpr, nil
}
// EvalShardBucketCount evaluates and checks the integer argument to a `USING HASH WITH
// BUCKET_COUNT` index creation query.
func EvalShardBucketCount(
semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, shardBuckets tree.Expr,
) (int32, error) {
const invalidBucketCountMsg = `BUCKET_COUNT must be an integer greater than 1`
typedExpr, err := SanitizeVarFreeExpr(
shardBuckets, types.Int, "BUCKET_COUNT", semaCtx, true, /* allowImpure */
)
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)), `_`,
)
}
// EncodeColumns is a version of EncodePartialIndexKey that takes ColumnIDs and
// directions explicitly. WARNING: unlike EncodePartialIndexKey, EncodeColumns
// appends directly to keyPrefix.
func EncodeColumns(
columnIDs []ColumnID,
directions directions,
colMap map[ColumnID]int,
values []tree.Datum,
keyPrefix []byte,
) (key []byte, containsNull bool, err error) {
key = keyPrefix
for colIdx, id := range columnIDs {
val := findColumnValue(id, colMap, values)
if val == tree.DNull {
containsNull = true
}
dir, err := directions.get(colIdx)
if err != nil {
return nil, containsNull, err
}
if key, err = EncodeTableKey(key, val, dir); err != nil {
return nil, containsNull, err
}
}
return key, containsNull, nil
}
// GetColumnTypes returns the types of the columns with the given IDs.
func GetColumnTypes(desc *TableDescriptor, columnIDs []ColumnID) ([]types.T, error) {
types := make([]types.T, len(columnIDs))
for i, id := range columnIDs {
col, err := desc.FindActiveColumnByID(id)
if err != nil {
return nil, err
}
types[i] = col.Type
}
return types, nil
}
// ConstraintType is used to identify the type of a constraint.
type ConstraintType string
const (
// ConstraintTypePK identifies a PRIMARY KEY constraint.
ConstraintTypePK ConstraintType = "PRIMARY KEY"
// ConstraintTypeFK identifies a FOREIGN KEY constraint.
ConstraintTypeFK ConstraintType = "FOREIGN KEY"
// ConstraintTypeUnique identifies a FOREIGN constraint.
ConstraintTypeUnique ConstraintType = "UNIQUE"
// ConstraintTypeCheck identifies a CHECK constraint.
ConstraintTypeCheck ConstraintType = "CHECK"
)
// ConstraintDetail describes a constraint.
type ConstraintDetail struct {
Kind ConstraintType
Columns []string
Details string
Unvalidated bool
// Only populated for PK and Unique Constraints.
Index *IndexDescriptor
// Only populated for FK Constraints.
FK *ForeignKeyConstraint
ReferencedTable *TableDescriptor
// Only populated for Check Constraints.
CheckConstraint *TableDescriptor_CheckConstraint
}
type tableLookupFn func(ID) (*TableDescriptor, error)
// GetConstraintInfo returns a summary of all constraints on the table.
func (desc *TableDescriptor) GetConstraintInfo(
ctx context.Context, txn *kv.Txn,
) (map[string]ConstraintDetail, error) {
var tableLookup tableLookupFn
if txn != nil {
tableLookup = func(id ID) (*TableDescriptor, error) {
return GetTableDescFromID(ctx, txn, id)
}
}
return desc.collectConstraintInfo(tableLookup)
}
// GetConstraintInfoWithLookup returns a summary of all constraints on the
// table using the provided function to fetch a TableDescriptor from an ID.
func (desc *TableDescriptor) GetConstraintInfoWithLookup(
tableLookup tableLookupFn,
) (map[string]ConstraintDetail, error) {
return desc.collectConstraintInfo(tableLookup)
}
// CheckUniqueConstraints returns a non-nil error if a descriptor contains two
// constraints with the same name.
func (desc *TableDescriptor) 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 *TableDescriptor) collectConstraintInfo(
tableLookup tableLookupFn,
) (map[string]ConstraintDetail, error) {
info := make(map[string]ConstraintDetail)
// Indexes provide PK, Unique and FK constraints.
indexes := desc.AllNonDropIndexes()
for _, index := range indexes {
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[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 := ConstraintDetail{Kind: 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 := ConstraintDetail{Kind: ConstraintTypeUnique}
detail.Columns = index.ColumnNames
detail.Index = index
info[index.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 := ConstraintDetail{Kind: ConstraintTypeFK}
// Constraints in the Validating state are considered Unvalidated for this purpose
detail.Unvalidated = fk.Validity != 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.Name, referencedColumnNames)
detail.ReferencedTable = other
}
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 := ConstraintDetail{Kind: ConstraintTypeCheck}
// Constraints in the Validating state are considered Unvalidated for this purpose
detail.Unvalidated = c.Validity != ConstraintValidity_Validated
detail.CheckConstraint = c
detail.Details = c.Expr
if tableLookup != nil {
colsUsed, err := c.ColumnsUsed(desc)
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.FindColumnByID(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.Name)
}
}
info[c.Name] = detail
}
return info, nil
}
// IsValidOriginIndex returns whether the index can serve as an origin index for a foreign
// key constraint with the provided set of originColIDs.
func (idx *IndexDescriptor) IsValidOriginIndex(originColIDs ColumnIDs) bool {
return ColumnIDs(idx.ColumnIDs).HasPrefix(originColIDs)
}
// IsValidReferencedIndex returns whether the index can serve as a referenced index for a foreign
// key constraint with the provided set of referencedColumnIDs.
func (idx *IndexDescriptor) IsValidReferencedIndex(referencedColIDs ColumnIDs) bool {
return idx.Unique && ColumnIDs(idx.ColumnIDs).Equals(referencedColIDs)
}
// FindFKReferencedIndex finds the first index in the supplied referencedTable
// that can satisfy a foreign key of the supplied column ids.
func FindFKReferencedIndex(
referencedTable *TableDescriptor, referencedColIDs ColumnIDs,
) (*IndexDescriptor, error) {
// Search for a unique index on the referenced table that matches our foreign
// key columns.
if referencedTable.PrimaryIndex.IsValidReferencedIndex(referencedColIDs) {
return &referencedTable.PrimaryIndex, nil
}
// If the PK doesn't match, find the index corresponding to the referenced column.
for i := range referencedTable.Indexes {
idx := &referencedTable.Indexes[i]
if idx.IsValidReferencedIndex(referencedColIDs) {
return idx, nil
}
}
return nil, pgerror.Newf(
pgcode.ForeignKeyViolation,
"there is no unique constraint matching given keys for referenced table %s",
referencedTable.Name,
)
}
// FindFKOriginIndex finds the first index in the supplied originTable
// that can satisfy an outgoing foreign key of the supplied column ids.
func FindFKOriginIndex(
originTable *TableDescriptor, originColIDs ColumnIDs,
) (*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
}
}
return nil, pgerror.Newf(
pgcode.ForeignKeyViolation,
"there is no index matching given keys for referenced table %s",
originTable.Name,
)
}
// 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 *MutableTableDescriptor, originColIDs ColumnIDs,
) (*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 == 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,
)
}
// ConditionalGetTableDescFromTxn validates that the supplied TableDescriptor
// matches the one currently stored in kv. This simulates a CPut and returns a
// ConditionFailedError on mismatch. We don't directly use CPut with protos
// because the marshaling is not guaranteed to be stable and also because it's
// sensitive to things like missing vs default values of fields.
func ConditionalGetTableDescFromTxn(
ctx context.Context, txn *kv.Txn, expectation *TableDescriptor,
) (*roachpb.Value, error) {
key := MakeDescMetadataKey(expectation.ID)
existingKV, err := txn.Get(ctx, key)
if err != nil {
return nil, err
}
var existing *Descriptor
if existingKV.Value != nil {
existing = &Descriptor{}
if err := existingKV.Value.GetProto(existing); err != nil {
return nil, errors.Wrapf(err,
"decoding current table descriptor value for id: %d", expectation.ID)
}
existing.Table(existingKV.Value.Timestamp)
}
wrapped := WrapDescriptor(expectation)
if !existing.Equal(wrapped) {
return nil, &roachpb.ConditionFailedError{ActualValue: existingKV.Value}
}
return existingKV.Value, nil
}