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
21 do_test aggnested-1.1 {
23 CREATE TABLE t1(a1 INTEGER);
24 INSERT INTO t1 VALUES(1), (2), (3);
25 CREATE TABLE t2(b1 INTEGER);
26 INSERT INTO t2 VALUES(4), (5);
27 SELECT (SELECT group_concat(a1,'x') FROM t2) FROM t1;
30 do_test aggnested-1.2 {
33 (SELECT group_concat(a1,'x') || '-' || group_concat(b1,'y') FROM t2)
37 do_test aggnested-1.3 {
39 SELECT (SELECT group_concat(b1,a1) FROM t2) FROM t1;
42 do_test aggnested-1.4 {
44 SELECT (SELECT group_concat(a1,b1) FROM t2) FROM t1;
49 # This test case is a copy of the one in
50 # http://www.mail-archive.com/sqlite-users@sqlite.org/msg70787.html
52 do_test aggnested-2.0 {
55 CREATE TABLE t1 (A1 INTEGER NOT NULL,A2 INTEGER NOT NULL,A3 INTEGER NOT
56 NULL,A4 INTEGER NOT NULL,PRIMARY KEY(A1));
57 REPLACE INTO t1 VALUES(1,11,111,1111);
58 REPLACE INTO t1 VALUES(2,22,222,2222);
59 REPLACE INTO t1 VALUES(3,33,333,3333);
60 CREATE TABLE t2 (B1 INTEGER NOT NULL,B2 INTEGER NOT NULL,B3 INTEGER NOT
61 NULL,B4 INTEGER NOT NULL,PRIMARY KEY(B1));
62 REPLACE INTO t2 VALUES(1,88,888,8888);
63 REPLACE INTO t2 VALUES(2,99,999,9999);
64 SELECT (SELECT GROUP_CONCAT(CASE WHEN a1=1 THEN'A' ELSE 'B' END) FROM t2),
68 } {A,B,B 3 33 333 3333}
71 ##################### Test cases for ticket [bfbf38e5e9956ac69f] ############
73 # This first test case is the original problem report:
74 do_test aggnested-3.0 {
77 aaa_id INTEGER PRIMARY KEY AUTOINCREMENT
80 rrr_id INTEGER PRIMARY KEY AUTOINCREMENT,
81 rrr_date INTEGER NOT NULL,
85 ttt_id INTEGER PRIMARY KEY AUTOINCREMENT,
86 target_aaa INTEGER NOT NULL,
87 source_aaa INTEGER NOT NULL
89 insert into AAA (aaa_id) values (2);
90 insert into TTT (ttt_id, target_aaa, source_aaa)
92 insert into TTT (ttt_id, target_aaa, source_aaa)
94 insert into RRR (rrr_id, rrr_date, rrr_aaa)
96 insert into RRR (rrr_id, rrr_date, rrr_aaa)
99 (SELECT sum(CASE WHEN (t.source_aaa == i.aaa_id) THEN 1 ELSE 0 END)
103 (SELECT curr.rrr_aaa as aaa_id
105 -- you also can comment out the next line
106 -- it causes segfault to happen after one row is outputted
107 INNER JOIN AAA a ON (curr.rrr_aaa = aaa_id)
108 LEFT JOIN RRR r ON (r.rrr_id <> 0 AND r.rrr_date < curr.rrr_date)
110 HAVING r.rrr_date IS NULL
115 # Further variants of the test case, as found in the ticket
117 do_test aggnested-3.1 {
119 DROP TABLE IF EXISTS t1;
120 DROP TABLE IF EXISTS t2;
122 id1 INTEGER PRIMARY KEY AUTOINCREMENT,
125 INSERT INTO t1 VALUES(4469,2),(4476,1);
127 id2 INTEGER PRIMARY KEY AUTOINCREMENT,
130 INSERT INTO t2 VALUES(0,1),(2,2);
132 (SELECT sum(value2==xyz) FROM t2)
134 (SELECT curr.value1 as xyz
135 FROM t1 AS curr LEFT JOIN t1 AS other
139 do_test aggnested-3.2 {
141 DROP TABLE IF EXISTS t1;
142 DROP TABLE IF EXISTS t2;
148 INSERT INTO t1 VALUES(4469,2,98),(4469,1,99),(4469,3,97);
152 INSERT INTO t2 VALUES(1);
154 (SELECT sum(value2==xyz) FROM t2)
156 (SELECT value1 as xyz, max(x1) AS pqr
160 (SELECT sum(value2<>xyz) FROM t2)
162 (SELECT value1 as xyz, max(x1) AS pqr
167 do_test aggnested-3.3 {
169 DROP TABLE IF EXISTS t1;
170 DROP TABLE IF EXISTS t2;
171 CREATE TABLE t1(id1, value1);
172 INSERT INTO t1 VALUES(4469,2),(4469,1);
173 CREATE TABLE t2 (value2);
174 INSERT INTO t2 VALUES(1);
175 SELECT (SELECT sum(value2=value1) FROM t2), max(value1)
181 # A batch of queries all doing approximately the same operation involving
182 # two nested aggregate queries.
184 do_test aggnested-3.11 {
186 DROP TABLE IF EXISTS t1;
187 DROP TABLE IF EXISTS t2;
188 CREATE TABLE t1(id1, value1);
189 INSERT INTO t1 VALUES(4469,12),(4469,11),(4470,34);
190 CREATE INDEX t1id1 ON t1(id1);
191 CREATE TABLE t2 (value2);
192 INSERT INTO t2 VALUES(12),(34),(34);
193 INSERT INTO t2 SELECT value2 FROM t2;
195 SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=max(value1))
200 do_test aggnested-3.12 {
202 SELECT max(value1), (SELECT count(*) FROM t2 WHERE value2=value1)
207 do_test aggnested-3.13 {
209 SELECT value1, (SELECT sum(value2=value1) FROM t2)
213 do_test aggnested-3.14 {
215 SELECT value1, (SELECT sum(value2=value1) FROM t2)
217 WHERE value1 IN (SELECT max(value1) FROM t1 GROUP BY id1);
220 do_test aggnested-3.15 {
221 # FIXME: If case 3.16 works, then this case really ought to work too...
223 SELECT max(value1), (SELECT sum(value2=max(value1)) FROM t2)
227 } {1 {misuse of aggregate function max()}}
228 do_test aggnested-3.16 {
230 SELECT max(value1), (SELECT sum(value2=value1) FROM t2)