-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
schema_resolver.go
535 lines (491 loc) · 18.9 KB
/
schema_resolver.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
// Copyright 2022 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"
"fmt"
"strings"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/funcdesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"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/schemachanger/scbuild"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/builtinsregistry"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
var _ resolver.SchemaResolver = &schemaResolver{}
// schemaResolve implements the resolver.SchemaResolver interface.
// Currently, this is only being embedded in the planner but also a convenience
// for inejcting it into the declarative schema changer.
// It holds sessionDataStack and a transaction handle which are reset when
// planner is reset.
// TODO (Chengxiong) refactor this out into a separate package.
type schemaResolver struct {
descCollection *descs.Collection
sessionDataStack *sessiondata.Stack
txn *kv.Txn
authAccessor scbuild.AuthorizationAccessor
// skipDescriptorCache, when true, instructs all code that accesses table/view
// descriptors to force reading the descriptors within the transaction. This
// is necessary to read descriptors from the store for:
// 1. Descriptors that are part of a schema change but are not modified by the
// schema change. (reading a table in CREATE VIEW)
// 2. Disable the use of the table cache in tests.
skipDescriptorCache bool
// typeResolutionDbID is the ID of a database. If set, type resolution steps
// will disallow resolution of types that have a parentID != typeResolutionDbID
// when it is set.
typeResolutionDbID descpb.ID
}
// Accessor implements the resolver.SchemaResolver interface.
func (sr *schemaResolver) Accessor() catalog.Accessor {
return sr.descCollection
}
// CurrentSearchPath implements the resolver.SchemaResolver interface.
func (sr *schemaResolver) CurrentSearchPath() sessiondata.SearchPath {
return sr.sessionDataStack.Top().SearchPath
}
// CommonLookupFlagsRequired implements the resolver.SchemaResolver interface.
func (sr *schemaResolver) CommonLookupFlagsRequired() tree.CommonLookupFlags {
return tree.CommonLookupFlags{
Required: true,
AvoidLeased: sr.skipDescriptorCache,
}
}
// LookupObject implements the tree.ObjectNameExistingResolver interface.
func (sr *schemaResolver) LookupObject(
ctx context.Context, flags tree.ObjectLookupFlags, dbName, scName, obName string,
) (found bool, prefix catalog.ResolvedObjectPrefix, objMeta catalog.Descriptor, err error) {
flags.CommonLookupFlags.Required = false
flags.CommonLookupFlags.AvoidLeased = sr.skipDescriptorCache
// Check if we are looking up a type which matches a built-in type in
// CockroachDB but is an extension type on the public schema in PostgreSQL.
if flags.DesiredObjectKind == tree.TypeObject && scName == tree.PublicSchema {
if alias, ok := types.PublicSchemaAliases[obName]; ok {
if flags.RequireMutable {
return true, catalog.ResolvedObjectPrefix{}, nil, pgerror.Newf(pgcode.WrongObjectType, "type %q is a built-in type", obName)
}
found, prefix, err = sr.LookupSchema(ctx, dbName, scName)
if err != nil || !found {
return found, prefix, nil, err
}
dbDesc, err := sr.descCollection.GetImmutableDatabaseByName(ctx, sr.txn, dbName,
tree.DatabaseLookupFlags{AvoidLeased: sr.skipDescriptorCache})
if err != nil {
return found, prefix, nil, err
}
if dbDesc.HasPublicSchemaWithDescriptor() {
publicSchemaID := dbDesc.GetSchemaID(tree.PublicSchema)
return true, prefix, typedesc.MakeSimpleAlias(alias, publicSchemaID), nil
}
return true, prefix, typedesc.MakeSimpleAlias(alias, keys.PublicSchemaID), nil
}
}
prefix, objMeta, err = sr.descCollection.GetObjectByName(ctx, sr.txn, dbName, scName, obName, flags)
return objMeta != nil, prefix, objMeta, err
}
// LookupSchema implements the resolver.ObjectNameTargetResolver interface.
func (sr *schemaResolver) LookupSchema(
ctx context.Context, dbName, scName string,
) (found bool, scMeta catalog.ResolvedObjectPrefix, err error) {
flags := sr.CommonLookupFlagsRequired()
flags.Required = false
db, err := sr.descCollection.GetImmutableDatabaseByName(ctx, sr.txn, dbName, flags)
if err != nil || db == nil {
return false, catalog.ResolvedObjectPrefix{}, err
}
sc, err := sr.descCollection.GetImmutableSchemaByName(ctx, sr.txn, db, scName, flags)
if err != nil || sc == nil {
return false, catalog.ResolvedObjectPrefix{}, err
}
return true, catalog.ResolvedObjectPrefix{Database: db, Schema: sc}, nil
}
func (sr *schemaResolver) LookupDatabase(ctx context.Context, dbName string) error {
flags := sr.CommonLookupFlagsRequired()
_, err := sr.descCollection.GetImmutableDatabaseByName(ctx, sr.txn, dbName, flags)
if err != nil {
return err
}
return nil
}
// CurrentDatabase implements the tree.QualifiedNameResolver interface.
func (sr *schemaResolver) CurrentDatabase() string {
return sr.sessionDataStack.Top().Database
}
// GetQualifiedTableNameByID returns the qualified name of the table,
// view or sequence represented by the provided ID and table kind.
func (sr *schemaResolver) GetQualifiedTableNameByID(
ctx context.Context, id int64, requiredType tree.RequiredTableKind,
) (*tree.TableName, error) {
lookupFlags := tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{Required: true},
DesiredObjectKind: tree.TableObject,
DesiredTableDescKind: requiredType,
}
table, err := sr.descCollection.GetImmutableTableByID(
ctx, sr.txn, descpb.ID(id), lookupFlags)
if err != nil {
return nil, err
}
return sr.getQualifiedTableName(ctx, table)
}
// getQualifiedTableName returns the database-qualified name of the table
// or view represented by the provided descriptor. It is a sort of
// reverse of the Resolve() functions.
func (sr *schemaResolver) getQualifiedTableName(
ctx context.Context, desc catalog.TableDescriptor,
) (*tree.TableName, error) {
_, dbDesc, err := sr.descCollection.GetImmutableDatabaseByID(ctx, sr.txn, desc.GetParentID(),
// When getting the fully qualified name use leased descriptors, since these
// will not involve any round trips.
tree.DatabaseLookupFlags{
Required: true,
IncludeOffline: true,
IncludeDropped: true,
AvoidLeased: sr.skipDescriptorCache,
})
if err != nil {
return nil, err
}
// Get the schema name. Use some specialized logic to deal with descriptors
// from other temporary schemas.
//
// TODO(ajwerner): We shouldn't need this temporary logic if we properly
// tracked all descriptors as we read them and made them available in the
// collection. We should only be hitting this edge case when dropping a
// database, in which case we've already read all of the temporary schema
// information from the namespace table.
var schemaName tree.Name
schemaID := desc.GetParentSchemaID()
scDesc, err := sr.descCollection.GetImmutableSchemaByID(ctx, sr.txn, schemaID,
tree.SchemaLookupFlags{
Required: true,
IncludeOffline: true,
IncludeDropped: true,
AvoidLeased: sr.skipDescriptorCache,
})
switch {
case scDesc != nil:
schemaName = tree.Name(scDesc.GetName())
case desc.IsTemporary() && scDesc == nil:
// We've lost track of the session which owned this schema, but we
// can come up with a name that is also going to be unique and
// informative and looks like a pg_temp_<session_id> name.
schemaName = tree.Name(fmt.Sprintf("pg_temp_%d", schemaID))
default:
return nil, errors.Wrapf(err,
"resolving schema name for %s.[%d].%s",
tree.Name(dbDesc.GetName()),
schemaID,
tree.Name(desc.GetName()),
)
}
tbName := tree.MakeTableNameWithSchema(
tree.Name(dbDesc.GetName()),
schemaName,
tree.Name(desc.GetName()),
)
return &tbName, nil
}
func (sr *schemaResolver) getQualifiedFunctionName(
ctx context.Context, fnDesc catalog.FunctionDescriptor,
) (*tree.FunctionName, error) {
lookupFlags := tree.CommonLookupFlags{
Required: true,
IncludeOffline: true,
IncludeDropped: true,
AvoidLeased: true,
}
_, dbDesc, err := sr.descCollection.GetImmutableDatabaseByID(ctx, sr.txn, fnDesc.GetParentID(), lookupFlags)
if err != nil {
return nil, err
}
scDesc, err := sr.descCollection.GetImmutableSchemaByID(ctx, sr.txn, fnDesc.GetParentSchemaID(), lookupFlags)
if err != nil {
return nil, err
}
fnName := tree.MakeQualifiedFunctionName(dbDesc.GetName(), scDesc.GetName(), fnDesc.GetName())
return &fnName, nil
}
// ResolveType implements the tree.TypeReferenceResolver interface.
func (sr *schemaResolver) ResolveType(
ctx context.Context, name *tree.UnresolvedObjectName,
) (*types.T, error) {
lookupFlags := tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{Required: true, RequireMutable: false},
DesiredObjectKind: tree.TypeObject,
}
desc, prefix, err := resolver.ResolveExistingObject(ctx, sr, name, lookupFlags)
if err != nil {
return nil, err
}
// For "reasons" we always fully qualify type names which are resolved via
// the type reference resolver.
//
// TODO(ajwerner): Understand these reasons.
prefix.ExplicitDatabase = prefix.Database != nil
prefix.ExplicitSchema = prefix.Schema != nil
tn := tree.MakeTypeNameWithPrefix(prefix.NamePrefix(), name.Object())
tdesc := desc.(catalog.TypeDescriptor)
// Disllow cross-database type resolution. Note that we check
// typeResolutionDbID != descpb.InvalidID when we have been restricted to
// accessing types in the database with ID = typeResolutionDbID by
// p.runWithOptions. So, check to see if the resolved descriptor's parentID
// matches, unless the descriptor's parentID is invalid. This could happen
// when the type being resolved is a builtin type prefaced with a virtual
// schema like `pg_catalog.int`. Resolution for these types returns a dummy
// TypeDescriptor, so ignore those cases.
if sr.typeResolutionDbID != descpb.InvalidID && tdesc.GetParentID() != descpb.InvalidID && tdesc.GetParentID() != sr.typeResolutionDbID {
return nil, pgerror.Newf(
pgcode.FeatureNotSupported, "cross database type references are not supported: %s", tn.String())
}
// Ensure that the user can access the target schema.
if err := sr.canResolveDescUnderSchema(ctx, prefix.Schema, tdesc); err != nil {
return nil, err
}
return tdesc.MakeTypesT(ctx, &tn, sr)
}
// ResolveTypeByOID implements the tree.TypeReferenceResolver interface.
func (sr *schemaResolver) ResolveTypeByOID(ctx context.Context, oid oid.Oid) (*types.T, error) {
id, err := typedesc.UserDefinedTypeOIDToID(oid)
if err != nil {
return nil, err
}
name, desc, err := sr.GetTypeDescriptor(ctx, id)
if err != nil {
return nil, err
}
return desc.MakeTypesT(ctx, &name, sr)
}
// GetTypeDescriptor implements the catalog.TypeDescriptorResolver interface.
func (sr *schemaResolver) GetTypeDescriptor(
ctx context.Context, id descpb.ID,
) (tree.TypeName, catalog.TypeDescriptor, error) {
tc := sr.descCollection
flags := sr.CommonLookupFlagsRequired()
flags.ParentID = sr.typeResolutionDbID
desc, err := tc.GetImmutableTypeByID(ctx, sr.txn, id, tree.ObjectLookupFlags{
CommonLookupFlags: flags,
})
if err != nil {
return tree.TypeName{}, nil, err
}
// Note that the value of required doesn't matter for lookups by ID.
dbName := sr.CurrentDatabase()
if !descpb.IsVirtualTable(desc.GetID()) {
_, db, err := tc.GetImmutableDatabaseByID(ctx, sr.txn, desc.GetParentID(), flags)
if err != nil {
return tree.TypeName{}, nil, err
}
dbName = db.GetName()
}
sc, err := tc.GetImmutableSchemaByID(ctx, sr.txn, desc.GetParentSchemaID(), flags)
if err != nil {
return tree.TypeName{}, nil, err
}
name := tree.MakeQualifiedTypeName(dbName, sc.GetName(), desc.GetName())
return name, desc, nil
}
func (sr *schemaResolver) canResolveDescUnderSchema(
ctx context.Context, scDesc catalog.SchemaDescriptor, desc catalog.Descriptor,
) error {
// We can't always resolve temporary schemas by ID (for example in the temporary
// object cleaner which accesses temporary schemas not in the current session).
// To avoid an internal error, we just don't check usage on temporary tables.
if tbl, ok := desc.(catalog.TableDescriptor); ok && tbl.IsTemporary() {
return nil
}
switch kind := scDesc.SchemaKind(); kind {
case catalog.SchemaPublic, catalog.SchemaTemporary, catalog.SchemaVirtual:
// Anyone can resolve under temporary, public or virtual schemas.
return nil
case catalog.SchemaUserDefined:
return sr.authAccessor.CheckPrivilegeForUser(ctx, scDesc, privilege.USAGE, sr.sessionDataStack.Top().User())
default:
forLog := kind // prevents kind from escaping
panic(errors.AssertionFailedf("unknown schema kind %d", forLog))
}
}
// runWithOptions sets the provided resolution flags for the
// duration of the call of the passed argument fn.
//
// This is meant to be used like this (for example):
//
// var someVar T
// var err error
//
// p.runWithOptions(resolveFlags{skipCache: true}, func() {
// someVar, err = ResolveExistingTableObject(ctx, p, ...)
// })
//
// if err != nil { ... }
// use(someVar)
func (sr *schemaResolver) runWithOptions(flags resolveFlags, fn func()) {
if flags.skipCache {
defer func(prev bool) { sr.skipDescriptorCache = prev }(sr.skipDescriptorCache)
sr.skipDescriptorCache = true
}
if flags.contextDatabaseID != descpb.InvalidID {
defer func(prev descpb.ID) { sr.typeResolutionDbID = prev }(sr.typeResolutionDbID)
sr.typeResolutionDbID = flags.contextDatabaseID
}
fn()
}
func (sr *schemaResolver) ResolveFunction(
ctx context.Context, name *tree.UnresolvedName, path tree.SearchPath,
) (*tree.ResolvedFunctionDefinition, error) {
if name.NumParts > 3 || len(name.Parts[0]) == 0 || name.Star {
return nil, pgerror.Newf(pgcode.InvalidName, "invalid function name: %s", name)
}
fn, err := name.ToFunctionName()
if err != nil {
return nil, err
}
if fn.ExplicitCatalog && fn.Catalog() != sr.CurrentDatabase() {
return nil, pgerror.New(pgcode.FeatureNotSupported, "cross-database function references not allowed")
}
// Get builtin functions if there is any match.
builtinDef, err := tree.GetBuiltinFuncDefinition(fn, path)
if err != nil {
return nil, err
}
var udfDef *tree.ResolvedFunctionDefinition
if fn.ExplicitSchema && fn.Schema() != catconstants.CRDBInternalSchemaName {
found, prefix, err := sr.LookupSchema(ctx, sr.CurrentDatabase(), fn.Schema())
if err != nil {
return nil, err
}
if !found {
return nil, pgerror.Newf(pgcode.UndefinedSchema, "schema %q does not exist", fn.Schema())
}
sc := prefix.Schema
udfDef, _ = sc.GetResolvedFuncDefinition(fn.Object())
} else {
for i, n := 0, path.NumElements(); i < n; i++ {
schema := path.GetSchema(i)
found, prefix, err := sr.LookupSchema(ctx, sr.CurrentDatabase(), schema)
if err != nil {
return nil, err
}
if !found {
continue
}
curUdfDef, found := prefix.Schema.GetResolvedFuncDefinition(fn.Object())
if !found {
continue
}
udfDef, err = udfDef.MergeWith(curUdfDef)
if err != nil {
return nil, err
}
}
}
if builtinDef == nil && udfDef == nil {
// If nothing found, there is a chance that user typed in a quoted function
// name which is not lowercase. So here we try to lowercase the given
// function name and find a suggested function name if possible.
extraMsg := ""
var lowerName tree.UnresolvedName
if fn.ExplicitSchema {
lowerName = tree.MakeUnresolvedName(strings.ToLower(name.Parts[1]), strings.ToLower(name.Parts[0]))
} else {
lowerName = tree.MakeUnresolvedName(strings.ToLower(name.Parts[0]))
}
if lowerName != *name {
alternative, err := sr.ResolveFunction(ctx, &lowerName, path)
if err == nil && alternative != nil {
extraMsg = fmt.Sprintf(", but %s() exists", alternative.Name)
}
}
return nil, errors.Wrapf(tree.ErrFunctionUndefined, "unknown function: %s()%s", tree.ErrString(name), extraMsg)
}
if builtinDef == nil {
return udfDef, nil
}
if udfDef == nil {
props, _ := builtinsregistry.GetBuiltinProperties(builtinDef.Name)
if props.UnsupportedWithIssue != 0 {
// Note: no need to embed the function name in the message; the
// caller will add the function name as prefix.
const msg = "this function is not yet supported"
var unImplErr error
if props.UnsupportedWithIssue < 0 {
unImplErr = unimplemented.New(builtinDef.Name+"()", msg)
} else {
unImplErr = unimplemented.NewWithIssueDetail(props.UnsupportedWithIssue, builtinDef.Name, msg)
}
return nil, pgerror.Wrapf(unImplErr, pgcode.InvalidParameterValue, "%s()", builtinDef.Name)
}
return builtinDef, nil
}
return builtinDef.MergeWith(udfDef)
}
func (sr *schemaResolver) ResolveFunctionByOID(
ctx context.Context, oid oid.Oid,
) (name string, fn *tree.Overload, err error) {
if !funcdesc.IsOIDUserDefinedFunc(oid) {
name, ok := tree.OidToBuiltinName[oid]
if !ok {
return "", nil, errors.Wrapf(tree.ErrFunctionUndefined, "function %d not found", oid)
}
funcDef := tree.FunDefs[name]
for _, o := range funcDef.Definition {
if o.Oid == oid {
return funcDef.Name, o, nil
}
}
}
flags := sr.CommonLookupFlagsRequired()
flags.AvoidLeased = sr.skipDescriptorCache
flags.ParentID = sr.typeResolutionDbID
descID, err := funcdesc.UserDefinedFunctionOIDToID(oid)
if err != nil {
return "", nil, err
}
funcDesc, err := sr.descCollection.GetImmutableFunctionByID(ctx, sr.txn, descID,
tree.ObjectLookupFlags{CommonLookupFlags: flags})
if err != nil {
return "", nil, err
}
ret, err := funcDesc.ToOverload()
if err != nil {
return "", nil, err
}
return funcDesc.GetName(), ret, nil
}
// NewSkippingCacheSchemaResolver constructs a schemaResolver which always skip
// descriptor cache.
func NewSkippingCacheSchemaResolver(
descCollection *descs.Collection,
sessionDataStack *sessiondata.Stack,
txn *kv.Txn,
authAccessor scbuild.AuthorizationAccessor,
) resolver.SchemaResolver {
return &schemaResolver{
descCollection: descCollection,
sessionDataStack: sessionDataStack,
txn: txn,
authAccessor: authAccessor,
skipDescriptorCache: true,
}
}