4 # The author disclaims copyright to this source code. In place of
5 # a legal notice, here is a blessing:
7 # May you do good and not evil.
8 # May you find forgiveness for yourself and forgive others.
9 # May you share freely, never taking more than you give.
11 #***********************************************************************
12 # This file implements regression tests for SQLite library.
14 # This file implements tests for miscellanous features that were
15 # left out of other test files.
17 # $Id: misc1.test,v 1.23 2003/08/05 13:13:39 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
22 # Test the creation and use of tables that have a large number
26 set cmd "CREATE TABLE manycol(x0 text"
27 for {set i 1} {$i<=99} {incr i} {
28 append cmd ",x$i text"
32 set cmd "INSERT INTO manycol VALUES(0"
33 for {set i 1} {$i<=99} {incr i} {
38 execsql "SELECT x99 FROM manycol"
41 execsql {SELECT x0, x10, x25, x50, x75 FROM manycol}
44 for {set j 100} {$j<=1000} {incr j 100} {
45 set cmd "INSERT INTO manycol VALUES($j"
46 for {set i 1} {$i<=99} {incr i} {
47 append cmd ",[expr {$i+$j}]"
52 execsql {SELECT x50 FROM manycol ORDER BY x80+0}
53 } {50 150 250 350 450 550 650 750 850 950 1050}
55 execsql {SELECT x50 FROM manycol ORDER BY x80}
56 } {1050 150 250 350 450 550 650 750 50 850 950}
58 execsql {SELECT x75 FROM manycol WHERE x50=350}
61 execsql {SELECT x50 FROM manycol WHERE x99=599}
64 execsql {CREATE INDEX manycol_idx1 ON manycol(x99)}
65 execsql {SELECT x50 FROM manycol WHERE x99=899}
68 execsql {SELECT count(*) FROM manycol}
71 execsql {DELETE FROM manycol WHERE x98=1234}
72 execsql {SELECT count(*) FROM manycol}
75 execsql {DELETE FROM manycol WHERE x98=998}
76 execsql {SELECT count(*) FROM manycol}
79 execsql {DELETE FROM manycol WHERE x99=500}
80 execsql {SELECT count(*) FROM manycol}
83 execsql {DELETE FROM manycol WHERE x99=599}
84 execsql {SELECT count(*) FROM manycol}
87 # Check GROUP BY expressions that name two or more columns.
92 CREATE TABLE agger(one text, two text, three text, four text);
93 INSERT INTO agger VALUES(1, 'one', 'hello', 'yes');
94 INSERT INTO agger VALUES(2, 'two', 'howdy', 'no');
95 INSERT INTO agger VALUES(3, 'thr', 'howareya', 'yes');
96 INSERT INTO agger VALUES(4, 'two', 'lothere', 'yes');
97 INSERT INTO agger VALUES(5, 'one', 'atcha', 'yes');
98 INSERT INTO agger VALUES(6, 'two', 'hello', 'no');
101 execsql {SELECT count(*) FROM agger}
104 execsql {SELECT sum(one), two, four FROM agger
105 GROUP BY two, four ORDER BY sum(one) desc}
106 } {8 two no 6 one yes 4 two yes 3 thr yes}
108 execsql {SELECT sum((one)), (two), (four) FROM agger
109 GROUP BY (two), (four) ORDER BY sum(one) desc}
110 } {8 two no 6 one yes 4 two yes 3 thr yes}
112 # Here's a test for a bug found by Joel Lucsy. The code below
113 # was causing an assertion failure.
118 INSERT INTO t1 VALUES('hi');
119 PRAGMA full_column_names=on;
120 SELECT rowid, * FROM t1;
125 # Here's a test for yet another bug found by Joel Lucsy. The code
126 # below was causing an assertion failure.
132 INSERT INTO t2 VALUES('This is a long string to use up a lot of disk -');
133 UPDATE t2 SET a=a||a||a||a;
134 INSERT INTO t2 SELECT '1 - ' || a FROM t2;
135 INSERT INTO t2 SELECT '2 - ' || a FROM t2;
136 INSERT INTO t2 SELECT '3 - ' || a FROM t2;
137 INSERT INTO t2 SELECT '4 - ' || a FROM t2;
138 INSERT INTO t2 SELECT '5 - ' || a FROM t2;
139 INSERT INTO t2 SELECT '6 - ' || a FROM t2;
141 SELECT count(*) FROM t2;
145 # Make sure we actually see a semicolon or end-of-file in the SQL input
146 # before executing a command. Thus if "WHERE" is misspelled on an UPDATE,
147 # the user won't accidently update every record.
151 CREATE TABLE t3(a,b);
152 INSERT INTO t3 VALUES(1,2);
153 INSERT INTO t3 VALUES(3,4);
154 UPDATE t3 SET a=0 WHEREwww b=2;
156 } {1 {near "WHEREwww": syntax error}}
159 SELECT * FROM t3 ORDER BY a;
163 # Certain keywords (especially non-standard keywords like "REPLACE") can
164 # also be used as identifiers. The way this works in the parser is that
165 # the parser first detects a syntax error, the error handling routine
166 # sees that the special keyword caused the error, then replaces the keyword
167 # with "ID" and tries again.
169 # Check the operation of this logic.
174 abort, asc, begin, cluster, conflict, copy, delimiters, desc, end,
175 explain, fail, ignore, key, offset, pragma, replace, temp,
183 VALUES(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
190 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
193 SELECT abort+asc,max(key,pragma,temp) FROM t4
197 # Test for multi-column primary keys, and for multiple primary keys.
206 } {1 {table "error1" has more than one primary key}}
210 a INTEGER PRIMARY KEY,
214 } {1 {table "error1" has more than one primary key}}
217 CREATE TABLE t5(a,b,c,PRIMARY KEY(a,b));
218 INSERT INTO t5 VALUES(1,2,3);
219 SELECT * FROM t5 ORDER BY a;
224 INSERT INTO t5 VALUES(1,2,4);
226 } {1 {columns a, b are not unique}}
229 INSERT INTO t5 VALUES(0,2,4);
234 SELECT * FROM t5 ORDER BY a;
242 } {1 {no tables specified}}
247 } {1 {no such table: t1}}
256 # If an integer is too big to be represented as a 32-bit machine integer,
257 # then treat it as a string.
261 CREATE TABLE t1(a unique not null, b unique not null);
262 INSERT INTO t1 VALUES('a',12345678901234567890);
263 INSERT INTO t1 VALUES('b',12345678911234567890);
264 INSERT INTO t1 VALUES('c',12345678921234567890);
267 } {0 {a 12345678901234567890 b 12345678911234567890 c 12345678921234567890}}
269 # A WHERE clause is not allowed to contain more than 99 terms. Check to
270 # make sure this limit is enforced.
273 execsql {SELECT count(*) FROM manycol}
276 set ::where {WHERE x0>=0}
277 for {set i 1} {$i<=99} {incr i} {
278 append ::where " AND x$i<>0"
280 catchsql "SELECT count(*) FROM manycol $::where"
283 catchsql "SELECT count(*) FROM manycol $::where AND rowid>0"
284 } {1 {WHERE clause too complex - no more than 100 terms allowed}}
286 regsub "x0>=0" $::where "x0=0" ::where
287 catchsql "DELETE FROM manycol $::where"
290 execsql {SELECT count(*) FROM manycol}
293 catchsql "DELETE FROM manycol $::where AND rowid>0"
294 } {1 {WHERE clause too complex - no more than 100 terms allowed}}
296 execsql {SELECT x1 FROM manycol WHERE x0=100}
299 regsub "x0=0" $::where "x0=100" ::where
300 catchsql "UPDATE manycol SET x1=x1+1 $::where"
303 execsql {SELECT x1 FROM manycol WHERE x0=100}
306 catchsql "UPDATE manycol SET x1=x1+1 $::where AND rowid>0"
307 } {1 {WHERE clause too complex - no more than 100 terms allowed}}
308 do_test misc1-10.10 {
309 execsql {SELECT x1 FROM manycol WHERE x0=100}
312 # Make sure the initialization works even if a database is opened while
313 # another process has the database locked.
318 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
320 } {1 {database is locked}}
323 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
328 # Make sure string comparisons really do compare strings in format4+.
329 # Similar tests in the format3.test file show that for format3 and earlier
330 # all comparisions where numeric if either operand looked like a number.
333 execsql {SELECT '0'=='0.0'}
336 execsql {SELECT '0'==0.0}
339 execsql {SELECT '12345678901234567890'=='12345678901234567891'}
343 CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
344 INSERT INTO t6 VALUES('0','0.0');
350 INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
356 INSERT OR IGNORE INTO t6 VALUES('y',0);
362 CREATE TABLE t7(x INTEGER, y TEXT, z);
363 INSERT INTO t7 VALUES(0,0,1);
364 INSERT INTO t7 VALUES(0.0,0,2);
365 INSERT INTO t7 VALUES(0,0.0,3);
366 INSERT INTO t7 VALUES(0.0,0.0,4);
367 SELECT DISTINCT x, y FROM t7 ORDER BY z;
372 SELECT min(z), max(z), count(z) FROM t7 GROUP BY x ORDER BY 1;
377 SELECT min(z), max(z), count(z) FROM t7 GROUP BY y ORDER BY 1;
381 # This used to be an error. But we changed the code so that arbitrary
382 # identifiers can be used as a collating sequence. Collation is by text
383 # if the identifier contains "text", "blob", or "clob" and is numeric
385 do_test misc1-12.10 {
387 SELECT * FROM t6 ORDER BY a COLLATE unknown;
390 do_test misc1-12.11 {
392 CREATE TABLE t8(x TEXT COLLATE numeric, y INTEGER COLLATE text, z);
393 INSERT INTO t8 VALUES(0,0,1);
394 INSERT INTO t8 VALUES(0.0,0,2);
395 INSERT INTO t8 VALUES(0,0.0,3);
396 INSERT INTO t8 VALUES(0.0,0.0,4);
397 SELECT DISTINCT x, y FROM t8 ORDER BY z;
400 do_test misc1-12.12 {
402 SELECT min(z), max(z), count(z) FROM t8 GROUP BY x ORDER BY 1;
405 do_test misc1-12.13 {
407 SELECT min(z), max(z), count(z) FROM t8 GROUP BY y ORDER BY 1;
411 # There was a problem with realloc() in the OP_MemStore operation of
412 # the VDBE. A buffer was being reallocated but some pointers into
413 # the old copy of the buffer were not being moved over to the new copy.
414 # The following code tests for the problem.
418 CREATE TABLE t9(x,y);
419 INSERT INTO t9 VALUES('one',1);
420 INSERT INTO t9 VALUES('two',2);
421 INSERT INTO t9 VALUES('three',3);
422 INSERT INTO t9 VALUES('four',4);
423 INSERT INTO t9 VALUES('five',5);
424 INSERT INTO t9 VALUES('six',6);
425 INSERT INTO t9 VALUES('seven',7);
426 INSERT INTO t9 VALUES('eight',8);
427 INSERT INTO t9 VALUES('nine',9);
428 INSERT INTO t9 VALUES('ten',10);
429 INSERT INTO t9 VALUES('eleven',11);
431 WHERE x=(SELECT x FROM t9 WHERE y=1)
432 OR x=(SELECT x FROM t9 WHERE y=2)
433 OR x=(SELECT x FROM t9 WHERE y=3)
434 OR x=(SELECT x FROM t9 WHERE y=4)
435 OR x=(SELECT x FROM t9 WHERE y=5)
436 OR x=(SELECT x FROM t9 WHERE y=6)
437 OR x=(SELECT x FROM t9 WHERE y=7)
438 OR x=(SELECT x FROM t9 WHERE y=8)
439 OR x=(SELECT x FROM t9 WHERE y=9)
440 OR x=(SELECT x FROM t9 WHERE y=10)
441 OR x=(SELECT x FROM t9 WHERE y=11)
442 OR x=(SELECT x FROM t9 WHERE y=12)
443 OR x=(SELECT x FROM t9 WHERE y=13)
444 OR x=(SELECT x FROM t9 WHERE y=14)
447 } {1 2 3 4 5 6 7 8 9 10 11}
449 # Make sure a database connection still works after changing the
456 file exists ./test.db-journal
459 file exists ../test.db-journal
465 file exists ./test.db-journal
468 # A failed create table should not leave the table in the internal
469 # data structures. Ticket #238.
473 CREATE TABLE t10 AS SELECT c1;
475 } {1 {no such column: c1}}
478 CREATE TABLE t10 AS SELECT 1;
480 # The bug in ticket #238 causes the statement above to fail with
481 # the error "table t10 alread exists"
484 # Test for memory leaks when a CREATE TABLE containing a primary key
485 # fails. Ticket #249.
488 catchsql {SELECT name FROM sqlite_master LIMIT 1}
490 CREATE TABLE test(a integer, primary key(a));
495 CREATE TABLE test(a integer, primary key(a));
497 } {1 {table test already exists}}
500 CREATE TABLE test2(a text primary key, b text, primary key(a,b));
502 } {1 {table "test2" has more than one primary key}}
505 INSERT INTO test VALUES(1);
506 SELECT rowid, a FROM test;
511 INSERT INTO test VALUES(5);
512 SELECT rowid, a FROM test;
517 INSERT INTO test VALUES(NULL);
518 SELECT rowid, a FROM test;
522 # Ticket #333: Temp triggers that modify persistent tables.
527 CREATE TABLE RealTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
528 CREATE TEMP TABLE TempTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
529 CREATE TEMP TRIGGER trigTest_1 AFTER UPDATE ON TempTable BEGIN
530 INSERT INTO RealTable(TestString)
531 SELECT new.TestString FROM TempTable LIMIT 1;
533 INSERT INTO TempTable(TestString) VALUES ('1');
534 INSERT INTO TempTable(TestString) VALUES ('2');
535 UPDATE TempTable SET TestString = TestString + 1 WHERE TestID IN (1, 2);
537 SELECT TestString FROM RealTable ORDER BY 1;