From e1a9362c244a88579c516285d9c709db56bae443 Mon Sep 17 00:00:00 2001 From: Chengxiong Ruan Date: Wed, 18 May 2022 16:25:26 -0400 Subject: [PATCH] sql: fix expandIndexName to return correct table mutable Fixes #81211 A regression was introduced with #80769 that the table mutable got created from a uncommitted descriptor. This pr tries to fix that. Release note: None. --- .../logictest/testdata/logic_test/rename_index | 16 ++++++++++++++++ pkg/sql/resolver.go | 11 +++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pkg/sql/logictest/testdata/logic_test/rename_index b/pkg/sql/logictest/testdata/logic_test/rename_index index dc40e6ed0245..83c4c44b393c 100644 --- a/pkg/sql/logictest/testdata/logic_test/rename_index +++ b/pkg/sql/logictest/testdata/logic_test/rename_index @@ -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; diff --git a/pkg/sql/resolver.go b/pkg/sql/resolver.go index e92ca975c093..21c8bf416819 100644 --- a/pkg/sql/resolver.go +++ b/pkg/sql/resolver.go @@ -662,7 +662,7 @@ func expandMutableIndexName( func expandIndexName( ctx context.Context, txn *kv.Txn, - sc resolver.SchemaResolver, + p *planner, codec keys.SQLCodec, index *tree.TableIndexName, requireTable bool, @@ -670,7 +670,7 @@ func expandIndexName( 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 } @@ -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 } @@ -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 }