Skip to content

Commit

Permalink
Merge #61093
Browse files Browse the repository at this point in the history
61093: opt: support UPSERT with partial UNIQUE WITHOUT INDEX constraints r=mgartner a=mgartner

This commit adds support for both `UPSERT` and
`INSERT...ON CONFLICT...DO UPDATE` statements on tables with partial
`UNIQUE WITHOUT INDEX` constraints.

There is no release note because these constraints are gated behind the
experimental_enable_unique_without_index_constraints session variable.

Release note: None

Release justification: This is required for supporting implicitly
partitioned unique partial indexes.

Co-authored-by: Marcus Gartner <[email protected]>
  • Loading branch information
craig[bot] and mgartner committed Mar 3, 2021
2 parents 4ab29d4 + dd249fc commit fba7ed1
Show file tree
Hide file tree
Showing 5 changed files with 1,057 additions and 69 deletions.
79 changes: 78 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/unique
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ CREATE TABLE uniq_partial (
UNIQUE WITHOUT INDEX (a) WHERE b > 0
)

statement ok
CREATE TABLE uniq_partial_pk (
k INT PRIMARY KEY,
a INT,
b INT,
UNIQUE WITHOUT INDEX (a) WHERE b > 0
)

statement ok
CREATE TYPE region AS ENUM ('us-east', 'us-west', 'eu-west')

Expand Down Expand Up @@ -713,7 +721,7 @@ a b c d e
2 2 2 2 2


# Upsert into a table in which the unique constraints are the suffix of an
# Upsert into a table in which the unique constraints are the suffix of an
# index, and the prefix of the index is an enum. This case uses the default
# value for columns r and j.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_i"\nDETAIL: Key \(i\)=\(2\) already exists\.
Expand All @@ -726,6 +734,75 @@ r s i j
us-west foo 1 1
eu-west bar 2 2

# Upsert into a table with a partial unique constraint.

# Upsert non-conflicting rows.
statement ok
UPSERT INTO uniq_partial_pk VALUES (1, 1, 1), (2, 2, 2), (3, 1, -1)

# Duplicate of a on insert path.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_a"\nDETAIL: Key \(a\)=\(1\) already exists\.
UPSERT INTO uniq_partial_pk VALUES (4, 1, 1)

# Duplicate of a on update path.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_a"\nDETAIL: Key \(a\)=\(1\) already exists\.
UPSERT INTO uniq_partial_pk VALUES (3, 1, 1)

# No duplicate on insert path.
statement ok
UPSERT INTO uniq_partial_pk VALUES (4, 1, -1)

# No duplicate on update path.
statement ok
UPSERT INTO uniq_partial_pk VALUES (2, 1, -1)

query III colnames,rowsort
SELECT * FROM uniq_partial_pk
----
k a b
1 1 1
2 1 -1
3 1 -1
4 1 -1

# On conflict do update with a partial unique constraint.

# Clear the table and insert new rows.
statement ok
DELETE FROM uniq_partial;
INSERT INTO uniq_partial VALUES (1, 1), (2, 2), (1, -1)

# Insert non-conflicting rows.
statement ok
INSERT INTO uniq_partial VALUES (3, 3), (1, -2) ON CONFLICT (a) WHERE b > 0 DO UPDATE SET b = -10

# Insert one conflicting row.
statement ok
INSERT INTO uniq_partial VALUES (4, 4), (3, 30) ON CONFLICT (a) WHERE b > 0 DO UPDATE SET b = 33

# Insert two rows that conflict with each other.
statement error pgcode 21000 UPSERT or INSERT...ON CONFLICT command cannot affect row a second time
INSERT INTO uniq_partial VALUES (5, 5), (5, 50) ON CONFLICT (a) WHERE b > 0 DO UPDATE SET b = 33

# Insert a conflicting row that conflicts after update.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_a"\nDETAIL: Key \(a\)=\(1\) already exists\.
INSERT INTO uniq_partial VALUES (4, 40) ON CONFLICT (a) WHERE b > 0 DO UPDATE SET a = 1

# Insert a conflicting row that does not conflict after update.
statement ok
INSERT INTO uniq_partial VALUES (4, 40) ON CONFLICT (a) WHERE b > 0 DO UPDATE SET a = 1, b = -40

query II colnames,rowsort
SELECT * FROM uniq_partial
----
a b
1 1
1 -1
1 -2
1 -40
2 2
3 33

# Ensure that we do not choose a partial index as the arbiter when there is a
# UNIQUE WITHOUT INDEX constraint.
statement ok
Expand Down
Loading

0 comments on commit fba7ed1

Please sign in to comment.