Skip to content

Commit

Permalink
Merge pull request #55822 from ajwerner/backport20.2-55766
Browse files Browse the repository at this point in the history
release-20.2: sql: fix bug dropping shard column which is not the last column
  • Loading branch information
ajwerner authored Nov 17, 2020
2 parents e2bb0ae + 96411b9 commit 06d1eb1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
12 changes: 4 additions & 8 deletions pkg/sql/drop_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,10 @@ func (n *dropIndexNode) dropShardColumnAndConstraint(
tableDesc.AddColumnMutation(shardColDesc, descpb.DescriptorMutation_DROP)
for i := range tableDesc.Columns {
if tableDesc.Columns[i].ID == shardColDesc.ID {
tmp := tableDesc.Columns[:0]
for j, col := range tableDesc.Columns {
if i == j {
continue
}
tmp = append(tmp, col)
}
tableDesc.Columns = tmp
// Note the third slice parameter which will force a copy of the backing
// array if the column being removed is not the last column.
tableDesc.Columns = append(tableDesc.Columns[:i:i],
tableDesc.Columns[i+1:]...)
break
}
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/hash_sharded_index
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,24 @@ ALTER TABLE rename_column RENAME crdb_internal_c2_shard_8 TO foo;

statement ok
DROP TABLE rename_column;

# This is a regression test for a bug whereby the dropping of a hash column
# could result in an invalid descriptor and would fail. The underlying bug was
# due to a column descriptor pointer to a slice being clobbered. See #55766.
subtest drop_earlier_column_due_to_hash_sharded_index

statement ok
CREATE TABLE IF NOT EXISTS drop_earlier_hash_column (
i INT PRIMARY KEY,
j INT,
k INT
);

statement ok
CREATE INDEX h1 ON drop_earlier_hash_column(j) USING HASH WITH BUCKET_COUNT = 8;

statement ok
CREATE INDEX h2 ON drop_earlier_hash_column(k) USING HASH WITH BUCKET_COUNT = 8;

statement ok
DROP INDEX h1;
2 changes: 1 addition & 1 deletion pkg/sql/pgwire/testdata/pgtest/notice
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Query {"String": "DROP INDEX t_x_idx"}
until crdb_only
CommandComplete
----
{"Severity":"NOTICE","Code":"00000","Message":"the data for dropped indexes is reclaimed asynchronously","Detail":"","Hint":"The reclamation delay can be customized in the zone configuration for the table.","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"drop_index.go","Line":524,"Routine":"dropIndexByName","UnknownFields":null}
{"Severity":"NOTICE","Code":"00000","Message":"the data for dropped indexes is reclaimed asynchronously","Detail":"","Hint":"The reclamation delay can be customized in the zone configuration for the table.","Position":0,"InternalPosition":0,"InternalQuery":"","Where":"","SchemaName":"","TableName":"","ColumnName":"","DataTypeName":"","ConstraintName":"","File":"drop_index.go","Line":520,"Routine":"dropIndexByName","UnknownFields":null}
{"Type":"CommandComplete","CommandTag":"DROP INDEX"}

until noncrdb_only
Expand Down

0 comments on commit 06d1eb1

Please sign in to comment.