xref: /sqlite-3.40.0/test/stat.test (revision 5f0d37b5)
1# 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
17set testprefix stat
18
19ifcapable !vtab||!compound {
20  finish_test
21  return
22}
23
24# 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;}
28
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 {
35  PRAGMA table_info(dbstat);
36} {/0 name TEXT .* 1 path TEXT .* 9 pgsize INTEGER/}
37
38# Attempts to drop an eponymous virtual table are a no-op.
39do_execsql_test stat-0.1 {
40  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 {
50  PRAGMA auto_vacuum = OFF;
51  CREATE VIRTUAL TABLE temp.stat USING dbstat;
52  SELECT * FROM stat;
53} {}
54
55
56if {[wal_is_capable]} {
57  do_execsql_test stat-0.1 {
58    PRAGMA journal_mode = WAL;
59    PRAGMA journal_mode = delete;
60    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
61      FROM stat;
62  } {wal delete sqlite_schema / 1 leaf 0 0 916 0}
63}
64
65do_test stat-1.0 {
66  execsql {
67    CREATE TABLE t1(a, b);
68    CREATE INDEX i1 ON t1(b);
69    INSERT INTO t1(rowid, a, b) VALUES(2, 2, 3);
70    INSERT INTO t1(rowid, a, b) VALUES(3, 4, 5);
71  }
72} {}
73do_test stat-1.1 {
74  execsql {
75    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
76      FROM stat WHERE name = 't1';
77  }
78} {t1 / 2 leaf 2 10 998 5}
79do_test stat-1.2 {
80  execsql {
81    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
82      FROM stat WHERE name = 'i1';
83  }
84} {i1 / 3 leaf 2 10 1000 5}
85do_test stat-1.3 {
86  execsql {
87    SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
88      FROM stat WHERE name = 'sqlite_schema';
89  }
90} {sqlite_schema / 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);
99  INSERT INTO t3(rowid, a, b) VALUES(2, a_string(111), a_string(222));
100  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;
110  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
111    FROM stat WHERE name != 'sqlite_schema' ORDER BY name;
112} [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]
136
137do_execsql_test stat-2.1agg {
138  SELECT * FROM dbstat WHERE aggregate=TRUE ORDER BY name;
139} [list \
140  sqlite_autoindex_t3_1 {}  5 {} 32  3898 1065 132 {}  5120 \
141  sqlite_schema         {}  1 {}  2    84  824  49 {}  1024 \
142  t3                    {} 17 {} 47 11188 5815 370 {} 17408 \
143]
144
145# With every index entry overflowing, make sure no pages are missed
146# (other than the locking page which is 64 in this test build.)
147#
148do_execsql_test stat-2.2 {
149  UPDATE t3 SET a=a||hex(randomblob(700));
150  VACUUM;
151  SELECT pageno FROM stat EXCEPT SELECT pageno-1 FROM stat;
152} {64 136}
153
154do_execsql_test stat-2.3 { DROP TABLE t3; VACUUM; } {}
155
156do_execsql_test stat-3.1 {
157  CREATE TABLE t4(x);
158  CREATE INDEX i4 ON t4(x);
159  INSERT INTO t4(rowid, x) VALUES(2, a_string(7777));
160  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
161    FROM stat WHERE name != 'sqlite_schema' ORDER BY name;
162} [list \
163  i4 / 3 leaf 1 103 905 7782                 \
164  i4 /000+000000 4 overflow 0 1020 0 0       \
165  i4 /000+000001 5 overflow 0 1020 0 0      \
166  i4 /000+000002 6 overflow 0 1020 0 0      \
167  i4 /000+000003 7 overflow 0 1020 0 0      \
168  i4 /000+000004 8 overflow 0 1020 0 0      \
169  i4 /000+000005 9 overflow 0 1020 0 0      \
170  i4 /000+000006 10 overflow 0 1020 0 0      \
171  i4 /000+000007 11 overflow 0 539 481 0     \
172  t4 / 2 leaf 1 640 367 7780                 \
173  t4 /000+000000 12 overflow 0 1020 0 0      \
174  t4 /000+000001 13 overflow 0 1020 0 0      \
175  t4 /000+000002 14 overflow 0 1020 0 0      \
176  t4 /000+000003 15 overflow 0 1020 0 0      \
177  t4 /000+000004 16 overflow 0 1020 0 0      \
178  t4 /000+000005 17 overflow 0 1020 0 0      \
179  t4 /000+000006 18 overflow 0 1020 0 0      \
180]
181
182do_execsql_test stat-3.2 {
183  SELECT *, '|' FROM dbstat WHERE aggregate=TRUE ORDER BY name;
184} [list \
185  i4            {} 9 {} 1 7782 1386 7782 {} 9216 | \
186  sqlite_schema {} 1 {} 2   74  834   40 {} 1024 | \
187  t4            {} 8 {} 1 7780  367 7780 {} 8192 | \
188]
189
190
191do_execsql_test stat-4.1 {
192  CREATE TABLE t5(x);
193  CREATE INDEX i5 ON t5(x);
194  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
195    FROM stat WHERE name = 't5' OR name = 'i5';
196} [list  \
197  i5 / 20 leaf 0 0 1016 0 \
198  t5 / 19 leaf 0 0 1016 0 \
199]
200
201db close
202forcedelete test.db
203sqlite3 db test.db
204register_dbstat_vtab db
205do_execsql_test stat-5.1 {
206  PRAGMA auto_vacuum = OFF;
207  CREATE TABLE tx(y);
208  ATTACH ':memory:' AS aux1;
209  CREATE VIRTUAL TABLE temp.stat USING dbstat(aux1);
210  CREATE TABLE aux1.t1(x);
211  INSERT INTO t1 VALUES(zeroblob(1513));
212  INSERT INTO t1 VALUES(zeroblob(1514));
213  SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
214    FROM stat WHERE name = 't1';
215} [list \
216  t1 / 2 leaf 2 993 5 1517                \
217  t1 /000+000000 3 overflow 0 1020 0 0    \
218  t1 /001+000000 4 overflow 0 1020 0 0    \
219]
220
221do_execsql_test stat-5.20 {
222  SELECT name, quote(path), pageno, quote(pagetype), ncell, payload,
223         unused, mx_payload, '|' FROM dbstat('main',1);
224} {sqlite_schema NULL 1 NULL 1 34 878 34 | tx NULL 1 NULL 0 0 1016 0 |}
225do_execsql_test stat-5.21 {
226  SELECT name, quote(path), pageno, quote(pagetype), ncell, payload,
227         unused, mx_payload, '|' FROM dbstat('aux1',1);
228} {sqlite_schema NULL 1 NULL 1 34 878 34 | t1 NULL 3 NULL 2 3033 5 1517 |}
229
230
231do_catchsql_test stat-6.1 {
232  CREATE VIRTUAL TABLE temp.s2 USING dbstat(mainx);
233} {1 {no such database: mainx}}
234
235#-------------------------------------------------------------------------
236# Test that the argument passed to the dbstat constructor is dequoted
237# before it is matched against the names of attached databases.
238#
239forcedelete test.db2
240do_execsql_test 7.1 {
241  ATTACH 'test.db2' AS '123';
242  PRAGMA "123".auto_vacuum = OFF;
243  CREATE TABLE "123".x1(a, b);
244  INSERT INTO x1 VALUES(1, 2);
245}
246
247do_execsql_test 7.1.1 {
248  SELECT * FROM dbstat('123');
249} {
250  sqlite_schema / 1 leaf 1 37 875 37 0 1024
251  x1 / 2 leaf 1 4 1008 4 1024 1024
252}
253do_execsql_test 7.1.2 {
254  SELECT * FROM dbstat(123);
255} {
256  sqlite_schema / 1 leaf 1 37 875 37 0 1024
257  x1 / 2 leaf 1 4 1008 4 1024 1024
258}
259do_execsql_test 7.1.3 {
260  CREATE VIRTUAL TABLE x2 USING dbstat('123');
261  SELECT * FROM x2;
262} {
263  sqlite_schema / 1 leaf 1 37 875 37 0 1024
264  x1 / 2 leaf 1 4 1008 4 1024 1024
265}
266do_execsql_test 7.1.4 {
267  CREATE VIRTUAL TABLE x3 USING dbstat(123);
268  SELECT * FROM x3;
269} {
270  sqlite_schema / 1 leaf 1 37 875 37 0 1024
271  x1 / 2 leaf 1 4 1008 4 1024 1024
272}
273
274do_execsql_test 7.2 {
275  DETACH 123;
276  DROP TABLE x2;
277  DROP TABLE x3;
278  ATTACH 'test.db2' AS '123corp';
279}
280do_execsql_test 7.2.1 {
281  SELECT * FROM dbstat('123corp');
282} {
283  sqlite_schema / 1 leaf 1 37 875 37 0 1024
284  x1 / 2 leaf 1 4 1008 4 1024 1024
285}
286do_catchsql_test 7.2.2 {
287  SELECT * FROM dbstat(123corp);
288} {1 {unrecognized token: "123corp"}}
289do_execsql_test 7.2.3 {
290  CREATE VIRTUAL TABLE x2 USING dbstat('123corp');
291  SELECT * FROM x2;
292} {
293  sqlite_schema / 1 leaf 1 37 875 37 0 1024
294  x1 / 2 leaf 1 4 1008 4 1024 1024
295}
296do_catchsql_test 7.2.4 {
297  CREATE VIRTUAL TABLE x3 USING dbstat(123corp);
298  SELECT * FROM x3;
299} {1 {unrecognized token: "123corp"}}
300
301
302do_execsql_test 8.1 {
303  CREATE VIRTUAL TABLE st4 USING dbstat;
304}
305do_execsql_test 8.2 {
306  SELECT * FROM st4 WHERE st4.aggregate = NULL;
307}
308do_execsql_test 8.3 {
309  SELECT aggregate=1 FROM st4 WHERE aggregate = 5
310}
311do_execsql_test 8.4 {
312  SELECT * FROM st4 WHERE name = NULL;
313} {}
314do_execsql_test 8.5 {
315  SELECT * FROM st4 WHERE schema = NULL;
316} {}
317
318finish_test
319