18103b7d2Sdrh# 2007 January 24 28103b7d2Sdrh# 38103b7d2Sdrh# The author disclaims copyright to this source code. In place of 48103b7d2Sdrh# a legal notice, here is a blessing: 58103b7d2Sdrh# 68103b7d2Sdrh# May you do good and not evil. 78103b7d2Sdrh# May you find forgiveness for yourself and forgive others. 88103b7d2Sdrh# May you share freely, never taking more than you give. 98103b7d2Sdrh# 108103b7d2Sdrh#*********************************************************************** 118103b7d2Sdrh# This file implements regression tests for SQLite library. The 12dd73521bSdrh# focus of this file is testing the INSERT transfer optimization. 138103b7d2Sdrh# 148103b7d2Sdrh 158103b7d2Sdrhset testdir [file dirname $argv0] 168103b7d2Sdrhsource $testdir/tester.tcl 1775f95588Sdanset testprefix insert4 188103b7d2Sdrh 194152e677Sdanielk1977ifcapable !view||!subquery { 204152e677Sdanielk1977 finish_test 214152e677Sdanielk1977 return 224152e677Sdanielk1977} 234152e677Sdanielk1977 24dd73521bSdrh# The sqlite3_xferopt_count variable is incremented whenever the 25dd73521bSdrh# insert transfer optimization applies. 26dd73521bSdrh# 27dd73521bSdrh# This procedure runs a test to see if the sqlite3_xferopt_count is 28dd73521bSdrh# set to N. 29dd73521bSdrh# 30dd73521bSdrhproc xferopt_test {testname N} { 31dd73521bSdrh do_test $testname {set ::sqlite3_xferopt_count} $N 32dd73521bSdrh} 33dd73521bSdrh 34fb658dedSdrh# Create tables used for testing. 35fb658dedSdrh# 3666c48907Sdrhsqlite3_db_config db LEGACY_FILE_FORMAT 0 37fb658dedSdrhexecsql { 38fb658dedSdrh CREATE TABLE t1(a int, b int, check(b>a)); 39fb658dedSdrh CREATE TABLE t2(x int, y int); 40fb658dedSdrh CREATE VIEW v2 AS SELECT y, x FROM t2; 41fb658dedSdrh CREATE TABLE t3(a int, b int); 42fb658dedSdrh} 43fb658dedSdrh 448103b7d2Sdrh# Ticket #2252. Make sure the an INSERT from identical tables 458103b7d2Sdrh# does not violate constraints. 468103b7d2Sdrh# 478103b7d2Sdrhdo_test insert4-1.1 { 48dd73521bSdrh set sqlite3_xferopt_count 0 498103b7d2Sdrh execsql { 50fb658dedSdrh DELETE FROM t1; 51fb658dedSdrh DELETE FROM t2; 528103b7d2Sdrh INSERT INTO t2 VALUES(9,1); 538103b7d2Sdrh } 548103b7d2Sdrh catchsql { 558103b7d2Sdrh INSERT INTO t1 SELECT * FROM t2; 568103b7d2Sdrh } 5792e21ef0Sdrh} {1 {CHECK constraint failed: b>a}} 58dd73521bSdrhxferopt_test insert4-1.2 0 59dd73521bSdrhdo_test insert4-1.3 { 608103b7d2Sdrh execsql { 618103b7d2Sdrh SELECT * FROM t1; 628103b7d2Sdrh } 638103b7d2Sdrh} {} 648103b7d2Sdrh 65dd73521bSdrh# Tests to make sure that the transfer optimization is not occurring 66dd73521bSdrh# when it is not a valid optimization. 678103b7d2Sdrh# 68dd73521bSdrh# The SELECT must be against a real table. 69dd73521bSdrhdo_test insert4-2.1.1 { 708103b7d2Sdrh execsql { 71fb658dedSdrh DELETE FROM t1; 728103b7d2Sdrh INSERT INTO t1 SELECT 4, 8; 738103b7d2Sdrh SELECT * FROM t1; 748103b7d2Sdrh } 758103b7d2Sdrh} {4 8} 76dd73521bSdrhxferopt_test insert4-2.1.2 0 778103b7d2Sdrhdo_test insert4-2.2.1 { 78dd73521bSdrh catchsql { 79dd73521bSdrh DELETE FROM t1; 80fb658dedSdrh INSERT INTO t1 SELECT * FROM v2; 81dd73521bSdrh SELECT * FROM t1; 82dd73521bSdrh } 83dd73521bSdrh} {0 {1 9}} 84dd73521bSdrhxferopt_test insert4-2.2.2 0 85dd73521bSdrh 86dd73521bSdrh# Do not run the transfer optimization if there is a LIMIT clause 87dd73521bSdrh# 88dd73521bSdrhdo_test insert4-2.3.1 { 898103b7d2Sdrh execsql { 90fb658dedSdrh DELETE FROM t2; 91fb658dedSdrh INSERT INTO t2 VALUES(9,1); 928103b7d2Sdrh INSERT INTO t2 SELECT y, x FROM t2; 938103b7d2Sdrh INSERT INTO t3 SELECT * FROM t2 LIMIT 1; 948103b7d2Sdrh SELECT * FROM t3; 958103b7d2Sdrh } 968103b7d2Sdrh} {9 1} 97dd73521bSdrhxferopt_test insert4-2.3.2 0 98dd73521bSdrhdo_test insert4-2.3.3 { 998103b7d2Sdrh catchsql { 1008103b7d2Sdrh DELETE FROM t1; 1018103b7d2Sdrh INSERT INTO t1 SELECT * FROM t2 LIMIT 1; 1028103b7d2Sdrh SELECT * FROM t1; 1038103b7d2Sdrh } 10492e21ef0Sdrh} {1 {CHECK constraint failed: b>a}} 105dd73521bSdrhxferopt_test insert4-2.3.4 0 106dd73521bSdrh 107dd73521bSdrh# Do not run the transfer optimization if there is a DISTINCT 108dd73521bSdrh# 109dd73521bSdrhdo_test insert4-2.4.1 { 1108103b7d2Sdrh execsql { 1118103b7d2Sdrh DELETE FROM t3; 1128103b7d2Sdrh INSERT INTO t3 SELECT DISTINCT * FROM t2; 1138103b7d2Sdrh SELECT * FROM t3; 1148103b7d2Sdrh } 11538cc40c2Sdan} {9 1 1 9} 116dd73521bSdrhxferopt_test insert4-2.4.2 0 117dd73521bSdrhdo_test insert4-2.4.3 { 1188103b7d2Sdrh catchsql { 1198103b7d2Sdrh DELETE FROM t1; 1208103b7d2Sdrh INSERT INTO t1 SELECT DISTINCT * FROM t2; 1218103b7d2Sdrh } 12292e21ef0Sdrh} {1 {CHECK constraint failed: b>a}} 123dd73521bSdrhxferopt_test insert4-2.4.4 0 124dd73521bSdrh 125fb658dedSdrh# The following procedure constructs two tables then tries to transfer 126fb658dedSdrh# data from one table to the other. Checks are made to make sure the 127fb658dedSdrh# transfer is successful and that the transfer optimization was used or 128fb658dedSdrh# not, as appropriate. 129fb658dedSdrh# 130fb658dedSdrh# xfer_check TESTID XFER-USED INIT-DATA DEST-SCHEMA SRC-SCHEMA 131fb658dedSdrh# 132fb658dedSdrh# The TESTID argument is the symbolic name for this test. The XFER-USED 133fb658dedSdrh# argument is true if the transfer optimization should be employed and 134fb658dedSdrh# false if not. INIT-DATA is a single row of data that is to be 135fb658dedSdrh# transfered. DEST-SCHEMA and SRC-SCHEMA are table declarations for 136fb658dedSdrh# the destination and source tables. 137fb658dedSdrh# 138fb658dedSdrhproc xfer_check {testid xferused initdata destschema srcschema} { 139fb658dedSdrh execsql "CREATE TABLE dest($destschema)" 140fb658dedSdrh execsql "CREATE TABLE src($srcschema)" 141fb658dedSdrh execsql "INSERT INTO src VALUES([join $initdata ,])" 142fb658dedSdrh set ::sqlite3_xferopt_count 0 143fb658dedSdrh do_test $testid.1 { 144fb658dedSdrh execsql { 145fb658dedSdrh INSERT INTO dest SELECT * FROM src; 146fb658dedSdrh SELECT * FROM dest; 147fb658dedSdrh } 148fb658dedSdrh } $initdata 149fb658dedSdrh do_test $testid.2 { 150fb658dedSdrh set ::sqlite3_xferopt_count 151fb658dedSdrh } $xferused 152fb658dedSdrh execsql { 153fb658dedSdrh DROP TABLE dest; 154fb658dedSdrh DROP TABLE src; 155fb658dedSdrh } 156fb658dedSdrh} 157fb658dedSdrh 158fb658dedSdrh 159dd73521bSdrh# Do run the transfer optimization if tables have identical 160dd73521bSdrh# CHECK constraints. 161dd73521bSdrh# 162fb658dedSdrhxfer_check insert4-3.1 1 {1 9} \ 163fb658dedSdrh {a int, b int CHECK(b>a)} \ 164fb658dedSdrh {x int, y int CHECK(y>x)} 165fb658dedSdrhxfer_check insert4-3.2 1 {1 9} \ 166fb658dedSdrh {a int, b int CHECK(b>a)} \ 167fb658dedSdrh {x int CHECK(y>x), y int} 168fb658dedSdrh 169fb658dedSdrh# Do run the transfer optimization if the destination table lacks 170fb658dedSdrh# any CHECK constraints regardless of whether or not there are CHECK 171fb658dedSdrh# constraints on the source table. 172fb658dedSdrh# 173fb658dedSdrhxfer_check insert4-3.3 1 {1 9} \ 174fb658dedSdrh {a int, b int} \ 175fb658dedSdrh {x int, y int CHECK(y>x)} 176fb658dedSdrh 177fb658dedSdrh# Do run the transfer optimization if the destination table omits 178fb658dedSdrh# NOT NULL constraints that the source table has. 179fb658dedSdrh# 180fb658dedSdrhxfer_check insert4-3.4 0 {1 9} \ 181fb658dedSdrh {a int, b int CHECK(b>a)} \ 182fb658dedSdrh {x int, y int} 183fb658dedSdrh 184fb658dedSdrh# Do not run the optimization if the destination has NOT NULL 185fb658dedSdrh# constraints that the source table lacks. 186fb658dedSdrh# 187fb658dedSdrhxfer_check insert4-3.5 0 {1 9} \ 188fb658dedSdrh {a int, b int NOT NULL} \ 189fb658dedSdrh {x int, y int} 190fb658dedSdrhxfer_check insert4-3.6 0 {1 9} \ 191fb658dedSdrh {a int, b int NOT NULL} \ 192fb658dedSdrh {x int NOT NULL, y int} 193fb658dedSdrhxfer_check insert4-3.7 0 {1 9} \ 194fb658dedSdrh {a int NOT NULL, b int NOT NULL} \ 195fb658dedSdrh {x int NOT NULL, y int} 196fb658dedSdrhxfer_check insert4-3.8 0 {1 9} \ 197fb658dedSdrh {a int NOT NULL, b int} \ 198fb658dedSdrh {x int, y int} 199fb658dedSdrh 200fb658dedSdrh 201fb658dedSdrh# Do run the transfer optimization if the destination table and 202fb658dedSdrh# source table have the same NOT NULL constraints or if the 203fb658dedSdrh# source table has extra NOT NULL constraints. 204fb658dedSdrh# 205fb658dedSdrhxfer_check insert4-3.9 1 {1 9} \ 206fb658dedSdrh {a int, b int} \ 207fb658dedSdrh {x int NOT NULL, y int} 208fb658dedSdrhxfer_check insert4-3.10 1 {1 9} \ 209fb658dedSdrh {a int, b int} \ 210fb658dedSdrh {x int NOT NULL, y int NOT NULL} 211fb658dedSdrhxfer_check insert4-3.11 1 {1 9} \ 212fb658dedSdrh {a int NOT NULL, b int} \ 213fb658dedSdrh {x int NOT NULL, y int NOT NULL} 214fb658dedSdrhxfer_check insert4-3.12 1 {1 9} \ 215fb658dedSdrh {a int, b int NOT NULL} \ 216fb658dedSdrh {x int NOT NULL, y int NOT NULL} 217fb658dedSdrh 218fb658dedSdrh# Do not run the optimization if any corresponding table 219fb658dedSdrh# columns have different affinities. 220fb658dedSdrh# 221fb658dedSdrhxfer_check insert4-3.20 0 {1 9} \ 222fb658dedSdrh {a text, b int} \ 223fb658dedSdrh {x int, b int} 224fb658dedSdrhxfer_check insert4-3.21 0 {1 9} \ 225fb658dedSdrh {a int, b int} \ 226fb658dedSdrh {x text, b int} 227fb658dedSdrh 228fb658dedSdrh# "int" and "integer" are equivalent so the optimization should 229fb658dedSdrh# run here. 230fb658dedSdrh# 231fb658dedSdrhxfer_check insert4-3.22 1 {1 9} \ 232fb658dedSdrh {a int, b int} \ 233fb658dedSdrh {x integer, b int} 234fb658dedSdrh 235f33c9fadSdrh# Ticket #2291. 236f33c9fadSdrh# 2375a8f9374Sdanielk1977 2385a8f9374Sdanielk1977do_test insert4-4.1a { 2395a8f9374Sdanielk1977 execsql {CREATE TABLE t4(a, b, UNIQUE(a,b))} 2405a8f9374Sdanielk1977} {} 2415a8f9374Sdanielk1977ifcapable vacuum { 2425a8f9374Sdanielk1977 do_test insert4-4.1b { 243f33c9fadSdrh execsql { 244f33c9fadSdrh INSERT INTO t4 VALUES(NULL,0); 245f33c9fadSdrh INSERT INTO t4 VALUES(NULL,1); 246f33c9fadSdrh INSERT INTO t4 VALUES(NULL,1); 247f33c9fadSdrh VACUUM; 248f33c9fadSdrh } 249f33c9fadSdrh } {} 2505a8f9374Sdanielk1977} 251f33c9fadSdrh 2525ce240a6Sdanielk1977# Check some error conditions: 2535ce240a6Sdanielk1977# 2545ce240a6Sdanielk1977do_test insert4-5.1 { 2555ce240a6Sdanielk1977 # Table does not exist. 25605a86c5cSdrh catchsql { INSERT INTO t2 SELECT a, b FROM nosuchtable } 2575ce240a6Sdanielk1977} {1 {no such table: nosuchtable}} 2585ce240a6Sdanielk1977do_test insert4-5.2 { 2595ce240a6Sdanielk1977 # Number of columns does not match. 2605ce240a6Sdanielk1977 catchsql { 2615ce240a6Sdanielk1977 CREATE TABLE t5(a, b, c); 2625ce240a6Sdanielk1977 INSERT INTO t4 SELECT * FROM t5; 2635ce240a6Sdanielk1977 } 2645ce240a6Sdanielk1977} {1 {table t4 has 2 columns but 3 values were supplied}} 2655ce240a6Sdanielk1977 2665ce240a6Sdanielk1977do_test insert4-6.1 { 26760a713c6Sdrh set ::sqlite3_xferopt_count 0 2685ce240a6Sdanielk1977 execsql { 2695ce240a6Sdanielk1977 CREATE INDEX t2_i2 ON t2(x, y COLLATE nocase); 2705ce240a6Sdanielk1977 CREATE INDEX t2_i1 ON t2(x ASC, y DESC); 2715ce240a6Sdanielk1977 CREATE INDEX t3_i1 ON t3(a, b); 2725ce240a6Sdanielk1977 INSERT INTO t2 SELECT * FROM t3; 2735ce240a6Sdanielk1977 } 27460a713c6Sdrh set ::sqlite3_xferopt_count 27560a713c6Sdrh} {0} 27660a713c6Sdrhdo_test insert4-6.2 { 27760a713c6Sdrh set ::sqlite3_xferopt_count 0 27860a713c6Sdrh execsql { 27960a713c6Sdrh DROP INDEX t2_i2; 28060a713c6Sdrh INSERT INTO t2 SELECT * FROM t3; 28160a713c6Sdrh } 28260a713c6Sdrh set ::sqlite3_xferopt_count 28360a713c6Sdrh} {0} 28460a713c6Sdrhdo_test insert4-6.3 { 28560a713c6Sdrh set ::sqlite3_xferopt_count 0 28660a713c6Sdrh execsql { 28760a713c6Sdrh DROP INDEX t2_i1; 28860a713c6Sdrh CREATE INDEX t2_i1 ON t2(x ASC, y ASC); 28960a713c6Sdrh INSERT INTO t2 SELECT * FROM t3; 29060a713c6Sdrh } 29160a713c6Sdrh set ::sqlite3_xferopt_count 29260a713c6Sdrh} {1} 29360a713c6Sdrhdo_test insert4-6.4 { 29460a713c6Sdrh set ::sqlite3_xferopt_count 0 29560a713c6Sdrh execsql { 29660a713c6Sdrh DROP INDEX t2_i1; 29760a713c6Sdrh CREATE INDEX t2_i1 ON t2(x ASC, y COLLATE RTRIM); 29860a713c6Sdrh INSERT INTO t2 SELECT * FROM t3; 29960a713c6Sdrh } 30060a713c6Sdrh set ::sqlite3_xferopt_count 30160a713c6Sdrh} {0} 30260a713c6Sdrh 3035ce240a6Sdanielk1977 3041d9da70aSdrhdo_test insert4-6.5 { 3051d9da70aSdrh execsql { 3061d9da70aSdrh CREATE TABLE t6a(x CHECK( x<>'abc' )); 3071d9da70aSdrh INSERT INTO t6a VALUES('ABC'); 3081d9da70aSdrh SELECT * FROM t6a; 3091d9da70aSdrh } 3101d9da70aSdrh} {ABC} 3111d9da70aSdrhdo_test insert4-6.6 { 3121d9da70aSdrh execsql { 3131d9da70aSdrh CREATE TABLE t6b(x CHECK( x<>'abc' COLLATE nocase )); 3141d9da70aSdrh } 3151d9da70aSdrh catchsql { 3161d9da70aSdrh INSERT INTO t6b SELECT * FROM t6a; 3171d9da70aSdrh } 31892e21ef0Sdrh} {1 {CHECK constraint failed: x<>'abc' COLLATE nocase}} 3191d9da70aSdrhdo_test insert4-6.7 { 3201d9da70aSdrh execsql { 3211d9da70aSdrh DROP TABLE t6b; 3221d9da70aSdrh CREATE TABLE t6b(x CHECK( x COLLATE nocase <>'abc' )); 3231d9da70aSdrh } 3241d9da70aSdrh catchsql { 3251d9da70aSdrh INSERT INTO t6b SELECT * FROM t6a; 3261d9da70aSdrh } 32792e21ef0Sdrh} {1 {CHECK constraint failed: x COLLATE nocase <>'abc'}} 3285ce240a6Sdanielk1977 329713de341Sdrh# Ticket [6284df89debdfa61db8073e062908af0c9b6118e] 330713de341Sdrh# Disable the xfer optimization if the destination table contains 331713de341Sdrh# a foreign key constraint 332713de341Sdrh# 333713de341Sdrhifcapable foreignkey { 334713de341Sdrh do_test insert4-7.1 { 335713de341Sdrh set ::sqlite3_xferopt_count 0 336713de341Sdrh execsql { 337713de341Sdrh CREATE TABLE t7a(x INTEGER PRIMARY KEY); INSERT INTO t7a VALUES(123); 338713de341Sdrh CREATE TABLE t7b(y INTEGER REFERENCES t7a); 339713de341Sdrh CREATE TABLE t7c(z INT); INSERT INTO t7c VALUES(234); 340713de341Sdrh INSERT INTO t7b SELECT * FROM t7c; 341713de341Sdrh SELECT * FROM t7b; 342713de341Sdrh } 343713de341Sdrh } {234} 344713de341Sdrh do_test insert4-7.2 { 345713de341Sdrh set ::sqlite3_xferopt_count 346713de341Sdrh } {1} 347713de341Sdrh do_test insert4-7.3 { 348713de341Sdrh set ::sqlite3_xferopt_count 0 349713de341Sdrh execsql { 350713de341Sdrh DELETE FROM t7b; 351713de341Sdrh PRAGMA foreign_keys=ON; 352713de341Sdrh } 353713de341Sdrh catchsql { 354713de341Sdrh INSERT INTO t7b SELECT * FROM t7c; 355713de341Sdrh } 356f9c8ce3cSdrh } {1 {FOREIGN KEY constraint failed}} 357713de341Sdrh do_test insert4-7.4 { 358713de341Sdrh execsql {SELECT * FROM t7b} 359713de341Sdrh } {} 360713de341Sdrh do_test insert4-7.5 { 361713de341Sdrh set ::sqlite3_xferopt_count 362713de341Sdrh } {0} 363713de341Sdrh do_test insert4-7.6 { 364713de341Sdrh set ::sqlite3_xferopt_count 0 365713de341Sdrh execsql { 366713de341Sdrh DELETE FROM t7b; DELETE FROM t7c; 367713de341Sdrh INSERT INTO t7c VALUES(123); 368713de341Sdrh INSERT INTO t7b SELECT * FROM t7c; 369713de341Sdrh SELECT * FROM t7b; 370713de341Sdrh } 371713de341Sdrh } {123} 372713de341Sdrh do_test insert4-7.7 { 373713de341Sdrh set ::sqlite3_xferopt_count 374713de341Sdrh } {0} 375713de341Sdrh do_test insert4-7.7 { 376713de341Sdrh set ::sqlite3_xferopt_count 0 377713de341Sdrh execsql { 378713de341Sdrh PRAGMA foreign_keys=OFF; 379713de341Sdrh DELETE FROM t7b; 380713de341Sdrh INSERT INTO t7b SELECT * FROM t7c; 381713de341Sdrh SELECT * FROM t7b; 382713de341Sdrh } 383713de341Sdrh } {123} 384713de341Sdrh do_test insert4-7.8 { 385713de341Sdrh set ::sqlite3_xferopt_count 386713de341Sdrh } {1} 387713de341Sdrh} 388713de341Sdrh 389e7224a01Sdrh# Ticket [676bc02b87176125635cb174d110b431581912bb] 390e7224a01Sdrh# Make sure INTEGER PRIMARY KEY ON CONFLICT ... works with the xfer 391e7224a01Sdrh# optimization. 392e7224a01Sdrh# 393e7224a01Sdrhdo_test insert4-8.1 { 394e7224a01Sdrh execsql { 395e7224a01Sdrh DROP TABLE IF EXISTS t1; 396e7224a01Sdrh DROP TABLE IF EXISTS t2; 397e7224a01Sdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b); 398e7224a01Sdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT REPLACE, y); 399e7224a01Sdrh INSERT INTO t1 VALUES(1,2); 400e7224a01Sdrh INSERT INTO t2 VALUES(1,3); 401e7224a01Sdrh INSERT INTO t1 SELECT * FROM t2; 402e7224a01Sdrh SELECT * FROM t1; 403e7224a01Sdrh } 404e7224a01Sdrh} {1 3} 405e7224a01Sdrhdo_test insert4-8.2 { 406e7224a01Sdrh execsql { 407e7224a01Sdrh DROP TABLE IF EXISTS t1; 408e7224a01Sdrh DROP TABLE IF EXISTS t2; 409e7224a01Sdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b); 410e7224a01Sdrh CREATE TABLE t2(x, y); 411e7224a01Sdrh INSERT INTO t1 VALUES(1,2); 412e7224a01Sdrh INSERT INTO t2 VALUES(1,3); 413e7224a01Sdrh INSERT INTO t1 SELECT * FROM t2; 414e7224a01Sdrh SELECT * FROM t1; 415e7224a01Sdrh } 416e7224a01Sdrh} {1 3} 417e7224a01Sdrhdo_test insert4-8.3 { 418e7224a01Sdrh execsql { 419e7224a01Sdrh DROP TABLE IF EXISTS t1; 420e7224a01Sdrh DROP TABLE IF EXISTS t2; 421e7224a01Sdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT IGNORE, b); 422e7224a01Sdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT IGNORE, y); 423e7224a01Sdrh INSERT INTO t1 VALUES(1,2); 424e7224a01Sdrh INSERT INTO t2 VALUES(1,3); 425e7224a01Sdrh INSERT INTO t1 SELECT * FROM t2; 426e7224a01Sdrh SELECT * FROM t1; 427e7224a01Sdrh } 428e7224a01Sdrh} {1 2} 429e7224a01Sdrhdo_test insert4-8.4 { 430e7224a01Sdrh execsql { 431e7224a01Sdrh DROP TABLE IF EXISTS t1; 432e7224a01Sdrh DROP TABLE IF EXISTS t2; 433e7224a01Sdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT IGNORE, b); 434e7224a01Sdrh CREATE TABLE t2(x, y); 435e7224a01Sdrh INSERT INTO t1 VALUES(1,2); 436e7224a01Sdrh INSERT INTO t2 VALUES(1,3); 437e7224a01Sdrh INSERT INTO t1 SELECT * FROM t2; 438e7224a01Sdrh SELECT * FROM t1; 439e7224a01Sdrh } 440e7224a01Sdrh} {1 2} 441e7224a01Sdrhdo_test insert4-8.5 { 442e7224a01Sdrh execsql { 443e7224a01Sdrh DROP TABLE IF EXISTS t1; 444e7224a01Sdrh DROP TABLE IF EXISTS t2; 445e7224a01Sdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT FAIL, b); 446e7224a01Sdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT FAIL, y); 447e7224a01Sdrh INSERT INTO t1 VALUES(1,2); 448e7224a01Sdrh INSERT INTO t2 VALUES(-99,100); 449e7224a01Sdrh INSERT INTO t2 VALUES(1,3); 450e7224a01Sdrh SELECT * FROM t1; 451e7224a01Sdrh } 452e7224a01Sdrh catchsql { 453e7224a01Sdrh INSERT INTO t1 SELECT * FROM t2; 454e7224a01Sdrh } 455f9c8ce3cSdrh} {1 {UNIQUE constraint failed: t1.a}} 456e7224a01Sdrhdo_test insert4-8.6 { 457e7224a01Sdrh execsql { 458e7224a01Sdrh SELECT * FROM t1; 459e7224a01Sdrh } 460e7224a01Sdrh} {-99 100 1 2} 461e7224a01Sdrhdo_test insert4-8.7 { 462e7224a01Sdrh execsql { 463e7224a01Sdrh DROP TABLE IF EXISTS t1; 464e7224a01Sdrh DROP TABLE IF EXISTS t2; 465e7224a01Sdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT ABORT, b); 466e7224a01Sdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT ABORT, y); 467e7224a01Sdrh INSERT INTO t1 VALUES(1,2); 468e7224a01Sdrh INSERT INTO t2 VALUES(-99,100); 469e7224a01Sdrh INSERT INTO t2 VALUES(1,3); 470e7224a01Sdrh SELECT * FROM t1; 471e7224a01Sdrh } 472e7224a01Sdrh catchsql { 473e7224a01Sdrh INSERT INTO t1 SELECT * FROM t2; 474e7224a01Sdrh } 475f9c8ce3cSdrh} {1 {UNIQUE constraint failed: t1.a}} 476e7224a01Sdrhdo_test insert4-8.8 { 477e7224a01Sdrh execsql { 478e7224a01Sdrh SELECT * FROM t1; 479e7224a01Sdrh } 480e7224a01Sdrh} {1 2} 481e7224a01Sdrhdo_test insert4-8.9 { 482e7224a01Sdrh execsql { 483e7224a01Sdrh DROP TABLE IF EXISTS t1; 484e7224a01Sdrh DROP TABLE IF EXISTS t2; 485e7224a01Sdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT ROLLBACK, b); 486e7224a01Sdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT ROLLBACK, y); 487e7224a01Sdrh INSERT INTO t1 VALUES(1,2); 488e7224a01Sdrh INSERT INTO t2 VALUES(-99,100); 489e7224a01Sdrh INSERT INTO t2 VALUES(1,3); 490e7224a01Sdrh SELECT * FROM t1; 491e7224a01Sdrh } 492e7224a01Sdrh catchsql { 493e7224a01Sdrh BEGIN; 494e7224a01Sdrh INSERT INTO t1 VALUES(2,3); 495e7224a01Sdrh INSERT INTO t1 SELECT * FROM t2; 496e7224a01Sdrh } 497f9c8ce3cSdrh} {1 {UNIQUE constraint failed: t1.a}} 498e7224a01Sdrhdo_test insert4-8.10 { 499e7224a01Sdrh catchsql {COMMIT} 500e7224a01Sdrh} {1 {cannot commit - no transaction is active}} 501e7224a01Sdrhdo_test insert4-8.11 { 502e7224a01Sdrh execsql { 503e7224a01Sdrh SELECT * FROM t1; 504e7224a01Sdrh } 505e7224a01Sdrh} {1 2} 506e7224a01Sdrh 507ccdf1baeSdrhdo_test insert4-8.21 { 508ccdf1baeSdrh execsql { 509ccdf1baeSdrh DROP TABLE IF EXISTS t1; 510ccdf1baeSdrh DROP TABLE IF EXISTS t2; 511ccdf1baeSdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT REPLACE, b); 512ccdf1baeSdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT REPLACE, y); 513ccdf1baeSdrh INSERT INTO t2 VALUES(1,3); 514ccdf1baeSdrh INSERT INTO t1 SELECT * FROM t2; 515ccdf1baeSdrh SELECT * FROM t1; 516ccdf1baeSdrh } 517ccdf1baeSdrh} {1 3} 518ccdf1baeSdrhdo_test insert4-8.22 { 519ccdf1baeSdrh execsql { 520ccdf1baeSdrh DROP TABLE IF EXISTS t1; 521ccdf1baeSdrh DROP TABLE IF EXISTS t2; 522ccdf1baeSdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT IGNORE, b); 523ccdf1baeSdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT IGNORE, y); 524ccdf1baeSdrh INSERT INTO t2 VALUES(1,3); 525ccdf1baeSdrh INSERT INTO t1 SELECT * FROM t2; 526ccdf1baeSdrh SELECT * FROM t1; 527ccdf1baeSdrh } 528ccdf1baeSdrh} {1 3} 529ccdf1baeSdrhdo_test insert4-8.23 { 530ccdf1baeSdrh execsql { 531ccdf1baeSdrh DROP TABLE IF EXISTS t1; 532ccdf1baeSdrh DROP TABLE IF EXISTS t2; 533ccdf1baeSdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT ABORT, b); 534ccdf1baeSdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT ABORT, y); 535ccdf1baeSdrh INSERT INTO t2 VALUES(1,3); 536ccdf1baeSdrh INSERT INTO t1 SELECT * FROM t2; 537ccdf1baeSdrh SELECT * FROM t1; 538ccdf1baeSdrh } 539ccdf1baeSdrh} {1 3} 540ccdf1baeSdrhdo_test insert4-8.24 { 541ccdf1baeSdrh execsql { 542ccdf1baeSdrh DROP TABLE IF EXISTS t1; 543ccdf1baeSdrh DROP TABLE IF EXISTS t2; 544ccdf1baeSdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT FAIL, b); 545ccdf1baeSdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT FAIL, y); 546ccdf1baeSdrh INSERT INTO t2 VALUES(1,3); 547ccdf1baeSdrh INSERT INTO t1 SELECT * FROM t2; 548ccdf1baeSdrh SELECT * FROM t1; 549ccdf1baeSdrh } 550ccdf1baeSdrh} {1 3} 551ccdf1baeSdrhdo_test insert4-8.25 { 552ccdf1baeSdrh execsql { 553ccdf1baeSdrh DROP TABLE IF EXISTS t1; 554ccdf1baeSdrh DROP TABLE IF EXISTS t2; 555ccdf1baeSdrh CREATE TABLE t1(a INTEGER PRIMARY KEY ON CONFLICT ROLLBACK, b); 556ccdf1baeSdrh CREATE TABLE t2(x INTEGER PRIMARY KEY ON CONFLICT ROLLBACK, y); 557ccdf1baeSdrh INSERT INTO t2 VALUES(1,3); 558ccdf1baeSdrh INSERT INTO t1 SELECT * FROM t2; 559ccdf1baeSdrh SELECT * FROM t1; 560ccdf1baeSdrh } 561ccdf1baeSdrh} {1 3} 562ccdf1baeSdrh 563992590beSdrhdo_catchsql_test insert4-9.1 { 564992590beSdrh DROP TABLE IF EXISTS t1; 565992590beSdrh CREATE TABLE t1(x); 566992590beSdrh INSERT INTO t1(x) VALUES(5 COLLATE xyzzy) UNION SELECT 0; 567992590beSdrh} {1 {no such collation sequence: xyzzy}} 568e7224a01Sdrh 56975f95588Sdan#------------------------------------------------------------------------- 57075f95588Sdan# Check that running an integrity-check does not disable the xfer 57175f95588Sdan# optimization for tables with CHECK constraints. 57275f95588Sdan# 57375f95588Sdando_execsql_test 10.1 { 57475f95588Sdan CREATE TABLE t8( 57575f95588Sdan rid INTEGER, 57675f95588Sdan pid INTEGER, 57775f95588Sdan mid INTEGER, 57875f95588Sdan px INTEGER DEFAULT(0) CHECK(px IN(0, 1)) 57975f95588Sdan ); 58075f95588Sdan CREATE TEMP TABLE x( 58175f95588Sdan rid INTEGER, 58275f95588Sdan pid INTEGER, 58375f95588Sdan mid INTEGER, 58475f95588Sdan px INTEGER DEFAULT(0) CHECK(px IN(0, 1)) 58575f95588Sdan ); 58675f95588Sdan} 58775f95588Sdando_test 10.2 { 58875f95588Sdan set sqlite3_xferopt_count 0 58975f95588Sdan execsql { INSERT INTO x SELECT * FROM t8 } 59075f95588Sdan set sqlite3_xferopt_count 59175f95588Sdan} {1} 59275f95588Sdan 59375f95588Sdando_test 10.3 { 59475f95588Sdan execsql { PRAGMA integrity_check } 59575f95588Sdan set sqlite3_xferopt_count 0 59675f95588Sdan execsql { INSERT INTO x SELECT * FROM t8 } 59775f95588Sdan set sqlite3_xferopt_count 59875f95588Sdan} {1} 59975f95588Sdan 600*935c3722Sdrhdo_test 10.4 { 601*935c3722Sdrh execsql { PRAGMA integrity_check } 602*935c3722Sdrh set sqlite3_xferopt_count 0 603*935c3722Sdrh execsql { INSERT INTO x SELECT * FROM t8 RETURNING * } 604*935c3722Sdrh set sqlite3_xferopt_count 605*935c3722Sdrh} {0} 606*935c3722Sdrh 607b84b38fdSdan#------------------------------------------------------------------------- 608b84b38fdSdan# xfer transfer between tables where the source has an empty partial index. 609b84b38fdSdan# 610b84b38fdSdando_execsql_test 11.0 { 611b84b38fdSdan CREATE TABLE t9(a, b, c); 612b84b38fdSdan CREATE INDEX t9a ON t9(a); 613b84b38fdSdan CREATE INDEX t9b ON t9(b) WHERE c=0; 614b84b38fdSdan 615b84b38fdSdan INSERT INTO t9 VALUES(1, 1, 1); 616b84b38fdSdan INSERT INTO t9 VALUES(2, 2, 2); 617b84b38fdSdan INSERT INTO t9 VALUES(3, 3, 3); 618b84b38fdSdan 619b84b38fdSdan CREATE TABLE t10(a, b, c); 620b84b38fdSdan CREATE INDEX t10a ON t10(a); 621b84b38fdSdan CREATE INDEX t10b ON t10(b) WHERE c=0; 622b84b38fdSdan 623b84b38fdSdan INSERT INTO t10 SELECT * FROM t9; 624b84b38fdSdan SELECT * FROM t10; 625b84b38fdSdan PRAGMA integrity_check; 626b84b38fdSdan} {1 1 1 2 2 2 3 3 3 ok} 62775f95588Sdan 6288103b7d2Sdrhfinish_test 629