sl@0
|
1 |
# 2001 September 15
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
# This file implements regression tests for SQLite library. The
|
sl@0
|
12 |
# focus of this file is testing aggregate functions and the
|
sl@0
|
13 |
# GROUP BY and HAVING clauses of SELECT statements.
|
sl@0
|
14 |
#
|
sl@0
|
15 |
# $Id: select3.test,v 1.23 2008/01/16 18:20:42 danielk1977 Exp $
|
sl@0
|
16 |
|
sl@0
|
17 |
set testdir [file dirname $argv0]
|
sl@0
|
18 |
source $testdir/tester.tcl
|
sl@0
|
19 |
|
sl@0
|
20 |
# Build some test data
|
sl@0
|
21 |
#
|
sl@0
|
22 |
do_test select3-1.0 {
|
sl@0
|
23 |
execsql {
|
sl@0
|
24 |
CREATE TABLE t1(n int, log int);
|
sl@0
|
25 |
BEGIN;
|
sl@0
|
26 |
}
|
sl@0
|
27 |
for {set i 1} {$i<32} {incr i} {
|
sl@0
|
28 |
for {set j 0} {(1<<$j)<$i} {incr j} {}
|
sl@0
|
29 |
execsql "INSERT INTO t1 VALUES($i,$j)"
|
sl@0
|
30 |
}
|
sl@0
|
31 |
execsql {
|
sl@0
|
32 |
COMMIT
|
sl@0
|
33 |
}
|
sl@0
|
34 |
execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
|
sl@0
|
35 |
} {0 1 2 3 4 5}
|
sl@0
|
36 |
|
sl@0
|
37 |
# Basic aggregate functions.
|
sl@0
|
38 |
#
|
sl@0
|
39 |
do_test select3-1.1 {
|
sl@0
|
40 |
execsql {SELECT count(*) FROM t1}
|
sl@0
|
41 |
} {31}
|
sl@0
|
42 |
do_test select3-1.2 {
|
sl@0
|
43 |
execsql {
|
sl@0
|
44 |
SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
|
sl@0
|
45 |
FROM t1
|
sl@0
|
46 |
}
|
sl@0
|
47 |
} {1 0 31 5 496 124 16.0 4.0}
|
sl@0
|
48 |
do_test select3-1.3 {
|
sl@0
|
49 |
execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
|
sl@0
|
50 |
} {1.9375 1.25}
|
sl@0
|
51 |
|
sl@0
|
52 |
# Try some basic GROUP BY clauses
|
sl@0
|
53 |
#
|
sl@0
|
54 |
do_test select3-2.1 {
|
sl@0
|
55 |
execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
|
sl@0
|
56 |
} {0 1 1 1 2 2 3 4 4 8 5 15}
|
sl@0
|
57 |
do_test select3-2.2 {
|
sl@0
|
58 |
execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
|
sl@0
|
59 |
} {0 1 1 2 2 3 3 5 4 9 5 17}
|
sl@0
|
60 |
do_test select3-2.3.1 {
|
sl@0
|
61 |
execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
|
sl@0
|
62 |
} {0 1.0 1 2.0 2 3.5 3 6.5 4 12.5 5 24.0}
|
sl@0
|
63 |
do_test select3-2.3.2 {
|
sl@0
|
64 |
execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
|
sl@0
|
65 |
} {0 2.0 1 3.0 2 4.5 3 7.5 4 13.5 5 25.0}
|
sl@0
|
66 |
do_test select3-2.4 {
|
sl@0
|
67 |
execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
|
sl@0
|
68 |
} {0 0.0 1 0.0 2 0.5 3 1.5 4 3.5 5 7.0}
|
sl@0
|
69 |
do_test select3-2.5 {
|
sl@0
|
70 |
execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
|
sl@0
|
71 |
} {1 0.0 3 0.0 5 0.5 7 1.5 9 3.5 11 7.0}
|
sl@0
|
72 |
do_test select3-2.6 {
|
sl@0
|
73 |
execsql {
|
sl@0
|
74 |
SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
|
sl@0
|
75 |
}
|
sl@0
|
76 |
} {1 1 3 1 5 2 7 4 9 8 11 15}
|
sl@0
|
77 |
do_test select3-2.7 {
|
sl@0
|
78 |
execsql {
|
sl@0
|
79 |
SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y, x
|
sl@0
|
80 |
}
|
sl@0
|
81 |
} {1 1 3 1 5 2 7 4 9 8 11 15}
|
sl@0
|
82 |
do_test select3-2.8 {
|
sl@0
|
83 |
execsql {
|
sl@0
|
84 |
SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
|
sl@0
|
85 |
}
|
sl@0
|
86 |
} {11 15 9 8 7 4 5 2 3 1 1 1}
|
sl@0
|
87 |
#do_test select3-2.9 {
|
sl@0
|
88 |
# catchsql {
|
sl@0
|
89 |
# SELECT log, count(*) FROM t1 GROUP BY 'x' ORDER BY log;
|
sl@0
|
90 |
# }
|
sl@0
|
91 |
#} {1 {GROUP BY terms must not be non-integer constants}}
|
sl@0
|
92 |
do_test select3-2.10 {
|
sl@0
|
93 |
catchsql {
|
sl@0
|
94 |
SELECT log, count(*) FROM t1 GROUP BY 0 ORDER BY log;
|
sl@0
|
95 |
}
|
sl@0
|
96 |
} {1 {1st GROUP BY term out of range - should be between 1 and 2}}
|
sl@0
|
97 |
do_test select3-2.11 {
|
sl@0
|
98 |
catchsql {
|
sl@0
|
99 |
SELECT log, count(*) FROM t1 GROUP BY 3 ORDER BY log;
|
sl@0
|
100 |
}
|
sl@0
|
101 |
} {1 {1st GROUP BY term out of range - should be between 1 and 2}}
|
sl@0
|
102 |
do_test select3-2.12 {
|
sl@0
|
103 |
catchsql {
|
sl@0
|
104 |
SELECT log, count(*) FROM t1 GROUP BY 1 ORDER BY log;
|
sl@0
|
105 |
}
|
sl@0
|
106 |
} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
|
sl@0
|
107 |
|
sl@0
|
108 |
# Cannot have an empty GROUP BY
|
sl@0
|
109 |
do_test select3-2.13 {
|
sl@0
|
110 |
catchsql {
|
sl@0
|
111 |
SELECT log, count(*) FROM t1 GROUP BY ORDER BY log;
|
sl@0
|
112 |
}
|
sl@0
|
113 |
} {1 {near "ORDER": syntax error}}
|
sl@0
|
114 |
do_test select3-2.14 {
|
sl@0
|
115 |
catchsql {
|
sl@0
|
116 |
SELECT log, count(*) FROM t1 GROUP BY;
|
sl@0
|
117 |
}
|
sl@0
|
118 |
} {1 {near ";": syntax error}}
|
sl@0
|
119 |
|
sl@0
|
120 |
# Cannot have a HAVING without a GROUP BY
|
sl@0
|
121 |
#
|
sl@0
|
122 |
do_test select3-3.1 {
|
sl@0
|
123 |
set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
|
sl@0
|
124 |
lappend v $msg
|
sl@0
|
125 |
} {1 {a GROUP BY clause is required before HAVING}}
|
sl@0
|
126 |
|
sl@0
|
127 |
# Toss in some HAVING clauses
|
sl@0
|
128 |
#
|
sl@0
|
129 |
do_test select3-4.1 {
|
sl@0
|
130 |
execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
|
sl@0
|
131 |
} {4 8 5 15}
|
sl@0
|
132 |
do_test select3-4.2 {
|
sl@0
|
133 |
execsql {
|
sl@0
|
134 |
SELECT log, count(*) FROM t1
|
sl@0
|
135 |
GROUP BY log
|
sl@0
|
136 |
HAVING count(*)>=4
|
sl@0
|
137 |
ORDER BY log
|
sl@0
|
138 |
}
|
sl@0
|
139 |
} {3 4 4 8 5 15}
|
sl@0
|
140 |
do_test select3-4.3 {
|
sl@0
|
141 |
execsql {
|
sl@0
|
142 |
SELECT log, count(*) FROM t1
|
sl@0
|
143 |
GROUP BY log
|
sl@0
|
144 |
HAVING count(*)>=4
|
sl@0
|
145 |
ORDER BY max(n)+0
|
sl@0
|
146 |
}
|
sl@0
|
147 |
} {3 4 4 8 5 15}
|
sl@0
|
148 |
do_test select3-4.4 {
|
sl@0
|
149 |
execsql {
|
sl@0
|
150 |
SELECT log AS x, count(*) AS y FROM t1
|
sl@0
|
151 |
GROUP BY x
|
sl@0
|
152 |
HAVING y>=4
|
sl@0
|
153 |
ORDER BY max(n)+0
|
sl@0
|
154 |
}
|
sl@0
|
155 |
} {3 4 4 8 5 15}
|
sl@0
|
156 |
do_test select3-4.5 {
|
sl@0
|
157 |
execsql {
|
sl@0
|
158 |
SELECT log AS x FROM t1
|
sl@0
|
159 |
GROUP BY x
|
sl@0
|
160 |
HAVING count(*)>=4
|
sl@0
|
161 |
ORDER BY max(n)+0
|
sl@0
|
162 |
}
|
sl@0
|
163 |
} {3 4 5}
|
sl@0
|
164 |
|
sl@0
|
165 |
do_test select3-5.1 {
|
sl@0
|
166 |
execsql {
|
sl@0
|
167 |
SELECT log, count(*), avg(n), max(n+log*2) FROM t1
|
sl@0
|
168 |
GROUP BY log
|
sl@0
|
169 |
ORDER BY max(n+log*2)+0, avg(n)+0
|
sl@0
|
170 |
}
|
sl@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}
|
sl@0
|
172 |
do_test select3-5.2 {
|
sl@0
|
173 |
execsql {
|
sl@0
|
174 |
SELECT log, count(*), avg(n), max(n+log*2) FROM t1
|
sl@0
|
175 |
GROUP BY log
|
sl@0
|
176 |
ORDER BY max(n+log*2)+0, min(log,avg(n))+0
|
sl@0
|
177 |
}
|
sl@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}
|
sl@0
|
179 |
|
sl@0
|
180 |
# Test sorting of GROUP BY results in the presence of an index
|
sl@0
|
181 |
# on the GROUP BY column.
|
sl@0
|
182 |
#
|
sl@0
|
183 |
do_test select3-6.1 {
|
sl@0
|
184 |
execsql {
|
sl@0
|
185 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
|
sl@0
|
186 |
}
|
sl@0
|
187 |
} {0 1 1 2 2 3 3 5 4 9 5 17}
|
sl@0
|
188 |
do_test select3-6.2 {
|
sl@0
|
189 |
execsql {
|
sl@0
|
190 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
|
sl@0
|
191 |
}
|
sl@0
|
192 |
} {5 17 4 9 3 5 2 3 1 2 0 1}
|
sl@0
|
193 |
do_test select3-6.3 {
|
sl@0
|
194 |
execsql {
|
sl@0
|
195 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
|
sl@0
|
196 |
}
|
sl@0
|
197 |
} {0 1 1 2 2 3 3 5 4 9 5 17}
|
sl@0
|
198 |
do_test select3-6.4 {
|
sl@0
|
199 |
execsql {
|
sl@0
|
200 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
|
sl@0
|
201 |
}
|
sl@0
|
202 |
} {5 17 4 9 3 5 2 3 1 2 0 1}
|
sl@0
|
203 |
do_test select3-6.5 {
|
sl@0
|
204 |
execsql {
|
sl@0
|
205 |
CREATE INDEX i1 ON t1(log);
|
sl@0
|
206 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
|
sl@0
|
207 |
}
|
sl@0
|
208 |
} {0 1 1 2 2 3 3 5 4 9 5 17}
|
sl@0
|
209 |
do_test select3-6.6 {
|
sl@0
|
210 |
execsql {
|
sl@0
|
211 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
|
sl@0
|
212 |
}
|
sl@0
|
213 |
} {5 17 4 9 3 5 2 3 1 2 0 1}
|
sl@0
|
214 |
do_test select3-6.7 {
|
sl@0
|
215 |
execsql {
|
sl@0
|
216 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
|
sl@0
|
217 |
}
|
sl@0
|
218 |
} {0 1 1 2 2 3 3 5 4 9 5 17}
|
sl@0
|
219 |
do_test select3-6.8 {
|
sl@0
|
220 |
execsql {
|
sl@0
|
221 |
SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
|
sl@0
|
222 |
}
|
sl@0
|
223 |
} {5 17 4 9 3 5 2 3 1 2 0 1}
|
sl@0
|
224 |
|
sl@0
|
225 |
# Sometimes an aggregate query can return no rows at all.
|
sl@0
|
226 |
#
|
sl@0
|
227 |
do_test select3-7.1 {
|
sl@0
|
228 |
execsql {
|
sl@0
|
229 |
CREATE TABLE t2(a,b);
|
sl@0
|
230 |
INSERT INTO t2 VALUES(1,2);
|
sl@0
|
231 |
SELECT a, sum(b) FROM t2 WHERE b=5 GROUP BY a;
|
sl@0
|
232 |
}
|
sl@0
|
233 |
} {}
|
sl@0
|
234 |
do_test select3-7.2 {
|
sl@0
|
235 |
execsql {
|
sl@0
|
236 |
SELECT a, sum(b) FROM t2 WHERE b=5;
|
sl@0
|
237 |
}
|
sl@0
|
238 |
} {{} {}}
|
sl@0
|
239 |
|
sl@0
|
240 |
# If a table column is of type REAL but we are storing integer values
|
sl@0
|
241 |
# in it, the values are stored as integers to take up less space. The
|
sl@0
|
242 |
# values are converted by to REAL as they are read out of the table.
|
sl@0
|
243 |
# Make sure the GROUP BY clause does this conversion correctly.
|
sl@0
|
244 |
# Ticket #2251.
|
sl@0
|
245 |
#
|
sl@0
|
246 |
do_test select3-8.1 {
|
sl@0
|
247 |
execsql {
|
sl@0
|
248 |
CREATE TABLE A (
|
sl@0
|
249 |
A1 DOUBLE,
|
sl@0
|
250 |
A2 VARCHAR COLLATE NOCASE,
|
sl@0
|
251 |
A3 DOUBLE
|
sl@0
|
252 |
);
|
sl@0
|
253 |
INSERT INTO A VALUES(39136,'ABC',1201900000);
|
sl@0
|
254 |
INSERT INTO A VALUES(39136,'ABC',1207000000);
|
sl@0
|
255 |
SELECT typeof(sum(a3)) FROM a;
|
sl@0
|
256 |
}
|
sl@0
|
257 |
} {real}
|
sl@0
|
258 |
do_test select3-8.2 {
|
sl@0
|
259 |
execsql {
|
sl@0
|
260 |
SELECT typeof(sum(a3)) FROM a GROUP BY a1;
|
sl@0
|
261 |
}
|
sl@0
|
262 |
} {real}
|
sl@0
|
263 |
|
sl@0
|
264 |
finish_test
|