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 languageid=xxx FTS4 option.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set ::testprefix fts4content
19 # If SQLITE_ENABLE_FTS3 is defined, omit this file.
25 set ::testprefix fts4langid
27 #---------------------------------------------------------------------------
30 # 1.* - Warm-body tests created for specific purposes during development.
31 # Passing these doesn't really prove much.
33 # 2.1.* - Test that FTS queries only ever return rows associated with
34 # the requested language.
36 # 2.2.* - Same as 2.1.*, after an 'optimize' command.
38 # 2.3.* - Same as 2.1.*, after a 'rebuild' command.
40 # 3.* - Tests with content= tables. Both where there is a real
41 # underlying content table and where there is not.
43 # 4.* - Test that if one is provided, the tokenizer xLanguage method
44 # is called to configure the tokenizer before tokenizing query
47 # 5.* - Test the fts4aux table when the associated FTS4 table contains
52 CREATE VIRTUAL TABLE t1 USING fts4(a, b, languageid=lang_id);
56 SELECT sql FROM sqlite_master WHERE name = 't1_content';
57 } {{CREATE TABLE 't1_content'(docid INTEGER PRIMARY KEY, 'c0a', 'c1b', langid)}}
59 do_execsql_test 1.3 {SELECT docid FROM t1} {}
60 do_execsql_test 1.4 {SELECT lang_id FROM t1} {}
62 do_execsql_test 1.5 {INSERT INTO t1(a, b) VALUES('aaa', 'bbb')}
63 do_execsql_test 1.6 {SELECT lang_id FROM t1 } {0}
65 do_execsql_test 1.7 {INSERT INTO t1(a, b, lang_id) VALUES('aaa', 'bbb', 4)}
66 do_execsql_test 1.8 {SELECT lang_id FROM t1 } {0 4}
68 do_execsql_test 1.9 {INSERT INTO t1(a, b, lang_id) VALUES('aaa', 'bbb', 'xyz')}
69 do_execsql_test 1.10 {SELECT lang_id FROM t1} {0 4 0}
71 do_execsql_test 1.11 {
72 CREATE VIRTUAL TABLE t2 USING fts4;
73 INSERT INTO t2 VALUES('abc');
75 do_execsql_test 1.12 { SELECT rowid FROM t2 WHERE content MATCH 'abc' } 1
77 do_execsql_test 1.13 {
79 CREATE VIRTUAL TABLE t1 USING fts4(languageid=lang_id);
80 INSERT INTO t1(content) VALUES('a b c');
81 INSERT INTO t1(content, lang_id) VALUES('a b c', 1);
84 do_execsql_test 1.14 {
85 SELECT rowid FROM t1 WHERE t1 MATCH 'b';
87 do_execsql_test 1.15 {
88 SELECT rowid FROM t1 WHERE t1 MATCH 'b' AND lang_id = 0;
91 do_execsql_test 1.16 {
92 SELECT rowid FROM t1 WHERE t1 MATCH 'b' AND lang_id = 1;
95 do_catchsql_test 1.17 {
96 INSERT INTO t1(content, lang_id) VALUES('123', -1);
97 } {1 {constraint failed}}
99 do_execsql_test 1.18 {
101 CREATE VIRTUAL TABLE t1 USING fts4(languageid=lang_id);
102 INSERT INTO t1(content, lang_id) VALUES('A', 13);
103 INSERT INTO t1(content, lang_id) VALUES('B', 13);
104 INSERT INTO t1(content, lang_id) VALUES('C', 13);
105 INSERT INTO t1(content, lang_id) VALUES('D', 13);
106 INSERT INTO t1(content, lang_id) VALUES('E', 13);
107 INSERT INTO t1(content, lang_id) VALUES('F', 13);
108 INSERT INTO t1(content, lang_id) VALUES('G', 13);
109 INSERT INTO t1(content, lang_id) VALUES('H', 13);
110 INSERT INTO t1(content, lang_id) VALUES('I', 13);
111 INSERT INTO t1(content, lang_id) VALUES('J', 13);
112 INSERT INTO t1(content, lang_id) VALUES('K', 13);
113 INSERT INTO t1(content, lang_id) VALUES('L', 13);
114 INSERT INTO t1(content, lang_id) VALUES('M', 13);
115 INSERT INTO t1(content, lang_id) VALUES('N', 13);
116 INSERT INTO t1(content, lang_id) VALUES('O', 13);
117 INSERT INTO t1(content, lang_id) VALUES('P', 13);
118 INSERT INTO t1(content, lang_id) VALUES('Q', 13);
119 INSERT INTO t1(content, lang_id) VALUES('R', 13);
120 INSERT INTO t1(content, lang_id) VALUES('S', 13);
121 SELECT rowid FROM t1 WHERE t1 MATCH 'A';
125 #-------------------------------------------------------------------------
128 proc build_multilingual_db_1 {db} {
129 $db eval { CREATE VIRTUAL TABLE t2 USING fts4(x, y, languageid=l) }
131 set xwords [list zero one two three four five six seven eight nine ten]
132 set ywords [list alpha beta gamma delta epsilon zeta eta theta iota kappa]
134 for {set i 0} {$i < 1000} {incr i} {
135 set iLangid [expr $i%9]
140 lappend x [lindex $xwords [expr ($i / 1000) % 10]]
141 lappend x [lindex $xwords [expr ($i / 100) % 10]]
142 lappend x [lindex $xwords [expr ($i / 10) % 10]]
143 lappend x [lindex $xwords [expr ($i / 1) % 10]]
146 lappend y [lindex $ywords [expr ($i / 1000) % 10]]
147 lappend y [lindex $ywords [expr ($i / 100) % 10]]
148 lappend y [lindex $ywords [expr ($i / 10) % 10]]
149 lappend y [lindex $ywords [expr ($i / 1) % 10]]
151 $db eval { INSERT INTO t2(docid, x, y, l) VALUES($i, $x, $y, $iLangid) }
155 CREATE TABLE data(x, y, l);
156 INSERT INTO data(rowid, x, y, l) SELECT docid, x, y, l FROM t2;
160 proc rowid_list_set_langid {langid} {
161 set ::rowid_list_langid $langid
163 proc rowid_list {pattern} {
164 set langid $::rowid_list_langid
166 db eval {SELECT rowid, x, y FROM data WHERE l = $langid ORDER BY rowid ASC} {
167 if {[string match "*$pattern*" $x] || [string match "*$pattern*" $y]} {
174 proc or_merge_list {list1 list2} {
180 set n1 [llength $list1]
181 set n2 [llength $list2]
183 while {$i1 < $n1 && $i2 < $n2} {
184 set e1 [lindex $list1 $i1]
185 set e2 [lindex $list2 $i2]
191 } elseif {$e1 < $e2} {
200 concat $res [lrange $list1 $i1 end] [lrange $list2 $i2 end]
203 proc or_merge_lists {args} {
204 set res [lindex $args 0]
205 for {set i 1} {$i < [llength $args]} {incr i} {
206 set res [or_merge_list $res [lindex $args $i]]
211 proc and_merge_list {list1 list2} {
212 foreach i $list2 { set a($i) 1 }
215 if {[info exists a($i)]} {lappend res $i}
221 proc and_merge_lists {args} {
222 set res [lindex $args 0]
223 for {set i 1} {$i < [llength $args]} {incr i} {
224 set res [and_merge_list $res [lindex $args $i]]
229 proc filter_list {list langid} {
232 if {($i % 9) == $langid} {lappend res $i}
239 build_multilingual_db_1 db
242 proc do_test_query1 {tn query res_script} {
243 for {set langid 0} {$langid < 10} {incr langid} {
244 rowid_list_set_langid $langid
245 set res [eval $res_script]
248 execsql {SELECT docid FROM t2 WHERE t2 MATCH $query AND l = $langid}
250 do_test $tn.$langid [list set {} $actual] $res
255 do_test_query1 2.1.1 {delta} { rowid_list delta }
256 do_test_query1 2.1.2 {"zero one two"} { rowid_list "zero one two" }
257 do_test_query1 2.1.3 {zero one two} {
258 and_merge_lists [rowid_list zero] [rowid_list one] [rowid_list two]
260 do_test_query1 2.1.4 {"zero one" OR "one two"} {
261 or_merge_lists [rowid_list "zero one"] [rowid_list "one two"]
264 # Now try the same tests as above, but after running the 'optimize'
265 # command on the FTS table.
267 do_execsql_test 2.2 {
268 INSERT INTO t2(t2) VALUES('optimize');
269 SELECT count(*) FROM t2_segdir;
271 do_test_query1 2.2.1 {delta} { rowid_list delta }
272 do_test_query1 2.2.2 {"zero one two"} { rowid_list "zero one two" }
273 do_test_query1 2.2.3 {zero one two} {
274 and_merge_lists [rowid_list zero] [rowid_list one] [rowid_list two]
276 do_test_query1 2.2.4 {"zero one" OR "one two"} {
277 or_merge_lists [rowid_list "zero one"] [rowid_list "one two"]
284 build_multilingual_db_1 db
285 execsql { INSERT INTO t2(t2) VALUES('rebuild') }
287 do_test_query1 2.3.1 {delta} { rowid_list delta }
288 do_test_query1 2.3.2 {"zero one two"} { rowid_list "zero one two" }
289 do_test_query1 2.3.3 {zero one two} {
290 and_merge_lists [rowid_list zero] [rowid_list one] [rowid_list two]
292 do_test_query1 2.3.4 {"zero one" OR "one two"} {
293 or_merge_lists [rowid_list "zero one"] [rowid_list "one two"]
296 #-------------------------------------------------------------------------
301 build_multilingual_db_1 db
303 CREATE TABLE t3_data(l, x, y);
304 INSERT INTO t3_data(rowid, l, x, y) SELECT docid, l, x, y FROM t2;
308 do_execsql_test 3.1 {
309 CREATE VIRTUAL TABLE t2 USING fts4(content=t3_data, languageid=l);
310 INSERT INTO t2(t2) VALUES('rebuild');
313 do_test_query1 3.1.1 {delta} { rowid_list delta }
314 do_test_query1 3.1.2 {"zero one two"} { rowid_list "zero one two" }
315 do_test_query1 3.1.3 {zero one two} {
316 and_merge_lists [rowid_list zero] [rowid_list one] [rowid_list two]
318 do_test_query1 3.1.4 {"zero one" OR "one two"} {
319 or_merge_lists [rowid_list "zero one"] [rowid_list "one two"]
322 do_execsql_test 3.2.1 {
324 CREATE VIRTUAL TABLE t2 USING fts4(x, y, languageid=l, content=nosuchtable);
327 do_execsql_test 3.2.2 {
328 INSERT INTO t2(docid, x, y, l) SELECT rowid, x, y, l FROM t3_data;
331 do_execsql_test 3.2.3 {
335 do_test_query1 3.3.1 {delta} { rowid_list delta }
336 do_test_query1 3.3.2 {"zero one two"} { rowid_list "zero one two" }
337 do_test_query1 3.3.3 {zero one two} {
338 and_merge_lists [rowid_list zero] [rowid_list one] [rowid_list two]
340 do_test_query1 3.3.4 {"zero one" OR "one two"} {
341 or_merge_lists [rowid_list "zero one"] [rowid_list "one two"]
344 #-------------------------------------------------------------------------
347 proc build_multilingual_db_2 {db} {
349 CREATE VIRTUAL TABLE t4 USING fts4(
350 tokenize=testtokenizer,
354 for {set i 0} {$i < 50} {incr i} {
356 INSERT INTO t4(docid, content, lid) VALUES($i, 'The Quick Brown Fox', $i)
363 set ptr [fts3_test_tokenizer]
364 sqlite3_db_config db SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER 1
365 execsql { SELECT fts3_tokenizer('testtokenizer', $ptr) }
366 build_multilingual_db_2 db
368 do_execsql_test 4.1.1 {
369 SELECT docid FROM t4 WHERE t4 MATCH 'quick';
371 do_execsql_test 4.1.2 {
372 SELECT docid FROM t4 WHERE t4 MATCH 'quick' AND lid=1;
374 do_execsql_test 4.1.3 {
375 SELECT docid FROM t4 WHERE t4 MATCH 'Quick' AND lid=1;
377 for {set i 0} {$i < 50} {incr i} {
378 do_execsql_test 4.1.4.$i {
379 SELECT count(*) FROM t4 WHERE t4 MATCH 'fox' AND lid=$i;
382 do_catchsql_test 4.1.5 {
383 INSERT INTO t4(content, lid) VALUES('hello world', 101)
384 } {1 {SQL logic error or missing database}}
386 #-------------------------------------------------------------------------
389 # The following test cases are designed to detect a 32-bit overflow bug
390 # that existed at one point.
392 proc build_multilingual_db_3 {db} {
394 CREATE VIRTUAL TABLE t5 USING fts4(languageid=lid);
396 set languages [list 0 1 2 [expr 1<<30]]
398 foreach lid $languages {
400 INSERT INTO t5(docid, content, lid) VALUES(
401 $lid, 'My language is ' || $lid, $lid
409 build_multilingual_db_3 db
412 do_execsql_test 5.1.1 {
413 SELECT level FROM t5_segdir;
414 } [list 0 1024 2048 [expr 1<<40]]
416 do_execsql_test 5.1.2 {SELECT docid FROM t5 WHERE t5 MATCH 'language'} 0
417 foreach langid [list 0 1 2 [expr 1<<30]] {
418 do_execsql_test 5.2.$langid {
419 SELECT docid FROM t5 WHERE t5 MATCH 'language' AND lid = $langid
424 do_execsql_test 5.3.1 {
425 CREATE VIRTUAL TABLE t6 USING fts4(languageid=lid);
426 INSERT INTO t6 VALUES('I belong to language 0!');
429 for {set i 0} {$i < 20} {incr i} {
431 INSERT INTO t6(content, lid) VALUES(
432 'I (row '||$i||') belong to langauge N!', $lid
436 execsql { SELECT docid FROM t6 WHERE t6 MATCH 'belong' }
440 execsql { SELECT docid FROM t6 WHERE t6 MATCH 'belong' AND lid=$lid}
441 } {2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21}
443 do_execsql_test 5.3.4 { INSERT INTO t6(t6) VALUES('optimize') } {}
444 do_execsql_test 5.3.5 { SELECT docid FROM t6 WHERE t6 MATCH 'belong' } {1}
445 do_execsql_test 5.3.6 {
446 SELECT docid FROM t6 WHERE t6 MATCH 'belong' AND lid=$lid
447 } {2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21}
451 foreach lid [list 4 [expr 1<<30]] {
452 do_execsql_test 5.4.$lid.1 {
454 SELECT count(*) FROM t6_segdir;
455 SELECT count(*) FROM t6_segments;
457 do_execsql_test 5.4.$lid.2 {
458 INSERT INTO t6(content, lid) VALUES('zero zero zero', $lid);
459 INSERT INTO t6(content, lid) VALUES('zero zero one', $lid);
460 INSERT INTO t6(content, lid) VALUES('zero one zero', $lid);
461 INSERT INTO t6(content, lid) VALUES('zero one one', $lid);
462 INSERT INTO t6(content, lid) VALUES('one zero zero', $lid);
463 INSERT INTO t6(content, lid) VALUES('one zero one', $lid);
464 INSERT INTO t6(content, lid) VALUES('one one zero', $lid);
465 INSERT INTO t6(content, lid) VALUES('one one one', $lid);
467 SELECT docid FROM t6 WHERE t6 MATCH '"zero zero"' AND lid=$lid;
470 do_execsql_test 5.4.$lid.3 {
471 SELECT count(*) FROM t6_segdir;
472 SELECT count(*) FROM t6_segments;
475 do_execsql_test 5.4.$lid.4 {
476 INSERT INTO t6(t6) VALUES('merge=100,3');
477 INSERT INTO t6(t6) VALUES('merge=100,3');
478 SELECT docid FROM t6 WHERE t6 MATCH '"zero zero"' AND lid=$lid;
481 do_execsql_test 5.4.$lid.5 {
482 SELECT count(*) FROM t6_segdir;
483 SELECT count(*) FROM t6_segments;