interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / ext / fts5 / test / fts5matchinfo.test

547 lines · 16567 bytes Raw Blame History
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
13source [file join [file dirname [info script]] fts5_common.tcl]
14set testprefix fts5matchinfo
15
16# If SQLITE_ENABLE_FTS5 is not defined, omit this file.
17ifcapable !fts5 { finish_test ; return }
18
19foreach_detail_mode $testprefix {
20
21proc mit {blob} {
22 set scan(littleEndian) i*
23 set scan(bigEndian) I*
24 binary scan $blob $scan($::tcl_platform(byteOrder)) r
25 return $r
26}
27db func mit mit
28
29sqlite3_fts5_register_matchinfo db
30
31do_execsql_test 1.0 {
32 CREATE VIRTUAL TABLE t1 USING fts5(content, detail=%DETAIL%);
33}
34
35do_execsql_test 1.1 {
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}}
42
43# Now create an FTS4 table that does not specify matchinfo=fts3.
44#
45do_execsql_test 1.2 {
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}}
50
51
52#--------------------------------------------------------------------------
53# Proc [do_matchinfo_test] is used to test the FTSX matchinfo() function.
54#
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").
58#
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:
64#
65# SELECT matchinfo($tbl, '$key') FROM $tbl WHERE $expr
66#
67# For example:
68#
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');
73#
74# do_matchinfo_test 1.1 t1 "t1 MATCH 'abc'" {
75# n {3 3}
76# p {1 1}
77# c {1 1}
78# x {{1 3 2} {2 3 2}}
79# }
80#
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:
85#
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}}
88# pcx -
89# }
90#
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'.
94#
95proc do_matchinfo_test {tn tbl expr results} {
96
97 foreach {fmt res} $results {
98 if {$res == "-"} continue
99 set resarray($fmt) $res
100 }
101
102 set nRow 0
103 foreach {fmt res} [array get resarray] {
104 if {[llength $res]>$nRow} { set nRow [llength $res] }
105 }
106
107 # Construct expected results for any formats for which the caller
108 # supplied result is "-".
109 #
110 foreach {fmt res} $results {
111 if {$res == "-"} {
112 set res [list]
113 for {set iRow 0} {$iRow<$nRow} {incr iRow} {
114 set rowres [list]
115 foreach c [split $fmt ""] {
116 set rowres [concat $rowres [lindex $resarray($c) $iRow]]
117 }
118 lappend res $rowres
119 }
120 set resarray($fmt) $res
121 }
122 }
123
124 # Test each matchinfo() request individually.
125 #
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]
129 }
130
131 # Test them all executed together (multiple invocations of matchinfo()).
132 #
133 set exprlist [list]
134 foreach {format res} [array get resarray] {
135 lappend exprlist "mit(matchinfo($tbl, '$format'))"
136 }
137 set allres [list]
138 for {set iRow 0} {$iRow<$nRow} {incr iRow} {
139 foreach {format res} [array get resarray] {
140 lappend allres [lindex $res $iRow]
141 }
142 }
143 set sql "SELECT [join $exprlist ,] FROM $tbl WHERE $expr"
144 do_execsql_test $tn.multi $sql [normalize2 $allres]
145}
146proc normalize2 {list_of_lists} {
147 set res [list]
148 foreach elem $list_of_lists {
149 lappend res [list {*}$elem]
150 }
151 return $res
152}
153
154# Similar to [do_matchinfo_test], except that this is a no-op if the FTS5
155# mode is not detail=full.
156#
157proc do_matchinfo_p_test {tn tbl expr results} {
158 if {[detail_is_full]} {
159 uplevel [list do_matchinfo_test $tn $tbl $expr $results]
160 }
161}
162
163do_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');
167}
168
169do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} {
170 s {{3 0} {0 3}}
171}
172
173do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} {
174 p {3 3}
175 x {
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}
178 }
179}
180
181do_matchinfo_test 4.1.1 t4 {t4 MATCH 'a b c'} {
182 p {3 3}
183 c {2 2}
184 x {
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}
187 }
188 n {2 2}
189 l {{5 5} {5 5}}
190 a {{5 5} {5 5}}
191
192 s {{3 0} {0 3}}
193
194 xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc -
195 xpxsscplax -
196}
197
198do_matchinfo_p_test 4.1.2 t4 {t4 MATCH '"g h i"'} {
199 p {1 1}
200 c {2 2}
201 x {
202 {0 1 1 1 1 1}
203 {1 1 1 0 1 1}
204 }
205 n {2 2}
206 l {{5 5} {5 5}}
207 a {{5 5} {5 5}}
208
209 s {{0 1} {1 0}}
210
211 xxxxxxxxxxxxxxxxxx - pcx - xpc - ccc - pppxpcpcx - laxnpc -
212 sxsxs -
213}
214
215do_matchinfo_test 4.1.3 t4 {t4 MATCH 'a b'} { s {{2 0} {0 2}} }
216do_matchinfo_p_test 4.1.4 t4 {t4 MATCH '"a b" c'} { s {{2 0} {0 2}} }
217do_matchinfo_p_test 4.1.5 t4 {t4 MATCH 'a "b c"'} { s {{2 0} {0 2}} }
218do_matchinfo_test 4.1.6 t4 {t4 MATCH 'd d'} { s {{1 0} {0 1}} }
219do_matchinfo_test 4.1.7 t4 {t4 MATCH 'f OR abcd'} {
220 x {
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}
223 }
224}
225do_matchinfo_test 4.1.8 t4 {t4 MATCH 'f NOT abcd'} {
226 x {
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}
229 }
230}
231
232do_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');
238}
239do_matchinfo_test 4.2.1 t5 {t5 MATCH 'a a'} {
240 x {{5 8 2 5 8 2} {3 8 2 3 8 2}}
241 s {2 1}
242}
243do_matchinfo_test 4.2.2 t5 {t5 MATCH 'a b'} { s {2} }
244do_matchinfo_test 4.2.3 t5 {t5 MATCH 'a b a'} { s {3} }
245do_matchinfo_test 4.2.4 t5 {t5 MATCH 'a a a'} { s {3 1} }
246do_matchinfo_p_test 4.2.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} }
247do_matchinfo_test 4.2.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1} }
248
249do_execsql_test 4.3.0 "INSERT INTO t5 VALUES('x y [string repeat {b } 50000]')";
250
251# It used to be that the second 'a' token would be deferred. That doesn't
252# work any longer.
253if 0 {
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}}
256 s {2 1}
257 }
258}
259
260do_matchinfo_test 4.3.2 t5 {t5 MATCH 'a b'} { s {2} }
261do_matchinfo_test 4.3.3 t5 {t5 MATCH 'a b a'} { s {3} }
262do_matchinfo_test 4.3.4 t5 {t5 MATCH 'a a a'} { s {3 1} }
263do_matchinfo_p_test 4.3.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} }
264do_matchinfo_test 4.3.6 t5 {t5 MATCH 'a OR b'} { s {1 2 1 1} }
265
266do_execsql_test 4.4.0.1 { INSERT INTO t5(t5) VALUES('optimize') }
267
268do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} }
269do_matchinfo_test 4.4.1 t5 {t5 MATCH 'a a'} { s {2 1} }
270do_matchinfo_test 4.4.2 t5 {t5 MATCH 'a b'} { s {2} }
271do_matchinfo_test 4.4.3 t5 {t5 MATCH 'a b a'} { s {3} }
272do_matchinfo_test 4.4.4 t5 {t5 MATCH 'a a a'} { s {3 1} }
273do_matchinfo_p_test 4.4.5 t5 {t5 MATCH '"a b" "a b"'} { s {2} }
274
275do_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');
278}
279do_matchinfo_test 4.5.1 t6 {t6 MATCH 'a b c'} { s {{1 1 1}} }
280
281
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).
285#
286do_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');
290}
291do_execsql_test 7.2 {
292 SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10;
293} {blob 8 blob 8}
294do_execsql_test 7.3 {
295 SELECT typeof(matchinfo(t10)), length(matchinfo(t10)) FROM t10 WHERE rowid=1;
296} {blob 8}
297do_execsql_test 7.4 {
298 SELECT typeof(matchinfo(t10)), length(matchinfo(t10))
299 FROM t10 WHERE t10 MATCH 'record'
300} {blob 20 blob 20}
301
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:
308#
309# SELECT sum(length(content)) < count(*) FROM fts4table;
310#
311do_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');
318}
319do_test 8.2 {
320 for {set i 0} {$i < 200} {incr i} {
321 execsql { INSERT INTO t11 VALUES('') }
322 }
323 execsql { INSERT INTO t11(t11) VALUES('optimize') }
324} {}
325do_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}}
328
329#-------------------------------------------------------------------------
330
331if {[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';
340 } {
341 {0 2 2 0 3 2 1 2 2} {1 2 2 1 3 2 1 2 2}
342 }
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';
346 } {
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}
348 }
349}
350
351#---------------------------------------------------------------------------
352# Test for a memory leak
353#
354do_execsql_test 10.1 {
355 DROP TABLE t10;
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.*
359 FROM 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
363 GROUP BY t10.rowid
364 ORDER BY 1;
365} {1 1 one 2 2 two 3 3 three}
366
367#---------------------------------------------------------------------------
368# Test the 'y' matchinfo flag
369#
370reset_db
371sqlite3_fts5_register_matchinfo db
372do_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
384}
385
386db func mit mit
387foreach {tn expr res} {
388 1 "a" {
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}
391 }
392
393 2 "b" {
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}
396 }
397
398 3 "y:a" {
399 1 {0 2} 3 {0 1}
400 7 {0 1} 8 {0 2} 9 {0 1} 10 {0 3}
401 }
402
403 4 "x:a" {
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}
406 }
407
408 5 "a OR b" {
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}
411 }
412
413 6 "a AND b" {
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}
416 }
417
418 7 "a OR (a AND b)" {
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}
422 }
423
424} {
425
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
429 } $res
430
431 set r2 [list]
432 foreach {rowid L} $res {
433 lappend r2 $rowid
434 set M [list]
435 foreach {a b} $L {
436 lappend M [expr ($a ? 1 : 0) + ($b ? 2 : 0)]
437 }
438 lappend r2 $M
439 }
440
441 do_execsql_test 11.1.$tn.2 {
442 SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr
443 } $r2
444
445 do_execsql_test 11.1.$tn.2 {
446 SELECT rowid, mit(matchinfo(tt, 'b')) FROM tt WHERE tt MATCH $expr
447 } $r2
448}
449
450#---------------------------------------------------------------------------
451# Test the 'b' matchinfo flag
452#
453reset_db
454sqlite3_fts5_register_matchinfo db
455db func mit mit
456
457do_test 12.0 {
458 set cols [list]
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%)"
461} {}
462
463do_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)]]]
467
468} ;# foreach_detail_mode
469
470#-------------------------------------------------------------------------
471# Test that a bad fts5() return is detected
472#
473reset_db
474proc xyz {} {}
475db func fts5 -argcount 1 xyz
476do_test 13.1 {
477 list [catch { sqlite3_fts5_register_matchinfo db } msg] $msg
478} {1 SQLITE_ERROR}
479
480#-------------------------------------------------------------------------
481# Test that an invalid matchinfo() flag is detected
482#
483reset_db
484sqlite3_fts5_register_matchinfo db
485do_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');
488} {}
489
490do_catchsql_test 14.2 {
491 SELECT matchinfo(x1, 'd') FROM x1('a b c');
492} {1 {unrecognized matchinfo flag: d}}
493
494#-------------------------------------------------------------------------
495# Test using matchinfo() and similar on a non-full-text query
496#
497do_execsql_test 15.0 {
498 CREATE VIRTUAL TABLE t1 USING fts5(x, y);
499 INSERT INTO t1 VALUES('a', 'b');
500 INSERT INTO t1 VALUES('c', 'd');
501}
502
503if {$tcl_platform(byteOrder)=="littleEndian"} {
504 set res {X'02000000'}
505} else {
506 set res {X'00000002'}
507}
508do_execsql_test 15.1 {
509 SELECT quote(matchinfo(t1, 'n')) FROM t1 LIMIT 1;
510} $res
511do_execsql_test 15.2 {
512 DELETE FROM t1_content WHERE rowid=1;
513 SELECT quote(matchinfo(t1, 'n')) FROM t1 LIMIT 1;
514} $res
515
516fts5_aux_test_functions db
517do_execsql_test 15.3 {
518 SELECT fts5_test_all(t1) FROM t1 LIMIT 1;
519} {
520 {columnsize {1 1} columntext {c d} columntotalsize {2 2} poslist {} tokenize {c d} rowcount 2}
521}
522
523#-------------------------------------------------------------------------
524#
525reset_db
526do_execsql_test 16.0 {
527 CREATE TABLE t1(x);
528 BEGIN EXCLUSIVE;
529}
530
531do_test 16.1 {
532 set rc [catch {
533 sqlite3 db2 test.db
534 db2 eval {SELECT * FROM t1}
535 } errmsg]
536 lappend rc $errmsg
537} {1 {database is locked}}
538
539do_execsql_test 16.2 {
540 ROLLBACK;
541}
542
543do_test 16.3 {
544 catchsql { SELECT * FROM t1 } db2
545} {0 {}}
546
547finish_test