2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library.
16 # This file implements tests for miscellanous features that were
17 # left out of other test files.
19 # $Id: misc1.test,v 1.23 2003/08/05 13:13:39 drh Exp $
21 set testdir [file dirname $argv0]
22 source $testdir/tester.tcl
24 # Test the creation and use of tables that have a large number
28 set cmd "CREATE TABLE manycol(x0 text"
29 for {set i 1} {$i<=99} {incr i} {
30 append cmd ",x$i text"
34 set cmd "INSERT INTO manycol VALUES(0"
35 for {set i 1} {$i<=99} {incr i} {
40 execsql "SELECT x99 FROM manycol"
43 execsql {SELECT x0, x10, x25, x50, x75 FROM manycol}
46 for {set j 100} {$j<=1000} {incr j 100} {
47 set cmd "INSERT INTO manycol VALUES($j"
48 for {set i 1} {$i<=99} {incr i} {
49 append cmd ",[expr {$i+$j}]"
54 execsql {SELECT x50 FROM manycol ORDER BY x80+0}
55 } {50 150 250 350 450 550 650 750 850 950 1050}
57 execsql {SELECT x50 FROM manycol ORDER BY x80}
58 } {1050 150 250 350 450 550 650 750 50 850 950}
60 execsql {SELECT x75 FROM manycol WHERE x50=350}
63 execsql {SELECT x50 FROM manycol WHERE x99=599}
66 execsql {CREATE INDEX manycol_idx1 ON manycol(x99)}
67 execsql {SELECT x50 FROM manycol WHERE x99=899}
70 execsql {SELECT count(*) FROM manycol}
73 execsql {DELETE FROM manycol WHERE x98=1234}
74 execsql {SELECT count(*) FROM manycol}
77 execsql {DELETE FROM manycol WHERE x98=998}
78 execsql {SELECT count(*) FROM manycol}
81 execsql {DELETE FROM manycol WHERE x99=500}
82 execsql {SELECT count(*) FROM manycol}
85 execsql {DELETE FROM manycol WHERE x99=599}
86 execsql {SELECT count(*) FROM manycol}
89 # Check GROUP BY expressions that name two or more columns.
94 CREATE TABLE agger(one text, two text, three text, four text);
95 INSERT INTO agger VALUES(1, 'one', 'hello', 'yes');
96 INSERT INTO agger VALUES(2, 'two', 'howdy', 'no');
97 INSERT INTO agger VALUES(3, 'thr', 'howareya', 'yes');
98 INSERT INTO agger VALUES(4, 'two', 'lothere', 'yes');
99 INSERT INTO agger VALUES(5, 'one', 'atcha', 'yes');
100 INSERT INTO agger VALUES(6, 'two', 'hello', 'no');
103 execsql {SELECT count(*) FROM agger}
106 execsql {SELECT sum(one), two, four FROM agger
107 GROUP BY two, four ORDER BY sum(one) desc}
108 } {8 two no 6 one yes 4 two yes 3 thr yes}
110 execsql {SELECT sum((one)), (two), (four) FROM agger
111 GROUP BY (two), (four) ORDER BY sum(one) desc}
112 } {8 two no 6 one yes 4 two yes 3 thr yes}
114 # Here's a test for a bug found by Joel Lucsy. The code below
115 # was causing an assertion failure.
120 INSERT INTO t1 VALUES('hi');
121 PRAGMA full_column_names=on;
122 SELECT rowid, * FROM t1;
127 # Here's a test for yet another bug found by Joel Lucsy. The code
128 # below was causing an assertion failure.
134 INSERT INTO t2 VALUES('This is a long string to use up a lot of disk -');
135 UPDATE t2 SET a=a||a||a||a;
136 INSERT INTO t2 SELECT '1 - ' || a FROM t2;
137 INSERT INTO t2 SELECT '2 - ' || a FROM t2;
138 INSERT INTO t2 SELECT '3 - ' || a FROM t2;
139 INSERT INTO t2 SELECT '4 - ' || a FROM t2;
140 INSERT INTO t2 SELECT '5 - ' || a FROM t2;
141 INSERT INTO t2 SELECT '6 - ' || a FROM t2;
143 SELECT count(*) FROM t2;
147 # Make sure we actually see a semicolon or end-of-file in the SQL input
148 # before executing a command. Thus if "WHERE" is misspelled on an UPDATE,
149 # the user won't accidently update every record.
153 CREATE TABLE t3(a,b);
154 INSERT INTO t3 VALUES(1,2);
155 INSERT INTO t3 VALUES(3,4);
156 UPDATE t3 SET a=0 WHEREwww b=2;
158 } {1 {near "WHEREwww": syntax error}}
161 SELECT * FROM t3 ORDER BY a;
165 # Certain keywords (especially non-standard keywords like "REPLACE") can
166 # also be used as identifiers. The way this works in the parser is that
167 # the parser first detects a syntax error, the error handling routine
168 # sees that the special keyword caused the error, then replaces the keyword
169 # with "ID" and tries again.
171 # Check the operation of this logic.
176 abort, asc, begin, cluster, conflict, copy, delimiters, desc, end,
177 explain, fail, ignore, key, offset, pragma, replace, temp,
185 VALUES(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
192 } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
195 SELECT abort+asc,max(key,pragma,temp) FROM t4
199 # Test for multi-column primary keys, and for multiple primary keys.
208 } {1 {table "error1" has more than one primary key}}
212 a INTEGER PRIMARY KEY,
216 } {1 {table "error1" has more than one primary key}}
219 CREATE TABLE t5(a,b,c,PRIMARY KEY(a,b));
220 INSERT INTO t5 VALUES(1,2,3);
221 SELECT * FROM t5 ORDER BY a;
226 INSERT INTO t5 VALUES(1,2,4);
228 } {1 {columns a, b are not unique}}
231 INSERT INTO t5 VALUES(0,2,4);
236 SELECT * FROM t5 ORDER BY a;
244 } {1 {no tables specified}}
249 } {1 {no such table: t1}}
258 # If an integer is too big to be represented as a 32-bit machine integer,
259 # then treat it as a string.
263 CREATE TABLE t1(a unique not null, b unique not null);
264 INSERT INTO t1 VALUES('a',12345678901234567890);
265 INSERT INTO t1 VALUES('b',12345678911234567890);
266 INSERT INTO t1 VALUES('c',12345678921234567890);
269 } {0 {a 12345678901234567890 b 12345678911234567890 c 12345678921234567890}}
271 # A WHERE clause is not allowed to contain more than 99 terms. Check to
272 # make sure this limit is enforced.
275 execsql {SELECT count(*) FROM manycol}
278 set ::where {WHERE x0>=0}
279 for {set i 1} {$i<=99} {incr i} {
280 append ::where " AND x$i<>0"
282 catchsql "SELECT count(*) FROM manycol $::where"
285 catchsql "SELECT count(*) FROM manycol $::where AND rowid>0"
286 } {1 {WHERE clause too complex - no more than 100 terms allowed}}
288 regsub "x0>=0" $::where "x0=0" ::where
289 catchsql "DELETE FROM manycol $::where"
292 execsql {SELECT count(*) FROM manycol}
295 catchsql "DELETE FROM manycol $::where AND rowid>0"
296 } {1 {WHERE clause too complex - no more than 100 terms allowed}}
298 execsql {SELECT x1 FROM manycol WHERE x0=100}
301 regsub "x0=0" $::where "x0=100" ::where
302 catchsql "UPDATE manycol SET x1=x1+1 $::where"
305 execsql {SELECT x1 FROM manycol WHERE x0=100}
308 catchsql "UPDATE manycol SET x1=x1+1 $::where AND rowid>0"
309 } {1 {WHERE clause too complex - no more than 100 terms allowed}}
310 do_test misc1-10.10 {
311 execsql {SELECT x1 FROM manycol WHERE x0=100}
314 # Make sure the initialization works even if a database is opened while
315 # another process has the database locked.
320 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
322 } {1 {database is locked}}
325 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
330 # Make sure string comparisons really do compare strings in format4+.
331 # Similar tests in the format3.test file show that for format3 and earlier
332 # all comparisions where numeric if either operand looked like a number.
335 execsql {SELECT '0'=='0.0'}
338 execsql {SELECT '0'==0.0}
341 execsql {SELECT '12345678901234567890'=='12345678901234567891'}
345 CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
346 INSERT INTO t6 VALUES('0','0.0');
352 INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
358 INSERT OR IGNORE INTO t6 VALUES('y',0);
364 CREATE TABLE t7(x INTEGER, y TEXT, z);
365 INSERT INTO t7 VALUES(0,0,1);
366 INSERT INTO t7 VALUES(0.0,0,2);
367 INSERT INTO t7 VALUES(0,0.0,3);
368 INSERT INTO t7 VALUES(0.0,0.0,4);
369 SELECT DISTINCT x, y FROM t7 ORDER BY z;
374 SELECT min(z), max(z), count(z) FROM t7 GROUP BY x ORDER BY 1;
379 SELECT min(z), max(z), count(z) FROM t7 GROUP BY y ORDER BY 1;
383 # This used to be an error. But we changed the code so that arbitrary
384 # identifiers can be used as a collating sequence. Collation is by text
385 # if the identifier contains "text", "blob", or "clob" and is numeric
387 do_test misc1-12.10 {
389 SELECT * FROM t6 ORDER BY a COLLATE unknown;
392 do_test misc1-12.11 {
394 CREATE TABLE t8(x TEXT COLLATE numeric, y INTEGER COLLATE text, z);
395 INSERT INTO t8 VALUES(0,0,1);
396 INSERT INTO t8 VALUES(0.0,0,2);
397 INSERT INTO t8 VALUES(0,0.0,3);
398 INSERT INTO t8 VALUES(0.0,0.0,4);
399 SELECT DISTINCT x, y FROM t8 ORDER BY z;
402 do_test misc1-12.12 {
404 SELECT min(z), max(z), count(z) FROM t8 GROUP BY x ORDER BY 1;
407 do_test misc1-12.13 {
409 SELECT min(z), max(z), count(z) FROM t8 GROUP BY y ORDER BY 1;
413 # There was a problem with realloc() in the OP_MemStore operation of
414 # the VDBE. A buffer was being reallocated but some pointers into
415 # the old copy of the buffer were not being moved over to the new copy.
416 # The following code tests for the problem.
420 CREATE TABLE t9(x,y);
421 INSERT INTO t9 VALUES('one',1);
422 INSERT INTO t9 VALUES('two',2);
423 INSERT INTO t9 VALUES('three',3);
424 INSERT INTO t9 VALUES('four',4);
425 INSERT INTO t9 VALUES('five',5);
426 INSERT INTO t9 VALUES('six',6);
427 INSERT INTO t9 VALUES('seven',7);
428 INSERT INTO t9 VALUES('eight',8);
429 INSERT INTO t9 VALUES('nine',9);
430 INSERT INTO t9 VALUES('ten',10);
431 INSERT INTO t9 VALUES('eleven',11);
433 WHERE x=(SELECT x FROM t9 WHERE y=1)
434 OR x=(SELECT x FROM t9 WHERE y=2)
435 OR x=(SELECT x FROM t9 WHERE y=3)
436 OR x=(SELECT x FROM t9 WHERE y=4)
437 OR x=(SELECT x FROM t9 WHERE y=5)
438 OR x=(SELECT x FROM t9 WHERE y=6)
439 OR x=(SELECT x FROM t9 WHERE y=7)
440 OR x=(SELECT x FROM t9 WHERE y=8)
441 OR x=(SELECT x FROM t9 WHERE y=9)
442 OR x=(SELECT x FROM t9 WHERE y=10)
443 OR x=(SELECT x FROM t9 WHERE y=11)
444 OR x=(SELECT x FROM t9 WHERE y=12)
445 OR x=(SELECT x FROM t9 WHERE y=13)
446 OR x=(SELECT x FROM t9 WHERE y=14)
449 } {1 2 3 4 5 6 7 8 9 10 11}
451 # Make sure a database connection still works after changing the
458 file exists ./test.db-journal
461 file exists ../test.db-journal
467 file exists ./test.db-journal
470 # A failed create table should not leave the table in the internal
471 # data structures. Ticket #238.
475 CREATE TABLE t10 AS SELECT c1;
477 } {1 {no such column: c1}}
480 CREATE TABLE t10 AS SELECT 1;
482 # The bug in ticket #238 causes the statement above to fail with
483 # the error "table t10 alread exists"
486 # Test for memory leaks when a CREATE TABLE containing a primary key
487 # fails. Ticket #249.
490 catchsql {SELECT name FROM sqlite_master LIMIT 1}
492 CREATE TABLE test(a integer, primary key(a));
497 CREATE TABLE test(a integer, primary key(a));
499 } {1 {table test already exists}}
502 CREATE TABLE test2(a text primary key, b text, primary key(a,b));
504 } {1 {table "test2" has more than one primary key}}
507 INSERT INTO test VALUES(1);
508 SELECT rowid, a FROM test;
513 INSERT INTO test VALUES(5);
514 SELECT rowid, a FROM test;
519 INSERT INTO test VALUES(NULL);
520 SELECT rowid, a FROM test;
524 # Ticket #333: Temp triggers that modify persistent tables.
529 CREATE TABLE RealTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
530 CREATE TEMP TABLE TempTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
531 CREATE TEMP TRIGGER trigTest_1 AFTER UPDATE ON TempTable BEGIN
532 INSERT INTO RealTable(TestString)
533 SELECT new.TestString FROM TempTable LIMIT 1;
535 INSERT INTO TempTable(TestString) VALUES ('1');
536 INSERT INTO TempTable(TestString) VALUES ('2');
537 UPDATE TempTable SET TestString = TestString + 1 WHERE TestID IN (1, 2);
539 SELECT TestString FROM RealTable ORDER BY 1;