Skip to content

Commit

Permalink
sql: bugfixes around writing the old primary key in pk changes
Browse files Browse the repository at this point in the history
This PR fixes two bugs:
* The logic for when to rewrite the old primary key was broken
  resulting in the old primary key not being rewritten in many cases.
* The old primary key being created was not properly dealing with
  its dangling interleave information.

Release note: None
  • Loading branch information
rohany committed Jan 29, 2020
1 parent 20908ab commit 4c1cc39
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/sql/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func (n *alterTableNode) startExec(params runParams) error {
// Create a new index that indexes everything the old primary index does, but doesn't store anything.
// TODO (rohany): is there an easier way of checking if the existing primary index was the
// automatically created one?
if len(n.tableDesc.PrimaryIndex.ColumnNames) == 1 && n.tableDesc.PrimaryIndex.ColumnNames[0] != "rowid" {
if !(len(n.tableDesc.PrimaryIndex.ColumnNames) == 1 && n.tableDesc.PrimaryIndex.ColumnNames[0] == "rowid") {
oldPrimaryIndexCopy := protoutil.Clone(&n.tableDesc.PrimaryIndex).(*sqlbase.IndexDescriptor)
name := generateUniqueConstraintName(
"old_primary_key",
Expand All @@ -408,6 +408,9 @@ func (n *alterTableNode) startExec(params runParams) error {
oldPrimaryIndexCopy.Name = name
oldPrimaryIndexCopy.StoreColumnIDs = nil
oldPrimaryIndexCopy.StoreColumnNames = nil
// Make the copy of the old primary index not-interleaved. This decision
// can be revisited based on user experience.
oldPrimaryIndexCopy.Interleave = sqlbase.InterleaveDescriptor{}
if err := addIndexMutationWithSpecificPrimaryKey(n.tableDesc, oldPrimaryIndexCopy, newPrimaryIndexDesc); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ child CREATE TABLE child (
y INT8 NOT NULL,
z INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (y ASC, z ASC),
UNIQUE INDEX old_primary_key (x ASC, y ASC, z ASC),
FAMILY fam_0_x_y_z (x, y, z)
)

Expand Down Expand Up @@ -194,6 +195,7 @@ child CREATE TABLE child (
z INT8 NOT NULL,
w INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (x ASC, y ASC, z ASC),
UNIQUE INDEX old_primary_key (x ASC, y ASC),
INDEX i (x ASC, w ASC) INTERLEAVE IN PARENT parent (x),
FAMILY fam_0_x_y_z_w (x, y, z, w)
) INTERLEAVE IN PARENT parent (x)
Expand Down

0 comments on commit 4c1cc39

Please sign in to comment.