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}
130 catchsql {SELECT count(*) FROM t2 HAVING count(*)>1}
131 } {1 {a GROUP BY clause is required before HAVING}}
133 uses_op_count {SELECT count(*) FROM (SELECT 1)}
136 execsql { CREATE VIEW v1 AS SELECT 1 AS a }
137 uses_op_count {SELECT count(*) FROM v1}
140 uses_op_count {SELECT count(*), max(a) FROM t2}
143 uses_op_count {SELECT count(*) FROM t1, t2}
147 register_echo_module [sqlite3_connection_pointer db]
149 execsql { CREATE VIRTUAL TABLE techo USING echo(t1); }
150 uses_op_count {SELECT count(*) FROM techo}
156 CREATE TABLE t3(a, b);
157 SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==0;
162 SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==1;
168 CREATE TABLE t4(a, b);
169 INSERT INTO t4 VALUES('a', 'b');
170 CREATE INDEX t4i1 ON t4(b, a);
171 SELECT count(*) FROM t4;
176 CREATE INDEX t4i2 ON t4(b);
177 SELECT count(*) FROM t4;
183 CREATE INDEX t4i1 ON t4(b, a);
184 SELECT count(*) FROM t4;
188 do_execsql_test count-5.1 {
189 CREATE TABLE t5(a TEXT PRIMARY KEY, b VARCHAR(50)) WITHOUT ROWID;
190 INSERT INTO t5 VALUES('bison','jazz');
191 SELECT count(*) FROM t5;
194 do_catchsql_test count-6.1 {
196 SELECT count(DISTINCT) FROM t6 GROUP BY x;
197 } {1 {DISTINCT aggregates must have exactly one argument}}