xref: /illumos-gate/usr/src/lib/libsqlite/test/select3.test (revision fc910014e8a32a65612105835a10995f2c13d942)
1#
2# 2001 September 15
3#
4# The author disclaims copyright to this source code.  In place of
5# a legal notice, here is a blessing:
6#
7#    May you do good and not evil.
8#    May you find forgiveness for yourself and forgive others.
9#    May you share freely, never taking more than you give.
10#
11#***********************************************************************
12# This file implements regression tests for SQLite library.  The
13# focus of this file is testing aggregate functions and the
14# GROUP BY and HAVING clauses of SELECT statements.
15#
16# $Id: select3.test,v 1.8 2003/01/31 17:16:37 drh Exp $
17
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
21# Build some test data
22#
23do_test select3-1.0 {
24  set fd [open data1.txt w]
25  for {set i 1} {$i<32} {incr i} {
26    for {set j 0} {pow(2,$j)<$i} {incr j} {}
27    puts $fd "$i\t$j"
28  }
29  close $fd
30  execsql {
31    CREATE TABLE t1(n int, log int);
32    COPY t1 FROM 'data1.txt'
33  }
34  file delete data1.txt
35  execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
36} {0 1 2 3 4 5}
37
38# Basic aggregate functions.
39#
40do_test select3-1.1 {
41  execsql {SELECT count(*) FROM t1}
42} {31}
43do_test select3-1.2 {
44  execsql {
45    SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
46    FROM t1
47  }
48} {1 0 31 5 496 124 16 4}
49do_test select3-1.3 {
50  execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
51} {1.9375 1.25}
52
53# Try some basic GROUP BY clauses
54#
55do_test select3-2.1 {
56  execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
57} {0 1 1 1 2 2 3 4 4 8 5 15}
58do_test select3-2.2 {
59  execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
60} {0 1 1 2 2 3 3 5 4 9 5 17}
61do_test select3-2.3 {
62  execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
63} {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24}
64do_test select3-2.3 {
65  execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
66} {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25}
67do_test select3-2.4 {
68  execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
69} {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7}
70do_test select3-2.5 {
71  execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
72} {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7}
73do_test select3-2.6 {
74  execsql {
75    SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
76  }
77} {1 1 3 1 5 2 7 4 9 8 11 15}
78do_test select3-2.7 {
79  execsql {
80    SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y
81  }
82} {1 1 3 1 5 2 7 4 9 8 11 15}
83do_test select3-2.8 {
84  execsql {
85    SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
86  }
87} {11 15 9 8 7 4 5 2 3 1 1 1}
88do_test select3-2.9 {
89  catchsql {
90    SELECT log, count(*) FROM t1 GROUP BY 'x' ORDER BY log;
91  }
92} {1 {GROUP BY terms must not be non-integer constants}}
93do_test select3-2.10 {
94  catchsql {
95    SELECT log, count(*) FROM t1 GROUP BY 0 ORDER BY log;
96  }
97} {1 {GROUP BY column number 0 out of range - should be between 1 and 2}}
98do_test select3-2.11 {
99  catchsql {
100    SELECT log, count(*) FROM t1 GROUP BY 3 ORDER BY log;
101  }
102} {1 {GROUP BY column number 3 out of range - should be between 1 and 2}}
103do_test select3-2.12 {
104  catchsql {
105    SELECT log, count(*) FROM t1 GROUP BY 1 ORDER BY log;
106  }
107} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
108#do_test select3-2.13 {
109#  catchsql {
110#    SELECT log, count(*) FROM t1 GROUP BY 2 ORDER BY log;
111#  }
112#} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
113#do_test select3-2.14 {
114#  catchsql {
115#    SELECT log, count(*) FROM t1 GROUP BY count(*) ORDER BY log;
116#  }
117#} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
118
119# Cannot have a HAVING without a GROUP BY
120#
121do_test select3-3.1 {
122  set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
123  lappend v $msg
124} {1 {a GROUP BY clause is required before HAVING}}
125
126# Toss in some HAVING clauses
127#
128do_test select3-4.1 {
129  execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
130} {4 8 5 15}
131do_test select3-4.2 {
132  execsql {
133    SELECT log, count(*) FROM t1
134    GROUP BY log
135    HAVING count(*)>=4
136    ORDER BY log
137  }
138} {3 4 4 8 5 15}
139do_test select3-4.3 {
140  execsql {
141    SELECT log, count(*) FROM t1
142    GROUP BY log
143    HAVING count(*)>=4
144    ORDER BY max(n)
145  }
146} {3 4 4 8 5 15}
147do_test select3-4.4 {
148  execsql {
149    SELECT log AS x, count(*) AS y FROM t1
150    GROUP BY x
151    HAVING y>=4
152    ORDER BY max(n)
153  }
154} {3 4 4 8 5 15}
155do_test select3-4.5 {
156  execsql {
157    SELECT log AS x FROM t1
158    GROUP BY x
159    HAVING count(*)>=4
160    ORDER BY max(n)
161  }
162} {3 4 5}
163
164do_test select3-5.1 {
165  execsql {
166    SELECT log, count(*), avg(n), max(n+log*2) FROM t1
167    GROUP BY log
168    ORDER BY max(n+log*2), avg(n)
169  }
170} {0 1 1 1 1 1 2 4 2 2 3.5 8 3 4 6.5 14 4 8 12.5 24 5 15 24 41}
171do_test select3-5.2 {
172  execsql {
173    SELECT log, count(*), avg(n), max(n+log*2) FROM t1
174    GROUP BY log
175    ORDER BY max(n+log*2), min(log,avg(n))
176  }
177} {0 1 1 1 1 1 2 4 2 2 3.5 8 3 4 6.5 14 4 8 12.5 24 5 15 24 41}
178
179# Test sorting of GROUP BY results in the presence of an index
180# on the GROUP BY column.
181#
182do_test select3-6.1 {
183  execsql {
184    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
185  }
186} {0 1 1 2 2 3 3 5 4 9 5 17}
187do_test select3-6.2 {
188  execsql {
189    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
190  }
191} {5 17 4 9 3 5 2 3 1 2 0 1}
192do_test select3-6.3 {
193  execsql {
194    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
195  }
196} {0 1 1 2 2 3 3 5 4 9 5 17}
197do_test select3-6.4 {
198  execsql {
199    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
200  }
201} {5 17 4 9 3 5 2 3 1 2 0 1}
202do_test select3-6.5 {
203  execsql {
204    CREATE INDEX i1 ON t1(log);
205    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
206  }
207} {0 1 1 2 2 3 3 5 4 9 5 17}
208do_test select3-6.6 {
209  execsql {
210    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
211  }
212} {5 17 4 9 3 5 2 3 1 2 0 1}
213do_test select3-6.7 {
214  execsql {
215    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
216  }
217} {0 1 1 2 2 3 3 5 4 9 5 17}
218do_test select3-6.8 {
219  execsql {
220    SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
221  }
222} {5 17 4 9 3 5 2 3 1 2 0 1}
223
224
225
226finish_test
227