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

sql: fix ALTER PRIMARY KEY with virtual columns on the table #62843

Merged
merged 1 commit into from
Mar 31, 2021
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
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)
)