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 script is testing correlated subqueries
14 # $Id: subquery.test,v 1.17 2009/01/09 01:12:28 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
25 do_test subquery-1.1 {
29 INSERT INTO t1 VALUES(1,2);
30 INSERT INTO t1 VALUES(3,4);
31 INSERT INTO t1 VALUES(5,6);
32 INSERT INTO t1 VALUES(7,8);
34 INSERT INTO t2 VALUES(1,1);
35 INSERT INTO t2 VALUES(3,9);
36 INSERT INTO t2 VALUES(5,25);
37 INSERT INTO t2 VALUES(7,49);
41 SELECT a, (SELECT y FROM t2 WHERE x=a) FROM t1 WHERE b<8
44 do_test subquery-1.2 {
46 UPDATE t1 SET b=b+(SELECT y FROM t2 WHERE x=a);
49 } {1 3 3 13 5 31 7 57}
51 do_test subquery-1.3 {
53 SELECT b FROM t1 WHERE EXISTS(SELECT * FROM t2 WHERE y=a)
56 do_test subquery-1.4 {
58 SELECT b FROM t1 WHERE NOT EXISTS(SELECT * FROM t2 WHERE y=a)
62 # Simple tests to make sure correlated subqueries in WHERE clauses
63 # are used by the query optimizer correctly.
64 do_test subquery-1.5 {
66 SELECT a, x FROM t1, t2 WHERE t1.a = (SELECT x);
69 do_test subquery-1.6 {
71 CREATE INDEX i1 ON t1(a);
72 SELECT a, x FROM t1, t2 WHERE t1.a = (SELECT x);
75 do_test subquery-1.7 {
77 SELECT a, x FROM t2, t1 WHERE t1.a = (SELECT x);
81 # Try an aggregate in both the subquery and the parent query.
82 do_test subquery-1.8 {
84 SELECT count(*) FROM t1 WHERE a > (SELECT count(*) FROM t2);
88 # Test a correlated subquery disables the "only open the index" optimization.
89 do_test subquery-1.9.1 {
91 SELECT (y*2)>b FROM t1, t2 WHERE a=x;
94 do_test subquery-1.9.2 {
96 SELECT a FROM t1 WHERE (SELECT (y*2)>b FROM t2 WHERE a=x);
100 # Test that the flattening optimization works with subquery expressions.
101 do_test subquery-1.10.1 {
103 SELECT (SELECT a), b FROM t1;
105 } {1 3 3 13 5 31 7 57}
106 do_test subquery-1.10.2 {
108 SELECT * FROM (SELECT (SELECT a), b FROM t1);
110 } {1 3 3 13 5 31 7 57}
111 do_test subquery-1.10.3 {
113 SELECT * FROM (SELECT (SELECT sum(a) FROM t1));
116 do_test subquery-1.10.4 {
118 CREATE TABLE t5 (val int, period text PRIMARY KEY);
119 INSERT INTO t5 VALUES(5, '2001-3');
120 INSERT INTO t5 VALUES(10, '2001-4');
121 INSERT INTO t5 VALUES(15, '2002-1');
122 INSERT INTO t5 VALUES(5, '2002-2');
123 INSERT INTO t5 VALUES(10, '2002-3');
124 INSERT INTO t5 VALUES(15, '2002-4');
125 INSERT INTO t5 VALUES(10, '2003-1');
126 INSERT INTO t5 VALUES(5, '2003-2');
127 INSERT INTO t5 VALUES(25, '2003-3');
128 INSERT INTO t5 VALUES(5, '2003-4');
133 (select sum(val) from t5 where period between a.period and '2002-4') vsum
134 FROM t5 a where a.period between '2002-1' and '2002-4')
137 } {2002-2 30 2002-3 25 2002-4 15}
138 do_test subquery-1.10.5 {
140 SELECT period, vsum from
142 (select sum(val) from t5 where period between a.period and '2002-4') vsum
143 FROM t5 a where a.period between '2002-1' and '2002-4')
146 } {2002-2 30 2002-3 25 2002-4 15}
147 do_test subquery-1.10.6 {
155 #------------------------------------------------------------------
156 # The following test cases - subquery-2.* - are not logically
157 # organized. They're here largely because they were failing during
158 # one stage of development of sub-queries.
160 do_test subquery-2.1 {
165 do_test subquery-2.2.1 {
167 CREATE TABLE t3(a PRIMARY KEY, b);
168 INSERT INTO t3 VALUES(1, 2);
169 INSERT INTO t3 VALUES(3, 1);
172 do_test subquery-2.2.2 {
174 SELECT * FROM t3 WHERE a IN (SELECT b FROM t3);
177 do_test subquery-2.2.3 {
182 do_test subquery-2.3.1 {
184 CREATE TABLE t3(a TEXT);
185 INSERT INTO t3 VALUES('10');
188 do_test subquery-2.3.2 {
190 SELECT a IN (10.0, 20) FROM t3;
193 do_test subquery-2.3.3 {
198 do_test subquery-2.4.1 {
200 CREATE TABLE t3(a TEXT);
201 INSERT INTO t3 VALUES('XX');
204 do_test subquery-2.4.2 {
206 SELECT count(*) FROM t3 WHERE a IN (SELECT 'XX')
209 do_test subquery-2.4.3 {
214 do_test subquery-2.5.1 {
216 CREATE TABLE t3(a INTEGER);
217 INSERT INTO t3 VALUES(10);
219 CREATE TABLE t4(x TEXT);
220 INSERT INTO t4 VALUES('10.0');
223 do_test subquery-2.5.2 {
224 # In the expr "x IN (SELECT a FROM t3)" the RHS of the IN operator
225 # has text affinity and the LHS has integer affinity. The rule is
226 # that we try to convert both sides to an integer before doing the
227 # comparision. Hence, the integer value 10 in t3 will compare equal
228 # to the string value '10.0' in t4 because the t4 value will be
229 # converted into an integer.
231 SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
234 do_test subquery-2.5.3.1 {
235 # The t4i index cannot be used to resolve the "x IN (...)" constraint
236 # because the constraint has integer affinity but t4i has text affinity.
238 CREATE INDEX t4i ON t4(x);
239 SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
242 do_test subquery-2.5.3.2 {
243 # Verify that the t4i index was not used in the previous query
246 SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
249 do_test subquery-2.5.4 {
256 #------------------------------------------------------------------
257 # The following test cases - subquery-3.* - test tickets that
258 # were raised during development of correlated subqueries.
263 do_test subquery-3.1 {
264 catchsql { DROP TABLE t1; }
265 catchsql { DROP TABLE t2; }
267 CREATE TABLE t1(a,b);
268 INSERT INTO t1 VALUES(1,2);
269 CREATE VIEW v1 AS SELECT b FROM t1 WHERE a>0;
270 CREATE TABLE t2(p,q);
271 INSERT INTO t2 VALUES(2,9);
272 SELECT * FROM v1 WHERE EXISTS(SELECT * FROM t2 WHERE p=v1.b);
275 do_test subquery-3.1.1 {
277 SELECT * FROM v1 WHERE EXISTS(SELECT 1);
281 catchsql { DROP TABLE t1; }
282 catchsql { DROP TABLE t2; }
284 CREATE TABLE t1(a,b);
285 INSERT INTO t1 VALUES(1,2);
286 CREATE TABLE t2(p,q);
287 INSERT INTO t2 VALUES(2,9);
292 do_test subquery-3.2 {
294 CREATE TABLE t1(a,b);
295 INSERT INTO t1 VALUES(1,2);
298 SELECT (SELECT t1.a) FROM t1;
302 # Test Cases subquery-3.3.* test correlated subqueries where the
303 # parent query is an aggregate query. Ticket #1105 is an example
306 do_test subquery-3.3.1 {
308 SELECT a, (SELECT b) FROM t1 GROUP BY a;
311 do_test subquery-3.3.2 {
312 catchsql {DROP TABLE t2}
314 CREATE TABLE t2(c, d);
315 INSERT INTO t2 VALUES(1, 'one');
316 INSERT INTO t2 VALUES(2, 'two');
317 SELECT a, (SELECT d FROM t2 WHERE a=c) FROM t1 GROUP BY a;
320 do_test subquery-3.3.3 {
322 INSERT INTO t1 VALUES(2, 4);
323 SELECT max(a), (SELECT d FROM t2 WHERE a=c) FROM t1;
326 do_test subquery-3.3.4 {
328 SELECT a, (SELECT (SELECT d FROM t2 WHERE a=c)) FROM t1 GROUP BY a;
331 do_test subquery-3.3.5 {
333 SELECT a, (SELECT count(*) FROM t2 WHERE a=c) FROM t1;
337 # The following tests check for aggregate subqueries in an aggregate
340 do_test subquery-3.4.1 {
342 CREATE TABLE t34(x,y);
343 INSERT INTO t34 VALUES(106,4), (107,3), (106,5), (107,5);
347 HAVING NOT EXISTS( SELECT b.x, avg(b.y)
350 HAVING avg(a.y) > avg(b.y));
353 do_test subquery-3.4.2 {
355 SELECT a.x, avg(a.y) AS avg1
358 HAVING NOT EXISTS( SELECT b.x, avg(b.y) AS avg2
364 do_test subquery-3.4.3 {
369 NOT EXISTS ( SELECT b.x, avg(b.y)
372 HAVING avg(a.y) > avg(b.y)),
373 EXISTS ( SELECT c.x, avg(c.y)
376 HAVING avg(a.y) > avg(c.y))
381 } {106 4.5 0 1 107 4.0 1 0}
383 do_test subquery-3.5.1 {
385 CREATE TABLE t35a(x); INSERT INTO t35a VALUES(1),(2),(3);
386 CREATE TABLE t35b(y); INSERT INTO t35b VALUES(98), (99);
387 SELECT max((SELECT avg(y) FROM t35b)) FROM t35a;
390 do_test subquery-3.5.2 {
392 SELECT max((SELECT count(y) FROM t35b)) FROM t35a;
395 do_test subquery-3.5.3 {
397 SELECT max((SELECT count() FROM t35b)) FROM t35a;
400 do_test subquery-3.5.4 {
402 SELECT max((SELECT count(x) FROM t35b)) FROM t35a;
404 } {1 {misuse of aggregate: count()}}
405 do_test subquery-3.5.5 {
407 SELECT max((SELECT count(x) FROM t35b)) FROM t35a;
409 } {1 {misuse of aggregate: count()}}
410 do_test subquery-3.5.6 {
412 SELECT max((SELECT a FROM (SELECT count(x) AS a FROM t35b))) FROM t35a;
414 } {1 {misuse of aggregate: count()}}
415 do_test subquery-3.5.7 {
417 SELECT max((SELECT a FROM (SELECT count(y) AS a FROM t35b))) FROM t35a;
422 #------------------------------------------------------------------
423 # These tests - subquery-4.* - use the TCL statement cache to try
424 # and expose bugs to do with re-using statements that have been
425 # passed to sqlite3_reset().
427 # One problem was that VDBE memory cells were not being initialized
428 # to NULL on the second and subsequent executions.
430 do_test subquery-4.1.1 {
432 SELECT (SELECT a FROM t1);
435 do_test subquery-4.2 {
438 SELECT (SELECT a FROM t1);
441 do_test subquery-4.2.1 {
443 CREATE TABLE t3(a PRIMARY KEY);
444 INSERT INTO t3 VALUES(10);
446 execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
448 do_test subquery-4.2.2 {
449 execsql {INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1)}
452 #------------------------------------------------------------------
453 # The subquery-5.* tests make sure string literals in double-quotes
454 # are handled efficiently. Double-quote literals are first checked
455 # to see if they match any column names. If there is not column name
456 # match then those literals are used a string constants. When a
457 # double-quoted string appears, we want to make sure that the search
458 # for a matching column name did not cause an otherwise static subquery
459 # to become a dynamic (correlated) subquery.
461 do_test subquery-5.1 {
462 proc callcntproc {n} {
467 db function callcnt callcntproc
469 CREATE TABLE t4(x,y);
470 INSERT INTO t4 VALUES('one',1);
471 INSERT INTO t4 VALUES('two',2);
472 INSERT INTO t4 VALUES('three',3);
473 INSERT INTO t4 VALUES('four',4);
474 CREATE TABLE t5(a,b);
475 INSERT INTO t5 VALUES(1,11);
476 INSERT INTO t5 VALUES(2,22);
477 INSERT INTO t5 VALUES(3,33);
478 INSERT INTO t5 VALUES(4,44);
479 SELECT b FROM t5 WHERE a IN
480 (SELECT callcnt(y)+0 FROM t4 WHERE x='two')
483 do_test subquery-5.2 {
484 # This is the key test. The subquery should have only run once. If
485 # The double-quoted identifier "two" were causing the subquery to be
486 # processed as a correlated subquery, then it would have run 4 times.
491 # Ticket #1380. Make sure correlated subqueries on an IN clause work
492 # correctly when the left-hand side of the IN operator is constant.
494 do_test subquery-6.1 {
497 SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=y)
499 } {one two three four}
500 do_test subquery-6.2 {
503 do_test subquery-6.3 {
506 SELECT x FROM t4 WHERE 1 IN (SELECT callcnt(count(*)) FROM t5 WHERE a=1)
508 } {one two three four}
509 do_test subquery-6.4 {
513 if 0 { ############# disable until we get #2652 fixed
514 # Ticket #2652. Allow aggregate functions of outer queries inside
515 # a non-aggregate subquery.
517 do_test subquery-7.1 {
520 INSERT INTO t7 VALUES(1);
521 INSERT INTO t7 VALUES(2);
522 INSERT INTO t7 VALUES(3);
524 INSERT INTO t8 VALUES(100);
525 INSERT INTO t8 VALUES(200);
526 INSERT INTO t8 VALUES(300);
528 INSERT INTO t9 VALUES(10000);
529 INSERT INTO t9 VALUES(20000);
530 INSERT INTO t9 VALUES(30000);
532 SELECT (SELECT c7+c8 FROM t7) FROM t8;
535 do_test subquery-7.2 {
537 SELECT (SELECT max(c7)+c8 FROM t7) FROM t8;
540 do_test subquery-7.3 {
542 SELECT (SELECT c7+max(c8) FROM t8) FROM t7
545 do_test subquery-7.4 {
547 SELECT (SELECT max(c7)+max(c8) FROM t8) FROM t7
550 do_test subquery-7.5 {
552 SELECT (SELECT c8 FROM t8 WHERE rowid=max(c7)) FROM t7
555 do_test subquery-7.6 {
557 SELECT (SELECT (SELECT max(c7+c8+c9) FROM t9) FROM t8) FROM t7
559 } {30101 30102 30103}
560 do_test subquery-7.7 {
562 SELECT (SELECT (SELECT c7+max(c8+c9) FROM t9) FROM t8) FROM t7
564 } {30101 30102 30103}
565 do_test subquery-7.8 {
567 SELECT (SELECT (SELECT max(c7)+c8+c9 FROM t9) FROM t8) FROM t7
570 do_test subquery-7.9 {
572 SELECT (SELECT (SELECT c7+max(c8)+c9 FROM t9) FROM t8) FROM t7
574 } {10301 10302 10303}
575 do_test subquery-7.10 {
577 SELECT (SELECT (SELECT c7+c8+max(c9) FROM t9) FROM t8) FROM t7
579 } {30101 30102 30103}
580 do_test subquery-7.11 {
582 SELECT (SELECT (SELECT max(c7)+max(c8)+max(c9) FROM t9) FROM t8) FROM t7
585 } ;############# Disabled
588 # Verify that a memory leak in the table column type and collation analysis
591 do_execsql_test subquery-8.1 {
592 CREATE TABLE t8(a TEXT, b INT);
593 SELECT (SELECT 0 FROM (SELECT * FROM t1)) AS x WHERE x;
594 SELECT (SELECT 0 FROM (SELECT * FROM (SELECT 0))) AS x WHERE x;
597 # 2022-01-12 https://sqlite.org/forum/forumpost/0ec80f12d02acb3f
600 do_execsql_test subquery-9.1 {
602 INSERT INTO t1 VALUES(1),(1),(1);
603 SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 100) FROM t1;
605 do_execsql_test subquery-9.2 {
606 SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 0) FROM t1;
608 do_execsql_test subquery-9.3 {
609 INSERT INTO t1 VALUES(2);
610 SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 1) FROM t1;
612 do_execsql_test subquery-9.4 {
613 SELECT (SELECT DISTINCT x FROM t1 ORDER BY +x LIMIT 1 OFFSET 2) FROM t1;