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.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
20 # count-0.*: Make sure count(*) works on an empty database. (Ticket #3774)
22 # count-1.*: Test that the OP_Count instruction appears to work on both
23 # tables and indexes. Test both when they contain 0 entries,
24 # when all entries are on the root page, and when the b-tree
25 # forms a structure 2 and 3 levels deep.
31 SELECT count(*) FROM sqlite_master;
36 foreach zIndex [list {
39 CREATE INDEX i1 ON t1(a);
42 do_test count-1.$iTest.1 {
44 DROP TABLE IF EXISTS t1;
45 CREATE TABLE t1(a, b);
48 execsql { SELECT count(*) FROM t1 }
51 do_test count-1.$iTest.2 {
53 INSERT INTO t1 VALUES(1, 2);
54 INSERT INTO t1 VALUES(3, 4);
55 SELECT count(*) FROM t1;
59 do_test count-1.$iTest.3 {
61 INSERT INTO t1 SELECT * FROM t1; -- 4
62 INSERT INTO t1 SELECT * FROM t1; -- 8
63 INSERT INTO t1 SELECT * FROM t1; -- 16
64 INSERT INTO t1 SELECT * FROM t1; -- 32
65 INSERT INTO t1 SELECT * FROM t1; -- 64
66 INSERT INTO t1 SELECT * FROM t1; -- 128
67 INSERT INTO t1 SELECT * FROM t1; -- 256
68 SELECT count(*) FROM t1;
72 do_test count-1.$iTest.4 {
74 INSERT INTO t1 SELECT * FROM t1; -- 512
75 INSERT INTO t1 SELECT * FROM t1; -- 1024
76 INSERT INTO t1 SELECT * FROM t1; -- 2048
77 INSERT INTO t1 SELECT * FROM t1; -- 4096
78 SELECT count(*) FROM t1;
82 do_test count-1.$iTest.5 {
85 INSERT INTO t1 SELECT * FROM t1; -- 8192
86 INSERT INTO t1 SELECT * FROM t1; -- 16384
87 INSERT INTO t1 SELECT * FROM t1; -- 32768
88 INSERT INTO t1 SELECT * FROM t1; -- 65536
90 SELECT count(*) FROM t1;
95 proc uses_op_count {sql} {
96 if {[lsearch [execsql "EXPLAIN $sql"] Count]>=0} {
104 CREATE TABLE t2(a, b);
106 uses_op_count {SELECT count(*) FROM t2}
109 catchsql {SELECT count(DISTINCT *) FROM t2}
110 } {1 {near "*": syntax error}}
112 uses_op_count {SELECT count(DISTINCT a) FROM t2}
115 uses_op_count {SELECT count(a) FROM t2}
118 uses_op_count {SELECT count() FROM t2}
121 catchsql {SELECT count(DISTINCT) FROM t2}
122 } {1 {DISTINCT aggregates must have exactly one argument}}
124 uses_op_count {SELECT count(*)+1 FROM t2}
127 uses_op_count {SELECT count(*) FROM t2 WHERE a IS NOT NULL}
129 do_execsql_test count-2.9a {
130 SELECT count(*) FROM t2 HAVING count(*)>1;
132 do_execsql_test count-2.9b {
133 SELECT count(*) FROM t2 HAVING count(*)<10;
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;
191 do_execsql_test count-5.1 {
192 CREATE TABLE t5(a TEXT PRIMARY KEY, b VARCHAR(50)) WITHOUT ROWID;
193 INSERT INTO t5 VALUES('bison','jazz');
194 SELECT count(*) FROM t5;
197 do_catchsql_test count-6.1 {
199 SELECT count(DISTINCT) FROM t6 GROUP BY x;
200 } {1 {DISTINCT aggregates must have exactly one argument}}
203 # The count() optimization should honor the NOT INDEXED clause
206 do_execsql_test count-7.1 {
207 CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c VARCHAR(1000));
208 CREATE INDEX t1b ON t1(b);
209 INSERT INTO t1(a,b,c) values(1,2,'count.test cases for NOT INDEXED');
211 UPDATE sqlite_stat1 SET stat='1000000 10' WHERE idx='t1b';
212 ANALYZE sqlite_master;
214 do_eqp_test count-7.2 {
215 SELECT count(1) FROM t1;
218 `--SCAN t1 USING COVERING INDEX t1b
220 do_eqp_test count-7.3 {
221 SELECT count(1) FROM t1 NOT INDEXED
226 do_eqp_test count-7.3 {
227 SELECT count(*) FROM t1;
230 `--SCAN t1 USING COVERING INDEX t1b
232 do_eqp_test count-7.4 {
233 SELECT count(*) FROM t1 NOT INDEXED
239 do_execsql_test count-8.0 {
240 CREATE TABLE t7(a INT,b TEXT,c BLOB,d REAL);
241 CREATE TABLE t8(a INT,b TEXT,c BLOB,d REAL);
242 CREATE INDEX t8a ON t8(a);
244 do_catchsql_test count-8.1 {
245 SELECT * FROM t8 WHERE (a, b) IN (
246 SELECT count(t8.b), count(*) FROM t7 AS ra0 ORDER BY count(*)
248 } {1 {misuse of aggregate: count()}}