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 auxiliary function APIs.
15 source [file join [file dirname [info script]] fts5_common.tcl]
16 set testprefix fts5aux
18 # If SQLITE_ENABLE_FTS5 is defined, omit this file.
27 sqlite3_fts5_create_function db inst inst
29 proc colsize {cmd i} {
32 sqlite3_fts5_create_function db colsize colsize
34 proc totalsize {cmd i} {
35 $cmd xColumnTotalSize $i
37 sqlite3_fts5_create_function db totalsize totalsize
40 CREATE VIRTUAL TABLE f1 USING fts5(a, b);
41 INSERT INTO f1 VALUES('one two', 'two one zero');
42 INSERT INTO f1 VALUES('one one', 'one one one');
45 do_catchsql_test 1.1 {
46 SELECT inst(f1, -1) FROM f1 WHERE f1 MATCH 'two';
48 do_catchsql_test 1.2 {
49 SELECT inst(f1, 0) FROM f1 WHERE f1 MATCH 'two';
51 do_catchsql_test 1.3 {
52 SELECT inst(f1, 1) FROM f1 WHERE f1 MATCH 'two';
54 do_catchsql_test 1.4 {
55 SELECT inst(f1, 2) FROM f1 WHERE f1 MATCH 'two';
58 do_catchsql_test 2.1 {
59 SELECT colsize(f1, 2) FROM f1 WHERE f1 MATCH 'two';
62 SELECT colsize(f1, 0), colsize(f1, 1) FROM f1 WHERE f1 MATCH 'zero';
65 SELECT colsize(f1, -1) FROM f1 WHERE f1 MATCH 'zero';
68 do_execsql_test 2.4.1 {
69 SELECT totalsize(f1, -1) FROM f1 WHERE f1 MATCH 'zero';
71 do_execsql_test 2.4.2 {
72 SELECT totalsize(f1, 0) FROM f1 WHERE f1 MATCH 'zero';
74 do_execsql_test 2.4.3 {
75 SELECT totalsize(f1, 1) FROM f1 WHERE f1 MATCH 'zero';
77 do_catchsql_test 2.4.4 {
78 SELECT totalsize(f1, 2) FROM f1 WHERE f1 MATCH 'zero';
81 #-------------------------------------------------------------------------
82 # Test the xSet and xGetAuxdata APIs with a NULL destructor.
84 proc prevrowid {add cmd} {
85 set res [$cmd xGetAuxdataInt 0]
87 $cmd xSetAuxdataInt $r
88 return [expr $res + $add]
90 sqlite3_fts5_create_function db prevrowid [list prevrowid 0]
91 sqlite3_fts5_create_function db prevrowid1 [list prevrowid 1]
94 CREATE VIRTUAL TABLE e5 USING fts5(x);
95 INSERT INTO e5 VALUES('a b c');
96 INSERT INTO e5 VALUES('d e f');
97 INSERT INTO e5 VALUES('a b c');
98 INSERT INTO e5 VALUES('d e f');
99 INSERT INTO e5 VALUES('a b c');
102 do_execsql_test 3.1 {
103 SELECT prevrowid(e5) || '+' || rowid FROM e5 WHERE e5 MATCH 'c'
106 do_execsql_test 3.2 {
107 SELECT prevrowid(e5) || '+' || prevrowid1(e5) || '+' || rowid
108 FROM e5 WHERE e5 MATCH 'e'
111 #-------------------------------------------------------------------------
112 # Test that if the xQueryPhrase callback returns other than SQLITE_OK,
113 # the query is abandoned. And that if it returns an error code other than
114 # SQLITE_DONE, the error is propagated back to the caller.
116 do_execsql_test 4.0 {
117 CREATE VIRTUAL TABLE e7 USING fts5(x);
118 INSERT INTO e7 VALUES('a x a');
119 INSERT INTO e7 VALUES('b x b');
120 INSERT INTO e7 VALUES('c x c');
121 INSERT INTO e7 VALUES('d x d');
122 INSERT INTO e7 VALUES('e x e');
125 proc xCallback {rowid code cmd} {
128 if {$r==$rowid} { return $code }
132 proc phrasequery {cmd code} {
134 $cmd xQueryPhrase 1 [list xCallback [$cmd xRowid] $code]
138 sqlite3_fts5_create_function db phrasequery phrasequery
140 do_execsql_test 4.1 {
141 SELECT phrasequery(e7, 'SQLITE_OK') FROM e7 WHERE e7 MATCH 'c x'
144 do_execsql_test 4.2 {
145 SELECT phrasequery(e7, 'SQLITE_DONE') FROM e7 WHERE e7 MATCH 'c x'
148 do_catchsql_test 4.3 {
149 SELECT phrasequery(e7, 'SQLITE_ERROR') FROM e7 WHERE e7 MATCH 'c x'
152 #-------------------------------------------------------------------------
153 # Auxiliary function calls with many cursors in the global cursor list.
155 do_execsql_test 5.0 {
156 CREATE VIRTUAL TABLE e9 USING fts5(y);
157 INSERT INTO e9(rowid, y) VALUES(1, 'i iii');
158 INSERT INTO e9(rowid, y) VALUES(2, 'ii iv');
159 INSERT INTO e9(rowid, y) VALUES(3, 'ii');
160 INSERT INTO e9(rowid, y) VALUES(4, 'i iv');
161 INSERT INTO e9(rowid, y) VALUES(5, 'iii');
164 proc my_rowid {cmd} { $cmd xRowid }
165 sqlite3_fts5_create_function db my_rowid my_rowid
173 set sql "SELECT my_rowid(e9) FROM e9 WHERE e9 MATCH '$q'"
174 set $var [sqlite3_prepare db $sql -1 dummy]
177 do_test 5.1.1 { sqlite3_step $s1 ; sqlite3_column_int $s1 0 } 1
178 do_test 5.1.2 { sqlite3_step $s2 ; sqlite3_column_int $s2 0 } 2
179 do_test 5.1.3 { sqlite3_step $s3 ; sqlite3_column_int $s3 0 } 1
180 do_test 5.1.4 { sqlite3_step $s4 ; sqlite3_column_int $s4 0 } 2
182 do_test 5.2.1 { sqlite3_step $s1 ; sqlite3_column_int $s1 0 } 4
183 do_test 5.2.2 { sqlite3_step $s2 ; sqlite3_column_int $s2 0 } 3
184 do_test 5.2.3 { sqlite3_step $s3 ; sqlite3_column_int $s3 0 } 5
185 do_test 5.2.4 { sqlite3_step $s4 ; sqlite3_column_int $s4 0 } 4
192 #-------------------------------------------------------------------------
193 # Passing an invalid first argument to an auxiliary function is detected.
195 do_execsql_test 6.0 {
196 CREATE VIRTUAL TABLE e11 USING fts5(y, z);
197 INSERT INTO e11(rowid, y, z) VALUES(1, 'a b', 45);
198 INSERT INTO e11(rowid, y, z) VALUES(2, 'b c', 46);
201 do_catchsql_test 6.1 {
202 SELECT my_rowid(z) FROM e11 WHERE e11 MATCH 'b'
203 } {1 {no such cursor: 45}}
205 do_catchsql_test 6.2 {
206 SELECT my_rowid(y) FROM e11 WHERE e11 MATCH 'b'
207 } {1 {no such cursor: 0}}
209 #-------------------------------------------------------------------------
210 # Test passing an out-of-range phrase number to xPhraseSize (should
213 proc my_phrasesize {cmd iPhrase} { $cmd xPhraseSize $iPhrase }
214 sqlite3_fts5_create_function db my_phrasesize my_phrasesize
216 do_execsql_test 7.1 {
217 CREATE VIRTUAL TABLE t1 USING fts5(a);
218 INSERT INTO t1 VALUES('a b c');
220 do_execsql_test 7.2 {
222 my_phrasesize(t1, -1),
223 my_phrasesize(t1, 0),
224 my_phrasesize(t1, 1),
226 FROM t1 WHERE t1 MATCH 'a OR b+c'
229 #-------------------------------------------------------------------------
231 do_execsql_test 8.0 {
232 CREATE VIRTUAL TABLE x1 USING fts5(a);
235 foreach {tn lRow res} {
236 4 {"a a a" "b" "a d"} {"[a] [a] [a]" "[a] d"}
237 1 {"b d" "a b"} {"[b] [d]" "[a] b"}
238 2 {"d b" "a d"} {"[d] [b]" "[a] d"}
239 3 {"a a d"} {"[a] [a] d"}
241 execsql { DELETE FROM x1 }
242 foreach row $lRow { execsql { INSERT INTO x1 VALUES($row) } }
243 do_execsql_test 8.$tn {
244 SELECT highlight(x1, 0, '[', ']') FROM x1 WHERE x1 MATCH 'a OR (b AND d)';
248 #-------------------------------------------------------------------------
249 # Test the built-in bm25() demo.
252 do_execsql_test 9.1 {
253 CREATE VIRTUAL TABLE t1 USING fts5(a, b);
254 INSERT INTO t1 VALUES('a', NULL); -- 1
255 INSERT INTO t1 VALUES('a', NULL); -- 2
256 INSERT INTO t1 VALUES('a', NULL); -- 3
257 INSERT INTO t1 VALUES('a', NULL); -- 4
258 INSERT INTO t1 VALUES('a', NULL); -- 5
259 INSERT INTO t1 VALUES('a', NULL); -- 6
260 INSERT INTO t1 VALUES('a', NULL); -- 7
261 INSERT INTO t1 VALUES('a', NULL); -- 8
262 INSERT INTO t1 VALUES(NULL, 'a a b'); -- 9
263 INSERT INTO t1 VALUES(NULL, 'b b a'); -- 10
266 do_execsql_test 9.2 {
267 SELECT rowid FROM t1('a AND b') ORDER BY rank;
272 do_execsql_test 9.3 {
273 SELECT rowid FROM t1('b:a AND b:b') ORDER BY rank;
278 #-------------------------------------------------------------------------
279 # Test that aux. functions may not be used in aggregate queries.
282 do_execsql_test 10.0 {
283 CREATE VIRTUAL TABLE t1 USING fts5(x, y, z);
284 INSERT INTO t1 VALUES('a', 'one two', 1);
285 INSERT INTO t1 VALUES('b', 'two three', 2);
286 INSERT INTO t1 VALUES('c', 'three four', 1);
287 INSERT INTO t1 VALUES('d', 'four five', 2);
288 INSERT INTO t1 VALUES('e', 'five six', 1);
289 INSERT INTO t1 VALUES('f', 'six seven', 2);
292 proc firstcol {cmd} { $cmd xColumnText 0 }
293 sqlite3_fts5_create_function db firstcol firstcol
295 do_execsql_test 10.1.1 {
296 SELECT firstcol(t1) FROM t1
298 do_execsql_test 10.1.2 {
299 SELECT group_concat(x, '.') FROM t1
302 do_catchsql_test 10.1.3 {
303 SELECT group_concat(firstcol(t1), '.') FROM t1
304 } {1 {unable to use function firstcol in the requested context}}
306 do_catchsql_test 10.1.4 {
307 SELECT group_concat(firstcol(t1), '.') FROM t1 GROUP BY rowid
308 } {1 {unable to use function firstcol in the requested context}}
310 #-------------------------------------------------------------------------
311 # Test that xInstCount() works from within an xPhraseQuery() callback.
315 proc xCallback {cmd} {
316 incr ::hitcount [$cmd xInstCount]
319 proc fts5_hitcount {cmd} {
321 $cmd xQueryPhrase 0 xCallback
324 sqlite3_fts5_create_function db fts5_hitcount fts5_hitcount
326 do_execsql_test 11.1 {
327 CREATE VIRTUAL TABLE x1 USING fts5(z);
328 INSERT INTO x1 VALUES('one two three');
329 INSERT INTO x1 VALUES('one two one three one');
330 INSERT INTO x1 VALUES('one two three');
333 do_execsql_test 11.2 {
334 SELECT fts5_hitcount(x1) FROM x1('one') LIMIT 1;
337 #-------------------------------------------------------------------------
338 # Test that xColumnText returns SQLITE_RANGE when it should.
341 fts5_aux_test_functions db
342 do_execsql_test 12.0 {
343 CREATE VIRTUAL TABLE t1 USING fts5(a, b, c);
344 INSERT INTO t1 VALUES('one', 'two', 'three');
345 INSERT INTO t1 VALUES('one', 'one', 'one');
346 INSERT INTO t1 VALUES('two', 'two', 'two');
347 INSERT INTO t1 VALUES('three', 'three', 'three');
350 do_catchsql_test 12.1.1 {
351 SELECT fts5_columntext(t1, -1) FROM t1('two');
353 do_catchsql_test 12.1.2 {
354 SELECT fts5_columntext(t1, 3) FROM t1('two');
356 do_catchsql_test 12.1.2 {
357 SELECT fts5_columntext(t1, 1) FROM t1('one AND two');
360 do_catchsql_test 12.2.1 {
361 SELECT fts5_queryphrase(t1, -1) FROM t1('one AND two');
363 do_catchsql_test 12.2.2 {
364 SELECT fts5_queryphrase(t1, 2) FROM t1('one AND two');
366 do_catchsql_test 12.2.3 {
367 SELECT fts5_queryphrase(t1, 1) FROM t1('one AND two');
370 do_catchsql_test 12.3.1 {
371 SELECT fts5_collist(t1, -1) FROM t1('one AND two');
373 do_catchsql_test 12.3.2 {
374 SELECT fts5_collist(t1, 2) FROM t1('one AND two');
376 do_catchsql_test 12.3.3 {
377 SELECT fts5_collist(t1, 1) FROM t1('one AND two');
380 #-------------------------------------------------------------------------
382 do_execsql_test 13.1 {
383 CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=ascii);
384 INSERT INTO t1 VALUES('a b c'), ('d e f');
385 PRAGMA integrity_check;
388 do_catchsql_test 13.2 {
389 SELECT highlight(t1, 0, '[', ']') FROM t1
390 } {0 {{a b c} {d e f}}}
392 do_execsql_test 13.3 {
393 PRAGMA writable_schema = 1;
394 UPDATE sqlite_schema SET sql = 'CREATE VIRTUAL TABLE t1 USING fts5(a, tokenize=blah)'
400 do_catchsql_test 13.4 {
401 SELECT highlight(t1, 0, '[', ']') FROM t1
402 } {1 {no such tokenizer: blah}}