Skip to content

Commit

Permalink
sql: add spaces to delimiter in uniqueness violation error
Browse files Browse the repository at this point in the history
Whenever a mutation statement violates a uniqueness constraint we return
a specific error which shows the affected row. This error is formatted
to match the corresponding error in Postgres.

We create this error in two places. In one place
(`pkg/sql/opt/exec/execbuilder/mutation.go`) we were using ", " as the
delimter between values, which matches Postgres. But in the other place
(`pkg/sql/row/errors.go`) we were using "," (without a space).

This patch adds the space.

Epic: None

Release note (bug fix): Fix formatting of uniqueness violation errors to
match the corresponding errors from PostgreSQL.
  • Loading branch information
michae2 committed Feb 18, 2023
1 parent c00dbe8 commit ddf0a70
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/insert
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ INSERT INTO abc VALUES (1, 2, 10)

# Verify we get the correct message, even though internally the ConditionalPut
# for the index key will also fail.
statement error pgcode 23505 duplicate key value violates unique constraint "abc_pkey"\nDETAIL: Key \(a,b\)=\(1,2\) already exists\.
statement error pgcode 23505 duplicate key value violates unique constraint "abc_pkey"\nDETAIL: Key \(a, b\)=\(1, 2\) already exists\.
INSERT INTO abc VALUES (1, 2, 20)

statement ok
Expand Down
12 changes: 6 additions & 6 deletions pkg/sql/logictest/testdata/logic_test/unique
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ NULL 5
statement ok
INSERT INTO uniq_computed_pk (i, s, d) VALUES (1, 'a', 1.0), (2, 'b', 2.0)

statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_pkey"\nDETAIL: Key \(c_i_expr,i\)=\('bar',1\) already exists\.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_pkey"\nDETAIL: Key \(c_i_expr, i\)=\('bar', 1\) already exists\.
INSERT INTO uniq_computed_pk (i, s, d) VALUES (1, 'c', 3.0)

statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_c_s_s_key"\nDETAIL: Key \(c_s,s\)=\('b','b'\) already exists\.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_c_s_s_key"\nDETAIL: Key \(c_s, s\)=\('b', 'b'\) already exists\.
INSERT INTO uniq_computed_pk (i, s, d) VALUES (3, 'b', 3.0)

statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_d"\nDETAIL: Key \(d\)=\(1\.00\) already exists\.
Expand Down Expand Up @@ -542,10 +542,10 @@ NULL 10
# Check that uniqueness violations are detected in a table with UNIQUE indexes
# containing computed columns that are dependent on UNIQUE WITHOUT INDEX
# columns.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_pkey"\nDETAIL: Key \(c_i_expr,i\)=\('bar',1\) already exists\.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_pkey"\nDETAIL: Key \(c_i_expr, i\)=\('bar', 1\) already exists\.
UPDATE uniq_computed_pk SET i = 1 WHERE i = 2

statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_c_s_s_key"\nDETAIL: Key \(c_s,s\)=\('a','a'\) already exists\.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_c_s_s_key"\nDETAIL: Key \(c_s, s\)=\('a', 'a'\) already exists\.
UPDATE uniq_computed_pk SET s = 'a' WHERE i = 2

statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_d"\nDETAIL: Key \(d\)=\(1\.00\) already exists\.
Expand Down Expand Up @@ -825,10 +825,10 @@ i
# Check that uniqueness violations are detected in a table with UNIQUE indexes
# containing computed columns that are dependent on UNIQUE WITHOUT INDEX
# columns.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_pkey"\nDETAIL: Key \(c_i_expr,i\)=\('bar',2\) already exists\.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_pkey"\nDETAIL: Key \(c_i_expr, i\)=\('bar', 2\) already exists\.
INSERT INTO uniq_computed_pk (i, s, d) VALUES (1, 'a', 1.0) ON CONFLICT (s) DO UPDATE SET i = 2

statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_c_s_s_key"\nDETAIL: Key \(c_s,s\)=\('b','b'\) already exists\.
statement error pgcode 23505 pq: duplicate key value violates unique constraint "uniq_computed_pk_c_s_s_key"\nDETAIL: Key \(c_s, s\)=\('b', 'b'\) already exists\.
UPSERT INTO uniq_computed_pk (i, s, d) VALUES (3, 'b', 3.0)

statement error pgcode 23505 pq: duplicate key value violates unique constraint "unique_d"\nDETAIL: Key \(d\)=\(2\.00\) already exists\.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/update
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ CREATE TABLE pks (
statement count 2
INSERT INTO pks VALUES (1, 2, 3), (4, 5, 3)

statement error duplicate key value violates unique constraint "i"\nDETAIL: Key \(k2,v\)=\(5,3\) already exists\.
statement error duplicate key value violates unique constraint "i"\nDETAIL: Key \(k2, v\)=\(5, 3\) already exists\.
UPDATE pks SET k2 = 5 where k1 = 1

# Test updating only one of the columns of a multi-column primary key.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/upsert
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ SELECT * FROM tdup@tdup_y_z_key
3 2 NULL

# Verify that duplicate secondary key fails with regular conflict error.
statement error pq: duplicate key value violates unique constraint "tdup_y_z_key"\nDETAIL: Key \(y,z\)=\(1,2\) already exists\.
statement error pq: duplicate key value violates unique constraint "tdup_y_z_key"\nDETAIL: Key \(y, z\)=\(1, 2\) already exists\.
INSERT INTO tdup VALUES (6, 1, 1), (7, 1, 2) ON CONFLICT (y, z) DO UPDATE SET z=2

# With constant grouping columns (no error).
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/exec/execbuilder/testdata/update
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ CREATE TABLE pks (
statement count 2
INSERT INTO pks VALUES (1, 2, 3), (4, 5, 3)

statement error duplicate key value violates unique constraint "i"\nDETAIL: Key \(k2,v\)=\(5,3\) already exists\.
statement error duplicate key value violates unique constraint "i"\nDETAIL: Key \(k2, v\)=\(5, 3\) already exists\.
UPDATE pks SET k2 = 5 where k1 = 1

# Test updating only one of the columns of a multi-column primary key.
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/row/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func NewUniquenessConstraintViolationError(
), indexName),
fmt.Sprintf(
"Key (%s)=(%s) already exists.",
strings.Join(names[skipCols:], ","),
strings.Join(values[skipCols:], ","),
strings.Join(names[skipCols:], ", "),
strings.Join(values[skipCols:], ", "),
),
)
}
Expand Down

0 comments on commit ddf0a70

Please sign in to comment.