Skip to content

Commit

Permalink
Merge pull request #54262 from otan-cockroach/backport20.2-54228
Browse files Browse the repository at this point in the history
release-20.2: sql: fix DROP COLUMN bug which ignores containing indexes
  • Loading branch information
otan committed Sep 14, 2020
2 parents 5ab29cb + 5346cd6 commit 4182aaa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ func (n *alterTableNode) startExec(params runParams) error {
return pgerror.Newf(pgcode.InvalidColumnReference,
"column %q is referenced by the primary key", colToDrop.Name)
}
var idxNamesToDelete []string
for _, idx := range n.tableDesc.AllNonDropIndexes() {
// We automatically drop indexes that reference the column
// being dropped.
Expand Down Expand Up @@ -466,15 +467,19 @@ func (n *alterTableNode) startExec(params runParams) error {

// Perform the DROP.
if containsThisColumn {
jobDesc := fmt.Sprintf("removing index %q dependent on column %q which is being"+
" dropped; full details: %s", idx.Name, colToDrop.ColName(),
tree.AsStringWithFQNames(n.n, params.Ann()))
if err := params.p.dropIndexByName(
params.ctx, tn, tree.UnrestrictedName(idx.Name), n.tableDesc, false,
t.DropBehavior, ignoreIdxConstraint, jobDesc,
); err != nil {
return err
}
idxNamesToDelete = append(idxNamesToDelete, idx.Name)
}
}

for _, idxName := range idxNamesToDelete {
jobDesc := fmt.Sprintf("removing index %q dependent on column %q which is being"+
" dropped; full details: %s", idxName, colToDrop.ColName(),
tree.AsStringWithFQNames(n.n, params.Ann()))
if err := params.p.dropIndexByName(
params.ctx, tn, tree.UnrestrictedName(idxName), n.tableDesc, false,
t.DropBehavior, ignoreIdxConstraint, jobDesc,
); err != nil {
return err
}
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -1364,3 +1364,23 @@ statement ok
CREATE TABLE t52816 (x INT, y INT);
ALTER TABLE t52816 RENAME COLUMN x TO x2, RENAME COLUMN y TO y;
SELECT x2, y FROM t52816

# Regression test for #54196.
statement ok
CREATE TABLE IF NOT EXISTS regression_54196 (
col1 int,
col2 int,
col3 int,
INDEX (col1, col2),
INDEX (col1, col3),
INDEX (col2, col3)
); ALTER TABLE regression_54196 DROP COLUMN col1

query TT
SELECT index_name, column_name FROM [SHOW INDEXES FROM regression_54196]
ORDER BY index_name, column_name ASC
----
primary rowid
regression_54196_col2_col3_idx col2
regression_54196_col2_col3_idx col3
regression_54196_col2_col3_idx rowid

0 comments on commit 4182aaa

Please sign in to comment.