Skip to content

Commit

Permalink
sql: fix expandIndexName to return correct table mutable
Browse files Browse the repository at this point in the history
Fixes cockroachdb#81211
A regression was introduced with cockroachdb#80769 that the table mutable
got created from a uncommitted descriptor. This pr tries to fix
that.

Release note: None.
  • Loading branch information
chengxiong-ruan authored and andrewbaptist committed May 25, 2022
1 parent 09ed5a6 commit e1a9362
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 16 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/rename_index
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,19 @@ SELECT DISTINCT index_name FROM [SHOW INDEXES FROM users]
pk
ufo
bar

# Regression test for #81211
statement ok
CREATE TABLE t1(a int);

statement ok
BEGIN;

statement ok
CREATE INDEX i1 ON t1(a);

statement ok
ALTER INDEX i1 RENAME TO i2;

statement ok
COMMIT;
11 changes: 7 additions & 4 deletions pkg/sql/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,15 +662,15 @@ func expandMutableIndexName(
func expandIndexName(
ctx context.Context,
txn *kv.Txn,
sc resolver.SchemaResolver,
p *planner,
codec keys.SQLCodec,
index *tree.TableIndexName,
requireTable bool,
) (tn *tree.TableName, desc *tabledesc.Mutable, err error) {
tn = &index.Table
if tn.Object() != "" {
// The index and its table prefix must exist already. Resolve the table.
_, desc, err = resolver.ResolveMutableExistingTableObject(ctx, sc, tn, requireTable, tree.ResolveRequireTableOrViewDesc)
_, desc, err = resolver.ResolveMutableExistingTableObject(ctx, p, tn, requireTable, tree.ResolveRequireTableOrViewDesc)
if err != nil {
return nil, nil, err
}
Expand All @@ -681,7 +681,7 @@ func expandIndexName(
return tn, desc, nil
}

found, resolvedPrefix, tbl, _, err := resolver.ResolveIndex(ctx, sc, index, txn, codec, requireTable, false /*requireActiveIndex*/)
found, resolvedPrefix, tbl, _, err := resolver.ResolveIndex(ctx, p, index, txn, codec, requireTable, false /*requireActiveIndex*/)
if err != nil {
return nil, nil, err
}
Expand All @@ -698,7 +698,10 @@ func expandIndexName(
// Memoize the table name that was found. tn is a reference to the table name
// stored in index.Table.
*tn = tableName
tblMutable := tbl.NewBuilder().BuildExistingMutable().(*tabledesc.Mutable)
tblMutable, err := p.Descriptors().GetMutableTableVersionByID(ctx, tbl.GetID(), p.Txn())
if err != nil {
return nil, nil, err
}
return &tableName, tblMutable, nil
}

Expand Down

0 comments on commit e1a9362

Please sign in to comment.