Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: third_party/sqlite/test/analyze2.test

Issue 3108030: Move bundled copy of sqlite one level deeper to better separate it... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/test/analyze.test ('k') | third_party/sqlite/test/async.test » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # 2009 August 06
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 #
12 # This file implements regression tests for SQLite library. This file
13 # implements tests for the extra functionality provided by the ANALYZE
14 # command when the library is compiled with SQLITE_ENABLE_STAT2 defined.
15 #
16
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19
20 ifcapable !stat2 {
21 finish_test
22 return
23 }
24
25 #--------------------------------------------------------------------
26 # Test organization:
27 #
28 # analyze2-1.*: Tests to verify that ANALYZE creates and populates the
29 # sqlite_stat2 table as expected.
30 #
31 # analyze2-2.*: Test that when a table has two indexes on it and either
32 # index may be used for the scan, the index suggested by
33 # the contents of sqlite_stat2 table is prefered.
34 #
35 # analyze2-3.*: Similar to the previous block of tests, but using tables
36 # that contain a mixture of NULL, numeric, text and blob
37 # values.
38 #
39 # analyze2-4.*: Check that when an indexed column uses a collation other
40 # than BINARY, the collation is taken into account when
41 # using the contents of sqlite_stat2 to estimate the cost
42 # of a range scan.
43 #
44 # analyze2-5.*: Check that collation sequences are used as described above
45 # even when the only available version of the collation
46 # function require UTF-16 encoded arguments.
47 #
48 # analyze2-6.*: Check that the library behaves correctly when one of the
49 # sqlite_stat2 or sqlite_stat1 tables are missing.
50 #
51 # analyze2-7.*: Check that in a shared-schema situation, nothing goes
52 # wrong if sqlite_stat2 data is read by one connection,
53 # and freed by another.
54 #
55
56 proc eqp {sql {db db}} {
57 uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
58 }
59
60 do_test analyze2-1.1 {
61 execsql { CREATE TABLE t1(x PRIMARY KEY) }
62 for {set i 0} {$i < 1000} {incr i} {
63 execsql { INSERT INTO t1 VALUES($i) }
64 }
65 execsql {
66 ANALYZE;
67 SELECT * FROM sqlite_stat2;
68 }
69 } [list t1 sqlite_autoindex_t1_1 0 50 \
70 t1 sqlite_autoindex_t1_1 1 149 \
71 t1 sqlite_autoindex_t1_1 2 249 \
72 t1 sqlite_autoindex_t1_1 3 349 \
73 t1 sqlite_autoindex_t1_1 4 449 \
74 t1 sqlite_autoindex_t1_1 5 549 \
75 t1 sqlite_autoindex_t1_1 6 649 \
76 t1 sqlite_autoindex_t1_1 7 749 \
77 t1 sqlite_autoindex_t1_1 8 849 \
78 t1 sqlite_autoindex_t1_1 9 949 \
79 ]
80
81 do_test analyze2-1.2 {
82 execsql {
83 DELETE FROM t1 WHERe x>9;
84 ANALYZE;
85 SELECT tbl, idx, group_concat(sample, ' ') FROM sqlite_stat2;
86 }
87 } {t1 sqlite_autoindex_t1_1 {0 1 2 3 4 5 6 7 8 9}}
88 do_test analyze2-1.3 {
89 execsql {
90 DELETE FROM t1 WHERE x>8;
91 ANALYZE;
92 SELECT * FROM sqlite_stat2;
93 }
94 } {}
95 do_test analyze2-1.4 {
96 execsql {
97 DELETE FROM t1;
98 ANALYZE;
99 SELECT * FROM sqlite_stat2;
100 }
101 } {}
102
103 do_test analyze2-2.1 {
104 execsql {
105 BEGIN;
106 DROP TABLE t1;
107 CREATE TABLE t1(x, y);
108 CREATE INDEX t1_x ON t1(x);
109 CREATE INDEX t1_y ON t1(y);
110 }
111 for {set i 0} {$i < 1000} {incr i} {
112 execsql { INSERT INTO t1 VALUES($i, $i) }
113 }
114 execsql COMMIT
115 execsql ANALYZE
116 } {}
117 do_test analyze2-2.2 {
118 eqp "SELECT * FROM t1 WHERE x>500 AND y>700"
119 } {0 0 {TABLE t1 WITH INDEX t1_y}}
120 do_test analyze2-2.3 {
121 eqp "SELECT * FROM t1 WHERE x>700 AND y>500"
122 } {0 0 {TABLE t1 WITH INDEX t1_x}}
123 do_test analyze2-2.3 {
124 eqp "SELECT * FROM t1 WHERE y>700 AND x>500"
125 } {0 0 {TABLE t1 WITH INDEX t1_y}}
126 do_test analyze2-2.4 {
127 eqp "SELECT * FROM t1 WHERE y>500 AND x>700"
128 } {0 0 {TABLE t1 WITH INDEX t1_x}}
129 do_test analyze2-2.5 {
130 eqp "SELECT * FROM t1 WHERE x BETWEEN 100 AND 200 AND y BETWEEN 400 AND 700"
131 } {0 0 {TABLE t1 WITH INDEX t1_x}}
132 do_test analyze2-2.6 {
133 eqp "SELECT * FROM t1 WHERE x BETWEEN 100 AND 500 AND y BETWEEN 400 AND 700"
134 } {0 0 {TABLE t1 WITH INDEX t1_y}}
135 do_test analyze2-2.7 {
136 eqp "SELECT * FROM t1 WHERE x BETWEEN -400 AND -300 AND y BETWEEN 100 AND 300"
137 } {0 0 {TABLE t1 WITH INDEX t1_x}}
138 do_test analyze2-2.8 {
139 eqp "SELECT * FROM t1 WHERE x BETWEEN 100 AND 300 AND y BETWEEN -400 AND -300"
140 } {0 0 {TABLE t1 WITH INDEX t1_y}}
141 do_test analyze2-2.9 {
142 eqp "SELECT * FROM t1 WHERE x BETWEEN 500 AND 100 AND y BETWEEN 100 AND 300"
143 } {0 0 {TABLE t1 WITH INDEX t1_x}}
144 do_test analyze2-2.10 {
145 eqp "SELECT * FROM t1 WHERE x BETWEEN 100 AND 300 AND y BETWEEN 500 AND 100"
146 } {0 0 {TABLE t1 WITH INDEX t1_y}}
147
148 do_test analyze2-3.1 {
149 set alphabet [list a b c d e f g h i j]
150 execsql BEGIN
151 for {set i 0} {$i < 1000} {incr i} {
152 set str [lindex $alphabet [expr ($i/100)%10]]
153 append str [lindex $alphabet [expr ($i/ 10)%10]]
154 append str [lindex $alphabet [expr ($i/ 1)%10]]
155 execsql { INSERT INTO t1 VALUES($str, $str) }
156 }
157 execsql COMMIT
158 execsql ANALYZE
159 execsql {
160 SELECT tbl,idx,group_concat(sample,' ')
161 FROM sqlite_stat2
162 WHERE idx = 't1_x'
163 GROUP BY tbl,idx
164 }
165 } {t1 t1_x {100 299 499 699 899 ajj cjj ejj gjj ijj}}
166 do_test analyze2-3.2 {
167 execsql {
168 SELECT tbl,idx,group_concat(sample,' ')
169 FROM sqlite_stat2
170 WHERE idx = 't1_y'
171 GROUP BY tbl,idx
172 }
173 } {t1 t1_y {100 299 499 699 899 ajj cjj ejj gjj ijj}}
174
175 do_test analyze2-3.3 {
176 eqp "SELECT * FROM t1 WHERE x BETWEEN 100 AND 500 AND y BETWEEN 'a' AND 'b'"
177 } {0 0 {TABLE t1 WITH INDEX t1_y}}
178 do_test analyze2-3.4 {
179 eqp "SELECT * FROM t1 WHERE x BETWEEN 100 AND 400 AND y BETWEEN 'a' AND 'h'"
180 } {0 0 {TABLE t1 WITH INDEX t1_x}}
181 do_test analyze2-3.5 {
182 eqp "SELECT * FROM t1 WHERE x<'a' AND y>'h'"
183 } {0 0 {TABLE t1 WITH INDEX t1_y}}
184 do_test analyze2-3.6 {
185 eqp "SELECT * FROM t1 WHERE x<444 AND y>'h'"
186 } {0 0 {TABLE t1 WITH INDEX t1_y}}
187 do_test analyze2-3.7 {
188 eqp "SELECT * FROM t1 WHERE x<221 AND y>'g'"
189 } {0 0 {TABLE t1 WITH INDEX t1_x}}
190
191 do_test analyze2-4.1 {
192 execsql { CREATE TABLE t3(a COLLATE nocase, b) }
193 execsql { CREATE INDEX t3a ON t3(a) }
194 execsql { CREATE INDEX t3b ON t3(b) }
195 set alphabet [list A b C d E f G h I j]
196 execsql BEGIN
197 for {set i 0} {$i < 1000} {incr i} {
198 set str [lindex $alphabet [expr ($i/100)%10]]
199 append str [lindex $alphabet [expr ($i/ 10)%10]]
200 append str [lindex $alphabet [expr ($i/ 1)%10]]
201 execsql { INSERT INTO t3 VALUES($str, $str) }
202 }
203 execsql COMMIT
204 execsql ANALYZE
205 } {}
206 do_test analyze2-4.2 {
207 execsql {
208 SELECT tbl,idx,group_concat(sample,' ')
209 FROM sqlite_stat2
210 WHERE idx = 't3a'
211 GROUP BY tbl,idx
212 }
213 } {t3 t3a {AfA bEj CEj dEj EEj fEj GEj hEj IEj jEj}}
214 do_test analyze2-4.3 {
215 execsql {
216 SELECT tbl,idx,group_concat(sample,' ')
217 FROM sqlite_stat2
218 WHERE idx = 't3b'
219 GROUP BY tbl,idx
220 }
221 } {t3 t3b {AbA CIj EIj GIj IIj bIj dIj fIj hIj jIj}}
222
223 do_test analyze2-4.4 {
224 eqp "SELECT * FROM t3 WHERE a > 'A' AND a < 'C' AND b > 'A' AND b < 'C'"
225 } {0 0 {TABLE t3 WITH INDEX t3b}}
226 do_test analyze2-4.5 {
227 eqp "SELECT * FROM t3 WHERE a > 'A' AND a < 'c' AND b > 'A' AND b < 'c'"
228 } {0 0 {TABLE t3 WITH INDEX t3a}}
229
230 proc test_collate {enc lhs rhs} {
231 # puts $enc
232 return [string compare $lhs $rhs]
233 }
234 do_test analyze2-5.1 {
235 add_test_collate db 0 0 1
236 execsql { CREATE TABLE t4(x COLLATE test_collate) }
237 execsql { CREATE INDEX t4x ON t4(x) }
238 set alphabet [list a b c d e f g h i j]
239 execsql BEGIN
240 for {set i 0} {$i < 1000} {incr i} {
241 set str [lindex $alphabet [expr ($i/100)%10]]
242 append str [lindex $alphabet [expr ($i/ 10)%10]]
243 append str [lindex $alphabet [expr ($i/ 1)%10]]
244 execsql { INSERT INTO t4 VALUES($str) }
245 }
246 execsql COMMIT
247 execsql ANALYZE
248 } {}
249 do_test analyze2-5.2 {
250 execsql {
251 SELECT tbl,idx,group_concat(sample,' ')
252 FROM sqlite_stat2
253 WHERE tbl = 't4'
254 GROUP BY tbl,idx
255 }
256 } {t4 t4x {afa bej cej dej eej fej gej hej iej jej}}
257 do_test analyze2-5.3 {
258 eqp "SELECT * FROM t4 WHERE x>'ccc'"
259 } {0 0 {TABLE t4 WITH INDEX t4x}}
260 do_test analyze2-5.4 {
261 eqp "SELECT * FROM t4 AS t41, t4 AS t42 WHERE t41.x>'ccc' AND t42.x>'ggg'"
262 } {0 1 {TABLE t4 AS t42 WITH INDEX t4x} 1 0 {TABLE t4 AS t41 WITH INDEX t4x}}
263 do_test analyze2-5.5 {
264 eqp "SELECT * FROM t4 AS t41, t4 AS t42 WHERE t41.x>'ddd' AND t42.x>'ccc'"
265 } {0 0 {TABLE t4 AS t41 WITH INDEX t4x} 1 1 {TABLE t4 AS t42 WITH INDEX t4x}}
266
267 #--------------------------------------------------------------------
268 # These tests, analyze2-6.*, verify that the library behaves correctly
269 # when one of the sqlite_stat1 and sqlite_stat2 tables is missing.
270 #
271 # If the sqlite_stat1 table is not present, then the sqlite_stat2
272 # table is not read. However, if it is the sqlite_stat2 table that
273 # is missing, the data in the sqlite_stat1 table is still used.
274 #
275 # Tests analyze2-6.1.* test the libary when the sqlite_stat2 table
276 # is missing. Tests analyze2-6.2.* test the library when sqlite_stat1
277 # is not present.
278 #
279 do_test analyze2-6.0 {
280 execsql {
281 DROP TABLE t4;
282 CREATE TABLE t5(a, b); CREATE INDEX t5i ON t5(a, b);
283 CREATE TABLE t6(a, b); CREATE INDEX t6i ON t6(a, b);
284 }
285 for {set ii 0} {$ii < 20} {incr ii} {
286 execsql {
287 INSERT INTO t5 VALUES($ii, $ii);
288 INSERT INTO t6 VALUES($ii/10, $ii/10);
289 }
290 }
291 execsql {
292 CREATE TABLE master AS
293 SELECT * FROM sqlite_master WHERE name LIKE 'sqlite_stat%'
294 }
295 } {}
296
297 do_test analyze2-6.1.1 {
298 eqp {SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
299 t5.a = 1 AND
300 t6.a = 1 AND t6.b = 1
301 }
302 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
303 do_test analyze2-6.1.2 {
304 db cache flush
305 execsql ANALYZE
306 eqp {SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
307 t5.a = 1 AND
308 t6.a = 1 AND t6.b = 1
309 }
310 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
311 do_test analyze2-6.1.3 {
312 sqlite3 db test.db
313 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
314 t5.a = 1 AND
315 t6.a = 1 AND t6.b = 1
316 }
317 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
318 do_test analyze2-6.1.4 {
319 execsql {
320 PRAGMA writable_schema = 1;
321 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat2';
322 }
323 sqlite3 db test.db
324 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
325 t5.a = 1 AND
326 t6.a = 1 AND t6.b = 1
327 }
328 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
329 do_test analyze2-6.1.5 {
330 execsql {
331 PRAGMA writable_schema = 1;
332 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat1';
333 }
334 sqlite3 db test.db
335 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
336 t5.a = 1 AND
337 t6.a = 1 AND t6.b = 1
338 }
339 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
340 do_test analyze2-6.1.6 {
341 execsql {
342 PRAGMA writable_schema = 1;
343 INSERT INTO sqlite_master SELECT * FROM master;
344 }
345 sqlite3 db test.db
346 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
347 t5.a = 1 AND
348 t6.a = 1 AND t6.b = 1
349 }
350 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
351
352 do_test analyze2-6.2.1 {
353 execsql {
354 DELETE FROM sqlite_stat1;
355 DELETE FROM sqlite_stat2;
356 }
357 sqlite3 db test.db
358 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
359 t5.a>1 AND t5.a<15 AND
360 t6.a>1
361 }
362 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
363 do_test analyze2-6.2.2 {
364 db cache flush
365 execsql ANALYZE
366 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
367 t5.a>1 AND t5.a<15 AND
368 t6.a>1
369 }
370 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
371 do_test analyze2-6.2.3 {
372 sqlite3 db test.db
373 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
374 t5.a>1 AND t5.a<15 AND
375 t6.a>1
376 }
377 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
378 do_test analyze2-6.2.4 {
379 execsql {
380 PRAGMA writable_schema = 1;
381 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat1';
382 }
383 sqlite3 db test.db
384 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
385 t5.a>1 AND t5.a<15 AND
386 t6.a>1
387 }
388 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
389 do_test analyze2-6.2.5 {
390 execsql {
391 PRAGMA writable_schema = 1;
392 DELETE FROM sqlite_master WHERE tbl_name = 'sqlite_stat2';
393 }
394 sqlite3 db test.db
395 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
396 t5.a>1 AND t5.a<15 AND
397 t6.a>1
398 }
399 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
400 do_test analyze2-6.2.6 {
401 execsql {
402 PRAGMA writable_schema = 1;
403 INSERT INTO sqlite_master SELECT * FROM master;
404 }
405 sqlite3 db test.db
406 execsql ANALYZE
407 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
408 t5.a>1 AND t5.a<15 AND
409 t6.a>1
410 }
411 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
412
413 #--------------------------------------------------------------------
414 # These tests, analyze2-7.*, test that the sqlite_stat2 functionality
415 # works in shared-cache mode. Note that these tests reuse the database
416 # created for the analyze2-6.* tests.
417 #
418 ifcapable shared_cache {
419 db close
420 set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
421
422 proc incr_schema_cookie {zDb} {
423 foreach iOffset {24 40} {
424 set cookie [hexio_get_int [hexio_read $zDb $iOffset 4]]
425 incr cookie
426 hexio_write $zDb $iOffset [hexio_render_int32 $cookie]
427 }
428 }
429
430 do_test analyze2-7.1 {
431 sqlite3 db1 test.db
432 sqlite3 db2 test.db
433 db1 cache size 0
434 db2 cache size 0
435 execsql { SELECT count(*) FROM t5 } db1
436 } {20}
437 do_test analyze2-7.2 {
438 incr_schema_cookie test.db
439 execsql { SELECT count(*) FROM t5 } db2
440 } {20}
441 do_test analyze2-7.3 {
442 incr_schema_cookie test.db
443 execsql { SELECT count(*) FROM t5 } db1
444 } {20}
445 do_test analyze2-7.4 {
446 incr_schema_cookie test.db
447 execsql { SELECT count(*) FROM t5 } db2
448 } {20}
449
450 do_test analyze2-7.5 {
451 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
452 t5.a>1 AND t5.a<15 AND
453 t6.a>1
454 } db1
455 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
456 do_test analyze2-7.6 {
457 incr_schema_cookie test.db
458 execsql { SELECT * FROM sqlite_master } db2
459 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
460 t5.a>1 AND t5.a<15 AND
461 t6.a>1
462 } db2
463 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
464 do_test analyze2-7.7 {
465 incr_schema_cookie test.db
466 execsql { SELECT * FROM sqlite_master } db1
467 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
468 t5.a>1 AND t5.a<15 AND
469 t6.a>1
470 } db1
471 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
472
473 do_test analyze2-7.8 {
474 execsql { DELETE FROM sqlite_stat2 } db2
475 execsql { SELECT * FROM sqlite_master } db1
476 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
477 t5.a>1 AND t5.a<15 AND
478 t6.a>1
479 } db1
480 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
481 do_test analyze2-7.9 {
482 execsql { SELECT * FROM sqlite_master } db2
483 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
484 t5.a>1 AND t5.a<15 AND
485 t6.a>1
486 } db2
487 } {0 1 {TABLE t6 WITH INDEX t6i} 1 0 {TABLE t5 USING PRIMARY KEY}}
488
489 do_test analyze2-7.10 {
490 incr_schema_cookie test.db
491 execsql { SELECT * FROM sqlite_master } db1
492 eqp { SELECT * FROM t5,t6 WHERE t5.rowid=t6.rowid AND
493 t5.a>1 AND t5.a<15 AND
494 t6.a>1
495 } db1
496 } {0 0 {TABLE t5 WITH INDEX t5i} 1 1 {TABLE t6 USING PRIMARY KEY}}
497
498 db1 close
499 db2 close
500 sqlite3_enable_shared_cache $::enable_shared_cache
501 }
502
503 finish_test
OLDNEW
« no previous file with comments | « third_party/sqlite/test/analyze.test ('k') | third_party/sqlite/test/async.test » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698