blob: 66ca5e2f2b3635c31dede821070cec6c3ce867b1 [file] [log] [blame]
dan6d3eb822010-07-12 18:12:41 +00001# 2010 July 09
2#
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 the SELECT statement.
13#
14
15set testdir [file dirname $argv0]
16source $testdir/tester.tcl
danb5c557b2016-01-22 15:44:07 +000017set testprefix stat
dan6d3eb822010-07-12 18:12:41 +000018
dan2f56da32012-02-13 10:00:35 +000019ifcapable !vtab||!compound {
dan6d3eb822010-07-12 18:12:41 +000020 finish_test
21 return
22}
23
drh7da56b42016-03-14 18:34:42 +000024# This module uses hard-coded results that depend on exact measurements of
25# pages sizes at the byte level, and hence will not work if the reserved_bytes
26# value is nonzero.
27if {[nonzero_reserved_bytes]} {finish_test; return;}
dan6d3eb822010-07-12 18:12:41 +000028
29set ::asc 1
30proc a_string {n} { string range [string repeat [incr ::asc]. $n] 1 $n }
31db func a_string a_string
32
33register_dbstat_vtab db
34do_execsql_test stat-0.0 {
drh4d249e62016-06-10 22:49:01 +000035 PRAGMA table_info(dbstat);
drh6c0e41b2016-06-13 15:59:37 +000036} {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/}
drh5360b552016-08-05 15:34:42 +000037
38# Attempts to drop an eponymous virtual table are a no-op.
drh4d249e62016-06-10 22:49:01 +000039do_execsql_test stat-0.1 {
drh5360b552016-08-05 15:34:42 +000040 DROP TABLE dbstat;
41 PRAGMA table_info=dbstat;
42} {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/}
43
44db close
45forcedelete test.db
46sqlite3 db test.db
47db func a_string a_string
48register_dbstat_vtab db
49do_execsql_test stat-0.2 {
dan1fab01a2010-09-13 14:38:19 +000050 PRAGMA auto_vacuum = OFF;
dan6d3eb822010-07-12 18:12:41 +000051 CREATE VIRTUAL TABLE temp.stat USING dbstat;
52 SELECT * FROM stat;
53} {}
shaneh9091f772010-08-24 18:35:12 +000054
drh5360b552016-08-05 15:34:42 +000055
dan05accd22016-04-27 18:54:49 +000056if {[wal_is_capable]} {
shaneh9091f772010-08-24 18:35:12 +000057 do_execsql_test stat-0.1 {
58 PRAGMA journal_mode = WAL;
59 PRAGMA journal_mode = delete;
drh4c9f1292011-09-28 00:50:14 +000060 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
61 FROM stat;
shaneh9091f772010-08-24 18:35:12 +000062 } {wal delete sqlite_master / 1 leaf 0 0 916 0}
63}
dan6d3eb822010-07-12 18:12:41 +000064
65do_test stat-1.0 {
66 execsql {
67 CREATE TABLE t1(a, b);
68 CREATE INDEX i1 ON t1(b);
dan2d07f262010-07-19 12:05:33 +000069 INSERT INTO t1(rowid, a, b) VALUES(2, 2, 3);
70 INSERT INTO t1(rowid, a, b) VALUES(3, 4, 5);
dan6d3eb822010-07-12 18:12:41 +000071 }
72} {}
73do_test stat-1.1 {
74 execsql {
drh4c9f1292011-09-28 00:50:14 +000075 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
76 FROM stat WHERE name = 't1';
dan6d3eb822010-07-12 18:12:41 +000077 }
78} {t1 / 2 leaf 2 10 998 5}
79do_test stat-1.2 {
80 execsql {
drh4c9f1292011-09-28 00:50:14 +000081 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
82 FROM stat WHERE name = 'i1';
dan6d3eb822010-07-12 18:12:41 +000083 }
84} {i1 / 3 leaf 2 10 1000 5}
85do_test stat-1.3 {
86 execsql {
drh4c9f1292011-09-28 00:50:14 +000087 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
88 FROM stat WHERE name = 'sqlite_master';
dan6d3eb822010-07-12 18:12:41 +000089 }
90} {sqlite_master / 1 leaf 2 77 831 40}
91do_test stat-1.4 {
92 execsql {
93 DROP TABLE t1;
94 }
95} {}
96
97do_execsql_test stat-2.1 {
98 CREATE TABLE t3(a PRIMARY KEY, b);
dan2d07f262010-07-19 12:05:33 +000099 INSERT INTO t3(rowid, a, b) VALUES(2, a_string(111), a_string(222));
drh3f4d1d12012-09-15 18:45:54 +0000100 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
101 ORDER BY rowid;
102 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
103 ORDER BY rowid;
104 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
105 ORDER BY rowid;
106 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
107 ORDER BY rowid;
108 INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
109 ORDER BY rowid;
drh4c9f1292011-09-28 00:50:14 +0000110 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
111 FROM stat WHERE name != 'sqlite_master';
dan6d3eb822010-07-12 18:12:41 +0000112} [list \
113 sqlite_autoindex_t3_1 / 3 internal 3 368 623 125 \
114 sqlite_autoindex_t3_1 /000/ 8 leaf 8 946 46 123 \
115 sqlite_autoindex_t3_1 /001/ 9 leaf 8 988 2 131 \
116 sqlite_autoindex_t3_1 /002/ 15 leaf 7 857 137 132 \
117 sqlite_autoindex_t3_1 /003/ 20 leaf 6 739 257 129 \
118 t3 / 2 internal 15 0 907 0 \
119 t3 /000/ 4 leaf 2 678 328 340 \
120 t3 /001/ 5 leaf 2 682 324 342 \
121 t3 /002/ 6 leaf 2 682 324 342 \
122 t3 /003/ 7 leaf 2 690 316 346 \
123 t3 /004/ 10 leaf 2 682 324 342 \
124 t3 /005/ 11 leaf 2 690 316 346 \
125 t3 /006/ 12 leaf 2 698 308 350 \
126 t3 /007/ 13 leaf 2 706 300 354 \
127 t3 /008/ 14 leaf 2 682 324 342 \
128 t3 /009/ 16 leaf 2 690 316 346 \
129 t3 /00a/ 17 leaf 2 698 308 350 \
130 t3 /00b/ 18 leaf 2 706 300 354 \
131 t3 /00c/ 19 leaf 2 714 292 358 \
132 t3 /00d/ 21 leaf 2 722 284 362 \
133 t3 /00e/ 22 leaf 2 730 276 366 \
134 t3 /00f/ 23 leaf 2 738 268 370 \
135]
drha4641712013-11-02 11:34:58 +0000136
137# With every index entry overflowing, make sure no pages are missed
138# (other than the locking page which is 64 in this test build.)
139#
140do_execsql_test stat-2.2 {
141 UPDATE t3 SET a=a||hex(randomblob(700));
142 VACUUM;
143 SELECT pageno FROM stat EXCEPT SELECT pageno-1 FROM stat;
144} {64 136}
145
146do_execsql_test stat-2.3 { DROP TABLE t3; VACUUM; } {}
dan6d3eb822010-07-12 18:12:41 +0000147
148do_execsql_test stat-3.1 {
149 CREATE TABLE t4(x);
150 CREATE INDEX i4 ON t4(x);
dan2d07f262010-07-19 12:05:33 +0000151 INSERT INTO t4(rowid, x) VALUES(2, a_string(7777));
drh4c9f1292011-09-28 00:50:14 +0000152 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
153 FROM stat WHERE name != 'sqlite_master';
dan6d3eb822010-07-12 18:12:41 +0000154} [list \
155 i4 / 3 leaf 1 103 905 7782 \
drha4641712013-11-02 11:34:58 +0000156 i4 /000+000000 4 overflow 0 1020 0 0 \
157 i4 /000+000001 5 overflow 0 1020 0 0 \
158 i4 /000+000002 6 overflow 0 1020 0 0 \
159 i4 /000+000003 7 overflow 0 1020 0 0 \
160 i4 /000+000004 8 overflow 0 1020 0 0 \
161 i4 /000+000005 9 overflow 0 1020 0 0 \
162 i4 /000+000006 10 overflow 0 1020 0 0 \
163 i4 /000+000007 11 overflow 0 539 481 0 \
dan6d3eb822010-07-12 18:12:41 +0000164 t4 / 2 leaf 1 640 367 7780 \
drha4641712013-11-02 11:34:58 +0000165 t4 /000+000000 12 overflow 0 1020 0 0 \
166 t4 /000+000001 13 overflow 0 1020 0 0 \
167 t4 /000+000002 14 overflow 0 1020 0 0 \
168 t4 /000+000003 15 overflow 0 1020 0 0 \
169 t4 /000+000004 16 overflow 0 1020 0 0 \
170 t4 /000+000005 17 overflow 0 1020 0 0 \
171 t4 /000+000006 18 overflow 0 1020 0 0 \
dan6d3eb822010-07-12 18:12:41 +0000172]
173
174do_execsql_test stat-4.1 {
175 CREATE TABLE t5(x);
176 CREATE INDEX i5 ON t5(x);
drh4c9f1292011-09-28 00:50:14 +0000177 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
178 FROM stat WHERE name = 't5' OR name = 'i5';
dan6d3eb822010-07-12 18:12:41 +0000179} [list \
drha4641712013-11-02 11:34:58 +0000180 i5 / 20 leaf 0 0 1016 0 \
181 t5 / 19 leaf 0 0 1016 0 \
dan6d3eb822010-07-12 18:12:41 +0000182]
183
184db close
mistachkinfda06be2011-08-02 00:57:34 +0000185forcedelete test.db
dan6d3eb822010-07-12 18:12:41 +0000186sqlite3 db test.db
187register_dbstat_vtab db
dan6d3eb822010-07-12 18:12:41 +0000188do_execsql_test stat-5.1 {
dan1fab01a2010-09-13 14:38:19 +0000189 PRAGMA auto_vacuum = OFF;
drh857df262015-05-07 14:41:56 +0000190 CREATE TABLE tx(y);
191 ATTACH ':memory:' AS aux1;
192 CREATE VIRTUAL TABLE temp.stat USING dbstat(aux1);
193 CREATE TABLE aux1.t1(x);
dan6d3eb822010-07-12 18:12:41 +0000194 INSERT INTO t1 VALUES(zeroblob(1513));
195 INSERT INTO t1 VALUES(zeroblob(1514));
drh4c9f1292011-09-28 00:50:14 +0000196 SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
197 FROM stat WHERE name = 't1';
dan6d3eb822010-07-12 18:12:41 +0000198} [list \
199 t1 / 2 leaf 2 993 5 1517 \
200 t1 /000+000000 3 overflow 0 1020 0 0 \
201 t1 /001+000000 4 overflow 0 1020 0 0 \
202]
203
drh857df262015-05-07 14:41:56 +0000204do_catchsql_test stat-6.1 {
205 CREATE VIRTUAL TABLE temp.s2 USING dbstat(mainx);
206} {1 {no such database: mainx}}
207
danb5c557b2016-01-22 15:44:07 +0000208#-------------------------------------------------------------------------
209# Test that the argument passed to the dbstat constructor is dequoted
210# before it is matched against the names of attached databases.
211#
212forcedelete test.db2
213do_execsql_test 7.1 {
214 ATTACH 'test.db2' AS '123';
dan482721a2016-01-30 21:09:02 +0000215 PRAGMA "123".auto_vacuum = OFF;
danb5c557b2016-01-22 15:44:07 +0000216 CREATE TABLE "123".x1(a, b);
217 INSERT INTO x1 VALUES(1, 2);
218}
219
220do_execsql_test 7.1.1 {
221 SELECT * FROM dbstat('123');
222} {
223 sqlite_master / 1 leaf 1 37 875 37 0 1024
224 x1 / 2 leaf 1 4 1008 4 1024 1024
225}
226do_execsql_test 7.1.2 {
227 SELECT * FROM dbstat(123);
228} {
229 sqlite_master / 1 leaf 1 37 875 37 0 1024
230 x1 / 2 leaf 1 4 1008 4 1024 1024
231}
232do_execsql_test 7.1.3 {
233 CREATE VIRTUAL TABLE x2 USING dbstat('123');
234 SELECT * FROM x2;
235} {
236 sqlite_master / 1 leaf 1 37 875 37 0 1024
237 x1 / 2 leaf 1 4 1008 4 1024 1024
238}
239do_execsql_test 7.1.4 {
240 CREATE VIRTUAL TABLE x3 USING dbstat(123);
241 SELECT * FROM x3;
242} {
243 sqlite_master / 1 leaf 1 37 875 37 0 1024
244 x1 / 2 leaf 1 4 1008 4 1024 1024
245}
246
247do_execsql_test 7.2 {
248 DETACH 123;
249 DROP TABLE x2;
250 DROP TABLE x3;
251 ATTACH 'test.db2' AS '123corp';
252}
253do_execsql_test 7.2.1 {
254 SELECT * FROM dbstat('123corp');
255} {
256 sqlite_master / 1 leaf 1 37 875 37 0 1024
257 x1 / 2 leaf 1 4 1008 4 1024 1024
258}
259do_catchsql_test 7.2.2 {
260 SELECT * FROM dbstat(123corp);
261} {1 {unrecognized token: "123corp"}}
262do_execsql_test 7.2.3 {
263 CREATE VIRTUAL TABLE x2 USING dbstat('123corp');
264 SELECT * FROM x2;
265} {
266 sqlite_master / 1 leaf 1 37 875 37 0 1024
267 x1 / 2 leaf 1 4 1008 4 1024 1024
268}
269do_catchsql_test 7.2.4 {
270 CREATE VIRTUAL TABLE x3 USING dbstat(123corp);
271 SELECT * FROM x3;
272} {1 {unrecognized token: "123corp"}}
273
dan6d3eb822010-07-12 18:12:41 +0000274finish_test