OLD | NEW |
| (Empty) |
1 # 2015 August 05 | |
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 | |
13 source [file join [file dirname [info script]] fts5_common.tcl] | |
14 set testprefix fts5matchinfo | |
15 | |
16 # If SQLITE_ENABLE_FTS5 is not defined, omit this file. | |
17 ifcapable !fts5 { finish_test ; return } | |
18 | |
19 proc mit {blob} { | |
20 set scan(littleEndian) i* | |
21 set scan(bigEndian) I* | |
22 binary scan $blob $scan($::tcl_platform(byteOrder)) r | |
23 return $r | |
24 } | |
25 db func mit mit | |
26 | |
27 sqlite3_fts5_register_matchinfo db | |
28 | |
29 do_execsql_test 1.0 { | |
30 CREATE VIRTUAL TABLE t1 USING fts5(content); | |
31 } | |
32 | |
33 do_execsql_test 1.1 { | |
34 INSERT INTO t1(content) VALUES('I wandered lonely as a cloud'); | |
35 INSERT INTO t1(content) VALUES('That floats on high o''er vales and hills,'); | |
36 INSERT INTO t1(content) VALUES('When all at once I saw a crowd,'); | |
37 INSERT INTO t1(content) VALUES('A host, of golden daffodils,'); | |
38 SELECT mit(matchinfo(t1)) FROM t1 WHERE t1 MATCH 'I'; | |
39 } {{1 1 1 2 2} {1 1 1 2 2}} | |
40 | |
41 # Now create an FTS4 table that does not specify matchinfo=fts3. | |
42 # | |
43 do_execsql_test 1.2 { | |
44 CREATE VIRTUAL TABLE t2 USING fts5(content); | |
45 INSERT INTO t2 SELECT * FROM t1; | |
46 SELECT mit(matchinfo(t2)) FROM t2 WHERE t2 MATCH 'I'; | |
47 } {{1 1 1 2 2} {1 1 1 2 2}} | |
48 | |
49 | |
50 #-------------------------------------------------------------------------- | |
51 # Proc [do_matchinfo_test] is used to test the FTSX matchinfo() function. | |
52 # | |
53 # The first argument - $tn - is a test identifier. This may be either a | |
54 # full identifier (i.e. "fts3matchinfo-1.1") or, if global var $testprefix | |
55 # is set, just the numeric component (i.e. "1.1"). | |
56 # | |
57 # The second argument is the name of an FTSX table. The third is the | |
58 # full text of a WHERE/MATCH expression to query the table for | |
59 # (i.e. "t1 MATCH 'abc'"). The final argument - $results - should be a | |
60 # key-value list (serialized array) with matchinfo() format specifiers | |
61 # as keys, and the results of executing the statement: | |
62 # | |
63 # SELECT matchinfo($tbl, '$key') FROM $tbl WHERE $expr | |
64 # | |
65 # For example: | |
66 # | |
67 # CREATE VIRTUAL TABLE t1 USING fts4; | |
68 # INSERT INTO t1 VALUES('abc'); | |
69 # INSERT INTO t1 VALUES('def'); | |
70 # INSERT INTO t1 VALUES('abc abc'); | |
71 # | |
72 # do_matchinfo_test 1.1 t1 "t1 MATCH 'abc'" { | |
73 # n {3 3} | |
74 # p {1 1} | |
75 # c {1 1} | |
76 # x {{1 3 2} {2 3 2}} | |
77 # } | |
78 # | |
79 # If the $results list contains keys mapped to "-" instead of a matchinfo() | |
80 # result, then this command computes the expected results based on other | |
81 # mappings to test the matchinfo() function. For example, the command above | |
82 # could be changed to: | |
83 # | |
84 # do_matchinfo_test 1.1 t1 "t1 MATCH 'abc'" { | |
85 # n {3 3} p {1 1} c {1 1} x {{1 3 2} {2 3 2}} | |
86 # pcx - | |
87 # } | |
88 # | |
89 # And this command would compute the expected results for matchinfo(t1, 'pcx') | |
90 # based on the results of matchinfo(t1, 'p'), matchinfo(t1, 'c') and | |
91 # matchinfo(t1, 'x') in order to test 'pcx'. | |
92 # | |
93 proc do_matchinfo_test {tn tbl expr results} { | |
94 | |
95 foreach {fmt res} $results { | |
96 if {$res == "-"} continue | |
97 set resarray($fmt) $res | |
98 } | |
99 | |
100 set nRow 0 | |
101 foreach {fmt res} [array get resarray] { | |
102 if {[llength $res]>$nRow} { set nRow [llength $res] } | |
103 } | |
104 | |
105 # Construct expected results for any formats for which the caller | |
106 # supplied result is "-". | |
107 # | |
108 foreach {fmt res} $results { | |
109 if {$res == "-"} { | |
110 set res [list] | |
111 for {set iRow 0} {$iRow<$nRow} {incr iRow} { | |
112 set rowres [list] | |
113 foreach c [split $fmt ""] { | |
114 set rowres [concat $rowres [lindex $resarray($c) $iRow]] | |
115 } | |
116 lappend res $rowres | |
117 } | |
118 set resarray($fmt) $res | |
119 } | |
120 } | |
121 | |
122 # Test each matchinfo() request individually. | |
123 # | |
124 foreach {fmt res} [array get resarray] { | |
125 set sql "SELECT mit(matchinfo($tbl, '$fmt')) FROM $tbl WHERE $expr" | |
126 do_execsql_test $tn.$fmt $sql [normalize2 $res] | |
127 } | |
128 | |
129 # Test them all executed together (multiple invocations of matchinfo()). | |
130 # | |
131 set exprlist [list] | |
132 foreach {format res} [array get resarray] { | |
133 lappend exprlist "mit(matchinfo($tbl, '$format'))" | |
134 } | |
135 set allres [list] | |
136 for {set iRow 0} {$iRow<$nRow} {incr iRow} { | |
137 foreach {format res} [array get resarray] { | |
138 lappend allres [lindex $res $iRow] | |
139 } | |
140 } | |
141 set sql "SELECT [join $exprlist ,] FROM $tbl WHERE $expr" | |
142 do_execsql_test $tn.multi $sql [normalize2 $allres] | |
143 } | |
144 proc normalize2 {list_of_lists} { | |
145 set res [list] | |
146 foreach elem $list_of_lists { | |
147 lappend res [list {*}$elem] | |
148 } | |
149 return $res | |
150 } | |
151 | |
152 | |
153 do_execsql_test 4.1.0 { | |
154 CREATE VIRTUAL TABLE t4 USING fts5(x, y); | |
155 INSERT INTO t4 VALUES('a b c d e', 'f g h i j'); | |
156 INSERT INTO t4 VALUES('f g h i j', 'a b c d e'); | |
157 } | |
158 | |
159 do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} { | |
160 s {{3 0} {0 3}} | |
161 } | |
162 | |
163 do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} { | |
164 p {3 3} | |
165 x { | |
166 {1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1} | |
167 {0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1} | |
168 } | |
169 } | |
170 | |
171 do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} { | |
172 p {3 3} | |
173 c {2 2} | |
174 x { | |
175 {1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1} | |
176 {0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1} | |
177 } | |
178 n {2 2} | |
179 l {{5 5} {5 5}} | |
180 a {{5 5} {5 5}} | |
181 | |
182 s {{3 0} {0 3}} | |
183 | |
184 xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc - | |
185 xpxsscplax - | |
186 } | |
187 | |
188 do_matchinfo_test 4.1.2 t4 {t4 MATCH '"g h i"'} { | |
189 p {1 1} | |
190 c {2 2} | |
191 x { | |
192 {0 1 1 1 1 1} | |
193 {1 1 1 0 1 1} | |
194 } | |
195 n {2 2} | |
196 l {{5 5} {5 5}} | |
197 a {{5 5} {5 5}} | |
198 | |
199 s {{0 1} {1 0}} | |
200 | |
201 xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc - | |
202 sxsxs - | |
203 } | |
204 | |
205 do_matchinfo_test 4.1.3 t4 {t4 MATCH 'a b'} { s {{2 0} {0 2}} } | |
206 do_matchinfo_test 4.1.4 t4 {t4 MATCH '"a b" c'} { s {{2 0} {0 2}} } | |
207 do_matchinfo_test 4.1.5 t4 {t4 MATCH 'a "b c"'} { s {{2 0} {0 2}} } | |
208 do_matchinfo_test 4.1.6 t4 {t4 MATCH 'd d'} { s {{1 0} {0 1}} } | |
209 do_matchinfo_test 4.1.7 t4 {t4 MATCH 'f OR abcd'} { | |
210 x { | |
211 {0 1 1 1 1 1 0 0 0 0 0 0} | |
212 {1 1 1 0 1 1 0 0 0 0 0 0} | |
213 } | |
214 } | |
215 do_matchinfo_test 4.1.8 t4 {t4 MATCH 'f NOT abcd'} { | |
216 x { | |
217 {0 1 1 1 1 1 0 0 0 0 0 0} | |
218 {1 1 1 0 1 1 0 0 0 0 0 0} | |
219 } | |
220 } | |
221 | |
222 do_execsql_test 4.2.0 { | |
223 CREATE VIRTUAL TABLE t5 USING fts5(content); | |
224 INSERT INTO t5 VALUES('a a a a a'); | |
225 INSERT INTO t5 VALUES('a b a b a'); | |
226 INSERT INTO t5 VALUES('c b c b c'); | |
227 INSERT INTO t5 VALUES('x x x x x'); | |
228 } | |
229 do_matchinfo_test 4.2.1 t5 {t5 MATCH 'a a'} { | |
230 x {{5 8 2 5 8 2} {3 8 2 3 8 2}} | |
231 s {2 1} | |
232 } | |
233 do_matchinfo_test 4.2.2 t5 {t5 MATCH 'a b'} { s {2} } | |
234 do_matchinfo_test 4.2.3 t5 {t5 MATCH 'a b a'} { s {3} } | |
235 do_matchinfo_test 4.2.4 t5 {t5 MATCH 'a a a'} { s {3 1} } | |
236 do_matchinfo_test 4.2.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} } | |
237 do_matchinfo_test 4.2.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1} } | |
238 | |
239 do_execsql_test 4.3.0 "INSERT INTO t5 VALUES('x y [string repeat {b } 50000]')"; | |
240 | |
241 # It used to be that the second 'a' token would be deferred. That doesn't | |
242 # work any longer. | |
243 if 0 { | |
244 do_matchinfo_test 4.3.1 t5 {t5 MATCH 'a a'} { | |
245 x {{5 8 2 5 5 5} {3 8 2 3 5 5}} | |
246 s {2 1} | |
247 } | |
248 } | |
249 | |
250 do_matchinfo_test 4.3.2 t5 {t5 MATCH 'a b'} { s {2} } | |
251 do_matchinfo_test 4.3.3 t5 {t5 MATCH 'a b a'} { s {3} } | |
252 do_matchinfo_test 4.3.4 t5 {t5 MATCH 'a a a'} { s {3 1} } | |
253 do_matchinfo_test 4.3.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} } | |
254 do_matchinfo_test 4.3.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1 1} } | |
255 | |
256 do_execsql_test 4.4.0.1 { INSERT INTO t5(t5) VALUES('optimize') } | |
257 | |
258 do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} } | |
259 do_matchinfo_test 4.4.1 t5 {t5 MATCH 'a a'} { s {2 1} } | |
260 do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} } | |
261 do_matchinfo_test 4.4.3 t5 {t5 MATCH 'a b a'} { s {3} } | |
262 do_matchinfo_test 4.4.4 t5 {t5 MATCH 'a a a'} { s {3 1} } | |
263 do_matchinfo_test 4.4.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} } | |
264 | |
265 do_execsql_test 4.5.0 { | |
266 CREATE VIRTUAL TABLE t6 USING fts5(a, b, c); | |
267 INSERT INTO t6 VALUES('a', 'b', 'c'); | |
268 } | |
269 do_matchinfo_test 4.5.1 t6 {t6 MATCH 'a b c'} { s {{1 1 1}} } | |
270 | |
271 | |
272 #------------------------------------------------------------------------- | |
273 # Test the outcome of matchinfo() when used within a query that does not | |
274 # use the full-text index (i.e. lookup by rowid or full-table scan). | |
275 # | |
276 do_execsql_test 7.1 { | |
277 CREATE VIRTUAL TABLE t10 USING fts5(content); | |
278 INSERT INTO t10 VALUES('first record'); | |
279 INSERT INTO t10 VALUES('second record'); | |
280 } | |
281 do_execsql_test 7.2 { | |
282 SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10; | |
283 } {blob 8 blob 8} | |
284 do_execsql_test 7.3 { | |
285 SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10 WHERE rowid=1; | |
286 } {blob 8} | |
287 do_execsql_test 7.4 { | |
288 SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) | |
289 FROM t10 WHERE t10 MATCH 'record' | |
290 } {blob 20 blob 20} | |
291 | |
292 #------------------------------------------------------------------------- | |
293 # Test a special case - matchinfo('nxa') with many zero length documents. | |
294 # Special because "x" internally uses a statement used by both "n" and "a". | |
295 # This was causing a problem at one point in the obscure case where the | |
296 # total number of bytes of data stored in an fts3 table was greater than | |
297 # the number of rows. i.e. when the following query returns true: | |
298 # | |
299 # SELECT sum(length(content)) < count(*) FROM fts4table; | |
300 # | |
301 do_execsql_test 8.1 { | |
302 CREATE VIRTUAL TABLE t11 USING fts5(content); | |
303 INSERT INTO t11(t11, rank) VALUES('pgsz', 32); | |
304 INSERT INTO t11 VALUES('quitealongstringoftext'); | |
305 INSERT INTO t11 VALUES('anotherquitealongstringoftext'); | |
306 INSERT INTO t11 VALUES('athirdlongstringoftext'); | |
307 INSERT INTO t11 VALUES('andonemoreforgoodluck'); | |
308 } | |
309 do_test 8.2 { | |
310 for {set i 0} {$i < 200} {incr i} { | |
311 execsql { INSERT INTO t11 VALUES('') } | |
312 } | |
313 execsql { INSERT INTO t11(t11) VALUES('optimize') } | |
314 } {} | |
315 do_execsql_test 8.3 { | |
316 SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*' | |
317 } {{204 1 3 3 0} {204 1 3 3 0} {204 1 3 3 0}} | |
318 | |
319 #------------------------------------------------------------------------- | |
320 | |
321 do_execsql_test 9.1 { | |
322 CREATE VIRTUAL TABLE t12 USING fts5(content); | |
323 INSERT INTO t12 VALUES('a b c d'); | |
324 SELECT mit(matchinfo(t12, 'x')) FROM t12 WHERE t12 MATCH 'NEAR(a d, 1) OR a'; | |
325 } {{0 1 1 0 1 1 1 1 1}} | |
326 do_execsql_test 9.2 { | |
327 INSERT INTO t12 VALUES('a d c d'); | |
328 SELECT mit(matchinfo(t12, 'x')) FROM t12 WHERE t12 MATCH 'NEAR(a d, 1) OR a'; | |
329 } { | |
330 {0 2 2 0 3 2 1 2 2} {1 2 2 1 3 2 1 2 2} | |
331 } | |
332 do_execsql_test 9.3 { | |
333 INSERT INTO t12 VALUES('a d d a'); | |
334 SELECT mit(matchinfo(t12, 'x')) FROM t12 WHERE t12 MATCH 'NEAR(a d, 1) OR a'; | |
335 } { | |
336 {0 4 3 0 5 3 1 4 3} {1 4 3 1 5 3 1 4 3} {2 4 3 2 5 3 2 4 3} | |
337 } | |
338 | |
339 #--------------------------------------------------------------------------- | |
340 # Test for a memory leak | |
341 # | |
342 do_execsql_test 10.1 { | |
343 DROP TABLE t10; | |
344 CREATE VIRTUAL TABLE t10 USING fts5(idx, value); | |
345 INSERT INTO t10 values (1, 'one'),(2, 'two'),(3, 'three'); | |
346 SELECT t10.rowid, t10.* | |
347 FROM t10 | |
348 JOIN (SELECT 1 AS idx UNION SELECT 2 UNION SELECT 3) AS x | |
349 WHERE t10 MATCH x.idx | |
350 AND matchinfo(t10) not null | |
351 GROUP BY t10.rowid | |
352 ORDER BY 1; | |
353 } {1 1 one 2 2 two 3 3 three} | |
354 | |
355 #--------------------------------------------------------------------------- | |
356 # Test the 'y' matchinfo flag | |
357 # | |
358 reset_db | |
359 sqlite3_fts5_register_matchinfo db | |
360 do_execsql_test 11.0 { | |
361 CREATE VIRTUAL TABLE tt USING fts5(x, y); | |
362 INSERT INTO tt VALUES('c d a c d d', 'e a g b d a'); -- 1 | |
363 INSERT INTO tt VALUES('c c g a e b', 'c g d g e c'); -- 2 | |
364 INSERT INTO tt VALUES('b e f d e g', 'b a c b c g'); -- 3 | |
365 INSERT INTO tt VALUES('a c f f g d', 'd b f d e g'); -- 4 | |
366 INSERT INTO tt VALUES('g a c f c f', 'd g g b c c'); -- 5 | |
367 INSERT INTO tt VALUES('g a c e b b', 'd b f b g g'); -- 6 | |
368 INSERT INTO tt VALUES('f d a a f c', 'e e a d c f'); -- 7 | |
369 INSERT INTO tt VALUES('a c b b g f', 'a b a e d f'); -- 8 | |
370 INSERT INTO tt VALUES('b a f e c c', 'f d b b a b'); -- 9 | |
371 INSERT INTO tt VALUES('f d c e a c', 'f a f a a f'); -- 10 | |
372 } | |
373 | |
374 db func mit mit | |
375 foreach {tn expr res} { | |
376 1 "a" { | |
377 1 {1 2} 2 {1 0} 3 {0 1} 4 {1 0} 5 {1 0} | |
378 6 {1 0} 7 {2 1} 8 {1 2} 9 {1 1} 10 {1 3} | |
379 } | |
380 | |
381 2 "b" { | |
382 1 {0 1} 2 {1 0} 3 {1 2} 4 {0 1} 5 {0 1} | |
383 6 {2 2} 8 {2 1} 9 {1 3} | |
384 } | |
385 | |
386 3 "y:a" { | |
387 1 {0 2} 3 {0 1} | |
388 7 {0 1} 8 {0 2} 9 {0 1} 10 {0 3} | |
389 } | |
390 | |
391 4 "x:a" { | |
392 1 {1 0} 2 {1 0} 4 {1 0} 5 {1 0} | |
393 6 {1 0} 7 {2 0} 8 {1 0} 9 {1 0} 10 {1 0} | |
394 } | |
395 | |
396 5 "a OR b" { | |
397 1 {1 2 0 1} 2 {1 0 1 0} 3 {0 1 1 2} 4 {1 0 0 1} 5 {1 0 0 1} | |
398 6 {1 0 2 2} 7 {2 1 0 0} 8 {1 2 2 1} 9 {1 1 1 3} 10 {1 3 0 0} | |
399 } | |
400 | |
401 6 "a AND b" { | |
402 1 {1 2 0 1} 2 {1 0 1 0} 3 {0 1 1 2} 4 {1 0 0 1} 5 {1 0 0 1} | |
403 6 {1 0 2 2} 8 {1 2 2 1} 9 {1 1 1 3} | |
404 } | |
405 | |
406 7 "a OR (a AND b)" { | |
407 1 {1 2 1 2 0 1} 2 {1 0 1 0 1 0} 3 {0 1 0 1 1 2} 4 {1 0 1 0 0 1} | |
408 5 {1 0 1 0 0 1} 6 {1 0 1 0 2 2} 7 {2 1 0 0 0 0} 8 {1 2 1 2 2 1} | |
409 9 {1 1 1 1 1 3} 10 {1 3 0 0 0 0} | |
410 } | |
411 | |
412 } { | |
413 do_execsql_test 11.1.$tn.1 { | |
414 SELECT rowid, mit(matchinfo(tt, 'y')) FROM tt WHERE tt MATCH $expr | |
415 } $res | |
416 | |
417 set r2 [list] | |
418 foreach {rowid L} $res { | |
419 lappend r2 $rowid | |
420 set M [list] | |
421 foreach {a b} $L { | |
422 lappend M [expr ($a ? 1 : 0) + ($b ? 2 : 0)] | |
423 } | |
424 lappend r2 $M | |
425 } | |
426 | |
427 do_execsql_test 11.1.$tn.2 { | |
428 SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr | |
429 } $r2 | |
430 | |
431 do_execsql_test 11.1.$tn.2 { | |
432 SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr | |
433 } $r2 | |
434 } | |
435 | |
436 #--------------------------------------------------------------------------- | |
437 # Test the 'b' matchinfo flag | |
438 # | |
439 reset_db | |
440 sqlite3_fts5_register_matchinfo db | |
441 db func mit mit | |
442 | |
443 do_test 12.0 { | |
444 set cols [list] | |
445 for {set i 0} {$i < 50} {incr i} { lappend cols "c$i" } | |
446 execsql "CREATE VIRTUAL TABLE tt USING fts5([join $cols ,])" | |
447 } {} | |
448 | |
449 do_execsql_test 12.1 { | |
450 INSERT INTO tt (rowid, c4, c45) VALUES(1, 'abc', 'abc'); | |
451 SELECT mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH 'abc'; | |
452 } [list [list [expr 1<<4] [expr 1<<(45-32)]]] | |
453 | |
454 finish_test | |
455 | |
OLD | NEW |