Skip to content

Commit

Permalink
sql: populate the pg_enum catalog table
Browse files Browse the repository at this point in the history
Fixes #48359.

Release note (sql change): Populate the catalog table
`pg_catalog.pg_enum`.
  • Loading branch information
rohany committed May 18, 2020
1 parent 131500c commit 7dc56cc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
22 changes: 12 additions & 10 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -916,13 +916,22 @@ JOIN pg_constraint ON objid=pg_constraint.oid AND refobjid=pg_constraint.conindi
contype
f

## pg_catalog.pg_type

# Create some user defined types for testing.
## pg_catalog.pg_enum
statement ok
CREATE TYPE newtype1 AS ENUM ('v1', 'v2');
CREATE TYPE newtype2 AS ENUM ('v3', 'v4')

query OORT colnames
SELECT * FROM pg_enum
----
oid enumtypid enumsortorder enumlabel
1043025669 100063 0 v1
1043025861 100063 1 v2
1865129000 100064 0 v3
1865129192 100064 1 v4

## pg_catalog.pg_type

query OTOOIBT colnames
SELECT oid, typname, typnamespace, typowner, typlen, typbyval, typtype
FROM pg_catalog.pg_type
Expand Down Expand Up @@ -1626,13 +1635,6 @@ SELECT objoid, classoid, description FROM pg_catalog.pg_shdescription
----
objoid classoid description

## pg_catalog.pg_enum

query OORT colnames
SELECT * FROM pg_catalog.pg_enum
----
oid enumtypid enumsortorder enumlabel

## pg_catalog.pg_event_trigger

query TTOOTT colnames
Expand Down
53 changes: 51 additions & 2 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,8 +1436,43 @@ CREATE TABLE pg_catalog.pg_enum (
enumsortorder FLOAT4,
enumlabel STRING
)`,
populate: func(_ context.Context, p *planner, _ *DatabaseDescriptor, addRow func(...tree.Datum) error) error {
// Enum types are not currently supported.
populate: func(ctx context.Context, p *planner, dbContext *DatabaseDescriptor, addRow func(...tree.Datum) error) error {
h := makeOidHasher()
descs, err := p.Tables().getAllDescriptors(ctx, p.txn)
if err != nil {
return err
}
for _, desc := range descs {
typDesc, ok := desc.(*sqlbase.TypeDescriptor)
if !ok {
continue
}
if dbContext != nil && typDesc.ParentID != dbContext.ID {
continue
}
if typDesc.Kind != sqlbase.TypeDescriptor_ENUM {
continue
}
// Generate a row for each member of the enum. We don't represent enums
// internally using floats for ordering like Postgres, so just pick a
// float entry for the rows.
typ := types.MakeEnum(uint32(typDesc.ID))
if err := typDesc.HydrateTypeInfo(typ); err != nil {
return err
}
enumData := typ.TypeMeta.EnumData
typOID := tree.NewDOid(tree.DInt(typ.Oid()))
for i := range enumData.LogicalRepresentations {
if err := addRow(
h.EnumEntryOid(typOID, enumData.PhysicalRepresentations[i]),
typOID,
tree.NewDFloat(tree.DFloat(float64(i))),
tree.NewDString(enumData.LogicalRepresentations[i]),
); err != nil {
return err
}
}
}
return nil
},
}
Expand Down Expand Up @@ -3098,6 +3133,12 @@ func (h oidHasher) writeStr(s string) {
}
}

func (h oidHasher) writeBytes(b []byte) {
if _, err := h.h.Write(b); err != nil {
panic(err)
}
}

func (h oidHasher) writeUInt8(i uint8) {
if err := binary.Write(h.h, binary.BigEndian, i); err != nil {
panic(err)
Expand Down Expand Up @@ -3135,6 +3176,7 @@ const (
userTypeTag
collationTypeTag
operatorTypeTag
enumEntryTypeTag
)

func (h oidHasher) writeTypeTag(tag oidTypeTag) {
Expand Down Expand Up @@ -3287,6 +3329,13 @@ func (h oidHasher) OperatorOid(name string, leftType, rightType, returnType *tre
return h.getOid()
}

func (h oidHasher) EnumEntryOid(typOID *tree.DOid, physicalRep []byte) *tree.DOid {
h.writeTypeTag(enumEntryTypeTag)
h.writeOID(typOID)
h.writeBytes(physicalRep)
return h.getOid()
}

func tableOid(id sqlbase.ID) *tree.DOid {
return tree.NewDOid(tree.DInt(id))
}
Expand Down

0 comments on commit 7dc56cc

Please sign in to comment.