Skip to content

Commit

Permalink
sql,tree: differentiate UNIQUE and UNIQUE INDEX in CREATE TABLE
Browse files Browse the repository at this point in the history
While we treated the cases of `ALTER TABLE ... ADD CONSTRAINT UNIQUE` from
`CREATE UNIQUE INDEX` we didn't treat the two clauses differently in the
context of `CREATE TABLE`. While functionally they are indentical concepts,
they do display differently and have different semantic meanings (you can't
use drop constraint on the index or drop index on the constraint).

Release note (bug fix): UNIQUE INDEX clauses in a CREATE TABLE statement
are no longer treated as CONSTRAINT UNIQUE clauses.
  • Loading branch information
ajwerner committed May 28, 2021
1 parent f10d2b6 commit aa2e283
Show file tree
Hide file tree
Showing 21 changed files with 198 additions and 163 deletions.
4 changes: 2 additions & 2 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ CREATE TABLE public.partition_by_nothing (
a INT8 NULL,
b INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (pk ASC),
UNIQUE INDEX partition_by_nothing_b_key (b ASC),
INDEX partition_by_nothing_b_idx (b ASC),
FAMILY fam_0_pk_a_b (pk, a, b)
FAMILY fam_0_pk_a_b (pk, a, b),
CONSTRAINT partition_by_nothing_b_key UNIQUE (b)
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ CREATE TABLE public.partition_all_by_nothing (
b INT8 NULL,
c INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (pk ASC),
UNIQUE INDEX partition_all_by_nothing_c_key (c ASC),
UNIQUE INDEX partition_all_by_nothing_b_key (b ASC),
INDEX partition_all_by_nothing_b_idx (b ASC),
FAMILY fam_0_pk_a_b_c (pk, a, b, c)
FAMILY fam_0_pk_a_b_c (pk, a, b, c),
CONSTRAINT partition_all_by_nothing_c_key UNIQUE (c),
CONSTRAINT partition_all_by_nothing_b_key UNIQUE (b)
) PARTITION ALL BY NOTHING

statement error cannot define PARTITION BY on an index if the table has a PARTITION ALL BY definition
Expand All @@ -54,9 +54,9 @@ CREATE TABLE public.partition_all_by_nothing (
b INT8 NULL,
c INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (pk ASC),
UNIQUE INDEX partition_all_by_nothing_c_key (c ASC),
UNIQUE INDEX partition_all_by_nothing_b_key (b ASC),
INDEX partition_all_by_nothing_b_idx (b ASC),
INDEX idx (pk ASC),
FAMILY fam_0_pk_a_b_c (pk, a, b, c)
FAMILY fam_0_pk_a_b_c (pk, a, b, c),
CONSTRAINT partition_all_by_nothing_c_key UNIQUE (c),
CONSTRAINT partition_all_by_nothing_b_key UNIQUE (b)
) PARTITION ALL BY NOTHING
32 changes: 18 additions & 14 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning_implicit
Original file line number Diff line number Diff line change
Expand Up @@ -475,15 +475,15 @@ CREATE TABLE public.t (
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, e)
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d_j_u (pk, pk2, partition_by, a, b, c, d, j, u, e),
CONSTRAINT t_u_key UNIQUE (u),
CONSTRAINT t_b_key UNIQUE (b),
CONSTRAINT t_e_key UNIQUE (e),
CONSTRAINT unique_c_d UNIQUE (c, d)
) PARTITION ALL BY LIST (partition_by) (
PARTITION one VALUES IN ((1)),
PARTITION two VALUES IN ((2))
Expand Down Expand Up @@ -566,16 +566,20 @@ CREATE TABLE public.t (
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),
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, e)
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d_j_u (pk, pk2, partition_by, a, b, c, d, j, u, e),
CONSTRAINT t_pk_key UNIQUE (pk),
CONSTRAINT t_u_key UNIQUE (u),
CONSTRAINT t_b_key UNIQUE (b),
CONSTRAINT t_e_key UNIQUE (e),
CONSTRAINT unique_c_d UNIQUE (c, d),
CONSTRAINT t_u_key UNIQUE (u),
CONSTRAINT t_b_key UNIQUE (b),
CONSTRAINT t_e_key UNIQUE (e),
CONSTRAINT unique_c_d UNIQUE (c, d)
) PARTITION ALL BY LIST (partition_by) (
PARTITION one VALUES IN ((1)),
PARTITION two VALUES IN ((2))
Expand Down Expand Up @@ -673,11 +677,11 @@ CREATE TABLE public.t (
d INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (pk ASC),
INDEX t_a_idx (a ASC),
UNIQUE INDEX t_b_key (b ASC),
UNIQUE INDEX t_c_key (c ASC) WHERE d > 100:::INT8,
INDEX t_partition_by_c_idx (partition_by ASC, c ASC),
INDEX created_idx (c ASC),
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d (pk, pk2, partition_by, a, b, c, d)
FAMILY fam_0_pk_pk2_partition_by_a_b_c_d (pk, pk2, partition_by, a, b, c, d),
CONSTRAINT t_b_key UNIQUE (b),
CONSTRAINT t_c_key UNIQUE (c)
) PARTITION ALL BY RANGE (partition_by) (
PARTITION one VALUES FROM (MINVALUE) TO (2),
PARTITION two VALUES FROM (2) TO (MAXVALUE)
Expand Down
6 changes: 2 additions & 4 deletions pkg/ccl/logictestccl/testdata/logic_test/partitioning_index
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ ok2 CREATE TABLE public.ok2 (
a INT8 NOT NULL,
b INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (a ASC),
UNIQUE INDEX ok2_b_key (b ASC) PARTITION BY LIST (b) (
PARTITION p1 VALUES IN ((1))
),
FAMILY "primary" (a, b)
FAMILY "primary" (a, b),
CONSTRAINT ok2_b_key UNIQUE (b)
)
-- Warning: Partitioned table with no zone configurations.

Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2003,10 +2003,11 @@ func NewTableDesc(
return nil, pgerror.Newf(pgcode.DuplicateRelation, "duplicate index name: %q", d.Name)
}
idx := descpb.IndexDescriptor{
Name: string(d.Name),
Unique: true,
StoreColumnNames: d.Storing.ToStrings(),
Version: indexEncodingVersion,
Name: string(d.Name),
Unique: true,
StoreColumnNames: d.Storing.ToStrings(),
Version: indexEncodingVersion,
CreatedExplicitly: d.ExplicitIndex,
}
columns := d.Columns
if d.Sharded != nil {
Expand Down
36 changes: 18 additions & 18 deletions pkg/sql/logictest/testdata/logic_test/alter_primary_key
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ child CREATE TABLE public.child (
y INT8 NOT NULL,
z INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (x ASC, y ASC, z ASC),
UNIQUE INDEX child_x_key (x ASC),
FAMILY fam_0_x_y_z (x, y, z)
FAMILY fam_0_x_y_z (x, y, z),
CONSTRAINT child_x_key UNIQUE (x)
) INTERLEAVE IN PARENT public.parent (x, y)


Expand Down Expand Up @@ -170,8 +170,8 @@ child CREATE TABLE public.child (
y INT8 NOT NULL,
z INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (y ASC, z ASC),
UNIQUE INDEX child_x_y_z_key (x ASC, y ASC, z ASC),
FAMILY fam_0_x_y_z (x, y, z)
FAMILY fam_0_x_y_z (x, y, z),
CONSTRAINT child_x_y_z_key UNIQUE (x, y, z)
)

# Check zone configs behave as appropriate.
Expand Down Expand Up @@ -223,9 +223,9 @@ child CREATE TABLE public.child (
z INT8 NOT NULL,
w INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (x ASC, y ASC, z ASC),
UNIQUE INDEX child_x_y_key (x ASC, y ASC),
INDEX i (x ASC, w ASC) INTERLEAVE IN PARENT public.parent (x),
FAMILY fam_0_x_y_z_w (x, y, z, w)
FAMILY fam_0_x_y_z_w (x, y, z, w),
CONSTRAINT child_x_y_key UNIQUE (x, y)
) INTERLEAVE IN PARENT public.parent (x)

# If child@i was not properly rewritten, we wouldn't be able to select
Expand Down Expand Up @@ -327,8 +327,8 @@ t CREATE TABLE public.t (
rowid INT8 NOT NULL,
y INT8 NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (y ASC),
UNIQUE INDEX t_rowid_key (rowid ASC),
FAMILY fam_0_rowid_y (rowid, y)
FAMILY fam_0_rowid_y (rowid, y),
CONSTRAINT t_rowid_key UNIQUE (rowid)
)

subtest index_rewrites
Expand Down Expand Up @@ -365,14 +365,14 @@ t CREATE TABLE public.t (
crdb_internal_z_shard_4 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(COALESCE(CAST(z AS STRING), '':::STRING)), 4:::INT8)) STORED,
CONSTRAINT "primary" PRIMARY KEY (y ASC),
UNIQUE INDEX i3 (z ASC) STORING (y),
UNIQUE INDEX t_x_key (x ASC),
INDEX i1 (w ASC),
INDEX i2 (y ASC),
UNIQUE INDEX i4 (z ASC),
UNIQUE INDEX i5 (w ASC) STORING (y),
INVERTED INDEX i6 (v),
INDEX i7 (z ASC) USING HASH WITH BUCKET_COUNT = 4,
FAMILY fam_0_x_y_z_w_v_crdb_internal_z_shard_4 (x, y, z, w, v, crdb_internal_z_shard_4)
FAMILY fam_0_x_y_z_w_v_crdb_internal_z_shard_4 (x, y, z, w, v, crdb_internal_z_shard_4),
CONSTRAINT t_x_key UNIQUE (x)
)

# Test that the indexes we expect got rewritten. All but i3 should have been rewritten,
Expand Down Expand Up @@ -519,9 +519,9 @@ t CREATE TABLE public.t (
crdb_internal_z_shard_5 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(COALESCE(CAST(z AS STRING), '':::STRING)), 5:::INT8)) STORED,
crdb_internal_y_shard_10 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(COALESCE(CAST(y AS STRING), '':::STRING)), 10:::INT8)) STORED,
CONSTRAINT "primary" PRIMARY KEY (y ASC) USING HASH WITH BUCKET_COUNT = 10,
UNIQUE INDEX t_x_key (x ASC),
INDEX i1 (z ASC) USING HASH WITH BUCKET_COUNT = 5,
FAMILY fam_0_x_y_z_crdb_internal_z_shard_5 (x, y, z, crdb_internal_z_shard_5, crdb_internal_y_shard_10)
FAMILY fam_0_x_y_z_crdb_internal_z_shard_5 (x, y, z, crdb_internal_z_shard_5, crdb_internal_y_shard_10),
CONSTRAINT t_x_key UNIQUE (x)
)

query T
Expand Down Expand Up @@ -583,9 +583,9 @@ t CREATE TABLE public.t (
y INT8 NOT NULL,
z INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (y ASC),
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH BUCKET_COUNT = 5,
INDEX i (z ASC),
FAMILY fam_0_x_y_z_crdb_internal_x_shard_5 (x, y, z, crdb_internal_x_shard_5)
FAMILY fam_0_x_y_z_crdb_internal_x_shard_5 (x, y, z, crdb_internal_x_shard_5),
CONSTRAINT t_x_key UNIQUE (x)
)

query III
Expand Down Expand Up @@ -1214,8 +1214,8 @@ t CREATE TABLE public.t (
y INT8 NOT NULL,
crdb_internal_y_shard_2 INT4 NOT VISIBLE NOT NULL AS (mod(fnv32(COALESCE(CAST(y AS STRING), '':::STRING)), 2:::INT8)) STORED,
CONSTRAINT "primary" PRIMARY KEY (y ASC) USING HASH WITH BUCKET_COUNT = 2,
UNIQUE INDEX t_x_key (x ASC) USING HASH WITH BUCKET_COUNT = 2,
FAMILY fam_0_x_y_crdb_internal_x_shard_2 (x, y, crdb_internal_x_shard_2, crdb_internal_y_shard_2)
FAMILY fam_0_x_y_crdb_internal_x_shard_2 (x, y, crdb_internal_x_shard_2, crdb_internal_y_shard_2),
CONSTRAINT t_x_key UNIQUE (x)
)

# Regression for #49079.
Expand Down Expand Up @@ -1422,6 +1422,6 @@ table_with_virtual_cols CREATE TABLE public.table_with_virtual_cols (
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)
FAMILY fam_0_id_new_pk (id, new_pk),
CONSTRAINT table_with_virtual_cols_id_key UNIQUE (id)
)
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -1151,8 +1151,8 @@ t3 CREATE TABLE public.t3 (
x INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x),
UNIQUE INDEX t3_x_key (x ASC),
FAMILY "primary" (y, rowid, x)
FAMILY "primary" (y, rowid, x),
CONSTRAINT t3_x_key UNIQUE (x)
)

# We allowed the foreign key validation code to look into the mutations
Expand Down Expand Up @@ -1345,8 +1345,8 @@ t2 CREATE TABLE public.t2 (
x INT8 NULL DEFAULT 1:::INT8,
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
CONSTRAINT fk_x_ref_t1 FOREIGN KEY (x) REFERENCES public.t1(x),
UNIQUE INDEX t2_x_key (x ASC),
FAMILY "primary" (y, rowid, x)
FAMILY "primary" (y, rowid, x),
CONSTRAINT t2_x_key UNIQUE (x)
)

# Regression 50069, computed exprs must only refer to columns inside the
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/create_statements
Original file line number Diff line number Diff line change
Expand Up @@ -3708,15 +3708,15 @@ CREATE TABLE public.v (
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
CONSTRAINT "fk_'_ref_t" FOREIGN KEY ("'") REFERENCES public.t(rowid),
CONSTRAINT fk_s_ref_v FOREIGN KEY (s) REFERENCES public.v(s),
UNIQUE INDEX v_s_key (s ASC),
FAMILY "primary" ("'", s, rowid)
FAMILY "primary" ("'", s, rowid),
CONSTRAINT v_s_key UNIQUE (s)
) CREATE TABLE public.v (
"'" INT8 NULL,
s STRING NULL,
rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
CONSTRAINT "primary" PRIMARY KEY (rowid ASC),
UNIQUE INDEX v_s_key (s ASC),
FAMILY "primary" ("'", s, rowid)
FAMILY "primary" ("'", s, rowid),
CONSTRAINT v_s_key UNIQUE (s)
) {"ALTER TABLE public.v ADD CONSTRAINT \"fk_'_ref_t\" FOREIGN KEY (\"'\") REFERENCES public.t(rowid)","ALTER TABLE public.v ADD CONSTRAINT fk_s_ref_v FOREIGN KEY (s) REFERENCES public.v(s)"} {"ALTER TABLE public.v VALIDATE CONSTRAINT \"fk_'_ref_t\"","ALTER TABLE public.v VALIDATE CONSTRAINT fk_s_ref_v"}
CREATE TABLE public.c (
a INT8 NOT NULL,
Expand Down
12 changes: 6 additions & 6 deletions pkg/sql/logictest/testdata/logic_test/create_table
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ like_indexes CREATE TABLE public.like_indexes (
j JSONB NULL,
k INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (a ASC, b ASC),
UNIQUE INDEX foo (b DESC, c ASC),
INDEX like_table_c_idx (c ASC) STORING (j),
INVERTED INDEX like_table_j_idx (j),
FAMILY "primary" (a, b, c, h, j, k)
FAMILY "primary" (a, b, c, h, j, k),
CONSTRAINT foo UNIQUE (b, c)
)

# INCLUDING GENERATED adds "generated columns", aka stored columns.
Expand Down Expand Up @@ -247,14 +247,14 @@ like_all CREATE TABLE public.like_all (
j JSONB NULL,
k INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (a ASC, b ASC),
UNIQUE INDEX foo (b DESC, c ASC),
INDEX like_table_c_idx (c ASC) STORING (j),
INVERTED INDEX like_table_j_idx (j),
FAMILY "primary" (a, b, c, h, j, k),
CONSTRAINT check_a CHECK (a > 3:::INT8),
CONSTRAINT unique_k UNIQUE WITHOUT INDEX (k),
CONSTRAINT unique_h UNIQUE WITHOUT INDEX (h),
CONSTRAINT unique_h_1 UNIQUE WITHOUT INDEX (h) WHERE h > 0:::INT8
CONSTRAINT unique_h_1 UNIQUE WITHOUT INDEX (h) WHERE h > 0:::INT8,
CONSTRAINT foo UNIQUE (b, c)
)

statement ok
Expand All @@ -273,10 +273,10 @@ like_mixed CREATE TABLE public.like_mixed (
j JSONB NULL,
k INT8 NULL,
CONSTRAINT "primary" PRIMARY KEY (a ASC, b ASC),
UNIQUE INDEX foo (b DESC, c ASC),
INDEX like_table_c_idx (c ASC) STORING (j),
INVERTED INDEX like_table_j_idx (j),
FAMILY "primary" (a, b, c, h, j, k)
FAMILY "primary" (a, b, c, h, j, k),
CONSTRAINT foo UNIQUE (b, c)
)

statement ok
Expand Down
18 changes: 14 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/drop_index
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ CREATE TABLE users (
title VARCHAR,
INDEX foo (name),
UNIQUE INDEX bar (id, name),
INDEX baw (name, title)
INDEX baw (name, title),
CONSTRAINT bax UNIQUE (id, name)
)

statement ok
Expand Down Expand Up @@ -58,6 +59,8 @@ SHOW INDEXES FROM users
table_name index_name non_unique seq_in_index column_name direction storing implicit
users bar false 1 id ASC false false
users bar false 2 name ASC false false
users bax false 1 id ASC false false
users bax false 2 name ASC false false
users foo true 1 name ASC false false
users foo true 2 id ASC false true
users primary false 1 id ASC false false
Expand All @@ -74,6 +77,8 @@ SHOW INDEXES FROM users
table_name index_name non_unique seq_in_index column_name direction storing implicit
users bar false 1 id ASC false false
users bar false 2 name ASC false false
users bax false 1 id ASC false false
users bax false 2 name ASC false false
users foo true 1 name ASC false false
users foo true 2 id ASC false true
users primary false 1 id ASC false false
Expand All @@ -89,6 +94,8 @@ SHOW INDEXES FROM users
table_name index_name non_unique seq_in_index column_name direction storing implicit
users bar false 1 id ASC false false
users bar false 2 name ASC false false
users bax false 1 id ASC false false
users bax false 2 name ASC false false
users primary false 1 id ASC false false

user testuser
Expand All @@ -104,13 +111,16 @@ GRANT CREATE ON TABLE users TO testuser
user testuser

statement error in use as unique constraint
DROP INDEX users@bar
DROP INDEX users@bax

statement error in use as unique constraint
DROP INDEX users@bar RESTRICT
DROP INDEX users@bax RESTRICT

statement ok
DROP INDEX users@bax CASCADE

statement ok
DROP INDEX users@bar CASCADE
DROP INDEX users@bar

query TTBITTBB colnames
SHOW INDEXES FROM users
Expand Down
Loading

0 comments on commit aa2e283

Please sign in to comment.