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.
15 # $Id: select3.test,v 1.23 2008/01/16 18:20:42 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Build some test data
24 CREATE TABLE t1(n int, log int);
27 for {set i 1} {$i<32} {incr i} {
28 for {set j 0} {(1<<$j)<$i} {incr j} {}
29 execsql "INSERT INTO t1 VALUES($i,$j)"
34 execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
37 # Basic aggregate functions.
40 execsql {SELECT count(*) FROM t1}
44 SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
47 } {1 0 31 5 496 124 16.0 4.0}
49 execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
52 # Try some basic GROUP BY clauses
55 execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
56 } {0 1 1 1 2 2 3 4 4 8 5 15}
58 execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
59 } {0 1 1 2 2 3 3 5 4 9 5 17}
60 do_test select3-2.3.1 {
61 execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
62 } {0 1.0 1 2.0 2 3.5 3 6.5 4 12.5 5 24.0}
63 do_test select3-2.3.2 {
64 execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
65 } {0 2.0 1 3.0 2 4.5 3 7.5 4 13.5 5 25.0}
67 execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
68 } {0 0.0 1 0.0 2 0.5 3 1.5 4 3.5 5 7.0}
70 execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
71 } {1 0.0 3 0.0 5 0.5 7 1.5 9 3.5 11 7.0}
74 SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
76 } {1 1 3 1 5 2 7 4 9 8 11 15}
79 SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y, x
81 } {1 1 3 1 5 2 7 4 9 8 11 15}
84 SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
86 } {11 15 9 8 7 4 5 2 3 1 1 1}
87 #do_test select3-2.9 {
89 # SELECT log, count(*) FROM t1 GROUP BY 'x' ORDER BY log;
91 #} {1 {GROUP BY terms must not be non-integer constants}}
92 do_test select3-2.10 {
94 SELECT log, count(*) FROM t1 GROUP BY 0 ORDER BY log;
96 } {1 {1st GROUP BY term out of range - should be between 1 and 2}}
97 do_test select3-2.11 {
99 SELECT log, count(*) FROM t1 GROUP BY 3 ORDER BY log;
101 } {1 {1st GROUP BY term out of range - should be between 1 and 2}}
102 do_test select3-2.12 {
104 SELECT log, count(*) FROM t1 GROUP BY 1 ORDER BY log;
106 } {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
108 # Cannot have an empty GROUP BY
109 do_test select3-2.13 {
111 SELECT log, count(*) FROM t1 GROUP BY ORDER BY log;
113 } {1 {near "ORDER": syntax error}}
114 do_test select3-2.14 {
116 SELECT log, count(*) FROM t1 GROUP BY;
118 } {1 {near ";": syntax error}}
120 # Cannot have a HAVING without a GROUP BY
122 do_test select3-3.1 {
123 set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
125 } {1 {a GROUP BY clause is required before HAVING}}
127 # Toss in some HAVING clauses
129 do_test select3-4.1 {
130 execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
132 do_test select3-4.2 {
134 SELECT log, count(*) FROM t1
140 do_test select3-4.3 {
142 SELECT log, count(*) FROM t1
148 do_test select3-4.4 {
150 SELECT log AS x, count(*) AS y FROM t1
156 do_test select3-4.5 {
158 SELECT log AS x FROM t1
165 do_test select3-5.1 {
167 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
169 ORDER BY max(n+log*2)+0, avg(n)+0
171 } {0 1 1.0 1 1 1 2.0 4 2 2 3.5 8 3 4 6.5 14 4 8 12.5 24 5 15 24.0 41}
172 do_test select3-5.2 {
174 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
176 ORDER BY max(n+log*2)+0, min(log,avg(n))+0
178 } {0 1 1.0 1 1 1 2.0 4 2 2 3.5 8 3 4 6.5 14 4 8 12.5 24 5 15 24.0 41}
180 # Test sorting of GROUP BY results in the presence of an index
181 # on the GROUP BY column.
183 do_test select3-6.1 {
185 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
187 } {0 1 1 2 2 3 3 5 4 9 5 17}
188 do_test select3-6.2 {
190 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
192 } {5 17 4 9 3 5 2 3 1 2 0 1}
193 do_test select3-6.3 {
195 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
197 } {0 1 1 2 2 3 3 5 4 9 5 17}
198 do_test select3-6.4 {
200 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
202 } {5 17 4 9 3 5 2 3 1 2 0 1}
203 do_test select3-6.5 {
205 CREATE INDEX i1 ON t1(log);
206 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
208 } {0 1 1 2 2 3 3 5 4 9 5 17}
209 do_test select3-6.6 {
211 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
213 } {5 17 4 9 3 5 2 3 1 2 0 1}
214 do_test select3-6.7 {
216 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
218 } {0 1 1 2 2 3 3 5 4 9 5 17}
219 do_test select3-6.8 {
221 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
223 } {5 17 4 9 3 5 2 3 1 2 0 1}
225 # Sometimes an aggregate query can return no rows at all.
227 do_test select3-7.1 {
229 CREATE TABLE t2(a,b);
230 INSERT INTO t2 VALUES(1,2);
231 SELECT a, sum(b) FROM t2 WHERE b=5 GROUP BY a;
234 do_test select3-7.2 {
236 SELECT a, sum(b) FROM t2 WHERE b=5;
240 # If a table column is of type REAL but we are storing integer values
241 # in it, the values are stored as integers to take up less space. The
242 # values are converted by to REAL as they are read out of the table.
243 # Make sure the GROUP BY clause does this conversion correctly.
246 do_test select3-8.1 {
250 A2 VARCHAR COLLATE NOCASE,
253 INSERT INTO A VALUES(39136,'ABC',1201900000);
254 INSERT INTO A VALUES(39136,'ABC',1207000000);
255 SELECT typeof(sum(a3)) FROM a;
258 do_test select3-8.2 {
260 SELECT typeof(sum(a3)) FROM a GROUP BY a1;
264 # 2019-05-09 ticket https://www.sqlite.org/src/tktview/6c1d3febc00b22d457c7
288 140 9223372036854775807
289 141 -9223372036854775807
290 142 -9223372036854775808
291 143 9223372036854775806
292 144 9223372036854775805
293 145 -9223372036854775806
294 146 -9223372036854775805
298 do_execsql_test select3-8.$id {
299 DROP TABLE IF EXISTS t1;
300 CREATE TABLE t1 (c0, c1 REAL PRIMARY KEY);
301 INSERT INTO t1(c0, c1) VALUES (0, $x), (0, 0);
302 UPDATE t1 SET c0 = NULL;
303 UPDATE OR REPLACE t1 SET c1 = 1;
304 SELECT DISTINCT * FROM t1 WHERE (t1.c0 IS NULL);
305 PRAGMA integrity_check;
309 # 2020-03-10 ticket e0c2ad1aa8a9c691
311 do_execsql_test select3-9.100 {
312 CREATE TABLE t0(c0 REAL, c1 REAL GENERATED ALWAYS AS (c0));
313 INSERT INTO t0(c0) VALUES (1);
314 SELECT * FROM t0 GROUP BY c0;
318 do_execsql_test select3.10.100 {
319 CREATE TABLE t1(a, b);
320 CREATE TABLE t2(c, d);
322 (SELECT 'xyz' FROM (SELECT * FROM t2 WHERE 0) WHERE t1.b=1)