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 aggregate functions and the
13 # GROUP BY and HAVING clauses of SELECT statements.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
19 # Build some test data
22 CREATE TABLE t1(x int, y int);
25 for {set i 1} {$i<32} {incr i} {
26 for {set j 0} {(1<<$j)<$i} {incr j} {}
27 execsql "INSERT INTO t1 VALUES([expr {32-$i}],[expr {10-$j}])"
34 execsql {SELECT DISTINCT y FROM t1 ORDER BY y}
37 # Sort by an aggregate function.
40 execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y}
41 } {5 15 6 8 7 4 8 2 9 1 10 1}
43 execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y}
44 } {9 1 10 1 8 2 7 4 6 8 5 15}
46 execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y}
47 } {1 9 1 10 2 8 4 7 8 6 15 5}
49 # Some error messages associated with aggregates and GROUP BY
51 do_test select5-2.1.1 {
53 SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
55 } {1 {no such column: z}}
56 do_test select5-2.1.2 {
58 SELECT y, count(*) FROM t1 GROUP BY temp.t1.y ORDER BY y
60 } {1 {no such column: temp.t1.y}}
62 set v [catch {execsql {
63 SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
66 } {1 {no such function: z}}
68 set v [catch {execsql {
69 SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
74 set v [catch {execsql {
75 SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
78 } {1 {no such function: z}}
80 set v [catch {execsql {
81 SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
84 } {1 {no such column: z}}
86 # Get the Agg function to rehash in vdbe.c
90 SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
92 } {1 1 5.0 2 1 5.0 3 1 5.0}
94 # Run various aggregate functions when the count is zero.
98 SELECT avg(x) FROM t1 WHERE x>100
101 do_test select5-4.2 {
103 SELECT count(x) FROM t1 WHERE x>100
106 do_test select5-4.3 {
108 SELECT min(x) FROM t1 WHERE x>100
111 do_test select5-4.4 {
113 SELECT max(x) FROM t1 WHERE x>100
116 do_test select5-4.5 {
118 SELECT sum(x) FROM t1 WHERE x>100
122 # Some tests for queries with a GROUP BY clause but no aggregate functions.
124 # Note: The query in test cases 5.1 through 5.5 are not legal SQL. So if the
125 # implementation changes in the future and it returns different results,
126 # this is not such a big deal.
128 do_test select5-5.1 {
130 CREATE TABLE t2(a, b, c);
131 INSERT INTO t2 VALUES(1, 2, 3);
132 INSERT INTO t2 VALUES(1, 4, 5);
133 INSERT INTO t2 VALUES(6, 4, 7);
134 CREATE INDEX t2_idx ON t2(a);
137 do_test select5-5.2 {
139 SELECT a FROM t2 GROUP BY a;
142 do_test select5-5.3 {
144 SELECT a FROM t2 WHERE a>2 GROUP BY a;
147 do_test select5-5.4 {
149 SELECT a, b FROM t2 GROUP BY a, b;
152 do_test select5-5.5 {
154 SELECT a, b FROM t2 GROUP BY a;
158 # Test rendering of columns for the GROUP BY clause.
160 do_test select5-5.11 {
162 SELECT max(c), b*a, b, a FROM t2 GROUP BY b*a, b, a
164 } {3 2 2 1 5 4 4 1 7 24 4 6}
166 # NULL compare equal to each other for the purposes of processing
167 # the GROUP BY clause.
169 do_test select5-6.1 {
171 CREATE TABLE t3(x,y);
172 INSERT INTO t3 VALUES(1,NULL);
173 INSERT INTO t3 VALUES(2,NULL);
174 INSERT INTO t3 VALUES(3,4);
175 SELECT count(x), y FROM t3 GROUP BY y ORDER BY 1
178 do_test select5-6.2 {
180 CREATE TABLE t4(x,y,z);
181 INSERT INTO t4 VALUES(1,2,NULL);
182 INSERT INTO t4 VALUES(2,3,NULL);
183 INSERT INTO t4 VALUES(3,NULL,5);
184 INSERT INTO t4 VALUES(4,NULL,6);
185 INSERT INTO t4 VALUES(4,NULL,6);
186 INSERT INTO t4 VALUES(5,NULL,NULL);
187 INSERT INTO t4 VALUES(5,NULL,NULL);
188 INSERT INTO t4 VALUES(6,7,8);
189 SELECT max(x), count(x), y, z FROM t4 GROUP BY y, z ORDER BY 1
191 } {1 1 2 {} 2 1 3 {} 3 1 {} 5 4 2 {} 6 5 2 {} {} 6 1 7 8}
193 do_test select5-7.2 {
195 SELECT count(*), count(x) as cnt FROM t4 GROUP BY y ORDER BY cnt;
201 do_test select5-8.1 {
203 CREATE TABLE t8a(a,b);
205 INSERT INTO t8a VALUES('one', 1);
206 INSERT INTO t8a VALUES('one', 2);
207 INSERT INTO t8a VALUES('two', 3);
208 INSERT INTO t8a VALUES('one', NULL);
209 INSERT INTO t8b(rowid,x) VALUES(1,111);
210 INSERT INTO t8b(rowid,x) VALUES(2,222);
211 INSERT INTO t8b(rowid,x) VALUES(3,333);
212 SELECT a, count(b) FROM t8a, t8b WHERE b=t8b.rowid GROUP BY a ORDER BY a;
215 do_test select5-8.2 {
217 SELECT a, count(b) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a;
220 do_test select5-8.3 {
222 SELECT t8a.a, count(t8a.b) FROM t8a, t8b WHERE t8a.b=t8b.rowid
223 GROUP BY 1 ORDER BY 1;
226 do_test select5-8.4 {
228 SELECT a, count(*) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a;
231 do_test select5-8.5 {
233 SELECT a, count(b) FROM t8a, t8b WHERE b<x GROUP BY a ORDER BY a;
236 do_test select5-8.6 {
238 SELECT a, count(t8a.b) FROM t8a, t8b WHERE b=t8b.rowid
239 GROUP BY a ORDER BY 2;
242 do_test select5-8.7 {
244 SELECT a, count(b) FROM t8a, t8b GROUP BY a ORDER BY 2;
247 do_test select5-8.8 {
249 SELECT a, count(*) FROM t8a, t8b GROUP BY a ORDER BY 2;
253 # 2021-04-26 forum https://sqlite.org/forum/forumpost/74330094d8
255 do_execsql_test select5-9.1 {
256 CREATE TABLE t1(a INT, b INT);
257 INSERT INTO t1(a,b) VALUES(1,null),(null,null),(1,null);
258 CREATE UNIQUE INDEX t1b ON t1(abs(b));
259 SELECT quote(a), quote(b), '|' FROM t1 GROUP BY a, abs(b);
260 } {NULL NULL | 1 NULL |}