Skip to content

Commit

Permalink
Merge #62843
Browse files Browse the repository at this point in the history
62843: sql: fix ALTER PRIMARY KEY with virtual columns on the table r=rafiss a=otan

Release note (bug fix): Fix a bug where ALTER PRIMARY KEY would fail if
the table contained virtual columns.

Co-authored-by: Oliver Tan <[email protected]>
  • Loading branch information
craig[bot] and otan committed Mar 31, 2021
2 parents 45f74b4 + c587797 commit c6125c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/sql/alter_primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ func (p *planner) AlterPrimaryKey(
// to consider the indexed columns to be newPrimaryIndexDesc.ColumnIDs.
newPrimaryIndexDesc.StoreColumnNames, newPrimaryIndexDesc.StoreColumnIDs = nil, nil
for _, col := range tableDesc.Columns {
// We do not store virtual columns.
if col.Virtual {
continue
}

containsCol := false
for _, colID := range newPrimaryIndexDesc.ColumnIDs {
if colID == col.ID {
Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -1404,3 +1404,24 @@ drop table child3;

statement ok
drop table parent3;

statement ok
CREATE TABLE table_with_virtual_cols (
id INT PRIMARY KEY,
new_pk INT NOT NULL,
virtual_col INT AS (1::int) VIRTUAL,
FAMILY (id, new_pk)
);
ALTER TABLE table_with_virtual_cols ALTER PRIMARY KEY USING COLUMNS (new_pk)

query TT
SHOW CREATE TABLE table_with_virtual_cols
----
table_with_virtual_cols CREATE TABLE public.table_with_virtual_cols (
id INT8 NOT NULL,
new_pk INT8 NOT NULL,
virtual_col INT8 NULL AS (1:::INT8) VIRTUAL,
CONSTRAINT "primary" PRIMARY KEY (new_pk ASC),
UNIQUE INDEX table_with_virtual_cols_id_key (id ASC),
FAMILY fam_0_id_new_pk (id, new_pk)
)

0 comments on commit c6125c3

Please sign in to comment.