blob: b5dbef5fb44cef397ef4fa1d84ef076650afd0fa [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drhda932812000-06-06 18:00:15 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drhda932812000-06-06 18:00:15 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# 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.
drhda932812000-06-06 18:00:15 +00009#
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.
14#
danielk197724acd8f2008-01-16 18:20:41 +000015# $Id: select3.test,v 1.23 2008/01/16 18:20:42 danielk1977 Exp $
drhda932812000-06-06 18:00:15 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Build some test data
21#
22do_test select3-1.0 {
drhda932812000-06-06 18:00:15 +000023 execsql {
24 CREATE TABLE t1(n int, log int);
drh5f3b4ab2004-05-27 17:22:54 +000025 BEGIN;
drhda932812000-06-06 18:00:15 +000026 }
drh5f3b4ab2004-05-27 17:22:54 +000027 for {set i 1} {$i<32} {incr i} {
danielk197724acd8f2008-01-16 18:20:41 +000028 for {set j 0} {(1<<$j)<$i} {incr j} {}
drh5f3b4ab2004-05-27 17:22:54 +000029 execsql "INSERT INTO t1 VALUES($i,$j)"
30 }
31 execsql {
32 COMMIT
33 }
drhda932812000-06-06 18:00:15 +000034 execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
35} {0 1 2 3 4 5}
36
37# Basic aggregate functions.
38#
39do_test select3-1.1 {
40 execsql {SELECT count(*) FROM t1}
41} {31}
42do_test select3-1.2 {
43 execsql {
44 SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
45 FROM t1
46 }
drh3d1d95e2005-09-08 10:37:01 +000047} {1 0 31 5 496 124 16.0 4.0}
drhda932812000-06-06 18:00:15 +000048do_test select3-1.3 {
49 execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
50} {1.9375 1.25}
51
52# Try some basic GROUP BY clauses
53#
54do_test select3-2.1 {
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}
57do_test select3-2.2 {
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}
drh8df447f2005-11-01 15:48:24 +000060do_test select3-2.3.1 {
drhda932812000-06-06 18:00:15 +000061 execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
drh92febd92004-08-20 18:34:20 +000062} {0 1.0 1 2.0 2 3.5 3 6.5 4 12.5 5 24.0}
drh8df447f2005-11-01 15:48:24 +000063do_test select3-2.3.2 {
drhda932812000-06-06 18:00:15 +000064 execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
drh8a512562005-11-14 22:29:05 +000065} {0 2.0 1 3.0 2 4.5 3 7.5 4 13.5 5 25.0}
drhda932812000-06-06 18:00:15 +000066do_test select3-2.4 {
67 execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
drh8a512562005-11-14 22:29:05 +000068} {0 0.0 1 0.0 2 0.5 3 1.5 4 3.5 5 7.0}
drhda932812000-06-06 18:00:15 +000069do_test select3-2.5 {
70 execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
drh8a512562005-11-14 22:29:05 +000071} {1 0.0 3 0.0 5 0.5 7 1.5 9 3.5 11 7.0}
drha2e00042002-01-22 03:13:42 +000072do_test select3-2.6 {
73 execsql {
74 SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
75 }
76} {1 1 3 1 5 2 7 4 9 8 11 15}
77do_test select3-2.7 {
78 execsql {
drh495c09a2005-04-01 10:47:40 +000079 SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y, x
drha2e00042002-01-22 03:13:42 +000080 }
drh495c09a2005-04-01 10:47:40 +000081} {1 1 3 1 5 2 7 4 9 8 11 15}
drha2e00042002-01-22 03:13:42 +000082do_test select3-2.8 {
83 execsql {
84 SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
85 }
86} {11 15 9 8 7 4 5 2 3 1 1 1}
drh18e87cf2006-04-11 14:16:21 +000087#do_test select3-2.9 {
88# catchsql {
89# SELECT log, count(*) FROM t1 GROUP BY 'x' ORDER BY log;
90# }
91#} {1 {GROUP BY terms must not be non-integer constants}}
drh88eee382003-01-31 17:16:36 +000092do_test select3-2.10 {
93 catchsql {
94 SELECT log, count(*) FROM t1 GROUP BY 0 ORDER BY log;
95 }
danielk197701874bf2007-12-13 07:58:50 +000096} {1 {1st GROUP BY term out of range - should be between 1 and 2}}
drh88eee382003-01-31 17:16:36 +000097do_test select3-2.11 {
98 catchsql {
99 SELECT log, count(*) FROM t1 GROUP BY 3 ORDER BY log;
100 }
danielk197701874bf2007-12-13 07:58:50 +0000101} {1 {1st GROUP BY term out of range - should be between 1 and 2}}
drh88eee382003-01-31 17:16:36 +0000102do_test select3-2.12 {
103 catchsql {
104 SELECT log, count(*) FROM t1 GROUP BY 1 ORDER BY log;
105 }
106} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
drh9245c242007-06-20 12:18:31 +0000107
108# Cannot have an empty GROUP BY
109do_test select3-2.13 {
110 catchsql {
111 SELECT log, count(*) FROM t1 GROUP BY ORDER BY log;
112 }
113} {1 {near "ORDER": syntax error}}
114do_test select3-2.14 {
115 catchsql {
116 SELECT log, count(*) FROM t1 GROUP BY;
117 }
118} {1 {near ";": syntax error}}
drhda932812000-06-06 18:00:15 +0000119
120# Cannot have a HAVING without a GROUP BY
121#
122do_test select3-3.1 {
123 set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
124 lappend v $msg
125} {1 {a GROUP BY clause is required before HAVING}}
126
127# Toss in some HAVING clauses
128#
129do_test select3-4.1 {
130 execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
131} {4 8 5 15}
132do_test select3-4.2 {
133 execsql {
134 SELECT log, count(*) FROM t1
135 GROUP BY log
136 HAVING count(*)>=4
137 ORDER BY log
138 }
139} {3 4 4 8 5 15}
140do_test select3-4.3 {
141 execsql {
142 SELECT log, count(*) FROM t1
143 GROUP BY log
144 HAVING count(*)>=4
drh736c22b2004-05-21 02:14:24 +0000145 ORDER BY max(n)+0
drhda932812000-06-06 18:00:15 +0000146 }
147} {3 4 4 8 5 15}
drha2e00042002-01-22 03:13:42 +0000148do_test select3-4.4 {
149 execsql {
150 SELECT log AS x, count(*) AS y FROM t1
151 GROUP BY x
152 HAVING y>=4
drh736c22b2004-05-21 02:14:24 +0000153 ORDER BY max(n)+0
drha2e00042002-01-22 03:13:42 +0000154 }
155} {3 4 4 8 5 15}
drhc66c5a22002-12-03 02:34:49 +0000156do_test select3-4.5 {
157 execsql {
158 SELECT log AS x FROM t1
159 GROUP BY x
160 HAVING count(*)>=4
drh736c22b2004-05-21 02:14:24 +0000161 ORDER BY max(n)+0
drhc66c5a22002-12-03 02:34:49 +0000162 }
163} {3 4 5}
drhda932812000-06-06 18:00:15 +0000164
drh4cfa7932000-06-08 15:10:46 +0000165do_test select3-5.1 {
166 execsql {
167 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
168 GROUP BY log
drh736c22b2004-05-21 02:14:24 +0000169 ORDER BY max(n+log*2)+0, avg(n)+0
drh4cfa7932000-06-08 15:10:46 +0000170 }
drh92febd92004-08-20 18:34:20 +0000171} {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}
drh4cfa7932000-06-08 15:10:46 +0000172do_test select3-5.2 {
173 execsql {
174 SELECT log, count(*), avg(n), max(n+log*2) FROM t1
175 GROUP BY log
drh736c22b2004-05-21 02:14:24 +0000176 ORDER BY max(n+log*2)+0, min(log,avg(n))+0
drh4cfa7932000-06-08 15:10:46 +0000177 }
drh92febd92004-08-20 18:34:20 +0000178} {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}
drh4cfa7932000-06-08 15:10:46 +0000179
drh68d2e592002-08-04 00:52:38 +0000180# Test sorting of GROUP BY results in the presence of an index
181# on the GROUP BY column.
182#
183do_test select3-6.1 {
184 execsql {
185 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
186 }
187} {0 1 1 2 2 3 3 5 4 9 5 17}
188do_test select3-6.2 {
189 execsql {
190 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
191 }
192} {5 17 4 9 3 5 2 3 1 2 0 1}
193do_test select3-6.3 {
194 execsql {
195 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
196 }
197} {0 1 1 2 2 3 3 5 4 9 5 17}
198do_test select3-6.4 {
199 execsql {
200 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
201 }
202} {5 17 4 9 3 5 2 3 1 2 0 1}
203do_test select3-6.5 {
204 execsql {
205 CREATE INDEX i1 ON t1(log);
206 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
207 }
208} {0 1 1 2 2 3 3 5 4 9 5 17}
209do_test select3-6.6 {
210 execsql {
211 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
212 }
213} {5 17 4 9 3 5 2 3 1 2 0 1}
214do_test select3-6.7 {
215 execsql {
216 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
217 }
218} {0 1 1 2 2 3 3 5 4 9 5 17}
219do_test select3-6.8 {
220 execsql {
221 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
222 }
223} {5 17 4 9 3 5 2 3 1 2 0 1}
224
drh97571952005-09-08 12:57:28 +0000225# Sometimes an aggregate query can return no rows at all.
226#
227do_test select3-7.1 {
228 execsql {
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;
232 }
233} {}
234do_test select3-7.2 {
235 execsql {
236 SELECT a, sum(b) FROM t2 WHERE b=5;
237 }
drhc2bd9132005-09-08 20:37:43 +0000238} {{} {}}
drh68d2e592002-08-04 00:52:38 +0000239
drh945498f2007-02-24 11:52:52 +0000240# 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.
244# Ticket #2251.
245#
246do_test select3-8.1 {
247 execsql {
248 CREATE TABLE A (
249 A1 DOUBLE,
250 A2 VARCHAR COLLATE NOCASE,
251 A3 DOUBLE
252 );
253 INSERT INTO A VALUES(39136,'ABC',1201900000);
254 INSERT INTO A VALUES(39136,'ABC',1207000000);
255 SELECT typeof(sum(a3)) FROM a;
256 }
257} {real}
258do_test select3-8.2 {
259 execsql {
260 SELECT typeof(sum(a3)) FROM a GROUP BY a1;
261 }
262} {real}
drh68d2e592002-08-04 00:52:38 +0000263
drhda932812000-06-06 18:00:15 +0000264finish_test