Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: include invalid type descriptors in crdb_internal.invalid_objects #59978

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3784,14 +3784,12 @@ CREATE TABLE crdb_internal.invalid_objects (
) error {
// The internalLookupContext will only have descriptors in the current
// database. To deal with this, we fall through.
// TODO(spaskob): we can also validate type descriptors. Add a new function
// `forEachTypeDescAllWithTableLookup` and the results to this table.
descs, err := catalogkv.GetAllDescriptorsUnvalidated(ctx, p.txn, p.extendedEvalCtx.Codec)
if err != nil {
return err
}
const allowAdding = true
return forEachTableDescWithTableLookupInternalFromDescriptors(
if err := forEachTableDescWithTableLookupInternalFromDescriptors(
ctx, p, dbContext, hideVirtual, allowAdding, descs, func(
dbDesc *dbdesc.Immutable, schema string, descriptor catalog.TableDescriptor, fn tableLookupFn,
) error {
Expand All @@ -3806,6 +3804,34 @@ CREATE TABLE crdb_internal.invalid_objects (
if dbDesc != nil {
dbName = dbDesc.GetName()
}
return addRow(
tree.NewDInt(tree.DInt(descriptor.GetID())),
tree.NewDString(dbName),
tree.NewDString(schema),
tree.NewDString(descriptor.GetName()),
tree.NewDString(err.Error()),
)
}); err != nil {
return err
}

// Validate type descriptors.
return forEachTypeDescWithTableLookupInternalFromDescriptors(
ctx, p, dbContext, allowAdding, descs, func(
dbDesc *dbdesc.Immutable, schema string, descriptor catalog.TypeDescriptor, fn tableLookupFn,
) error {
if descriptor == nil {
return nil
}
err = descriptor.Validate(ctx, fn)
if err == nil {
return nil
}
var dbName string
if dbDesc != nil {
dbName = dbDesc.GetName()
}

return addRow(
tree.NewDInt(tree.DInt(descriptor.GetID())),
tree.NewDString(dbName),
Expand Down
34 changes: 34 additions & 0 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,40 @@ func forEachTableDescWithTableLookupInternal(
ctx, p, dbContext, virtualOpts, allowAdding, descs, fn)
}

func forEachTypeDescWithTableLookupInternalFromDescriptors(
ctx context.Context,
p *planner,
dbContext *dbdesc.Immutable,
allowAdding bool,
descs []catalog.Descriptor,
fn func(*dbdesc.Immutable, string, catalog.TypeDescriptor, tableLookupFn) error,
) error {
lCtx := newInternalLookupCtx(ctx, descs, dbContext,
catalogkv.NewOneLevelUncachedDescGetter(p.txn, p.execCfg.Codec))

for _, typID := range lCtx.typIDs {
typDesc := lCtx.typDescs[typID]
if typDesc.Dropped() || !userCanSeeDescriptor(ctx, p, typDesc, allowAdding) {
continue
}
var scName string
dbDesc, parentExists := lCtx.dbDescs[typDesc.GetParentID()]
if parentExists {
var ok bool
scName, ok = lCtx.schemaNames[typDesc.GetParentSchemaID()]
if !ok {
return errors.AssertionFailedf("schema id %d not found", typDesc.GetParentSchemaID())
}
if err := fn(dbDesc, scName, typDesc, lCtx); err != nil {
return err
}
} else {
return errors.AssertionFailedf("database id %d not found", typDesc.GetParentID())
}
}
return nil
}

func forEachTableDescWithTableLookupInternalFromDescriptors(
ctx context.Context,
p *planner,
Expand Down