-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: rationalize the handling of comments #95431
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -737,6 +737,11 @@ func (tc *Collection) GetAllDatabases(ctx context.Context, txn *kv.Txn) (nstree. | |
if err != nil { | ||
return nstree.Catalog{}, err | ||
} | ||
|
||
// FIXME: here probably I want to call LoadComments() with just | ||
// DatabaseCommentType. But I only want to do so when the caller is | ||
// interested in comments. Not all are. | ||
|
||
ret, err := tc.aggregateAllLayers(ctx, txn, stored) | ||
if err != nil { | ||
return nstree.Catalog{}, err | ||
|
@@ -843,8 +848,14 @@ func (tc *Collection) GetAllInDatabase( | |
if err != nil { | ||
return nstree.Catalog{}, err | ||
} | ||
|
||
// Also ensure the db desc itself is included, which ensures we can | ||
// fetch its comment if any below. | ||
// FIXME: this does not seem to work. Why? | ||
ret.UpsertDescriptor(db) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the call to |
||
|
||
var inDatabaseIDs catalog.DescriptorIDSet | ||
_ = ret.ForEachDescriptor(func(desc catalog.Descriptor) error { | ||
if err := ret.ForEachDescriptor(func(desc catalog.Descriptor) error { | ||
if desc.DescriptorType() == catalog.Schema { | ||
if dbID := desc.GetParentID(); dbID != descpb.InvalidID && dbID != db.GetID() { | ||
return nil | ||
|
@@ -855,11 +866,32 @@ func (tc *Collection) GetAllInDatabase( | |
} | ||
} | ||
inDatabaseIDs.Add(desc.GetID()) | ||
|
||
// Also include all the comments for this object. | ||
// FIXME: This does not seem to be the right place to call this -- | ||
// it should load comments into `stored` _before_ the call | ||
// to aggregateLayers(), so that shadowed comments don't get overwritten. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And zone configs. I think your instinct is correct but I'd go even further: |
||
comments, err := tc.cr.LoadComments(ctx, txn, catalogkeys.AllCommentTypes, desc.GetID()) | ||
if err != nil { | ||
return err | ||
} | ||
ret.AddAll(comments) | ||
return nil | ||
}) | ||
}); err != nil { | ||
return nstree.Catalog{}, err | ||
} | ||
|
||
return ret.FilterByIDs(inDatabaseIDs.Ordered()), nil | ||
} | ||
|
||
// LoadComments extends the given catalog with all comments of the given type | ||
// associated with descriptors already in the catalog. | ||
func (tc *Collection) LoadComments( | ||
ctx context.Context, txn *kv.Txn, commentTypes []catalogkeys.CommentType, descID descpb.ID, | ||
) (nstree.Catalog, error) { | ||
return tc.cr.LoadComments(ctx, txn, commentTypes, descID) | ||
} | ||
|
||
// GetAllTablesInDatabase is like GetAllInDatabase but filtered to tables. | ||
// Includes virtual objects. Does not include dropped objects. | ||
func (tc *Collection) GetAllTablesInDatabase( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,10 @@ type CatalogReader interface { | |
ctx context.Context, txn *kv.Txn, db catalog.DatabaseDescriptor, sc catalog.SchemaDescriptor, | ||
) (nstree.Catalog, error) | ||
|
||
// LoadComments returns a catalog with at least the comment of the | ||
// given types for the given object. It may include more comments. | ||
LoadComments(ctx context.Context, txn *kv.Txn, commentTypes []catalogkeys.CommentType, objID descpb.ID) (nstree.Catalog, error) | ||
|
||
// GetByIDs reads the system.descriptor, system.comments and system.zone | ||
// entries for the desired IDs, but looks in the system database cache | ||
// first if there is one. | ||
|
@@ -159,6 +163,23 @@ func (cr catalogReader) ScanAll(ctx context.Context, txn *kv.Txn) (nstree.Catalo | |
return mc.Catalog, nil | ||
} | ||
|
||
// LoadComments is part of the CatalogReader interface. | ||
func (cr catalogReader) LoadComments( | ||
ctx context.Context, txn *kv.Txn, commentTypes []catalogkeys.CommentType, objID descpb.ID, | ||
) (nstree.Catalog, error) { | ||
var mc nstree.MutableCatalog | ||
cq := catalogQuery{codec: cr.codec} | ||
err := cq.query(ctx, txn, &mc, func(codec keys.SQLCodec, b *kv.Batch) { | ||
for _, ct := range commentTypes { | ||
scan(ctx, b, catalogkeys.MakeObjectCommentsMetadataPrefix(codec, ct, objID)) | ||
} | ||
}) | ||
if err != nil { | ||
return nstree.Catalog{}, err | ||
} | ||
return mc.Catalog, nil | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Serious question: what's wrong with using |
||
|
||
func (cr catalogReader) scanNamespace( | ||
ctx context.Context, txn *kv.Txn, prefix roachpb.Key, | ||
) (nstree.Catalog, error) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming that it's fine to replace
LoadComments
in favour ofGetByIDs
, and assuming that we would in fact rather not scan the comments table so much (do we really care? those scans are going to be empty most of the time, aren't they?), what we could do is make theGetByIDs
implementation smarter based on theexpectedType
argument: if we're expecting a database descriptor then there's no point in scanning for non-database comments, an so forth.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to do something similar for zone configs as well.