should be it

This commit is contained in:
2025-10-24 19:21:19 -05:00
parent a4b23fc57c
commit f09560c7b1
14047 changed files with 3161551 additions and 1 deletions

View File

@@ -0,0 +1,54 @@
# name: test/sql/constraints/primarykey/test_pk_append_many_duplicates.test_slow
# description: Test appending the same value many times to a primary key column
# group: [primarykey]
statement ok
CREATE TABLE integers(i INTEGER PRIMARY KEY);
# insert a bunch of values into the index and query the index
loop val 0 100
query I
SELECT COUNT(*) FROM integers WHERE i = ${val}
----
0
#then we insert $val
statement ok
INSERT INTO integers VALUES (${val});
query I
SELECT COUNT(*) FROM integers WHERE i = ${val}
----
1
endloop
loop val 0 100
# hack: use a complex expression to prevent index lookup
query I
SELECT COUNT(*) FROM integers WHERE i+i = ${val}*2
----
1
query I
SELECT COUNT(*) FROM integers WHERE i = ${val}
----
1
endloop
# now insert the same values: this should fail this time
loop it 0 10
statement error
INSERT INTO integers VALUES ($val)
----
endloop
# now test that the counts are correct
statement ok
SELECT COUNT(*), COUNT(DISTINCT i) FROM integers

View File

@@ -0,0 +1,25 @@
# name: test/sql/constraints/primarykey/test_pk_bool.test
# description: Multi-column boolean PRIMARY KEY constraint
# group: [primarykey]
statement ok
CREATE TABLE integers(i INTEGER, j BOOLEAN, PRIMARY KEY(i, j))
statement ok
INSERT INTO integers VALUES (1, false), (1, true), (2, false)
# duplicate value!
statement error
INSERT INTO integers VALUES (1, false)
----
statement ok
INSERT INTO integers VALUES (2, true)
query IT
SELECT * FROM integers ORDER BY 1, 2
----
1 0
1 1
2 0
2 1

View File

@@ -0,0 +1,38 @@
# name: test/sql/constraints/primarykey/test_pk_col_subset.test
# description: PRIMARY KEY constraint that only covers a subset of the columns
# group: [primarykey]
statement ok
CREATE TABLE numbers(a integer, b integer, c integer, d integer, e integer, PRIMARY KEY(a,b))
# insert two conflicting pairs at the same time
statement error
INSERT INTO numbers VALUES (1,1,1,1,1), (1,1,1,1,1)
----
# insert unique values
statement ok
INSERT INTO numbers VALUES (1,1,1,1,1),(1,2,1,1,1),(2,1,2,1,1),(2,2,2,2,2)
# insert a duplicate value as part of a chain of values
statement error
INSERT INTO numbers VALUES (1,1,1,1,1),(1,5,1,1,4);
----
# now insert just the second value
statement ok
INSERT INTO numbers VALUES (1,5,1,1,4);
# this should work since is not part of primary key
statement ok
UPDATE numbers SET c=1 WHERE c=2
# this should fail since is will cause a duplicate
statement error
UPDATE numbers SET b=1 WHERE b=2
----
# this should work since it won't cause a duplicate
statement ok
UPDATE numbers SET b=3 WHERE b=2

View File

@@ -0,0 +1,83 @@
# name: test/sql/constraints/primarykey/test_pk_concurrency_conflicts.test
# description: PRIMARY KEY and concurency conflicts
# group: [primarykey]
statement ok con1
CREATE TABLE integers(i INTEGER PRIMARY KEY)
statement ok con1
INSERT INTO integers VALUES (1), (2), (3)
# con1 starts a transaction and modifies the second value
statement ok con1
BEGIN TRANSACTION
statement ok con1
UPDATE integers SET i=4 WHERE i=2
# con2 can't update the second value
statement error con2
UPDATE integers SET i=4 WHERE i=2
----
statement error con2
UPDATE integers SET i=5 WHERE i=2
----
# nor can it delete it
statement error con2
DELETE FROM integers WHERE i=2
----
# we tried to set i=5 in con2 but it failed, we can set it in con1 now though
statement ok con1
UPDATE integers SET i=5 WHERE i=3
# rollback con1
statement ok con1
ROLLBACK
# now we can perform the changes in con2
statement ok con2
BEGIN TRANSACTION
statement ok con2
UPDATE integers SET i=4 WHERE i=2
statement ok con2
UPDATE integers SET i=5 WHERE i=3
# check the results, con1 still gets the old results
query I con1
SELECT * FROM integers ORDER BY i
----
1
2
3
query I con2
SELECT * FROM integers ORDER BY i
----
1
4
5
# now commit
statement ok con2
COMMIT
# check the results again, both get the same (new) results now
query I con1
SELECT * FROM integers ORDER BY i
----
1
4
5
query I con2
SELECT * FROM integers ORDER BY i
----
1
4
5

View File

@@ -0,0 +1,32 @@
# name: test/sql/constraints/primarykey/test_pk_many_columns.test
# description: PRIMARY KEY constraint on more than two columns
# group: [primarykey]
statement ok
CREATE TABLE numbers(a integer, b integer, c integer, d integer, e integer, PRIMARY KEY(a,b,c,d,e))
# insert two conflicting pairs at the same time
statement error
INSERT INTO numbers VALUES (1,1,1,1,1), (1,1,1,1,1)
----
<REGEX>:.*Constraint Error.*constraint violation.*
# insert unique values
statement ok
INSERT INTO numbers VALUES (1,1,1,1,1),(1,2,1,1,1),(1,1,2,1,1),(2,2,2,2,2)
# insert a duplicate value as part of a chain of values
statement error
INSERT INTO numbers VALUES (1,1,1,1,1),(1,1,1,1,4);
----
<REGEX>:.*Constraint Error: Duplicate key.*
# now insert just the second value
statement ok
INSERT INTO numbers VALUES (1,1,1,1,4);
# this should fail since will cause a duplicate
statement error
UPDATE numbers SET c=1 WHERE c=2
----
<REGEX>:.*Constraint Error: Duplicate key.*

View File

@@ -0,0 +1,30 @@
# name: test/sql/constraints/primarykey/test_pk_multi_column.test
# description: Multi-column PRIMARY KEY constraint
# group: [primarykey]
statement ok
CREATE TABLE integers(i INTEGER, j VARCHAR, PRIMARY KEY(i, j))
statement ok
INSERT INTO integers VALUES (3, 'hello'), (3, 'world')
query IT
SELECT * FROM integers
----
3 hello
3 world
statement error
INSERT INTO integers VALUES (6, 'bla'), (3, 'hello');
----
statement ok
INSERT INTO integers VALUES (6, 'bla');
query IT
SELECT * FROM integers
----
3 hello
3 world
6 bla

View File

@@ -0,0 +1,34 @@
# name: test/sql/constraints/primarykey/test_pk_multi_string.test
# description: PRIMARY KEY constraint on multiple string columns with overlapping values
# group: [primarykey]
statement ok
CREATE TABLE tst(a varchar, b varchar,PRIMARY KEY(a,b))
# insert two conflicting pairs at the same time
statement error
INSERT INTO tst VALUES ('hell', 'hello'), ('hell','hello')
----
# insert unique values
statement ok
INSERT INTO tst VALUES ('hell', 'hello'), ('hello','hell'), ('hel','hell'), ('hell','hel')
# insert a duplicate value as part of a chain of values
statement error
INSERT INTO tst VALUES ('hell', 'hello'),('hel', 'hello');
----
# now insert just the second value
statement ok
INSERT INTO tst VALUES ('hel', 'hello');
# this should fail since it will cause a duplicate
statement error
UPDATE tst SET b='hello' WHERE b='hel'
----
# this should work since it won't cause a duplicate
statement ok
UPDATE tst SET b='hell' WHERE b='hel'

View File

@@ -0,0 +1,46 @@
# name: test/sql/constraints/primarykey/test_pk_prefix.test_slow
# description: PRIMARY KEY prefix tests with compound indexes.
# group: [primarykey]
statement ok
CREATE TABLE test (a INTEGER, b VARCHAR, PRIMARY KEY(a, b));
loop i 1 300
statement ok
INSERT INTO test VALUES (${i}, 'hello');
endloop
# Try to insert again - this must throw a constraint violation.
loop i 1 300
statement error
INSERT INTO test VALUES (${i}, 'hello');
----
<REGEX>:Constraint Error.*violates primary key constraint.*
endloop
statement ok
UPDATE test SET a = a + 1000;
# Insert the original values again.
loop i 1 300
statement ok
INSERT INTO test VALUES (${i}, 'hello');
endloop
# Increment one more time.
statement ok
UPDATE test SET a = a + 1000;
query II
SELECT MIN(a), MAX(a) FROM test;
----
1001 2299

View File

@@ -0,0 +1,24 @@
# name: test/sql/constraints/primarykey/test_pk_rollback.test
# description: PRIMARY KEY and transactions
# group: [primarykey]
statement ok
CREATE TABLE integers(i INTEGER PRIMARY KEY)
statement ok
BEGIN TRANSACTION
statement ok
INSERT INTO integers VALUES (1);
statement ok
ROLLBACK
statement ok
INSERT INTO integers VALUES (1);
query I
SELECT * FROM integers
----
1

View File

@@ -0,0 +1,51 @@
# name: test/sql/constraints/primarykey/test_pk_string.test
# description: ART FP String Constraint
# group: [primarykey]
statement ok
CREATE TABLE numbers(i varchar PRIMARY KEY, j INTEGER)
# insert two conflicting pairs at the same time
statement error
INSERT INTO numbers VALUES ('1', 4), ('1', 5)
----
<REGEX>:.*Constraint Error.*constraint violation.*
# insert unique values
statement ok
INSERT INTO numbers VALUES ('1', 4), ('2', 5)
query TI
SELECT * FROM numbers
----
1 4
2 5
# insert a duplicate value as part of a chain of values
statement error
INSERT INTO numbers VALUES ('6', 6), ('1', 4);
----
<REGEX>:.*Constraint Error: Duplicate key.*
# now insert just the first value
statement ok
INSERT INTO numbers VALUES ('6', 6);
query TI
SELECT * FROM numbers
----
1 4
2 5
6 6
# insert NULL value in PRIMARY KEY is not allowed
statement error
INSERT INTO numbers VALUES (NULL, 4);
----
<REGEX>:.*Constraint Error.*constraint failed.*
# update NULL is also not allowed
statement error
UPDATE numbers SET i=NULL;
----
<REGEX>:.*Constraint Error.*constraint failed.*

View File

@@ -0,0 +1,76 @@
# name: test/sql/constraints/primarykey/test_pk_update_delete.test
# description: PRIMARY KEY and update/delete
# group: [primarykey]
statement ok
CREATE TABLE test (a INTEGER PRIMARY KEY, b INTEGER);
statement ok
INSERT INTO test VALUES (11, 1), (12, 2), (13, 3)
statement ok
UPDATE test SET b=2 WHERE b=3;
statement error
UPDATE test SET a=a+1 WHERE b=1;
----
statement error
UPDATE test SET a=4;
----
query II
SELECT * FROM test;
----
11 1
12 2
13 2
statement ok
DELETE FROM test WHERE a=11
query II
SELECT * FROM test ORDER BY a;
----
12 2
13 2
statement ok
INSERT INTO test VALUES (11, 1);
query II
SELECT * FROM test ORDER BY a;
----
11 1
12 2
13 2
statement error
INSERT INTO test VALUES (11, 1);
----
query II
SELECT * FROM test ORDER BY a;
----
11 1
12 2
13 2
statement ok
UPDATE test SET a=4 WHERE b=1;
query II
SELECT * FROM test ORDER BY a;
----
4 1
12 2
13 2
statement error
UPDATE test SET a=NULL WHERE b=1;
----
statement error
UPDATE test SET a=NULL;
----

View File

@@ -0,0 +1,52 @@
# name: test/sql/constraints/primarykey/test_pk_updel_local.test
# description: PRIMARY KEY and update/delete in the same transaction
# group: [primarykey]
statement ok
CREATE TABLE integers(i INTEGER PRIMARY KEY)
statement ok
BEGIN TRANSACTION
statement ok
INSERT INTO integers VALUES (1);
statement ok
UPDATE integers SET i=33;
statement ok
ROLLBACK
statement ok
INSERT INTO integers VALUES (1);
statement ok
INSERT INTO integers VALUES (33);
query I
SELECT * FROM integers ORDER BY i
----
1
33
statement ok
DROP TABLE integers
statement ok
CREATE TABLE integers(i INTEGER PRIMARY KEY)
statement ok
INSERT INTO integers VALUES (1);
statement ok
UPDATE integers SET i=33;
statement ok
INSERT INTO integers VALUES (1);
query I
SELECT * FROM integers ORDER BY i
----
1
33

View File

@@ -0,0 +1,110 @@
# name: test/sql/constraints/primarykey/test_pk_updel_multi_column.test
# description: PRIMARY KEY and update/delete on multiple columns
# group: [primarykey]
statement ok
CREATE TABLE test (a INTEGER, b VARCHAR, PRIMARY KEY(a, b));
statement ok
INSERT INTO test VALUES (11, 'hello'), (12, 'world'), (13, 'blablabla');
statement ok
UPDATE test SET b = 'pandas';
query IT
SELECT * FROM test ORDER BY ALL;
----
11 pandas
12 pandas
13 pandas
statement ok
UPDATE test SET a = a + 3;
query IT
SELECT * FROM test ORDER BY ALL;
----
14 pandas
15 pandas
16 pandas
statement error
UPDATE test SET a = 15 WHERE a = 14;
----
<REGEX>:Constraint Error.*violates primary key constraint.*
statement error
UPDATE test SET a = 4;
----
<REGEX>:Constraint Error.*PRIMARY KEY or UNIQUE constraint violation.*
query IT
SELECT * FROM test ORDER BY a;
----
14 pandas
15 pandas
16 pandas
statement ok
UPDATE test SET a = a - 3;
query IT
SELECT * FROM test ORDER BY ALL;
----
11 pandas
12 pandas
13 pandas
statement ok
DELETE FROM test WHERE a = 12;
query IT
SELECT * FROM test ORDER BY a;
----
11 pandas
13 pandas
statement ok
INSERT INTO test VALUES (12, 'pandas');
query IT
SELECT * FROM test ORDER BY a;
----
11 pandas
12 pandas
13 pandas
statement error
INSERT INTO test VALUES (12, 'pandas');
----
<REGEX>:Constraint Error.*violates primary key constraint.*
statement ok
DELETE FROM test WHERE a = 12;
query IT
SELECT * FROM test ORDER BY a;
----
11 pandas
13 pandas
statement ok
INSERT INTO test VALUES (12, 'other pandas');
statement ok
UPDATE test SET a = 4 WHERE a = 42;
statement ok
UPDATE test SET a = 4 WHERE a = 12;
query IT
SELECT * FROM test ORDER BY a;
----
4 other pandas
11 pandas
13 pandas
statement error
UPDATE test SET b = NULL WHERE a = 13;
----
<REGEX>:Constraint Error.*NOT NULL constraint failed.*

View File

@@ -0,0 +1,78 @@
# name: test/sql/constraints/primarykey/test_primary_key.test
# description: Single PRIMARY KEY constraint
# group: [primarykey]
statement ok
CREATE TABLE integers(i INTEGER PRIMARY KEY, j INTEGER)
# insert two conflicting pairs at the same time
statement error
INSERT INTO integers VALUES (3, 4), (3, 5)
----
<REGEX>:.*Constraint Error.*constraint violation.*
# insert unique values
statement ok
INSERT INTO integers VALUES (3, 4), (2, 5)
query II
SELECT * FROM integers
----
3 4
2 5
# insert a duplicate value as part of a chain of values
statement error
INSERT INTO integers VALUES (6, 6), (3, 4);
----
<REGEX>:.*Constraint Error.*violates primary key constraint.*
# now insert just the first value
statement ok
INSERT INTO integers VALUES (6, 6);
query II
SELECT * FROM integers
----
3 4
2 5
6 6
# insert NULL value in PRIMARY KEY is not allowed
statement error
INSERT INTO integers VALUES (NULL, 4);
----
<REGEX>:.*Constraint Error.*constraint failed.*
# update NULL is also not allowed
statement error
UPDATE integers SET i=NULL;
----
<REGEX>:.*Constraint Error.*constraint failed.*
# insert the same value from multiple connections
# NOTE: this tests current behavior
# this can potentially change in the future
statement ok
BEGIN TRANSACTION
statement ok con2
BEGIN TRANSACTION
# insert from first connection succeeds
statement ok
INSERT INTO integers VALUES (7, 8);
# insert from second connection also succeeds
statement ok con2
INSERT INTO integers VALUES (7, 33);
# now committing the first transaction works
statement ok
COMMIT
# but the second transaction results in a conflict
statement error con2
COMMIT
----
<REGEX>:.*TransactionContext Error.*constraint violation.*