diff --git a/pkg/sql/logictest/testdata/logic_test/alter_type b/pkg/sql/logictest/testdata/logic_test/alter_type index 5d52879ccec3..1807c01afc9e 100644 --- a/pkg/sql/logictest/testdata/logic_test/alter_type +++ b/pkg/sql/logictest/testdata/logic_test/alter_type @@ -693,6 +693,7 @@ ALTER TYPE typ_110827 DROP VALUE 'a'; statement error pgcode 2BP01 could not remove enum value "b" as it is being used by table ".*t_110827" ALTER TYPE typ_110827 DROP VALUE 'b'; + subtest end # We accidentally introduced a regression formatting dependent rows out, @@ -711,3 +712,23 @@ statement error pgcode 2BP01 could not remove enum value "a" as it is being used ALTER TYPE typ_127136 DROP VALUE 'a'; subtest end + +# Previously, we did not properly handle scanning index expressions for type +# references when removing an enum value (#127147). +subtest validate_type_in_index_expr + +statement ok +CREATE TYPE typ_127147 AS ENUM ('a', 'b', 'c'); +CREATE TABLE t (x TEXT PRIMARY KEY, INDEX ((x::typ_127147))); +INSERT INTO t VALUES ('a'); + +statement error pgcode 2BP01 could not remove enum value "a" as it is being used by "t" in row: x='a' +ALTER TYPE typ_127147 DROP VALUE 'a'; + +statement ok +TRUNCATE TABLE t; + +statement ok +ALTER TYPE typ_127147 DROP VALUE 'a'; + +subtest end diff --git a/pkg/sql/type_change.go b/pkg/sql/type_change.go index c06cb86d7fb1..ae9a8e8ca43c 100644 --- a/pkg/sql/type_change.go +++ b/pkg/sql/type_change.go @@ -829,6 +829,19 @@ func (t *typeSchemaChanger) canRemoveEnumValueFromTable( } } + // If the descriptor has any inaccessible columns, we need to scan those. + var syntheticDescs []catalog.Descriptor + if len(desc.AccessibleColumns()) != len(desc.PublicColumns()) { + descBuilder := desc.NewBuilder() + fullyAccessibleDesc := descBuilder.BuildExistingMutable().(*tabledesc.Mutable) + for colIdx := range fullyAccessibleDesc.Columns { + if col := &fullyAccessibleDesc.Columns[colIdx]; col.Inaccessible { + col.Inaccessible = false + } + } + syntheticDescs = []catalog.Descriptor{descBuilder.BuildImmutable().(catalog.TableDescriptor)} + } + var query strings.Builder colSelectors := tabledesc.ColumnsSelectors(desc.AccessibleColumns()) columns := tree.AsStringWithFlags(&colSelectors, tree.FmtSerializable) @@ -976,7 +989,12 @@ func (t *typeSchemaChanger) canRemoveEnumValueFromTable( User: username.NodeUserName(), Database: dbDesc.GetName(), } - rows, err := txn.QueryRowEx(ctx, "count-value-usage", txn.KV(), override, query.String()) + var rows tree.Datums + err = txn.WithSyntheticDescriptors(syntheticDescs, func() error { + var err error + rows, err = txn.QueryRowEx(ctx, "count-value-usage", txn.KV(), override, query.String()) + return err + }) if err != nil { return errors.Wrapf(err, validationErr, member.LogicalRepresentation) }