-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathinformation_schema.go
397 lines (365 loc) · 12.2 KB
/
information_schema.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
// Copyright 2016 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.
//
// Author: Nathan VanBenschoten ([email protected])
package sql
import (
"sort"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/sqlbase"
"github.com/pkg/errors"
)
var informationSchema = virtualSchema{
name: "information_schema",
tables: []virtualSchemaTable{
informationSchemaColumnsTable,
informationSchemaSchemataTable,
informationSchemaTableConstraintTable,
informationSchemaTablesTable,
},
}
var (
// defString is used as the value for columns included in the sql standard
// of information_schema that don't make sense for CockroachDB. This is
// identical to the behavior of MySQL.
defString = parser.NewDString("def")
// information_schema was defined before the BOOLEAN data type was added to
// the SQL specification. Because of this, boolean values are represented
// as strings.
yesString = parser.NewDString("YES")
noString = parser.NewDString("NO")
)
func yesOrNoDatum(b bool) parser.Datum {
if b {
return yesString
}
return noString
}
func dStringOrNull(s string) parser.Datum {
if s == "" {
return parser.DNull
}
return parser.NewDString(s)
}
func dStringPtrOrNull(s *string) parser.Datum {
if s == nil {
return parser.DNull
}
return parser.NewDString(*s)
}
func dIntFnOrNull(fn func() (int32, bool)) parser.Datum {
if n, ok := fn(); ok {
return parser.NewDInt(parser.DInt(n))
}
return parser.DNull
}
var informationSchemaColumnsTable = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.columns (
TABLE_CATALOG STRING NOT NULL DEFAULT '',
TABLE_SCHEMA STRING NOT NULL DEFAULT '',
TABLE_NAME STRING NOT NULL DEFAULT '',
COLUMN_NAME STRING NOT NULL DEFAULT '',
ORDINAL_POSITION INT NOT NULL DEFAULT 0,
COLUMN_DEFAULT STRING,
IS_NULLABLE STRING NOT NULL DEFAULT '',
DATA_TYPE STRING NOT NULL DEFAULT '',
CHARACTER_MAXIMUM_LENGTH INT,
CHARACTER_OCTET_LENGTH INT,
NUMERIC_PRECISION INT,
NUMERIC_SCALE INT,
DATETIME_PRECISION INT
);
`,
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
// Table descriptors already holds columns in-order.
visible := 0
for _, column := range table.Columns {
if column.Hidden {
continue
}
visible++
if err := addRow(
defString, // table_catalog
parser.NewDString(db.Name), // table_schema
parser.NewDString(table.Name), // table_name
parser.NewDString(column.Name), // column_name
parser.NewDInt(parser.DInt(visible)), // ordinal_position, 1-indexed
dStringPtrOrNull(column.DefaultExpr), // column_default
yesOrNoDatum(column.Nullable), // is_nullable
parser.NewDString(column.Type.Kind.String()), // data_type
characterMaximumLength(column.Type), // character_maximum_length
characterOctetLength(column.Type), // character_octet_length
numericPrecision(column.Type), // numeric_precision
numericScale(column.Type), // numeric_scale
datetimePrecision(column.Type), // datetime_precision
); err != nil {
return err
}
}
return nil
},
)
},
}
func characterMaximumLength(colType sqlbase.ColumnType) parser.Datum {
return dIntFnOrNull(colType.MaxCharacterLength)
}
func characterOctetLength(colType sqlbase.ColumnType) parser.Datum {
return dIntFnOrNull(colType.MaxOctetLength)
}
func numericPrecision(colType sqlbase.ColumnType) parser.Datum {
return dIntFnOrNull(colType.NumericPrecision)
}
func numericScale(colType sqlbase.ColumnType) parser.Datum {
return dIntFnOrNull(colType.NumericScale)
}
func datetimePrecision(colType sqlbase.ColumnType) parser.Datum {
// We currently do not support a datetime precision.
return parser.DNull
}
var informationSchemaSchemataTable = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.schemata (
CATALOG_NAME STRING NOT NULL DEFAULT '',
SCHEMA_NAME STRING NOT NULL DEFAULT '',
DEFAULT_CHARACTER_SET_NAME STRING NOT NULL DEFAULT '',
SQL_PATH STRING
);`,
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachDatabaseDesc(p, func(db *sqlbase.DatabaseDescriptor) error {
return addRow(
defString, // catalog_name
parser.NewDString(db.Name), // schema_name
parser.DNull, // default_character_set_name
parser.DNull, // sql_path
)
})
},
}
var (
constraintTypeCheck = parser.NewDString("CHECK")
constraintTypeForeignKey = parser.NewDString("FOREIGN KEY")
constraintTypePrimaryKey = parser.NewDString("PRIMARY KEY")
constraintTypeUnique = parser.NewDString("UNIQUE")
)
// TODO(dt): switch using common GetConstraintInfo helper.
var informationSchemaTableConstraintTable = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.table_constraints (
CONSTRAINT_CATALOG STRING NOT NULL DEFAULT '',
CONSTRAINT_SCHEMA STRING NOT NULL DEFAULT '',
CONSTRAINT_NAME STRING NOT NULL DEFAULT '',
TABLE_SCHEMA STRING NOT NULL DEFAULT '',
TABLE_NAME STRING NOT NULL DEFAULT '',
CONSTRAINT_TYPE STRING NOT NULL DEFAULT ''
);`,
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
type constraint struct {
name string
typ parser.Datum
}
var constraints []constraint
if table.HasPrimaryKey() {
constraints = append(constraints, constraint{
name: table.PrimaryIndex.Name,
typ: constraintTypePrimaryKey,
})
}
for _, index := range table.Indexes {
c := constraint{name: index.Name}
switch {
case index.Unique:
c.typ = constraintTypeUnique
constraints = append(constraints, c)
case index.ForeignKey.IsSet():
c.typ = constraintTypeForeignKey
constraints = append(constraints, c)
}
}
for _, check := range table.Checks {
constraints = append(constraints, constraint{
name: check.Name,
typ: constraintTypeCheck,
})
}
for _, c := range constraints {
if err := addRow(
defString, // constraint_catalog
parser.NewDString(db.Name), // constraint_schema
dStringOrNull(c.name), // constraint_name
parser.NewDString(db.Name), // table_schema
parser.NewDString(table.Name), // table_name
c.typ, // constraint_type
); err != nil {
return err
}
}
return nil
},
)
},
}
var (
tableTypeSystemView = parser.NewDString("SYSTEM VIEW")
tableTypeBaseTable = parser.NewDString("BASE TABLE")
)
var informationSchemaTablesTable = virtualSchemaTable{
schema: `
CREATE TABLE information_schema.tables (
TABLE_CATALOG STRING NOT NULL DEFAULT '',
TABLE_SCHEMA STRING NOT NULL DEFAULT '',
TABLE_NAME STRING NOT NULL DEFAULT '',
TABLE_TYPE STRING NOT NULL DEFAULT '',
VERSION INT
);`,
populate: func(p *planner, addRow func(...parser.Datum) error) error {
return forEachTableDesc(p,
func(db *sqlbase.DatabaseDescriptor, table *sqlbase.TableDescriptor) error {
tableType := tableTypeBaseTable
if isVirtualDescriptor(table) {
tableType = tableTypeSystemView
}
return addRow(
defString, // table_catalog
parser.NewDString(db.Name), // table_schema
parser.NewDString(table.Name), // table_name
tableType, // table_type
parser.NewDInt(parser.DInt(table.Version)), // version
)
},
)
},
}
type sortedDBDescs []*sqlbase.DatabaseDescriptor
// sortedDBDescs implements sort.Interface. It sorts a slice of DatabaseDescriptors
// by the database name.
func (dbs sortedDBDescs) Len() int { return len(dbs) }
func (dbs sortedDBDescs) Swap(i, j int) { dbs[i], dbs[j] = dbs[j], dbs[i] }
func (dbs sortedDBDescs) Less(i, j int) bool { return dbs[i].Name < dbs[j].Name }
// forEachTableDesc retrieves all database descriptors and iterates through them in
// lexicographical order with respect to their name. For each database, the function
// will call fn with its descriptor.
func forEachDatabaseDesc(
p *planner,
fn func(*sqlbase.DatabaseDescriptor) error,
) error {
// Handle real schemas
dbDescs, err := p.getAllDatabaseDescs()
if err != nil {
return err
}
// Handle virtual schemas.
for _, schema := range virtualSchemaMap {
dbDescs = append(dbDescs, schema.desc)
}
sort.Sort(sortedDBDescs(dbDescs))
for _, db := range dbDescs {
if userCanSeeDatabase(db, p.session.User) {
if err := fn(db); err != nil {
return err
}
}
}
return nil
}
// forEachTableDesc retrieves all table descriptors and iterates through them in
// lexicographical order with respect primarily to database name and secondarily
// to table name. For each table, the function will call fn with its respective
// database and table descriptor.
func forEachTableDesc(
p *planner,
fn func(*sqlbase.DatabaseDescriptor, *sqlbase.TableDescriptor) error,
) error {
type dbDescTables struct {
desc *sqlbase.DatabaseDescriptor
tables map[string]*sqlbase.TableDescriptor
}
databases := make(map[string]dbDescTables)
// Handle real schemas.
descs, err := p.getAllDescriptors()
if err != nil {
return err
}
dbIDsToName := make(map[sqlbase.ID]string)
// First, iterate through all database descriptors, constructing dbDescTables
// objects and populating a mapping from sqlbase.ID to database name.
for _, desc := range descs {
if db, ok := desc.(*sqlbase.DatabaseDescriptor); ok {
dbIDsToName[db.GetID()] = db.GetName()
databases[db.GetName()] = dbDescTables{
desc: db,
tables: make(map[string]*sqlbase.TableDescriptor),
}
}
}
// Next, iterate through all table descriptors, using the mapping from sqlbase.ID
// to database name to add descriptors to a dbDescTables' tables map.
for _, desc := range descs {
if table, ok := desc.(*sqlbase.TableDescriptor); ok {
dbName, ok := dbIDsToName[table.GetParentID()]
if !ok {
return errors.Errorf("no database with ID %d found", table.GetParentID())
}
dbTables := databases[dbName]
dbTables.tables[table.GetName()] = table
}
}
// Handle virtual schemas.
for dbName, schema := range virtualSchemaMap {
dbTables := make(map[string]*sqlbase.TableDescriptor, len(schema.tables))
for tableName, entry := range schema.tables {
dbTables[tableName] = entry.desc
}
databases[dbName] = dbDescTables{
desc: schema.desc,
tables: dbTables,
}
}
// Below we use the same trick twice of sorting a slice of strings lexicographically
// and iterating through these strings to index into a map. Effectively, this allows
// us to iterate through a map in sorted order.
dbNames := make([]string, 0, len(databases))
for dbName := range databases {
dbNames = append(dbNames, dbName)
}
sort.Strings(dbNames)
for _, dbName := range dbNames {
db := databases[dbName]
dbTableNames := make([]string, 0, len(db.tables))
for tableName := range db.tables {
dbTableNames = append(dbTableNames, tableName)
}
sort.Strings(dbTableNames)
for _, tableName := range dbTableNames {
tableDesc := db.tables[tableName]
if userCanSeeTable(tableDesc, p.session.User) {
if err := fn(db.desc, tableDesc); err != nil {
return err
}
}
}
}
return nil
}
func userCanSeeDatabase(db *sqlbase.DatabaseDescriptor, user string) bool {
return userCanSeeDescriptor(db, user)
}
func userCanSeeTable(table *sqlbase.TableDescriptor, user string) bool {
return userCanSeeDescriptor(table, user) && table.State == sqlbase.TableDescriptor_PUBLIC
}