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;
62 } {0 {0 110} 110 0 {0 132} 132 0 {0 129} 129 1 {128 658} 2}
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.}
74 {It left the track, and through the trees, just like a silver steak,}
75 {It whistled down the awful slope towards the Dead Man's Creek.}
76 {It shaved a stump by half an inch, it dodged a big white-box:}
77 {The very wallaroos in fright went scrambling up the rocks,}
78 {The wombats hiding in their caves dug deeper underground,}
79 {As Mulga Bill, as white as chalk, sat tight to every bound.}
80 {It struck a stone and gave a spring that cleared a fallen tree,}
81 {It raced beside a precipice as close as close could be;}
82 {And then as Mulga Bill let out one last despairing shriek}
83 {It made a leap of twenty feet into the Dead Man's Creek.}
85 execsql { INSERT INTO x1 VALUES($L) }
88 SELECT level, end_block, length(root) FROM x1_segdir;
90 } {1 {128 658} 2 1 {130 1377} 6 0 {0 117} 117}
93 SELECT sum(length(block)) FROM x1_segments WHERE blockid IN (129, 130);
96 #-------------------------------------------------------------------------
99 CREATE TABLE t1(docid, words);
100 CREATE VIRTUAL TABLE x2 USING fts4;
104 foreach id [db eval {SELECT docid FROM t1}] {
106 INSERT INTO x2(docid, content) SELECT $id, words FROM t1 WHERE docid=$id
109 foreach id [db eval {SELECT docid FROM t1}] {
111 INSERT INTO x2(docid, content) SELECT NULL, words FROM t1 WHERE docid=$id
113 if {[db one {SELECT count(*) FROM x2_segdir WHERE level<2}]==2} break
117 do_execsql_test 2.3 {
118 SELECT count(*) FROM x2_segdir WHERE level=2;
119 SELECT count(*) FROM x2_segdir WHERE level=3;
122 do_execsql_test 2.4 {
123 INSERT INTO x2(x2) VALUES('merge=4,4');
124 SELECT count(*) FROM x2_segdir WHERE level=2;
125 SELECT count(*) FROM x2_segdir WHERE level=3;
128 do_execsql_test 2.5 {
129 SELECT end_block FROM x2_segdir WHERE level=3;
130 INSERT INTO x2(x2) VALUES('merge=4,4');
131 SELECT end_block FROM x2_segdir WHERE level=3;
132 INSERT INTO x2(x2) VALUES('merge=4,4');
133 SELECT end_block FROM x2_segdir WHERE level=3;
134 } {{3828 -3430} {3828 -10191} {3828 -14109}}
136 do_execsql_test 2.6 {
137 SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE
138 blockid BETWEEN start_block AND leaves_end_block
142 do_execsql_test 2.7 {
143 INSERT INTO x2(x2) VALUES('merge=1000,4');
144 SELECT end_block FROM x2_segdir WHERE level=3;
147 do_execsql_test 2.8 {
148 SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE
149 blockid BETWEEN start_block AND leaves_end_block
153 #--------------------------------------------------------------------------
154 # Test that delete markers are removed from FTS segments when possible.
155 # It is only possible to remove delete markers when the output of the
156 # merge operation will become the oldest segment in the index.
158 # 3.1 - when the oldest segment is created by an 'optimize'.
159 # 3.2 - when the oldest segment is created by an incremental merge.
160 # 3.3 - by a crisis merge.
163 proc insert_doc {args} {
166 {In your eagerness to engage the Trojans,}
167 {don’t any of you charge ahead of others,}
168 {trusting in your strength and horsemanship.}
169 {And don’t lag behind. That will hurt our charge.}
170 {Any man whose chariot confronts an enemy’s}
171 {should thrust with his spear at him from there.}
172 {That’s the most effective tactic, the way}
173 {men wiped out city strongholds long ago —}
174 {their chests full of that style and spirit.}
176 execsql { REPLACE INTO x3(docid, content) VALUES($iDoc, $L) }
180 proc delete_doc {args} {
182 execsql { DELETE FROM x3 WHERE docid = $iDoc }
186 proc second {x} { lindex $x 1 }
187 db func second second
189 do_execsql_test 3.0 { CREATE VIRTUAL TABLE x3 USING fts4 }
192 db transaction { insert_doc 1 2 3 4 5 6 }
193 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
196 delete_doc 1 2 3 4 5 6
197 execsql { SELECT count(*) FROM x3_segdir }
201 insert_doc 1 2 3 4 5 6 7 8 9
204 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
205 } {0 0 591 0 1 65 0 2 72 0 3 76}
207 execsql { INSERT INTO x3(x3) VALUES('optimize') }
208 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
212 execsql { DELETE FROM x3 }
213 insert_doc 8 7 6 5 4 3 2 1
215 execsql { SELECT count(*) FROM x3_segdir }
218 execsql { INSERT INTO x3(x3) VALUES('merge=500,10') }
219 execsql { SELECT level, idx, second(end_block) FROM x3_segdir }
222 # This assumes the crisis merge happens when there are already 16
223 # segments and one more is added.
226 execsql { DELETE FROM x3 }
227 insert_doc 1 2 3 4 5 6 7 8 9 10 11
228 delete_doc 11 10 9 8 7
229 execsql { SELECT count(*) FROM x3_segdir }
234 execsql { SELECT level, idx, second(end_block) FROM x3_segdir WHERE level=1 }
237 #--------------------------------------------------------------------------
238 # Check a theory on a bug in fts4 - that segments with idx==0 were not
239 # being incrementally merged correctly. Theory turned out to be false.
241 do_execsql_test 4.1 {
242 DROP TABLE IF EXISTS x4;
243 DROP TABLE IF EXISTS t1;
244 CREATE TABLE t1(docid, words);
245 CREATE VIRTUAL TABLE x4 USING fts4(words);
249 execsql { INSERT INTO x4 SELECT words FROM t1 }
250 execsql { INSERT INTO x4 SELECT words FROM t1 }
253 do_execsql_test 4.3 {
254 SELECT level, idx, second(end_block) FROM x4_segdir
255 } {0 0 117483 0 1 118006}
257 do_execsql_test 4.4 {
258 INSERT INTO x4(x4) VALUES('merge=10,2');
259 SELECT count(*) FROM x4_segdir;
262 do_execsql_test 4.5 {
263 INSERT INTO x4(x4) VALUES('merge=10,2');
264 SELECT count(*) FROM x4_segdir;
267 do_execsql_test 4.6 {
268 INSERT INTO x4(x4) VALUES('merge=1000,2');
269 SELECT count(*) FROM x4_segdir;
274 #--------------------------------------------------------------------------
275 # Check that segments are not promoted if the "end_block" field does not
278 do_execsql_test 5.1 {
279 DROP TABLE IF EXISTS x2;
280 DROP TABLE IF EXISTS t1;
281 CREATE TABLE t1(docid, words);
282 CREATE VIRTUAL TABLE x2 USING fts4;
286 proc first {L} {lindex $L 0}
290 foreach r [db eval { SELECT rowid FROM t1 }] {
292 INSERT INTO x2(docid, content) SELECT docid, words FROM t1 WHERE rowid=$r
295 foreach d [db eval { SELECT docid FROM t1 LIMIT -1 OFFSET 20 }] {
296 execsql { DELETE FROM x2 WHERE docid = $d }
300 INSERT INTO x2(x2) VALUES('optimize');
301 SELECT level, idx, end_block FROM x2_segdir
305 do_execsql_test 5.3 {
306 UPDATE x2_segdir SET end_block = CAST( first(end_block) AS INTEGER );
307 SELECT end_block, typeof(end_block) FROM x2_segdir;
310 do_execsql_test 5.4 {
311 INSERT INTO x2 SELECT words FROM t1 LIMIT 50;
312 SELECT level, idx, end_block FROM x2_segdir
313 } {2 0 752 0 0 {758 5174}}
315 do_execsql_test 5.5 {
316 UPDATE x2_segdir SET end_block = end_block || ' 1926' WHERE level=2;
317 INSERT INTO x2 SELECT words FROM t1 LIMIT 40;
318 SELECT level, idx, end_block FROM x2_segdir
319 } {0 0 {752 1926} 0 1 {758 5174} 0 2 {763 4170}}
322 foreach id [db eval {SELECT docid FROM t1 LIMIT 2}] {
324 DELETE FROM x2 WHERE docid=$id;
325 INSERT INTO x2(docid, content) SELECT $id, words FROM t1 WHERE docid=$id;
330 #--------------------------------------------------------------------------
331 # Check that segments created by auto-merge are not promoted until they
335 do_execsql_test 6.1 {
336 CREATE VIRTUAL TABLE x5 USING fts4;
337 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 0;
338 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 25;
339 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 50;
340 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 75;
341 SELECT count(*) FROM x5_segdir
344 do_execsql_test 6.2 {
345 INSERT INTO x5(x5) VALUES('merge=2,4');
346 SELECT level, idx, end_block FROM x5_segdir;
347 } {0 0 {10 9216} 0 1 {21 9330} 0 2 {31 8850} 0 3 {40 8689} 1 0 {1320 -3117}}
349 do_execsql_test 6.3 {
350 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 100;
351 SELECT level, idx, end_block FROM x5_segdir;
353 0 0 {10 9216} 0 1 {21 9330} 0 2 {31 8850}
354 0 3 {40 8689} 1 0 {1320 -3117} 0 4 {1329 8297}
357 do_execsql_test 6.4 {
358 INSERT INTO x5(x5) VALUES('merge=200,4');
359 SELECT level, idx, end_block FROM x5_segdir;
360 } {0 0 {1329 8297} 1 0 {1320 28009}}
362 do_execsql_test 6.5 {
363 INSERT INTO x5 SELECT words FROM t1;
364 SELECT level, idx, end_block FROM x5_segdir;
366 0 1 {1329 8297} 0 0 {1320 28009} 0 2 {1449 118006}
369 #--------------------------------------------------------------------------
370 # Ensure that if part of an incremental merge is performed by an old
371 # version that does not support storing segment sizes in the end_block
372 # field, no size is stored in the final segment (as it would be incorrect).
374 do_execsql_test 7.1 {
375 CREATE VIRTUAL TABLE x6 USING fts4;
376 INSERT INTO x6 SELECT words FROM t1;
377 INSERT INTO x6 SELECT words FROM t1;
378 INSERT INTO x6 SELECT words FROM t1;
379 INSERT INTO x6 SELECT words FROM t1;
380 INSERT INTO x6 SELECT words FROM t1;
381 INSERT INTO x6 SELECT words FROM t1;
382 SELECT level, idx, end_block FROM x6_segdir;
384 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
385 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
388 do_execsql_test 7.2 {
389 INSERT INTO x6(x6) VALUES('merge=25,4');
390 SELECT level, idx, end_block FROM x6_segdir;
392 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
393 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
397 do_execsql_test 7.3 {
398 UPDATE x6_segdir SET end_block = first(end_block) WHERE level=1;
399 SELECT level, idx, end_block FROM x6_segdir;
401 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
402 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
406 do_execsql_test 7.4 {
407 INSERT INTO x6(x6) VALUES('merge=25,4');
408 SELECT level, idx, end_block FROM x6_segdir;
410 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006}
411 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006}
415 do_execsql_test 7.5 {
416 INSERT INTO x6(x6) VALUES('merge=2500,4');
417 SELECT level, idx, end_block FROM x6_segdir;
419 0 0 {598 118006} 0 1 {718 118006} 1 0 16014
422 do_execsql_test 7.6 {
423 INSERT INTO x6(x6) VALUES('merge=2500,2');
424 SELECT level, idx, start_block, leaves_end_block, end_block FROM x6_segdir;
426 2 0 23695 24147 {41262 633507}
429 do_execsql_test 7.7 {
430 SELECT sum(length(block)) FROM x6_segments
431 WHERE blockid BETWEEN 23695 AND 24147