Skip to content

Commit

Permalink
descs: remove GetObjectNamesAndIDs method
Browse files Browse the repository at this point in the history
This commit replaces usages of that method with calls to
GetAllObjectsInSchema.

Informs cockroachdb#64089.

Release note: None
  • Loading branch information
Marius Posta committed Dec 14, 2022
1 parent 192b26e commit ccffd88
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 40 deletions.
19 changes: 0 additions & 19 deletions pkg/sql/catalog/descs/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,25 +1111,6 @@ func (tc *Collection) GetSchemasForDatabase(
return ret, nil
}

// GetObjectNamesAndIDs returns the names and IDs of all objects in a schema.
// Deprecated: prefer GetAllObjectsInSchema.
func (tc *Collection) GetObjectNamesAndIDs(
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor,
) (nstree.Catalog, error) {
c, err := tc.GetAllObjectsInSchema(ctx, txn, db, sc)
if err != nil {
return nstree.Catalog{}, err
}
var ret nstree.MutableCatalog
_ = c.ForEachDescriptor(func(desc catalog.Descriptor) error {
if !desc.Dropped() {
ret.UpsertNamespaceEntry(desc, desc.GetID(), desc.GetModificationTime())
}
return nil
})
return ret.Catalog, nil
}

// SetSyntheticDescriptors sets the provided descriptors as the synthetic
// descriptors to override all other matching descriptors during immutable
// access. An immutable copy is made if the descriptor is mutable. See the
Expand Down
24 changes: 17 additions & 7 deletions pkg/sql/schema_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"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/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
"github.com/lib/pq/oid"
)
Expand Down Expand Up @@ -69,18 +70,27 @@ type schemaResolver struct {
// GetObjectNamesAndIDs implements the resolver.SchemaResolver interface.
func (sr *schemaResolver) GetObjectNamesAndIDs(
ctx context.Context, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor,
) (tableNames tree.TableNames, tableIDs descpb.IDs, _ error) {
c, err := sr.descCollection.GetObjectNamesAndIDs(ctx, sr.txn, db, sc)
) (objectNames tree.TableNames, objectIDs descpb.IDs, _ error) {
c, err := sr.descCollection.GetAllObjectsInSchema(ctx, sr.txn, db, sc)
if err != nil {
return nil, nil, err
}
_ = c.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
tn := tree.MakeTableNameWithSchema(tree.Name(db.GetName()), tree.Name(sc.GetName()), tree.Name(e.GetName()))
tableNames = append(tableNames, tn)
tableIDs = append(tableIDs, e.GetID())
var mc nstree.MutableCatalog
_ = c.ForEachDescriptor(func(desc catalog.Descriptor) error {
if !desc.SkipNamespace() && !desc.Dropped() {
mc.UpsertNamespaceEntry(desc, desc.GetID(), hlc.Timestamp{})
}
return nil
})
_ = mc.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
tn := tree.MakeTableNameWithSchema(
tree.Name(db.GetName()), tree.Name(sc.GetName()), tree.Name(e.GetName()),
)
objectNames = append(objectNames, tn)
objectIDs = append(objectIDs, e.GetID())
return nil
})
return tableNames, tableIDs, nil
return objectNames, objectIDs, nil
}

// MustGetCurrentSessionDatabase implements the resolver.SchemaResolver interface.
Expand Down
28 changes: 14 additions & 14 deletions pkg/sql/temporary_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"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/nstree"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
Expand Down Expand Up @@ -230,7 +229,7 @@ func cleanupTempSchemaObjects(
db catalog.DatabaseDescriptor,
sc catalog.SchemaDescriptor,
) error {
c, err := descsCol.GetObjectNamesAndIDs(ctx, txn, db, sc)
objects, err := descsCol.GetAllObjectsInSchema(ctx, txn, db, sc)
if err != nil {
return err
}
Expand All @@ -247,27 +246,28 @@ func cleanupTempSchemaObjects(

tblDescsByID := make(map[descpb.ID]catalog.TableDescriptor)
tblNamesByID := make(map[descpb.ID]tree.TableName)
_ = c.ForEachNamespaceEntry(func(e nstree.NamespaceEntry) error {
desc, err := descsCol.Direct().MustGetTableDescByID(ctx, txn, e.GetID())
if err != nil {
return err
_ = objects.ForEachDescriptor(func(desc catalog.Descriptor) error {
tbl, ok := desc.(catalog.TableDescriptor)
if !ok || desc.Dropped() {
return nil
}

tblDescsByID[desc.GetID()] = desc
tblNamesByID[desc.GetID()] = tree.MakeTableNameWithSchema(tree.Name(db.GetName()), tree.Name(sc.GetName()), tree.Name(e.GetName()))
tblDescsByID[desc.GetID()] = tbl
tblNamesByID[desc.GetID()] = tree.MakeTableNameWithSchema(
tree.Name(db.GetName()), tree.Name(sc.GetName()), tree.Name(tbl.GetName()),
)

databaseIDToTempSchemaID[uint32(desc.GetParentID())] = uint32(desc.GetParentSchemaID())

// If a sequence is owned by a table column, it is dropped when the owner
// table/column is dropped. So here we want to only drop sequences not
// owned.
if desc.IsSequence() &&
desc.GetSequenceOpts().SequenceOwner.OwnerColumnID == 0 &&
desc.GetSequenceOpts().SequenceOwner.OwnerTableID == 0 {
if tbl.IsSequence() &&
tbl.GetSequenceOpts().SequenceOwner.OwnerColumnID == 0 &&
tbl.GetSequenceOpts().SequenceOwner.OwnerTableID == 0 {
sequences = append(sequences, desc.GetID())
} else if desc.GetViewQuery() != "" {
} else if tbl.GetViewQuery() != "" {
views = append(views, desc.GetID())
} else if !desc.IsSequence() {
} else if !tbl.IsSequence() {
tables = append(tables, desc.GetID())
}
return nil
Expand Down

0 comments on commit ccffd88

Please sign in to comment.