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: select5.test,v 1.20 2008/08/21 14:15:59 drh 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 |
execsql {
|
sl@0
|
23 |
CREATE TABLE t1(x int, y int);
|
sl@0
|
24 |
BEGIN;
|
sl@0
|
25 |
}
|
sl@0
|
26 |
for {set i 1} {$i<32} {incr i} {
|
sl@0
|
27 |
for {set j 0} {(1<<$j)<$i} {incr j} {}
|
sl@0
|
28 |
execsql "INSERT INTO t1 VALUES([expr {32-$i}],[expr {10-$j}])"
|
sl@0
|
29 |
}
|
sl@0
|
30 |
execsql {
|
sl@0
|
31 |
COMMIT
|
sl@0
|
32 |
}
|
sl@0
|
33 |
|
sl@0
|
34 |
do_test select5-1.0 {
|
sl@0
|
35 |
execsql {SELECT DISTINCT y FROM t1 ORDER BY y}
|
sl@0
|
36 |
} {5 6 7 8 9 10}
|
sl@0
|
37 |
|
sl@0
|
38 |
# Sort by an aggregate function.
|
sl@0
|
39 |
#
|
sl@0
|
40 |
do_test select5-1.1 {
|
sl@0
|
41 |
execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y}
|
sl@0
|
42 |
} {5 15 6 8 7 4 8 2 9 1 10 1}
|
sl@0
|
43 |
do_test select5-1.2 {
|
sl@0
|
44 |
execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y}
|
sl@0
|
45 |
} {9 1 10 1 8 2 7 4 6 8 5 15}
|
sl@0
|
46 |
do_test select5-1.3 {
|
sl@0
|
47 |
execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y}
|
sl@0
|
48 |
} {1 9 1 10 2 8 4 7 8 6 15 5}
|
sl@0
|
49 |
|
sl@0
|
50 |
# Some error messages associated with aggregates and GROUP BY
|
sl@0
|
51 |
#
|
sl@0
|
52 |
do_test select5-2.1.1 {
|
sl@0
|
53 |
catchsql {
|
sl@0
|
54 |
SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
|
sl@0
|
55 |
}
|
sl@0
|
56 |
} {1 {no such column: z}}
|
sl@0
|
57 |
do_test select5-2.1.2 {
|
sl@0
|
58 |
catchsql {
|
sl@0
|
59 |
SELECT y, count(*) FROM t1 GROUP BY temp.t1.y ORDER BY y
|
sl@0
|
60 |
}
|
sl@0
|
61 |
} {1 {no such column: temp.t1.y}}
|
sl@0
|
62 |
do_test select5-2.2 {
|
sl@0
|
63 |
set v [catch {execsql {
|
sl@0
|
64 |
SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
|
sl@0
|
65 |
}} msg]
|
sl@0
|
66 |
lappend v $msg
|
sl@0
|
67 |
} {1 {no such function: z}}
|
sl@0
|
68 |
do_test select5-2.3 {
|
sl@0
|
69 |
set v [catch {execsql {
|
sl@0
|
70 |
SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
|
sl@0
|
71 |
}} msg]
|
sl@0
|
72 |
lappend v $msg
|
sl@0
|
73 |
} {0 {8 2 9 1 10 1}}
|
sl@0
|
74 |
do_test select5-2.4 {
|
sl@0
|
75 |
set v [catch {execsql {
|
sl@0
|
76 |
SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
|
sl@0
|
77 |
}} msg]
|
sl@0
|
78 |
lappend v $msg
|
sl@0
|
79 |
} {1 {no such function: z}}
|
sl@0
|
80 |
do_test select5-2.5 {
|
sl@0
|
81 |
set v [catch {execsql {
|
sl@0
|
82 |
SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
|
sl@0
|
83 |
}} msg]
|
sl@0
|
84 |
lappend v $msg
|
sl@0
|
85 |
} {1 {no such column: z}}
|
sl@0
|
86 |
|
sl@0
|
87 |
# Get the Agg function to rehash in vdbe.c
|
sl@0
|
88 |
#
|
sl@0
|
89 |
do_test select5-3.1 {
|
sl@0
|
90 |
execsql {
|
sl@0
|
91 |
SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
|
sl@0
|
92 |
}
|
sl@0
|
93 |
} {1 1 5.0 2 1 5.0 3 1 5.0}
|
sl@0
|
94 |
|
sl@0
|
95 |
# Run various aggregate functions when the count is zero.
|
sl@0
|
96 |
#
|
sl@0
|
97 |
do_test select5-4.1 {
|
sl@0
|
98 |
execsql {
|
sl@0
|
99 |
SELECT avg(x) FROM t1 WHERE x>100
|
sl@0
|
100 |
}
|
sl@0
|
101 |
} {{}}
|
sl@0
|
102 |
do_test select5-4.2 {
|
sl@0
|
103 |
execsql {
|
sl@0
|
104 |
SELECT count(x) FROM t1 WHERE x>100
|
sl@0
|
105 |
}
|
sl@0
|
106 |
} {0}
|
sl@0
|
107 |
do_test select5-4.3 {
|
sl@0
|
108 |
execsql {
|
sl@0
|
109 |
SELECT min(x) FROM t1 WHERE x>100
|
sl@0
|
110 |
}
|
sl@0
|
111 |
} {{}}
|
sl@0
|
112 |
do_test select5-4.4 {
|
sl@0
|
113 |
execsql {
|
sl@0
|
114 |
SELECT max(x) FROM t1 WHERE x>100
|
sl@0
|
115 |
}
|
sl@0
|
116 |
} {{}}
|
sl@0
|
117 |
do_test select5-4.5 {
|
sl@0
|
118 |
execsql {
|
sl@0
|
119 |
SELECT sum(x) FROM t1 WHERE x>100
|
sl@0
|
120 |
}
|
sl@0
|
121 |
} {{}}
|
sl@0
|
122 |
|
sl@0
|
123 |
# Some tests for queries with a GROUP BY clause but no aggregate functions.
|
sl@0
|
124 |
#
|
sl@0
|
125 |
# Note: The query in test cases 5.1 through 5.5 are not legal SQL. So if the
|
sl@0
|
126 |
# implementation changes in the future and it returns different results,
|
sl@0
|
127 |
# this is not such a big deal.
|
sl@0
|
128 |
#
|
sl@0
|
129 |
do_test select5-5.1 {
|
sl@0
|
130 |
execsql {
|
sl@0
|
131 |
CREATE TABLE t2(a, b, c);
|
sl@0
|
132 |
INSERT INTO t2 VALUES(1, 2, 3);
|
sl@0
|
133 |
INSERT INTO t2 VALUES(1, 4, 5);
|
sl@0
|
134 |
INSERT INTO t2 VALUES(6, 4, 7);
|
sl@0
|
135 |
CREATE INDEX t2_idx ON t2(a);
|
sl@0
|
136 |
}
|
sl@0
|
137 |
} {}
|
sl@0
|
138 |
do_test select5-5.2 {
|
sl@0
|
139 |
execsql {
|
sl@0
|
140 |
SELECT a FROM t2 GROUP BY a;
|
sl@0
|
141 |
}
|
sl@0
|
142 |
} {1 6}
|
sl@0
|
143 |
do_test select5-5.3 {
|
sl@0
|
144 |
execsql {
|
sl@0
|
145 |
SELECT a FROM t2 WHERE a>2 GROUP BY a;
|
sl@0
|
146 |
}
|
sl@0
|
147 |
} {6}
|
sl@0
|
148 |
do_test select5-5.4 {
|
sl@0
|
149 |
execsql {
|
sl@0
|
150 |
SELECT a, b FROM t2 GROUP BY a, b;
|
sl@0
|
151 |
}
|
sl@0
|
152 |
} {1 2 1 4 6 4}
|
sl@0
|
153 |
do_test select5-5.5 {
|
sl@0
|
154 |
execsql {
|
sl@0
|
155 |
SELECT a, b FROM t2 GROUP BY a;
|
sl@0
|
156 |
}
|
sl@0
|
157 |
} {1 4 6 4}
|
sl@0
|
158 |
|
sl@0
|
159 |
# Test rendering of columns for the GROUP BY clause.
|
sl@0
|
160 |
#
|
sl@0
|
161 |
do_test select5-5.11 {
|
sl@0
|
162 |
execsql {
|
sl@0
|
163 |
SELECT max(c), b*a, b, a FROM t2 GROUP BY b*a, b, a
|
sl@0
|
164 |
}
|
sl@0
|
165 |
} {3 2 2 1 5 4 4 1 7 24 4 6}
|
sl@0
|
166 |
|
sl@0
|
167 |
# NULL compare equal to each other for the purposes of processing
|
sl@0
|
168 |
# the GROUP BY clause.
|
sl@0
|
169 |
#
|
sl@0
|
170 |
do_test select5-6.1 {
|
sl@0
|
171 |
execsql {
|
sl@0
|
172 |
CREATE TABLE t3(x,y);
|
sl@0
|
173 |
INSERT INTO t3 VALUES(1,NULL);
|
sl@0
|
174 |
INSERT INTO t3 VALUES(2,NULL);
|
sl@0
|
175 |
INSERT INTO t3 VALUES(3,4);
|
sl@0
|
176 |
SELECT count(x), y FROM t3 GROUP BY y ORDER BY 1
|
sl@0
|
177 |
}
|
sl@0
|
178 |
} {1 4 2 {}}
|
sl@0
|
179 |
do_test select5-6.2 {
|
sl@0
|
180 |
execsql {
|
sl@0
|
181 |
CREATE TABLE t4(x,y,z);
|
sl@0
|
182 |
INSERT INTO t4 VALUES(1,2,NULL);
|
sl@0
|
183 |
INSERT INTO t4 VALUES(2,3,NULL);
|
sl@0
|
184 |
INSERT INTO t4 VALUES(3,NULL,5);
|
sl@0
|
185 |
INSERT INTO t4 VALUES(4,NULL,6);
|
sl@0
|
186 |
INSERT INTO t4 VALUES(4,NULL,6);
|
sl@0
|
187 |
INSERT INTO t4 VALUES(5,NULL,NULL);
|
sl@0
|
188 |
INSERT INTO t4 VALUES(5,NULL,NULL);
|
sl@0
|
189 |
INSERT INTO t4 VALUES(6,7,8);
|
sl@0
|
190 |
SELECT max(x), count(x), y, z FROM t4 GROUP BY y, z ORDER BY 1
|
sl@0
|
191 |
}
|
sl@0
|
192 |
} {1 1 2 {} 2 1 3 {} 3 1 {} 5 4 2 {} 6 5 2 {} {} 6 1 7 8}
|
sl@0
|
193 |
|
sl@0
|
194 |
do_test select5-7.2 {
|
sl@0
|
195 |
execsql {
|
sl@0
|
196 |
SELECT count(*), count(x) as cnt FROM t4 GROUP BY y ORDER BY cnt;
|
sl@0
|
197 |
}
|
sl@0
|
198 |
} {1 1 1 1 1 1 5 5}
|
sl@0
|
199 |
|
sl@0
|
200 |
# See ticket #3324.
|
sl@0
|
201 |
#
|
sl@0
|
202 |
do_test select5-8.1 {
|
sl@0
|
203 |
execsql {
|
sl@0
|
204 |
CREATE TABLE t8a(a,b);
|
sl@0
|
205 |
CREATE TABLE t8b(x);
|
sl@0
|
206 |
INSERT INTO t8a VALUES('one', 1);
|
sl@0
|
207 |
INSERT INTO t8a VALUES('one', 2);
|
sl@0
|
208 |
INSERT INTO t8a VALUES('two', 3);
|
sl@0
|
209 |
INSERT INTO t8a VALUES('one', NULL);
|
sl@0
|
210 |
INSERT INTO t8b(rowid,x) VALUES(1,111);
|
sl@0
|
211 |
INSERT INTO t8b(rowid,x) VALUES(2,222);
|
sl@0
|
212 |
INSERT INTO t8b(rowid,x) VALUES(3,333);
|
sl@0
|
213 |
SELECT a, count(b) FROM t8a, t8b WHERE b=t8b.rowid GROUP BY a ORDER BY a;
|
sl@0
|
214 |
}
|
sl@0
|
215 |
} {one 2 two 1}
|
sl@0
|
216 |
do_test select5-8.2 {
|
sl@0
|
217 |
execsql {
|
sl@0
|
218 |
SELECT a, count(b) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a;
|
sl@0
|
219 |
}
|
sl@0
|
220 |
} {one 2 two 1}
|
sl@0
|
221 |
do_test select5-8.3 {
|
sl@0
|
222 |
execsql {
|
sl@0
|
223 |
SELECT t8a.a, count(t8a.b) FROM t8a, t8b WHERE t8a.b=t8b.rowid
|
sl@0
|
224 |
GROUP BY 1 ORDER BY 1;
|
sl@0
|
225 |
}
|
sl@0
|
226 |
} {one 2 two 1}
|
sl@0
|
227 |
do_test select5-8.4 {
|
sl@0
|
228 |
execsql {
|
sl@0
|
229 |
SELECT a, count(*) FROM t8a, t8b WHERE b=+t8b.rowid GROUP BY a ORDER BY a;
|
sl@0
|
230 |
}
|
sl@0
|
231 |
} {one 2 two 1}
|
sl@0
|
232 |
do_test select5-8.5 {
|
sl@0
|
233 |
execsql {
|
sl@0
|
234 |
SELECT a, count(b) FROM t8a, t8b WHERE b<x GROUP BY a ORDER BY a;
|
sl@0
|
235 |
}
|
sl@0
|
236 |
} {one 6 two 3}
|
sl@0
|
237 |
do_test select5-8.6 {
|
sl@0
|
238 |
execsql {
|
sl@0
|
239 |
SELECT a, count(t8a.b) FROM t8a, t8b WHERE b=t8b.rowid
|
sl@0
|
240 |
GROUP BY a ORDER BY 2;
|
sl@0
|
241 |
}
|
sl@0
|
242 |
} {two 1 one 2}
|
sl@0
|
243 |
do_test select5-8.7 {
|
sl@0
|
244 |
execsql {
|
sl@0
|
245 |
SELECT a, count(b) FROM t8a, t8b GROUP BY a ORDER BY 2;
|
sl@0
|
246 |
}
|
sl@0
|
247 |
} {two 3 one 6}
|
sl@0
|
248 |
do_test select5-8.8 {
|
sl@0
|
249 |
execsql {
|
sl@0
|
250 |
SELECT a, count(*) FROM t8a, t8b GROUP BY a ORDER BY 2;
|
sl@0
|
251 |
}
|
sl@0
|
252 |
} {two 3 one 9}
|
sl@0
|
253 |
|
sl@0
|
254 |
|
sl@0
|
255 |
|
sl@0
|
256 |
|
sl@0
|
257 |
finish_test
|