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.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Omit this whole file if the library is build without subquery support.
24 set ::testprefix select6
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)
173 } {1 1 1 2 2 3 3 4 7 4 8 15 5 5 20}
174 do_test select6-3.3 {
176 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
179 do_test select6-3.4 {
181 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
184 do_test select6-3.5 {
186 SELECT x,y,x+y FROM (SELECT avg(a) as 'x', avg(b) as 'y' FROM t2 WHERE a=4)
189 do_test select6-3.6 {
191 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
195 do_test select6-3.7 {
197 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1)
201 do_test select6-3.8 {
203 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
207 do_test select6-3.9 {
209 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', avg(y) as 'b' FROM t1 WHERE y=4)
213 do_test select6-3.10 {
215 SELECT a,b,a+b FROM (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
218 } {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}
219 do_test select6-3.11 {
222 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b)
225 } {1.0 1 2.0 2.5 2 4.5 5.5 3 8.5}
226 do_test select6-3.12 {
229 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
232 } {2.5 2 4.5 5.5 3 8.5}
233 do_test select6-3.13 {
236 (SELECT avg(x) as 'a', y as 'b' FROM t1 GROUP BY b HAVING a>1)
239 } {2.5 2 4.5 5.5 3 8.5 11.5 4 15.5 18.0 5 23.0}
240 do_test select6-3.14 {
242 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
245 } {1 1 2 2 4 3 5 5 8 4}
246 do_test select6-3.15 {
248 SELECT [count(*)],y FROM (SELECT count(*), y FROM t1 GROUP BY y)
251 } {1 1 2 2 4 3 8 4 5 5}
253 do_test select6-4.1 {
256 (SELECT x AS 'a', y AS 'b', x+y AS 'c' FROM t1 WHERE y=4)
257 WHERE a<10 ORDER BY a;
260 do_test select6-4.2 {
262 SELECT y FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
265 do_test select6-4.3 {
267 SELECT DISTINCT y FROM (SELECT y FROM t1) WHERE y<5 ORDER BY y
270 do_test select6-4.4 {
272 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1) WHERE y<5 ORDER BY y
275 do_test select6-4.5 {
277 SELECT avg(y) FROM (SELECT DISTINCT y FROM t1 WHERE y<5) ORDER BY y
281 do_test select6-5.1 {
284 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3) AS 'p',
285 (SELECT x AS 'b' FROM t1 WHERE y=4) AS 'q'
289 } {8 5 8 9 6 9 10 7 10}
290 do_test select6-5.2 {
293 (SELECT x+3 AS 'a', x FROM t1 WHERE y=3),
294 (SELECT x AS 'b' FROM t1 WHERE y=4)
298 } {8 5 8 9 6 9 10 7 10}
300 # Tests of compound sub-selects
302 do_test select6-6.1 {
304 DELETE FROM t1 WHERE x>4;
309 do_test select6-6.2 {
312 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+10 AS 'a' FROM t1
315 } {1 2 3 4 11 12 13 14}
316 do_test select6-6.3 {
319 SELECT x AS 'a' FROM t1 UNION ALL SELECT x+1 AS 'a' FROM t1
323 do_test select6-6.4 {
326 SELECT x AS 'a' FROM t1 UNION SELECT x+1 AS 'a' FROM t1
330 do_test select6-6.5 {
333 SELECT x AS 'a' FROM t1 INTERSECT SELECT x+1 AS 'a' FROM t1
337 do_test select6-6.6 {
340 SELECT x AS 'a' FROM t1 EXCEPT SELECT x*2 AS 'a' FROM t1
344 } ;# ifcapable compound
346 # Subselects with no FROM clause
348 do_test select6-7.1 {
350 SELECT * FROM (SELECT 1)
353 do_test select6-7.2 {
355 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c')
358 do_test select6-7.3 {
360 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 0)
363 do_test select6-7.4 {
365 SELECT c,b,a,* FROM (SELECT 1 AS 'a', 2 AS 'b', 'abc' AS 'c' WHERE 1)
367 } {c abc b 2 a 1 a 1 b 2 c abc}
369 # The remaining tests in this file depend on the EXPLAIN keyword.
370 # Skip these tests if EXPLAIN is disabled in the current build.
372 ifcapable {!explain} {
377 # The following procedure compiles the SQL given as an argument and returns
378 # TRUE if that SQL uses any transient tables and returns FALSE if no
379 # transient tables are used. This is used to make sure that the
380 # sqliteFlattenSubquery() routine in select.c is doing its job.
383 return [expr 0>[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]]
386 # Check that the flattener works correctly for deeply nested subqueries
389 do_test select6-8.1 {
392 CREATE TABLE t3(p,q);
393 INSERT INTO t3 VALUES(1,11);
394 INSERT INTO t3 VALUES(2,22);
395 CREATE TABLE t4(q,r);
396 INSERT INTO t4 VALUES(11,111);
397 INSERT INTO t4 VALUES(22,222);
399 SELECT * FROM t3 NATURAL JOIN t4;
401 } {1 11 111 2 22 222}
402 do_test select6-8.2 {
404 SELECT y, p, q, r FROM
405 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
406 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
409 } {1 1 11 111 2 2 22 222 2 2 22 222}
410 # If view support is omitted from the build, then so is the query
411 # "flattener". So omit this test and test select6-8.6 in that case.
413 do_test select6-8.3 {
415 SELECT y, p, q, r FROM
416 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
417 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
422 do_test select6-8.4 {
424 SELECT DISTINCT y, p, q, r FROM
425 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
426 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
429 } {1 1 11 111 2 2 22 222}
430 do_test select6-8.5 {
433 (SELECT y, p, q, r FROM
434 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
435 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
437 (SELECT r AS z FROM t4 WHERE q=11) AS f
442 do_test select6-8.6 {
445 (SELECT y, p, q, r FROM
446 (SELECT t1.y AS y, t2.b AS b FROM t1, t2 WHERE t1.x=t2.a) AS m,
447 (SELECT t3.p AS p, t3.q AS q, t4.r AS r FROM t3 NATURAL JOIN t4) as n
449 (SELECT r AS z FROM t4 WHERE q=11) AS f
457 do_test select6-9.1 {
459 SELECT a.x, b.x FROM t1 AS a, (SELECT x FROM t1 LIMIT 2) AS b
462 } {1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2}
463 do_test select6-9.2 {
465 SELECT x FROM (SELECT x FROM t1 LIMIT 2);
468 do_test select6-9.3 {
470 SELECT x FROM (SELECT x FROM t1 LIMIT 2 OFFSET 1);
473 do_test select6-9.4 {
475 SELECT x FROM (SELECT x FROM t1) LIMIT 2;
478 do_test select6-9.5 {
480 SELECT x FROM (SELECT x FROM t1) LIMIT 2 OFFSET 1;
483 do_test select6-9.6 {
485 SELECT x FROM (SELECT x FROM t1 LIMIT 2) LIMIT 3;
488 do_test select6-9.7 {
490 SELECT x FROM (SELECT x FROM t1 LIMIT -1) LIMIT 3;
493 do_test select6-9.8 {
495 SELECT x FROM (SELECT x FROM t1 LIMIT -1);
498 do_test select6-9.9 {
500 SELECT x FROM (SELECT x FROM t1 LIMIT -1 OFFSET 1);
503 do_test select6-9.10 {
505 SELECT x, y FROM (SELECT x, (SELECT 10+x) y FROM t1 LIMIT -1 OFFSET 1);
508 do_test select6-9.11 {
510 SELECT x, y FROM (SELECT x, (SELECT 10)+x y FROM t1 LIMIT -1 OFFSET 1);
515 #-------------------------------------------------------------------------
516 # Test that if a UNION ALL sub-query that would otherwise be eligible for
517 # flattening consists of two or more SELECT statements that do not all
518 # return the same number of result columns, the error is detected.
520 do_execsql_test 10.1 {
521 CREATE TABLE t(i,j,k);
526 set err [list 1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}]
528 do_execsql_test 10.2 {
529 SELECT * FROM (SELECT * FROM t), j;
531 do_catchsql_test 10.3 {
532 SELECT * FROM t UNION ALL SELECT * FROM j
534 do_catchsql_test 10.4 {
535 SELECT * FROM (SELECT i FROM t UNION ALL SELECT l, m FROM j)
537 do_catchsql_test 10.5 {
538 SELECT * FROM (SELECT j FROM t UNION ALL SELECT * FROM j)
540 do_catchsql_test 10.6 {
541 SELECT * FROM (SELECT * FROM t UNION ALL SELECT * FROM j)
543 do_catchsql_test 10.7 {
545 SELECT * FROM t UNION ALL
546 SELECT l,m,l FROM j UNION ALL
550 do_catchsql_test 10.8 {
552 SELECT * FROM k UNION ALL
553 SELECT * FROM t UNION ALL
558 # 2015-02-09 Ticket [2f7170d73bf9abf80339187aa3677dce3dbcd5ca]
559 # "misuse of aggregate" error if aggregate column from FROM
560 # subquery is used in correlated subquery
562 do_execsql_test 11.1 {
563 DROP TABLE IF EXISTS t1;
564 CREATE TABLE t1(w INT, x INT);
566 VALUES(1,10),(2,20),(3,30),
569 CREATE INDEX t1wx ON t1(w,x);
571 DROP TABLE IF EXISTS t2;
572 CREATE TABLE t2(w INT, y VARCHAR(8));
573 INSERT INTO t2(w,y) VALUES(1,'one'),(2,'two'),(3,'three'),(4,'four');
574 CREATE INDEX t2wy ON t2(w,y);
576 SELECT cnt, xyz, (SELECT y FROM t2 WHERE w=cnt), '|'
577 FROM (SELECT count(*) AS cnt, w AS xyz FROM t1 GROUP BY 2)
579 } {1 1 one | 2 2 two | 3 3 three |}
580 do_execsql_test 11.2 {
581 SELECT cnt, xyz, lower((SELECT y FROM t2 WHERE w=cnt)), '|'
582 FROM (SELECT count(*) AS cnt, w AS xyz FROM t1 GROUP BY 2)
584 } {1 1 one | 2 2 two | 3 3 three |}
585 do_execsql_test 11.3 {
587 FROM (SELECT count(*) AS cnt, w AS xyz FROM t1 GROUP BY 2)
588 WHERE (SELECT y FROM t2 WHERE w=cnt)!='two'
591 do_execsql_test 11.4 {
593 FROM (SELECT count(*) AS cnt, w AS xyz FROM t1 GROUP BY 2)
594 ORDER BY lower((SELECT y FROM t2 WHERE w=cnt));
595 } {1 1 | 3 3 | 2 2 |}
596 do_execsql_test 11.5 {
598 CASE WHEN (SELECT y FROM t2 WHERE w=cnt)=='two'
599 THEN 'aaa' ELSE 'bbb'
601 FROM (SELECT count(*) AS cnt, w AS xyz FROM t1 GROUP BY 2)
603 } {1 1 bbb | 2 2 aaa | 3 3 bbb |}
605 do_execsql_test 11.100 {
609 CREATE TABLE t2(y, z);
610 SELECT ( SELECT y FROM t2 WHERE z = cnt )
611 FROM ( SELECT count(*) AS cnt FROM t1 );
614 # 2019-05-29 ticket https://www.sqlite.org/src/info/c41afac34f15781f
615 # A LIMIT clause in a subquery is incorrectly applied to a subquery.
617 do_execsql_test 12.100 {
621 INSERT INTO t1 VALUES(1);
622 INSERT INTO t1 VALUES(2);
624 INSERT INTO t2 VALUES(3);
626 SELECT * FROM (SELECT * FROM t1 LIMIT 1)