Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix expandIndexName to return correct table mutable #81473

Merged
merged 1 commit into from
May 19, 2022
Merged
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
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