Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/typecollection/typecollection.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,15 @@ func (pgs *TypeCollection) IterateTypes(ctx context.Context, f func(typ *pgtypes
}
t, err := pgtypes.DeserializeTypeFromCollection(sqlCtx, pgs, data)
if err != nil {
// A type may reference another type that no longer exists (such as a domain over the row type of a
// temporary table from another session). Such a type cannot be loaded, so we skip it rather than
// preventing iteration over every other type.
// TODO: this is a workaround. Postgres tracks dependencies on table row types, so dropping a table (or
// cleaning up temporary tables at session end) either errors or cascades to dependent types, meaning
// such dangling types never exist. Once we track these dependencies, this check should be removed.
if errors.Is(err, pgtypes.ErrUnresolvedTypeDuringDeserialization) {
return nil
}
return err
}
stop, err := f(t.(*pgtypes.DoltgresType))
Expand All @@ -483,6 +492,9 @@ func (pgs *TypeCollection) IterateTypes(ctx context.Context, f func(typ *pgtypes
return nil
}
})
if err == io.EOF {
return nil
}
return err
}

Expand Down
4 changes: 4 additions & 0 deletions server/node/drop_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (c *DropDomain) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) {
}
}

if err = checkTypeDependents(ctx, collection, domain.ID); err != nil {
return nil, err
}

if err = collection.DropType(ctx, typeID); err != nil {
return nil, err
}
Expand Down
44 changes: 44 additions & 0 deletions server/node/drop_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/dolthub/doltgresql/core"
"github.com/dolthub/doltgresql/core/id"
"github.com/dolthub/doltgresql/core/typecollection"
"github.com/dolthub/doltgresql/server/auth"
"github.com/dolthub/doltgresql/server/types"
)
Expand Down Expand Up @@ -147,6 +148,10 @@ func (c *DropType) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) {
}
}

if err = checkTypeDependents(ctx, collection, typ.ID); err != nil {
return nil, err
}

if err = collection.DropType(ctx, typeID); err != nil {
return nil, err
}
Expand Down Expand Up @@ -185,3 +190,42 @@ func (c *DropType) WithResolvedChildren(ctx context.Context, children []any) (an
}
return c, nil
}

// checkTypeDependents returns an error if any other type in the collection depends on the type with the given ID. A type
// depends on the target if it is a domain over the target (or over the target's array type), or a composite type with
// an attribute of the target type (or its array type). The target's own array type is not considered a dependent, as it
// is dropped alongside the target.
func checkTypeDependents(ctx *sql.Context, collection *typecollection.TypeCollection, target id.Type) error {
refersToTarget := func(t *types.DoltgresType) bool {
if t == nil {
return false
}
return t.ID == target || (t.Elem != nil && t.Elem.ID == target)
}
var dependent *types.DoltgresType
err := collection.IterateTypes(ctx, func(t *types.DoltgresType) (stop bool, err error) {
if t.ID == target {
return false, nil
}
if t.TypType == types.TypeType_Domain && refersToTarget(t.BaseTypeType) {
dependent = t
return true, nil
}
for _, attr := range t.CompositeAttrs {
if refersToTarget(attr.Type) {
dependent = t
return true, nil
}
}
return false, nil
})
if err != nil {
return err
}
if dependent != nil {
// TODO: list all dependent objects in a DETAIL, and add a HINT once CASCADE is supported
return errors.Errorf(`cannot drop type %s because other objects depend on it - type %s depends on type %s`,
target.TypeName(), dependent.ID.TypeName(), target.TypeName())
}
return nil
}
6 changes: 5 additions & 1 deletion server/types/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func init() {
types.SetExtendedTypeSerializers(SerializeType, DeserializeType)
}

// ErrUnresolvedTypeDuringDeserialization is returned when a type references another type that no longer exists (such
// as a domain over the row type of a dropped temporary table).
var ErrUnresolvedTypeDuringDeserialization = errors.New("unable to resolve type during deserialization")

// arrayTypeVersion is the serialized version of newly created array types. Each type carries its own version, and
// existing types keep the version that they were created with.
const arrayTypeVersion = 1
Expand Down Expand Up @@ -295,7 +299,7 @@ func recursiveDeserializeType(ctx *sql.Context, typ *DoltgresType, typeColl Type
})
// `GetType` returns a nil type if it cannot be found, so we want to error in that case
if err == nil && recursedType == nil {
return target, typeColl, errors.Errorf("unable to resolve type `%s` during deserialization", target.ID.TypeName())
return target, typeColl, errors.Mark(errors.Errorf("unable to resolve type `%s` during deserialization", target.ID.TypeName()), ErrUnresolvedTypeDuringDeserialization)
}
return recursedType, typeColl, err
}
122 changes: 122 additions & 0 deletions testing/go/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,128 @@ func TestDomain(t *testing.T) {
},
},
},
{
Name: "drop domain with dependent types",
SetUpScript: []string{
`CREATE DOMAIN d_base AS integer;`,
`CREATE DOMAIN d_child AS d_base CHECK (VALUE > 0);`,
`CREATE DOMAIN d_arr AS d_base[];`,
`CREATE TYPE d_comp AS (f1 d_base, f2 text);`,
},
Assertions: []ScriptTestAssertion{
{
Query: `DROP DOMAIN d_base;`,
ExpectedErr: `cannot drop type d_base because other objects depend on it`,
},
{
Query: `SELECT typname FROM pg_type WHERE typname IN ('d_base', 'd_child', 'd_arr', 'd_comp') ORDER BY typname;`,
Expected: []sql.Row{{"d_arr"}, {"d_base"}, {"d_child"}, {"d_comp"}},
},
{
Query: `DROP DOMAIN d_child;`,
Expected: []sql.Row{},
},
{
Query: `DROP DOMAIN d_base;`,
ExpectedErr: `cannot drop type d_base because other objects depend on it`,
},
{
Query: `DROP DOMAIN d_arr;`,
Expected: []sql.Row{},
},
{
Query: `DROP DOMAIN d_base;`,
ExpectedErr: `cannot drop type d_base because other objects depend on it`,
},
{
Query: `DROP TYPE d_comp;`,
Expected: []sql.Row{},
},
{
Query: `DROP DOMAIN d_base;`,
Expected: []sql.Row{},
},
{
Query: `SELECT count(*) FROM pg_type WHERE typname LIKE 'd\_%';`,
Expected: []sql.Row{{0}},
},
},
},
{
Name: "drop domain used by another domain keeps pg_type readable",
SetUpScript: []string{
`CREATE DOMAIN domaindroptest int4;`,
`CREATE DOMAIN dependenttypetest domaindroptest;`,
},
Assertions: []ScriptTestAssertion{
{
Query: `DROP DOMAIN domaindroptest;`,
ExpectedErr: `cannot drop type domaindroptest because other objects depend on it`,
},
{
// The total count changes whenever built-in types are added, so we only verify that pg_type can be read
Query: `SELECT count(*) FROM pg_type;`,
SkipResultsCheck: true,
},
{
Query: `SELECT typname FROM pg_type WHERE typname IN ('domaindroptest', 'dependenttypetest') ORDER BY typname;`,
Expected: []sql.Row{{"dependenttypetest"}, {"domaindroptest"}},
},
},
},
{
Name: "drop type with dependent domain",
SetUpScript: []string{
`CREATE TYPE mood AS ENUM ('sad', 'happy');`,
`CREATE DOMAIN happy_mood AS mood;`,
},
Assertions: []ScriptTestAssertion{
{
Query: `DROP TYPE mood;`,
ExpectedErr: `cannot drop type mood because other objects depend on it`,
},
{
Query: `SELECT typname FROM pg_type WHERE typname IN ('mood', 'happy_mood') ORDER BY typname;`,
Expected: []sql.Row{{"happy_mood"}, {"mood"}},
},
{
Query: `DROP DOMAIN happy_mood;`,
Expected: []sql.Row{},
},
{
Query: `DROP TYPE mood;`,
Expected: []sql.Row{},
},
},
},
{
Name: "drop type with unresolvable domain in collection",
SetUpScript: []string{
`CREATE TEMP TABLE recur1 (f1 int);`,
`CREATE DOMAIN array_of_recur1 AS recur1[];`,
`DROP TABLE recur1;`,
`CREATE TYPE pp_colors AS ENUM ('green', 'blue', 'black');`,
`CREATE TYPE pp_rectype AS (a int, b int);`,
},
Assertions: []ScriptTestAssertion{
{
Query: `DROP TYPE pp_colors;`,
Expected: []sql.Row{},
},
{
Query: `CREATE TABLE pp_recpart (a pp_rectype);`,
Expected: []sql.Row{},
},
{
Query: `DROP TABLE pp_recpart;`,
Expected: []sql.Row{},
},
{
Query: `DROP TYPE pp_rectype;`,
Expected: []sql.Row{},
},
},
},
{
Name: "domain array type column",
SetUpScript: []string{
Expand Down
Loading