2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A# The author disclaims copyright to this source code. In place of
2N/A# a legal notice, here is a blessing:
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# 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/Aset testdir [file dirname $argv0]
2N/A# Build some test data
2N/Afor {set i 1} {$i<32} {incr i} {
2N/A for {set j 0} {pow(2,$j)<$i} {incr j} {}
2N/A puts $fd "[expr {32-$i}]\t[expr {10-$j}]"
2N/A CREATE TABLE t1(x int, y int);
2N/Ado_test select5-1.0 {
2N/A execsql {SELECT DISTINCT y FROM t1 ORDER BY y}
2N/A# Sort by an aggregate function.
2N/Ado_test select5-1.1 {
2N/A execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y}
2N/A} {5 15 6 8 7 4 8 2 9 1 10 1}
2N/Ado_test select5-1.2 {
2N/A execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y}
2N/A} {9 1 10 1 8 2 7 4 6 8 5 15}
2N/Ado_test select5-1.3 {
2N/A execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y}
2N/A} {1 9 1 10 2 8 4 7 8 6 15 5}
2N/A# Some error messages associated with aggregates and GROUP BY
2N/Ado_test select5-2.1 {
2N/A set v [catch {execsql {
2N/A SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
2N/A} {1 {no such column: z}}
2N/Ado_test select5-2.2 {
2N/A set v [catch {execsql {
2N/A SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
2N/A} {1 {no such function: z}}
2N/Ado_test select5-2.3 {
2N/A set v [catch {execsql {
2N/A SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
2N/Ado_test select5-2.4 {
2N/A set v [catch {execsql {
2N/A SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
2N/A} {1 {no such function: z}}
2N/Ado_test select5-2.5 {
2N/A set v [catch {execsql {
2N/A SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
2N/A} {1 {no such column: z}}
2N/Ado_test select5-3.1 {
2N/A SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
2N/A} {1 1 5 2 1 5 3 1 5}
2N/A# Run various aggregate functions when the count is zero.
2N/Ado_test select5-4.1 {
2N/A SELECT avg(x) FROM t1 WHERE x>100
2N/Ado_test select5-4.2 {
2N/A SELECT count(x) FROM t1 WHERE x>100
2N/Ado_test select5-4.3 {
2N/A SELECT min(x) FROM t1 WHERE x>100
2N/Ado_test select5-4.4 {
2N/A SELECT max(x) FROM t1 WHERE x>100
2N/Ado_test select5-4.5 {
2N/A SELECT sum(x) FROM t1 WHERE x>100