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 #***********************************************************************
13 source [file join [file dirname [info script]] fts5_common.tcl]
14 set testprefix fts5matchinfo
16 # If SQLITE_ENABLE_FTS5 is not defined, omit this file.
17 ifcapable !fts5 { finish_test ; return }
19 foreach_detail_mode $testprefix {
22 set scan(littleEndian) i*
23 set scan(bigEndian) I*
24 binary scan $blob $scan($::tcl_platform(byteOrder)) r
29 sqlite3_fts5_register_matchinfo db
32 CREATE VIRTUAL TABLE t1 USING fts5(content, detail=%DETAIL%);
36 INSERT INTO t1(content) VALUES('I wandered lonely as a cloud');
37 INSERT INTO t1(content) VALUES('That floats on high o''er vales and hills,');
38 INSERT INTO t1(content) VALUES('When all at once I saw a crowd,');
39 INSERT INTO t1(content) VALUES('A host, of golden daffodils,');
40 SELECT mit(matchinfo(t1)) FROM t1 WHERE t1 MATCH 'I';
41 } {{1 1 1 2 2} {1 1 1 2 2}}
43 # Now create an FTS4 table that does not specify matchinfo=fts3.
46 CREATE VIRTUAL TABLE t2 USING fts5(content, detail=%DETAIL%);
47 INSERT INTO t2 SELECT * FROM t1;
48 SELECT mit(matchinfo(t2)) FROM t2 WHERE t2 MATCH 'I';
49 } {{1 1 1 2 2} {1 1 1 2 2}}
52 #--------------------------------------------------------------------------
53 # Proc [do_matchinfo_test] is used to test the FTSX matchinfo() function.
55 # The first argument - $tn - is a test identifier. This may be either a
56 # full identifier (i.e. "fts3matchinfo-1.1") or, if global var $testprefix
57 # is set, just the numeric component (i.e. "1.1").
59 # The second argument is the name of an FTSX table. The third is the
60 # full text of a WHERE/MATCH expression to query the table for
61 # (i.e. "t1 MATCH 'abc'"). The final argument - $results - should be a
62 # key-value list (serialized array) with matchinfo() format specifiers
63 # as keys, and the results of executing the statement:
65 # SELECT matchinfo($tbl, '$key') FROM $tbl WHERE $expr
69 # CREATE VIRTUAL TABLE t1 USING fts4;
70 # INSERT INTO t1 VALUES('abc');
71 # INSERT INTO t1 VALUES('def');
72 # INSERT INTO t1 VALUES('abc abc');
74 # do_matchinfo_test 1.1 t1 "t1 MATCH 'abc'" {
81 # If the $results list contains keys mapped to "-" instead of a matchinfo()
82 # result, then this command computes the expected results based on other
83 # mappings to test the matchinfo() function. For example, the command above
84 # could be changed to:
86 # do_matchinfo_test 1.1 t1 "t1 MATCH 'abc'" {
87 # n {3 3} p {1 1} c {1 1} x {{1 3 2} {2 3 2}}
91 # And this command would compute the expected results for matchinfo(t1, 'pcx')
92 # based on the results of matchinfo(t1, 'p'), matchinfo(t1, 'c') and
93 # matchinfo(t1, 'x') in order to test 'pcx'.
95 proc do_matchinfo_test {tn tbl expr results} {
97 foreach {fmt res} $results {
98 if {$res == "-"} continue
99 set resarray($fmt) $res
103 foreach {fmt res} [array get resarray] {
104 if {[llength $res]>$nRow} { set nRow [llength $res] }
107 # Construct expected results for any formats for which the caller
108 # supplied result is "-".
110 foreach {fmt res} $results {
113 for {set iRow 0} {$iRow<$nRow} {incr iRow} {
115 foreach c [split $fmt ""] {
116 set rowres [concat $rowres [lindex $resarray($c) $iRow]]
120 set resarray($fmt) $res
124 # Test each matchinfo() request individually.
126 foreach {fmt res} [array get resarray] {
127 set sql "SELECT mit(matchinfo($tbl, '$fmt')) FROM $tbl WHERE $expr"
128 do_execsql_test $tn.$fmt $sql [normalize2 $res]
131 # Test them all executed together (multiple invocations of matchinfo()).
134 foreach {format res} [array get resarray] {
135 lappend exprlist "mit(matchinfo($tbl, '$format'))"
138 for {set iRow 0} {$iRow<$nRow} {incr iRow} {
139 foreach {format res} [array get resarray] {
140 lappend allres [lindex $res $iRow]
143 set sql "SELECT [join $exprlist ,] FROM $tbl WHERE $expr"
144 do_execsql_test $tn.multi $sql [normalize2 $allres]
146 proc normalize2 {list_of_lists} {
148 foreach elem $list_of_lists {
149 lappend res [list {*}$elem]
154 # Similar to [do_matchinfo_test], except that this is a no-op if the FTS5
155 # mode is not detail=full.
157 proc do_matchinfo_p_test {tn tbl expr results} {
158 if {[detail_is_full]} {
159 uplevel [list do_matchinfo_test $tn $tbl $expr $results]
163 do_execsql_test 4.1.0 {
164 CREATE VIRTUAL TABLE t4 USING fts5(x, y, detail=%DETAIL%);
165 INSERT INTO t4 VALUES('a b c d e', 'f g h i j');
166 INSERT INTO t4 VALUES('f g h i j', 'a b c d e');
169 do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} {
173 do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} {
176 {1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1}
177 {0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1}
181 do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} {
185 {1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1}
186 {0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1}
194 xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc -
198 do_matchinfo_p_test 4.1.2 t4 {t4 MATCH '"g h i"'} {
211 xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc -
215 do_matchinfo_test 4.1.3 t4 {t4 MATCH 'a b'} { s {{2 0} {0 2}} }
216 do_matchinfo_p_test 4.1.4 t4 {t4 MATCH '"a b" c'} { s {{2 0} {0 2}} }
217 do_matchinfo_p_test 4.1.5 t4 {t4 MATCH 'a "b c"'} { s {{2 0} {0 2}} }
218 do_matchinfo_test 4.1.6 t4 {t4 MATCH 'd d'} { s {{1 0} {0 1}} }
219 do_matchinfo_test 4.1.7 t4 {t4 MATCH 'f OR abcd'} {
221 {0 1 1 1 1 1 0 0 0 0 0 0}
222 {1 1 1 0 1 1 0 0 0 0 0 0}
225 do_matchinfo_test 4.1.8 t4 {t4 MATCH 'f NOT abcd'} {
227 {0 1 1 1 1 1 0 0 0 0 0 0}
228 {1 1 1 0 1 1 0 0 0 0 0 0}
232 do_execsql_test 4.2.0 {
233 CREATE VIRTUAL TABLE t5 USING fts5(content, detail=%DETAIL%);
234 INSERT INTO t5 VALUES('a a a a a');
235 INSERT INTO t5 VALUES('a b a b a');
236 INSERT INTO t5 VALUES('c b c b c');
237 INSERT INTO t5 VALUES('x x x x x');
239 do_matchinfo_test 4.2.1 t5 {t5 MATCH 'a a'} {
240 x {{5 8 2 5 8 2} {3 8 2 3 8 2}}
243 do_matchinfo_test 4.2.2 t5 {t5 MATCH 'a b'} { s {2} }
244 do_matchinfo_test 4.2.3 t5 {t5 MATCH 'a b a'} { s {3} }
245 do_matchinfo_test 4.2.4 t5 {t5 MATCH 'a a a'} { s {3 1} }
246 do_matchinfo_p_test 4.2.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} }
247 do_matchinfo_test 4.2.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1} }
249 do_execsql_test 4.3.0 "INSERT INTO t5 VALUES('x y [string repeat {b } 50000]')";
251 # It used to be that the second 'a' token would be deferred. That doesn't
254 do_matchinfo_test 4.3.1 t5 {t5 MATCH 'a a'} {
255 x {{5 8 2 5 5 5} {3 8 2 3 5 5}}
260 do_matchinfo_test 4.3.2 t5 {t5 MATCH 'a b'} { s {2} }
261 do_matchinfo_test 4.3.3 t5 {t5 MATCH 'a b a'} { s {3} }
262 do_matchinfo_test 4.3.4 t5 {t5 MATCH 'a a a'} { s {3 1} }
263 do_matchinfo_p_test 4.3.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} }
264 do_matchinfo_test 4.3.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1 1} }
266 do_execsql_test 4.4.0.1 { INSERT INTO t5(t5) VALUES('optimize') }
268 do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} }
269 do_matchinfo_test 4.4.1 t5 {t5 MATCH 'a a'} { s {2 1} }
270 do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} }
271 do_matchinfo_test 4.4.3 t5 {t5 MATCH 'a b a'} { s {3} }
272 do_matchinfo_test 4.4.4 t5 {t5 MATCH 'a a a'} { s {3 1} }
273 do_matchinfo_p_test 4.4.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} }
275 do_execsql_test 4.5.0 {
276 CREATE VIRTUAL TABLE t6 USING fts5(a, b, c, detail=%DETAIL%);
277 INSERT INTO t6 VALUES('a', 'b', 'c');
279 do_matchinfo_test 4.5.1 t6 {t6 MATCH 'a b c'} { s {{1 1 1}} }
282 #-------------------------------------------------------------------------
283 # Test the outcome of matchinfo() when used within a query that does not
284 # use the full-text index (i.e. lookup by rowid or full-table scan).
286 do_execsql_test 7.1 {
287 CREATE VIRTUAL TABLE t10 USING fts5(content, detail=%DETAIL%);
288 INSERT INTO t10 VALUES('first record');
289 INSERT INTO t10 VALUES('second record');
291 do_execsql_test 7.2 {
292 SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10;
294 do_execsql_test 7.3 {
295 SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10 WHERE rowid=1;
297 do_execsql_test 7.4 {
298 SELECT typeof(matchinfo(t10)), length(matchinfo(t10))
299 FROM t10 WHERE t10 MATCH 'record'
302 #-------------------------------------------------------------------------
303 # Test a special case - matchinfo('nxa') with many zero length documents.
304 # Special because "x" internally uses a statement used by both "n" and "a".
305 # This was causing a problem at one point in the obscure case where the
306 # total number of bytes of data stored in an fts3 table was greater than
307 # the number of rows. i.e. when the following query returns true:
309 # SELECT sum(length(content)) < count(*) FROM fts4table;
311 do_execsql_test 8.1 {
312 CREATE VIRTUAL TABLE t11 USING fts5(content, detail=%DETAIL%);
313 INSERT INTO t11(t11, rank) VALUES('pgsz', 32);
314 INSERT INTO t11 VALUES('quitealongstringoftext');
315 INSERT INTO t11 VALUES('anotherquitealongstringoftext');
316 INSERT INTO t11 VALUES('athirdlongstringoftext');
317 INSERT INTO t11 VALUES('andonemoreforgoodluck');
320 for {set i 0} {$i < 200} {incr i} {
321 execsql { INSERT INTO t11 VALUES('') }
323 execsql { INSERT INTO t11(t11) VALUES('optimize') }
325 do_execsql_test 8.3 {
326 SELECT mit(matchinfo(t11, 'nxa')) FROM t11 WHERE t11 MATCH 'a*'
327 } {{204 1 3 3 0} {204 1 3 3 0} {204 1 3 3 0}}
329 #-------------------------------------------------------------------------
331 if {[detail_is_full]} {
332 do_execsql_test 9.1 {
333 CREATE VIRTUAL TABLE t12 USING fts5(content, detail=%DETAIL%);
334 INSERT INTO t12 VALUES('a b c d');
335 SELECT mit(matchinfo(t12,'x')) FROM t12 WHERE t12 MATCH 'NEAR(a d, 1) OR a';
336 } {{0 1 1 0 1 1 1 1 1}}
337 do_execsql_test 9.2 {
338 INSERT INTO t12 VALUES('a d c d');
339 SELECT mit(matchinfo(t12,'x')) FROM t12 WHERE t12 MATCH 'NEAR(a d, 1) OR a';
341 {0 2 2 0 3 2 1 2 2} {1 2 2 1 3 2 1 2 2}
343 do_execsql_test 9.3 {
344 INSERT INTO t12 VALUES('a d d a');
345 SELECT mit(matchinfo(t12,'x')) FROM t12 WHERE t12 MATCH 'NEAR(a d, 1) OR a';
347 {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}
351 #---------------------------------------------------------------------------
352 # Test for a memory leak
354 do_execsql_test 10.1 {
356 CREATE VIRTUAL TABLE t10 USING fts5(idx, value, detail=%DETAIL%);
357 INSERT INTO t10 values (1, 'one'),(2, 'two'),(3, 'three');
358 SELECT t10.rowid, t10.*
360 JOIN (SELECT 1 AS idx UNION SELECT 2 UNION SELECT 3) AS x
361 WHERE t10 MATCH x.idx
362 AND matchinfo(t10) not null
365 } {1 1 one 2 2 two 3 3 three}
367 #---------------------------------------------------------------------------
368 # Test the 'y' matchinfo flag
371 sqlite3_fts5_register_matchinfo db
372 do_execsql_test 11.0 {
373 CREATE VIRTUAL TABLE tt USING fts5(x, y, detail=%DETAIL%);
374 INSERT INTO tt VALUES('c d a c d d', 'e a g b d a'); -- 1
375 INSERT INTO tt VALUES('c c g a e b', 'c g d g e c'); -- 2
376 INSERT INTO tt VALUES('b e f d e g', 'b a c b c g'); -- 3
377 INSERT INTO tt VALUES('a c f f g d', 'd b f d e g'); -- 4
378 INSERT INTO tt VALUES('g a c f c f', 'd g g b c c'); -- 5
379 INSERT INTO tt VALUES('g a c e b b', 'd b f b g g'); -- 6
380 INSERT INTO tt VALUES('f d a a f c', 'e e a d c f'); -- 7
381 INSERT INTO tt VALUES('a c b b g f', 'a b a e d f'); -- 8
382 INSERT INTO tt VALUES('b a f e c c', 'f d b b a b'); -- 9
383 INSERT INTO tt VALUES('f d c e a c', 'f a f a a f'); -- 10
387 foreach {tn expr res} {
389 1 {1 2} 2 {1 0} 3 {0 1} 4 {1 0} 5 {1 0}
390 6 {1 0} 7 {2 1} 8 {1 2} 9 {1 1} 10 {1 3}
394 1 {0 1} 2 {1 0} 3 {1 2} 4 {0 1} 5 {0 1}
395 6 {2 2} 8 {2 1} 9 {1 3}
400 7 {0 1} 8 {0 2} 9 {0 1} 10 {0 3}
404 1 {1 0} 2 {1 0} 4 {1 0} 5 {1 0}
405 6 {1 0} 7 {2 0} 8 {1 0} 9 {1 0} 10 {1 0}
409 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}
410 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}
414 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}
415 6 {1 0 2 2} 8 {1 2 2 1} 9 {1 1 1 3}
419 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}
420 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}
421 9 {1 1 1 1 1 3} 10 {1 3 0 0 0 0}
426 if {[string match *:* $expr] && [detail_is_none]} continue
427 do_execsql_test 11.1.$tn.1 {
428 SELECT rowid, mit(matchinfo(tt, 'y')) FROM tt WHERE tt MATCH $expr
432 foreach {rowid L} $res {
436 lappend M [expr ($a ? 1 : 0) + ($b ? 2 : 0)]
441 do_execsql_test 11.1.$tn.2 {
442 SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr
445 do_execsql_test 11.1.$tn.2 {
446 SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr
450 #---------------------------------------------------------------------------
451 # Test the 'b' matchinfo flag
454 sqlite3_fts5_register_matchinfo db
459 for {set i 0} {$i < 50} {incr i} { lappend cols "c$i" }
460 execsql "CREATE VIRTUAL TABLE tt USING fts5([join $cols ,], detail=%DETAIL%)"
463 do_execsql_test 12.1 {
464 INSERT INTO tt (rowid, c4, c45) VALUES(1, 'abc', 'abc');
465 SELECT mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH 'abc';
466 } [list [list [expr 1<<4] [expr 1<<(45-32)]]]
468 } ;# foreach_detail_mode
470 #-------------------------------------------------------------------------
471 # Test that a bad fts5() return is detected
475 db func fts5 -argcount 0 xyz
477 list [catch { sqlite3_fts5_register_matchinfo db } msg] $msg
480 #-------------------------------------------------------------------------
481 # Test that an invalid matchinfo() flag is detected
484 sqlite3_fts5_register_matchinfo db
485 do_execsql_test 14.1 {
486 CREATE VIRTUAL TABLE x1 USING fts5(z);
487 INSERT INTO x1 VALUES('a b c a b c a b c');
490 do_catchsql_test 14.2 {
491 SELECT matchinfo(x1, 'd') FROM x1('a b c');
492 } {1 {unrecognized matchinfo flag: d}}