-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
table.go
344 lines (289 loc) · 11.2 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
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package cat
import (
"bytes"
"context"
"fmt"
"time"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
)
// Table is an interface to a database table, exposing only the information
// needed by the query optimizer.
//
// Both columns and indexes are grouped into three sets: public, write-only, and
// delete-only. When a column or index is added or dropped, it proceeds through
// each of the three states as that schema change is incrementally rolled out to
// the cluster without blocking ongoing queries. In the public state, reads,
// writes, and deletes are allowed. In the write-only state, only writes and
// deletes are allowed. Finally, in the delete-only state, only deletes are
// allowed. Further details about "online schema change" can be found in:
//
// docs/RFCS/20151014_online_schema_change.md
//
// Calling code must take care to use the right collection of columns or
// indexes. Usually this should be the public collections, since most usages are
// read-only, but mutation operators generally need to consider non-public
// columns and indexes.
type Table interface {
DataSource
// IsVirtualTable returns true if this table is a special system table that
// constructs its rows "on the fly" when it's queried. An example is the
// information_schema tables.
IsVirtualTable() bool
// IsInterleaved returns true if any of this table's indexes are interleaved
// with index(es) from other table(s).
IsInterleaved() bool
// IsReferenced returns true if this table is referenced by at least one
// foreign key defined on another table (or this one if self-referential).
IsReferenced() bool
// ColumnCount returns the number of public columns in the table. Public
// columns are not currently being added or dropped from the table. This
// method should be used when mutation columns can be ignored (the common
// case).
ColumnCount() int
// WritableColumnCount returns the number of public and write-only columns in
// the table. Although write-only columns are not visible, any inserts and
// updates must still set them. WritableColumnCount is always >= ColumnCount.
WritableColumnCount() int
// DeletableColumnCount returns the number of public, write-only, and
// delete- only columns in the table. DeletableColumnCount is always >=
// WritableColumnCount.
DeletableColumnCount() int
// Column returns a Column interface to the column at the ith ordinal
// position within the table, where i < ColumnCount. Note that the Columns
// collection includes mutation columns, if present. Mutation columns are in
// the process of being added or dropped from the table, and may need to have
// default or computed values set when inserting or updating rows. See this
// RFC for more details:
//
// cockroachdb/cockroach/docs/RFCS/20151014_online_schema_change.md
//
// Writable columns are always situated after public columns, and are followed
// by deletable columns.
Column(i int) Column
// IndexCount returns the number of public indexes defined on this table.
// Public indexes are not currently being added or dropped from the table.
// This method should be used when mutation columns can be ignored (the common
// case). The returned indexes include the primary index, so the count is
// always >= 1.
IndexCount() int
// WritableIndexCount returns the number of public and write-only indexes
// defined on this table. Although write-only indexes are not visible, any
// table mutation operations must still be applied to them. WritableIndexCount
// is always >= IndexCount.
WritableIndexCount() int
// DeletableIndexCount returns the number of public, write-only, and
// delete-onlyindexes defined on this table. DeletableIndexCount is always
// >= WritableIndexCount.
DeletableIndexCount() int
// Index returns the ith index, where i < IndexCount. The table's primary
// index is always the 0th index, and is always present (use cat.PrimaryIndex
// to select it). The primary index corresponds to the table's primary key.
// If a primary key was not explicitly specified, then the system implicitly
// creates one based on a hidden rowid column.
Index(i int) Index
// StatisticCount returns the number of statistics available for the table.
StatisticCount() int
// Statistic returns the ith statistic, where i < StatisticCount.
Statistic(i int) TableStatistic
// CheckCount returns the number of check constraints present on the table.
CheckCount() int
// Check returns the ith check constraint, where i < CheckCount.
Check(i int) CheckConstraint
// FamilyCount returns the number of column families present on the table.
// There is always at least one primary family (always family 0) where columns
// go if they are not explicitly assigned to another family.
FamilyCount() int
// Family returns the interface for the ith column family, where
// i < FamilyCount.
Family(i int) Family
}
// CheckConstraint is the SQL text for a check constraint on a table. Check
// constraints are user-defined restrictions on the content of each row in a
// table. For example, this check constraint ensures that only values greater
// than zero can be inserted into the table:
//
// CREATE TABLE a (a INT CHECK (a > 0))
//
type CheckConstraint string
// TableStatistic is an interface to a table statistic. Each statistic is
// associated with a set of columns.
type TableStatistic interface {
// CreatedAt indicates when the statistic was generated.
CreatedAt() time.Time
// ColumnCount is the number of columns the statistic pertains to.
ColumnCount() int
// ColumnOrdinal returns the column ordinal (see Table.Column) of the ith
// column in this statistic, with 0 <= i < ColumnCount.
ColumnOrdinal(i int) int
// RowCount returns the estimated number of rows in the table.
RowCount() uint64
// DistinctCount returns the estimated number of distinct values on the
// columns of the statistic. If there are multiple columns, each "value" is a
// tuple with the values on each column. Rows where any statistic column have
// a NULL don't contribute to this count.
DistinctCount() uint64
// NullCount returns the estimated number of rows which have a NULL value on
// any column in the statistic.
NullCount() uint64
// TODO(radu): add Histogram().
}
// ForeignKeyReference is a struct representing an outbound foreign key reference.
// It has accessors for table and index IDs, as well as the prefix length.
type ForeignKeyReference struct {
// Table contains the referenced table's stable identifier.
TableID StableID
// Index contains the stable identifier of the index that represents the
// destination table's side of the foreign key relation.
IndexID StableID
// PrefixLen contains the length of columns that form the foreign key
// relation in the current and destination indexes.
PrefixLen int32
// Match contains the method used for comparing composite foreign keys.
Match tree.CompositeKeyMatchMethod
}
// FindTableColumnByName returns the ordinal of the non-mutation column having
// the given name, if one exists in the given table. Otherwise, it returns -1.
func FindTableColumnByName(tab Table, name tree.Name) int {
for ord, n := 0, tab.ColumnCount(); ord < n; ord++ {
if tab.Column(ord).ColName() == name {
return ord
}
}
return -1
}
// FormatCatalogTable nicely formats a catalog table using a treeprinter for
// debugging and testing.
func FormatCatalogTable(cat Catalog, tab Table, tp treeprinter.Node) {
child := tp.Childf("TABLE %s", tab.Name().TableName)
var buf bytes.Buffer
for i := 0; i < tab.DeletableColumnCount(); i++ {
buf.Reset()
formatColumn(tab.Column(i), IsMutationColumn(tab, i), &buf)
child.Child(buf.String())
}
for i := 0; i < tab.DeletableIndexCount(); i++ {
formatCatalogIndex(tab, i, child)
}
for i := 0; i < tab.IndexCount(); i++ {
fkRef, ok := tab.Index(i).ForeignKey()
if ok {
formatCatalogFKRef(cat, tab.Index(i), fkRef, child)
}
}
for i := 0; i < tab.CheckCount(); i++ {
child.Childf("CHECK (%s)", tab.Check(i))
}
// Don't print the primary family, since it's implied.
if tab.FamilyCount() > 1 || tab.Family(0).Name() != "primary" {
for i := 0; i < tab.FamilyCount(); i++ {
buf.Reset()
formatFamily(tab.Family(i), &buf)
child.Child(buf.String())
}
}
}
// formatCatalogIndex nicely formats a catalog index using a treeprinter for
// debugging and testing.
func formatCatalogIndex(tab Table, ord int, tp treeprinter.Node) {
idx := tab.Index(ord)
inverted := ""
if idx.IsInverted() {
inverted = "INVERTED "
}
mutation := ""
if IsMutationIndex(tab, ord) {
mutation = " (mutation)"
}
child := tp.Childf("%sINDEX %s%s", inverted, idx.Name(), mutation)
var buf bytes.Buffer
colCount := idx.ColumnCount()
if ord == PrimaryIndex {
// Omit the "stored" columns from the primary index.
colCount = idx.KeyColumnCount()
}
for i := 0; i < colCount; i++ {
buf.Reset()
idxCol := idx.Column(i)
formatColumn(idxCol.Column, false /* isMutationCol */, &buf)
if idxCol.Descending {
fmt.Fprintf(&buf, " desc")
}
if i >= idx.LaxKeyColumnCount() {
fmt.Fprintf(&buf, " (storing)")
}
child.Child(buf.String())
}
}
// formatColPrefix returns a string representation of the first prefixLen columns of idx.
func formatColPrefix(idx Index, prefixLen int) string {
var buf bytes.Buffer
buf.WriteByte('(')
for i := 0; i < prefixLen; i++ {
if i > 0 {
buf.WriteString(", ")
}
colName := idx.Column(i).ColName()
buf.WriteString(colName.String())
}
buf.WriteByte(')')
return buf.String()
}
// formatCatalogFKRef nicely formats a catalog foreign key reference using a
// treeprinter for debugging and testing.
func formatCatalogFKRef(cat Catalog, idx Index, fkRef ForeignKeyReference, tp treeprinter.Node) {
ds, err := cat.ResolveDataSourceByID(context.TODO(), fkRef.TableID)
if err != nil {
panic(err)
}
fkTable := ds.(Table)
var fkIndex Index
for j, cnt := 0, fkTable.IndexCount(); j < cnt; j++ {
if fkTable.Index(j).ID() == fkRef.IndexID {
fkIndex = fkTable.Index(j)
break
}
}
tp.Childf(
"FOREIGN KEY %s REFERENCES %v %s",
formatColPrefix(idx, int(fkRef.PrefixLen)),
ds.Name(),
formatColPrefix(fkIndex, int(fkRef.PrefixLen)),
)
}
func formatColumn(col Column, isMutationCol bool, buf *bytes.Buffer) {
fmt.Fprintf(buf, "%s %s", col.ColName(), col.DatumType())
if !col.IsNullable() {
fmt.Fprintf(buf, " not null")
}
if col.IsHidden() {
fmt.Fprintf(buf, " (hidden)")
}
if isMutationCol {
fmt.Fprintf(buf, " (mutation)")
}
}
func formatFamily(family Family, buf *bytes.Buffer) {
fmt.Fprintf(buf, "FAMILY %s (", family.Name())
for i, n := 0, family.ColumnCount(); i < n; i++ {
if i != 0 {
buf.WriteString(", ")
}
col := family.Column(i)
buf.WriteString(string(col.ColName()))
}
buf.WriteString(")")
}