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 ADD COLUMN ... UNIQUE for PARTITION ALL BY tables #63123

Merged
merged 1 commit into from
Apr 7, 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
15 changes: 13 additions & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning_implicit
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ query T noticetrace
CREATE INDEX created_idx ON t(c)
----

statement ok
ALTER TABLE t ADD COLUMN e INT8 NOT NULL UNIQUE

statement ok
ALTER TABLE t ADD CONSTRAINT unique_c_d UNIQUE(c, d)

Expand All @@ -470,15 +473,17 @@ CREATE TABLE public.t (
d INT8 NOT NULL,
j JSONB NULL,
u STRING NULL,
e INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (pk ASC),
UNIQUE INDEX t_u_key (u ASC),
INDEX t_a_idx (a ASC),
UNIQUE INDEX t_b_key (b ASC),
INDEX t_partition_by_c_idx (partition_by ASC, c ASC),
INVERTED INDEX t_j_idx (j),
INDEX created_idx (c ASC),
UNIQUE INDEX t_e_key (e ASC),
UNIQUE INDEX unique_c_d (c ASC, d ASC),
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d_j_u (pk, pk2, partition_by, a, b, c, d, j, u)
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d_j_u (pk, pk2, partition_by, a, b, c, d, j, u, e)
) PARTITION ALL BY LIST (partition_by) (
PARTITION one VALUES IN ((1)),
PARTITION two VALUES IN ((2))
Expand All @@ -499,6 +504,8 @@ t_a_idx a false
t_a_idx partition_by true
t_b_key b false
t_b_key partition_by true
t_e_key e false
t_e_key partition_by true
t_j_idx j false
t_j_idx partition_by true
t_partition_by_c_idx c false
Expand All @@ -525,6 +532,7 @@ CREATE TABLE public.t (
d INT8 NOT NULL,
j JSONB NULL,
u STRING NULL,
e INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (pk2 ASC),
UNIQUE INDEX t_pk_key (pk ASC),
UNIQUE INDEX t_u_key (u ASC),
Expand All @@ -533,8 +541,9 @@ CREATE TABLE public.t (
INDEX t_partition_by_c_idx (partition_by ASC, c ASC),
INVERTED INDEX t_j_idx (j),
INDEX created_idx (c ASC),
UNIQUE INDEX t_e_key (e ASC),
UNIQUE INDEX unique_c_d (c ASC, d ASC),
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d_j_u (pk, pk2, partition_by, a, b, c, d, j, u)
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d_j_u (pk, pk2, partition_by, a, b, c, d, j, u, e)
) PARTITION ALL BY LIST (partition_by) (
PARTITION one VALUES IN ((1)),
PARTITION two VALUES IN ((2))
Expand All @@ -555,6 +564,8 @@ t_a_idx a false
t_a_idx partition_by true
t_b_key b false
t_b_key partition_by true
t_e_key e false
t_e_key partition_by true
t_j_idx j false
t_j_idx partition_by true
t_partition_by_c_idx c false
Expand Down
27 changes: 27 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/regional_by_row
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,33 @@ ALTER TABLE regional_by_row_table ALTER PRIMARY KEY USING COLUMNS(pk2) USING HAS
statement error interleaved tables are not compatible with REGIONAL BY ROW tables
CREATE INDEX bad_idx ON regional_by_row_table(pk) INTERLEAVE IN PARENT parent_table(pk)

# Try add a new unique column.
statement ok
ALTER TABLE regional_by_row_table ADD COLUMN unique_col INT8 NOT NULL UNIQUE

query T
SELECT create_statement FROM [SHOW CREATE TABLE regional_by_row_table]
----
CREATE TABLE public.regional_by_row_table (
pk INT8 NOT NULL,
pk2 INT8 NOT NULL,
a INT8 NOT NULL,
b INT8 NOT NULL,
j JSONB NULL,
crdb_region public.crdb_internal_region NOT VISIBLE NOT NULL DEFAULT default_to_database_primary_region(gateway_region())::public.crdb_internal_region,
unique_col INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (pk ASC),
INDEX regional_by_row_table_a_idx (a ASC),
UNIQUE INDEX regional_by_row_table_b_key (b ASC),
INVERTED INDEX regional_by_row_table_j_idx (j),
UNIQUE INDEX regional_by_row_table_unique_col_key (unique_col ASC),
FAMILY fam_0_pk_pk2_a_b_j_crdb_region (pk, pk2, a, b, j, crdb_region, unique_col)
) LOCALITY REGIONAL BY ROW;
ALTER PARTITION "us-east-1" OF INDEX multi_region_test_db.public.regional_by_row_table@regional_by_row_table_a_idx CONFIGURE ZONE USING "gc.ttlseconds" = 10

statement ok
ALTER TABLE regional_by_row_table DROP COLUMN unique_col

# Insert some rows into the regional_by_row_table.
query TI
INSERT INTO regional_by_row_table (pk, pk2, a, b, j) VALUES
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ func (p *planner) addColumnImpl(
}
incTelemetryForNewColumn(d, col)

// Ensure all new indexes are partitioned appropriately.
if idx != nil {
*idx, err = p.configureIndexDescForNewIndexPartitioning(
params.ctx,
desc,
*idx,
nil, /* PartitionByIndex */
)
if err != nil {
return err
}
}

// If the new column has a DEFAULT expression that uses a sequence, add references between
// its descriptor and this column descriptor.
if d.HasDefaultExpr() {
Expand Down