sqlite/test/select5.test
danielk1977 e257300f2e Allow GROUP BY on non-aggregate queries. Ticket #1064 (CVS 2276)
FossilOrigin-Name: 0642d3e3d6636a5f922f75c05252c9c1372d3936
2005-01-26 03:58:35 +00:00

157 lines
3.7 KiB
Plaintext

# 2001 September 15
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing aggregate functions and the
# GROUP BY and HAVING clauses of SELECT statements.
#
# $Id: select5.test,v 1.9 2005/01/26 03:58:36 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Build some test data
#
execsql {
CREATE TABLE t1(x int, y int);
BEGIN;
}
for {set i 1} {$i<32} {incr i} {
for {set j 0} {pow(2,$j)<$i} {incr j} {}
execsql "INSERT INTO t1 VALUES([expr {32-$i}],[expr {10-$j}])"
}
execsql {
COMMIT
}
do_test select5-1.0 {
execsql {SELECT DISTINCT y FROM t1 ORDER BY y}
} {5 6 7 8 9 10}
# Sort by an aggregate function.
#
do_test select5-1.1 {
execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y}
} {5 15 6 8 7 4 8 2 9 1 10 1}
do_test select5-1.2 {
execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y}
} {9 1 10 1 8 2 7 4 6 8 5 15}
do_test select5-1.3 {
execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y}
} {1 9 1 10 2 8 4 7 8 6 15 5}
# Some error messages associated with aggregates and GROUP BY
#
do_test select5-2.1 {
set v [catch {execsql {
SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
}} msg]
lappend v $msg
} {1 {no such column: z}}
do_test select5-2.2 {
set v [catch {execsql {
SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
}} msg]
lappend v $msg
} {1 {no such function: z}}
do_test select5-2.3 {
set v [catch {execsql {
SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
}} msg]
lappend v $msg
} {0 {8 2 9 1 10 1}}
do_test select5-2.4 {
set v [catch {execsql {
SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
}} msg]
lappend v $msg
} {1 {no such function: z}}
do_test select5-2.5 {
set v [catch {execsql {
SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
}} msg]
lappend v $msg
} {1 {no such column: z}}
# Get the Agg function to rehash in vdbe.c
#
do_test select5-3.1 {
execsql {
SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
}
} {1 1 5.0 2 1 5.0 3 1 5.0}
# Run various aggregate functions when the count is zero.
#
do_test select5-4.1 {
execsql {
SELECT avg(x) FROM t1 WHERE x>100
}
} {{}}
do_test select5-4.2 {
execsql {
SELECT count(x) FROM t1 WHERE x>100
}
} {0}
do_test select5-4.3 {
execsql {
SELECT min(x) FROM t1 WHERE x>100
}
} {{}}
do_test select5-4.4 {
execsql {
SELECT max(x) FROM t1 WHERE x>100
}
} {{}}
do_test select5-4.5 {
execsql {
SELECT sum(x) FROM t1 WHERE x>100
}
} {0.0}
# Some tests for queries with a GROUP BY clause but no aggregate functions.
#
# Note: The query in test case 5-5.5 are not legal SQL. So if the
# implementation changes in the future and it returns different results,
# this is not such a big deal.
#
do_test select5-5.1 {
execsql {
CREATE TABLE t2(a, b, c);
INSERT INTO t2 VALUES(1, 2, 3);
INSERT INTO t2 VALUES(1, 4, 5);
INSERT INTO t2 VALUES(6, 4, 7);
CREATE INDEX t2_idx ON t2(a);
}
} {}
do_test select5-5.2 {
execsql {
SELECT a FROM t2 GROUP BY a;
}
} {1 6}
do_test select5-5.3 {
execsql {
SELECT a FROM t2 WHERE a>2 GROUP BY a;
}
} {6}
do_test select5-5.4 {
execsql {
SELECT a, b FROM t2 GROUP BY a, b;
}
} {1 2 1 4 6 4}
do_test select5-5.5 {
execsql {
SELECT a, b FROM t2 GROUP BY a;
}
} {1 2 6 4}
finish_test