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. The
15 # focus of this file is testing SELECT statements that contain
16 # subqueries in their FROM clause.
18 # $Id: select6.test,v 1.11 2004/01/24 20:18:13 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
26 CREATE TABLE t1(x, y);
27 INSERT INTO t1 VALUES(1,1);
28 INSERT INTO t1 VALUES(2,2);
29 INSERT INTO t1 VALUES(3,2);
30 INSERT INTO t1 VALUES(4,3);
31 INSERT INTO t1 VALUES(5,3);
32 INSERT INTO t1 VALUES(6,3);
33 INSERT INTO t1 VALUES(7,3);
34 INSERT INTO t1 VALUES(8,4);
35 INSERT INTO t1 VALUES(9,4);
36 INSERT INTO t1 VALUES(10,4);
37 INSERT INTO t1 VALUES(11,4);
38 INSERT INTO t1 VALUES(12,4);
39 INSERT INTO t1 VALUES(13,4);
40 INSERT INTO t1 VALUES(14,4);
41 INSERT INTO t1 VALUES(15,4);
42 INSERT INTO t1 VALUES(16,5);
43 INSERT INTO t1 VALUES(17,5);
44 INSERT INTO t1 VALUES(18,5);
45 INSERT INTO t1 VALUES(19,5);
46 INSERT INTO t1 VALUES(20,5);
48 SELECT DISTINCT y FROM t1 ORDER BY y;
53 execsql2 {SELECT * FROM (SELECT x, y FROM t1 WHERE x<2)}
56 execsql {SELECT count(*) FROM (SELECT y FROM t1)}
59 execsql {SELECT count(*) FROM (SELECT DISTINCT y FROM t1)}
62 execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT y FROM t1))}
65 execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT y FROM t1))}
71 FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
72 (SELECT max(x),y FROM t1 GROUP BY y) as b
73 WHERE a.y=b.y ORDER BY a.y
75 } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
78 SELECT a.y, a.[count(*)], [max(x)], [count(*)]
79 FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
80 (SELECT max(x),y FROM t1 GROUP BY y) as b
81 WHERE a.y=b.y ORDER BY a.y
83 } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
87 FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
88 (SELECT max(x) as r, y as s FROM t1 GROUP BY y) as b
91 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
94 SELECT q, p, r, b.[min(x)+y]
95 FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
96 (SELECT max(x) as r, y as s, min(x)+y FROM t1 GROUP BY y) as b
99 } {1 1 1 2 2 2 3 4 3 4 7 7 4 8 15 12 5 5 20 21}
101 do_test select6-2.0 {
103 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
104 INSERT INTO t2 SELECT * FROM t1;
105 SELECT DISTINCT b FROM t2 ORDER BY b;
108 do_test select6-2.1 {
109 execsql2 {SELECT * FROM (SELECT a, b FROM t2 WHERE a<2)}
111 do_test select6-2.2 {
112 execsql {SELECT count(*) FROM (SELECT b FROM t2)}
114 do_test select6-2.3 {
115 execsql {SELECT count(*) FROM (SELECT DISTINCT b FROM t2)}
117 do_test select6-2.4 {
118 execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT b FROM t2))}
120 do_test select6-2.5 {
121 execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT b FROM t2))}
124 do_test select6-2.6 {
127 FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
128 (SELECT max(a),b FROM t2 GROUP BY b) as b
129 WHERE a.b=b.b ORDER BY a.b
131 } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
132 do_test select6-2.7 {
134 SELECT a.b, a.[count(*)], [max(a)], [count(*)]
135 FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
136 (SELECT max(a),b FROM t2 GROUP BY b) as b
137 WHERE a.b=b.b ORDER BY a.b
139 } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
140 do_test select6-2.8 {
143 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY b) AS a,
144 (SELECT max(a) as r, b as s FROM t2 GROUP BY b) as b
147 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
148 do_test select6-2.9 {
151 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
152 (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
153 WHERE a.q=b.s ORDER BY a.q
155 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
157 do_test sqlite6-3.1 {
159 SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE x=3));
162 do_test sqlite6-3.2 {
165 (SELECT a.q, a.p, b.r
166 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
167 (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
168 WHERE a.q=b.s ORDER BY a.q)
171 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
172 do_test select6-3.3 {
174 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
177 do_test select6-3.4 {
179 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
182 do_test select6-3.5 {
184 SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
187 do_test select6-3.6 {
189 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
193 do_test select6-3.7 {
195 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
199 do_test select6-3.8 {
201 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
205 do_test select6-3.9 {
207 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
211 do_test select6-3.10 {
213 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
216 } {1 1 2 2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
217 do_test select6-3.11 {
220 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
223 } {1 1 2 2.5 2 4.5 5.5 3 8.5}
224 do_test select6-3.12 {
227 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
230 } {2.5 2 4.5 5.5 3 8.5}
231 do_test select6-3.13 {
234 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
237 } {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18 5 23}
238 do_test select6-3.14 {
240 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
243 } {1 1 2 2 4 3 5 5 8 4}
244 do_test select6-3.15 {
246 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
249 } {1 1 2 2 4 3 8 4 5 5}
251 do_test select6-4.1 {
254 (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
255 WHERE a<10 ORDER BY a;
258 do_test select6-4.2 {
260 SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
263 do_test select6-4.3 {
265 SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
268 do_test select6-4.4 {
270 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
273 do_test select6-4.5 {
275 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
279 do_test select6-5.1 {
282 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
283 (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
287 } {8 5 8 9 6 9 10 7 10}
288 do_test select6-5.2 {
291 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
292 (SELECT x AS 'b' FROM t1 WHERE y=4)
296 } {8 5 8 9 6 9 10 7 10}
298 # Tests of compound sub-selects
300 do_test select5-6.1 {
302 DELETE FROM t1 WHERE x>4;
306 do_test select6-6.2 {
309 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
312 } {1 2 3 4 11 12 13 14}
313 do_test select6-6.3 {
316 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
320 do_test select6-6.4 {
323 SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
327 do_test select6-6.5 {
330 SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
334 do_test select6-6.6 {
337 SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
342 # Subselects with no FROM clause
344 do_test select6-7.1 {
346 SELECT * FROM (SELECT 1)
349 do_test select6-7.2 {
351 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
354 do_test select6-7.3 {
356 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
359 do_test select6-7.4 {
361 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
363 } {c abc b 2 a 1 a 1 b 2 c abc}
365 # The following procedure compiles the SQL given as an argument and returns
366 # TRUE if that SQL uses any transient tables and returns FALSE if no
367 # transient tables are used. This is used to make sure that the
368 # sqliteFlattenSubquery() routine in select.c is doing its job.
371 return [expr 0>[lsearch [execsql "EXPLAIN $sql"] OpenTemp]]
374 # Check that the flattener works correctly for deeply nested subqueries
377 do_test select6-8.1 {
380 CREATE TABLE t3(p,q);
381 INSERT INTO t3 VALUES(1,11);
382 INSERT INTO t3 VALUES(2,22);
383 CREATE TABLE t4(q,r);
384 INSERT INTO t4 VALUES(11,111);
385 INSERT INTO t4 VALUES(22,222);
387 SELECT * FROM t3 NATURAL JOIN t4;
389 } {1 11 111 2 22 222}
390 do_test select6-8.2 {
392 SELECT y, p, q, r FROM
393 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
394 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
397 } {1 1 11 111 2 2 22 222 2 2 22 222}
398 do_test select6-8.3 {
400 SELECT y, p, q, r FROM
401 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
402 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
406 do_test select6-8.4 {
408 SELECT DISTINCT y, p, q, r FROM
409 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
410 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
413 } {1 1 11 111 2 2 22 222}
414 do_test select6-8.5 {
417 (SELECT y, p, q, r FROM
418 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
419 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
421 (SELECT r AS z FROM t4 WHERE q=11) AS f
425 do_test select6-8.6 {
428 (SELECT y, p, q, r FROM
429 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
430 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
432 (SELECT r AS z FROM t4 WHERE q=11) AS f