Skip to content

sql: stop returning raw protobufs from descriptor accessors #56306

@jordanlewis

Description

@jordanlewis

Currently, the interfaces defined for the top level descriptor types (catalog.{,Database,Schema,Table,Type}Descriptor) return raw protobufs from most of their methods. This is undesirable (at minimum) because the return values are easily mutated by accident, and because there's no opportunity to extend the raw protobufs with more advanced caching data structures.

Adding interfaces here instead of raw protos will also improve API uniformity and make our codebase more easily tested and understood.


The goal of this issue is to wrap the returned protobufs in an struct and accompanying interface that hides the underlying protobufs. Some of the leaf structs matter less - for example, UserPrivilege isn't important to wrap. It's more important to prevent the big ones from mutability: ColumnDescriptor, IndexDescriptor, and so on.

We'll be extending catalog.Descriptor and its sub-interfaces (DatabaseDescriptor, SchemaDescriptor, TableDescriptor, TypeDescriptor) so that their methods do not return things from the descpb package at all.

For example, we'll want to start returning a new catalog.IndexDescriptor interface instead of descpb.IndexDescriptor from TableDescriptor.GetPrimaryIndex().

Here is the list of interfaces we'll want to extract into sql/catalog, along with associated new structs inside of sql/catalog/tabledesc:

We'll need to figure out a pattern for iteration as well, for methods like GetPublicNonPrimaryIndexes(). We don't want to have to allocate a slice of n interfaces and structs for each of these items - for example, the ColumnDescriptors inside of an IndexDescriptor.


In addition to the new interfaces listed above we'll also want to extend and use the existing catalog interfaces:


We'll also want to unify the new interfaces with those in pkg/sql/opt/cat at some point:

  • cat.Index -> catalog.Index
  • cat.Column -> catalog.Column
  • others as well most likely (TBD)

Metadata

Metadata

Assignees

Labels

A-schema-descriptorsRelating to SQL table/db descriptor handling.meta-issueContains a list of several other issues.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions