Skip to content

Commit

Permalink
sql: don't create old when changing between hash sharded indexes
Browse files Browse the repository at this point in the history
Fixes #45889.

Release note (sql change): This PR causes primary key changes to
not create a copy of the old primary key if the primary key change
is done from a hash sharded index into another hash sharded index.
  • Loading branch information
rohany committed Mar 9, 2020
1 parent e0ca60f commit 1175374
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,9 @@ func (n *alterTableNode) startExec(params runParams) error {
// does, but doesn't store anything. We only recreate the index if
// the table has a primary key (no DROP PRIMARY KEY statements have
// been executed), and the primary key is not the default rowid key.
if n.tableDesc.HasPrimaryKey() && !n.tableDesc.IsPrimaryIndexDefaultRowID() {
if n.tableDesc.HasPrimaryKey() &&
!n.tableDesc.IsPrimaryIndexDefaultRowID() &&
!(n.tableDesc.PrimaryIndex.IsSharded() && t.Sharded != nil) {
oldPrimaryIndexCopy := protoutil.Clone(&n.tableDesc.PrimaryIndex).(*sqlbase.IndexDescriptor)
// Clear the name of the index so that it gets generated by AllocateIDs.
oldPrimaryIndexCopy.Name = ""
Expand Down
15 changes: 15 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -961,3 +961,18 @@ t2 CREATE TABLE t2 (
FAMILY fam_0_x (x),
FAMILY fam_1_y (y)
) INTERLEAVE IN PARENT t (x)

# Regression for #45889.
statement ok
DROP TABLE IF EXISTS t CASCADE;
CREATE TABLE t (x INT PRIMARY KEY USING HASH WITH BUCKET_COUNT = 2);
ALTER TABLE t ALTER PRIMARY KEY USING COLUMNS (x) USING HASH WITH BUCKET_COUNT=3

query TT
SHOW CREATE t
----
t CREATE TABLE t (
x INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (x ASC) USING HASH WITH BUCKET_COUNT = 3,
FAMILY "primary" (crdb_internal_x_shard_2, x, crdb_internal_x_shard_3)
)

0 comments on commit 1175374

Please sign in to comment.