3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing SELECT statements that contain
13 # subqueries in their FROM clause.
15 # $Id: select6.test,v 1.29 2009/01/09 01:12:28 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Omit this whole file if the library is build without subquery support.
29 CREATE TABLE t1(x, y);
30 INSERT INTO t1 VALUES(1,1);
31 INSERT INTO t1 VALUES(2,2);
32 INSERT INTO t1 VALUES(3,2);
33 INSERT INTO t1 VALUES(4,3);
34 INSERT INTO t1 VALUES(5,3);
35 INSERT INTO t1 VALUES(6,3);
36 INSERT INTO t1 VALUES(7,3);
37 INSERT INTO t1 VALUES(8,4);
38 INSERT INTO t1 VALUES(9,4);
39 INSERT INTO t1 VALUES(10,4);
40 INSERT INTO t1 VALUES(11,4);
41 INSERT INTO t1 VALUES(12,4);
42 INSERT INTO t1 VALUES(13,4);
43 INSERT INTO t1 VALUES(14,4);
44 INSERT INTO t1 VALUES(15,4);
45 INSERT INTO t1 VALUES(16,5);
46 INSERT INTO t1 VALUES(17,5);
47 INSERT INTO t1 VALUES(18,5);
48 INSERT INTO t1 VALUES(19,5);
49 INSERT INTO t1 VALUES(20,5);
51 SELECT DISTINCT y FROM t1 ORDER BY y;
56 execsql2 {SELECT * FROM (SELECT x, y FROM t1 WHERE x<2)}
59 execsql {SELECT count(*) FROM (SELECT y FROM t1)}
62 execsql {SELECT count(*) FROM (SELECT DISTINCT y FROM t1)}
65 execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT y FROM t1))}
68 execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT y FROM t1))}
74 FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
75 (SELECT max(x),y FROM t1 GROUP BY y) as b
76 WHERE a.y=b.y ORDER BY a.y
78 } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
81 SELECT a.y, a.[count(*)], [max(x)], [count(*)]
82 FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
83 (SELECT max(x),y FROM t1 GROUP BY y) as b
84 WHERE a.y=b.y ORDER BY a.y
86 } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
90 FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
91 (SELECT max(x) as r, y as s FROM t1 GROUP BY y) as b
94 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
97 SELECT q, p, r, b.[min(x)+y]
98 FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
99 (SELECT max(x) as r, y as s, min(x)+y FROM t1 GROUP BY y) as b
102 } {1 1 1 2 2 2 3 4 3 4 7 7 4 8 15 12 5 5 20 21}
104 do_test select6-2.0 {
106 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
107 INSERT INTO t2 SELECT * FROM t1;
108 SELECT DISTINCT b FROM t2 ORDER BY b;
111 do_test select6-2.1 {
112 execsql2 {SELECT * FROM (SELECT a, b FROM t2 WHERE a<2)}
114 do_test select6-2.2 {
115 execsql {SELECT count(*) FROM (SELECT b FROM t2)}
117 do_test select6-2.3 {
118 execsql {SELECT count(*) FROM (SELECT DISTINCT b FROM t2)}
120 do_test select6-2.4 {
121 execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT b FROM t2))}
123 do_test select6-2.5 {
124 execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT b FROM t2))}
127 do_test select6-2.6 {
130 FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
131 (SELECT max(a),b FROM t2 GROUP BY b) as b
132 WHERE a.b=b.b ORDER BY a.b
134 } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
135 do_test select6-2.7 {
137 SELECT a.b, a.[count(*)], [max(a)], [count(*)]
138 FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
139 (SELECT max(a),b FROM t2 GROUP BY b) as b
140 WHERE a.b=b.b ORDER BY a.b
142 } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
143 do_test select6-2.8 {
146 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY b) AS a,
147 (SELECT max(a) as r, b as s FROM t2 GROUP BY b) as b
150 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
151 do_test select6-2.9 {
154 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
155 (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
156 WHERE a.q=b.s ORDER BY a.q
158 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
160 do_test select6-3.1 {
162 SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE x=3));
165 do_test select6-3.2 {
168 (SELECT a.q, a.p, b.r
169 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
170 (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
171 WHERE a.q=b.s ORDER BY a.q)
174 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
175 do_test select6-3.3 {
177 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
180 do_test select6-3.4 {
182 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
185 do_test select6-3.5 {
187 SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
190 do_test select6-3.6 {
192 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
196 do_test select6-3.7 {
198 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
202 do_test select6-3.8 {
204 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
208 do_test select6-3.9 {
210 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
214 do_test select6-3.10 {
216 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
219 } {1.0 1 2.0 2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18.0 5 23.0}
220 do_test select6-3.11 {
223 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
226 } {1.0 1 2.0 2.5 2 4.5 5.5 3 8.5}
227 do_test select6-3.12 {
230 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
233 } {2.5 2 4.5 5.5 3 8.5}
234 do_test select6-3.13 {
237 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
240 } {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18.0 5 23.0}
241 do_test select6-3.14 {
243 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
246 } {1 1 2 2 4 3 5 5 8 4}
247 do_test select6-3.15 {
249 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
252 } {1 1 2 2 4 3 8 4 5 5}
254 do_test select6-4.1 {
257 (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
258 WHERE a<10 ORDER BY a;
261 do_test select6-4.2 {
263 SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
266 do_test select6-4.3 {
268 SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
271 do_test select6-4.4 {
273 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
276 do_test select6-4.5 {
278 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
282 do_test select6-5.1 {
285 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
286 (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
290 } {8 5 8 9 6 9 10 7 10}
291 do_test select6-5.2 {
294 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
295 (SELECT x AS 'b' FROM t1 WHERE y=4)
299 } {8 5 8 9 6 9 10 7 10}
301 # Tests of compound sub-selects
303 do_test select6-6.1 {
305 DELETE FROM t1 WHERE x>4;
310 do_test select6-6.2 {
313 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
316 } {1 2 3 4 11 12 13 14}
317 do_test select6-6.3 {
320 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
324 do_test select6-6.4 {
327 SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
331 do_test select6-6.5 {
334 SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
338 do_test select6-6.6 {
341 SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
345 } ;# ifcapable compound
347 # Subselects with no FROM clause
349 do_test select6-7.1 {
351 SELECT * FROM (SELECT 1)
354 do_test select6-7.2 {
356 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
359 do_test select6-7.3 {
361 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
364 do_test select6-7.4 {
366 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
368 } {c abc b 2 a 1 a 1 b 2 c abc}
370 # The remaining tests in this file depend on the EXPLAIN keyword.
371 # Skip these tests if EXPLAIN is disabled in the current build.
373 ifcapable {!explain} {
378 # The following procedure compiles the SQL given as an argument and returns
379 # TRUE if that SQL uses any transient tables and returns FALSE if no
380 # transient tables are used. This is used to make sure that the
381 # sqliteFlattenSubquery() routine in select.c is doing its job.
384 return [expr 0>[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]]
387 # Check that the flattener works correctly for deeply nested subqueries
390 do_test select6-8.1 {
393 CREATE TABLE t3(p,q);
394 INSERT INTO t3 VALUES(1,11);
395 INSERT INTO t3 VALUES(2,22);
396 CREATE TABLE t4(q,r);
397 INSERT INTO t4 VALUES(11,111);
398 INSERT INTO t4 VALUES(22,222);
400 SELECT * FROM t3 NATURAL JOIN t4;
402 } {1 11 111 2 22 222}
403 do_test select6-8.2 {
405 SELECT y, p, q, r FROM
406 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
407 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
410 } {1 1 11 111 2 2 22 222 2 2 22 222}
411 # If view support is omitted from the build, then so is the query
412 # "flattener". So omit this test and test select6-8.6 in that case.
414 do_test select6-8.3 {
416 SELECT y, p, q, r FROM
417 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
418 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
423 do_test select6-8.4 {
425 SELECT DISTINCT y, p, q, r FROM
426 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
427 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
430 } {1 1 11 111 2 2 22 222}
431 do_test select6-8.5 {
434 (SELECT y, p, q, r FROM
435 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
436 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
438 (SELECT r AS z FROM t4 WHERE q=11) AS f
443 do_test select6-8.6 {
446 (SELECT y, p, q, r FROM
447 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
448 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
450 (SELECT r AS z FROM t4 WHERE q=11) AS f
458 do_test select6-9.1 {
460 SELECT a.x, b.x FROM t1 AS a, (SELECT x FROM t1 LIMIT 2) AS b
463 } {1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2}
464 do_test select6-9.2 {
466 SELECT x FROM (SELECT x FROM t1 LIMIT 2);
469 do_test select6-9.3 {
471 SELECT x FROM (SELECT x FROM t1 LIMIT 2 OFFSET 1);
474 do_test select6-9.4 {
476 SELECT x FROM (SELECT x FROM t1) LIMIT 2;
479 do_test select6-9.5 {
481 SELECT x FROM (SELECT x FROM t1) LIMIT 2 OFFSET 1;
484 do_test select6-9.6 {
486 SELECT x FROM (SELECT x FROM t1 LIMIT 2) LIMIT 3;
489 do_test select6-9.7 {
491 SELECT x FROM (SELECT x FROM t1 LIMIT -1) LIMIT 3;
494 do_test select6-9.8 {
496 SELECT x FROM (SELECT x FROM t1 LIMIT -1);
499 do_test select6-9.9 {
501 SELECT x FROM (SELECT x FROM t1 LIMIT -1 OFFSET 1);
504 do_test select6-9.10 {
506 SELECT x, y FROM (SELECT x, (SELECT 10+x) y FROM t1 LIMIT -1 OFFSET 1);
509 do_test select6-9.11 {
511 SELECT x, y FROM (SELECT x, (SELECT 10)+x y FROM t1 LIMIT -1 OFFSET 1);