blob: 4d6c07f2d0c6e02842da14b33950d4e1d54b7629 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh3aadb2e2000-05-31 17:59:25 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh3aadb2e2000-05-31 17:59:25 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drh3aadb2e2000-05-31 17:59:25 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the SELECT statement.
13#
drh33e619f2009-05-28 01:00:55 +000014# $Id: select1.test,v 1.70 2009/05/28 01:00:56 drh Exp $
drh3aadb2e2000-05-31 17:59:25 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Try to select on a non-existant table.
20#
drh22827922000-06-06 17:27:05 +000021do_test select1-1.1 {
drh3aadb2e2000-05-31 17:59:25 +000022 set v [catch {execsql {SELECT * FROM test1}} msg]
23 lappend v $msg
24} {1 {no such table: test1}}
25
danielk197713a68c32005-12-15 10:11:30 +000026
drh3aadb2e2000-05-31 17:59:25 +000027execsql {CREATE TABLE test1(f1 int, f2 int)}
28
drh22827922000-06-06 17:27:05 +000029do_test select1-1.2 {
drh3aadb2e2000-05-31 17:59:25 +000030 set v [catch {execsql {SELECT * FROM test1, test2}} msg]
31 lappend v $msg
32} {1 {no such table: test2}}
drh22827922000-06-06 17:27:05 +000033do_test select1-1.3 {
drh3aadb2e2000-05-31 17:59:25 +000034 set v [catch {execsql {SELECT * FROM test2, test1}} msg]
35 lappend v $msg
36} {1 {no such table: test2}}
37
38execsql {INSERT INTO test1(f1,f2) VALUES(11,22)}
39
40
drh967e8b72000-06-21 13:59:10 +000041# Make sure the columns are extracted correctly.
drh3aadb2e2000-05-31 17:59:25 +000042#
drh22827922000-06-06 17:27:05 +000043do_test select1-1.4 {
drh3aadb2e2000-05-31 17:59:25 +000044 execsql {SELECT f1 FROM test1}
45} {11}
drh22827922000-06-06 17:27:05 +000046do_test select1-1.5 {
drh3aadb2e2000-05-31 17:59:25 +000047 execsql {SELECT f2 FROM test1}
48} {22}
drh22827922000-06-06 17:27:05 +000049do_test select1-1.6 {
drh3aadb2e2000-05-31 17:59:25 +000050 execsql {SELECT f2, f1 FROM test1}
51} {22 11}
drh22827922000-06-06 17:27:05 +000052do_test select1-1.7 {
drh3aadb2e2000-05-31 17:59:25 +000053 execsql {SELECT f1, f2 FROM test1}
54} {11 22}
drh22827922000-06-06 17:27:05 +000055do_test select1-1.8 {
drh3aadb2e2000-05-31 17:59:25 +000056 execsql {SELECT * FROM test1}
57} {11 22}
drh7c917d12001-12-16 20:05:05 +000058do_test select1-1.8.1 {
59 execsql {SELECT *, * FROM test1}
60} {11 22 11 22}
61do_test select1-1.8.2 {
62 execsql {SELECT *, min(f1,f2), max(f1,f2) FROM test1}
63} {11 22 11 22}
64do_test select1-1.8.3 {
65 execsql {SELECT 'one', *, 'two', * FROM test1}
66} {one 11 22 two 11 22}
drh3aadb2e2000-05-31 17:59:25 +000067
68execsql {CREATE TABLE test2(r1 real, r2 real)}
69execsql {INSERT INTO test2(r1,r2) VALUES(1.1,2.2)}
70
drh22827922000-06-06 17:27:05 +000071do_test select1-1.9 {
drh3aadb2e2000-05-31 17:59:25 +000072 execsql {SELECT * FROM test1, test2}
73} {11 22 1.1 2.2}
drh7c917d12001-12-16 20:05:05 +000074do_test select1-1.9.1 {
75 execsql {SELECT *, 'hi' FROM test1, test2}
76} {11 22 1.1 2.2 hi}
77do_test select1-1.9.2 {
78 execsql {SELECT 'one', *, 'two', * FROM test1, test2}
79} {one 11 22 1.1 2.2 two 11 22 1.1 2.2}
drh22827922000-06-06 17:27:05 +000080do_test select1-1.10 {
drh3aadb2e2000-05-31 17:59:25 +000081 execsql {SELECT test1.f1, test2.r1 FROM test1, test2}
82} {11 1.1}
drh22827922000-06-06 17:27:05 +000083do_test select1-1.11 {
drh3aadb2e2000-05-31 17:59:25 +000084 execsql {SELECT test1.f1, test2.r1 FROM test2, test1}
85} {11 1.1}
drh17e24df2001-11-06 14:10:41 +000086do_test select1-1.11.1 {
87 execsql {SELECT * FROM test2, test1}
88} {1.1 2.2 11 22}
89do_test select1-1.11.2 {
90 execsql {SELECT * FROM test1 AS a, test1 AS b}
91} {11 22 11 22}
drh22827922000-06-06 17:27:05 +000092do_test select1-1.12 {
drh3aadb2e2000-05-31 17:59:25 +000093 execsql {SELECT max(test1.f1,test2.r1), min(test1.f2,test2.r2)
94 FROM test2, test1}
95} {11 2.2}
drh22827922000-06-06 17:27:05 +000096do_test select1-1.13 {
drh3aadb2e2000-05-31 17:59:25 +000097 execsql {SELECT min(test1.f1,test2.r1), max(test1.f2,test2.r2)
98 FROM test1, test2}
99} {1.1 22}
100
drh832508b2002-03-02 17:04:07 +0000101set long {This is a string that is too big to fit inside a NBFS buffer}
102do_test select1-2.0 {
103 execsql "
104 DROP TABLE test2;
105 DELETE FROM test1;
106 INSERT INTO test1 VALUES(11,22);
107 INSERT INTO test1 VALUES(33,44);
108 CREATE TABLE t3(a,b);
109 INSERT INTO t3 VALUES('abc',NULL);
110 INSERT INTO t3 VALUES(NULL,'xyz');
111 INSERT INTO t3 SELECT * FROM test1;
112 CREATE TABLE t4(a,b);
113 INSERT INTO t4 VALUES(NULL,'$long');
114 SELECT * FROM t3;
115 "
116} {abc {} {} xyz 11 22 33 44}
drh3aadb2e2000-05-31 17:59:25 +0000117
118# Error messges from sqliteExprCheck
119#
drh22827922000-06-06 17:27:05 +0000120do_test select1-2.1 {
drh3aadb2e2000-05-31 17:59:25 +0000121 set v [catch {execsql {SELECT count(f1,f2) FROM test1}} msg]
122 lappend v $msg
drh89425d52002-02-28 03:04:48 +0000123} {1 {wrong number of arguments to function count()}}
drh22827922000-06-06 17:27:05 +0000124do_test select1-2.2 {
drh3aadb2e2000-05-31 17:59:25 +0000125 set v [catch {execsql {SELECT count(f1) FROM test1}} msg]
126 lappend v $msg
127} {0 2}
drh22827922000-06-06 17:27:05 +0000128do_test select1-2.3 {
drh3aadb2e2000-05-31 17:59:25 +0000129 set v [catch {execsql {SELECT Count() FROM test1}} msg]
130 lappend v $msg
131} {0 2}
drh22827922000-06-06 17:27:05 +0000132do_test select1-2.4 {
drh3aadb2e2000-05-31 17:59:25 +0000133 set v [catch {execsql {SELECT COUNT(*) FROM test1}} msg]
134 lappend v $msg
135} {0 2}
drh22827922000-06-06 17:27:05 +0000136do_test select1-2.5 {
drh3aadb2e2000-05-31 17:59:25 +0000137 set v [catch {execsql {SELECT COUNT(*)+1 FROM test1}} msg]
138 lappend v $msg
drh22827922000-06-06 17:27:05 +0000139} {0 3}
drh832508b2002-03-02 17:04:07 +0000140do_test select1-2.5.1 {
141 execsql {SELECT count(*),count(a),count(b) FROM t3}
142} {4 3 3}
143do_test select1-2.5.2 {
144 execsql {SELECT count(*),count(a),count(b) FROM t4}
145} {1 0 1}
146do_test select1-2.5.3 {
147 execsql {SELECT count(*),count(a),count(b) FROM t4 WHERE b=5}
148} {0 0 0}
drh22827922000-06-06 17:27:05 +0000149do_test select1-2.6 {
drh3aadb2e2000-05-31 17:59:25 +0000150 set v [catch {execsql {SELECT min(*) FROM test1}} msg]
151 lappend v $msg
drhfbc99082002-02-28 03:14:18 +0000152} {1 {wrong number of arguments to function min()}}
drh22827922000-06-06 17:27:05 +0000153do_test select1-2.7 {
drh3aadb2e2000-05-31 17:59:25 +0000154 set v [catch {execsql {SELECT Min(f1) FROM test1}} msg]
155 lappend v $msg
156} {0 11}
drh22827922000-06-06 17:27:05 +0000157do_test select1-2.8 {
drh3aadb2e2000-05-31 17:59:25 +0000158 set v [catch {execsql {SELECT MIN(f1,f2) FROM test1}} msg]
159 lappend v [lsort $msg]
160} {0 {11 33}}
drh832508b2002-03-02 17:04:07 +0000161do_test select1-2.8.1 {
162 execsql {SELECT coalesce(min(a),'xyzzy') FROM t3}
drh9eb516c2004-07-18 20:52:32 +0000163} {11}
drh832508b2002-03-02 17:04:07 +0000164do_test select1-2.8.2 {
165 execsql {SELECT min(coalesce(a,'xyzzy')) FROM t3}
166} {11}
167do_test select1-2.8.3 {
168 execsql {SELECT min(b), min(b) FROM t4}
169} [list $long $long]
drh22827922000-06-06 17:27:05 +0000170do_test select1-2.9 {
drh3aadb2e2000-05-31 17:59:25 +0000171 set v [catch {execsql {SELECT MAX(*) FROM test1}} msg]
172 lappend v $msg
drhfbc99082002-02-28 03:14:18 +0000173} {1 {wrong number of arguments to function MAX()}}
drh22827922000-06-06 17:27:05 +0000174do_test select1-2.10 {
drh3aadb2e2000-05-31 17:59:25 +0000175 set v [catch {execsql {SELECT Max(f1) FROM test1}} msg]
176 lappend v $msg
177} {0 33}
drh22827922000-06-06 17:27:05 +0000178do_test select1-2.11 {
drh3aadb2e2000-05-31 17:59:25 +0000179 set v [catch {execsql {SELECT max(f1,f2) FROM test1}} msg]
180 lappend v [lsort $msg]
181} {0 {22 44}}
drh22827922000-06-06 17:27:05 +0000182do_test select1-2.12 {
drh3aadb2e2000-05-31 17:59:25 +0000183 set v [catch {execsql {SELECT MAX(f1,f2)+1 FROM test1}} msg]
184 lappend v [lsort $msg]
185} {0 {23 45}}
drh22827922000-06-06 17:27:05 +0000186do_test select1-2.13 {
drh3aadb2e2000-05-31 17:59:25 +0000187 set v [catch {execsql {SELECT MAX(f1)+1 FROM test1}} msg]
188 lappend v $msg
drh22827922000-06-06 17:27:05 +0000189} {0 34}
drh832508b2002-03-02 17:04:07 +0000190do_test select1-2.13.1 {
191 execsql {SELECT coalesce(max(a),'xyzzy') FROM t3}
drh739105c2002-05-29 23:22:23 +0000192} {abc}
drhbb113512002-05-27 01:04:51 +0000193do_test select1-2.13.2 {
drh832508b2002-03-02 17:04:07 +0000194 execsql {SELECT max(coalesce(a,'xyzzy')) FROM t3}
195} {xyzzy}
drh22827922000-06-06 17:27:05 +0000196do_test select1-2.14 {
drh3aadb2e2000-05-31 17:59:25 +0000197 set v [catch {execsql {SELECT SUM(*) FROM test1}} msg]
198 lappend v $msg
drh89425d52002-02-28 03:04:48 +0000199} {1 {wrong number of arguments to function SUM()}}
drh22827922000-06-06 17:27:05 +0000200do_test select1-2.15 {
drh3aadb2e2000-05-31 17:59:25 +0000201 set v [catch {execsql {SELECT Sum(f1) FROM test1}} msg]
202 lappend v $msg
drh3d1d95e2005-09-08 10:37:01 +0000203} {0 44}
drh22827922000-06-06 17:27:05 +0000204do_test select1-2.16 {
drh3aadb2e2000-05-31 17:59:25 +0000205 set v [catch {execsql {SELECT sum(f1,f2) FROM test1}} msg]
206 lappend v $msg
drh89425d52002-02-28 03:04:48 +0000207} {1 {wrong number of arguments to function sum()}}
drh22827922000-06-06 17:27:05 +0000208do_test select1-2.17 {
drh3aadb2e2000-05-31 17:59:25 +0000209 set v [catch {execsql {SELECT SUM(f1)+1 FROM test1}} msg]
210 lappend v $msg
drh3d1d95e2005-09-08 10:37:01 +0000211} {0 45}
drh832508b2002-03-02 17:04:07 +0000212do_test select1-2.17.1 {
213 execsql {SELECT sum(a) FROM t3}
drh29d72102006-02-09 22:13:41 +0000214} {44.0}
drh22827922000-06-06 17:27:05 +0000215do_test select1-2.18 {
drh3aadb2e2000-05-31 17:59:25 +0000216 set v [catch {execsql {SELECT XYZZY(f1) FROM test1}} msg]
217 lappend v $msg
218} {1 {no such function: XYZZY}}
drh22827922000-06-06 17:27:05 +0000219do_test select1-2.19 {
drh3aadb2e2000-05-31 17:59:25 +0000220 set v [catch {execsql {SELECT SUM(min(f1,f2)) FROM test1}} msg]
221 lappend v $msg
drh3d1d95e2005-09-08 10:37:01 +0000222} {0 44}
drh22827922000-06-06 17:27:05 +0000223do_test select1-2.20 {
drh3aadb2e2000-05-31 17:59:25 +0000224 set v [catch {execsql {SELECT SUM(min(f1)) FROM test1}} msg]
225 lappend v $msg
drh8e0a2f92002-02-23 23:45:45 +0000226} {1 {misuse of aggregate function min()}}
drh3aadb2e2000-05-31 17:59:25 +0000227
drh36379e92007-07-23 22:51:15 +0000228# Ticket #2526
229#
230do_test select1-2.21 {
231 catchsql {
232 SELECT min(f1) AS m FROM test1 GROUP BY f1 HAVING max(m+5)<10
233 }
234} {1 {misuse of aliased aggregate m}}
235do_test select1-2.22 {
236 catchsql {
237 SELECT coalesce(min(f1)+5,11) AS m FROM test1
238 GROUP BY f1
239 HAVING max(m+5)<10
240 }
241} {1 {misuse of aliased aggregate m}}
242do_test select1-2.23 {
243 execsql {
244 CREATE TABLE tkt2526(a,b,c PRIMARY KEY);
245 INSERT INTO tkt2526 VALUES('x','y',NULL);
246 INSERT INTO tkt2526 VALUES('x','z',NULL);
247 }
248 catchsql {
249 SELECT count(a) AS cn FROM tkt2526 GROUP BY a HAVING cn<max(cn)
250 }
251} {1 {misuse of aliased aggregate cn}}
252
drh3aadb2e2000-05-31 17:59:25 +0000253# WHERE clause expressions
254#
drh22827922000-06-06 17:27:05 +0000255do_test select1-3.1 {
drh3aadb2e2000-05-31 17:59:25 +0000256 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<11}} msg]
257 lappend v $msg
258} {0 {}}
drh22827922000-06-06 17:27:05 +0000259do_test select1-3.2 {
drh3aadb2e2000-05-31 17:59:25 +0000260 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<=11}} msg]
261 lappend v $msg
262} {0 11}
drh22827922000-06-06 17:27:05 +0000263do_test select1-3.3 {
drh3aadb2e2000-05-31 17:59:25 +0000264 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1=11}} msg]
265 lappend v $msg
266} {0 11}
drh22827922000-06-06 17:27:05 +0000267do_test select1-3.4 {
drh3aadb2e2000-05-31 17:59:25 +0000268 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>=11}} msg]
269 lappend v [lsort $msg]
270} {0 {11 33}}
drh22827922000-06-06 17:27:05 +0000271do_test select1-3.5 {
drh3aadb2e2000-05-31 17:59:25 +0000272 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>11}} msg]
273 lappend v [lsort $msg]
274} {0 33}
drh22827922000-06-06 17:27:05 +0000275do_test select1-3.6 {
drh3aadb2e2000-05-31 17:59:25 +0000276 set v [catch {execsql {SELECT f1 FROM test1 WHERE f1!=11}} msg]
277 lappend v [lsort $msg]
278} {0 33}
drh22827922000-06-06 17:27:05 +0000279do_test select1-3.7 {
drh3aadb2e2000-05-31 17:59:25 +0000280 set v [catch {execsql {SELECT f1 FROM test1 WHERE min(f1,f2)!=11}} msg]
281 lappend v [lsort $msg]
282} {0 33}
drh22827922000-06-06 17:27:05 +0000283do_test select1-3.8 {
drh3aadb2e2000-05-31 17:59:25 +0000284 set v [catch {execsql {SELECT f1 FROM test1 WHERE max(f1,f2)!=11}} msg]
285 lappend v [lsort $msg]
286} {0 {11 33}}
drh22827922000-06-06 17:27:05 +0000287do_test select1-3.9 {
drh3aadb2e2000-05-31 17:59:25 +0000288 set v [catch {execsql {SELECT f1 FROM test1 WHERE count(f1,f2)!=11}} msg]
289 lappend v $msg
drh89425d52002-02-28 03:04:48 +0000290} {1 {wrong number of arguments to function count()}}
drh3aadb2e2000-05-31 17:59:25 +0000291
292# ORDER BY expressions
293#
drh22827922000-06-06 17:27:05 +0000294do_test select1-4.1 {
drh3aadb2e2000-05-31 17:59:25 +0000295 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY f1}} msg]
296 lappend v $msg
297} {0 {11 33}}
drh22827922000-06-06 17:27:05 +0000298do_test select1-4.2 {
drh3aadb2e2000-05-31 17:59:25 +0000299 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY -f1}} msg]
300 lappend v $msg
301} {0 {33 11}}
drh22827922000-06-06 17:27:05 +0000302do_test select1-4.3 {
drh3aadb2e2000-05-31 17:59:25 +0000303 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1,f2)}} msg]
304 lappend v $msg
305} {0 {11 33}}
drh22827922000-06-06 17:27:05 +0000306do_test select1-4.4 {
drh3aadb2e2000-05-31 17:59:25 +0000307 set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1)}} msg]
308 lappend v $msg
drhb7916a72009-05-27 10:31:29 +0000309} {1 {misuse of aggregate: min()}}
dan5b1c07e2015-04-16 07:19:23 +0000310do_catchsql_test select1-4.5 {
311 INSERT INTO test1(f1) SELECT f1 FROM test1 ORDER BY min(f1);
312} {1 {misuse of aggregate: min()}}
drh18e87cf2006-04-11 14:16:21 +0000313
314# The restriction not allowing constants in the ORDER BY clause
315# has been removed. See ticket #1768
316#do_test select1-4.5 {
317# catchsql {
318# SELECT f1 FROM test1 ORDER BY 8.4;
319# }
320#} {1 {ORDER BY terms must not be non-integer constants}}
321#do_test select1-4.6 {
322# catchsql {
323# SELECT f1 FROM test1 ORDER BY '8.4';
324# }
325#} {1 {ORDER BY terms must not be non-integer constants}}
326#do_test select1-4.7.1 {
327# catchsql {
328# SELECT f1 FROM test1 ORDER BY 'xyz';
329# }
330#} {1 {ORDER BY terms must not be non-integer constants}}
331#do_test select1-4.7.2 {
332# catchsql {
333# SELECT f1 FROM test1 ORDER BY -8.4;
334# }
335#} {1 {ORDER BY terms must not be non-integer constants}}
336#do_test select1-4.7.3 {
337# catchsql {
338# SELECT f1 FROM test1 ORDER BY +8.4;
339# }
340#} {1 {ORDER BY terms must not be non-integer constants}}
341#do_test select1-4.7.4 {
342# catchsql {
343# SELECT f1 FROM test1 ORDER BY 4294967296; -- constant larger than 32 bits
344# }
345#} {1 {ORDER BY terms must not be non-integer constants}}
346
drh92086432002-01-22 14:11:29 +0000347do_test select1-4.5 {
drh18e87cf2006-04-11 14:16:21 +0000348 execsql {
349 SELECT f1 FROM test1 ORDER BY 8.4
drh92086432002-01-22 14:11:29 +0000350 }
drh18e87cf2006-04-11 14:16:21 +0000351} {11 33}
drhe4de1fe2002-06-02 16:09:01 +0000352do_test select1-4.6 {
drh18e87cf2006-04-11 14:16:21 +0000353 execsql {
354 SELECT f1 FROM test1 ORDER BY '8.4'
drhe4de1fe2002-06-02 16:09:01 +0000355 }
drh18e87cf2006-04-11 14:16:21 +0000356} {11 33}
357
drhe4de1fe2002-06-02 16:09:01 +0000358do_test select1-4.8 {
359 execsql {
360 CREATE TABLE t5(a,b);
361 INSERT INTO t5 VALUES(1,10);
362 INSERT INTO t5 VALUES(2,9);
363 SELECT * FROM t5 ORDER BY 1;
364 }
365} {1 10 2 9}
drh018d1a42005-01-15 01:52:31 +0000366do_test select1-4.9.1 {
drhe4de1fe2002-06-02 16:09:01 +0000367 execsql {
368 SELECT * FROM t5 ORDER BY 2;
369 }
370} {2 9 1 10}
drh018d1a42005-01-15 01:52:31 +0000371do_test select1-4.9.2 {
372 execsql {
373 SELECT * FROM t5 ORDER BY +2;
374 }
375} {2 9 1 10}
376do_test select1-4.10.1 {
drhe4de1fe2002-06-02 16:09:01 +0000377 catchsql {
378 SELECT * FROM t5 ORDER BY 3;
379 }
danielk197701874bf2007-12-13 07:58:50 +0000380} {1 {1st ORDER BY term out of range - should be between 1 and 2}}
drh018d1a42005-01-15 01:52:31 +0000381do_test select1-4.10.2 {
382 catchsql {
383 SELECT * FROM t5 ORDER BY -1;
384 }
danielk197701874bf2007-12-13 07:58:50 +0000385} {1 {1st ORDER BY term out of range - should be between 1 and 2}}
drhe4de1fe2002-06-02 16:09:01 +0000386do_test select1-4.11 {
387 execsql {
388 INSERT INTO t5 VALUES(3,10);
389 SELECT * FROM t5 ORDER BY 2, 1 DESC;
390 }
391} {2 9 3 10 1 10}
392do_test select1-4.12 {
393 execsql {
394 SELECT * FROM t5 ORDER BY 1 DESC, b;
395 }
396} {3 10 2 9 1 10}
397do_test select1-4.13 {
398 execsql {
399 SELECT * FROM t5 ORDER BY b DESC, 1;
400 }
401} {1 10 3 10 2 9}
402
drh3aadb2e2000-05-31 17:59:25 +0000403
404# ORDER BY ignored on an aggregate query
405#
drh22827922000-06-06 17:27:05 +0000406do_test select1-5.1 {
drh3aadb2e2000-05-31 17:59:25 +0000407 set v [catch {execsql {SELECT max(f1) FROM test1 ORDER BY f2}} msg]
408 lappend v $msg
409} {0 33}
410
drh85e9e222008-07-15 00:27:34 +0000411execsql {CREATE TABLE test2(t1 text, t2 text)}
drh3aadb2e2000-05-31 17:59:25 +0000412execsql {INSERT INTO test2 VALUES('abc','xyz')}
413
drh967e8b72000-06-21 13:59:10 +0000414# Check for column naming
drh3aadb2e2000-05-31 17:59:25 +0000415#
drh22827922000-06-06 17:27:05 +0000416do_test select1-6.1 {
drh3aadb2e2000-05-31 17:59:25 +0000417 set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
418 lappend v $msg
419} {0 {f1 11 f1 33}}
drh382c0242001-10-06 16:33:02 +0000420do_test select1-6.1.1 {
drhafed0862006-03-14 13:10:42 +0000421 db eval {PRAGMA full_column_names=on}
drh382c0242001-10-06 16:33:02 +0000422 set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
423 lappend v $msg
drh1bee3d72001-10-15 00:44:35 +0000424} {0 {test1.f1 11 test1.f1 33}}
drh382c0242001-10-06 16:33:02 +0000425do_test select1-6.1.2 {
drh1bee3d72001-10-15 00:44:35 +0000426 set v [catch {execsql2 {SELECT f1 as 'f1' FROM test1 ORDER BY f2}} msg]
427 lappend v $msg
428} {0 {f1 11 f1 33}}
429do_test select1-6.1.3 {
drh382c0242001-10-06 16:33:02 +0000430 set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
drh382c0242001-10-06 16:33:02 +0000431 lappend v $msg
drh79d5f632005-01-18 17:20:10 +0000432} {0 {f1 11 f2 22}}
drh1bee3d72001-10-15 00:44:35 +0000433do_test select1-6.1.4 {
drh98808ba2001-10-18 12:34:46 +0000434 set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
drhafed0862006-03-14 13:10:42 +0000435 db eval {PRAGMA full_column_names=off}
drh98808ba2001-10-18 12:34:46 +0000436 lappend v $msg
drh79d5f632005-01-18 17:20:10 +0000437} {0 {f1 11 f2 22}}
drh98808ba2001-10-18 12:34:46 +0000438do_test select1-6.1.5 {
drh382c0242001-10-06 16:33:02 +0000439 set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
440 lappend v $msg
441} {0 {f1 11 f2 22}}
drh98808ba2001-10-18 12:34:46 +0000442do_test select1-6.1.6 {
443 set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
444 lappend v $msg
445} {0 {f1 11 f2 22}}
drh22827922000-06-06 17:27:05 +0000446do_test select1-6.2 {
drh3aadb2e2000-05-31 17:59:25 +0000447 set v [catch {execsql2 {SELECT f1 as xyzzy FROM test1 ORDER BY f2}} msg]
448 lappend v $msg
449} {0 {xyzzy 11 xyzzy 33}}
drh22827922000-06-06 17:27:05 +0000450do_test select1-6.3 {
drh3aadb2e2000-05-31 17:59:25 +0000451 set v [catch {execsql2 {SELECT f1 as "xyzzy" FROM test1 ORDER BY f2}} msg]
452 lappend v $msg
453} {0 {xyzzy 11 xyzzy 33}}
drhd4007282001-04-12 23:21:58 +0000454do_test select1-6.3.1 {
455 set v [catch {execsql2 {SELECT f1 as 'xyzzy ' FROM test1 ORDER BY f2}} msg]
456 lappend v $msg
457} {0 {{xyzzy } 11 {xyzzy } 33}}
drh22827922000-06-06 17:27:05 +0000458do_test select1-6.4 {
drh3aadb2e2000-05-31 17:59:25 +0000459 set v [catch {execsql2 {SELECT f1+F2 as xyzzy FROM test1 ORDER BY f2}} msg]
460 lappend v $msg
461} {0 {xyzzy 33 xyzzy 77}}
drhc4a3c772001-04-04 11:48:57 +0000462do_test select1-6.4a {
463 set v [catch {execsql2 {SELECT f1+F2 FROM test1 ORDER BY f2}} msg]
464 lappend v $msg
465} {0 {f1+F2 33 f1+F2 77}}
drh22827922000-06-06 17:27:05 +0000466do_test select1-6.5 {
drh3aadb2e2000-05-31 17:59:25 +0000467 set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
468 lappend v $msg
drhe1b6a5b2000-07-29 13:06:59 +0000469} {0 {test1.f1+F2 33 test1.f1+F2 77}}
drh1bee3d72001-10-15 00:44:35 +0000470do_test select1-6.5.1 {
471 execsql2 {PRAGMA full_column_names=on}
472 set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
473 execsql2 {PRAGMA full_column_names=off}
474 lappend v $msg
475} {0 {test1.f1+F2 33 test1.f1+F2 77}}
drh22827922000-06-06 17:27:05 +0000476do_test select1-6.6 {
drh3aadb2e2000-05-31 17:59:25 +0000477 set v [catch {execsql2 {SELECT test1.f1+F2, t1 FROM test1, test2
478 ORDER BY f2}} msg]
479 lappend v $msg
drhe1b6a5b2000-07-29 13:06:59 +0000480} {0 {test1.f1+F2 33 t1 abc test1.f1+F2 77 t1 abc}}
drh22827922000-06-06 17:27:05 +0000481do_test select1-6.7 {
drhda9d6c42000-05-31 18:20:14 +0000482 set v [catch {execsql2 {SELECT A.f1, t1 FROM test1 as A, test2
483 ORDER BY f2}} msg]
484 lappend v $msg
drh47a6db22005-01-18 16:02:40 +0000485} {0 {f1 11 t1 abc f1 33 t1 abc}}
drh22827922000-06-06 17:27:05 +0000486do_test select1-6.8 {
drhda9d6c42000-05-31 18:20:14 +0000487 set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as B
488 ORDER BY f2}} msg]
489 lappend v $msg
drh967e8b72000-06-21 13:59:10 +0000490} {1 {ambiguous column name: f1}}
drhcc85b412000-06-07 15:11:27 +0000491do_test select1-6.8b {
drhda9d6c42000-05-31 18:20:14 +0000492 set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
493 ORDER BY f2}} msg]
494 lappend v $msg
drh967e8b72000-06-21 13:59:10 +0000495} {1 {ambiguous column name: f2}}
drhcc85b412000-06-07 15:11:27 +0000496do_test select1-6.8c {
497 set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as A
498 ORDER BY f2}} msg]
499 lappend v $msg
drh967e8b72000-06-21 13:59:10 +0000500} {1 {ambiguous column name: A.f1}}
drh47a6db22005-01-18 16:02:40 +0000501do_test select1-6.9.1 {
502 set v [catch {execsql {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
503 ORDER BY A.f1, B.f1}} msg]
504 lappend v $msg
505} {0 {11 11 11 33 33 11 33 33}}
506do_test select1-6.9.2 {
drhda9d6c42000-05-31 18:20:14 +0000507 set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
508 ORDER BY A.f1, B.f1}} msg]
509 lappend v $msg
drh47a6db22005-01-18 16:02:40 +0000510} {0 {f1 11 f1 11 f1 33 f1 33 f1 11 f1 11 f1 33 f1 33}}
danielk197727c77432004-11-22 13:35:41 +0000511
drhe49b1462008-07-09 01:39:44 +0000512do_test select1-6.9.3 {
513 db eval {
514 PRAGMA short_column_names=OFF;
515 PRAGMA full_column_names=OFF;
516 }
517 execsql2 {
518 SELECT test1 . f1, test1 . f2 FROM test1 LIMIT 1
519 }
520} {{test1 . f1} 11 {test1 . f2} 22}
521do_test select1-6.9.4 {
522 db eval {
523 PRAGMA short_column_names=OFF;
524 PRAGMA full_column_names=ON;
525 }
526 execsql2 {
527 SELECT test1 . f1, test1 . f2 FROM test1 LIMIT 1
528 }
529} {test1.f1 11 test1.f2 22}
530do_test select1-6.9.5 {
531 db eval {
532 PRAGMA short_column_names=OFF;
533 PRAGMA full_column_names=ON;
534 }
535 execsql2 {
536 SELECT 123.45;
537 }
538} {123.45 123.45}
drh93a960a2008-07-10 00:32:42 +0000539do_test select1-6.9.6 {
540 execsql2 {
541 SELECT * FROM test1 a, test1 b LIMIT 1
542 }
543} {a.f1 11 a.f2 22 b.f1 11 b.f2 22}
544do_test select1-6.9.7 {
545 set x [execsql2 {
546 SELECT * FROM test1 a, (select 5, 6) LIMIT 1
547 }]
drhb50596d2013-10-08 20:42:41 +0000548 regsub -all {sq_[0-9a-fA-F_]+} $x {subquery} x
drh93a960a2008-07-10 00:32:42 +0000549 set x
550} {a.f1 11 a.f2 22 sqlite_subquery.5 5 sqlite_subquery.6 6}
551do_test select1-6.9.8 {
552 set x [execsql2 {
553 SELECT * FROM test1 a, (select 5 AS x, 6 AS y) AS b LIMIT 1
554 }]
555 regsub -all {subquery_[0-9a-fA-F]+_} $x {subquery} x
556 set x
557} {a.f1 11 a.f2 22 b.x 5 b.y 6}
drh85e9e222008-07-15 00:27:34 +0000558do_test select1-6.9.9 {
559 execsql2 {
560 SELECT a.f1, b.f2 FROM test1 a, test1 b LIMIT 1
561 }
562} {test1.f1 11 test1.f2 22}
563do_test select1-6.9.10 {
564 execsql2 {
565 SELECT f1, t1 FROM test1, test2 LIMIT 1
566 }
567} {test1.f1 11 test2.t1 abc}
568do_test select1-6.9.11 {
569 db eval {
570 PRAGMA short_column_names=ON;
571 PRAGMA full_column_names=ON;
572 }
573 execsql2 {
574 SELECT a.f1, b.f2 FROM test1 a, test1 b LIMIT 1
575 }
576} {test1.f1 11 test1.f2 22}
577do_test select1-6.9.12 {
578 execsql2 {
579 SELECT f1, t1 FROM test1, test2 LIMIT 1
580 }
581} {test1.f1 11 test2.t1 abc}
582do_test select1-6.9.13 {
583 db eval {
584 PRAGMA short_column_names=ON;
585 PRAGMA full_column_names=OFF;
586 }
587 execsql2 {
588 SELECT a.f1, b.f1 FROM test1 a, test1 b LIMIT 1
589 }
590} {f1 11 f1 11}
591do_test select1-6.9.14 {
592 execsql2 {
593 SELECT f1, t1 FROM test1, test2 LIMIT 1
594 }
595} {f1 11 t1 abc}
596do_test select1-6.9.15 {
597 db eval {
598 PRAGMA short_column_names=OFF;
599 PRAGMA full_column_names=ON;
600 }
601 execsql2 {
602 SELECT a.f1, b.f1 FROM test1 a, test1 b LIMIT 1
603 }
604} {test1.f1 11 test1.f1 11}
605do_test select1-6.9.16 {
606 execsql2 {
607 SELECT f1, t1 FROM test1, test2 LIMIT 1
608 }
609} {test1.f1 11 test2.t1 abc}
610
drh93a960a2008-07-10 00:32:42 +0000611
drhe49b1462008-07-09 01:39:44 +0000612db eval {
613 PRAGMA short_column_names=ON;
614 PRAGMA full_column_names=OFF;
615}
616
danielk197727c77432004-11-22 13:35:41 +0000617ifcapable compound {
drhc4a3c772001-04-04 11:48:57 +0000618do_test select1-6.10 {
619 set v [catch {execsql2 {
620 SELECT f1 FROM test1 UNION SELECT f2 FROM test1
621 ORDER BY f2;
622 }} msg]
623 lappend v $msg
drh92378252006-03-26 01:21:22 +0000624} {0 {f1 11 f1 22 f1 33 f1 44}}
drhc4a3c772001-04-04 11:48:57 +0000625do_test select1-6.11 {
626 set v [catch {execsql2 {
627 SELECT f1 FROM test1 UNION SELECT f2+100 FROM test1
drh4c774312007-12-08 21:10:20 +0000628 ORDER BY f2+101;
drhc4a3c772001-04-04 11:48:57 +0000629 }} msg]
630 lappend v $msg
danielk197701874bf2007-12-13 07:58:50 +0000631} {1 {1st ORDER BY term does not match any column in the result set}}
drh94ccde52007-04-13 16:06:32 +0000632
633# Ticket #2296
danielk1977de3e41e2008-08-04 03:51:24 +0000634ifcapable subquery&&compound {
drh94ccde52007-04-13 16:06:32 +0000635do_test select1-6.20 {
636 execsql {
637 CREATE TABLE t6(a TEXT, b TEXT);
638 INSERT INTO t6 VALUES('a','0');
639 INSERT INTO t6 VALUES('b','1');
640 INSERT INTO t6 VALUES('c','2');
641 INSERT INTO t6 VALUES('d','3');
642 SELECT a FROM t6 WHERE b IN
643 (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
644 ORDER BY 1 LIMIT 1)
645 }
646} {a}
647do_test select1-6.21 {
648 execsql {
649 SELECT a FROM t6 WHERE b IN
650 (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
651 ORDER BY 1 DESC LIMIT 1)
652 }
653} {d}
654do_test select1-6.22 {
655 execsql {
656 SELECT a FROM t6 WHERE b IN
657 (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
658 ORDER BY b LIMIT 2)
659 ORDER BY a;
660 }
661} {a b}
662do_test select1-6.23 {
663 execsql {
664 SELECT a FROM t6 WHERE b IN
665 (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
666 ORDER BY x DESC LIMIT 2)
667 ORDER BY a;
668 }
669} {b d}
danielk1977284f4ac2007-12-10 05:03:46 +0000670}
drh94ccde52007-04-13 16:06:32 +0000671
danielk197727c77432004-11-22 13:35:41 +0000672} ;#ifcapable compound
drh3aadb2e2000-05-31 17:59:25 +0000673
drhd4007282001-04-12 23:21:58 +0000674do_test select1-7.1 {
675 set v [catch {execsql {
676 SELECT f1 FROM test1 WHERE f2=;
677 }} msg]
678 lappend v $msg
679} {1 {near ";": syntax error}}
danielk197727c77432004-11-22 13:35:41 +0000680ifcapable compound {
drhd4007282001-04-12 23:21:58 +0000681do_test select1-7.2 {
682 set v [catch {execsql {
683 SELECT f1 FROM test1 UNION SELECT WHERE;
684 }} msg]
685 lappend v $msg
686} {1 {near "WHERE": syntax error}}
danielk197727c77432004-11-22 13:35:41 +0000687} ;# ifcapable compound
drhd4007282001-04-12 23:21:58 +0000688do_test select1-7.3 {
689 set v [catch {execsql {SELECT f1 FROM test1 as 'hi', test2 as}} msg]
690 lappend v $msg
691} {1 {near "as": syntax error}}
692do_test select1-7.4 {
693 set v [catch {execsql {
694 SELECT f1 FROM test1 ORDER BY;
695 }} msg]
696 lappend v $msg
697} {1 {near ";": syntax error}}
698do_test select1-7.5 {
699 set v [catch {execsql {
700 SELECT f1 FROM test1 ORDER BY f1 desc, f2 where;
701 }} msg]
702 lappend v $msg
703} {1 {near "where": syntax error}}
704do_test select1-7.6 {
705 set v [catch {execsql {
706 SELECT count(f1,f2 FROM test1;
707 }} msg]
708 lappend v $msg
709} {1 {near "FROM": syntax error}}
710do_test select1-7.7 {
711 set v [catch {execsql {
712 SELECT count(f1,f2+) FROM test1;
713 }} msg]
714 lappend v $msg
715} {1 {near ")": syntax error}}
716do_test select1-7.8 {
717 set v [catch {execsql {
718 SELECT f1 FROM test1 ORDER BY f2, f1+;
719 }} msg]
720 lappend v $msg
721} {1 {near ";": syntax error}}
drh15926592007-04-06 15:02:13 +0000722do_test select1-7.9 {
723 catchsql {
724 SELECT f1 FROM test1 LIMIT 5+3 OFFSET 11 ORDER BY f2;
725 }
726} {1 {near "ORDER": syntax error}}
drhd4007282001-04-12 23:21:58 +0000727
728do_test select1-8.1 {
729 execsql {SELECT f1 FROM test1 WHERE 4.3+2.4 OR 1 ORDER BY f1}
730} {11 33}
731do_test select1-8.2 {
732 execsql {
733 SELECT f1 FROM test1 WHERE ('x' || f1) BETWEEN 'x10' AND 'x20'
734 ORDER BY f1
735 }
736} {11}
737do_test select1-8.3 {
738 execsql {
739 SELECT f1 FROM test1 WHERE 5-3==2
740 ORDER BY f1
741 }
742} {11 33}
danielk19778d059842004-05-12 11:24:02 +0000743
744# TODO: This test is failing because f1 is now being loaded off the
745# disk as a vdbe integer, not a string. Hence the value of f1/(f1-11)
746# changes because of rounding. Disable the test for now.
747if 0 {
drhd4007282001-04-12 23:21:58 +0000748do_test select1-8.4 {
749 execsql {
drhbb113512002-05-27 01:04:51 +0000750 SELECT coalesce(f1/(f1-11),'x'),
751 coalesce(min(f1/(f1-11),5),'y'),
752 coalesce(max(f1/(f1-33),6),'z')
drhd4007282001-04-12 23:21:58 +0000753 FROM test1 ORDER BY f1
754 }
drhbb113512002-05-27 01:04:51 +0000755} {x y 6 1.5 1.5 z}
danielk19778d059842004-05-12 11:24:02 +0000756}
drhd4007282001-04-12 23:21:58 +0000757do_test select1-8.5 {
758 execsql {
759 SELECT min(1,2,3), -max(1,2,3)
760 FROM test1 ORDER BY f1
761 }
762} {1 -3 1 -3}
763
drh6a535342001-10-19 16:44:56 +0000764
765# Check the behavior when the result set is empty
766#
danielk1977cbb18d22004-05-28 11:37:27 +0000767# SQLite v3 always sets r(*).
768#
769# do_test select1-9.1 {
770# catch {unset r}
771# set r(*) {}
772# db eval {SELECT * FROM test1 WHERE f1<0} r {}
773# set r(*)
774# } {}
drh6a535342001-10-19 16:44:56 +0000775do_test select1-9.2 {
776 execsql {PRAGMA empty_result_callbacks=on}
danielk1977cbb18d22004-05-28 11:37:27 +0000777 catch {unset r}
drh6a535342001-10-19 16:44:56 +0000778 set r(*) {}
779 db eval {SELECT * FROM test1 WHERE f1<0} r {}
780 set r(*)
781} {f1 f2}
danielk19773e8c37e2005-01-21 03:12:14 +0000782ifcapable subquery {
783 do_test select1-9.3 {
784 set r(*) {}
785 db eval {SELECT * FROM test1 WHERE f1<(select count(*) from test2)} r {}
786 set r(*)
787 } {f1 f2}
788}
drh6a535342001-10-19 16:44:56 +0000789do_test select1-9.4 {
790 set r(*) {}
791 db eval {SELECT * FROM test1 ORDER BY f1} r {}
792 set r(*)
793} {f1 f2}
794do_test select1-9.5 {
795 set r(*) {}
796 db eval {SELECT * FROM test1 WHERE f1<0 ORDER BY f1} r {}
797 set r(*)
798} {f1 f2}
799unset r
800
drha2e00042002-01-22 03:13:42 +0000801# Check for ORDER BY clauses that refer to an AS name in the column list
802#
803do_test select1-10.1 {
804 execsql {
805 SELECT f1 AS x FROM test1 ORDER BY x
806 }
807} {11 33}
808do_test select1-10.2 {
809 execsql {
810 SELECT f1 AS x FROM test1 ORDER BY -x
811 }
812} {33 11}
813do_test select1-10.3 {
814 execsql {
815 SELECT f1-23 AS x FROM test1 ORDER BY abs(x)
816 }
817} {10 -12}
818do_test select1-10.4 {
819 execsql {
820 SELECT f1-23 AS x FROM test1 ORDER BY -abs(x)
821 }
822} {-12 10}
823do_test select1-10.5 {
824 execsql {
825 SELECT f1-22 AS x, f2-22 as y FROM test1
826 }
827} {-11 0 11 22}
828do_test select1-10.6 {
829 execsql {
830 SELECT f1-22 AS x, f2-22 as y FROM test1 WHERE x>0 AND y<50
831 }
832} {11 22}
drh93a960a2008-07-10 00:32:42 +0000833do_test select1-10.7 {
834 execsql {
835 SELECT f1 COLLATE nocase AS x FROM test1 ORDER BY x
836 }
837} {11 33}
drha2e00042002-01-22 03:13:42 +0000838
drh54473222002-04-04 02:10:55 +0000839# Check the ability to specify "TABLE.*" in the result set of a SELECT
840#
841do_test select1-11.1 {
842 execsql {
843 DELETE FROM t3;
844 DELETE FROM t4;
845 INSERT INTO t3 VALUES(1,2);
846 INSERT INTO t4 VALUES(3,4);
847 SELECT * FROM t3, t4;
848 }
849} {1 2 3 4}
drh47a6db22005-01-18 16:02:40 +0000850do_test select1-11.2.1 {
851 execsql {
852 SELECT * FROM t3, t4;
853 }
854} {1 2 3 4}
855do_test select1-11.2.2 {
drh54473222002-04-04 02:10:55 +0000856 execsql2 {
857 SELECT * FROM t3, t4;
858 }
drh47a6db22005-01-18 16:02:40 +0000859} {a 3 b 4 a 3 b 4}
drhcf55b7a2004-07-20 01:45:19 +0000860do_test select1-11.4.1 {
drh54473222002-04-04 02:10:55 +0000861 execsql {
862 SELECT t3.*, t4.b FROM t3, t4;
863 }
864} {1 2 4}
drhcf55b7a2004-07-20 01:45:19 +0000865do_test select1-11.4.2 {
866 execsql {
867 SELECT "t3".*, t4.b FROM t3, t4;
868 }
869} {1 2 4}
drh47a6db22005-01-18 16:02:40 +0000870do_test select1-11.5.1 {
drh54473222002-04-04 02:10:55 +0000871 execsql2 {
872 SELECT t3.*, t4.b FROM t3, t4;
873 }
drh47a6db22005-01-18 16:02:40 +0000874} {a 1 b 4 b 4}
drh54473222002-04-04 02:10:55 +0000875do_test select1-11.6 {
876 execsql2 {
877 SELECT x.*, y.b FROM t3 AS x, t4 AS y;
878 }
drh47a6db22005-01-18 16:02:40 +0000879} {a 1 b 4 b 4}
drh54473222002-04-04 02:10:55 +0000880do_test select1-11.7 {
881 execsql {
882 SELECT t3.b, t4.* FROM t3, t4;
883 }
884} {2 3 4}
885do_test select1-11.8 {
886 execsql2 {
887 SELECT t3.b, t4.* FROM t3, t4;
888 }
drh47a6db22005-01-18 16:02:40 +0000889} {b 4 a 3 b 4}
drh54473222002-04-04 02:10:55 +0000890do_test select1-11.9 {
891 execsql2 {
892 SELECT x.b, y.* FROM t3 AS x, t4 AS y;
893 }
drh47a6db22005-01-18 16:02:40 +0000894} {b 4 a 3 b 4}
drh54473222002-04-04 02:10:55 +0000895do_test select1-11.10 {
896 catchsql {
897 SELECT t5.* FROM t3, t4;
898 }
899} {1 {no such table: t5}}
900do_test select1-11.11 {
901 catchsql {
902 SELECT t3.* FROM t3 AS x, t4;
903 }
904} {1 {no such table: t3}}
danielk19773e8c37e2005-01-21 03:12:14 +0000905ifcapable subquery {
906 do_test select1-11.12 {
907 execsql2 {
908 SELECT t3.* FROM t3, (SELECT max(a), max(b) FROM t4)
909 }
910 } {a 1 b 2}
911 do_test select1-11.13 {
912 execsql2 {
913 SELECT t3.* FROM (SELECT max(a), max(b) FROM t4), t3
914 }
915 } {a 1 b 2}
916 do_test select1-11.14 {
917 execsql2 {
918 SELECT * FROM t3, (SELECT max(a), max(b) FROM t4) AS 'tx'
919 }
920 } {a 1 b 2 max(a) 3 max(b) 4}
921 do_test select1-11.15 {
922 execsql2 {
923 SELECT y.*, t3.* FROM t3, (SELECT max(a), max(b) FROM t4) AS y
924 }
925 } {max(a) 3 max(b) 4 a 1 b 2}
926}
drhc754fa52002-05-27 03:25:51 +0000927do_test select1-11.16 {
928 execsql2 {
929 SELECT y.* FROM t3 as y, t4 as z
930 }
drh47a6db22005-01-18 16:02:40 +0000931} {a 1 b 2}
drha2e00042002-01-22 03:13:42 +0000932
drhbf3a4fa2002-04-06 13:57:42 +0000933# Tests of SELECT statements without a FROM clause.
934#
935do_test select1-12.1 {
936 execsql2 {
937 SELECT 1+2+3
938 }
939} {1+2+3 6}
940do_test select1-12.2 {
941 execsql2 {
942 SELECT 1,'hello',2
943 }
944} {1 1 'hello' hello 2 2}
945do_test select1-12.3 {
946 execsql2 {
947 SELECT 1 AS 'a','hello' AS 'b',2 AS 'c'
948 }
949} {a 1 b hello c 2}
950do_test select1-12.4 {
951 execsql {
952 DELETE FROM t3;
953 INSERT INTO t3 VALUES(1,2);
danielk197727c77432004-11-22 13:35:41 +0000954 }
955} {}
956
957ifcapable compound {
958do_test select1-12.5 {
959 execsql {
drhbf3a4fa2002-04-06 13:57:42 +0000960 SELECT * FROM t3 UNION SELECT 3 AS 'a', 4 ORDER BY a;
961 }
962} {1 2 3 4}
danielk197727c77432004-11-22 13:35:41 +0000963
964do_test select1-12.6 {
drhbf3a4fa2002-04-06 13:57:42 +0000965 execsql {
966 SELECT 3, 4 UNION SELECT * FROM t3;
967 }
968} {1 2 3 4}
danielk197727c77432004-11-22 13:35:41 +0000969} ;# ifcapable compound
970
danielk19773e8c37e2005-01-21 03:12:14 +0000971ifcapable subquery {
972 do_test select1-12.7 {
973 execsql {
974 SELECT * FROM t3 WHERE a=(SELECT 1);
975 }
976 } {1 2}
977 do_test select1-12.8 {
978 execsql {
979 SELECT * FROM t3 WHERE a=(SELECT 2);
980 }
981 } {}
982}
danielk197727c77432004-11-22 13:35:41 +0000983
danielk1977e61b9f42005-01-21 04:25:47 +0000984ifcapable {compound && subquery} {
985 do_test select1-12.9 {
986 execsql2 {
987 SELECT x FROM (
drh92378252006-03-26 01:21:22 +0000988 SELECT a AS x, b AS y FROM t3 UNION SELECT a,b FROM t4 ORDER BY a,b
danielk1977e61b9f42005-01-21 04:25:47 +0000989 ) ORDER BY x;
990 }
991 } {x 1 x 3}
992 do_test select1-12.10 {
993 execsql2 {
994 SELECT z.x FROM (
drh92378252006-03-26 01:21:22 +0000995 SELECT a AS x,b AS y FROM t3 UNION SELECT a, b FROM t4 ORDER BY a,b
danielk1977e61b9f42005-01-21 04:25:47 +0000996 ) AS 'z' ORDER BY x;
997 }
998 } {x 1 x 3}
danielk197727c77432004-11-22 13:35:41 +0000999} ;# ifcapable compound
drhd5feede2002-05-08 21:46:14 +00001000
danielk197713a68c32005-12-15 10:11:30 +00001001
danielk1977327bd592006-01-13 13:01:19 +00001002# Check for a VDBE stack growth problem that existed at one point.
1003#
danielk19771576cd92006-01-14 08:02:28 +00001004ifcapable subquery {
1005 do_test select1-13.1 {
danielk1977327bd592006-01-13 13:01:19 +00001006 execsql {
danielk19771576cd92006-01-14 08:02:28 +00001007 BEGIN;
1008 create TABLE abc(a, b, c, PRIMARY KEY(a, b));
1009 INSERT INTO abc VALUES(1, 1, 1);
danielk1977327bd592006-01-13 13:01:19 +00001010 }
danielk19771576cd92006-01-14 08:02:28 +00001011 for {set i 0} {$i<10} {incr i} {
1012 execsql {
1013 INSERT INTO abc SELECT a+(select max(a) FROM abc),
1014 b+(select max(a) FROM abc), c+(select max(a) FROM abc) FROM abc;
1015 }
1016 }
1017 execsql {COMMIT}
1018
1019 # This used to seg-fault when the problem existed.
1020 execsql {
1021 SELECT count(
1022 (SELECT a FROM abc WHERE a = NULL AND b >= upper.c)
1023 ) FROM abc AS upper;
1024 }
1025 } {0}
1026}
danielk1977327bd592006-01-13 13:01:19 +00001027
danielk1977a3f06592009-04-23 14:58:39 +00001028foreach tab [db eval {SELECT name FROM sqlite_master WHERE type = 'table'}] {
1029 db eval "DROP TABLE $tab"
1030}
danielk1977f7b9d662008-06-23 18:49:43 +00001031db close
danielk1977f7b9d662008-06-23 18:49:43 +00001032sqlite3 db test.db
danielk1977a3f06592009-04-23 14:58:39 +00001033
danielk1977f7b9d662008-06-23 18:49:43 +00001034do_test select1-14.1 {
1035 execsql {
1036 SELECT * FROM sqlite_master WHERE rowid>10;
1037 SELECT * FROM sqlite_master WHERE rowid=10;
1038 SELECT * FROM sqlite_master WHERE rowid<10;
1039 SELECT * FROM sqlite_master WHERE rowid<=10;
1040 SELECT * FROM sqlite_master WHERE rowid>=10;
1041 SELECT * FROM sqlite_master;
1042 }
1043} {}
1044do_test select1-14.2 {
1045 execsql {
1046 SELECT 10 IN (SELECT rowid FROM sqlite_master);
1047 }
1048} {0}
1049
drh01e61ee2009-04-10 15:38:42 +00001050if {[db one {PRAGMA locking_mode}]=="normal"} {
1051 # Check that ticket #3771 has been fixed. This test does not
1052 # work with locking_mode=EXCLUSIVE so disable in that case.
1053 #
1054 do_test select1-15.1 {
1055 execsql {
1056 CREATE TABLE t1(a);
1057 CREATE INDEX i1 ON t1(a);
1058 INSERT INTO t1 VALUES(1);
1059 INSERT INTO t1 VALUES(2);
1060 INSERT INTO t1 VALUES(3);
1061 }
1062 } {}
1063 do_test select1-15.2 {
1064 sqlite3 db2 test.db
1065 execsql { DROP INDEX i1 } db2
1066 db2 close
1067 } {}
1068 do_test select1-15.3 {
1069 execsql { SELECT 2 IN (SELECT a FROM t1) }
1070 } {1}
1071}
drh63296052012-02-23 17:35:28 +00001072
1073# Crash bug reported on the mailing list on 2012-02-23
1074#
1075do_test select1-16.1 {
1076 catchsql {SELECT 1 FROM (SELECT *)}
1077} {1 {no tables specified}}
drhb8289a82015-04-17 15:16:58 +00001078
1079# 2015-04-17: assertion fix.
1080do_catchsql_test select1-16.2 {
1081 SELECT 1 FROM sqlite_master LIMIT 1,#1;
1082} {1 {near "#1": syntax error}}
drh01e61ee2009-04-10 15:38:42 +00001083
drh3aadb2e2000-05-31 17:59:25 +00001084finish_test