drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 1 | # 2001 September 15 |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 2 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 3 | # The author disclaims copyright to this source code. In place of |
| 4 | # a legal notice, here is a blessing: |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 5 | # |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 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. |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 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 | # |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame^] | 15 | # $Id: select5.test,v 1.8 2004/08/20 18:34:20 drh Exp $ |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Build some test data |
| 21 | # |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 22 | execsql { |
| 23 | CREATE TABLE t1(x int, y int); |
drh | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame] | 24 | BEGIN; |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 25 | } |
drh | 5f3b4ab | 2004-05-27 17:22:54 +0000 | [diff] [blame] | 26 | for {set i 1} {$i<32} {incr i} { |
| 27 | for {set j 0} {pow(2,$j)<$i} {incr j} {} |
| 28 | execsql "INSERT INTO t1 VALUES([expr {32-$i}],[expr {10-$j}])" |
| 29 | } |
| 30 | execsql { |
| 31 | COMMIT |
| 32 | } |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 33 | |
| 34 | do_test select5-1.0 { |
| 35 | execsql {SELECT DISTINCT y FROM t1 ORDER BY y} |
| 36 | } {5 6 7 8 9 10} |
| 37 | |
| 38 | # Sort by an aggregate function. |
| 39 | # |
| 40 | do_test select5-1.1 { |
| 41 | execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y} |
| 42 | } {5 15 6 8 7 4 8 2 9 1 10 1} |
| 43 | do_test select5-1.2 { |
| 44 | execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y} |
| 45 | } {9 1 10 1 8 2 7 4 6 8 5 15} |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 46 | do_test select5-1.3 { |
| 47 | execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y} |
| 48 | } {1 9 1 10 2 8 4 7 8 6 15 5} |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 49 | |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 50 | # Some error messages associated with aggregates and GROUP BY |
| 51 | # |
| 52 | do_test select5-2.1 { |
| 53 | set v [catch {execsql { |
| 54 | SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y |
| 55 | }} msg] |
| 56 | lappend v $msg |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 57 | } {1 {no such column: z}} |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 58 | do_test select5-2.2 { |
| 59 | set v [catch {execsql { |
| 60 | SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y |
| 61 | }} msg] |
| 62 | lappend v $msg |
| 63 | } {1 {no such function: z}} |
| 64 | do_test select5-2.3 { |
| 65 | set v [catch {execsql { |
| 66 | SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y |
| 67 | }} msg] |
| 68 | lappend v $msg |
| 69 | } {0 {8 2 9 1 10 1}} |
| 70 | do_test select5-2.4 { |
| 71 | set v [catch {execsql { |
| 72 | SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y |
| 73 | }} msg] |
| 74 | lappend v $msg |
| 75 | } {1 {no such function: z}} |
| 76 | do_test select5-2.5 { |
| 77 | set v [catch {execsql { |
| 78 | SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y |
| 79 | }} msg] |
| 80 | lappend v $msg |
drh | 967e8b7 | 2000-06-21 13:59:10 +0000 | [diff] [blame] | 81 | } {1 {no such column: z}} |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 82 | |
| 83 | # Get the Agg function to rehash in vdbe.c |
| 84 | # |
| 85 | do_test select5-3.1 { |
| 86 | execsql { |
| 87 | SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x |
| 88 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame^] | 89 | } {1 1 5.0 2 1 5.0 3 1 5.0} |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 90 | |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 91 | # Run various aggregate functions when the count is zero. |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 92 | # |
| 93 | do_test select5-4.1 { |
| 94 | execsql { |
| 95 | SELECT avg(x) FROM t1 WHERE x>100 |
| 96 | } |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 97 | } {{}} |
| 98 | do_test select5-4.2 { |
| 99 | execsql { |
| 100 | SELECT count(x) FROM t1 WHERE x>100 |
| 101 | } |
| 102 | } {0} |
| 103 | do_test select5-4.3 { |
| 104 | execsql { |
| 105 | SELECT min(x) FROM t1 WHERE x>100 |
| 106 | } |
| 107 | } {{}} |
| 108 | do_test select5-4.4 { |
| 109 | execsql { |
| 110 | SELECT max(x) FROM t1 WHERE x>100 |
| 111 | } |
| 112 | } {{}} |
| 113 | do_test select5-4.5 { |
| 114 | execsql { |
| 115 | SELECT sum(x) FROM t1 WHERE x>100 |
| 116 | } |
drh | 92febd9 | 2004-08-20 18:34:20 +0000 | [diff] [blame^] | 117 | } {0.0} |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame] | 118 | |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 119 | finish_test |