xref: /titanic_52/usr/src/lib/libsqlite/test/select5.test (revision c5c4113dfcabb1eed3d4bdf7609de5170027a794)
1*c5c4113dSnw141292
2*c5c4113dSnw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*c5c4113dSnw141292
4*c5c4113dSnw141292# 2001 September 15
5*c5c4113dSnw141292#
6*c5c4113dSnw141292# The author disclaims copyright to this source code.  In place of
7*c5c4113dSnw141292# a legal notice, here is a blessing:
8*c5c4113dSnw141292#
9*c5c4113dSnw141292#    May you do good and not evil.
10*c5c4113dSnw141292#    May you find forgiveness for yourself and forgive others.
11*c5c4113dSnw141292#    May you share freely, never taking more than you give.
12*c5c4113dSnw141292#
13*c5c4113dSnw141292#***********************************************************************
14*c5c4113dSnw141292# This file implements regression tests for SQLite library.  The
15*c5c4113dSnw141292# focus of this file is testing aggregate functions and the
16*c5c4113dSnw141292# GROUP BY and HAVING clauses of SELECT statements.
17*c5c4113dSnw141292#
18*c5c4113dSnw141292# $Id: select5.test,v 1.6 2001/10/15 00:44:36 drh Exp $
19*c5c4113dSnw141292
20*c5c4113dSnw141292set testdir [file dirname $argv0]
21*c5c4113dSnw141292source $testdir/tester.tcl
22*c5c4113dSnw141292
23*c5c4113dSnw141292# Build some test data
24*c5c4113dSnw141292#
25*c5c4113dSnw141292set fd [open data1.txt w]
26*c5c4113dSnw141292for {set i 1} {$i<32} {incr i} {
27*c5c4113dSnw141292  for {set j 0} {pow(2,$j)<$i} {incr j} {}
28*c5c4113dSnw141292  puts $fd "[expr {32-$i}]\t[expr {10-$j}]"
29*c5c4113dSnw141292}
30*c5c4113dSnw141292close $fd
31*c5c4113dSnw141292execsql {
32*c5c4113dSnw141292  CREATE TABLE t1(x int, y int);
33*c5c4113dSnw141292  COPY t1 FROM 'data1.txt'
34*c5c4113dSnw141292}
35*c5c4113dSnw141292file delete data1.txt
36*c5c4113dSnw141292
37*c5c4113dSnw141292do_test select5-1.0 {
38*c5c4113dSnw141292  execsql {SELECT DISTINCT y FROM t1 ORDER BY y}
39*c5c4113dSnw141292} {5 6 7 8 9 10}
40*c5c4113dSnw141292
41*c5c4113dSnw141292# Sort by an aggregate function.
42*c5c4113dSnw141292#
43*c5c4113dSnw141292do_test select5-1.1 {
44*c5c4113dSnw141292  execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY y}
45*c5c4113dSnw141292} {5 15 6 8 7 4 8 2 9 1 10 1}
46*c5c4113dSnw141292do_test select5-1.2 {
47*c5c4113dSnw141292  execsql {SELECT y, count(*) FROM t1 GROUP BY y ORDER BY count(*), y}
48*c5c4113dSnw141292} {9 1 10 1 8 2 7 4 6 8 5 15}
49*c5c4113dSnw141292do_test select5-1.3 {
50*c5c4113dSnw141292  execsql {SELECT count(*), y FROM t1 GROUP BY y ORDER BY count(*), y}
51*c5c4113dSnw141292} {1 9 1 10 2 8 4 7 8 6 15 5}
52*c5c4113dSnw141292
53*c5c4113dSnw141292# Some error messages associated with aggregates and GROUP BY
54*c5c4113dSnw141292#
55*c5c4113dSnw141292do_test select5-2.1 {
56*c5c4113dSnw141292  set v [catch {execsql {
57*c5c4113dSnw141292    SELECT y, count(*) FROM t1 GROUP BY z ORDER BY y
58*c5c4113dSnw141292  }} msg]
59*c5c4113dSnw141292  lappend v $msg
60*c5c4113dSnw141292} {1 {no such column: z}}
61*c5c4113dSnw141292do_test select5-2.2 {
62*c5c4113dSnw141292  set v [catch {execsql {
63*c5c4113dSnw141292    SELECT y, count(*) FROM t1 GROUP BY z(y) ORDER BY y
64*c5c4113dSnw141292  }} msg]
65*c5c4113dSnw141292  lappend v $msg
66*c5c4113dSnw141292} {1 {no such function: z}}
67*c5c4113dSnw141292do_test select5-2.3 {
68*c5c4113dSnw141292  set v [catch {execsql {
69*c5c4113dSnw141292    SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<3 ORDER BY y
70*c5c4113dSnw141292  }} msg]
71*c5c4113dSnw141292  lappend v $msg
72*c5c4113dSnw141292} {0 {8 2 9 1 10 1}}
73*c5c4113dSnw141292do_test select5-2.4 {
74*c5c4113dSnw141292  set v [catch {execsql {
75*c5c4113dSnw141292    SELECT y, count(*) FROM t1 GROUP BY y HAVING z(y)<3 ORDER BY y
76*c5c4113dSnw141292  }} msg]
77*c5c4113dSnw141292  lappend v $msg
78*c5c4113dSnw141292} {1 {no such function: z}}
79*c5c4113dSnw141292do_test select5-2.5 {
80*c5c4113dSnw141292  set v [catch {execsql {
81*c5c4113dSnw141292    SELECT y, count(*) FROM t1 GROUP BY y HAVING count(*)<z ORDER BY y
82*c5c4113dSnw141292  }} msg]
83*c5c4113dSnw141292  lappend v $msg
84*c5c4113dSnw141292} {1 {no such column: z}}
85*c5c4113dSnw141292
86*c5c4113dSnw141292# Get the Agg function to rehash in vdbe.c
87*c5c4113dSnw141292#
88*c5c4113dSnw141292do_test select5-3.1 {
89*c5c4113dSnw141292  execsql {
90*c5c4113dSnw141292    SELECT x, count(*), avg(y) FROM t1 GROUP BY x HAVING x<4 ORDER BY x
91*c5c4113dSnw141292  }
92*c5c4113dSnw141292} {1 1 5 2 1 5 3 1 5}
93*c5c4113dSnw141292
94*c5c4113dSnw141292# Run various aggregate functions when the count is zero.
95*c5c4113dSnw141292#
96*c5c4113dSnw141292do_test select5-4.1 {
97*c5c4113dSnw141292  execsql {
98*c5c4113dSnw141292    SELECT avg(x) FROM t1 WHERE x>100
99*c5c4113dSnw141292  }
100*c5c4113dSnw141292} {{}}
101*c5c4113dSnw141292do_test select5-4.2 {
102*c5c4113dSnw141292  execsql {
103*c5c4113dSnw141292    SELECT count(x) FROM t1 WHERE x>100
104*c5c4113dSnw141292  }
105*c5c4113dSnw141292} {0}
106*c5c4113dSnw141292do_test select5-4.3 {
107*c5c4113dSnw141292  execsql {
108*c5c4113dSnw141292    SELECT min(x) FROM t1 WHERE x>100
109*c5c4113dSnw141292  }
110*c5c4113dSnw141292} {{}}
111*c5c4113dSnw141292do_test select5-4.4 {
112*c5c4113dSnw141292  execsql {
113*c5c4113dSnw141292    SELECT max(x) FROM t1 WHERE x>100
114*c5c4113dSnw141292  }
115*c5c4113dSnw141292} {{}}
116*c5c4113dSnw141292do_test select5-4.5 {
117*c5c4113dSnw141292  execsql {
118*c5c4113dSnw141292    SELECT sum(x) FROM t1 WHERE x>100
119*c5c4113dSnw141292  }
120*c5c4113dSnw141292} {0}
121*c5c4113dSnw141292
122*c5c4113dSnw141292finish_test
123