drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 1 | # Copyright (c) 1999, 2000 D. Richard Hipp |
| 2 | # |
| 3 | # This program is free software; you can redistribute it and/or |
| 4 | # modify it under the terms of the GNU General Public |
| 5 | # License as published by the Free Software Foundation; either |
| 6 | # version 2 of the License, or (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | # General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public |
| 14 | # License along with this library; if not, write to the |
| 15 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | # Boston, MA 02111-1307, USA. |
| 17 | # |
| 18 | # Author contact information: |
| 19 | # drh@hwaci.com |
| 20 | # http://www.hwaci.com/drh/ |
| 21 | # |
| 22 | #*********************************************************************** |
| 23 | # This file implements regression tests for SQLite library. The |
| 24 | # focus of this file is testing aggregate functions and the |
| 25 | # GROUP BY and HAVING clauses of SELECT statements. |
| 26 | # |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame^] | 27 | # $Id: select5.test,v 1.3 2000/06/08 16:26:25 drh Exp $ |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 28 | |
| 29 | set testdir [file dirname $argv0] |
| 30 | source $testdir/tester.tcl |
| 31 | |
| 32 | # Build some test data |
| 33 | # |
| 34 | set fd [open data1.txt w] |
| 35 | for {set i 1} {$i<32} {incr i} { |
| 36 | for {set j 0} {pow(2,$j)<$i} {incr j} {} |
| 37 | puts $fd "[expr {32-$i}]\t[expr {10-$j}]" |
| 38 | } |
| 39 | close $fd |
| 40 | execsql { |
| 41 | CREATE TABLE t1(x int, y int); |
| 42 | COPY t1 FROM 'data1.txt' |
| 43 | } |
| 44 | file delete data1.txt |
| 45 | |
| 46 | do_test select5-1.0 { |
| 47 | execsql {SELECT DISTINCT y FROM t1 ORDER BY y} |
| 48 | } {5 6 7 8 9 10} |
| 49 | |
| 50 | # Sort by an aggregate function. |
| 51 | # |
| 52 | do_test select5-1.1 { |
| 53 | execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y} |
| 54 | } {5 15 6 8 7 4 8 2 9 1 10 1} |
| 55 | do_test select5-1.2 { |
| 56 | execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y} |
| 57 | } {9 1 10 1 8 2 7 4 6 8 5 15} |
drh | aaf8872 | 2000-06-08 11:25:00 +0000 | [diff] [blame] | 58 | do_test select5-1.3 { |
| 59 | execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y} |
| 60 | } {1 9 1 10 2 8 4 7 8 6 15 5} |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 61 | |
drh | c837e70 | 2000-06-08 16:26:24 +0000 | [diff] [blame^] | 62 | # Some error messages associated with aggregates and GROUP BY |
| 63 | # |
| 64 | do_test select5-2.1 { |
| 65 | set v [catch {execsql { |
| 66 | SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y |
| 67 | }} msg] |
| 68 | lappend v $msg |
| 69 | } {1 {no such field: z}} |
| 70 | do_test select5-2.2 { |
| 71 | set v [catch {execsql { |
| 72 | SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y |
| 73 | }} msg] |
| 74 | lappend v $msg |
| 75 | } {1 {no such function: z}} |
| 76 | do_test select5-2.3 { |
| 77 | set v [catch {execsql { |
| 78 | SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y |
| 79 | }} msg] |
| 80 | lappend v $msg |
| 81 | } {0 {8 2 9 1 10 1}} |
| 82 | do_test select5-2.4 { |
| 83 | set v [catch {execsql { |
| 84 | SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y |
| 85 | }} msg] |
| 86 | lappend v $msg |
| 87 | } {1 {no such function: z}} |
| 88 | do_test select5-2.5 { |
| 89 | set v [catch {execsql { |
| 90 | SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y |
| 91 | }} msg] |
| 92 | lappend v $msg |
| 93 | } {1 {no such field: z}} |
| 94 | |
| 95 | # Get the Agg function to rehash in vdbe.c |
| 96 | # |
| 97 | do_test select5-3.1 { |
| 98 | execsql { |
| 99 | SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x |
| 100 | } |
| 101 | } {1 1 5 2 1 5 3 1 5} |
| 102 | |
| 103 | # Run the AVG() function when the count is zero. |
| 104 | # |
| 105 | do_test select5-4.1 { |
| 106 | execsql { |
| 107 | SELECT avg(x) FROM t1 WHERE x>100 |
| 108 | } |
| 109 | } {} |
| 110 | |
drh | 191b690 | 2000-06-08 11:13:01 +0000 | [diff] [blame] | 111 | finish_test |