2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A# 2001 September 15
2N/A#
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
2N/A#
2N/A# May you do good and not evil.
2N/A# May you find forgiveness for yourself and forgive others.
2N/A# May you share freely, never taking more than you give.
2N/A#
2N/A#***********************************************************************
2N/A# This file implements regression tests for SQLite library. The
2N/A# focus of this file is testing aggregate functions and the
2N/A# GROUP BY and HAVING clauses of SELECT statements.
2N/A#
2N/A# $Id: select3.test,v 1.8 2003/01/31 17:16:37 drh Exp $
2N/A
2N/Aset testdir [file dirname $argv0]
2N/Asource $testdir/tester.tcl
2N/A
2N/A# Build some test data
2N/A#
2N/Ado_test select3-1.0 {
2N/A set fd [open data1.txt w]
2N/A for {set i 1} {$i<32} {incr i} {
2N/A for {set j 0} {pow(2,$j)<$i} {incr j} {}
2N/A puts $fd "$i\t$j"
2N/A }
2N/A close $fd
2N/A execsql {
2N/A CREATE TABLE t1(n int, log int);
2N/A COPY t1 FROM 'data1.txt'
2N/A }
2N/A file delete data1.txt
2N/A execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
2N/A} {0 1 2 3 4 5}
2N/A
2N/A# Basic aggregate functions.
2N/A#
2N/Ado_test select3-1.1 {
2N/A execsql {SELECT count(*) FROM t1}
2N/A} {31}
2N/Ado_test select3-1.2 {
2N/A execsql {
2N/A SELECT min(n),min(log),max(n),max(log),sum(n),sum(log),avg(n),avg(log)
2N/A FROM t1
2N/A }
2N/A} {1 0 31 5 496 124 16 4}
2N/Ado_test select3-1.3 {
2N/A execsql {SELECT max(n)/avg(n), max(log)/avg(log) FROM t1}
2N/A} {1.9375 1.25}
2N/A
2N/A# Try some basic GROUP BY clauses
2N/A#
2N/Ado_test select3-2.1 {
2N/A execsql {SELECT log, count(*) FROM t1 GROUP BY log ORDER BY log}
2N/A} {0 1 1 1 2 2 3 4 4 8 5 15}
2N/Ado_test select3-2.2 {
2N/A execsql {SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log}
2N/A} {0 1 1 2 2 3 3 5 4 9 5 17}
2N/Ado_test select3-2.3 {
2N/A execsql {SELECT log, avg(n) FROM t1 GROUP BY log ORDER BY log}
2N/A} {0 1 1 2 2 3.5 3 6.5 4 12.5 5 24}
2N/Ado_test select3-2.3 {
2N/A execsql {SELECT log, avg(n)+1 FROM t1 GROUP BY log ORDER BY log}
2N/A} {0 2 1 3 2 4.5 3 7.5 4 13.5 5 25}
2N/Ado_test select3-2.4 {
2N/A execsql {SELECT log, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
2N/A} {0 0 1 0 2 0.5 3 1.5 4 3.5 5 7}
2N/Ado_test select3-2.5 {
2N/A execsql {SELECT log*2+1, avg(n)-min(n) FROM t1 GROUP BY log ORDER BY log}
2N/A} {1 0 3 0 5 0.5 7 1.5 9 3.5 11 7}
2N/Ado_test select3-2.6 {
2N/A execsql {
2N/A SELECT log*2+1 as x, count(*) FROM t1 GROUP BY x ORDER BY x
2N/A }
2N/A} {1 1 3 1 5 2 7 4 9 8 11 15}
2N/Ado_test select3-2.7 {
2N/A execsql {
2N/A SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY y
2N/A }
2N/A} {1 1 3 1 5 2 7 4 9 8 11 15}
2N/Ado_test select3-2.8 {
2N/A execsql {
2N/A SELECT log*2+1 AS x, count(*) AS y FROM t1 GROUP BY x ORDER BY 10-(x+y)
2N/A }
2N/A} {11 15 9 8 7 4 5 2 3 1 1 1}
2N/Ado_test select3-2.9 {
2N/A catchsql {
2N/A SELECT log, count(*) FROM t1 GROUP BY 'x' ORDER BY log;
2N/A }
2N/A} {1 {GROUP BY terms must not be non-integer constants}}
2N/Ado_test select3-2.10 {
2N/A catchsql {
2N/A SELECT log, count(*) FROM t1 GROUP BY 0 ORDER BY log;
2N/A }
2N/A} {1 {GROUP BY column number 0 out of range - should be between 1 and 2}}
2N/Ado_test select3-2.11 {
2N/A catchsql {
2N/A SELECT log, count(*) FROM t1 GROUP BY 3 ORDER BY log;
2N/A }
2N/A} {1 {GROUP BY column number 3 out of range - should be between 1 and 2}}
2N/Ado_test select3-2.12 {
2N/A catchsql {
2N/A SELECT log, count(*) FROM t1 GROUP BY 1 ORDER BY log;
2N/A }
2N/A} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
2N/A#do_test select3-2.13 {
2N/A# catchsql {
2N/A# SELECT log, count(*) FROM t1 GROUP BY 2 ORDER BY log;
2N/A# }
2N/A#} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
2N/A#do_test select3-2.14 {
2N/A# catchsql {
2N/A# SELECT log, count(*) FROM t1 GROUP BY count(*) ORDER BY log;
2N/A# }
2N/A#} {0 {0 1 1 1 2 2 3 4 4 8 5 15}}
2N/A
2N/A# Cannot have a HAVING without a GROUP BY
2N/A#
2N/Ado_test select3-3.1 {
2N/A set v [catch {execsql {SELECT log, count(*) FROM t1 HAVING log>=4}} msg]
2N/A lappend v $msg
2N/A} {1 {a GROUP BY clause is required before HAVING}}
2N/A
2N/A# Toss in some HAVING clauses
2N/A#
2N/Ado_test select3-4.1 {
2N/A execsql {SELECT log, count(*) FROM t1 GROUP BY log HAVING log>=4 ORDER BY log}
2N/A} {4 8 5 15}
2N/Ado_test select3-4.2 {
2N/A execsql {
2N/A SELECT log, count(*) FROM t1
2N/A GROUP BY log
2N/A HAVING count(*)>=4
2N/A ORDER BY log
2N/A }
2N/A} {3 4 4 8 5 15}
2N/Ado_test select3-4.3 {
2N/A execsql {
2N/A SELECT log, count(*) FROM t1
2N/A GROUP BY log
2N/A HAVING count(*)>=4
2N/A ORDER BY max(n)
2N/A }
2N/A} {3 4 4 8 5 15}
2N/Ado_test select3-4.4 {
2N/A execsql {
2N/A SELECT log AS x, count(*) AS y FROM t1
2N/A GROUP BY x
2N/A HAVING y>=4
2N/A ORDER BY max(n)
2N/A }
2N/A} {3 4 4 8 5 15}
2N/Ado_test select3-4.5 {
2N/A execsql {
2N/A SELECT log AS x FROM t1
2N/A GROUP BY x
2N/A HAVING count(*)>=4
2N/A ORDER BY max(n)
2N/A }
2N/A} {3 4 5}
2N/A
2N/Ado_test select3-5.1 {
2N/A execsql {
2N/A SELECT log, count(*), avg(n), max(n+log*2) FROM t1
2N/A GROUP BY log
2N/A ORDER BY max(n+log*2), avg(n)
2N/A }
2N/A} {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}
2N/Ado_test select3-5.2 {
2N/A execsql {
2N/A SELECT log, count(*), avg(n), max(n+log*2) FROM t1
2N/A GROUP BY log
2N/A ORDER BY max(n+log*2), min(log,avg(n))
2N/A }
2N/A} {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}
2N/A
2N/A# Test sorting of GROUP BY results in the presence of an index
2N/A# on the GROUP BY column.
2N/A#
2N/Ado_test select3-6.1 {
2N/A execsql {
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
2N/A }
2N/A} {0 1 1 2 2 3 3 5 4 9 5 17}
2N/Ado_test select3-6.2 {
2N/A execsql {
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
2N/A }
2N/A} {5 17 4 9 3 5 2 3 1 2 0 1}
2N/Ado_test select3-6.3 {
2N/A execsql {
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
2N/A }
2N/A} {0 1 1 2 2 3 3 5 4 9 5 17}
2N/Ado_test select3-6.4 {
2N/A execsql {
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
2N/A }
2N/A} {5 17 4 9 3 5 2 3 1 2 0 1}
2N/Ado_test select3-6.5 {
2N/A execsql {
2N/A CREATE INDEX i1 ON t1(log);
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log;
2N/A }
2N/A} {0 1 1 2 2 3 3 5 4 9 5 17}
2N/Ado_test select3-6.6 {
2N/A execsql {
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY log DESC;
2N/A }
2N/A} {5 17 4 9 3 5 2 3 1 2 0 1}
2N/Ado_test select3-6.7 {
2N/A execsql {
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1;
2N/A }
2N/A} {0 1 1 2 2 3 3 5 4 9 5 17}
2N/Ado_test select3-6.8 {
2N/A execsql {
2N/A SELECT log, min(n) FROM t1 GROUP BY log ORDER BY 1 DESC;
2N/A }
2N/A} {5 17 4 9 3 5 2 3 1 2 0 1}
2N/A
2N/A
2N/A
2N/Afinish_test