summaryrefslogtreecommitdiffstats
path: root/test/fts3cov.test
blob: 5d838365764ed246a046aa6d260f0d2f030b9755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# 2009 December 03
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# The tests in this file are structural coverage tests for FTS3.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# If this build does not include FTS3, skip the tests in this file.
#
ifcapable !fts3 { finish_test ; return }
source $testdir/fts3_common.tcl
source $testdir/malloc_common.tcl

set DO_MALLOC_TEST 0
set testprefix fts3cov

#--------------------------------------------------------------------------
# When it first needs to read a block from the %_segments table, the FTS3 
# module compiles an SQL statement for that purpose. The statement is 
# stored and reused each subsequent time a block is read. This test case 
# tests the effects of an OOM error occuring while compiling the statement.
#
# Similarly, when FTS3 first needs to scan through a set of segment leaves
# to find a set of documents that matches a term, it allocates a string
# containing the text of the required SQL, and compiles one or more 
# statements to traverse the leaves. This test case tests that OOM errors
# that occur while allocating this string and statement are handled correctly
# also.
#
do_test fts3cov-1.1 {
  execsql { 
    CREATE VIRTUAL TABLE t1 USING fts3(x);
    INSERT INTO t1(t1) VALUES('nodesize=24');
    BEGIN;
      INSERT INTO t1 VALUES('Is the night chilly and dark?');
      INSERT INTO t1 VALUES('The night is chilly, but not dark.');
      INSERT INTO t1 VALUES('The thin gray cloud is spread on high,');
      INSERT INTO t1 VALUES('It covers but not hides the sky.');
    COMMIT;
    SELECT count(*)>0 FROM t1_segments;
  }
} {1}

set DO_MALLOC_TEST 1
do_restart_select_test fts3cov-1.2 {
  SELECT docid FROM t1 WHERE t1 MATCH 'chilly';
} {1 2}
set DO_MALLOC_TEST 0

#--------------------------------------------------------------------------
# When querying the full-text index, if an expected internal node block is 
# missing from the %_segments table, or if a NULL value is stored in the 
# %_segments table instead of a binary blob, database corruption should be 
# reported.
#
# Even with tiny 24 byte nodes, it takes a fair bit of data to produce a
# segment b-tree that uses the %_segments table to store internal nodes. 
#
do_test fts3cov-2.1 {
  execsql {
    INSERT INTO t1(t1) VALUES('nodesize=24');
    BEGIN;
      INSERT INTO t1 VALUES('The moon is behind, and at the full;');
      INSERT INTO t1 VALUES('And yet she looks both small and dull.');
      INSERT INTO t1 VALUES('The night is chill, the cloud is gray:');
      INSERT INTO t1 VALUES('''T is a month before the month of May,');
      INSERT INTO t1 VALUES('And the Spring comes slowly up this way.');
      INSERT INTO t1 VALUES('The lovely lady, Christabel,');
      INSERT INTO t1 VALUES('Whom her father loves so well,');
      INSERT INTO t1 VALUES('What makes her in the wood so late,');
      INSERT INTO t1 VALUES('A furlong from the castle gate?');
      INSERT INTO t1 VALUES('She had dreams all yesternight');
      INSERT INTO t1 VALUES('Of her own betrothed knight;');
      INSERT INTO t1 VALUES('And she in the midnight wood will pray');
      INSERT INTO t1 VALUES('For the weal of her lover that''s far away.');
    COMMIT;
  }
  execsql {
    INSERT INTO t1(t1) VALUES('optimize');
    SELECT substr(hex(root), 1, 2) FROM t1_segdir;
  }
} {03}

# Test the "missing entry" case:
sqlite3_db_config db DEFENSIVE 0
do_test fts3cov-2.2 {
  set root [db one {SELECT root FROM t1_segdir}]
  read_fts3varint [string range $root 1 end] left_child
  execsql { DELETE FROM t1_segments WHERE blockid = $left_child }
} {}
do_error_test fts3cov-2.3 {
  SELECT * FROM t1 WHERE t1 MATCH 'c*'
} {database disk image is malformed}

# Test the "replaced with NULL" case:
do_test fts3cov-2.4 {
  execsql { INSERT INTO t1_segments VALUES($left_child, NULL) }
} {}
do_error_test fts3cov-2.5 {
  SELECT * FROM t1 WHERE t1 MATCH 'cloud'
} {database disk image is malformed}

#--------------------------------------------------------------------------
# The following tests are to test the effects of OOM errors while storing
# terms in the pending-hash table. Specifically, while creating doclist
# blobs to store in the table. More specifically, to test OOM errors while
# appending column numbers to doclists. For example, if a doclist consists
# of:
#
#   <docid> <column 0 offset-list> 0x01 <column N> <column N offset-list>
#
# The following tests check that malloc errors encountered while appending
# the "0x01 <column N>" data to the dynamically growable blob used to 
# accumulate the doclist in memory are handled correctly.
#
do_test fts3cov-3.1 {
  set cols [list]
  set vals [list]
  for {set i 0} {$i < 120} {incr i} {
    lappend cols "col$i"
    lappend vals "'word'"
  }
  execsql "CREATE VIRTUAL TABLE t2 USING fts3([join $cols ,])"
} {}
set DO_MALLOC_TEST 1 
do_write_test fts3cov-3.2 t2_content "
  INSERT INTO t2(docid, [join $cols ,]) VALUES(1, [join $vals ,])
"
do_write_test fts3cov-3.3 t2_content "
  INSERT INTO t2(docid, [join $cols ,]) VALUES(200, [join $vals ,])
"
do_write_test fts3cov-3.4 t2_content "
  INSERT INTO t2(docid, [join $cols ,]) VALUES(60000, [join $vals ,])
"

#-------------------------------------------------------------------------
# If too much data accumulates in the pending-terms hash table, it is
# flushed to the database automatically, even if the transaction has not
# finished. The following tests check the effects of encountering an OOM 
# while doing this.
#
do_test fts3cov-4.1 {
  execsql {
    CREATE VIRTUAL TABLE t3 USING fts3(x);
    INSERT INTO t3(t3) VALUES('nodesize=24');
    INSERT INTO t3(t3) VALUES('maxpending=100');
  }
} {}
set DO_MALLOC_TEST 1 
do_write_test fts3cov-4.2 t3_content {
  INSERT INTO t3(docid, x)
    SELECT 1, 'Then Christabel stretched forth her hand,' UNION ALL
    SELECT 3, 'And comforted fair Geraldine:'             UNION ALL
    SELECT 4, '''O well, bright dame, may you command'    UNION ALL
    SELECT 5, 'The service of Sir Leoline;'               UNION ALL
    SELECT 2, 'And gladly our stout chivalry'             UNION ALL
    SELECT 7, 'Will he send forth, and friends withal,'   UNION ALL
    SELECT 8, 'To guide and guard you safe and free'      UNION ALL
    SELECT 6, 'Home to your noble father''s hall.'''
}

#-------------------------------------------------------------------------
# When building the internal tree structure for each segment b-tree, FTS3
# assumes that the content of each internal node will be less than
# $nodesize bytes, where $nodesize is the advisory node size. If this turns
# out to be untrue, then an extra buffer must be malloc'd for each term.
# This test case tests these paths and the effects of said mallocs failing
# by inserting insert a document with some fairly large terms into a
# full-text table with a very small node-size. 
#
# Test this handling of large terms in three contexts:
#
#   1. When flushing the pending-terms table.
#   2. When optimizing the data structures using the INSERT syntax. 
#   2. When optimizing the data structures using the deprecated SELECT syntax. 
#
do_test fts3cov-5.1 {
  execsql {
    CREATE VIRTUAL TABLE t4 USING fts3(x);
    INSERT INTO t4(t4) VALUES('nodesize=24');
  }
} {}
set DO_MALLOC_TEST 1

# Test when flushing pending-terms table.
do_write_test fts3cov-5.2 t4_content {
  INSERT INTO t4
    SELECT 'ItisanancientMarinerAndhestoppethoneofthreeAA' UNION ALL
    SELECT 'ItisanancientMarinerAndhestoppethoneofthreeBB' UNION ALL
    SELECT 'ItisanancientMarinerAndhestoppethoneofthreeCC' UNION ALL
    SELECT 'BythylonggreybeardandglitteringeyeNowwhereforestoppstAA' UNION ALL
    SELECT 'BythylonggreybeardandglitteringeyeNowwhereforestoppstBB' UNION ALL
    SELECT 'BythylonggreybeardandglitteringeyeNowwhereforestoppstCC'
}

# Test when optimizing via INSERT.
do_test fts3cov-5.3 { execsql { INSERT INTO t4 VALUES('extra!') } } {}
do_write_test fts3cov-5.2 t4_segments { INSERT INTO t4(t4) VALUES('optimize') }

# Test when optimizing via SELECT.
do_test fts3cov-5.5 { execsql { INSERT INTO t4 VALUES('more extra!') } } {}
do_write_test fts3cov-5.6 t4_segments {
  SELECT * FROM (SELECT optimize(t4) FROM t4 LIMIT 1)
  EXCEPT SELECT 'Index optimized'
}

#-------------------------------------------------------------------------
# When merging all segments at a given level to create a single segment
# at level+1, FTS3 runs a query of the form:
#
#   SELECT count(*) FROM %_segdir WHERE level = ?
#
# The query is compiled the first time this operation is required and
# reused thereafter. This test aims to test the effects of an OOM while
# preparing and executing this query for the first time.
#
# Then, keep inserting rows into the table so that the effects of an OOM
# while re-executing the same query can also be tested.
#
do_test fts3cov-6.1 {
  execsql { CREATE VIRTUAL TABLE t5 USING fts3(x) }
  for {set i 0} {$i<16} {incr i} { execsql "INSERT INTO t5 VALUES('term$i')" }
  execsql { SELECT count(*) FROM t5_segdir }
} {16}

# First time.
db close
sqlite3 db test.db
do_write_test fts3cov-6.2 t5_content {
  INSERT INTO t5 VALUES('segment number 16!');
}

# Second time.
do_test fts3cov-6.3 {
  for {set i 1} {$i<16} {incr i} { execsql "INSERT INTO t5 VALUES('term$i')" }
  execsql { SELECT count(*) FROM t5_segdir }
} {17}
do_write_test fts3cov-6.4 t5_content {
  INSERT INTO t5 VALUES('segment number 16!');
}

#-------------------------------------------------------------------------
# Update the docid of a row. Test this in two scenarios:
#
#   1. When the row being updated is the only row in the table.
#   2. When it is not.
#
# The two cases above take different paths because in case 1 all data 
# structures can simply be emptied before inserting the new row record.
# In case 2, the data structures actually have to be updated.
#
do_test fts3cov-7.1 {
  execsql {
    CREATE VIRTUAL TABLE t7 USING fts3(a, b, c);
    INSERT INTO t7 VALUES('A', 'B', 'C');
    UPDATE t7 SET docid = 5;
    SELECT docid, * FROM t7;
  }
} {5 A B C}
do_test fts3cov-7.2 {
  execsql {
    INSERT INTO t7 VALUES('D', 'E', 'F');
    UPDATE t7 SET docid = 1 WHERE docid = 6;
    SELECT docid, * FROM t7;
  }
} {1 D E F 5 A B C}

#-------------------------------------------------------------------------
# If a set of documents are modified within a transaction, the 
# pending-terms table must be flushed each time a document with a docid
# less than or equal to the previous docid is modified. 
#
# This test checks the effects of an OOM error occuring when the 
# pending-terms table is flushed for this reason as part of a DELETE 
# statement.
#
do_malloc_test fts3cov-8 -sqlprep {
  BEGIN;
    CREATE VIRTUAL TABLE t8 USING fts3;
    INSERT INTO t8 VALUES('the output of each batch run');
    INSERT INTO t8 VALUES('(possibly a day''s work)');
    INSERT INTO t8 VALUES('was written to two separate disks');
  COMMIT;
} -sqlbody {
  BEGIN;
    DELETE FROM t8 WHERE rowid = 3;
    DELETE FROM t8 WHERE rowid = 2;
    DELETE FROM t8 WHERE rowid = 1;
  COMMIT;
}

#-------------------------------------------------------------------------
# Test some branches in the code that handles "special" inserts like:
#
#   INSERT INTO t1(t1) VALUES('optimize');
#
# Also test that an optimize (INSERT method) works on an empty table.
#
set DO_MALLOC_TEST 0
do_test fts3cov-9.1 {
  execsql { CREATE VIRTUAL TABLE xx USING fts3 }
} {}
do_error_test fts3cov-9.2 {
  INSERT INTO xx(xx) VALUES('optimise');   -- British spelling
} {SQL logic error}
do_error_test fts3cov-9.3 {
  INSERT INTO xx(xx) VALUES('short');
} {SQL logic error}
do_error_test fts3cov-9.4 {
  INSERT INTO xx(xx) VALUES('waytoolongtobecorrect');
} {SQL logic error}
do_test fts3cov-9.5 {
  execsql { INSERT INTO xx(xx) VALUES('optimize') }
} {}

#-------------------------------------------------------------------------
# Test that a table can be optimized in the middle of a transaction when
# the pending-terms table is non-empty. This case involves some extra
# branches because data must be read not only from the database, but
# also from the pending-terms table.
#
do_malloc_test fts3cov-10 -sqlprep {
  CREATE VIRTUAL TABLE t10 USING fts3;
  INSERT INTO t10 VALUES('Optimising images for the web is a tricky business');
  BEGIN;
    INSERT INTO t10 VALUES('You have to get the right balance between');
} -sqlbody {
  INSERT INTO t10(t10) VALUES('optimize');
}

#-------------------------------------------------------------------------
# Test a full-text query for a term that was once in the index, but is
# no longer.
#
do_test fts3cov-11.1 {
  execsql { 
    CREATE VIRTUAL TABLE xx USING fts3;
    INSERT INTO xx VALUES('one two three');
    INSERT INTO xx VALUES('four five six');
    DELETE FROM xx WHERE docid = 1;
  }
  execsql { SELECT * FROM xx WHERE xx MATCH 'two' }
} {}


do_malloc_test fts3cov-12 -sqlprep {
  CREATE VIRTUAL TABLE t12 USING fts3;
  INSERT INTO t12 VALUES('is one of the two togther');
  BEGIN;
    INSERT INTO t12 VALUES('one which was appropriate at the time');
} -sqlbody {
  SELECT * FROM t12 WHERE t12 MATCH 'one'
}

do_malloc_test fts3cov-13 -sqlprep {
  PRAGMA encoding = 'UTF-16';
  CREATE VIRTUAL TABLE t13 USING fts3;
  INSERT INTO t13 VALUES('two scalar functions');
  INSERT INTO t13 VALUES('scalar two functions');
  INSERT INTO t13 VALUES('functions scalar two');
} -sqlbody {
  SELECT snippet(t13, '%%', '%%', '#') FROM t13 WHERE t13 MATCH 'two';
  SELECT snippet(t13, '%%', '%%') FROM t13 WHERE t13 MATCH 'two';
  SELECT snippet(t13, '%%') FROM t13 WHERE t13 MATCH 'two';
}

do_execsql_test 14.0 {
  CREATE VIRTUAL TABLE t14 USING fts4(a, b);
  INSERT INTO t14 VALUES('one two three', 'one three four');
  INSERT INTO t14 VALUES('a b c', 'd e a');
}
do_execsql_test 14.1 {
  SELECT rowid FROM t14 WHERE t14 MATCH '"one two three"'
} {1}
do_execsql_test 14.2 {
  SELECT rowid FROM t14 WHERE t14 MATCH '"one four"'
} {}
do_execsql_test 14.3 {
  SELECT rowid FROM t14 WHERE t14 MATCH '"e a"'
} {2}
do_execsql_test 14.5 {
  SELECT rowid FROM t14 WHERE t14 MATCH '"e b"'
} {}
do_catchsql_test 14.6 {
  SELECT rowid FROM t14 WHERE rowid MATCH 'one'
} {1 {unable to use function MATCH in the requested context}}
do_catchsql_test 14.7 {
  SELECT rowid FROM t14 WHERE docid MATCH 'one'
} {1 {unable to use function MATCH in the requested context}}

do_execsql_test 15.0 {
  CREATE VIRTUAL TABLE t15 USING fts4(a, b, c);
  INSERT INTO t15 VALUES('abc def ghi', 'abc2 def2 ghi2', 'abc3 def3 ghi3');
  INSERT INTO t15 VALUES('abc2 def2 ghi2', 'abc2 def2 ghi2', 'abc def3 ghi3');
}
do_execsql_test 15.1 {
  SELECT rowid FROM t15 WHERE t15 MATCH '"abc* def2"'
} {1 2}

# Test a corruption case.
#
sqlite3_db_config db DEFENSIVE 0
do_execsql_test 16.1 {
  CREATE VIRTUAL TABLE t16 USING fts4;
  INSERT INTO t16 VALUES('theoretical work to examine the relationship');
  INSERT INTO t16 VALUES('solution of our problems on the invisible');
  DELETE FROM t16_content WHERE rowid = 2;
}
do_catchsql_test 16.2 {
  SELECT * FROM t16 WHERE t16 MATCH 'invisible'
} {1 {database disk image is malformed}}

# And another corruption test case.
#
do_execsql_test 17.1 {
  CREATE VIRTUAL TABLE t17 USING fts4;
  INSERT INTO t17(content) VALUES('one one one');
  UPDATE t17_segdir SET root = X'00036F6E65FFFFFFFFFFFFFFFFFFFFFF02030300'
} {}
do_catchsql_test 17.2 {
  SELECT * FROM t17 WHERE t17 MATCH 'one'
} {1 {database disk image is malformed}}




finish_test