3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing the INSERT statement.
14 # $Id: insert.test,v 1.31 2007/04/05 11:25:59 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Try to insert into a non-existant table.
22 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
24 } {1 {no such table: test1}}
26 # Try to insert into sqlite_master
29 set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
31 } {1 {table sqlite_master may not be modified}}
33 # Try to insert the wrong number of entries.
36 execsql {CREATE TABLE test1(one int, two int, three int)}
37 set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
39 } {1 {table test1 has 3 columns but 2 values were supplied}}
41 set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
43 } {1 {table test1 has 3 columns but 4 values were supplied}}
45 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
47 } {1 {4 values for 2 columns}}
49 set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
51 } {1 {1 values for 2 columns}}
53 # Try to insert into a non-existant column of a table.
56 set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
58 } {1 {table test1 has no column named four}}
60 # Make sure the inserts actually happen
63 execsql {INSERT INTO test1 VALUES(1,2,3)}
64 execsql {SELECT * FROM test1}
67 execsql {INSERT INTO test1 VALUES(4,5,6)}
68 execsql {SELECT * FROM test1 ORDER BY one}
71 execsql {INSERT INTO test1 VALUES(7,8,9)}
72 execsql {SELECT * FROM test1 ORDER BY one}
76 execsql {DELETE FROM test1}
77 execsql {INSERT INTO test1(one,two) VALUES(1,2)}
78 execsql {SELECT * FROM test1 ORDER BY one}
81 execsql {INSERT INTO test1(two,three) VALUES(5,6)}
82 execsql {SELECT * FROM test1 ORDER BY one}
85 execsql {INSERT INTO test1(three,one) VALUES(7,8)}
86 execsql {SELECT * FROM test1 ORDER BY one}
87 } {{} 5 6 1 2 {} 8 {} 7}
89 # A table to use for testing default values
95 f2 real default +4.32,
100 execsql {SELECT * from test2}
103 execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
104 execsql {SELECT * FROM test2}
107 execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
108 execsql {SELECT * FROM test2 WHERE f1==-111}
109 } {-111 1.23 222 -3.45}
111 execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
112 execsql {SELECT * FROM test2 WHERE f1==77}
114 do_test insert-2.10 {
119 f2 real default -4.32,
121 f4 text default 'abc-123',
125 execsql {SELECT * from test2}
127 do_test insert-2.11 {
128 execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
129 execsql {SELECT * FROM test2}
130 } {111 -2.22 hi hi! {}}
131 do_test insert-2.12 {
132 execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
133 execsql {SELECT * FROM test2 ORDER BY f1}
134 } {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
136 # Do additional inserts with default values, but this time
137 # on a table that has indices. In particular we want to verify
138 # that the correct default values are inserted into the indices.
143 CREATE INDEX index9 ON test2(f1,f2);
144 CREATE INDEX indext ON test2(f4,f5);
149 # Update for sqlite3 v3:
150 # Change the 111 to '111' in the following two test cases, because
151 # the default value is being inserted as a string. TODO: It shouldn't be.
153 execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
154 execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33}
155 } {111 -3.33 hi hum {}}
157 execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
158 execsql {SELECT * FROM test2 WHERE f1='111' AND f2=-3.33}
159 } {111 -3.33 hi hum {}}
161 execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
162 } {22 -4.44 hi abc-123 wham}
163 ifcapable {reindex} {
168 integrity_check insert-3.5
170 # Test of expressions in the VALUES clause
174 CREATE TABLE t3(a,b,c);
175 INSERT INTO t3 VALUES(1+2+3,4,5);
181 execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);}
183 set maxa [execsql {SELECT max(a) FROM t3}]
184 execsql "INSERT INTO t3 VALUES($maxa+1,5,6);"
187 SELECT * FROM t3 ORDER BY a;
193 INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
194 SELECT * FROM t3 ORDER BY a;
196 } {1 {no such column: t3.a}}
200 execsql {INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);}
202 set b [execsql {SELECT b FROM t3 WHERE a = 0}]
203 if {$b==""} {set b NULL}
204 execsql "INSERT INTO t3 VALUES($b,6,7);"
207 SELECT * FROM t3 ORDER BY a;
209 } {{} 6 7 6 4 5 7 5 6}
212 SELECT b,c FROM t3 WHERE a IS NULL;
217 INSERT INTO t3 VALUES(notafunc(2,3),2,3);
219 } {1 {no such function: notafunc}}
222 INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
223 SELECT * FROM t3 WHERE c=99;
227 # Test the ability to insert from a temporary table into itself.
233 CREATE TEMP TABLE t4(x);
234 INSERT INTO t4 VALUES(1);
240 INSERT INTO t4 SELECT x+1 FROM t4;
244 ifcapable {explain} {
246 # verify that a temporary table is used to copy t4 to t4
248 EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
250 expr {[lsearch $x OpenEphemeral]>0}
255 # Verify that table "test1" begins on page 3. This should be the same
256 # page number used by "t4" above.
258 # Update for v3 - the first table now begins on page 2 of each file, not 3.
260 SELECT rootpage FROM sqlite_master WHERE name='test1';
262 } [expr $AUTOVACUUM?3:2]
264 # Verify that "t4" begins on page 3.
266 # Update for v3 - the first table now begins on page 2 of each file, not 3.
268 SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
272 # This should not use an intermediate temporary table.
274 INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
278 ifcapable {explain} {
280 # verify that no temporary table is used to copy test1 to t4
282 EXPLAIN INSERT INTO t4 SELECT one FROM test1;
284 expr {[lsearch $x OpenTemp]>0}
289 # Ticket #334: REPLACE statement corrupting indices.
292 # The REPLACE command is not available if SQLITE_OMIT_CONFLICT is
293 # defined at compilation time.
296 CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
297 INSERT INTO t1 VALUES(1,2);
298 INSERT INTO t1 VALUES(2,3);
299 SELECT b FROM t1 WHERE b=2;
304 REPLACE INTO t1 VALUES(1,4);
305 SELECT b FROM t1 WHERE b=2;
310 UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
311 SELECT * FROM t1 WHERE b=4;
316 SELECT * FROM t1 WHERE b=3;
319 ifcapable {reindex} {
331 # Test that the special optimization for queries of the form
332 # "SELECT max(x) FROM tbl" where there is an index on tbl(x) works with
337 INSERT INTO t1 VALUES(1);
338 INSERT INTO t1 VALUES(2);
339 CREATE INDEX i1 ON t1(a);
344 INSERT INTO t1 SELECT max(a) FROM t1;
353 # Ticket #1140: Check for an infinite loop in the algorithm that tests
354 # to see if the right-hand side of an INSERT...SELECT references the left-hand
357 ifcapable subquery&&compound {
360 INSERT INTO t3 SELECT * FROM (SELECT * FROM t3 UNION ALL SELECT 1,2,3)
365 # Make sure the rowid cache in the VDBE is reset correctly when
366 # an explicit rowid is given.
371 INSERT INTO t5 VALUES(1);
372 INSERT INTO t5 VALUES(2);
373 INSERT INTO t5 VALUES(3);
374 INSERT INTO t5(rowid, x) SELECT nullif(x*2+10,14), x+100 FROM t5;
375 SELECT rowid, x FROM t5;
377 } {1 1 2 2 3 3 12 101 13 102 16 103}
380 CREATE TABLE t6(x INTEGER PRIMARY KEY, y);
381 INSERT INTO t6 VALUES(1,1);
382 INSERT INTO t6 VALUES(2,2);
383 INSERT INTO t6 VALUES(3,3);
384 INSERT INTO t6 SELECT nullif(y*2+10,14), y+100 FROM t6;
387 } {1 1 2 2 3 3 12 101 13 102 16 103}
389 # Multiple VALUES clauses
392 do_test insert-10.1 {
394 CREATE TABLE t10(a,b,c);
395 INSERT INTO t10 VALUES(1,2,3), (4,5,6), (7,8,9);
398 } {1 2 3 4 5 6 7 8 9}
399 do_test insert-10.2 {
401 INSERT INTO t10 VALUES(11,12,13), (14,15), (16,17,28);
403 } {1 {all VALUES must have the same number of terms}}
406 # Need for the OP_SoftNull opcode
408 do_execsql_test insert-11.1 {
409 CREATE TABLE t11a AS SELECT '123456789' AS x;
410 CREATE TABLE t11b (a INTEGER PRIMARY KEY, b, c);
411 INSERT INTO t11b SELECT x, x, x FROM t11a;
412 SELECT quote(a), quote(b), quote(c) FROM t11b;
413 } {123456789 '123456789' '123456789'}
416 # More columns of input than there are columns in the table.
417 # Ticket http://www.sqlite.org/src/info/e9654505cfda9361
419 do_execsql_test insert-12.1 {
420 CREATE TABLE t12a(a,b,c,d,e,f,g);
421 INSERT INTO t12a VALUES(101,102,103,104,105,106,107);
422 CREATE TABLE t12b(x);
423 INSERT INTO t12b(x,rowid,x,x,x,x,x) SELECT * FROM t12a;
424 SELECT rowid, x FROM t12b;
426 do_execsql_test insert-12.2 {
427 CREATE TABLE tab1( value INTEGER);
428 INSERT INTO tab1 (value, _rowid_) values( 11, 1);
429 INSERT INTO tab1 (value, _rowid_) SELECT 22,999;
432 do_execsql_test insert-12.3 {
433 CREATE TABLE t12c(a, b DEFAULT 'xyzzy', c);
434 INSERT INTO t12c(a, rowid, c) SELECT 'one', 999, 'two';
438 # 2018-06-11. From OSSFuzz. A column cache malfunction in
439 # the constraint checking on an index of expressions causes
440 # an assertion fault in a REPLACE. Ticket
441 # https://www.sqlite.org/src/info/c2432ef9089ee73b
443 do_execsql_test insert-13.1 {
444 DROP TABLE IF EXISTS t13;
445 CREATE TABLE t13(a INTEGER PRIMARY KEY,b UNIQUE);
446 CREATE INDEX t13x1 ON t13(-b=b);
447 INSERT INTO t13 VALUES(1,5),(6,2);
448 REPLACE INTO t13 SELECT b,0 FROM t13;
449 SELECT * FROM t13 ORDER BY +b;
452 # 2019-01-17. From the chromium fuzzer.
454 do_execsql_test insert-14.1 {
455 DROP TABLE IF EXISTS t14;
456 CREATE TABLE t14(x INTEGER PRIMARY KEY);
457 INSERT INTO t14 VALUES(CASE WHEN 1 THEN null END);
461 integrity_check insert-99.0
465 do_execsql_test insert-15.1 {
466 DROP TABLE IF EXISTS t1;
467 DROP TABLE IF EXISTS t2;
468 CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);
469 CREATE INDEX i1 ON t1(b);
470 CREATE TABLE t2(a, b);
471 INSERT INTO t2 VALUES(4, randomblob(31000));
472 INSERT INTO t2 VALUES(4, randomblob(32000));
473 INSERT INTO t2 VALUES(4, randomblob(33000));
474 REPLACE INTO t1 SELECT a, b FROM t2;
475 SELECT a, length(b) FROM t1;