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.
13 # This file implements tests for miscellanous features that were
14 # left out of other test files.
16 # $Id: misc1.test,v 1.42 2007/11/05 14:58:23 drh Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # Mimic the SQLite 2 collation type NUMERIC.
22 db collate numeric numeric_collate
23 proc numeric_collate {lhs rhs} {
24 if {$lhs == $rhs} {return 0}
25 return [expr ($lhs>$rhs)?1:-1]
28 # Mimic the SQLite 2 collation type TEXT.
29 db collate text text_collate
30 proc numeric_collate {lhs rhs} {
31 return [string compare $lhs $rhs]
34 # Test the creation and use of tables that have a large number
38 set cmd "CREATE TABLE manycol(x0 text"
39 for {set i 1} {$i<=99} {incr i} {
40 append cmd ",x$i text"
44 set cmd "INSERT INTO manycol VALUES(0"
45 for {set i 1} {$i<=99} {incr i} {
50 execsql "SELECT x99 FROM manycol"
53 execsql {SELECT x0, x10, x25, x50, x75 FROM manycol}
56 for {set j 100} {$j<=1000} {incr j 100} {
57 set cmd "INSERT INTO manycol VALUES($j"
58 for {set i 1} {$i<=99} {incr i} {
59 append cmd ",[expr {$i+$j}]"
64 execsql {SELECT x50 FROM manycol ORDER BY x80+0}
65 } {50 150 250 350 450 550 650 750 850 950 1050}
67 execsql {SELECT x50 FROM manycol ORDER BY x80}
68 } {1050 150 250 350 450 550 650 750 50 850 950}
70 execsql {SELECT x75 FROM manycol WHERE x50=350}
73 execsql {SELECT x50 FROM manycol WHERE x99=599}
76 execsql {CREATE INDEX manycol_idx1 ON manycol(x99)}
77 execsql {SELECT x50 FROM manycol WHERE x99=899}
80 execsql {SELECT count(*) FROM manycol}
83 execsql {DELETE FROM manycol WHERE x98=1234}
84 execsql {SELECT count(*) FROM manycol}
87 execsql {DELETE FROM manycol WHERE x98=998}
88 execsql {SELECT count(*) FROM manycol}
91 execsql {DELETE FROM manycol WHERE x99=500}
92 execsql {SELECT count(*) FROM manycol}
95 execsql {DELETE FROM manycol WHERE x99=599}
96 execsql {SELECT count(*) FROM manycol}
99 # Check GROUP BY expressions that name two or more columns.
104 CREATE TABLE agger(one text, two text, three text, four text);
105 INSERT INTO agger VALUES(1, 'one', 'hello', 'yes');
106 INSERT INTO agger VALUES(2, 'two', 'howdy', 'no');
107 INSERT INTO agger VALUES(3, 'thr', 'howareya', 'yes');
108 INSERT INTO agger VALUES(4, 'two', 'lothere', 'yes');
109 INSERT INTO agger VALUES(5, 'one', 'atcha', 'yes');
110 INSERT INTO agger VALUES(6, 'two', 'hello', 'no');
113 execsql {SELECT count(*) FROM agger}
116 execsql {SELECT sum(one), two, four FROM agger
117 GROUP BY two, four ORDER BY sum(one) desc}
118 } {8 two no 6 one yes 4 two yes 3 thr yes}
120 execsql {SELECT sum((one)), (two), (four) FROM agger
121 GROUP BY (two), (four) ORDER BY sum(one) desc}
122 } {8 two no 6 one yes 4 two yes 3 thr yes}
124 # Here's a test for a bug found by Joel Lucsy. The code below
125 # was causing an assertion failure.
130 INSERT INTO t1 VALUES('hi');
131 PRAGMA full_column_names=on;
132 SELECT rowid, * FROM t1;
137 # Here's a test for yet another bug found by Joel Lucsy. The code
138 # below was causing an assertion failure.
144 INSERT INTO t2 VALUES('This is a long string to use up a lot of disk -');
145 UPDATE t2 SET a=a||a||a||a;
146 INSERT INTO t2 SELECT '1 - ' || a FROM t2;
147 INSERT INTO t2 SELECT '2 - ' || a FROM t2;
148 INSERT INTO t2 SELECT '3 - ' || a FROM t2;
149 INSERT INTO t2 SELECT '4 - ' || a FROM t2;
150 INSERT INTO t2 SELECT '5 - ' || a FROM t2;
151 INSERT INTO t2 SELECT '6 - ' || a FROM t2;
153 SELECT count(*) FROM t2;
157 # Make sure we actually see a semicolon or end-of-file in the SQL input
158 # before executing a command. Thus if "WHERE" is misspelled on an UPDATE,
159 # the user won't accidently update every record.
163 CREATE TABLE t3(a,b);
164 INSERT INTO t3 VALUES(1,2);
165 INSERT INTO t3 VALUES(3,4);
166 UPDATE t3 SET a=0 WHEREwww b=2;
168 } {1 {near "WHEREwww": syntax error}}
171 SELECT * FROM t3 ORDER BY a;
175 # Certain keywords (especially non-standard keywords like "REPLACE") can
176 # also be used as identifiers. The way this works in the parser is that
177 # the parser first detects a syntax error, the error handling routine
178 # sees that the special keyword caused the error, then replaces the keyword
179 # with "ID" and tries again.
181 # Check the operation of this logic.
186 abort, asc, begin, cluster, conflict, copy, delimiters, desc, end,
187 explain, fail, ignore, key, offset, pragma, replace, temp,
195 VALUES(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
202 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
205 SELECT abort+asc,max(key,pragma,temp) FROM t4
209 # Test for multi-column primary keys, and for multiple primary keys.
218 } {1 {table "error1" has more than one primary key}}
222 a INTEGER PRIMARY KEY,
226 } {1 {table "error1" has more than one primary key}}
229 CREATE TABLE t5(a,b,c,PRIMARY KEY(a,b));
230 INSERT INTO t5 VALUES(1,2,3);
231 SELECT * FROM t5 ORDER BY a;
236 INSERT INTO t5 VALUES(1,2,4);
238 } {1 {UNIQUE constraint failed: t5.a, t5.b}}
241 INSERT INTO t5 VALUES(0,2,4);
246 SELECT * FROM t5 ORDER BY a;
254 } {1 {no tables specified}}
259 } {1 {no such table: t1}}
268 # 64-bit integers are represented exactly.
272 CREATE TABLE t1(a unique not null, b unique not null);
273 INSERT INTO t1 VALUES('a',1234567890123456789);
274 INSERT INTO t1 VALUES('b',1234567891123456789);
275 INSERT INTO t1 VALUES('c',1234567892123456789);
278 } {0 {a 1234567890123456789 b 1234567891123456789 c 1234567892123456789}}
280 # A WHERE clause is not allowed to contain more than 99 terms. Check to
281 # make sure this limit is enforced.
283 # 2005-07-16: There is no longer a limit on the number of terms in a
284 # WHERE clause. But keep these tests just so that we have some tests
285 # that use a large number of terms in the WHERE clause.
288 execsql {SELECT count(*) FROM manycol}
291 set ::where {WHERE x0>=0}
292 for {set i 1} {$i<=99} {incr i} {
293 append ::where " AND x$i<>0"
295 catchsql "SELECT count(*) FROM manycol $::where"
298 catchsql "SELECT count(*) FROM manycol $::where AND rowid>0"
301 regsub "x0>=0" $::where "x0=0" ::where
302 catchsql "DELETE FROM manycol $::where"
305 execsql {SELECT count(*) FROM manycol}
308 catchsql "DELETE FROM manycol $::where AND rowid>0"
311 execsql {SELECT x1 FROM manycol WHERE x0=100}
314 regsub "x0=0" $::where "x0=100" ::where
315 catchsql "UPDATE manycol SET x1=x1+1 $::where"
318 execsql {SELECT x1 FROM manycol WHERE x0=100}
321 catchsql "UPDATE manycol SET x1=x1+1 $::where AND rowid>0"
323 do_test misc1-10.10 {
324 execsql {SELECT x1 FROM manycol WHERE x0=100}
327 # Make sure the initialization works even if a database is opened while
328 # another process has the database locked.
330 # Update for v3: The BEGIN doesn't lock the database so the schema is read
331 # and the SELECT returns successfully.
334 execsql {UPDATE t1 SET a=0 WHERE 0}
336 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
338 # v2 result: {1 {database is locked}}
342 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
347 # Make sure string comparisons really do compare strings in format4+.
348 # Similar tests in the format3.test file show that for format3 and earlier
349 # all comparisions where numeric if either operand looked like a number.
352 execsql {SELECT '0'=='0.0'}
355 execsql {SELECT '0'==0.0}
358 execsql {SELECT '12345678901234567890'=='12345678901234567891'}
362 CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
363 INSERT INTO t6 VALUES('0','0.0');
370 INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
376 INSERT OR IGNORE INTO t6 VALUES('y',0);
383 CREATE TABLE t7(x INTEGER, y TEXT, z);
384 INSERT INTO t7 VALUES(0,0,1);
385 INSERT INTO t7 VALUES(0.0,0,2);
386 INSERT INTO t7 VALUES(0,0.0,3);
387 INSERT INTO t7 VALUES(0.0,0.0,4);
388 SELECT DISTINCT x, y FROM t7 ORDER BY z;
393 SELECT min(z), max(z), count(z) FROM t7 GROUP BY x ORDER BY 1;
398 SELECT min(z), max(z), count(z) FROM t7 GROUP BY y ORDER BY 1;
402 # This used to be an error. But we changed the code so that arbitrary
403 # identifiers can be used as a collating sequence. Collation is by text
404 # if the identifier contains "text", "blob", or "clob" and is numeric
407 # Update: In v3, it is an error again.
409 #do_test misc1-12.10 {
411 # SELECT * FROM t6 ORDER BY a COLLATE unknown;
414 do_test misc1-12.11 {
416 CREATE TABLE t8(x TEXT COLLATE numeric, y INTEGER COLLATE text, z);
417 INSERT INTO t8 VALUES(0,0,1);
418 INSERT INTO t8 VALUES(0.0,0,2);
419 INSERT INTO t8 VALUES(0,0.0,3);
420 INSERT INTO t8 VALUES(0.0,0.0,4);
421 SELECT DISTINCT x, y FROM t8 ORDER BY z;
424 do_test misc1-12.12 {
426 SELECT min(z), max(z), count(z) FROM t8 GROUP BY x ORDER BY 1;
429 do_test misc1-12.13 {
431 SELECT min(z), max(z), count(z) FROM t8 GROUP BY y ORDER BY 1;
435 # There was a problem with realloc() in the OP_MemStore operation of
436 # the VDBE. A buffer was being reallocated but some pointers into
437 # the old copy of the buffer were not being moved over to the new copy.
438 # The following code tests for the problem.
443 CREATE TABLE t9(x,y);
444 INSERT INTO t9 VALUES('one',1);
445 INSERT INTO t9 VALUES('two',2);
446 INSERT INTO t9 VALUES('three',3);
447 INSERT INTO t9 VALUES('four',4);
448 INSERT INTO t9 VALUES('five',5);
449 INSERT INTO t9 VALUES('six',6);
450 INSERT INTO t9 VALUES('seven',7);
451 INSERT INTO t9 VALUES('eight',8);
452 INSERT INTO t9 VALUES('nine',9);
453 INSERT INTO t9 VALUES('ten',10);
454 INSERT INTO t9 VALUES('eleven',11);
456 WHERE x=(SELECT x FROM t9 WHERE y=1)
457 OR x=(SELECT x FROM t9 WHERE y=2)
458 OR x=(SELECT x FROM t9 WHERE y=3)
459 OR x=(SELECT x FROM t9 WHERE y=4)
460 OR x=(SELECT x FROM t9 WHERE y=5)
461 OR x=(SELECT x FROM t9 WHERE y=6)
462 OR x=(SELECT x FROM t9 WHERE y=7)
463 OR x=(SELECT x FROM t9 WHERE y=8)
464 OR x=(SELECT x FROM t9 WHERE y=9)
465 OR x=(SELECT x FROM t9 WHERE y=10)
466 OR x=(SELECT x FROM t9 WHERE y=11)
467 OR x=(SELECT x FROM t9 WHERE y=12)
468 OR x=(SELECT x FROM t9 WHERE y=13)
469 OR x=(SELECT x FROM t9 WHERE y=14)
472 } {1 2 3 4 5 6 7 8 9 10 11}
476 # The following tests can only work if the current SQLite VFS has the concept
477 # of a current directory.
480 # Make sure a database connection still works after changing the
487 file exists ./test.db-journal
489 do_test misc1-14.2a {
490 execsql {UPDATE t1 SET a=a||'x' WHERE 0}
491 file exists ../test.db-journal
493 do_test misc1-14.2b {
494 execsql {UPDATE t1 SET a=a||'y' WHERE 1}
495 file exists ../test.db-journal
501 file exists ./test.db-journal
505 # A failed create table should not leave the table in the internal
506 # data structures. Ticket #238.
508 do_test misc1-15.1.1 {
510 CREATE TABLE t10 AS SELECT c1;
512 } {1 {no such column: c1}}
513 do_test misc1-15.1.2 {
515 CREATE TABLE t10 AS SELECT t9.c1;
517 } {1 {no such column: t9.c1}}
518 do_test misc1-15.1.3 {
520 CREATE TABLE t10 AS SELECT main.t9.c1;
522 } {1 {no such column: main.t9.c1}}
525 CREATE TABLE t10 AS SELECT 1;
527 # The bug in ticket #238 causes the statement above to fail with
528 # the error "table t10 alread exists"
531 # Test for memory leaks when a CREATE TABLE containing a primary key
532 # fails. Ticket #249.
535 catchsql {SELECT name FROM sqlite_master LIMIT 1}
537 CREATE TABLE test(a integer, primary key(a));
542 CREATE TABLE test(a integer, primary key(a));
544 } {1 {table test already exists}}
547 CREATE TABLE test2(a text primary key, b text, primary key(a,b));
549 } {1 {table "test2" has more than one primary key}}
552 INSERT INTO test VALUES(1);
553 SELECT rowid, a FROM test;
558 INSERT INTO test VALUES(5);
559 SELECT rowid, a FROM test;
564 INSERT INTO test VALUES(NULL);
565 SELECT rowid, a FROM test;
569 ifcapable trigger&&tempdb {
570 # Ticket #333: Temp triggers that modify persistent tables.
575 CREATE TABLE RealTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
576 CREATE TEMP TABLE TempTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
577 CREATE TEMP TRIGGER trigTest_1 AFTER UPDATE ON TempTable BEGIN
578 INSERT INTO RealTable(TestString)
579 SELECT new.TestString FROM TempTable LIMIT 1;
581 INSERT INTO TempTable(TestString) VALUES ('1');
582 INSERT INTO TempTable(TestString) VALUES ('2');
583 UPDATE TempTable SET TestString = TestString + 1 WHERE TestID=1 OR TestId=2;
585 SELECT TestString FROM RealTable ORDER BY 1;
591 set n [sqlite3_sleep 100]
595 # 2014-01-10: In a CREATE TABLE AS, if one or more of the column names
596 # are an empty string, that is still OK.
598 do_execsql_test misc1-19.1 {
599 CREATE TABLE t19 AS SELECT 1, 2 AS '', 3;
602 do_execsql_test misc1-19.2 {
603 CREATE TABLE t19b AS SELECT 4 AS '', 5 AS '', 6 AS '';
607 # 2014-05-16: Tests for the SQLITE_TESTCTRL_FAULT_INSTALL feature.
609 unset -nocomplain fault_callbacks
610 set fault_callbacks {}
611 proc fault_callback {n} {
612 lappend ::fault_callbacks $n
616 sqlite3_test_control_fault_install fault_callback
620 sqlite3_test_control_fault_install