github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/cat/catalog.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 cat contains interfaces that are used by the query optimizer to avoid 12 // including specifics of sqlbase structures in the opt code. 13 package cat 14 15 import ( 16 "context" 17 18 "github.com/cockroachdb/cockroach/pkg/sql/privilege" 19 "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" 20 ) 21 22 // StableID permanently and uniquely identifies a catalog object (table, view, 23 // index, column, etc.) within its scope: 24 // 25 // data source StableID: unique within database 26 // index StableID: unique within table 27 // column StableID: unique within table 28 // 29 // If a new catalog object is created, it will always be assigned a new StableID 30 // that has never, and will never, be reused by a different object in the same 31 // scope. This uniqueness guarantee is true even if the new object has the same 32 // name and schema as an old (and possibly dropped) object. The StableID will 33 // never change as long as the object exists. 34 // 35 // Note that while two instances of the same catalog object will always have the 36 // same StableID, they can have different schema if the schema has changed over 37 // time. See the Version type comments for more details. 38 // 39 // For most sqlbase objects, the StableID is the 32-bit descriptor ID. However, 40 // this is not always the case. For example, the StableID for virtual tables 41 // prepends the database ID, since the same descriptor ID is reused across 42 // databases. 43 type StableID uint64 44 45 // SchemaName is an alias for tree.ObjectNamePrefix, since it consists of the 46 // catalog + schema name. 47 type SchemaName = tree.ObjectNamePrefix 48 49 // Flags allows controlling aspects of some Catalog operations. 50 type Flags struct { 51 // AvoidDescriptorCaches avoids using any cached descriptors (for tables, 52 // views, schemas, etc). This is useful in cases where we are running a 53 // statement like SHOW and we don't want to get table leases or otherwise 54 // pollute the caches. 55 AvoidDescriptorCaches bool 56 57 // NoTableStats doesn't retrieve table statistics. This should be used in all 58 // cases where we don't need them (like SHOW variants), to avoid polluting the 59 // stats cache. 60 NoTableStats bool 61 } 62 63 // Catalog is an interface to a database catalog, exposing only the information 64 // needed by the query optimizer. 65 // 66 // NOTE: Catalog implementations need not be thread-safe. However, the objects 67 // returned by the Resolve methods (schemas and data sources) *must* be 68 // immutable after construction, and therefore also thread-safe. 69 type Catalog interface { 70 // ResolveSchema locates a schema with the given name and returns it along 71 // with the resolved SchemaName (which has all components filled in). 72 // If the SchemaName is empty, returns the current database/schema (if one is 73 // set; otherwise returns an error). 74 // 75 // The resolved SchemaName is the same with the resulting Schema.Name() except 76 // that it has the ExplicitCatalog/ExplicitSchema flags set to correspond to 77 // the input name. Its use is mainly for cosmetic purposes. 78 // 79 // If no such schema exists, then ResolveSchema returns an error. 80 // 81 // NOTE: The returned schema must be immutable after construction, and so can 82 // be safely copied or used across goroutines. 83 ResolveSchema(ctx context.Context, flags Flags, name *SchemaName) (Schema, SchemaName, error) 84 85 // ResolveDataSource locates a data source with the given name and returns it 86 // along with the resolved DataSourceName. 87 // 88 // The resolved DataSourceName is the same with the resulting 89 // DataSource.Name() except that it has the ExplicitCatalog/ExplicitSchema 90 // flags set to correspond to the input name. Its use is mainly for cosmetic 91 // purposes. For example: the input name might be "t". The fully qualified 92 // DataSource.Name() would be "currentdb.public.t"; the returned 93 // DataSourceName would have the same fields but would still be formatted as 94 // "t". 95 // 96 // If no such data source exists, then ResolveDataSource returns an error. 97 // 98 // NOTE: The returned data source must be immutable after construction, and 99 // so can be safely copied or used across goroutines. 100 ResolveDataSource( 101 ctx context.Context, flags Flags, name *DataSourceName, 102 ) (DataSource, DataSourceName, error) 103 104 // ResolveDataSourceByID is similar to ResolveDataSource, except that it 105 // locates a data source by its StableID. See the comment for StableID for 106 // more details. 107 // 108 // If the table is in the process of being added, returns an 109 // "undefined relation" error but also returns isAdding=true. 110 // 111 // NOTE: The returned data source must be immutable after construction, and 112 // so can be safely copied or used across goroutines. 113 ResolveDataSourceByID( 114 ctx context.Context, flags Flags, id StableID, 115 ) (_ DataSource, isAdding bool, _ error) 116 117 // CheckPrivilege verifies that the current user has the given privilege on 118 // the given catalog object. If not, then CheckPrivilege returns an error. 119 CheckPrivilege(ctx context.Context, o Object, priv privilege.Kind) error 120 121 // CheckAnyPrivilege verifies that the current user has any privilege on 122 // the given catalog object. If not, then CheckAnyPrivilege returns an error. 123 CheckAnyPrivilege(ctx context.Context, o Object) error 124 125 // HasAdminRole checks that the current user has admin privileges. If yes, 126 // returns true. Returns an error if query on the `system.users` table failed 127 HasAdminRole(ctx context.Context) (bool, error) 128 129 // RequireAdminRole checks that the current user has admin privileges. If not, 130 // returns an error. 131 RequireAdminRole(ctx context.Context, action string) error 132 133 // FullyQualifiedName retrieves the fully qualified name of a data source. 134 // Note that: 135 // - this call may involve a database operation so it shouldn't be used in 136 // performance sensitive paths; 137 // - the fully qualified name of a data source object can change without the 138 // object itself changing (e.g. when a database is renamed). 139 FullyQualifiedName(ctx context.Context, ds DataSource) (DataSourceName, error) 140 }