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,28 @@
# name: test/sql/alter/add_pk/test_add_multi_column_pk.test
# description: Test adding a multi-column PRIMARY KEY to a table.
# group: [add_pk]
statement ok
PRAGMA enable_verification;
statement ok
CREATE TABLE test (i INTEGER, j INTEGER, d TEXT);
statement ok
INSERT INTO test VALUES (3, 4, 'hello'), (44, 45, '56');
statement ok
ALTER TABLE test ADD PRIMARY KEY (i, j);
statement ok
INSERT INTO test VALUES (1, 1, 'foo'), (1, 2, 'bar');
statement error
INSERT INTO test VALUES (1, 2, 'oops');
----
<REGEX>:Constraint Error.*Duplicate key "i: 1, j: 2" violates primary key constraint.*
statement error
INSERT INTO test VALUES (NULL, 2, 'nada');
----
<REGEX>:Constraint Error.*NOT NULL constraint failed: test.i.*

View File

@@ -0,0 +1,82 @@
# name: test/sql/alter/add_pk/test_add_pk.test
# description: Test adding a PRIMARY KEY to a table.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
INSERT INTO test VALUES (1, 1)
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)
query II
SELECT * FROM test
----
1 1
statement ok
INSERT INTO test VALUES (1, 2)
statement error
INSERT INTO test VALUES (2, 1)
----
<REGEX>:Constraint Error.*Duplicate key "j: 1" violates primary key constraint.*
statement error
INSERT INTO test VALUES (2, NULL)
----
<REGEX>:Constraint Error.*NOT NULL constraint failed.*
# FIXME: Perform duplicate verification before building the index.
statement error
ALTER TABLE test ADD PRIMARY KEY (i)
----
<REGEX>:Constraint Error.*contains duplicates on indexed column.*
statement error
ALTER TABLE test ADD PRIMARY KEY (j)
----
<REGEX>:Catalog Error.*an index with that name already exists for this table: PRIMARY_test_j.*
# Reverse the column order in the index.
statement ok
CREATE TABLE reverse (i INTEGER, j INTEGER)
statement ok
INSERT INTO reverse VALUES (1, 2)
statement ok
ALTER TABLE reverse ADD PRIMARY KEY (j, i)
statement error
INSERT INTO reverse VALUES (1, 2)
----
<REGEX>:Constraint Error.*Duplicate key "j: 2, i: 1" violates primary key constraint.*
statement error
INSERT INTO reverse (j, i) VALUES (2, 1)
----
<REGEX>:Constraint Error.*Duplicate key "j: 2, i: 1" violates primary key constraint.*
# Use an index scan.
statement ok
CREATE TABLE scan (i INTEGER, j INTEGER)
statement ok
INSERT INTO scan SELECT range, range + 1 FROM range(30000);
statement ok
ALTER TABLE scan ADD PRIMARY KEY (i)
query II
SELECT * FROM scan WHERE i = 2
----
2 3

View File

@@ -0,0 +1,36 @@
# name: test/sql/alter/add_pk/test_add_pk_alter_in_tx.test
# description: Test altering a different constraint in the transaction.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
ALTER TABLE test ADD PRIMARY KEY (j);
statement ok
INSERT INTO test VALUES (1, 1)
statement ok
BEGIN TRANSACTION
statement ok
ALTER TABLE test ALTER COLUMN j SET NOT NULL;
# Throw a constraint violation on the transaction-local storage.
statement error
INSERT INTO test VALUES (2, 1)
----
<REGEX>:Constraint Error.*violates primary key constraint.*
statement ok
ROLLBACK
statement error
INSERT INTO test VALUES (2, 1)
----
<REGEX>:Constraint Error.*violates primary key constraint.*

View File

@@ -0,0 +1,44 @@
# name: test/sql/alter/add_pk/test_add_pk_attach.test
# description: Test adding a PRIMARY KEY combined with ATTACH/DETACH.
# group: [add_pk]
require skip_reload
load __TEST_DIR__/test_add_pk_attach.db
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)
statement ok
INSERT INTO test VALUES (1, 1)
statement ok
ATTACH ':memory:' as memory
statement ok
USE memory
statement ok
DETACH test_add_pk_attach
statement ok
ATTACH '__TEST_DIR__/test_add_pk_attach.db' as test_add_pk_attach
statement ok
USE test_add_pk_attach
statement error
ALTER TABLE test ADD PRIMARY KEY (i)
----
<REGEX>:Catalog Error.*table "test" can have only one primary key.*
statement error
INSERT INTO test VALUES (2, 1)
----
<REGEX>:Constraint Error.*Duplicate key "j: 1" violates primary key constraint.*

View File

@@ -0,0 +1,45 @@
# name: test/sql/alter/add_pk/test_add_pk_catalog_error.test
# description: Test adding a PRIMARY KEY to an invalid table and column.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement error
ALTER TABLE test ADD PRIMARY KEY (i_do_not_exist)
----
<REGEX>:Catalog Error.*table "test" does not have a column named "i_do_not_exist".*
statement error
ALTER TABLE i_do_not_exist ADD PRIMARY KEY (i, j)
----
<REGEX>:Catalog Error.*Table with name i_do_not_exist does not exist!.*
# Try to add it to a column that already has a UNIQUE index.
statement ok
CREATE TABLE uniq (i INTEGER UNIQUE, j INTEGER)
statement ok
INSERT INTO uniq VALUES (1, 10), (2, 20), (3, 30)
statement error
INSERT INTO uniq VALUES (1, 100)
----
<REGEX>:Constraint Error.*Duplicate key "i: 1" violates unique constraint.*
statement ok
ALTER TABLE uniq ADD PRIMARY KEY (i)
statement error
INSERT INTO uniq VALUES (1, 101)
----
<REGEX>:Constraint Error.*Duplicate key "i: 1" violates.*constraint.*
statement error
INSERT INTO uniq VALUES (NULL, 100)
----
<REGEX>:Constraint Error.*NOT NULL constraint failed: uniq.i.*

View File

@@ -0,0 +1,27 @@
# name: test/sql/alter/add_pk/test_add_pk_commit.test
# description: Test committing the transaction-local ADD PRIMARY KEY command.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
BEGIN TRANSACTION
statement ok
INSERT INTO test VALUES (1, 1)
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)
statement ok
COMMIT
statement error
INSERT INTO test VALUES (2, 1)
----
<REGEX>:Constraint Error.*Duplicate key "j: 1" violates primary key constraint.*

View File

@@ -0,0 +1,31 @@
# name: test/sql/alter/add_pk/test_add_pk_drop_and_reload.test
# description: Test adding a PRIMARY KEY, dropping everything, restarting, and recreating the PRIMARY KEY.
# group: [add_pk]
load __TEST_DIR__/test_add_pk_drop_reload.db
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)
statement ok
INSERT INTO test VALUES (1, 1)
statement ok
DROP TABLE test
restart
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
INSERT INTO test VALUES (1, 1)
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)

View File

@@ -0,0 +1,28 @@
# name: test/sql/alter/add_pk/test_add_pk_gaps_in_rowids.test
# description: Test adding a PRIMARY KEY to a table with gaps in its row IDs.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE integers(i integer)
statement ok
INSERT INTO integers SELECT * FROM range(50000);
query I
SELECT i FROM integers WHERE i = 100
----
100
statement ok
DELETE FROM integers WHERE i = 42;
statement ok
ALTER TABLE integers ADD PRIMARY KEY (i);
query I
SELECT i FROM integers WHERE i = 100
----
100

View File

@@ -0,0 +1,57 @@
# name: test/sql/alter/add_pk/test_add_pk_invalid_data.test
# description: Test adding a PRIMARY KEY to a table with invalid data.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE duplicates (i INTEGER, j INTEGER)
statement ok
INSERT INTO duplicates VALUES (1, 10), (2, 20), (3, 30), (1, 100)
statement error
ALTER TABLE duplicates ADD PRIMARY KEY (i)
----
<REGEX>:Constraint Error.*contains duplicates on indexed column.*
# Now test with NULLs.
statement ok
CREATE TABLE nulls (i INTEGER, j INTEGER);
statement ok
INSERT INTO nulls VALUES (1, 10), (2, NULL), (3, 30), (4, 40);
statement error
ALTER TABLE nulls ADD PRIMARY KEY (i, j);
----
<REGEX>:Constraint Error.*NOT NULL constraint failed.*
statement ok
DROP TABLE nulls;
statement ok
CREATE TABLE nulls (i INTEGER, j INTEGER);
statement ok
INSERT INTO nulls VALUES (5, 10), (NULL, 20), (7, 30), (8, 100);
statement error
ALTER TABLE nulls ADD PRIMARY KEY (i)
----
<REGEX>:Constraint Error.*NOT NULL constraint failed.*
# Validate NULL on a compound index.
statement ok
CREATE TABLE nulls_compound (i INTEGER, j INTEGER, k VARCHAR)
statement ok
INSERT INTO nulls_compound VALUES (1, 10, 'hello'), (2, 20, 'world'), (NULL, NULL, NULL), (3, 100, 'yay');
statement error
ALTER TABLE nulls_compound ADD PRIMARY KEY (k, i)
----
<REGEX>:Constraint Error.*NOT NULL constraint failed.*

View File

@@ -0,0 +1,19 @@
# name: test/sql/alter/add_pk/test_add_pk_invalid_type.test
# description: Test adding a PRIMARY KEY with an invalid type.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (a INTEGER[], b INTEGER)
statement error
ALTER TABLE test ADD PRIMARY KEY (a)
----
<REGEX>:Invalid type Error.*Invalid Type.*Invalid type for index key.*
statement error
ALTER TABLE test ADD PRIMARY KEY (a, b)
----
<REGEX>:Invalid type Error.*Invalid Type.*Invalid type for index key.*

View File

@@ -0,0 +1,51 @@
# name: test/sql/alter/add_pk/test_add_pk_naming_conflict.test
# description: Test adding a PRIMARY KEY to a table that already has an index with that name.
# group: [add_pk]
# There is also a catalog dependency conflict,
# which prevents altering a table with a dependency (index) on it.
load __TEST_DIR__/test_add_pk_naming_conflict.db
statement ok
PRAGMA enable_verification;
statement ok
CREATE TABLE tbl (i INTEGER);
statement ok
INSERT INTO tbl VALUES (1);
statement ok
CREATE INDEX PRIMARY_tbl_i ON tbl(i);
statement error
ALTER TABLE tbl ADD PRIMARY KEY (i);
----
<REGEX>:Catalog Error.*already exists.*
restart
statement ok
PRAGMA enable_verification;
statement error
ALTER TABLE tbl ADD PRIMARY KEY (i);
----
<REGEX>:Catalog Error.*already exists.*
# Let's do it the other way around now.
statement ok
CREATE TABLE test (i INTEGER)
statement ok
INSERT INTO test VALUES (1);
statement ok
ALTER TABLE test ADD PRIMARY KEY (i);
statement error
CREATE INDEX PRIMARY_test_i ON test(i);
----
<REGEX>:Catalog Error.*index with the name PRIMARY_test_i already exists.*

View File

@@ -0,0 +1,44 @@
# name: test/sql/alter/add_pk/test_add_pk_reclaim_storage.test_slow
# description: Test that we reclaim the storage after adding and dropping a PK.
# group: [add_pk]
load __TEST_DIR__/test_add_pk_reclaim_storage.db
statement ok
PRAGMA disable_checkpoint_on_shutdown
statement ok
PRAGMA wal_autocheckpoint='1TB';
statement ok
PRAGMA enable_verification
statement ok
SET force_compression='uncompressed';
loop i 0 10
statement ok
CREATE TABLE tbl${i} AS SELECT range AS i FROM range(500000)
statement ok
ALTER TABLE tbl${i} ADD PRIMARY KEY (i);
query I
SELECT i FROM tbl${i} WHERE i = 500;
----
500
statement ok
CHECKPOINT;
statement ok
DROP TABLE tbl${i};
statement ok
CHECKPOINT;
query I nosort expected_blocks
SELECT round(total_blocks / 100.0) FROM pragma_database_size();
endloop

View File

@@ -0,0 +1,57 @@
# name: test/sql/alter/add_pk/test_add_pk_rollback.test
# description: Test invalidating the constraint with uncommitted changes.
# group: [add_pk]
statement ok
PRAGMA enable_verification;
statement ok
CREATE TABLE test (i INTEGER, j INTEGER);
statement ok
BEGIN TRANSACTION
statement ok
INSERT INTO test VALUES (1, 1);
statement ok
ALTER TABLE test ADD PRIMARY KEY (j);
statement ok
INSERT INTO test VALUES (2, 1);
# Throw a constraint violation on the transaction-local storage.
statement error
COMMIT
----
<REGEX>:TransactionContext Error.*Failed to commit: PRIMARY KEY or UNIQUE constraint violation: duplicate key "1".*
# Inserting duplicate values must work after rolling back.
statement ok
INSERT INTO test VALUES (1, 1), (2, 1), (2, NULL);
# Invalidate the transaction with a constraint violation.
statement ok
CREATE TABLE other (i INTEGER, j INTEGER);
statement ok
BEGIN TRANSACTION
statement ok
INSERT INTO other VALUES (1, 1), (2, 1);
statement ok
ALTER TABLE other ADD PRIMARY KEY (j);
statement error
COMMIT
----
<REGEX>:TransactionContext Error.*Failed to commit: PRIMARY KEY or UNIQUE constraint violation: duplicate key "1".*
# Inserting duplicate values must work after rolling back.
statement ok
INSERT INTO test VALUES (1, 1), (2, 1), (2, NULL);

View File

@@ -0,0 +1,29 @@
# name: test/sql/alter/add_pk/test_add_pk_storage.test
# description: Test adding and persisting a PRIMARY KEY.
# group: [add_pk]
load __TEST_DIR__/test_add_pk.db
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)
statement ok
INSERT INTO test VALUES (1, 1)
restart
statement error
ALTER TABLE test ADD PRIMARY KEY (i)
----
<REGEX>:Catalog Error.*table "test" can have only one primary key.*
statement error
INSERT INTO test VALUES (2, 1)
----
<REGEX>:Constraint Error.*Duplicate key "j: 1" violates primary key constraint.*

View File

@@ -0,0 +1,37 @@
# name: test/sql/alter/add_pk/test_add_pk_wal.test
# description: Test persisting the ALTER TABLE ... ADD PRIMARY KEY statement to the WAL.
# group: [add_pk]
load __TEST_DIR__/test_add_pk_wal.db
statement ok
PRAGMA disable_checkpoint_on_shutdown
statement ok
PRAGMA wal_autocheckpoint='1TB';
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER);
statement ok
INSERT INTO test VALUES (1, 2), (3, 4);
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)
restart
statement error
ALTER TABLE test ADD PRIMARY KEY (i)
----
<REGEX>:Catalog Error.*table "test" can have only one primary key.*
statement error
INSERT INTO test VALUES (2, 2)
----
<REGEX>:Constraint Error.*Duplicate key "j: 2" violates primary key constraint.*

View File

@@ -0,0 +1,39 @@
# name: test/sql/alter/add_pk/test_add_pk_with_generated_column.test
# description: Test adding a PRIMARY KEY to a table with a generated column.
# group: [add_pk]
statement ok
PRAGMA enable_verification;
statement ok
CREATE TABLE test (
a INT NOT NULL,
b INT GENERATED ALWAYS AS (a) VIRTUAL,
c INT,
);
statement ok
INSERT INTO test VALUES (5, 4);
# Cannot add a PK to generated columns.
statement error
ALTER TABLE test ADD PRIMARY KEY (b);
----
<REGEX>:Binder Error.*cannot create a PRIMARY KEY on a generated column: b.*
statement error
ALTER TABLE test ADD PRIMARY KEY (b, c);
----
<REGEX>:Binder Error.*cannot create a PRIMARY KEY on a generated column: b.*
# Can add a PK to a non-generated column.
statement ok
ALTER TABLE test ADD PRIMARY KEY (c);
statement error
INSERT INTO test VALUES (1, 4);
----
<REGEX>:Constraint Error.*violates primary key constraint.*

View File

@@ -0,0 +1,47 @@
# name: test/sql/alter/add_pk/test_add_same_pk_simultaneously.test
# description: Test adding the same PRIMARY KEY in two different transactions.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok tran1
BEGIN TRANSACTION
statement ok tran1
ALTER TABLE test ADD PRIMARY KEY (j)
statement ok tran2
BEGIN TRANSACTION
# We trigger a Catalog write-write conflict.
statement error tran2
ALTER TABLE test ADD PRIMARY KEY (j)
----
<REGEX>:TransactionContext Error.*cannot add an index to a table that has been altered.*
statement ok tran3
BEGIN TRANSACTION
statement ok tran1
ROLLBACK
statement ok tran2
ROLLBACK
statement ok tran3
ALTER TABLE test ADD PRIMARY KEY (j)
statement ok tran3
COMMIT
statement error
INSERT INTO test VALUES (1, 1), (1, 1)
----
<REGEX>:Constraint Error.*PRIMARY KEY or UNIQUE constraint violation.*

View File

@@ -0,0 +1,43 @@
# name: test/sql/alter/add_pk/test_add_same_pk_twice.test
# description: Test adding the same PRIMARY KEY twice.
# group: [add_pk]
statement ok
PRAGMA enable_verification
statement ok
CREATE TABLE test (i INTEGER, j INTEGER)
statement ok
BEGIN TRANSACTION
statement ok
ALTER TABLE test ADD PRIMARY KEY (j)
statement error
ALTER TABLE test ADD PRIMARY KEY (i)
----
<REGEX>:Catalog Error.*table "test" can have only one primary key.*
statement ok
COMMIT
# Without a transaction.
statement ok
CREATE TABLE other (i INTEGER PRIMARY KEY, j INTEGER)
statement error
ALTER TABLE other ADD PRIMARY KEY (i, j)
----
<REGEX>:Catalog Error.*table "other" can have only one primary key.*
statement error
ALTER TABLE other ADD PRIMARY KEY (i)
----
<REGEX>:Catalog Error.*table "other" can have only one primary key.*
statement error
ALTER TABLE other ADD PRIMARY KEY (j)
----
<REGEX>:Catalog Error.*table "other" can have only one primary key.*