Skip to content

Commit

Permalink
Merge #49621
Browse files Browse the repository at this point in the history
49621: sql: introduce `{Imm,M}utableTypeDescriptor` and plumbing r=ajwerner a=rohany

This PR introduces the `ImmutableTypeDescriptor` and
`MutableTypeDescriptor` types, and plumbs them through to existing
usages. Once more descriptor refactoring completes, only operations on
`TypeDescriptor`s through these wrapper structs will be allowed.

Release note: None

Co-authored-by: Rohan Yadav <[email protected]>
  • Loading branch information
craig[bot] and rohany committed May 28, 2020
2 parents 75bd7a9 + 3b3389f commit 8927b11
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
5 changes: 4 additions & 1 deletion pkg/sql/catalog/catalogkv/physical_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ func (a UncachedPhysicalAccessor) GetObjectDesc(
}
return nil, nil
case *sqlbase.TypeDescriptor:
return desc, nil
if flags.RequireMutable {
return sqlbase.NewMutableExistingTypeDescriptor(*desc), nil
}
return sqlbase.NewImmutableTypeDescriptor(*desc), nil
default:
return nil, nil
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/sql/catalog/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ func ResolveExistingObject(
if obj.TypeDesc() == nil {
return nil, prefix, sqlbase.NewUndefinedTypeError(&resolvedTn)
}
return obj.TypeDesc(), prefix, nil
if lookupFlags.RequireMutable {
return obj.(*sqlbase.MutableTypeDescriptor), prefix, nil
}
return obj.(*sqlbase.ImmutableTypeDescriptor), prefix, nil
case tree.TableObject:
if obj.TableDesc() == nil {
return nil, prefix, sqlbase.NewUndefinedRelationError(&resolvedTn)
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (p *planner) ResolveType(name *tree.UnresolvedObjectName) (*types.T, error)
lookupFlags := tree.ObjectLookupFlags{
CommonLookupFlags: tree.CommonLookupFlags{Required: true},
DesiredObjectKind: tree.TypeObject,
RequireMutable: false,
}
// TODO (rohany): The ResolveAnyDescType argument doesn't do anything here
// if we are looking for a type. This should be cleaned up.
Expand All @@ -146,7 +147,7 @@ func (p *planner) ResolveType(name *tree.UnresolvedObjectName) (*types.T, error)
return nil, err
}
tn := tree.MakeTypeNameFromPrefix(prefix, tree.Name(name.Object()))
tdesc := desc.(*sqlbase.TypeDescriptor)
tdesc := desc.(*sqlbase.ImmutableTypeDescriptor)
// Hydrate the types.T from the resolved descriptor. Once we cache
// descriptors, this hydration should install pointers to cached data.
switch t := tdesc.Kind; t {
Expand Down
39 changes: 39 additions & 0 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -4309,6 +4309,45 @@ func (desc *ImmutableTableDescriptor) TypeDesc() *TypeDescriptor {
return nil
}

// MutableTypeDescriptor is a custom type for TypeDescriptors undergoing
// any types of modifications.
type MutableTypeDescriptor struct {
TypeDescriptor

// ClusterVersion represents the version of the type descriptor read
// from the store.
ClusterVersion TypeDescriptor
}

// ImmutableTypeDescriptor is a custom type for wrapping TypeDescriptors
// when used in a read only way.
type ImmutableTypeDescriptor struct {
TypeDescriptor
}

// Avoid linter unused warnings.
var _ = NewMutableCreatedTypeDescriptor

// NewMutableCreatedTypeDescriptor returns a MutableTypeDescriptor from the
// given type descriptor with the cluster version being the zero type. This
// is for a type that is created in the same transaction.
func NewMutableCreatedTypeDescriptor(desc TypeDescriptor) *MutableTypeDescriptor {
return &MutableTypeDescriptor{TypeDescriptor: desc}
}

// NewMutableExistingTypeDescriptor returns a MutableTypeDescriptor from the
// given type descriptor with the cluster version also set to the descriptor.
// This is for types that already exist.
func NewMutableExistingTypeDescriptor(desc TypeDescriptor) *MutableTypeDescriptor {
return &MutableTypeDescriptor{TypeDescriptor: desc, ClusterVersion: desc}
}

// NewImmutableTypeDescriptor returns an ImmutableTypeDescriptor from the
// given TypeDescriptor.
func NewImmutableTypeDescriptor(desc TypeDescriptor) *ImmutableTypeDescriptor {
return &ImmutableTypeDescriptor{TypeDescriptor: desc}
}

// DatabaseDesc implements the ObjectDescriptor interface.
func (desc *TypeDescriptor) DatabaseDesc() *DatabaseDescriptor {
return nil
Expand Down
13 changes: 10 additions & 3 deletions pkg/sql/virtual_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ func (v virtualSchemaEntry) GetObjectByName(
if !ok {
return nil, nil
}
return virtualTypeEntry{desc: sqlbase.MakeSimpleAliasTypeDescriptor(typ)}, nil
return virtualTypeEntry{
desc: sqlbase.MakeSimpleAliasTypeDescriptor(typ),
mutable: flags.RequireMutable,
}, nil
default:
return nil, errors.AssertionFailedf("unknown desired object kind %d", flags.DesiredObjectKind)
}
Expand All @@ -371,11 +374,15 @@ func (e mutableVirtualDefEntry) Desc() catalog.Descriptor {
}

type virtualTypeEntry struct {
desc *sqlbase.TypeDescriptor
desc *sqlbase.TypeDescriptor
mutable bool
}

func (e virtualTypeEntry) Desc() catalog.Descriptor {
return e.desc
if e.mutable {
return sqlbase.NewMutableExistingTypeDescriptor(*e.desc)
}
return sqlbase.NewImmutableTypeDescriptor(*e.desc)
}

type virtualTableConstructor func(context.Context, *planner, string) (planNode, error)
Expand Down

0 comments on commit 8927b11

Please sign in to comment.