Skip to content

Commit

Permalink
Merge pull request cockroachdb#42838 from rafiss/backport19.2-42797
Browse files Browse the repository at this point in the history
release-19.2: sql: fix ALTER INDEX IF EXISTS for non-existing index
  • Loading branch information
rafiss authored Nov 27, 2019
2 parents 0da08d8 + 57263d5 commit 97051e3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
25 changes: 24 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/drop_index
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ CREATE TABLE users (
statement ok
CREATE TABLE othertable (
x INT,
INDEX baw (x)
y INT,
INDEX baw (x),
INDEX yak (y, x)
)

statement error index name "baw" is ambiguous
Expand All @@ -22,6 +24,27 @@ DROP INDEX baw
statement error index name "baw" is ambiguous
DROP INDEX IF EXISTS baw

statement error index "ark" does not exist
DROP INDEX ark

statement ok
DROP INDEX IF EXISTS ark

statement error index "ark" does not exist
DROP INDEX users@ark

statement ok
DROP INDEX IF EXISTS users@ark

statement ok
DROP INDEX yak

statement ok
CREATE INDEX yak ON othertable (y, x)

statement ok
DROP INDEX IF EXISTS yak

statement ok
DROP TABLE othertable

Expand Down
40 changes: 39 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/rename_index
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,21 @@ CREATE TABLE users (
UNIQUE INDEX bar (id, name)
)

statement ok
CREATE TABLE users_dupe (
id INT PRIMARY KEY,
name VARCHAR NOT NULL,
title VARCHAR,
INDEX foo (name),
UNIQUE INDEX bar (id, name)
)

statement ok
INSERT INTO users VALUES (1, 'tom', 'cat'),(2, 'jerry', 'rat')

statement ok
INSERT INTO users_dupe VALUES (1, 'tom', 'cat'),(2, 'jerry', 'rat')

query TTBITTBB colnames
SHOW INDEXES FROM users
----
Expand All @@ -20,6 +32,16 @@ users foo true 2 id ASC false
users bar false 1 id ASC false false
users bar false 2 name ASC false false

query TTBITTBB colnames
SHOW INDEXES FROM users_dupe
----
table_name index_name non_unique seq_in_index column_name direction storing implicit
users_dupe primary false 1 id ASC false false
users_dupe foo true 1 name ASC false false
users_dupe foo true 2 id ASC false true
users_dupe bar false 1 id ASC false false
users_dupe bar false 2 name ASC false false

statement error index name "bar" already exists
ALTER INDEX users@foo RENAME TO bar

Expand All @@ -29,11 +51,27 @@ ALTER INDEX users@foo RENAME TO ""
statement error index "ffo" does not exist
ALTER INDEX users@ffo RENAME TO ufo

statement error index "ffo" does not exist
ALTER INDEX ffo RENAME TO ufo

statement error index name "foo" is ambiguous
ALTER INDEX foo RENAME TO ufo

statement error index name "foo" is ambiguous
ALTER INDEX IF EXISTS foo RENAME TO ufo

statement ok
ALTER INDEX IF EXISTS users@ffo RENAME TO ufo

# Regression test for #42399.
statement ok
ALTER INDEX IF EXISTS ffo RENAME TO ufo

statement ok
ALTER INDEX users@foo RENAME TO ufooo

statement ok
ALTER INDEX users@foo RENAME TO ufoo
ALTER INDEX IF EXISTS ufooo RENAME TO ufoo

statement ok
ALTER INDEX ufoo RENAME TO ufo
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ func TestParse(t *testing.T) {
{`EXPLAIN ALTER INDEX b RENAME TO b`},
{`ALTER INDEX a@b RENAME TO b`},
{`ALTER INDEX a@primary RENAME TO like`},
{`ALTER INDEX IF EXISTS b RENAME TO b`},
{`ALTER INDEX IF EXISTS a@b RENAME TO b`},
{`ALTER INDEX IF EXISTS a@primary RENAME TO like`},

Expand Down
6 changes: 5 additions & 1 deletion pkg/sql/rename_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ type renameIndexNode struct {
// notes: postgres requires CREATE on the table.
// mysql requires ALTER, CREATE, INSERT on the table.
func (p *planner) RenameIndex(ctx context.Context, n *tree.RenameIndex) (planNode, error) {
_, tableDesc, err := expandMutableIndexName(ctx, p, n.Index, true /* requireTable */)
_, tableDesc, err := expandMutableIndexName(ctx, p, n.Index, !n.IfExists /* requireTable */)
if err != nil {
return nil, err
}
if tableDesc == nil {
// IfExists specified and table did not exist -- noop.
return newZeroNode(nil /* columns */), nil
}

idx, _, err := tableDesc.FindIndexByName(string(n.Index.Index))
if err != nil {
Expand Down

0 comments on commit 97051e3

Please sign in to comment.