# name: test/sql/upsert/test_big_insert.test # description: Test insert into statements # group: [upsert] statement ok PRAGMA enable_verification; statement ok SET preserve_insertion_order = false; statement ok CREATE TABLE integers( i INT UNIQUE, j INT DEFAULT 0, k INT DEFAULT 0 ); statement ok INSERT INTO integers(i) SELECT i FROM range(5000) tbl(i); query I SELECT COUNT(*) FROM integers ---- 5000 # All tuples hit a conflict - DO NOTHING. statement ok INSERT INTO integers SELECT * FROM integers ON CONFLICT DO NOTHING; # All tuples hit a conflict - DO UPDATE. statement ok INSERT INTO integers SELECT * FROM integers ON CONFLICT DO UPDATE SET j = 10; # All 'j' entries are changed to 10. query I SELECT COUNT(*) FILTER (WHERE j = 10) FROM integers ---- 5000 statement ok INSERT INTO integers(i, j) SELECT i % 5, i FROM range(4995, 5000) tbl(i) ON CONFLICT DO UPDATE SET j = excluded.j, k = excluded.i; query I SELECT j FROM integers LIMIT 5; ---- 4995 4996 4997 4998 4999 # This is the worst conflicting rowid pattern we could have. # Every odd-indexed insert tuple conflicts with a row at the start of the existing tuples. # And every even-indexed insert tuple conflicts with a row at the end of the existing tuples. statement ok INSERT INTO integers(i, j) SELECT CASE WHEN i % 2 = 0 THEN 4999 - (i // 2) ELSE i - ((i // 2) + 1) END, i FROM range(5000) tbl(i) ON CONFLICT DO UPDATE SET j = excluded.j; # This shows that the odd-indexed insert tuples conflicted with the first rows. query I SELECT j FROM integers LIMIT 5; ---- 1 3 5 7 9 # This shows that the even-indexed insert tuples conflicted with the last rows. query I SELECT j FROM integers LIMIT 5 OFFSET 4995; ---- 8 6 4 2 0 # Reset j. statement ok UPDATE integers SET j = 0; # Only set j if both the existing tuple and the insert tuple are even. statement ok INSERT INTO integers(i, j) SELECT CASE WHEN i % 2 = 0 THEN 4999 - (i // 2) ELSE i - ((i // 2) + 1) END, i FROM range(5000) tbl(i) ON CONFLICT DO UPDATE SET j = excluded.j WHERE i % 2 = 0 AND excluded.j % 2 = 0; # The DO UPDATE WHERE clause is only true for a quarter of the cases. query I SELECT COUNT(j) FILTER (WHERE j != 0) FROM integers; ---- 1250