1# 2001 September 15 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 6# 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. 9# 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# 15# $Id: select3.test,v 1.6 2002/08/04 00:52:38 drh Exp $ 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# Build some test data 21# 22do_test select3-1.0 { 23 set fd [open data1.txt w] 24 for {set i 1} {$i<32} {incr i} { 25 for {set j 0} {pow(2,$j)<$i} {incr j} {} 26 puts $fd "$i\t$j" 27 } 28 close $fd 29 execsql { 30 CREATE TABLE t1(n int, log int); 31 COPY t1 FROM 'data1.txt' 32 } 33 file delete data1.txt 34 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 } 47} {1 0 31 5 496 124 16 4} 48do_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} 60do_test select3-2.3 { 61 execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log} 62} {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24} 63do_test select3-2.3 { 64 execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log} 65} {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25} 66do_test select3-2.4 { 67 execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log} 68} {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7} 69do_test select3-2.5 { 70 execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log} 71} {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7} 72do_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 { 79 SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y 80 } 81} {1 1 3 1 5 2 7 4 9 8 11 15} 82do_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} 87do_test select3-2.9 { 88 catchsql { 89 SELECT log, count(*) FROM t1 GROUP BY 8 ORDER BY log; 90 } 91} {1 {GROUP BY expressions should not be constant}} 92 93# Cannot have a HAVING without a GROUP BY 94# 95do_test select3-3.1 { 96 set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg] 97 lappend v $msg 98} {1 {a GROUP BY clause is required before HAVING}} 99 100# Toss in some HAVING clauses 101# 102do_test select3-4.1 { 103 execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log} 104} {4 8 5 15} 105do_test select3-4.2 { 106 execsql { 107 SELECT log, count(*) FROM t1 108 GROUP BY log 109 HAVING count(*)>=4 110 ORDER BY log 111 } 112} {3 4 4 8 5 15} 113do_test select3-4.3 { 114 execsql { 115 SELECT log, count(*) FROM t1 116 GROUP BY log 117 HAVING count(*)>=4 118 ORDER BY max(n) 119 } 120} {3 4 4 8 5 15} 121do_test select3-4.4 { 122 execsql { 123 SELECT log AS x, count(*) AS y FROM t1 124 GROUP BY x 125 HAVING y>=4 126 ORDER BY max(n) 127 } 128} {3 4 4 8 5 15} 129 130do_test select3-5.1 { 131 execsql { 132 SELECT log, count(*), avg(n), max(n+log*2) FROM t1 133 GROUP BY log 134 ORDER BY max(n+log*2), avg(n) 135 } 136} {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} 137do_test select3-5.2 { 138 execsql { 139 SELECT log, count(*), avg(n), max(n+log*2) FROM t1 140 GROUP BY log 141 ORDER BY max(n+log*2), min(log,avg(n)) 142 } 143} {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} 144 145# Test sorting of GROUP BY results in the presence of an index 146# on the GROUP BY column. 147# 148do_test select3-6.1 { 149 execsql { 150 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log; 151 } 152} {0 1 1 2 2 3 3 5 4 9 5 17} 153do_test select3-6.2 { 154 execsql { 155 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC; 156 } 157} {5 17 4 9 3 5 2 3 1 2 0 1} 158do_test select3-6.3 { 159 execsql { 160 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1; 161 } 162} {0 1 1 2 2 3 3 5 4 9 5 17} 163do_test select3-6.4 { 164 execsql { 165 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC; 166 } 167} {5 17 4 9 3 5 2 3 1 2 0 1} 168do_test select3-6.5 { 169 execsql { 170 CREATE INDEX i1 ON t1(log); 171 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log; 172 } 173} {0 1 1 2 2 3 3 5 4 9 5 17} 174do_test select3-6.6 { 175 execsql { 176 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC; 177 } 178} {5 17 4 9 3 5 2 3 1 2 0 1} 179do_test select3-6.7 { 180 execsql { 181 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1; 182 } 183} {0 1 1 2 2 3 3 5 4 9 5 17} 184do_test select3-6.8 { 185 execsql { 186 SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC; 187 } 188} {5 17 4 9 3 5 2 3 1 2 0 1} 189 190 191 192finish_test 193