3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this file is testing "SELECT count(*)" statements.
14 # $Id: count.test,v 1.6 2009/06/05 17:09:12 drh Exp $
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
21 # count-0.*: Make sure count(*) works on an empty database. (Ticket #3774)
23 # count-1.*: Test that the OP_Count instruction appears to work on both
24 # tables and indexes. Test both when they contain 0 entries,
25 # when all entries are on the root page, and when the b-tree
26 # forms a structure 2 and 3 levels deep.
28 # count-2.*: Test that
34 SELECT count(*) FROM sqlite_master;
39 foreach zIndex [list {
42 CREATE INDEX i1 ON t1(a);
45 do_test count-1.$iTest.1 {
47 DROP TABLE IF EXISTS t1;
48 CREATE TABLE t1(a, b);
51 execsql { SELECT count(*) FROM t1 }
54 do_test count-1.$iTest.2 {
56 INSERT INTO t1 VALUES(1, 2);
57 INSERT INTO t1 VALUES(3, 4);
58 SELECT count(*) FROM t1;
62 do_test count-1.$iTest.3 {
64 INSERT INTO t1 SELECT * FROM t1; -- 4
65 INSERT INTO t1 SELECT * FROM t1; -- 8
66 INSERT INTO t1 SELECT * FROM t1; -- 16
67 INSERT INTO t1 SELECT * FROM t1; -- 32
68 INSERT INTO t1 SELECT * FROM t1; -- 64
69 INSERT INTO t1 SELECT * FROM t1; -- 128
70 INSERT INTO t1 SELECT * FROM t1; -- 256
71 SELECT count(*) FROM t1;
75 do_test count-1.$iTest.4 {
77 INSERT INTO t1 SELECT * FROM t1; -- 512
78 INSERT INTO t1 SELECT * FROM t1; -- 1024
79 INSERT INTO t1 SELECT * FROM t1; -- 2048
80 INSERT INTO t1 SELECT * FROM t1; -- 4096
81 SELECT count(*) FROM t1;
85 do_test count-1.$iTest.5 {
88 INSERT INTO t1 SELECT * FROM t1; -- 8192
89 INSERT INTO t1 SELECT * FROM t1; -- 16384
90 INSERT INTO t1 SELECT * FROM t1; -- 32768
91 INSERT INTO t1 SELECT * FROM t1; -- 65536
93 SELECT count(*) FROM t1;
98 proc uses_op_count {sql} {
99 if {[lsearch [execsql "EXPLAIN $sql"] Count]>=0} {
107 CREATE TABLE t2(a, b);
109 uses_op_count {SELECT count(*) FROM t2}
112 catchsql {SELECT count(DISTINCT *) FROM t2}
113 } {1 {near "*": syntax error}}
115 uses_op_count {SELECT count(DISTINCT a) FROM t2}
118 uses_op_count {SELECT count(a) FROM t2}
121 uses_op_count {SELECT count() FROM t2}
124 catchsql {SELECT count(DISTINCT) FROM t2}
125 } {1 {DISTINCT aggregates must have exactly one argument}}
127 uses_op_count {SELECT count(*)+1 FROM t2}
130 uses_op_count {SELECT count(*) FROM t2 WHERE a IS NOT NULL}
133 catchsql {SELECT count(*) FROM t2 HAVING count(*)>1}
134 } {1 {a GROUP BY clause is required before HAVING}}
136 uses_op_count {SELECT count(*) FROM (SELECT 1)}
139 execsql { CREATE VIEW v1 AS SELECT 1 AS a }
140 uses_op_count {SELECT count(*) FROM v1}
143 uses_op_count {SELECT count(*), max(a) FROM t2}
146 uses_op_count {SELECT count(*) FROM t1, t2}
150 register_echo_module [sqlite3_connection_pointer db]
152 execsql { CREATE VIRTUAL TABLE techo USING echo(t1); }
153 uses_op_count {SELECT count(*) FROM techo}
159 CREATE TABLE t3(a, b);
160 SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==0;
165 SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==1;
171 CREATE TABLE t4(a, b);
172 INSERT INTO t4 VALUES('a', 'b');
173 CREATE INDEX t4i1 ON t4(b, a);
174 SELECT count(*) FROM t4;
179 CREATE INDEX t4i2 ON t4(b);
180 SELECT count(*) FROM t4;
186 CREATE INDEX t4i1 ON t4(b, a);
187 SELECT count(*) FROM t4;