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