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 #***********************************************************************
12 # Tests focusing on the built-in fts5 tokenizers.
15 source [file join [file dirname [info script]] fts5_common.tcl]
16 set testprefix fts5tokenizer
18 # If SQLITE_ENABLE_FTS5 is defined, omit this file.
26 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize=porter);
30 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize='porter');
34 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize = porter);
38 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize = 'porter');
42 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize = 'porter ascii');
46 do_catchsql_test 1.5 {
47 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize = 'nosuch');
48 } {1 {no such tokenizer: nosuch}}
50 do_catchsql_test 1.6 {
51 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize = 'porter nosuch');
52 } {1 {error in tokenizer constructor}}
55 CREATE VIRTUAL TABLE ft1 USING fts5(x, tokenize=porter);
56 INSERT INTO ft1 VALUES('embedded databases');
58 do_execsql_test 2.1 { SELECT rowid FROM ft1 WHERE ft1 MATCH 'embedding' } 1
59 do_execsql_test 2.2 { SELECT rowid FROM ft1 WHERE ft1 MATCH 'database' } 1
61 SELECT rowid FROM ft1 WHERE ft1 MATCH 'database embedding'
64 proc tcl_create {args} {
68 sqlite3_fts5_create_tokenizer db tcl tcl_create
70 foreach {tn directive expected} {
71 1 {tokenize='tcl a b c'} {a b c}
72 2 {tokenize='tcl ''d'' ''e'' ''f'''} {d e f}
73 3 {tokenize="tcl 'g' 'h' 'i'"} {g h i}
76 do_catchsql_test 3.$tn.1 "
77 CREATE VIRTUAL TABLE ft2 USING fts5(x, $directive)
78 " {1 {error in tokenizer constructor}}
79 do_test 3.$tn.2 { set ::targs } $expected
82 do_catchsql_test 4.1 {
83 CREATE VIRTUAL TABLE ft2 USING fts5(x, tokenize = tcl abc);
84 } {1 {parse error in "tokenize = tcl abc"}}
85 do_catchsql_test 4.2 {
86 CREATE VIRTUAL TABLE ft2 USING fts5(x y)
87 } {1 {unrecognized column option: y}}
89 #-------------------------------------------------------------------------
90 # Test the "separators" and "tokenchars" options a bit.
92 foreach {tn tokenizer} {1 ascii 2 unicode61} {
94 set T "$tokenizer tokenchars ',.:' separators 'xyz'"
95 execsql "CREATE VIRTUAL TABLE t1 USING fts5(x, tokenize = \"$T\")"
96 do_execsql_test 5.$tn.1 {
97 INSERT INTO t1 VALUES('abcxdefyghizjkl.mno,pqr:stu/vwx+yz');
99 foreach {tn2 token res} {
100 1 abc 1 2 def 1 3 ghi 1 4 jkl {}
101 5 mno {} 6 pqr {} 7 stu {} 8 jkl.mno,pqr:stu 1
104 do_execsql_test 5.$tn.2.$tn2 "
105 SELECT rowid FROM t1 WHERE t1 MATCH '\"$token\"'
110 #-------------------------------------------------------------------------
111 # Miscellaneous tests for the ascii tokenizer.
113 # 5.1.*: Test that the ascii tokenizer ignores non-ASCII characters in the
114 # 'separators' option. But unicode61 does not.
116 # 5.2.*: An option without an argument is an error.
121 CREATE VIRTUAL TABLE a1 USING fts5(x, tokenize=`ascii separators '\u1234'`);
122 INSERT INTO a1 VALUES('abc\u1234def');
124 execsql { SELECT rowid FROM a1 WHERE a1 MATCH 'def' }
129 CREATE VIRTUAL TABLE a2 USING fts5(
130 x, tokenize=`unicode61 separators '\u1234'`);
131 INSERT INTO a2 VALUES('abc\u1234def');
133 execsql { SELECT rowid FROM a2 WHERE a2 MATCH 'def' }
136 do_catchsql_test 5.2 {
137 CREATE VIRTUAL TABLE a3 USING fts5(x, y, tokenize = 'ascii tokenchars');
138 } {1 {error in tokenizer constructor}}
139 do_catchsql_test 5.3 {
140 CREATE VIRTUAL TABLE a3 USING fts5(x, y, tokenize = 'ascii opt arg');
141 } {1 {error in tokenizer constructor}}
143 #-------------------------------------------------------------------------
144 # Test that the ASCII and unicode61 tokenizers both handle SQLITE_DONE
148 proc test_token_cb {varname token iStart iEnd} {
151 if {[llength $var]==3} { return "SQLITE_DONE" }
155 proc tokenize {cmd} {
157 $cmd xTokenize [$cmd xColumnText 0] [list test_token_cb res]
160 sqlite3_fts5_create_function db tokenize tokenize
162 do_execsql_test 6.0 {
163 CREATE VIRTUAL TABLE x1 USING fts5(a, tokenize=ascii);
164 INSERT INTO x1 VALUES('q w e r t y');
165 INSERT INTO x1 VALUES('y t r e w q');
166 SELECT tokenize(x1) FROM x1 WHERE x1 MATCH 'e AND r';
171 do_execsql_test 6.1 {
172 CREATE VIRTUAL TABLE x2 USING fts5(a, tokenize=unicode61);
173 INSERT INTO x2 VALUES('q w e r t y');
174 INSERT INTO x2 VALUES('y t r e w q');
175 SELECT tokenize(x2) FROM x2 WHERE x2 MATCH 'e AND r';
181 #-------------------------------------------------------------------------
182 # Miscellaneous tests for the unicode tokenizer.
184 do_catchsql_test 6.1 {
185 CREATE VIRTUAL TABLE a3 USING fts5(x, y, tokenize = 'unicode61 tokenchars');
186 } {1 {error in tokenizer constructor}}
187 do_catchsql_test 6.2 {
188 CREATE VIRTUAL TABLE a3 USING fts5(x, y, tokenize = 'unicode61 a b');
189 } {1 {error in tokenizer constructor}}
190 do_catchsql_test 6.3 {
191 CREATE VIRTUAL TABLE a3 USING fts5(
192 x, y, tokenize = 'unicode61 remove_diacritics 3'
194 } {1 {error in tokenizer constructor}}
195 do_catchsql_test 6.4 {
196 CREATE VIRTUAL TABLE a3 USING fts5(
197 x, y, tokenize = 'unicode61 remove_diacritics 10'
199 } {1 {error in tokenizer constructor}}
201 #-------------------------------------------------------------------------
202 # Porter tokenizer with very large tokens.
204 set a [string repeat a 100]
205 set b [string repeat b 500]
206 set c [string repeat c 1000]
207 do_execsql_test 7.0 {
208 CREATE VIRTUAL TABLE e5 USING fts5(x, tokenize=porter);
209 INSERT INTO e5 VALUES($a || ' ' || $b);
210 INSERT INTO e5 VALUES($b || ' ' || $c);
211 INSERT INTO e5 VALUES($c || ' ' || $a);
214 do_execsql_test 7.1 {SELECT rowid FROM e5 WHERE e5 MATCH $a} { 1 3 }
215 do_execsql_test 7.2 {SELECT rowid FROM e5 WHERE e5 MATCH $b} { 1 2 }
216 do_execsql_test 7.3 {SELECT rowid FROM e5 WHERE e5 MATCH $c} { 2 3 }
218 #-------------------------------------------------------------------------
219 # Test the 'separators' option with the unicode61 tokenizer.
221 do_execsql_test 8.1 {
223 CREATE VIRTUAL TABLE e6 USING fts5(x,
224 tokenize="unicode61 separators ABCDEFGHIJKLMNOPQRSTUVWXYZ"
226 INSERT INTO e6 VALUES('theAquickBbrownCfoxDjumpedWoverXtheYlazyZdog');
227 CREATE VIRTUAL TABLE e7 USING fts5vocab(e6, 'row');
231 brown dog fox jumped lazy over quick the
234 do_execsql_test 8.2 [subst {
236 CREATE VIRTUAL TABLE e6 USING fts5(x,
237 tokenize="unicode61 separators '\u0E01\u0E02\u0E03\u0E04\u0E05\u0E06\u0E07'"
239 INSERT INTO e6 VALUES('the\u0E01quick\u0E01brown\u0E01fox\u0E01'
240 || 'jumped\u0E01over\u0E01the\u0E01lazy\u0E01dog'
242 INSERT INTO e6 VALUES('\u0E08\u0E07\u0E09');
243 CREATE VIRTUAL TABLE e7 USING fts5vocab(e6, 'row');
247 brown dog fox jumped lazy over quick the \u0E08 \u0E09
250 # Test that the porter tokenizer correctly passes arguments through to
251 # its parent tokenizer.
252 do_execsql_test 8.3 {
254 CREATE VIRTUAL TABLE e6 USING fts5(x,
255 tokenize="porter unicode61 separators ABCDEFGHIJKLMNOPQRSTUVWXYZ"
257 INSERT INTO e6 VALUES('theAquickBbrownCfoxDjumpedWoverXtheYlazyZdog');
258 CREATE VIRTUAL TABLE e7 USING fts5vocab(e6, 'row');
262 brown dog fox jump lazi over quick the
265 #-------------------------------------------------------------------------
266 # Check that the FTS5_TOKENIZE_PREFIX flag is passed to the tokenizer
270 proc tcl_create {args} { return "tcl_tokenize" }
271 sqlite3_fts5_create_tokenizer db tcl tcl_create
273 proc tcl_tokenize {tflags text} {
274 lappend ::flags $tflags
275 foreach {w iStart iEnd} [fts5_tokenize_split $text] {
276 sqlite3_fts5_token $w $iStart $iEnd
280 do_execsql_test 9.1.1 {
281 CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=tcl);
282 INSERT INTO t1 VALUES('abc');
283 INSERT INTO t1 VALUES('xyz');
285 do_test 9.1.2 { set ::flags } {document document}
288 do_execsql_test 9.2.1 { SELECT * FROM t1('abc'); } {abc}
289 do_test 9.2.2 { set ::flags } {query}
292 do_execsql_test 9.3.1 { SELECT * FROM t1('ab*'); } {abc}
293 do_test 9.3.2 { set ::flags } {prefixquery}
296 do_execsql_test 9.4.1 { SELECT * FROM t1('"abc xyz" *'); } {}
297 do_test 9.4.2 { set ::flags } {prefixquery}
300 do_execsql_test 9.5.1 { SELECT * FROM t1('"abc xyz*"'); } {}
301 do_test 9.5.2 { set ::flags } {query}