Skip to content

Commit

Permalink
Merge pull request #127455 from cockroachdb/blathers/backport-release…
Browse files Browse the repository at this point in the history
…-24.2-127247

release-24.2: sql: cannot drop enum values if referenced in index expressions
  • Loading branch information
fqazi authored Jul 27, 2024
2 parents 6dfc822 + 8ea8913 commit 742a225
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_type
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
20 changes: 19 additions & 1 deletion pkg/sql/type_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,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)
Expand Down Expand Up @@ -984,7 +997,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)
}
Expand Down

0 comments on commit 742a225

Please sign in to comment.