-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
structured.go
385 lines (320 loc) · 12.7 KB
/
structured.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
// 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 descpb
import (
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/errors"
)
// ToEncodingDirection converts a direction from the proto to an encoding.Direction.
func (dir IndexDescriptor_Direction) ToEncodingDirection() (encoding.Direction, error) {
switch dir {
case IndexDescriptor_ASC:
return encoding.Ascending, nil
case IndexDescriptor_DESC:
return encoding.Descending, nil
default:
return encoding.Ascending, errors.Errorf("invalid direction: %s", dir)
}
}
// ID, ColumnID, FamilyID, and IndexID are all uint32, but are each given a
// type alias to prevent accidental use of one of the types where
// another is expected.
// ID is a custom type for {Database,Table}Descriptor IDs.
type ID tree.ID
// SafeValue implements the redact.SafeValue interface.
func (ID) SafeValue() {}
// InvalidID is the uninitialised descriptor id.
const InvalidID ID = 0
// IDs is a sortable list of IDs.
type IDs []ID
func (ids IDs) Len() int { return len(ids) }
func (ids IDs) Less(i, j int) bool { return ids[i] < ids[j] }
func (ids IDs) Swap(i, j int) { ids[i], ids[j] = ids[j], ids[i] }
// FormatVersion is a custom type for TableDescriptor versions of the sql to
// key:value mapping.
//go:generate stringer -type=FormatVersion
type FormatVersion uint32
const (
_ FormatVersion = iota
// BaseFormatVersion corresponds to the encoding described in
// https://www.cockroachlabs.com/blog/sql-in-cockroachdb-mapping-table-data-to-key-value-storage/.
BaseFormatVersion
// FamilyFormatVersion corresponds to the encoding described in
// https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20151214_sql_column_families.md
FamilyFormatVersion
// InterleavedFormatVersion corresponds to the encoding described in
// https://github.com/cockroachdb/cockroach/blob/master/docs/RFCS/20160624_sql_interleaved_tables.md
InterleavedFormatVersion
)
// FamilyID is a custom type for ColumnFamilyDescriptor IDs.
type FamilyID uint32
// SafeValue implements the redact.SafeValue interface.
func (FamilyID) SafeValue() {}
// IndexID is a custom type for IndexDescriptor IDs.
type IndexID tree.IndexID
// SafeValue implements the redact.SafeValue interface.
func (IndexID) SafeValue() {}
// DescriptorVersion is a custom type for TableDescriptor Versions.
type DescriptorVersion uint32
// SafeValue implements the redact.SafeValue interface.
func (DescriptorVersion) SafeValue() {}
// IndexDescriptorVersion is a custom type for IndexDescriptor Versions.
type IndexDescriptorVersion uint32
// SafeValue implements the redact.SafeValue interface.
func (IndexDescriptorVersion) SafeValue() {}
const (
// BaseIndexFormatVersion corresponds to the original encoding of secondary indexes that
// don't respect table level column family definitions. We allow the 0 value of the type to
// have a value so that existing index descriptors are denoted as having the base format.
BaseIndexFormatVersion IndexDescriptorVersion = iota
// SecondaryIndexFamilyFormatVersion corresponds to the encoding of secondary indexes that
// use table level column family definitions.
SecondaryIndexFamilyFormatVersion
// EmptyArraysInInvertedIndexesVersion corresponds to the encoding of secondary indexes
// that is identical to SecondaryIndexFamilyFormatVersion, but also includes a key encoding
// for empty arrays in array inverted indexes.
EmptyArraysInInvertedIndexesVersion
)
// ColumnID is a custom type for ColumnDescriptor IDs.
type ColumnID tree.ColumnID
// SafeValue implements the redact.SafeValue interface.
func (ColumnID) SafeValue() {}
// ColumnIDs is a slice of ColumnDescriptor IDs.
type ColumnIDs []ColumnID
func (c ColumnIDs) Len() int { return len(c) }
func (c ColumnIDs) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c ColumnIDs) Less(i, j int) bool { return c[i] < c[j] }
// HasPrefix returns true if the input list is a prefix of this list.
func (c ColumnIDs) HasPrefix(input ColumnIDs) bool {
if len(input) > len(c) {
return false
}
for i := range input {
if input[i] != c[i] {
return false
}
}
return true
}
// Equals returns true if the input list is equal to this list.
func (c ColumnIDs) Equals(input ColumnIDs) bool {
if len(input) != len(c) {
return false
}
for i := range input {
if input[i] != c[i] {
return false
}
}
return true
}
// Contains returns whether this list contains the input ID.
func (c ColumnIDs) Contains(i ColumnID) bool {
for _, id := range c {
if i == id {
return true
}
}
return false
}
// IndexDescriptorEncodingType is a custom type to represent different encoding types
// for secondary indexes.
type IndexDescriptorEncodingType uint32
const (
// SecondaryIndexEncoding corresponds to the standard way of encoding secondary indexes
// as described in docs/tech-notes/encoding.md. We allow the 0 value of this type
// to have a value so that existing descriptors are encoding using this encoding.
SecondaryIndexEncoding IndexDescriptorEncodingType = iota
// PrimaryIndexEncoding corresponds to when a secondary index is encoded using the
// primary index encoding as described in docs/tech-notes/encoding.md.
PrimaryIndexEncoding
)
// Remove unused warning.
var _ = SecondaryIndexEncoding
// MutationID is a custom type for TableDescriptor mutations.
type MutationID uint32
// SafeValue implements the redact.SafeValue interface.
func (MutationID) SafeValue() {}
// InvalidMutationID is the uninitialised mutation id.
const InvalidMutationID MutationID = 0
// IsSet returns whether or not the foreign key actually references a table.
func (f ForeignKeyReference) IsSet() bool {
return f.Table != 0
}
// FindPartitionByName searches this partitioning descriptor for a partition
// whose name is the input and returns it, or nil if no match is found.
func (desc *PartitioningDescriptor) FindPartitionByName(name string) *PartitioningDescriptor {
for _, l := range desc.List {
if l.Name == name {
return desc
}
if s := l.Subpartitioning.FindPartitionByName(name); s != nil {
return s
}
}
for _, r := range desc.Range {
if r.Name == name {
return desc
}
}
return nil
}
// PartitionNames returns a slice containing the name of every partition and
// subpartition in an arbitrary order.
func (desc *PartitioningDescriptor) PartitionNames() []string {
var names []string
for _, l := range desc.List {
names = append(names, l.Name)
names = append(names, l.Subpartitioning.PartitionNames()...)
}
for _, r := range desc.Range {
names = append(names, r.Name)
}
return names
}
// Public implements the Descriptor interface.
func (desc *TableDescriptor) Public() bool {
return desc.State == DescriptorState_PUBLIC
}
// Offline returns true if the table is importing.
func (desc *TableDescriptor) Offline() bool {
return desc.State == DescriptorState_OFFLINE
}
// Dropped returns true if the table is being dropped.
func (desc *TableDescriptor) Dropped() bool {
return desc.State == DescriptorState_DROP
}
// Adding returns true if the table is being added.
func (desc *TableDescriptor) Adding() bool {
return desc.State == DescriptorState_ADD
}
// IsTable returns true if the TableDescriptor actually describes a
// Table resource, as opposed to a different resource (like a View).
func (desc *TableDescriptor) IsTable() bool {
return !desc.IsView() && !desc.IsSequence()
}
// IsView returns true if the TableDescriptor actually describes a
// View resource rather than a Table.
func (desc *TableDescriptor) IsView() bool {
return desc.ViewQuery != ""
}
// MaterializedView returns whether or not this TableDescriptor is a
// MaterializedView.
func (desc *TableDescriptor) MaterializedView() bool {
return desc.IsMaterializedView
}
// IsPhysicalTable returns true if the TableDescriptor actually describes a
// physical Table that needs to be stored in the kv layer, as opposed to a
// different resource like a view or a virtual table. Physical tables have
// primary keys, column families, and indexes (unlike virtual tables).
// Sequences count as physical tables because their values are stored in
// the KV layer.
func (desc *TableDescriptor) IsPhysicalTable() bool {
return desc.IsSequence() || (desc.IsTable() && !desc.IsVirtualTable()) || desc.MaterializedView()
}
// IsAs returns true if the TableDescriptor actually describes
// a Table resource with an As source.
func (desc *TableDescriptor) IsAs() bool {
return desc.CreateQuery != ""
}
// IsSequence returns true if the TableDescriptor actually describes a
// Sequence resource rather than a Table.
func (desc *TableDescriptor) IsSequence() bool {
return desc.SequenceOpts != nil
}
// IsVirtualTable returns true if the TableDescriptor describes a
// virtual Table (like the information_schema tables) and thus doesn't
// need to be physically stored.
func (desc *TableDescriptor) IsVirtualTable() bool {
return IsVirtualTable(desc.ID)
}
// Persistence returns the Persistence from the TableDescriptor.
func (desc *TableDescriptor) Persistence() tree.Persistence {
if desc.Temporary {
return tree.PersistenceTemporary
}
return tree.PersistencePermanent
}
// IsVirtualTable returns true if the TableDescriptor describes a
// virtual Table (like the information_schema tables) and thus doesn't
// need to be physically stored.
func IsVirtualTable(id ID) bool {
return catconstants.MinVirtualID <= id
}
// IsSystemConfigID returns whether this ID is for a system config object.
func IsSystemConfigID(id ID) bool {
return id > 0 && id <= keys.MaxSystemConfigDescID
}
// IsReservedID returns whether this ID is for any system object.
func IsReservedID(id ID) bool {
return id > 0 && id <= keys.MaxReservedDescID
}
// AnonymousTable is the empty table name, used when a data source
// has no own name, e.g. VALUES, subqueries or the empty source.
var AnonymousTable = tree.TableName{}
// HasOwner returns true if the sequence options indicate an owner exists.
func (opts *TableDescriptor_SequenceOpts) HasOwner() bool {
return !opts.SequenceOwner.Equal(TableDescriptor_SequenceOpts_SequenceOwner{})
}
// EffectiveCacheSize returns the CacheSize field of a sequence option with
// the exception that it will return 1 if the CacheSize field is 0.
// A cache size of 1 indicates that there is no caching. The returned value
// will always be greater than or equal to 1.
//
// Prior to #51259, sequence caching was unimplemented and cache sizes were
// left uninitialized (ie. to have a value of 0). If a sequence has a cache
// size of 0, it should be treated in the same was as sequences with cache
// sizes of 1.
func (opts *TableDescriptor_SequenceOpts) EffectiveCacheSize() int64 {
if opts.CacheSize == 0 {
return 1
}
return opts.CacheSize
}
// SafeValue implements the redact.SafeValue interface.
func (ConstraintValidity) SafeValue() {}
// SafeValue implements the redact.SafeValue interface.
func (DescriptorMutation_Direction) SafeValue() {}
// SafeValue implements the redact.SafeValue interface.
func (DescriptorMutation_State) SafeValue() {}
// SafeValue implements the redact.SafeValue interface.
func (DescriptorState) SafeValue() {}
// SafeValue implements the redact.SafeValue interface.
func (ConstraintType) SafeValue() {}
// UniqueConstraint is an interface for a unique constraint. It allows
// both UNIQUE indexes and UNIQUE WITHOUT INDEX constraints to serve as
// the referenced side of a foreign key constraint.
type UniqueConstraint interface {
// IsValidReferencedUniqueConstraint returns whether the unique constraint can
// serve as a referenced unique constraint for a foreign key constraint with the
// provided set of referencedColumnIDs.
IsValidReferencedUniqueConstraint(referencedColIDs ColumnIDs) bool
// GetName returns the constraint name.
GetName() string
}
var _ UniqueConstraint = &UniqueWithoutIndexConstraint{}
var _ UniqueConstraint = &IndexDescriptor{}
// IsValidReferencedUniqueConstraint is part of the UniqueConstraint interface.
func (u *UniqueWithoutIndexConstraint) IsValidReferencedUniqueConstraint(
referencedColIDs ColumnIDs,
) bool {
return ColumnIDs(u.ColumnIDs).Equals(referencedColIDs)
}
// GetName is part of the UniqueConstraint interface.
func (u *UniqueWithoutIndexConstraint) GetName() string {
return u.Name
}
// IsPartial returns true if the constraint is a partial unique constraint.
func (u *UniqueWithoutIndexConstraint) IsPartial() bool {
return u.Predicate != ""
}