1*4520Snw141292 2*4520Snw141292#pragma ident "%Z%%M% %I% %E% SMI" 3*4520Snw141292 4*4520Snw141292# 2001 September 15 5*4520Snw141292# 6*4520Snw141292# The author disclaims copyright to this source code. In place of 7*4520Snw141292# a legal notice, here is a blessing: 8*4520Snw141292# 9*4520Snw141292# May you do good and not evil. 10*4520Snw141292# May you find forgiveness for yourself and forgive others. 11*4520Snw141292# May you share freely, never taking more than you give. 12*4520Snw141292# 13*4520Snw141292#*********************************************************************** 14*4520Snw141292# This file implements regression tests for SQLite library. The 15*4520Snw141292# focus of this file is testing aggregate functions and the 16*4520Snw141292# GROUP BY and HAVING clauses of SELECT statements. 17*4520Snw141292# 18*4520Snw141292# $Id: select5.test,v 1.6 2001/10/15 00:44:36 drh Exp $ 19*4520Snw141292 20*4520Snw141292set testdir [file dirname $argv0] 21*4520Snw141292source $testdir/tester.tcl 22*4520Snw141292 23*4520Snw141292# Build some test data 24*4520Snw141292# 25*4520Snw141292set fd [open data1.txt w] 26*4520Snw141292for {set i 1} {$i<32} {incr i} { 27*4520Snw141292 for {set j 0} {pow(2,$j)<$i} {incr j} {} 28*4520Snw141292 puts $fd "[expr {32-$i}]\t[expr {10-$j}]" 29*4520Snw141292} 30*4520Snw141292close $fd 31*4520Snw141292execsql { 32*4520Snw141292 CREATE TABLE t1(x int, y int); 33*4520Snw141292 COPY t1 FROM 'data1.txt' 34*4520Snw141292} 35*4520Snw141292file delete data1.txt 36*4520Snw141292 37*4520Snw141292do_test select5-1.0 { 38*4520Snw141292 execsql {SELECT DISTINCT y FROM t1 ORDER BY y} 39*4520Snw141292} {5 6 7 8 9 10} 40*4520Snw141292 41*4520Snw141292# Sort by an aggregate function. 42*4520Snw141292# 43*4520Snw141292do_test select5-1.1 { 44*4520Snw141292 execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y} 45*4520Snw141292} {5 15 6 8 7 4 8 2 9 1 10 1} 46*4520Snw141292do_test select5-1.2 { 47*4520Snw141292 execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y} 48*4520Snw141292} {9 1 10 1 8 2 7 4 6 8 5 15} 49*4520Snw141292do_test select5-1.3 { 50*4520Snw141292 execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y} 51*4520Snw141292} {1 9 1 10 2 8 4 7 8 6 15 5} 52*4520Snw141292 53*4520Snw141292# Some error messages associated with aggregates and GROUP BY 54*4520Snw141292# 55*4520Snw141292do_test select5-2.1 { 56*4520Snw141292 set v [catch {execsql { 57*4520Snw141292 SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y 58*4520Snw141292 }} msg] 59*4520Snw141292 lappend v $msg 60*4520Snw141292} {1 {no such column: z}} 61*4520Snw141292do_test select5-2.2 { 62*4520Snw141292 set v [catch {execsql { 63*4520Snw141292 SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y 64*4520Snw141292 }} msg] 65*4520Snw141292 lappend v $msg 66*4520Snw141292} {1 {no such function: z}} 67*4520Snw141292do_test select5-2.3 { 68*4520Snw141292 set v [catch {execsql { 69*4520Snw141292 SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y 70*4520Snw141292 }} msg] 71*4520Snw141292 lappend v $msg 72*4520Snw141292} {0 {8 2 9 1 10 1}} 73*4520Snw141292do_test select5-2.4 { 74*4520Snw141292 set v [catch {execsql { 75*4520Snw141292 SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y 76*4520Snw141292 }} msg] 77*4520Snw141292 lappend v $msg 78*4520Snw141292} {1 {no such function: z}} 79*4520Snw141292do_test select5-2.5 { 80*4520Snw141292 set v [catch {execsql { 81*4520Snw141292 SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y 82*4520Snw141292 }} msg] 83*4520Snw141292 lappend v $msg 84*4520Snw141292} {1 {no such column: z}} 85*4520Snw141292 86*4520Snw141292# Get the Agg function to rehash in vdbe.c 87*4520Snw141292# 88*4520Snw141292do_test select5-3.1 { 89*4520Snw141292 execsql { 90*4520Snw141292 SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x 91*4520Snw141292 } 92*4520Snw141292} {1 1 5 2 1 5 3 1 5} 93*4520Snw141292 94*4520Snw141292# Run various aggregate functions when the count is zero. 95*4520Snw141292# 96*4520Snw141292do_test select5-4.1 { 97*4520Snw141292 execsql { 98*4520Snw141292 SELECT avg(x) FROM t1 WHERE x>100 99*4520Snw141292 } 100*4520Snw141292} {{}} 101*4520Snw141292do_test select5-4.2 { 102*4520Snw141292 execsql { 103*4520Snw141292 SELECT count(x) FROM t1 WHERE x>100 104*4520Snw141292 } 105*4520Snw141292} {0} 106*4520Snw141292do_test select5-4.3 { 107*4520Snw141292 execsql { 108*4520Snw141292 SELECT min(x) FROM t1 WHERE x>100 109*4520Snw141292 } 110*4520Snw141292} {{}} 111*4520Snw141292do_test select5-4.4 { 112*4520Snw141292 execsql { 113*4520Snw141292 SELECT max(x) FROM t1 WHERE x>100 114*4520Snw141292 } 115*4520Snw141292} {{}} 116*4520Snw141292do_test select5-4.5 { 117*4520Snw141292 execsql { 118*4520Snw141292 SELECT sum(x) FROM t1 WHERE x>100 119*4520Snw141292 } 120*4520Snw141292} {0} 121*4520Snw141292 122*4520Snw141292finish_test 123