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

release-20.2: sql: fix DROP COLUMN bug which ignores containing indexes #54262

Merged
merged 1 commit into from
Sep 14, 2020
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
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