blob: 3461e49c1de76f89630811d25238473febfb662e [file] [log] [blame]
drhabcc1942013-11-12 14:55:40 +00001# 2009-02-24
danielk19774d9c1dd2009-02-24 10:48:27 +00002#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing "SELECT count(*)" statements.
13#
danielk19774d9c1dd2009-02-24 10:48:27 +000014
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
17
danielk19777a895a82009-02-24 18:33:15 +000018# Test plan:
19#
drh818e39a2009-04-02 20:27:28 +000020# count-0.*: Make sure count(*) works on an empty database. (Ticket #3774)
21#
danielk19777a895a82009-02-24 18:33:15 +000022# 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.
danielk19777a895a82009-02-24 18:33:15 +000026#
27#
28
drh818e39a2009-04-02 20:27:28 +000029do_test count-0.1 {
30 db eval {
31 SELECT count(*) FROM sqlite_master;
32 }
33} {0}
34
danielk19774d9c1dd2009-02-24 10:48:27 +000035set iTest 0
36foreach zIndex [list {
37 /* no-op */
38} {
39 CREATE INDEX i1 ON t1(a);
40}] {
41 incr iTest
42 do_test count-1.$iTest.1 {
43 execsql {
44 DROP TABLE IF EXISTS t1;
45 CREATE TABLE t1(a, b);
46 }
47 execsql $zIndex
danielk19777a895a82009-02-24 18:33:15 +000048 execsql { SELECT count(*) FROM t1 }
49 } {0}
50
51 do_test count-1.$iTest.2 {
danielk19774d9c1dd2009-02-24 10:48:27 +000052 execsql {
53 INSERT INTO t1 VALUES(1, 2);
54 INSERT INTO t1 VALUES(3, 4);
55 SELECT count(*) FROM t1;
56 }
57 } {2}
danielk19777a895a82009-02-24 18:33:15 +000058
59 do_test count-1.$iTest.3 {
danielk19774d9c1dd2009-02-24 10:48:27 +000060 execsql {
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;
69 }
70 } {256}
71
danielk19777a895a82009-02-24 18:33:15 +000072 do_test count-1.$iTest.4 {
danielk19774d9c1dd2009-02-24 10:48:27 +000073 execsql {
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;
79 }
80 } {4096}
81
danielk19777a895a82009-02-24 18:33:15 +000082 do_test count-1.$iTest.5 {
danielk19774d9c1dd2009-02-24 10:48:27 +000083 execsql {
84 BEGIN;
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
89 COMMIT;
90 SELECT count(*) FROM t1;
91 }
92 } {65536}
93}
94
danielk19777a895a82009-02-24 18:33:15 +000095proc uses_op_count {sql} {
96 if {[lsearch [execsql "EXPLAIN $sql"] Count]>=0} {
97 return 1;
98 }
99 return 0
100}
101
102do_test count-2.1 {
103 execsql {
104 CREATE TABLE t2(a, b);
105 }
106 uses_op_count {SELECT count(*) FROM t2}
107} {1}
108do_test count-2.2 {
109 catchsql {SELECT count(DISTINCT *) FROM t2}
110} {1 {near "*": syntax error}}
111do_test count-2.3 {
112 uses_op_count {SELECT count(DISTINCT a) FROM t2}
113} {0}
114do_test count-2.4 {
115 uses_op_count {SELECT count(a) FROM t2}
116} {0}
117do_test count-2.5 {
118 uses_op_count {SELECT count() FROM t2}
119} {1}
120do_test count-2.6 {
121 catchsql {SELECT count(DISTINCT) FROM t2}
122} {1 {DISTINCT aggregates must have exactly one argument}}
123do_test count-2.7 {
124 uses_op_count {SELECT count(*)+1 FROM t2}
125} {0}
126do_test count-2.8 {
127 uses_op_count {SELECT count(*) FROM t2 WHERE a IS NOT NULL}
128} {0}
129do_test count-2.9 {
130 catchsql {SELECT count(*) FROM t2 HAVING count(*)>1}
131} {1 {a GROUP BY clause is required before HAVING}}
132do_test count-2.10 {
133 uses_op_count {SELECT count(*) FROM (SELECT 1)}
134} {0}
135do_test count-2.11 {
136 execsql { CREATE VIEW v1 AS SELECT 1 AS a }
137 uses_op_count {SELECT count(*) FROM v1}
138} {0}
139do_test count-2.12 {
140 uses_op_count {SELECT count(*), max(a) FROM t2}
141} {0}
142do_test count-2.13 {
143 uses_op_count {SELECT count(*) FROM t1, t2}
144} {0}
145
danielk197702f33722009-02-25 08:56:47 +0000146ifcapable vtab {
147 register_echo_module [sqlite3_connection_pointer db]
148 do_test count-2.14 {
149 execsql { CREATE VIRTUAL TABLE techo USING echo(t1); }
150 uses_op_count {SELECT count(*) FROM techo}
151 } {0}
152}
153
danielk19777a895a82009-02-24 18:33:15 +0000154do_test count-3.1 {
155 execsql {
156 CREATE TABLE t3(a, b);
157 SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==0;
158 }
159} {0}
160do_test count-3.2 {
161 execsql {
162 SELECT a FROM (SELECT count(*) AS a FROM t3) WHERE a==1;
163 }
164} {}
165
danielk197702f33722009-02-25 08:56:47 +0000166do_test count-4.1 {
167 execsql {
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;
172 }
173} {1}
174do_test count-4.2 {
175 execsql {
176 CREATE INDEX t4i2 ON t4(b);
177 SELECT count(*) FROM t4;
178 }
179} {1}
180do_test count-4.3 {
181 execsql {
182 DROP INDEX t4i1;
183 CREATE INDEX t4i1 ON t4(b, a);
184 SELECT count(*) FROM t4;
185 }
186} {1}
187
drhabcc1942013-11-12 14:55:40 +0000188do_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;
192} {1}
danielk197702f33722009-02-25 08:56:47 +0000193
danielk19774d9c1dd2009-02-24 10:48:27 +0000194finish_test