-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
virtual_schema.go
733 lines (653 loc) · 25.1 KB
/
virtual_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
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// Copyright 2016 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 sql
import (
"context"
"math"
"sort"
"time"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/opt/constraint"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
)
//
// Programmer interface to define virtual schemas.
//
// virtualSchema represents a database with a set of virtual tables. Virtual
// tables differ from standard tables in that they are not persisted to storage,
// and instead their contents are populated whenever they are queried.
//
// The virtual database and its virtual tables also differ from standard databases
// and tables in that their descriptors are not distributed, but instead live statically
// in code. This means that they are accessed separately from standard descriptors.
type virtualSchema struct {
name string
allTableNames map[string]struct{}
tableDefs map[sqlbase.ID]virtualSchemaDef
tableValidator func(*sqlbase.TableDescriptor) error // optional
// Some virtual tables can be used if there is no current database set; others can't.
validWithNoDatabaseContext bool
// Some virtual schemas (like pg_catalog) contain types that we can resolve.
containsTypes bool
}
// virtualSchemaDef represents the interface of a table definition within a virtualSchema.
type virtualSchemaDef interface {
getSchema() string
initVirtualTableDesc(
ctx context.Context, st *cluster.Settings, parentSchemaID, id sqlbase.ID,
) (sqlbase.TableDescriptor, error)
getComment() string
}
type virtualIndex struct {
// populate populates the table given the constraint. matched is true if any
// rows were generated.
populate func(ctx context.Context, constraint tree.Datum, p *planner, db *sqlbase.ImmutableDatabaseDescriptor,
addRow func(...tree.Datum) error,
) (matched bool, err error)
// partial is true if the virtual index isn't able to satisfy all constraints.
// For example, the pg_class table contains both indexes and tables. Tables
// can be looked up via a virtual index, since we can look up their descriptor
// by their ID directly. But indexes can't - they're hashed identifiers with
// no actual index. So we mark this index as partial, and if we get no match
// during populate, we'll fall back on populating the entire table.
partial bool
}
// virtualSchemaTable represents a table within a virtualSchema.
type virtualSchemaTable struct {
// Exactly one of the populate and generator fields should be defined for
// each virtualSchemaTable.
schema string
// comment represents comment of virtual schema table.
comment string
// populate, if non-nil, is a function that is used when creating a
// valuesNode. This function eagerly loads every row of the virtual table
// during initialization of the valuesNode.
populate func(ctx context.Context, p *planner, db *sqlbase.ImmutableDatabaseDescriptor, addRow func(...tree.Datum) error) error
// indexes, if non empty, is a slice of populate methods that also take a
// constraint, only generating rows that match the constraint. The order of
// indexes must match the order of the index definitions in the virtual table's
// schema.
indexes []virtualIndex
// generator, if non-nil, is a function that is used when creating a
// virtualTableNode. This function returns a virtualTableGenerator function
// which generates the next row of the virtual table when called.
generator func(ctx context.Context, p *planner, db *sqlbase.ImmutableDatabaseDescriptor) (virtualTableGenerator, cleanupFunc, error)
}
// virtualSchemaView represents a view within a virtualSchema
type virtualSchemaView struct {
schema string
resultColumns sqlbase.ResultColumns
}
// getSchema is part of the virtualSchemaDef interface.
func (t virtualSchemaTable) getSchema() string {
return t.schema
}
// initVirtualTableDesc is part of the virtualSchemaDef interface.
func (t virtualSchemaTable) initVirtualTableDesc(
ctx context.Context, st *cluster.Settings, parentSchemaID, id sqlbase.ID,
) (sqlbase.TableDescriptor, error) {
stmt, err := parser.ParseOne(t.schema)
if err != nil {
return sqlbase.TableDescriptor{}, err
}
create := stmt.AST.(*tree.CreateTable)
var firstColDef *tree.ColumnTableDef
for _, def := range create.Defs {
if d, ok := def.(*tree.ColumnTableDef); ok {
if d.HasDefaultExpr() {
return sqlbase.TableDescriptor{},
errors.Errorf("virtual tables are not allowed to use default exprs "+
"because bootstrapping: %s:%s", &create.Table, d.Name)
}
if firstColDef == nil {
firstColDef = d
}
}
if _, ok := def.(*tree.UniqueConstraintTableDef); ok {
return sqlbase.TableDescriptor{},
errors.Errorf("virtual tables are not allowed to have unique constraints")
}
}
if firstColDef == nil {
return sqlbase.TableDescriptor{},
errors.Errorf("can't have empty virtual tables")
}
// Virtual tables never use SERIAL so we need not process SERIAL
// types here.
mutDesc, err := MakeTableDesc(
ctx,
nil, /* txn */
nil, /* vs */
st,
create,
0, /* parentID */
parentSchemaID,
id,
startTime, /* creationTime */
publicSelectPrivileges,
nil, /* affected */
nil, /* semaCtx */
nil, /* evalCtx */
&sessiondata.SessionData{}, /* sessionData */
false, /* temporary */
)
if err != nil {
return mutDesc.TableDescriptor, err
}
for i := range mutDesc.Indexes {
idx := &mutDesc.Indexes[i]
if len(idx.ColumnIDs) > 1 {
panic("we don't know how to deal with virtual composite indexes yet")
}
// All indexes of virtual tables automatically STORE all other columns in
// the table.
idx.StoreColumnIDs = make([]sqlbase.ColumnID, len(mutDesc.Columns)-len(idx.ColumnIDs))
idx.StoreColumnNames = make([]string, len(mutDesc.Columns)-len(idx.ColumnIDs))
// Store all columns but the ones in the index.
outputIdx := 0
EACHCOLUMN:
for j := range mutDesc.Columns {
for _, id := range idx.ColumnIDs {
if mutDesc.Columns[j].ID == id {
// Skip columns in the index.
continue EACHCOLUMN
}
}
idx.StoreColumnIDs[outputIdx] = mutDesc.Columns[j].ID
idx.StoreColumnNames[outputIdx] = mutDesc.Columns[j].Name
outputIdx++
}
}
return mutDesc.TableDescriptor, nil
}
// getComment is part of the virtualSchemaDef interface.
func (t virtualSchemaTable) getComment() string {
return t.comment
}
// getIndex returns the virtual index with the input ID.
func (t virtualSchemaTable) getIndex(id sqlbase.IndexID) *virtualIndex {
// Subtract 2 from the index id to get the ordinal in def.indexes, since
// the index with ID 1 is the "primary" index defined by def.populate.
return &t.indexes[id-2]
}
// getSchema is part of the virtualSchemaDef interface.
func (v virtualSchemaView) getSchema() string {
return v.schema
}
// initVirtualTableDesc is part of the virtualSchemaDef interface.
func (v virtualSchemaView) initVirtualTableDesc(
ctx context.Context, st *cluster.Settings, parentSchemaID sqlbase.ID, id sqlbase.ID,
) (sqlbase.TableDescriptor, error) {
stmt, err := parser.ParseOne(v.schema)
if err != nil {
return sqlbase.TableDescriptor{}, err
}
create := stmt.AST.(*tree.CreateView)
columns := v.resultColumns
if len(create.ColumnNames) != 0 {
columns = overrideColumnNames(columns, create.ColumnNames)
}
mutDesc, err := makeViewTableDesc(
ctx,
create.Name.Table(),
tree.AsStringWithFlags(create.AsSource, tree.FmtParsable),
0, /* parentID */
parentSchemaID,
id,
columns,
startTime, /* creationTime */
publicSelectPrivileges,
nil, /* semaCtx */
nil, /* evalCtx */
false, /* temporary */
)
return mutDesc.TableDescriptor, err
}
// getComment is part of the virtualSchemaDef interface.
func (v virtualSchemaView) getComment() string {
return ""
}
// virtualSchemas holds a slice of statically registered virtualSchema objects.
//
// When adding a new virtualSchema, define a virtualSchema in a separate file, and
// add that object to this slice.
var virtualSchemas = map[sqlbase.ID]virtualSchema{
sqlbase.InformationSchemaID: informationSchema,
sqlbase.PgCatalogID: pgCatalog,
sqlbase.CrdbInternalID: crdbInternal,
sqlbase.PgExtensionSchemaID: pgExtension,
}
var startTime = hlc.Timestamp{
WallTime: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
}
//
// SQL-layer interface to work with virtual schemas.
//
// VirtualSchemaHolder is a type used to provide convenient access to virtual
// database and table descriptors. VirtualSchemaHolder, virtualSchemaEntry,
// and virtualDefEntry make up the generated data structure which the
// virtualSchemas slice is mapped to. Because of this, they should not be
// created directly, but instead will be populated in a post-startup hook
// on an Executor.
type VirtualSchemaHolder struct {
entries map[string]virtualSchemaEntry
defsByID map[sqlbase.ID]*virtualDefEntry
orderedNames []string
}
// GetVirtualSchema makes VirtualSchemaHolder implement schema.VirtualSchemas.
func (vs *VirtualSchemaHolder) GetVirtualSchema(schemaName string) (catalog.VirtualSchema, bool) {
virtualSchema, ok := vs.entries[schemaName]
return virtualSchema, ok
}
var _ catalog.VirtualSchemas = (*VirtualSchemaHolder)(nil)
type virtualSchemaEntry struct {
// TODO(ajwerner): Use a sqlbase.SchemaDescriptor here as part of the
// user-defined schema work.
desc *sqlbase.ImmutableDatabaseDescriptor
defs map[string]virtualDefEntry
orderedDefNames []string
allTableNames map[string]struct{}
containsTypes bool
}
func (v virtualSchemaEntry) Desc() catalog.Descriptor {
return v.desc
}
func (v virtualSchemaEntry) NumTables() int {
return len(v.defs)
}
func (v virtualSchemaEntry) VisitTables(f func(object catalog.VirtualObject)) {
for _, name := range v.orderedDefNames {
f(v.defs[name])
}
}
func (v virtualSchemaEntry) GetObjectByName(
name string, flags tree.ObjectLookupFlags,
) (catalog.VirtualObject, error) {
switch flags.DesiredObjectKind {
case tree.TableObject:
if def, ok := v.defs[name]; ok {
if flags.RequireMutable {
return mutableVirtualDefEntry{desc: def.desc}, nil
}
return &def, nil
}
if _, ok := v.allTableNames[name]; ok {
return nil, unimplemented.Newf(v.desc.GetName()+"."+name,
"virtual schema table not implemented: %s.%s", v.desc.GetName(), name)
}
return nil, nil
case tree.TypeObject:
if !v.containsTypes {
return nil, nil
}
// Currently, we don't allow creation of types in virtual schemas, so
// the only types present in the virtual schemas that have types (i.e.
// pg_catalog) are types that are known at parse time. So, attempt to
// parse the input object as a statically known type. Note that an
// invalid input type like "notatype" will be parsed successfully as
// a ResolvableTypeReference, so the error here does not need to be
// intercepted and inspected.
typRef, err := parser.ParseType(name)
if err != nil {
return nil, err
}
// If the parsed reference is actually a statically known type, then
// we can return it. We return a simple wrapping of this type as
// TypeDescriptor that represents an alias of the result type.
typ, ok := tree.GetStaticallyKnownType(typRef)
if !ok {
return nil, nil
}
return virtualTypeEntry{
desc: sqlbase.MakeSimpleAliasTypeDescriptor(typ),
mutable: flags.RequireMutable,
}, nil
default:
return nil, errors.AssertionFailedf("unknown desired object kind %d", flags.DesiredObjectKind)
}
}
type virtualDefEntry struct {
virtualDef virtualSchemaDef
desc *sqlbase.TableDescriptor
comment string
validWithNoDatabaseContext bool
}
func (e virtualDefEntry) Desc() catalog.Descriptor {
return sqlbase.NewImmutableTableDescriptor(*e.desc)
}
type mutableVirtualDefEntry struct {
desc *sqlbase.TableDescriptor
}
func (e mutableVirtualDefEntry) Desc() catalog.Descriptor {
return sqlbase.NewMutableExistingTableDescriptor(*e.desc)
}
type virtualTypeEntry struct {
desc *sqlbase.ImmutableTypeDescriptor
mutable bool
}
func (e virtualTypeEntry) Desc() catalog.Descriptor {
// TODO(ajwerner): Should this be allowed? I think no. Let's just store an
// ImmutableTypeDesc off of this thing.
return e.desc
}
type virtualTableConstructor func(context.Context, *planner, string) (planNode, error)
var errInvalidDbPrefix = errors.WithHint(
pgerror.New(pgcode.UndefinedObject,
"cannot access virtual schema in anonymous database"),
"verify that the current database is set")
func newInvalidVirtualSchemaError() error {
return errors.AssertionFailedf("virtualSchema cannot have both the populate and generator functions defined")
}
func newInvalidVirtualDefEntryError() error {
return errors.AssertionFailedf("virtualDefEntry.virtualDef must be a virtualSchemaTable")
}
func (e virtualDefEntry) validateRow(datums tree.Datums, columns sqlbase.ResultColumns) error {
if r, c := len(datums), len(columns); r != c {
return errors.AssertionFailedf("datum row count and column count differ: %d vs %d", r, c)
}
for i := range columns {
col := &columns[i]
datum := datums[i]
if datum == tree.DNull {
if !e.desc.Columns[i].Nullable {
return errors.AssertionFailedf("column %s.%s not nullable, but found NULL value",
e.desc.Name, col.Name)
}
} else if !datum.ResolvedType().Equivalent(col.Typ) {
return errors.AssertionFailedf("datum column %q expected to be type %s; found type %s",
col.Name, col.Typ, datum.ResolvedType())
}
}
return nil
}
// getPlanInfo returns the column metadata and a constructor for a new
// valuesNode for the virtual table. We use deferred construction here
// so as to avoid populating a RowContainer during query preparation,
// where we can't guarantee it will be Close()d in case of error.
func (e virtualDefEntry) getPlanInfo(
table *sqlbase.TableDescriptor,
index *sqlbase.IndexDescriptor,
idxConstraint *constraint.Constraint,
) (sqlbase.ResultColumns, virtualTableConstructor) {
var columns sqlbase.ResultColumns
for i := range e.desc.Columns {
col := &e.desc.Columns[i]
columns = append(columns, sqlbase.ResultColumn{
Name: col.Name,
Typ: col.Type,
TableID: table.GetID(),
PGAttributeNum: col.GetLogicalColumnID(),
})
}
constructor := func(ctx context.Context, p *planner, dbName string) (planNode, error) {
var dbDesc *sqlbase.ImmutableDatabaseDescriptor
if dbName != "" {
dbDescI, err := p.LogicalSchemaAccessor().GetDatabaseDesc(ctx, p.txn, p.ExecCfg().Codec,
dbName, tree.DatabaseLookupFlags{Required: true, AvoidCached: p.avoidCachedDescriptors})
if err != nil {
return nil, err
}
dbDesc = dbDescI.(*sqlbase.ImmutableDatabaseDescriptor)
} else {
if !e.validWithNoDatabaseContext {
return nil, errInvalidDbPrefix
}
}
switch def := e.virtualDef.(type) {
case virtualSchemaTable:
if def.generator != nil && def.populate != nil {
return nil, newInvalidVirtualSchemaError()
}
if def.generator != nil {
next, cleanup, err := def.generator(ctx, p, dbDesc)
if err != nil {
return nil, err
}
return p.newVirtualTableNode(columns, next, cleanup), nil
}
constrainedScan := idxConstraint != nil && !idxConstraint.IsUnconstrained()
if !constrainedScan {
generator, cleanup := setupGenerator(ctx, func(pusher rowPusher) error {
return def.populate(ctx, p, dbDesc, func(row ...tree.Datum) error {
if err := e.validateRow(row, columns); err != nil {
return err
}
return pusher.pushRow(row...)
})
})
return p.newVirtualTableNode(columns, generator, cleanup), nil
}
// We are now dealing with a constrained virtual index scan.
if index.ID == 1 {
return nil, errors.AssertionFailedf(
"programming error: can't constrain scan on primary virtual index of table %s", e.desc.Name)
}
// Figure out the ordinal position of the column that we're filtering on.
columnIdxMap := table.ColumnIdxMap()
indexKeyDatums := make([]tree.Datum, len(index.ColumnIDs))
generator, cleanup := setupGenerator(ctx, e.makeConstrainedRowsGenerator(
ctx, p, dbDesc, index, indexKeyDatums, columnIdxMap, idxConstraint, columns))
return p.newVirtualTableNode(columns, generator, cleanup), nil
default:
return nil, newInvalidVirtualDefEntryError()
}
}
return columns, constructor
}
// makeConstrainedRowsGenerator returns a generator function that can be invoked
// to push all rows from this virtual table that satisfy the input index
// constraint to a row pusher that's supplied to the generator function.
func (e virtualDefEntry) makeConstrainedRowsGenerator(
ctx context.Context,
p *planner,
dbDesc *sqlbase.ImmutableDatabaseDescriptor,
index *sqlbase.IndexDescriptor,
indexKeyDatums []tree.Datum,
columnIdxMap map[sqlbase.ColumnID]int,
idxConstraint *constraint.Constraint,
columns sqlbase.ResultColumns,
) func(pusher rowPusher) error {
def := e.virtualDef.(virtualSchemaTable)
return func(pusher rowPusher) error {
var span constraint.Span
addRowIfPassesFilter := func(datums ...tree.Datum) error {
for i, id := range index.ColumnIDs {
indexKeyDatums[i] = datums[columnIdxMap[id]]
}
// Construct a single key span out of the current row, so that
// we can test it for containment within the constraint span of the
// filter that we're applying. The results of this containment check
// will tell us whether or not to let the current row pass the filter.
key := constraint.MakeCompositeKey(indexKeyDatums...)
span.Init(key, constraint.IncludeBoundary, key, constraint.IncludeBoundary)
var err error
if idxConstraint.ContainsSpan(p.EvalContext(), &span) {
if err := e.validateRow(datums, columns); err != nil {
return err
}
return pusher.pushRow(datums...)
}
return err
}
// We have a virtual index with a constraint. Run the constrained
// populate routine for every span. If for some reason we can't use the
// index for a given span, we exit the loop early and run a "full scan"
// over the virtual table, filtering the output using the remaining
// spans.
// N.B. we count down in this loop so that, if we have to give up half
// way through, we can easily truncate the spans we already processed
// from the end and use them as a filter for the remaining rows of the
// table.
currentConstraint := idxConstraint.Spans.Count() - 1
for ; currentConstraint >= 0; currentConstraint-- {
span := idxConstraint.Spans.Get(currentConstraint)
if span.StartKey().Length() > 1 {
return errors.AssertionFailedf(
"programming error: can't push down composite constraints into vtables")
}
if !span.HasSingleKey(p.EvalContext()) {
// No hope - we can't deal with range scans on virtual indexes.
break
}
constraintDatum := span.StartKey().Value(0)
virtualIndex := def.getIndex(index.ID)
// For each span, run the index's populate method, constrained to the
// constraint span's value.
found, err := virtualIndex.populate(ctx, constraintDatum, p, dbDesc,
addRowIfPassesFilter)
if err != nil {
return err
}
if !found && virtualIndex.partial {
// If we found nothing, and the index was partial, we have no choice
// but to populate the entire table and search through it.
break
}
}
if currentConstraint < 0 {
// We successfully processed all constraints, so we can leave now.
return nil
}
// Fall back to a full scan of the table, using the remaining filters
// that weren't able to be used as constraints.
idxConstraint.Spans.Truncate(currentConstraint + 1)
return def.populate(ctx, p, dbDesc, addRowIfPassesFilter)
}
}
// NewVirtualSchemaHolder creates a new VirtualSchemaHolder.
func NewVirtualSchemaHolder(
ctx context.Context, st *cluster.Settings,
) (*VirtualSchemaHolder, error) {
vs := &VirtualSchemaHolder{
entries: make(map[string]virtualSchemaEntry, len(virtualSchemas)),
orderedNames: make([]string, len(virtualSchemas)),
defsByID: make(map[sqlbase.ID]*virtualDefEntry, math.MaxUint32-sqlbase.MinVirtualID),
}
order := 0
for schemaID, schema := range virtualSchemas {
dbName := schema.name
dbDesc := initVirtualDatabaseDesc(schemaID, dbName)
defs := make(map[string]virtualDefEntry, len(schema.tableDefs))
orderedDefNames := make([]string, 0, len(schema.tableDefs))
for id, def := range schema.tableDefs {
tableDesc, err := def.initVirtualTableDesc(ctx, st, schemaID, id)
if err != nil {
return nil, errors.NewAssertionErrorWithWrappedErrf(err,
"failed to initialize %s", errors.Safe(def.getSchema()))
}
if schema.tableValidator != nil {
if err := schema.tableValidator(&tableDesc); err != nil {
return nil, errors.NewAssertionErrorWithWrappedErrf(err, "programmer error")
}
}
entry := virtualDefEntry{
virtualDef: def,
desc: &tableDesc,
validWithNoDatabaseContext: schema.validWithNoDatabaseContext,
comment: def.getComment(),
}
defs[tableDesc.Name] = entry
vs.defsByID[tableDesc.ID] = &entry
orderedDefNames = append(orderedDefNames, tableDesc.Name)
}
sort.Strings(orderedDefNames)
vs.entries[dbName] = virtualSchemaEntry{
desc: dbDesc,
defs: defs,
orderedDefNames: orderedDefNames,
allTableNames: schema.allTableNames,
containsTypes: schema.containsTypes,
}
vs.orderedNames[order] = dbName
order++
}
sort.Strings(vs.orderedNames)
return vs, nil
}
// Virtual databases and tables each have SELECT privileges for "public", which includes
// all users. However, virtual schemas have more fine-grained access control.
// For instance, information_schema will only expose rows to a given user which that
// user has access to.
var publicSelectPrivileges = sqlbase.NewPrivilegeDescriptor(sqlbase.PublicRole, privilege.List{privilege.SELECT})
func initVirtualDatabaseDesc(id sqlbase.ID, name string) *sqlbase.ImmutableDatabaseDescriptor {
return sqlbase.NewInitialDatabaseDescriptorWithPrivileges(id, name, publicSelectPrivileges)
}
// getEntries is part of the VirtualTabler interface.
func (vs *VirtualSchemaHolder) getEntries() map[string]virtualSchemaEntry {
return vs.entries
}
// getSchemaNames is part of the VirtualTabler interface.
func (vs *VirtualSchemaHolder) getSchemaNames() []string {
return vs.orderedNames
}
// getVirtualSchemaEntry retrieves a virtual schema entry given a database name.
// getVirtualSchemaEntry is part of the VirtualTabler interface.
func (vs *VirtualSchemaHolder) getVirtualSchemaEntry(name string) (virtualSchemaEntry, bool) {
e, ok := vs.entries[name]
return e, ok
}
// getVirtualTableEntry checks if the provided name matches a virtual database/table
// pair. The function will return the table's virtual table entry if the name matches
// a specific table. It will return an error if the name references a virtual database
// but the table is non-existent.
// getVirtualTableEntry is part of the VirtualTabler interface.
func (vs *VirtualSchemaHolder) getVirtualTableEntry(tn *tree.TableName) (virtualDefEntry, error) {
if db, ok := vs.getVirtualSchemaEntry(tn.Schema()); ok {
tableName := tn.Table()
if t, ok := db.defs[tableName]; ok {
return t, nil
}
if _, ok := db.allTableNames[tableName]; ok {
return virtualDefEntry{}, unimplemented.NewWithIssueDetailf(8675,
tn.Schema()+"."+tableName,
"virtual schema table not implemented: %s.%s", tn.Schema(), tableName)
}
return virtualDefEntry{}, sqlbase.NewUndefinedRelationError(tn)
}
return virtualDefEntry{}, nil
}
func (vs *VirtualSchemaHolder) getVirtualTableEntryByID(id sqlbase.ID) (virtualDefEntry, error) {
entry, ok := vs.defsByID[id]
if !ok {
return virtualDefEntry{}, sqlbase.ErrDescriptorNotFound
}
return *entry, nil
}
// VirtualTabler is used to fetch descriptors for virtual tables and databases.
type VirtualTabler interface {
getVirtualTableDesc(tn *tree.TableName) (*sqlbase.TableDescriptor, error)
getVirtualSchemaEntry(name string) (virtualSchemaEntry, bool)
getVirtualTableEntry(tn *tree.TableName) (virtualDefEntry, error)
getVirtualTableEntryByID(id sqlbase.ID) (virtualDefEntry, error)
getEntries() map[string]virtualSchemaEntry
getSchemaNames() []string
}
// getVirtualTableDesc checks if the provided name matches a virtual database/table
// pair, and returns its descriptor if it does.
// getVirtualTableDesc is part of the VirtualTabler interface.
func (vs *VirtualSchemaHolder) getVirtualTableDesc(
tn *tree.TableName,
) (*sqlbase.TableDescriptor, error) {
t, err := vs.getVirtualTableEntry(tn)
if err != nil {
return nil, err
}
return t.desc, nil
}