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.
25 set ::testprefix select6
30 CREATE TABLE t1(x, y);
31 INSERT INTO t1 VALUES(1,1);
32 INSERT INTO t1 VALUES(2,2);
33 INSERT INTO t1 VALUES(3,2);
34 INSERT INTO t1 VALUES(4,3);
35 INSERT INTO t1 VALUES(5,3);
36 INSERT INTO t1 VALUES(6,3);
37 INSERT INTO t1 VALUES(7,3);
38 INSERT INTO t1 VALUES(8,4);
39 INSERT INTO t1 VALUES(9,4);
40 INSERT INTO t1 VALUES(10,4);
41 INSERT INTO t1 VALUES(11,4);
42 INSERT INTO t1 VALUES(12,4);
43 INSERT INTO t1 VALUES(13,4);
44 INSERT INTO t1 VALUES(14,4);
45 INSERT INTO t1 VALUES(15,4);
46 INSERT INTO t1 VALUES(16,5);
47 INSERT INTO t1 VALUES(17,5);
48 INSERT INTO t1 VALUES(18,5);
49 INSERT INTO t1 VALUES(19,5);
50 INSERT INTO t1 VALUES(20,5);
52 SELECT DISTINCT y FROM t1 ORDER BY y;
57 execsql2 {SELECT * FROM (SELECT x, y FROM t1 WHERE x<2)}
60 execsql {SELECT count(*) FROM (SELECT y FROM t1)}
63 execsql {SELECT count(*) FROM (SELECT DISTINCT y FROM t1)}
66 execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT y FROM t1))}
69 execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT y FROM t1))}
75 FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
76 (SELECT max(x),y FROM t1 GROUP BY y) as b
77 WHERE a.y=b.y ORDER BY a.y
79 } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
82 SELECT a.y, a.[count(*)], [max(x)], [count(*)]
83 FROM (SELECT count(*),y FROM t1 GROUP BY y) AS a,
84 (SELECT max(x),y FROM t1 GROUP BY y) as b
85 WHERE a.y=b.y ORDER BY a.y
87 } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
91 FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
92 (SELECT max(x) as r, y as s FROM t1 GROUP BY y) as b
95 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
98 SELECT q, p, r, b.[min(x)+y]
99 FROM (SELECT count(*) as p , y as q FROM t1 GROUP BY y) AS a,
100 (SELECT max(x) as r, y as s, min(x)+y FROM t1 GROUP BY y) as b
103 } {1 1 1 2 2 2 3 4 3 4 7 7 4 8 15 12 5 5 20 21}
105 do_test select6-2.0 {
107 CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
108 INSERT INTO t2 SELECT * FROM t1;
109 SELECT DISTINCT b FROM t2 ORDER BY b;
112 do_test select6-2.1 {
113 execsql2 {SELECT * FROM (SELECT a, b FROM t2 WHERE a<2)}
115 do_test select6-2.2 {
116 execsql {SELECT count(*) FROM (SELECT b FROM t2)}
118 do_test select6-2.3 {
119 execsql {SELECT count(*) FROM (SELECT DISTINCT b FROM t2)}
121 do_test select6-2.4 {
122 execsql {SELECT count(*) FROM (SELECT DISTINCT * FROM (SELECT b FROM t2))}
124 do_test select6-2.5 {
125 execsql {SELECT count(*) FROM (SELECT * FROM (SELECT DISTINCT b FROM t2))}
128 do_test select6-2.6 {
131 FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
132 (SELECT max(a),b FROM t2 GROUP BY b) as b
133 WHERE a.b=b.b ORDER BY a.b
135 } {1 1 1 1 2 2 3 2 4 3 7 3 8 4 15 4 5 5 20 5}
136 do_test select6-2.7 {
138 SELECT a.b, a.[count(*)], [max(a)], [count(*)]
139 FROM (SELECT count(*),b FROM t2 GROUP BY b) AS a,
140 (SELECT max(a),b FROM t2 GROUP BY b) as b
141 WHERE a.b=b.b ORDER BY a.b
143 } {1 1 1 1 2 2 3 2 3 4 7 4 4 8 15 8 5 5 20 5}
144 do_test select6-2.8 {
147 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY b) AS a,
148 (SELECT max(a) as r, b as s FROM t2 GROUP BY b) as b
151 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
152 do_test select6-2.9 {
155 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
156 (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
157 WHERE a.q=b.s ORDER BY a.q
159 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
161 do_test select6-3.1 {
163 SELECT * FROM (SELECT * FROM (SELECT * FROM t1 WHERE x=3));
166 do_test select6-3.2 {
169 (SELECT a.q, a.p, b.r
170 FROM (SELECT count(*) as p , b as q FROM t2 GROUP BY q) AS a,
171 (SELECT max(a) as r, b as s FROM t2 GROUP BY s) as b
172 WHERE a.q=b.s ORDER BY a.q)
175 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
176 do_test select6-3.3 {
178 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
181 do_test select6-3.4 {
183 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
186 do_test select6-3.5 {
188 SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
191 do_test select6-3.6 {
193 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
197 do_test select6-3.7 {
199 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
203 do_test select6-3.8 {
205 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
209 do_test select6-3.9 {
211 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
215 do_test select6-3.10 {
217 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
220 } {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}
221 do_test select6-3.11 {
224 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
227 } {1.0 1 2.0 2.5 2 4.5 5.5 3 8.5}
228 do_test select6-3.12 {
231 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
234 } {2.5 2 4.5 5.5 3 8.5}
235 do_test select6-3.13 {
238 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
241 } {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18.0 5 23.0}
242 do_test select6-3.14 {
244 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
247 } {1 1 2 2 4 3 5 5 8 4}
248 do_test select6-3.15 {
250 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
253 } {1 1 2 2 4 3 8 4 5 5}
255 do_test select6-4.1 {
258 (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
259 WHERE a<10 ORDER BY a;
262 do_test select6-4.2 {
264 SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
267 do_test select6-4.3 {
269 SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
272 do_test select6-4.4 {
274 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
277 do_test select6-4.5 {
279 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
283 do_test select6-5.1 {
286 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
287 (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
291 } {8 5 8 9 6 9 10 7 10}
292 do_test select6-5.2 {
295 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
296 (SELECT x AS 'b' FROM t1 WHERE y=4)
300 } {8 5 8 9 6 9 10 7 10}
302 # Tests of compound sub-selects
304 do_test select6-6.1 {
306 DELETE FROM t1 WHERE x>4;
311 do_test select6-6.2 {
314 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
317 } {1 2 3 4 11 12 13 14}
318 do_test select6-6.3 {
321 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
325 do_test select6-6.4 {
328 SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
332 do_test select6-6.5 {
335 SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
339 do_test select6-6.6 {
342 SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
346 } ;# ifcapable compound
348 # Subselects with no FROM clause
350 do_test select6-7.1 {
352 SELECT * FROM (SELECT 1)
355 do_test select6-7.2 {
357 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
360 do_test select6-7.3 {
362 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
365 do_test select6-7.4 {
367 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
369 } {c abc b 2 a 1 a 1 b 2 c abc}
371 # The remaining tests in this file depend on the EXPLAIN keyword.
372 # Skip these tests if EXPLAIN is disabled in the current build.
374 ifcapable {!explain} {
379 # The following procedure compiles the SQL given as an argument and returns
380 # TRUE if that SQL uses any transient tables and returns FALSE if no
381 # transient tables are used. This is used to make sure that the
382 # sqliteFlattenSubquery() routine in select.c is doing its job.
385 return [expr 0>[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]]
388 # Check that the flattener works correctly for deeply nested subqueries
391 do_test select6-8.1 {
394 CREATE TABLE t3(p,q);
395 INSERT INTO t3 VALUES(1,11);
396 INSERT INTO t3 VALUES(2,22);
397 CREATE TABLE t4(q,r);
398 INSERT INTO t4 VALUES(11,111);
399 INSERT INTO t4 VALUES(22,222);
401 SELECT * FROM t3 NATURAL JOIN t4;
403 } {1 11 111 2 22 222}
404 do_test select6-8.2 {
406 SELECT y, p, q, r FROM
407 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
408 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
411 } {1 1 11 111 2 2 22 222 2 2 22 222}
412 # If view support is omitted from the build, then so is the query
413 # "flattener". So omit this test and test select6-8.6 in that case.
415 do_test select6-8.3 {
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
424 do_test select6-8.4 {
426 SELECT DISTINCT y, p, q, r FROM
427 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
428 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
431 } {1 1 11 111 2 2 22 222}
432 do_test select6-8.5 {
435 (SELECT y, p, q, r FROM
436 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
437 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
439 (SELECT r AS z FROM t4 WHERE q=11) AS f
444 do_test select6-8.6 {
447 (SELECT y, p, q, r FROM
448 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
449 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
451 (SELECT r AS z FROM t4 WHERE q=11) AS f
459 do_test select6-9.1 {
461 SELECT a.x, b.x FROM t1 AS a, (SELECT x FROM t1 LIMIT 2) AS b
464 } {1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2}
465 do_test select6-9.2 {
467 SELECT x FROM (SELECT x FROM t1 LIMIT 2);
470 do_test select6-9.3 {
472 SELECT x FROM (SELECT x FROM t1 LIMIT 2 OFFSET 1);
475 do_test select6-9.4 {
477 SELECT x FROM (SELECT x FROM t1) LIMIT 2;
480 do_test select6-9.5 {
482 SELECT x FROM (SELECT x FROM t1) LIMIT 2 OFFSET 1;
485 do_test select6-9.6 {
487 SELECT x FROM (SELECT x FROM t1 LIMIT 2) LIMIT 3;
490 do_test select6-9.7 {
492 SELECT x FROM (SELECT x FROM t1 LIMIT -1) LIMIT 3;
495 do_test select6-9.8 {
497 SELECT x FROM (SELECT x FROM t1 LIMIT -1);
500 do_test select6-9.9 {
502 SELECT x FROM (SELECT x FROM t1 LIMIT -1 OFFSET 1);
505 do_test select6-9.10 {
507 SELECT x, y FROM (SELECT x, (SELECT 10+x) y FROM t1 LIMIT -1 OFFSET 1);
510 do_test select6-9.11 {
512 SELECT x, y FROM (SELECT x, (SELECT 10)+x y FROM t1 LIMIT -1 OFFSET 1);
517 #-------------------------------------------------------------------------
518 # Test that if a UNION ALL sub-query that would otherwise be eligible for
519 # flattening consists of two or more SELECT statements that do not all
520 # return the same number of result columns, the error is detected.
522 do_execsql_test 10.1 {
523 CREATE TABLE t(i,j,k);
528 set err [list 1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}]
530 do_execsql_test 10.2 {
531 SELECT * FROM (SELECT * FROM t), j;
533 do_catchsql_test 10.3 {
534 SELECT * FROM t UNION ALL SELECT * FROM j
536 do_catchsql_test 10.4 {
537 SELECT * FROM (SELECT i FROM t UNION ALL SELECT l, m FROM j)
539 do_catchsql_test 10.5 {
540 SELECT * FROM (SELECT j FROM t UNION ALL SELECT * FROM j)
542 do_catchsql_test 10.6 {
543 SELECT * FROM (SELECT * FROM t UNION ALL SELECT * FROM j)
545 do_catchsql_test 10.7 {
547 SELECT * FROM t UNION ALL
548 SELECT l,m,l FROM j UNION ALL
552 do_catchsql_test 10.8 {
554 SELECT * FROM k UNION ALL
555 SELECT * FROM t UNION ALL