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 script is testing the FTS4 module.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set testprefix fts4growth
20 # If SQLITE_ENABLE_FTS3 is defined, omit this file.
26 source $testdir/genesis.tcl
28 do_execsql_test 1.1 { CREATE VIRTUAL TABLE x1 USING fts3; }
32 {"See here, young man," said Mulga Bill, "from Walgett to the sea,}
33 {From Conroy's Gap to Castlereagh, there's none can ride like me.}
34 {I'm good all round at everything as everybody knows,}
35 {Although I'm not the one to talk -- I hate a man that blows.}
37 execsql { INSERT INTO x1 VALUES($L) }
39 execsql { SELECT end_block, length(root) FROM x1_segdir }
40 } {{0 114} 114 {0 118} 118 {0 95} 95 {0 115} 115}
43 INSERT INTO x1(x1) VALUES('optimize');
44 SELECT level, end_block, length(root) FROM x1_segdir;
49 {But riding is my special gift, my chiefest, sole delight;}
50 {Just ask a wild duck can it swim, a wildcat can it fight.}
51 {There's nothing clothed in hair or hide, or built of flesh or steel,}
52 {There's nothing walks or jumps, or runs, on axle, hoof, or wheel,}
53 {But what I'll sit, while hide will hold and girths and straps are tight:}
54 {I'll ride this here two-wheeled concern right straight away at sight."}
56 execsql { INSERT INTO x1 VALUES($L) }
59 INSERT INTO x1(x1) VALUES('merge=4,4');
60 SELECT level, end_block, length(root) FROM x1_segdir;
65 SELECT length(block) FROM x1_segments;
70 {'Twas Mulga Bill, from Eaglehawk, that sought his own abode,}
71 {That perched above Dead Man's Creek, beside the mountain road.}
72 {He turned the cycle down the hill and mounted for the fray,}
73 {But 'ere he'd gone a dozen yards it bolted clean away.}
75 {It left the track, and through the trees, just like a silver steak,}
76 {It whistled down the awful slope towards the Dead Man's Creek.}
77 {It shaved a stump by half an inch, it dodged a big white-box:}
78 {The very wallaroos in fright went scrambling up the rocks,}
80 {The wombats hiding in their caves dug deeper underground,}
81 {As Mulga Bill, as white as chalk, sat tight to every bound.}
82 {It struck a stone and gave a spring that cleared a fallen tree,}
83 {It raced beside a precipice as close as close could be;}
85 {And then as Mulga Bill let out one last despairing shriek}
86 {It made a leap of twenty feet into the Dead Man's Creek.}
87 {It shaved a stump by half an inch, it dodged a big white-box:}
88 {The very wallaroos in fright went scrambling up the rocks,}
89 {The wombats hiding in their caves dug deeper underground,}
91 execsql { INSERT INTO x1 VALUES($L) }
94 SELECT level, end_block, length(root) FROM x1_segdir;
96 } {1 {224 921} 2 1 {226 1230} 7 0 {0 98} 98}
99 SELECT sum(length(block)) FROM x1_segments WHERE blockid IN (224,225,226)
102 #-------------------------------------------------------------------------
104 do_execsql_test 2.1 {
105 CREATE TABLE t1(docid, words);
106 CREATE VIRTUAL TABLE x2 USING fts4;
110 foreach id [db eval {SELECT docid FROM t1}] {
112 INSERT INTO x2(docid, content) SELECT $id, words FROM t1 WHERE docid=$id
115 foreach id [db eval {SELECT docid FROM t1}] {
117 INSERT INTO x2(docid, content) SELECT NULL, words FROM t1 WHERE docid=$id
119 if {[db one {SELECT count(*) FROM x2_segdir WHERE level<2}]==2} break
123 do_execsql_test 2.3 {
124 SELECT count(*) FROM x2_segdir WHERE level=2;
125 SELECT count(*) FROM x2_segdir WHERE level=3;
128 do_execsql_test 2.4 {
129 INSERT INTO x2(x2) VALUES('merge=4,4');
130 SELECT count(*) FROM x2_segdir WHERE level=2;
131 SELECT count(*) FROM x2_segdir WHERE level=3;
134 do_execsql_test 2.5 {
135 SELECT end_block FROM x2_segdir WHERE level=3;
136 INSERT INTO x2(x2) VALUES('merge=4,4');
137 SELECT end_block FROM x2_segdir WHERE level=3;
138 INSERT INTO x2(x2) VALUES('merge=4,4');
139 SELECT end_block FROM x2_segdir WHERE level=3;
140 } {{5588 -3950} {5588 -11766} {5588 -15541}}
142 do_execsql_test 2.6 {
143 SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE
144 blockid BETWEEN start_block AND leaves_end_block
148 do_execsql_test 2.7 {
149 INSERT INTO x2(x2) VALUES('merge=1000,4');
150 SELECT end_block FROM x2_segdir WHERE level=3;
153 do_execsql_test 2.8 {
154 SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE
155 blockid BETWEEN start_block AND leaves_end_block
159 #--------------------------------------------------------------------------
160 # Test that delete markers are removed from FTS segments when possible.
161 # It is only possible to remove delete markers when the output of the
162 # merge operation will become the oldest segment in the index.
164 # 3.1 - when the oldest segment is created by an 'optimize'.
165 # 3.2 - when the oldest segment is created by an incremental merge.
166 # 3.3 - by a crisis merge.
169 proc insert_doc {args} {
172 {In your eagerness to engage the Trojans,}
173 {don’t any of you charge ahead of others,}
174 {trusting in your strength and horsemanship.}
175 {And don’t lag behind. That will hurt our charge.}
176 {Any man whose chariot confronts an enemy’s}
177 {should thrust with his spear at him from there.}
178 {That’s the most effective tactic, the way}
179 {men wiped out city strongholds long ago —}
180 {their chests full of that style and spirit.}
182 execsql { REPLACE INTO x3(docid, content) VALUES($iDoc, $L) }
186 proc delete_doc {args} {
188 execsql { DELETE FROM x3 WHERE docid = $iDoc }
192 proc second {x} { lindex $x 1 }
193 db func second second
195 do_execsql_test 3.0 { CREATE VIRTUAL TABLE x3 USING fts4 }
198 db transaction { insert_doc 1 2 3 4 5 6 }
199 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
202 delete_doc 1 2 3 4 5 6
203 execsql { SELECT count(*) FROM x3_segdir }
207 insert_doc 1 2 3 4 5 6 7 8 9
210 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
211 } {0 0 591 0 1 65 0 2 72 0 3 76}
213 execsql { INSERT INTO x3(x3) VALUES('optimize') }
214 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
218 execsql { DELETE FROM x3 }
219 insert_doc 8 7 6 5 4 3 2 1
221 execsql { SELECT count(*) FROM x3_segdir }
224 execsql { INSERT INTO x3(x3) VALUES('merge=500,10') }
225 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
228 # This assumes the crisis merge happens when there are already 16
229 # segments and one more is added.
232 execsql { DELETE FROM x3 }
233 insert_doc 1 2 3 4 5 6 7 8 9 10 11
234 delete_doc 11 10 9 8 7
235 execsql { SELECT count(*) FROM x3_segdir }
240 execsql { SELECT level, idx, second(end_block) FROM x3_segdir WHERE level=1 }
243 #--------------------------------------------------------------------------
244 # Check a theory on a bug in fts4 - that segments with idx==0 were not
245 # being incrementally merged correctly. Theory turned out to be false.
247 do_execsql_test 4.1 {
248 DROP TABLE IF EXISTS x4;
249 DROP TABLE IF EXISTS t1;
250 CREATE TABLE t1(docid, words);
251 CREATE VIRTUAL TABLE x4 USING fts4(words);
255 execsql { INSERT INTO x4 SELECT words FROM t1 }
256 execsql { INSERT INTO x4 SELECT words FROM t1 }
259 do_execsql_test 4.3 {
260 SELECT level, idx, second(end_block) FROM x4_segdir
261 } {0 0 117483 0 1 118006}
263 do_execsql_test 4.4 {
264 INSERT INTO x4(x4) VALUES('merge=10,2');
265 SELECT count(*) FROM x4_segdir;
268 do_execsql_test 4.5 {
269 INSERT INTO x4(x4) VALUES('merge=10,2');
270 SELECT count(*) FROM x4_segdir;
273 do_execsql_test 4.6 {
274 INSERT INTO x4(x4) VALUES('merge=1000,2');
275 SELECT count(*) FROM x4_segdir;
280 #--------------------------------------------------------------------------
281 # Check that segments are not promoted if the "end_block" field does not
284 do_execsql_test 5.1 {
285 DROP TABLE IF EXISTS x2;
286 DROP TABLE IF EXISTS t1;
287 CREATE TABLE t1(docid, words);
288 CREATE VIRTUAL TABLE x2 USING fts4;
292 proc first {L} {lindex $L 0}
296 foreach r [db eval { SELECT rowid FROM t1 }] {
298 INSERT INTO x2(docid, content) SELECT docid, words FROM t1 WHERE rowid=$r
301 foreach d [db eval { SELECT docid FROM t1 LIMIT -1 OFFSET 20 }] {
302 execsql { DELETE FROM x2 WHERE docid = $d }
306 INSERT INTO x2(x2) VALUES('optimize');
307 SELECT level, idx, end_block FROM x2_segdir
311 do_execsql_test 5.3 {
312 UPDATE x2_segdir SET end_block = CAST( first(end_block) AS INTEGER );
313 SELECT end_block, typeof(end_block) FROM x2_segdir;
316 do_execsql_test 5.4 {
317 INSERT INTO x2 SELECT words FROM t1 LIMIT 50;
318 SELECT level, idx, end_block FROM x2_segdir
319 } {2 0 752 0 0 {758 5174}}
321 do_execsql_test 5.5 {
322 UPDATE x2_segdir SET end_block = end_block || ' 1926' WHERE level=2;
323 INSERT INTO x2 SELECT words FROM t1 LIMIT 40;
324 SELECT level, idx, end_block FROM x2_segdir
325 } {0 0 {752 1926} 0 1 {758 5174} 0 2 {763 4170}}
328 foreach id [db eval {SELECT docid FROM t1 LIMIT 2}] {
330 DELETE FROM x2 WHERE docid=$id;
331 INSERT INTO x2(docid, content) SELECT $id, words FROM t1 WHERE docid=$id;
336 #--------------------------------------------------------------------------
337 # Check that segments created by auto-merge are not promoted until they
341 do_execsql_test 6.1 {
342 CREATE VIRTUAL TABLE x5 USING fts4;
343 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 0;
344 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 25;
345 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 50;
346 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 75;
347 SELECT count(*) FROM x5_segdir
350 do_execsql_test 6.2 {
351 INSERT INTO x5(x5) VALUES('merge=2,4');
352 SELECT level, idx, end_block FROM x5_segdir;
353 } {0 0 {10 9216} 0 1 {21 9330} 0 2 {31 8850} 0 3 {40 8689} 1 0 {1320 -3117}}
355 do_execsql_test 6.3 {
356 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 100;
357 SELECT level, idx, end_block FROM x5_segdir;
359 0 0 {10 9216} 0 1 {21 9330} 0 2 {31 8850}
360 0 3 {40 8689} 1 0 {1320 -3117} 0 4 {1329 8297}
363 do_execsql_test 6.4 {
364 INSERT INTO x5(x5) VALUES('merge=200,4');
365 SELECT level, idx, end_block FROM x5_segdir;
366 } {0 0 {1329 8297} 1 0 {1320 28009}}
368 do_execsql_test 6.5 {
369 INSERT INTO x5 SELECT words FROM t1;
370 SELECT level, idx, end_block FROM x5_segdir;
372 0 1 {1329 8297} 0 0 {1320 28009} 0 2 {1449 118006}
375 #--------------------------------------------------------------------------
376 # Ensure that if part of an incremental merge is performed by an old
377 # version that does not support storing segment sizes in the end_block
378 # field, no size is stored in the final segment (as it would be incorrect).
380 do_execsql_test 7.1 {
381 CREATE VIRTUAL TABLE x6 USING fts4;
382 INSERT INTO x6 SELECT words FROM t1;
383 INSERT INTO x6 SELECT words FROM t1;
384 INSERT INTO x6 SELECT words FROM t1;
385 INSERT INTO x6 SELECT words FROM t1;
386 INSERT INTO x6 SELECT words FROM t1;
387 INSERT INTO x6 SELECT words FROM t1;
388 SELECT level, idx, end_block FROM x6_segdir;
390 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
391 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
394 do_execsql_test 7.2 {
395 INSERT INTO x6(x6) VALUES('merge=25,4');
396 SELECT level, idx, end_block FROM x6_segdir;
398 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
399 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
403 do_execsql_test 7.3 {
404 UPDATE x6_segdir SET end_block = first(end_block) WHERE level=1;
405 SELECT level, idx, end_block FROM x6_segdir;
407 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
408 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
412 do_execsql_test 7.4 {
413 INSERT INTO x6(x6) VALUES('merge=25,4');
414 SELECT level, idx, end_block FROM x6_segdir;
416 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
417 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
421 do_execsql_test 7.5 {
422 INSERT INTO x6(x6) VALUES('merge=2500,4');
423 SELECT level, idx, start_block, leaves_end_block, end_block FROM x6_segdir;
428 do_execsql_test 7.6 {
429 INSERT INTO x6(x6) VALUES('merge=2500,2');
430 SELECT level, idx, start_block, leaves_end_block, end_block FROM x6_segdir;
435 do_execsql_test 7.7 {
436 SELECT sum(length(block)) FROM x6_segments