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.
13 # This file implements tests for processing aggregate queries with
14 # subqueries in which the subqueries hold the aggregate functions
15 # or in which the subqueries are themselves aggregate queries
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
20 set testprefix aggnested
22 do_test aggnested-1.1 {
24 CREATE TABLE t1(a1 INTEGER);
25 INSERT INTO t1 VALUES(1), (2), (3);
26 CREATE TABLE t2(b1 INTEGER);
27 INSERT INTO t2 VALUES(4), (5);
28 SELECT (SELECT group_concat(a1,'x') FROM t2) FROM t1;
31 do_test aggnested-1.2 {
34 (SELECT group_concat(a1,'x') || '-' || group_concat(b1,'y') FROM t2)
38 do_test aggnested-1.3 {
40 SELECT (SELECT group_concat(b1,a1) FROM t2) FROM t1;
43 do_test aggnested-1.4 {
45 SELECT (SELECT group_concat(a1,b1) FROM t2) FROM t1;
50 # This test case is a copy of the one in
51 # http://www.mail-archive.com/sqlite-users@sqlite.org/msg70787.html
53 do_test aggnested-2.0 {
56 CREATE TABLE t1 (A1 INTEGER NOT NULL,A2 INTEGER NOT NULL,A3 INTEGER NOT
57 NULL,A4 INTEGER NOT NULL,PRIMARY KEY(A1));
58 REPLACE INTO t1 VALUES(1,11,111,1111);
59 REPLACE INTO t1 VALUES(2,22,222,2222);
60 REPLACE INTO t1 VALUES(3,33,333,3333);
61 CREATE TABLE t2 (B1 INTEGER NOT NULL,B2 INTEGER NOT NULL,B3 INTEGER NOT
62 NULL,B4 INTEGER NOT NULL,PRIMARY KEY(B1));
63 REPLACE INTO t2 VALUES(1,88,888,8888);
64 REPLACE INTO t2 VALUES(2,99,999,9999);
65 SELECT (SELECT GROUP_CONCAT(CASE WHEN a1=1 THEN'A' ELSE 'B' END) FROM t2),
69 } {A,B,B 1 11 111 1111}
72 ##################### Test cases for ticket [bfbf38e5e9956ac69f] ############
74 # This first test case is the original problem report:
75 do_test aggnested-3.0 {
78 aaa_id INTEGER PRIMARY KEY AUTOINCREMENT
81 rrr_id INTEGER PRIMARY KEY AUTOINCREMENT,
82 rrr_date INTEGER NOT NULL,
86 ttt_id INTEGER PRIMARY KEY AUTOINCREMENT,
87 target_aaa INTEGER NOT NULL,
88 source_aaa INTEGER NOT NULL
90 insert into AAA (aaa_id) values (2);
91 insert into TTT (ttt_id, target_aaa, source_aaa)
93 insert into TTT (ttt_id, target_aaa, source_aaa)
95 insert into RRR (rrr_id, rrr_date, rrr_aaa)
97 insert into RRR (rrr_id, rrr_date, rrr_aaa)
100 (SELECT sum(CASE WHEN (t.source_aaa == i.aaa_id) THEN 1 ELSE 0 END)
104 (SELECT curr.rrr_aaa as aaa_id
106 -- you also can comment out the next line
107 -- it causes segfault to happen after one row is outputted
108 INNER JOIN AAA a ON (curr.rrr_aaa = aaa_id)
109 LEFT JOIN RRR r ON (r.rrr_id <> 0 AND r.rrr_date < curr.rrr_date)
111 HAVING r.rrr_date IS NULL
116 # Further variants of the test case, as found in the ticket
118 do_test aggnested-3.1 {
120 DROP TABLE IF EXISTS t1;
121 DROP TABLE IF EXISTS t2;
123 id1 INTEGER PRIMARY KEY AUTOINCREMENT,
126 INSERT INTO t1 VALUES(4469,2),(4476,1);
128 id2 INTEGER PRIMARY KEY AUTOINCREMENT,
131 INSERT INTO t2 VALUES(0,1),(2,2);
133 (SELECT sum(value2==xyz) FROM t2)
135 (SELECT curr.value1 as xyz
136 FROM t1 AS curr LEFT JOIN t1 AS other
140 do_test aggnested-3.2 {
142 DROP TABLE IF EXISTS t1;
143 DROP TABLE IF EXISTS t2;
149 INSERT INTO t1 VALUES(4469,2,98),(4469,1,99),(4469,3,97);
153 INSERT INTO t2 VALUES(1);
155 (SELECT sum(value2==xyz) FROM t2)
157 (SELECT value1 as xyz, max(x1) AS pqr
161 (SELECT sum(value2<>xyz) FROM t2)
163 (SELECT value1 as xyz, max(x1) AS pqr
168 do_test aggnested-3.3 {
170 DROP TABLE IF EXISTS t1;
171 DROP TABLE IF EXISTS t2;
172 CREATE TABLE t1(id1, value1);
173 INSERT INTO t1 VALUES(4469,2),(4469,1);
174 CREATE TABLE t2 (value2);
175 INSERT INTO t2 VALUES(1);
176 SELECT (SELECT sum(value2=value1) FROM t2), max(value1)
182 # A batch of queries all doing approximately the same operation involving
183 # two nested aggregate queries.
185 do_test aggnested-3.11 {
187 DROP TABLE IF EXISTS t1;
188 DROP TABLE IF EXISTS t2;
189 CREATE TABLE t1(id1, value1);
190 INSERT INTO t1 VALUES(4469,12),(4469,11),(4470,34);
191 CREATE INDEX t1id1 ON t1(id1);
192 CREATE TABLE t2 (value2);
193 INSERT INTO t2 VALUES(12),(34),(34);
194 INSERT INTO t2 SELECT value2 FROM t2;
196 SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=max(value1))
201 do_test aggnested-3.12 {
203 SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=value1)
208 do_test aggnested-3.13 {
210 SELECT value1, (SELECT sum(value2=value1) FROM t2)
214 do_test aggnested-3.14 {
216 SELECT value1, (SELECT sum(value2=value1) FROM t2)
218 WHERE value1 IN (SELECT max(value1) FROM t1 GROUP BY id1);
221 do_test aggnested-3.15 {
222 # FIXME: If case 3.16 works, then this case really ought to work too...
224 SELECT max(value1), (SELECT sum(value2=max(value1)) FROM t2)
228 } {1 {misuse of aggregate function max()}}
229 do_test aggnested-3.16 {
231 SELECT max(value1), (SELECT sum(value2=value1) FROM t2)
238 # Problem found by dbsqlfuzz
240 do_execsql_test aggnested-4.1 {
241 DROP TABLE IF EXISTS aa;
242 DROP TABLE IF EXISTS bb;
243 CREATE TABLE aa(x INT); INSERT INTO aa(x) VALUES(123);
244 CREATE TABLE bb(y INT); INSERT INTO bb(y) VALUES(456);
245 SELECT (SELECT sum(x+(SELECT y)) FROM bb) FROM aa;
247 do_execsql_test aggnested-4.2 {
248 SELECT (SELECT sum(x+y) FROM bb) FROM aa;
250 do_execsql_test aggnested-4.3 {
251 DROP TABLE IF EXISTS tx;
252 DROP TABLE IF EXISTS ty;
253 CREATE TABLE tx(x INT);
254 INSERT INTO tx VALUES(1),(2),(3),(4),(5);
255 CREATE TABLE ty(y INT);
256 INSERT INTO ty VALUES(91),(92),(93);
257 SELECT min((SELECT count(y) FROM ty)) FROM tx;
259 do_execsql_test aggnested-4.4 {
260 SELECT max((SELECT a FROM (SELECT count(*) AS a FROM ty) AS s)) FROM tx;
263 #--------------------------------------------------------------------------
266 do_execsql_test 5.0 {
267 CREATE TABLE x1(a, b);
268 INSERT INTO x1 VALUES(1, 2);
270 INSERT INTO x2 VALUES(NULL), (NULL), (NULL);
273 # At one point, aggregate "total()" in the query below was being processed
274 # as part of the outer SELECT, not as part of the sub-select with no FROM
276 do_execsql_test 5.1 {
277 SELECT ( SELECT total( (SELECT b FROM x1) ) ) FROM x2;
280 do_execsql_test 5.2 {
281 SELECT ( SELECT total( (SELECT 2 FROM x1) ) ) FROM x2;
284 do_execsql_test 5.3 {
289 do_execsql_test 5.4 {
291 SELECT max(b) LIMIT (
292 SELECT total( (SELECT a FROM t1) )
298 do_execsql_test 5.5 {
301 SELECT(SELECT(SELECT group_concat(b, b)
302 LIMIT(SELECT 0.100000 *
303 AVG(DISTINCT(SELECT 0 FROM a ORDER BY b, b, b))))
305 b, b) FROM a EXCEPT SELECT b FROM a ORDER BY b,