blob: e646bfd09804ca19aab9e81cb457f36c7d5f97a7 [file] [log] [blame]
danielk19770202b292004-06-09 09:55:16 +00001# 2001 September 15.
drhdb5ed6d2001-09-18 22:17:44 +00002#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library.
12#
13# This file implements tests for miscellanous features that were
14# left out of other test files.
15#
drhdb5ed6d2001-09-18 22:17:44 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
danielk19770202b292004-06-09 09:55:16 +000020# Mimic the SQLite 2 collation type NUMERIC.
21db collate numeric numeric_collate
22proc numeric_collate {lhs rhs} {
23 if {$lhs == $rhs} {return 0}
24 return [expr ($lhs>$rhs)?1:-1]
25}
26
27# Mimic the SQLite 2 collation type TEXT.
28db collate text text_collate
29proc numeric_collate {lhs rhs} {
30 return [string compare $lhs $rhs]
31}
32
drhdb5ed6d2001-09-18 22:17:44 +000033# Test the creation and use of tables that have a large number
34# of columns.
35#
36do_test misc1-1.1 {
37 set cmd "CREATE TABLE manycol(x0 text"
38 for {set i 1} {$i<=99} {incr i} {
39 append cmd ",x$i text"
40 }
41 append cmd ")";
42 execsql $cmd
43 set cmd "INSERT INTO manycol VALUES(0"
44 for {set i 1} {$i<=99} {incr i} {
45 append cmd ",$i"
46 }
47 append cmd ")";
48 execsql $cmd
49 execsql "SELECT x99 FROM manycol"
50} 99
51do_test misc1-1.2 {
52 execsql {SELECT x0, x10, x25, x50, x75 FROM manycol}
53} {0 10 25 50 75}
drha9e99ae2002-08-13 23:02:57 +000054do_test misc1-1.3.1 {
drhdb5ed6d2001-09-18 22:17:44 +000055 for {set j 100} {$j<=1000} {incr j 100} {
56 set cmd "INSERT INTO manycol VALUES($j"
57 for {set i 1} {$i<=99} {incr i} {
58 append cmd ",[expr {$i+$j}]"
59 }
60 append cmd ")"
61 execsql $cmd
62 }
drha9e99ae2002-08-13 23:02:57 +000063 execsql {SELECT x50 FROM manycol ORDER BY x80+0}
drhdb5ed6d2001-09-18 22:17:44 +000064} {50 150 250 350 450 550 650 750 850 950 1050}
drha9e99ae2002-08-13 23:02:57 +000065do_test misc1-1.3.2 {
66 execsql {SELECT x50 FROM manycol ORDER BY x80}
67} {1050 150 250 350 450 550 650 750 50 850 950}
drhdb5ed6d2001-09-18 22:17:44 +000068do_test misc1-1.4 {
69 execsql {SELECT x75 FROM manycol WHERE x50=350}
70} 375
71do_test misc1-1.5 {
72 execsql {SELECT x50 FROM manycol WHERE x99=599}
73} 550
74do_test misc1-1.6 {
75 execsql {CREATE INDEX manycol_idx1 ON manycol(x99)}
76 execsql {SELECT x50 FROM manycol WHERE x99=899}
77} 850
78do_test misc1-1.7 {
79 execsql {SELECT count(*) FROM manycol}
80} 11
81do_test misc1-1.8 {
82 execsql {DELETE FROM manycol WHERE x98=1234}
83 execsql {SELECT count(*) FROM manycol}
84} 11
85do_test misc1-1.9 {
86 execsql {DELETE FROM manycol WHERE x98=998}
87 execsql {SELECT count(*) FROM manycol}
88} 10
89do_test misc1-1.10 {
90 execsql {DELETE FROM manycol WHERE x99=500}
91 execsql {SELECT count(*) FROM manycol}
92} 10
93do_test misc1-1.11 {
94 execsql {DELETE FROM manycol WHERE x99=599}
95 execsql {SELECT count(*) FROM manycol}
96} 9
97
98# Check GROUP BY expressions that name two or more columns.
99#
100do_test misc1-2.1 {
101 execsql {
102 BEGIN TRANSACTION;
103 CREATE TABLE agger(one text, two text, three text, four text);
104 INSERT INTO agger VALUES(1, 'one', 'hello', 'yes');
105 INSERT INTO agger VALUES(2, 'two', 'howdy', 'no');
106 INSERT INTO agger VALUES(3, 'thr', 'howareya', 'yes');
107 INSERT INTO agger VALUES(4, 'two', 'lothere', 'yes');
108 INSERT INTO agger VALUES(5, 'one', 'atcha', 'yes');
109 INSERT INTO agger VALUES(6, 'two', 'hello', 'no');
110 COMMIT
111 }
112 execsql {SELECT count(*) FROM agger}
113} 6
114do_test misc1-2.2 {
115 execsql {SELECT sum(one), two, four FROM agger
116 GROUP BY two, four ORDER BY sum(one) desc}
drh3d1d95e2005-09-08 10:37:01 +0000117} {8 two no 6 one yes 4 two yes 3 thr yes}
drh6977fea2002-10-22 23:38:04 +0000118do_test misc1-2.3 {
119 execsql {SELECT sum((one)), (two), (four) FROM agger
120 GROUP BY (two), (four) ORDER BY sum(one) desc}
drh3d1d95e2005-09-08 10:37:01 +0000121} {8 two no 6 one yes 4 two yes 3 thr yes}
drhdb5ed6d2001-09-18 22:17:44 +0000122
drh97665872002-02-13 23:22:53 +0000123# Here's a test for a bug found by Joel Lucsy. The code below
124# was causing an assertion failure.
125#
126do_test misc1-3.1 {
127 set r [execsql {
128 CREATE TABLE t1(a);
129 INSERT INTO t1 VALUES('hi');
130 PRAGMA full_column_names=on;
131 SELECT rowid, * FROM t1;
132 }]
133 lindex $r 1
134} {hi}
135
drh1e336b42002-02-14 12:50:33 +0000136# Here's a test for yet another bug found by Joel Lucsy. The code
137# below was causing an assertion failure.
138#
139do_test misc1-4.1 {
140 execsql {
141 BEGIN;
142 CREATE TABLE t2(a);
143 INSERT INTO t2 VALUES('This is a long string to use up a lot of disk -');
144 UPDATE t2 SET a=a||a||a||a;
145 INSERT INTO t2 SELECT '1 - ' || a FROM t2;
146 INSERT INTO t2 SELECT '2 - ' || a FROM t2;
147 INSERT INTO t2 SELECT '3 - ' || a FROM t2;
148 INSERT INTO t2 SELECT '4 - ' || a FROM t2;
149 INSERT INTO t2 SELECT '5 - ' || a FROM t2;
150 INSERT INTO t2 SELECT '6 - ' || a FROM t2;
151 COMMIT;
152 SELECT count(*) FROM t2;
153 }
154} {64}
155
drh094b2bb2002-03-13 18:54:07 +0000156# Make sure we actually see a semicolon or end-of-file in the SQL input
157# before executing a command. Thus if "WHERE" is misspelled on an UPDATE,
158# the user won't accidently update every record.
159#
160do_test misc1-5.1 {
161 catchsql {
162 CREATE TABLE t3(a,b);
163 INSERT INTO t3 VALUES(1,2);
164 INSERT INTO t3 VALUES(3,4);
165 UPDATE t3 SET a=0 WHEREwww b=2;
166 }
167} {1 {near "WHEREwww": syntax error}}
168do_test misc1-5.2 {
169 execsql {
170 SELECT * FROM t3 ORDER BY a;
171 }
172} {1 2 3 4}
173
drhf18543c2002-03-30 15:26:50 +0000174# Certain keywords (especially non-standard keywords like "REPLACE") can
175# also be used as identifiers. The way this works in the parser is that
176# the parser first detects a syntax error, the error handling routine
177# sees that the special keyword caused the error, then replaces the keyword
178# with "ID" and tries again.
179#
180# Check the operation of this logic.
181#
182do_test misc1-6.1 {
183 catchsql {
184 CREATE TABLE t4(
185 abort, asc, begin, cluster, conflict, copy, delimiters, desc, end,
186 explain, fail, ignore, key, offset, pragma, replace, temp,
187 vacuum, view
188 );
189 }
190} {0 {}}
191do_test misc1-6.2 {
192 catchsql {
193 INSERT INTO t4
194 VALUES(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
195 }
196} {0 {}}
197do_test misc1-6.3 {
198 execsql {
199 SELECT * FROM t4
200 }
201} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
202do_test misc1-6.4 {
203 execsql {
204 SELECT abort+asc,max(key,pragma,temp) FROM t4
205 }
206} {3 17}
drh2e392e22002-05-24 02:14:50 +0000207
208# Test for multi-column primary keys, and for multiple primary keys.
209#
210do_test misc1-7.1 {
211 catchsql {
212 CREATE TABLE error1(
213 a TYPE PRIMARY KEY,
214 b TYPE PRIMARY KEY
215 );
216 }
217} {1 {table "error1" has more than one primary key}}
218do_test misc1-7.2 {
219 catchsql {
220 CREATE TABLE error1(
221 a INTEGER PRIMARY KEY,
222 b TYPE PRIMARY KEY
223 );
224 }
225} {1 {table "error1" has more than one primary key}}
226do_test misc1-7.3 {
227 execsql {
228 CREATE TABLE t5(a,b,c,PRIMARY KEY(a,b));
229 INSERT INTO t5 VALUES(1,2,3);
230 SELECT * FROM t5 ORDER BY a;
231 }
232} {1 2 3}
233do_test misc1-7.4 {
234 catchsql {
235 INSERT INTO t5 VALUES(1,2,4);
236 }
drhf9c8ce32013-11-05 13:33:55 +0000237} {1 {UNIQUE constraint failed: t5.a, t5.b}}
drh2e392e22002-05-24 02:14:50 +0000238do_test misc1-7.5 {
239 catchsql {
240 INSERT INTO t5 VALUES(0,2,4);
241 }
242} {0 {}}
243do_test misc1-7.6 {
244 execsql {
245 SELECT * FROM t5 ORDER BY a;
246 }
247} {0 2 4 1 2 3}
248
drhf5db2d32002-06-06 23:42:27 +0000249do_test misc1-8.1 {
250 catchsql {
251 SELECT *;
252 }
253} {1 {no tables specified}}
254do_test misc1-8.2 {
255 catchsql {
256 SELECT t1.*;
257 }
258} {1 {no such table: t1}}
259
drhd9e30932002-06-09 01:16:01 +0000260execsql {
261 DROP TABLE t1;
262 DROP TABLE t2;
263 DROP TABLE t3;
264 DROP TABLE t4;
265}
266
drhfec19aa2004-05-19 20:41:03 +0000267# 64-bit integers are represented exactly.
drhd9e30932002-06-09 01:16:01 +0000268#
269do_test misc1-9.1 {
270 catchsql {
271 CREATE TABLE t1(a unique not null, b unique not null);
drhfec19aa2004-05-19 20:41:03 +0000272 INSERT INTO t1 VALUES('a',1234567890123456789);
273 INSERT INTO t1 VALUES('b',1234567891123456789);
274 INSERT INTO t1 VALUES('c',1234567892123456789);
drhd9e30932002-06-09 01:16:01 +0000275 SELECT * FROM t1;
276 }
drhfec19aa2004-05-19 20:41:03 +0000277} {0 {a 1234567890123456789 b 1234567891123456789 c 1234567892123456789}}
drhd9e30932002-06-09 01:16:01 +0000278
drh83dcb1a2002-06-28 01:02:38 +0000279# A WHERE clause is not allowed to contain more than 99 terms. Check to
280# make sure this limit is enforced.
281#
drh0aa74ed2005-07-16 13:33:20 +0000282# 2005-07-16: There is no longer a limit on the number of terms in a
283# WHERE clause. But keep these tests just so that we have some tests
284# that use a large number of terms in the WHERE clause.
285#
drh83dcb1a2002-06-28 01:02:38 +0000286do_test misc1-10.0 {
287 execsql {SELECT count(*) FROM manycol}
288} {9}
289do_test misc1-10.1 {
290 set ::where {WHERE x0>=0}
291 for {set i 1} {$i<=99} {incr i} {
292 append ::where " AND x$i<>0"
293 }
294 catchsql "SELECT count(*) FROM manycol $::where"
295} {0 9}
296do_test misc1-10.2 {
297 catchsql "SELECT count(*) FROM manycol $::where AND rowid>0"
drh0aa74ed2005-07-16 13:33:20 +0000298} {0 9}
drh83dcb1a2002-06-28 01:02:38 +0000299do_test misc1-10.3 {
300 regsub "x0>=0" $::where "x0=0" ::where
301 catchsql "DELETE FROM manycol $::where"
302} {0 {}}
303do_test misc1-10.4 {
304 execsql {SELECT count(*) FROM manycol}
305} {8}
306do_test misc1-10.5 {
307 catchsql "DELETE FROM manycol $::where AND rowid>0"
drh0aa74ed2005-07-16 13:33:20 +0000308} {0 {}}
drh83dcb1a2002-06-28 01:02:38 +0000309do_test misc1-10.6 {
310 execsql {SELECT x1 FROM manycol WHERE x0=100}
311} {101}
312do_test misc1-10.7 {
313 regsub "x0=0" $::where "x0=100" ::where
314 catchsql "UPDATE manycol SET x1=x1+1 $::where"
315} {0 {}}
316do_test misc1-10.8 {
317 execsql {SELECT x1 FROM manycol WHERE x0=100}
drh8df447f2005-11-01 15:48:24 +0000318} {102}
drh83dcb1a2002-06-28 01:02:38 +0000319do_test misc1-10.9 {
320 catchsql "UPDATE manycol SET x1=x1+1 $::where AND rowid>0"
drh0aa74ed2005-07-16 13:33:20 +0000321} {0 {}}
drh83dcb1a2002-06-28 01:02:38 +0000322do_test misc1-10.10 {
323 execsql {SELECT x1 FROM manycol WHERE x0=100}
drh8df447f2005-11-01 15:48:24 +0000324} {103}
drh83dcb1a2002-06-28 01:02:38 +0000325
drh92ed08a2002-07-30 18:43:40 +0000326# Make sure the initialization works even if a database is opened while
327# another process has the database locked.
328#
danielk19770de0bb32004-06-10 05:59:24 +0000329# Update for v3: The BEGIN doesn't lock the database so the schema is read
330# and the SELECT returns successfully.
drh92ed08a2002-07-30 18:43:40 +0000331do_test misc1-11.1 {
332 execsql {BEGIN}
danielk19771d850a72004-05-31 08:26:49 +0000333 execsql {UPDATE t1 SET a=0 WHERE 0}
drhef4ac8f2004-06-19 00:16:31 +0000334 sqlite3 db2 test.db
drh92ed08a2002-07-30 18:43:40 +0000335 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
336 lappend rc $msg
danielk19770de0bb32004-06-10 05:59:24 +0000337# v2 result: {1 {database is locked}}
338} {0 3}
drh92ed08a2002-07-30 18:43:40 +0000339do_test misc1-11.2 {
340 execsql {COMMIT}
341 set rc [catch {db2 eval {SELECT count(*) FROM t1}} msg]
342 db2 close
343 lappend rc $msg
344} {0 3}
345
drh310ae7b2002-08-15 11:48:13 +0000346# Make sure string comparisons really do compare strings in format4+.
347# Similar tests in the format3.test file show that for format3 and earlier
348# all comparisions where numeric if either operand looked like a number.
349#
350do_test misc1-12.1 {
351 execsql {SELECT '0'=='0.0'}
drh5f6a87b2004-07-19 00:39:45 +0000352} {0}
drh310ae7b2002-08-15 11:48:13 +0000353do_test misc1-12.2 {
354 execsql {SELECT '0'==0.0}
drh5f6a87b2004-07-19 00:39:45 +0000355} {0}
drh310ae7b2002-08-15 11:48:13 +0000356do_test misc1-12.3 {
357 execsql {SELECT '12345678901234567890'=='12345678901234567891'}
358} {0}
359do_test misc1-12.4 {
360 execsql {
361 CREATE TABLE t6(a INT UNIQUE, b TEXT UNIQUE);
362 INSERT INTO t6 VALUES('0','0.0');
363 SELECT * FROM t6;
364 }
365} {0 0.0}
danielk19773bdca9c2006-01-17 09:35:01 +0000366ifcapable conflict {
367 do_test misc1-12.5 {
368 execsql {
369 INSERT OR IGNORE INTO t6 VALUES(0.0,'x');
370 SELECT * FROM t6;
371 }
372 } {0 0.0}
373 do_test misc1-12.6 {
374 execsql {
375 INSERT OR IGNORE INTO t6 VALUES('y',0);
376 SELECT * FROM t6;
377 }
378 } {0 0.0 y 0}
379}
drh310ae7b2002-08-15 11:48:13 +0000380do_test misc1-12.7 {
381 execsql {
382 CREATE TABLE t7(x INTEGER, y TEXT, z);
383 INSERT INTO t7 VALUES(0,0,1);
384 INSERT INTO t7 VALUES(0.0,0,2);
385 INSERT INTO t7 VALUES(0,0.0,3);
386 INSERT INTO t7 VALUES(0.0,0.0,4);
387 SELECT DISTINCT x, y FROM t7 ORDER BY z;
388 }
389} {0 0 0 0.0}
drhf04d5082002-08-18 22:41:22 +0000390do_test misc1-12.8 {
391 execsql {
392 SELECT min(z), max(z), count(z) FROM t7 GROUP BY x ORDER BY 1;
393 }
394} {1 4 4}
395do_test misc1-12.9 {
396 execsql {
397 SELECT min(z), max(z), count(z) FROM t7 GROUP BY y ORDER BY 1;
398 }
399} {1 2 2 3 4 2}
drhfcb78a42003-01-18 20:11:05 +0000400
401# This used to be an error. But we changed the code so that arbitrary
402# identifiers can be used as a collating sequence. Collation is by text
403# if the identifier contains "text", "blob", or "clob" and is numeric
404# otherwise.
danielk19770202b292004-06-09 09:55:16 +0000405#
406# Update: In v3, it is an error again.
407#
408#do_test misc1-12.10 {
409# catchsql {
410# SELECT * FROM t6 ORDER BY a COLLATE unknown;
411# }
drh8df447f2005-11-01 15:48:24 +0000412#} {0 {0 0 y 0}}
drhf04d5082002-08-18 22:41:22 +0000413do_test misc1-12.11 {
414 execsql {
415 CREATE TABLE t8(x TEXT COLLATE numeric, y INTEGER COLLATE text, z);
416 INSERT INTO t8 VALUES(0,0,1);
417 INSERT INTO t8 VALUES(0.0,0,2);
418 INSERT INTO t8 VALUES(0,0.0,3);
419 INSERT INTO t8 VALUES(0.0,0.0,4);
420 SELECT DISTINCT x, y FROM t8 ORDER BY z;
421 }
drhfec19aa2004-05-19 20:41:03 +0000422} {0 0 0.0 0}
drhf04d5082002-08-18 22:41:22 +0000423do_test misc1-12.12 {
424 execsql {
425 SELECT min(z), max(z), count(z) FROM t8 GROUP BY x ORDER BY 1;
426 }
drhfec19aa2004-05-19 20:41:03 +0000427} {1 3 2 2 4 2}
drhf04d5082002-08-18 22:41:22 +0000428do_test misc1-12.13 {
429 execsql {
430 SELECT min(z), max(z), count(z) FROM t8 GROUP BY y ORDER BY 1;
431 }
drhfec19aa2004-05-19 20:41:03 +0000432} {1 4 4}
drhf04d5082002-08-18 22:41:22 +0000433
drh3e56c042002-09-17 03:20:46 +0000434# There was a problem with realloc() in the OP_MemStore operation of
435# the VDBE. A buffer was being reallocated but some pointers into
436# the old copy of the buffer were not being moved over to the new copy.
437# The following code tests for the problem.
438#
danielk19773e8c37e2005-01-21 03:12:14 +0000439ifcapable subquery {
440 do_test misc1-13.1 {
441 execsql {
442 CREATE TABLE t9(x,y);
443 INSERT INTO t9 VALUES('one',1);
444 INSERT INTO t9 VALUES('two',2);
445 INSERT INTO t9 VALUES('three',3);
446 INSERT INTO t9 VALUES('four',4);
447 INSERT INTO t9 VALUES('five',5);
448 INSERT INTO t9 VALUES('six',6);
449 INSERT INTO t9 VALUES('seven',7);
450 INSERT INTO t9 VALUES('eight',8);
451 INSERT INTO t9 VALUES('nine',9);
452 INSERT INTO t9 VALUES('ten',10);
453 INSERT INTO t9 VALUES('eleven',11);
454 SELECT y FROM t9
455 WHERE x=(SELECT x FROM t9 WHERE y=1)
456 OR x=(SELECT x FROM t9 WHERE y=2)
457 OR x=(SELECT x FROM t9 WHERE y=3)
458 OR x=(SELECT x FROM t9 WHERE y=4)
459 OR x=(SELECT x FROM t9 WHERE y=5)
460 OR x=(SELECT x FROM t9 WHERE y=6)
461 OR x=(SELECT x FROM t9 WHERE y=7)
462 OR x=(SELECT x FROM t9 WHERE y=8)
463 OR x=(SELECT x FROM t9 WHERE y=9)
464 OR x=(SELECT x FROM t9 WHERE y=10)
465 OR x=(SELECT x FROM t9 WHERE y=11)
466 OR x=(SELECT x FROM t9 WHERE y=12)
467 OR x=(SELECT x FROM t9 WHERE y=13)
468 OR x=(SELECT x FROM t9 WHERE y=14)
469 ;
470 }
471 } {1 2 3 4 5 6 7 8 9 10 11}
472}
drh310ae7b2002-08-15 11:48:13 +0000473
mistachkinc5484652012-03-05 22:52:33 +0000474#
475# The following tests can only work if the current SQLite VFS has the concept
476# of a current directory.
477#
478ifcapable curdir {
drh3e7a6092002-12-07 21:45:14 +0000479# Make sure a database connection still works after changing the
480# working directory.
481#
482do_test misc1-14.1 {
483 file mkdir tempdir
484 cd tempdir
485 execsql {BEGIN}
486 file exists ./test.db-journal
487} {0}
drhba726f42010-03-19 15:48:13 +0000488do_test misc1-14.2a {
489 execsql {UPDATE t1 SET a=a||'x' WHERE 0}
490 file exists ../test.db-journal
491} {0}
492do_test misc1-14.2b {
493 execsql {UPDATE t1 SET a=a||'y' WHERE 1}
drh3e7a6092002-12-07 21:45:14 +0000494 file exists ../test.db-journal
495} {1}
496do_test misc1-14.3 {
497 cd ..
mistachkinfda06be2011-08-02 00:57:34 +0000498 forcedelete tempdir
drh3e7a6092002-12-07 21:45:14 +0000499 execsql {COMMIT}
500 file exists ./test.db-journal
501} {0}
mistachkinc5484652012-03-05 22:52:33 +0000502}
drh3e7a6092002-12-07 21:45:14 +0000503
drh17e9e292003-02-01 13:53:28 +0000504# A failed create table should not leave the table in the internal
505# data structures. Ticket #238.
506#
drhf14fd032005-08-19 03:03:51 +0000507do_test misc1-15.1.1 {
drh17e9e292003-02-01 13:53:28 +0000508 catchsql {
509 CREATE TABLE t10 AS SELECT c1;
510 }
511} {1 {no such column: c1}}
drhf14fd032005-08-19 03:03:51 +0000512do_test misc1-15.1.2 {
513 catchsql {
514 CREATE TABLE t10 AS SELECT t9.c1;
515 }
516} {1 {no such column: t9.c1}}
517do_test misc1-15.1.3 {
518 catchsql {
519 CREATE TABLE t10 AS SELECT main.t9.c1;
520 }
521} {1 {no such column: main.t9.c1}}
drh17e9e292003-02-01 13:53:28 +0000522do_test misc1-15.2 {
523 catchsql {
524 CREATE TABLE t10 AS SELECT 1;
525 }
526 # The bug in ticket #238 causes the statement above to fail with
527 # the error "table t10 alread exists"
528} {0 {}}
drh3e7a6092002-12-07 21:45:14 +0000529
drhe0194f22003-02-26 13:52:51 +0000530# Test for memory leaks when a CREATE TABLE containing a primary key
531# fails. Ticket #249.
532#
533do_test misc1-16.1 {
534 catchsql {SELECT name FROM sqlite_master LIMIT 1}
535 catchsql {
536 CREATE TABLE test(a integer, primary key(a));
537 }
538} {0 {}}
539do_test misc1-16.2 {
540 catchsql {
541 CREATE TABLE test(a integer, primary key(a));
542 }
543} {1 {table test already exists}}
544do_test misc1-16.3 {
545 catchsql {
546 CREATE TABLE test2(a text primary key, b text, primary key(a,b));
547 }
548} {1 {table "test2" has more than one primary key}}
549do_test misc1-16.4 {
550 execsql {
551 INSERT INTO test VALUES(1);
552 SELECT rowid, a FROM test;
553 }
554} {1 1}
555do_test misc1-16.5 {
556 execsql {
557 INSERT INTO test VALUES(5);
558 SELECT rowid, a FROM test;
559 }
560} {1 1 5 5}
561do_test misc1-16.6 {
562 execsql {
563 INSERT INTO test VALUES(NULL);
564 SELECT rowid, a FROM test;
565 }
566} {1 1 5 5 6 6}
567
danielk197753c0f742005-03-29 03:10:59 +0000568ifcapable trigger&&tempdb {
drh4312db52003-06-03 01:47:11 +0000569# Ticket #333: Temp triggers that modify persistent tables.
570#
571do_test misc1-17.1 {
572 execsql {
573 BEGIN;
574 CREATE TABLE RealTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
575 CREATE TEMP TABLE TempTable(TestID INTEGER PRIMARY KEY, TestString TEXT);
576 CREATE TEMP TRIGGER trigTest_1 AFTER UPDATE ON TempTable BEGIN
577 INSERT INTO RealTable(TestString)
578 SELECT new.TestString FROM TempTable LIMIT 1;
579 END;
580 INSERT INTO TempTable(TestString) VALUES ('1');
581 INSERT INTO TempTable(TestString) VALUES ('2');
danielk1977e61b9f42005-01-21 04:25:47 +0000582 UPDATE TempTable SET TestString = TestString + 1 WHERE TestID=1 OR TestId=2;
drh4312db52003-06-03 01:47:11 +0000583 COMMIT;
584 SELECT TestString FROM RealTable ORDER BY 1;
585 }
drh8df447f2005-11-01 15:48:24 +0000586} {2 3}
drh798da522004-11-04 04:42:28 +0000587}
drhe0194f22003-02-26 13:52:51 +0000588
drhf9cb7f52006-06-27 20:06:44 +0000589do_test misc1-18.1 {
590 set n [sqlite3_sleep 100]
591 expr {$n>=100}
592} {1}
593
drhc7407522014-01-10 20:38:12 +0000594# 2014-01-10: In a CREATE TABLE AS, if one or more of the column names
595# are an empty string, that is still OK.
596#
597do_execsql_test misc1-19.1 {
598 CREATE TABLE t19 AS SELECT 1, 2 AS '', 3;
599 SELECT * FROM t19;
600} {1 2 3}
601do_execsql_test misc1-19.2 {
602 CREATE TABLE t19b AS SELECT 4 AS '', 5 AS '', 6 AS '';
603 SELECT * FROM t19b;
604} {4 5 6}
605
drhf058b9c2015-05-20 17:25:44 +0000606# 2015-05-20: CREATE TABLE AS should not store INT value is a TEXT
607# column.
608#
609do_execsql_test misc1-19.3 {
610 CREATE TABLE t19c(x TEXT);
611 CREATE TABLE t19d AS SELECT * FROM t19c UNION ALL SELECT 1234;
612 SELECT x, typeof(x) FROM t19d;
613} {1234 text}
614
drhc007f612014-05-16 14:17:01 +0000615# 2014-05-16: Tests for the SQLITE_TESTCTRL_FAULT_INSTALL feature.
616#
617unset -nocomplain fault_callbacks
618set fault_callbacks {}
619proc fault_callback {n} {
620 lappend ::fault_callbacks $n
621 return 0
622}
623do_test misc1-19.1 {
624 sqlite3_test_control_fault_install fault_callback
625 set fault_callbacks
626} {0}
627do_test misc1-19.2 {
628 sqlite3_test_control_fault_install
629 set fault_callbacks
630} {0}
drhc7407522014-01-10 20:38:12 +0000631
drha58d4a92015-01-27 13:17:05 +0000632# 2015-01-26: Valgrind-detected over-read.
633# Reported on sqlite-users@sqlite.org by Michal Zalewski. Found by afl-fuzz
634# presumably.
635#
636do_execsql_test misc1-20.1 {
637 CREATE TABLE t0(x INTEGER DEFAULT(0==0) NOT NULL);
638 REPLACE INTO t0(x) VALUES('');
639 SELECT rowid, quote(x) FROM t0;
640} {1 ''}
641
drh74893a42015-03-22 10:23:17 +0000642# 2015-03-22: NULL pointer dereference after a syntax error
643#
644do_catchsql_test misc1-21.1 {
645 select''like''like''like#0;
646} {1 {near "#0": syntax error}}
647do_catchsql_test misc1-21.2 {
648 VALUES(0,0x0MATCH#0;
649} {1 {near ";": syntax error}}
650
drh0ec68f82015-04-15 04:51:28 +0000651# 2015-04-15
652do_execsql_test misc1-22.1 {
653 SELECT ""+3 FROM (SELECT ""+5);
654} {3}
655
drhd0c73052015-04-19 19:21:19 +0000656# 2015-04-19: NULL pointer dereference on a corrupt schema
657#
drh33c59ec2015-04-19 20:39:17 +0000658db close
659sqlite3 db :memory:
drhd0c73052015-04-19 19:21:19 +0000660do_execsql_test misc1-23.1 {
drhd0c73052015-04-19 19:21:19 +0000661 CREATE TABLE t1(x);
662 PRAGMA writable_schema=ON;
663 UPDATE sqlite_master SET sql='CREATE table t(d CHECK(T(#0)';
664 BEGIN;
665 CREATE TABLE t2(y);
666 ROLLBACK;
667 DROP TABLE IF EXISTS t3;
668} {}
669
drh33c59ec2015-04-19 20:39:17 +0000670# 2015-04-19: Faulty assert() statement
671#
672db close
673database_may_be_corrupt
674sqlite3 db :memory:
675do_catchsql_test misc1-23.2 {
676 CREATE TABLE t1(x UNIQUE);
677 PRAGMA writable_schema=ON;
678 UPDATE sqlite_master SET sql='CREATE TABLE IF not EXISTS t(c)';
679 BEGIN;
680 CREATE TABLE t2(x);
681 ROLLBACK;
682 DROP TABLE F;
683} {1 {no such table: F}}
drh5f1d2fa2015-04-19 21:59:19 +0000684db close
685sqlite3 db :memory:
686do_catchsql_test misc1-23.3 {
687 CREATE TABLE t1(x UNIQUE);
688 PRAGMA writable_schema=ON;
689 UPDATE sqlite_master SET sql='CREATE table y(a TEXT, a TEXT)';
690 BEGIN;
691 CREATE TABLE t2(y);
692 ROLLBACK;
693 DROP TABLE IF EXISTS t;
694} {0 {}}
drh33c59ec2015-04-19 20:39:17 +0000695
dan584390e2015-05-26 18:58:57 +0000696
697# At one point, running this would read one byte passed the end of a
698# buffer, upsetting valgrind.
699#
700do_test misc1-24.0 {
701 list [catch { sqlite3_prepare_v2 db ! -1 dummy } msg] $msg
drhb2bddbb2016-02-18 14:49:28 +0000702} {1 {(1) unrecognized token: "!"}}
dan584390e2015-05-26 18:58:57 +0000703
drh96ceaf82015-11-14 22:04:22 +0000704# The following query (provided by Kostya Serebryany) used to take 25
705# minutes to prepare. This has been speeded up to about 250 milliseconds.
706#
707do_catchsql_test misc1-25.0 {
708SELECT-1 UNION SELECT 5 UNION SELECT 0 UNION SElECT*from(SELECT-5) UNION SELECT*from(SELECT-0) UNION SELECT:SELECT-0 UNION SELECT-1 UNION SELECT 1 UNION SELECT 1 ORDER BY S in(WITH K AS(WITH K AS(select'CREINDERcharREADEVIRTUL5TABLECONFLICT !1 USIN'' MFtOR(b38q,eWITH K AS(selectCREATe TABLE t0(a,b,c,d,e, PRIMARY KEY(a,b,c,d,c,a,b,b,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,b,c,d,c,a,c,d,c,d,c,e,d,d,c,a,b,b,c,c,a,b,b,E,d,c,d,c,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,a,b,c,e,d,d,c,a,b,b,c,d,d,c,a,b,c,e,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,a,b,c,e,d,d,c,a,b,b,b,c,e,d,d,c,a,b,b,c,c,a,b,b,c,d,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,c,d,c,a,b,c,e,d,d,c,a,b,b,c,d,c,d,c,a,b,c,e,c,d,c,a,b,b,c,d,c,d,c,e,d,d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