github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/testutils/testcat/vtable.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package testcat 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/sql/parser" 15 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 16 "github.com/cockroachdb/cockroach/pkg/sql/vtable" 17 "github.com/cockroachdb/errors" 18 ) 19 20 var informationSchemaMap = map[string]*tree.CreateTable{} 21 22 var informationSchemaTables = []string{ 23 vtable.InformationSchemaColumns, 24 vtable.InformationSchemaAdministrableRoleAuthorizations, 25 vtable.InformationSchemaApplicableRoles, 26 vtable.InformationSchemaColumnPrivileges, 27 vtable.InformationSchemaSchemata, 28 vtable.InformationSchemaTables, 29 } 30 31 func init() { 32 // Build a map that maps the names of the various information_schema tables 33 // to their CREATE TABLE AST. 34 for _, table := range informationSchemaTables { 35 parsed, err := parser.ParseOne(table) 36 if err != nil { 37 panic(errors.Wrap(err, "error initializing virtual table map")) 38 } 39 40 ct, ok := parsed.AST.(*tree.CreateTable) 41 if !ok { 42 panic(errors.New("virtual table schemas must be CREATE TABLE statements")) 43 } 44 45 ct.Table.SchemaName = tree.Name("information_schema") 46 ct.Table.ExplicitSchema = true 47 48 ct.Table.CatalogName = testDB 49 ct.Table.ExplicitCatalog = true 50 51 name := ct.Table 52 informationSchemaMap[name.ObjectName.String()] = ct 53 } 54 } 55 56 // Resolve returns true and the AST node describing the virtual table referenced. 57 // TODO(justin): make this complete for all virtual tables. 58 func resolveVTable(name *tree.TableName) (*tree.CreateTable, bool) { 59 switch name.SchemaName { 60 case "information_schema": 61 schema, ok := informationSchemaMap[name.ObjectName.String()] 62 return schema, ok 63 } 64 65 return nil, false 66 }