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 FTS5 module.
14 # Specifically, this function tests the %_config table.
17 source [file join [file dirname [info script]] fts5_common.tcl]
20 # If SQLITE_ENABLE_FTS5 is defined, omit this file.
26 foreach_detail_mode $testprefix {
29 CREATE VIRTUAL TABLE ft1 USING fts5(x, detail=%DETAIL%);
30 SELECT * FROM ft1_config;
34 INSERT INTO ft1(ft1, rank) VALUES('pgsz', 32);
35 SELECT * FROM ft1_config;
39 INSERT INTO ft1(ft1, rank) VALUES('pgsz', 64);
40 SELECT * FROM ft1_config;
43 #--------------------------------------------------------------------------
44 # Test the logic for parsing the rank() function definition.
50 4 "fname(null,NULL,nUlL)"
51 5 " fname ( null , NULL , nUlL ) "
65 13 "myfunc(.01,'abc')"
67 do_execsql_test 2.1.$tn {
68 INSERT INTO ft1(ft1, rank) VALUES('rank', $defn);
79 catchsql { INSERT INTO ft1(ft1, rank) VALUES('rank', $defn) }
80 } {1 {SQL logic error}}
83 #-------------------------------------------------------------------------
84 # Assorted tests of the tcl interface for creating extension functions.
88 CREATE VIRTUAL TABLE t1 USING fts5(x, detail=%DETAIL%);
89 INSERT INTO t1 VALUES('q w e r t y');
90 INSERT INTO t1 VALUES('y t r e w q');
93 proc argtest {cmd args} { return $args }
94 sqlite3_fts5_create_function db argtest argtest
96 do_execsql_test 3.2.1 {
97 SELECT argtest(t1, 123) FROM t1 WHERE t1 MATCH 'q'
100 do_execsql_test 3.2.2 {
101 SELECT argtest(t1, 123, 456) FROM t1 WHERE t1 MATCH 'q'
102 } {{123 456} {123 456}}
104 proc rowidtest {cmd} { $cmd xRowid }
105 sqlite3_fts5_create_function db rowidtest rowidtest
107 do_execsql_test 3.3.1 {
108 SELECT rowidtest(t1) FROM t1 WHERE t1 MATCH 'q'
111 proc insttest {cmd} {
113 for {set i 0} {$i < [$cmd xInstCount]} {incr i} {
114 lappend res [$cmd xInst $i]
118 sqlite3_fts5_create_function db insttest insttest
120 do_execsql_test 3.4.1 {
121 SELECT insttest(t1) FROM t1 WHERE t1 MATCH 'q'
127 if {[detail_is_full]} {
128 do_execsql_test 3.4.2 {
129 SELECT insttest(t1) FROM t1 WHERE t1 MATCH 'r+e OR w'
137 list [$cmd xColumnSize 0] [$cmd xColumnText 0]
139 sqlite3_fts5_create_function db coltest coltest
141 do_execsql_test 3.5.1 {
142 SELECT coltest(t1) FROM t1 WHERE t1 MATCH 'q'
148 #-------------------------------------------------------------------------
149 # Tests for remapping the "rank" column.
151 # 4.1.*: Mapped to a function with no arguments.
152 # 4.2.*: Mapped to a function with one or more arguments.
155 do_execsql_test 4.0 {
156 CREATE VIRTUAL TABLE t2 USING fts5(a, b, detail=%DETAIL%);
157 INSERT INTO t2 VALUES('a s h g s b j m r h', 's b p a d b b a o e');
158 INSERT INTO t2 VALUES('r h n t a g r d d i', 'l d n j r c f t o q');
159 INSERT INTO t2 VALUES('q k n i k c a a e m', 'c h n j p g s c i t');
160 INSERT INTO t2 VALUES('h j g t r e l s g s', 'k q k c i i c k n s');
161 INSERT INTO t2 VALUES('b l k h d n n n m i', 'p t i a r b t q o l');
162 INSERT INTO t2 VALUES('k r i l j b g i p a', 't q c h a i m g n l');
163 INSERT INTO t2 VALUES('a e c q n m o m d g', 'l c t g i s q g q e');
164 INSERT INTO t2 VALUES('b o j h f o g b p e', 'r t l h s b g i c p');
165 INSERT INTO t2 VALUES('s q k f q b j g h f', 'n m a o p e i e k t');
166 INSERT INTO t2 VALUES('o q g g q c o k a b', 'r t k p t f t h p c');
169 proc firstinst {cmd} {
170 foreach {p c o} [$cmd xInst 0] {}
173 sqlite3_fts5_create_function db firstinst firstinst
175 do_execsql_test 4.1.1 {
176 SELECT rowid, firstinst(t2) FROM t2 WHERE t2 MATCH 'a' ORDER BY rowid ASC
182 do_execsql_test 4.1.2 {
183 SELECT rowid, rank FROM t2
184 WHERE t2 MATCH 'a' AND rank MATCH 'firstinst()'
191 do_execsql_test 4.1.3 {
192 SELECT rowid, rank FROM t2
193 WHERE t2 MATCH 'a' AND rank MATCH 'firstinst()'
196 5 103 9 102 6 9 10 8 3 6 2 4 1 0 7 0
199 do_execsql_test 4.1.4 {
200 INSERT INTO t2(t2, rank) VALUES('rank', 'firstinst()');
201 SELECT rowid, rank FROM t2 WHERE t2 MATCH 'a' ORDER BY rowid ASC
207 do_execsql_test 4.1.5 {
208 SELECT rowid, rank FROM t2 WHERE t2 MATCH 'a' ORDER BY rank DESC
210 5 103 9 102 6 9 10 8 3 6 2 4 1 0 7 0
213 do_execsql_test 4.1.6 {
214 INSERT INTO t2(t2, rank) VALUES('rank', 'firstinst ( ) ');
215 SELECT rowid, rank FROM t2 WHERE t2 MATCH 'a' ORDER BY rank DESC
217 5 103 9 102 6 9 10 8 3 6 2 4 1 0 7 0
220 proc rowidplus {cmd ival} {
221 expr [$cmd xRowid] + $ival
223 sqlite3_fts5_create_function db rowidplus rowidplus
225 if {[detail_is_full]} {
226 do_execsql_test 4.2.1 {
227 INSERT INTO t2(t2, rank) VALUES('rank', 'rowidplus(100) ');
228 SELECT rowid, rank FROM t2 WHERE t2 MATCH 'o + q + g'
232 do_execsql_test 4.2.2 {
233 INSERT INTO t2(t2, rank) VALUES('rank', 'rowidplus(111) ');
234 SELECT rowid, rank FROM t2 WHERE t2 MATCH 'o + q + g'
239 do_execsql_test 4.2.3 {
240 SELECT rowid, rank FROM t2
241 WHERE t2 MATCH 'o + q + g' AND rank MATCH 'rowidplus(112)'
247 proc rowidmod {cmd imod} {
248 expr [$cmd xRowid] % $imod
250 sqlite3_fts5_create_function db rowidmod rowidmod
251 do_execsql_test 4.3.1 {
252 CREATE VIRTUAL TABLE t3 USING fts5(x, detail=%DETAIL%);
253 INSERT INTO t3 VALUES('a one');
254 INSERT INTO t3 VALUES('a two');
255 INSERT INTO t3 VALUES('a three');
256 INSERT INTO t3 VALUES('a four');
257 INSERT INTO t3 VALUES('a five');
258 INSERT INTO t3(t3, rank) VALUES('rank', 'bm25()');
261 do_execsql_test 4.3.2 {
263 WHERE t3 MATCH 'a' AND rank MATCH 'rowidmod(4)'
266 {a four} {a one} {a five} {a two} {a three}
269 do_execsql_test 4.3.3 {
270 SELECT *, rank FROM t3
271 WHERE t3 MATCH 'a' AND rank MATCH 'rowidmod(3)'
274 {a three} 0 {a one} 1 {a four} 1 {a two} 2 {a five} 2
277 do_execsql_test 4.3.4 {
278 SELECT * FROM t3('a', 'rowidmod(4)') ORDER BY rank ASC;
280 {a four} {a one} {a five} {a two} {a three}
283 do_execsql_test 4.3.5 {
284 SELECT *, rank FROM t3('a', 'rowidmod(3)') ORDER BY rank ASC
286 {a three} 0 {a one} 1 {a four} 1 {a two} 2 {a five} 2
289 do_catchsql_test 4.4.3 {
290 SELECT *, rank FROM t3 WHERE t3 MATCH 'a' AND rank MATCH 'xyz(3)'
291 } {1 {no such function: xyz}}
292 do_catchsql_test 4.4.4 {
293 SELECT *, rank FROM t3 WHERE t3 MATCH 'a' AND rank MATCH NULL
294 } {1 {parse error in rank function: }}
296 } ;# foreach_detail_mode