blob: 1eaf62e379e1b5cdd74a3f95020c5969b799e024 [file] [log] [blame]
drh8ce10ba2003-06-22 01:41:49 +00001# 2003 June 21
2#
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#
drh2564ef92006-09-29 14:01:04 +000016# $Id: misc2.test,v 1.26 2006/09/29 14:01:07 drh Exp $
drh8ce10ba2003-06-22 01:41:49 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
drh798da522004-11-04 04:42:28 +000021ifcapable {trigger} {
drh8ce10ba2003-06-22 01:41:49 +000022# Test for ticket #360
23#
24do_test misc2-1.1 {
25 catchsql {
26 CREATE TABLE FOO(bar integer);
27 CREATE TRIGGER foo_insert BEFORE INSERT ON foo BEGIN
28 SELECT CASE WHEN (NOT new.bar BETWEEN 0 AND 20)
29 THEN raise(rollback, 'aiieee') END;
30 END;
31 INSERT INTO foo(bar) VALUES (1);
32 }
drhdc339ee2003-06-29 20:25:08 +000033} {0 {}}
34do_test misc2-1.2 {
35 catchsql {
36 INSERT INTO foo(bar) VALUES (111);
37 }
drh8ce10ba2003-06-22 01:41:49 +000038} {1 aiieee}
drh798da522004-11-04 04:42:28 +000039} ;# endif trigger
drhd60ccc62003-06-24 10:39:46 +000040
41# Make sure ROWID works on a view and a subquery. Ticket #364
42#
43do_test misc2-2.1 {
44 execsql {
45 CREATE TABLE t1(a,b,c);
46 INSERT INTO t1 VALUES(1,2,3);
drhda808d52003-07-09 16:34:56 +000047 CREATE TABLE t2(a,b,c);
drhd60ccc62003-06-24 10:39:46 +000048 INSERT INTO t2 VALUES(7,8,9);
danielk1977d8702b42004-11-22 15:05:58 +000049 }
50} {}
drh2564ef92006-09-29 14:01:04 +000051ifcapable subquery {
52 do_test misc2-2.2 {
53 execsql {
54 SELECT rowid, * FROM (SELECT * FROM t1, t2);
55 }
56 } {{} 1 2 3 7 8 9}
57}
danielk1977d8702b42004-11-22 15:05:58 +000058ifcapable view {
danielk1977e61b9f42005-01-21 04:25:47 +000059 do_test misc2-2.3 {
60 execsql {
61 CREATE VIEW v1 AS SELECT * FROM t1, t2;
62 SELECT rowid, * FROM v1;
63 }
64 } {{} 1 2 3 7 8 9}
danielk19770fa8ddb2004-11-22 08:43:32 +000065} ;# ifcapable view
drhda808d52003-07-09 16:34:56 +000066
drh2564ef92006-09-29 14:01:04 +000067# Ticket #2002 and #1952.
68ifcapable subquery {
69 do_test misc2-2.4 {
70 execsql2 {
71 SELECT * FROM (SELECT a, b AS 'a', c AS 'a', 4 AS 'a' FROM t1)
72 }
73 } {a 1 a:1 2 a:2 3 a:3 4}
74}
75
drhda808d52003-07-09 16:34:56 +000076# Check name binding precedence. Ticket #387
77#
78do_test misc2-3.1 {
79 catchsql {
80 SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10
81 }
82} {1 {ambiguous column name: a}}
drhdc2d94d2003-07-27 17:16:06 +000083
84# Make sure 32-bit integer overflow is handled properly in queries.
85# ticket #408
86#
87do_test misc2-4.1 {
88 execsql {
89 INSERT INTO t1 VALUES(4000000000,'a','b');
90 SELECT a FROM t1 WHERE a>1;
91 }
92} {4000000000}
93do_test misc2-4.2 {
94 execsql {
95 INSERT INTO t1 VALUES(2147483648,'b2','c2');
96 INSERT INTO t1 VALUES(2147483647,'b3','c3');
97 SELECT a FROM t1 WHERE a>2147483647;
98 }
99} {4000000000 2147483648}
100do_test misc2-4.3 {
101 execsql {
102 SELECT a FROM t1 WHERE a<2147483648;
103 }
104} {1 2147483647}
105do_test misc2-4.4 {
106 execsql {
107 SELECT a FROM t1 WHERE a<=2147483648;
108 }
109} {1 2147483648 2147483647}
110do_test misc2-4.5 {
111 execsql {
112 SELECT a FROM t1 WHERE a<10000000000;
113 }
114} {1 4000000000 2147483648 2147483647}
115do_test misc2-4.6 {
116 execsql {
117 SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1;
118 }
119} {1 2147483647 2147483648 4000000000}
drh4305d102003-07-30 12:34:12 +0000120
121# There were some issues with expanding a SrcList object using a call
122# to sqliteSrcListAppend() if the SrcList had previously been duplicated
123# using a call to sqliteSrcListDup(). Ticket #416. The following test
124# makes sure the problem has been fixed.
125#
danielk19770fa8ddb2004-11-22 08:43:32 +0000126ifcapable view {
drh4305d102003-07-30 12:34:12 +0000127do_test misc2-5.1 {
128 execsql {
129 CREATE TABLE x(a,b);
130 CREATE VIEW y AS
131 SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a;
132 CREATE VIEW z AS
133 SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q;
134 SELECT * from z;
135 }
136} {}
danielk19770fa8ddb2004-11-22 08:43:32 +0000137}
drh901afd42003-08-26 11:25:58 +0000138
139# Make sure we can open a database with an empty filename. What this
140# does is store the database in a temporary file that is deleted when
141# the database is closed. Ticket #432.
142#
143do_test misc2-6.1 {
144 db close
drhef4ac8f2004-06-19 00:16:31 +0000145 sqlite3 db {}
drh901afd42003-08-26 11:25:58 +0000146 execsql {
147 CREATE TABLE t1(a,b);
148 INSERT INTO t1 VALUES(1,2);
149 SELECT * FROM t1;
150 }
151} {1 2}
drhe4c61692003-08-27 22:54:31 +0000152
danielk197712b13002004-11-22 10:02:21 +0000153# Make sure we get an error message (not a segfault) on an attempt to
154# update a table from within the callback of a select on that same
155# table.
156#
drh980b1a72006-08-16 16:42:48 +0000157# 2006-08-16: This has changed. It is now permitted to update
158# the table being SELECTed from within the callback of the query.
159#
danielk197712b13002004-11-22 10:02:21 +0000160do_test misc2-7.1 {
161 db close
162 file delete -force test.db
163 sqlite3 db test.db
164 execsql {
165 CREATE TABLE t1(x);
166 INSERT INTO t1 VALUES(1);
drh980b1a72006-08-16 16:42:48 +0000167 INSERT INTO t1 VALUES(2);
168 INSERT INTO t1 VALUES(3);
169 SELECT * FROM t1;
danielk197712b13002004-11-22 10:02:21 +0000170 }
drh980b1a72006-08-16 16:42:48 +0000171} {1 2 3}
172do_test misc2-7.2 {
danielk197712b13002004-11-22 10:02:21 +0000173 set rc [catch {
174 db eval {SELECT rowid FROM t1} {} {
175 db eval "DELETE FROM t1 WHERE rowid=$rowid"
176 }
177 } msg]
178 lappend rc $msg
drh980b1a72006-08-16 16:42:48 +0000179} {0 {}}
180do_test misc2-7.3 {
181 execsql {SELECT * FROM t1}
182} {}
183do_test misc2-7.4 {
184 execsql {
185 DELETE FROM t1;
186 INSERT INTO t1 VALUES(1);
187 INSERT INTO t1 VALUES(2);
188 INSERT INTO t1 VALUES(3);
189 INSERT INTO t1 VALUES(4);
190 }
191 db eval {SELECT rowid, x FROM t1} {
192 if {$x & 1} {
193 db eval {DELETE FROM t1 WHERE rowid=$rowid}
danielk197712b13002004-11-22 10:02:21 +0000194 }
drh980b1a72006-08-16 16:42:48 +0000195 }
196 execsql {SELECT * FROM t1}
197} {2 4}
198do_test misc2-7.5 {
199 execsql {
200 DELETE FROM t1;
201 INSERT INTO t1 VALUES(1);
202 INSERT INTO t1 VALUES(2);
203 INSERT INTO t1 VALUES(3);
204 INSERT INTO t1 VALUES(4);
205 }
206 db eval {SELECT rowid, x FROM t1} {
207 if {$x & 1} {
208 db eval {DELETE FROM t1 WHERE rowid=$rowid+1}
danielk197712b13002004-11-22 10:02:21 +0000209 }
drh980b1a72006-08-16 16:42:48 +0000210 }
211 execsql {SELECT * FROM t1}
212} {1 3}
213do_test misc2-7.6 {
214 execsql {
215 DELETE FROM t1;
216 INSERT INTO t1 VALUES(1);
217 INSERT INTO t1 VALUES(2);
218 INSERT INTO t1 VALUES(3);
219 INSERT INTO t1 VALUES(4);
220 }
221 db eval {SELECT rowid, x FROM t1} {
222 if {$x & 1} {
223 db eval {DELETE FROM t1}
224 }
225 }
226 execsql {SELECT * FROM t1}
227} {}
228do_test misc2-7.7 {
229 execsql {
230 DELETE FROM t1;
231 INSERT INTO t1 VALUES(1);
232 INSERT INTO t1 VALUES(2);
233 INSERT INTO t1 VALUES(3);
234 INSERT INTO t1 VALUES(4);
235 }
236 db eval {SELECT rowid, x FROM t1} {
237 if {$x & 1} {
238 db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid}
239 }
240 }
241 execsql {SELECT * FROM t1}
242} {101 2 103 4}
243do_test misc2-7.8 {
244 execsql {
245 DELETE FROM t1;
246 INSERT INTO t1 VALUES(1);
247 }
248 db eval {SELECT rowid, x FROM t1} {
249 if {$x<10} {
250 db eval {INSERT INTO t1 VALUES($x+1)}
251 }
252 }
253 execsql {SELECT * FROM t1}
254} {1 2 3 4 5 6 7 8 9 10}
danielk19774489f9b2005-01-20 02:17:01 +0000255
256db close
257file delete -force test.db
258sqlite3 db test.db
danielk197712b13002004-11-22 10:02:21 +0000259
drh61b487d2003-09-12 02:08:14 +0000260# Ticket #453. If the SQL ended with "-", the tokenizer was calling that
261# an incomplete token, which caused problem. The solution was to just call
262# it a minus sign.
263#
264do_test misc2-8.1 {
265 catchsql {-}
266} {1 {near "-": syntax error}}
drh0f18bfa2003-12-10 01:31:21 +0000267
268# Ticket #513. Make sure the VDBE stack does not grow on a 3-way join.
269#
danielk197753c0f742005-03-29 03:10:59 +0000270ifcapable tempdb {
271 do_test misc2-9.1 {
272 execsql {
273 BEGIN;
274 CREATE TABLE counts(n INTEGER PRIMARY KEY);
275 INSERT INTO counts VALUES(0);
276 INSERT INTO counts VALUES(1);
277 INSERT INTO counts SELECT n+2 FROM counts;
278 INSERT INTO counts SELECT n+4 FROM counts;
279 INSERT INTO counts SELECT n+8 FROM counts;
280 COMMIT;
281
282 CREATE TEMP TABLE x AS
283 SELECT dim1.n, dim2.n, dim3.n
284 FROM counts AS dim1, counts AS dim2, counts AS dim3
285 WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10;
286
287 SELECT count(*) FROM x;
288 }
289 } {1000}
290 do_test misc2-9.2 {
291 execsql {
292 DROP TABLE x;
293 CREATE TEMP TABLE x AS
294 SELECT dim1.n, dim2.n, dim3.n
295 FROM counts AS dim1, counts AS dim2, counts AS dim3
296 WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6;
297
298 SELECT count(*) FROM x;
299 }
300 } {1000}
301 do_test misc2-9.3 {
302 execsql {
303 DROP TABLE x;
304 CREATE TEMP TABLE x AS
305 SELECT dim1.n, dim2.n, dim3.n, dim4.n
306 FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4
307 WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5;
308
309 SELECT count(*) FROM x;
310 }
311 } [expr 5*5*5*5]
312}
drhacf4ac92003-12-17 23:57:34 +0000313
drh7e627792005-04-29 02:10:00 +0000314# Ticket #1229. Sometimes when a "NEW.X" appears in a SELECT without
315# a FROM clause deep within a trigger, the code generator is unable to
316# trace the NEW.X back to an original table and thus figure out its
317# declared datatype.
318#
319# The SQL code below was causing a segfault.
320#
danielk19773bdca9c2006-01-17 09:35:01 +0000321ifcapable subquery&&trigger {
danielk19771576cd92006-01-14 08:02:28 +0000322 do_test misc2-10.1 {
323 execsql {
324 CREATE TABLE t1229(x);
325 CREATE TRIGGER r1229 BEFORE INSERT ON t1229 BEGIN
326 INSERT INTO t1229 SELECT y FROM (SELECT new.x y);
327 END;
328 INSERT INTO t1229 VALUES(1);
329 }
330 } {}
331}
drh7e627792005-04-29 02:10:00 +0000332
drhacf4ac92003-12-17 23:57:34 +0000333finish_test