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 / fts5al.test

309 lines · 7910 bytes Raw Blame History
1# 2014 November 24
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# This file implements regression tests for SQLite library. The
12# focus of this script is testing the FTS5 module.
13#
14# Specifically, this function tests the %_config table.
15#
16
17source [file join [file dirname [info script]] fts5_common.tcl]
18set testprefix fts5al
19
20# If SQLITE_ENABLE_FTS5 is not defined, omit this file.
21ifcapable !fts5 {
22 finish_test
23 return
24}
25
26foreach_detail_mode $testprefix {
27
28do_execsql_test 1.1 {
29 CREATE VIRTUAL TABLE ft1 USING fts5(x, detail=%DETAIL%);
30 SELECT * FROM ft1_config;
31} {version 4}
32
33do_execsql_test 1.2 {
34 INSERT INTO ft1(ft1, rank) VALUES('pgsz', 32);
35 SELECT * FROM ft1_config;
36} {pgsz 32 version 4}
37
38do_execsql_test 1.3 {
39 INSERT INTO ft1(ft1, rank) VALUES('pgsz', 64);
40 SELECT * FROM ft1_config;
41} {pgsz 64 version 4}
42
43#--------------------------------------------------------------------------
44# Test the logic for parsing the rank() function definition.
45#
46foreach {tn defn} {
47 1 "fname()"
48 2 "fname(1)"
49 3 "fname(1,2)"
50 4 "fname(null,NULL,nUlL)"
51 5 " fname ( null , NULL , nUlL ) "
52 6 "fname('abc')"
53 7 "fname('a''bc')"
54 8 "fname('''abc')"
55 9 "fname('abc''')"
56
57 7 "fname( 'a''bc' )"
58 8 "fname('''abc' )"
59 9 "fname( 'abc''' )"
60
61 10 "fname(X'1234ab')"
62
63 11 "myfunc(1.2)"
64 12 "myfunc(-1.0)"
65 13 "myfunc(.01,'abc')"
66} {
67 do_execsql_test 2.1.$tn {
68 INSERT INTO ft1(ft1, rank) VALUES('rank', $defn);
69 }
70}
71
72foreach {tn defn} {
73 1 ""
74 2 "fname"
75 3 "fname(X'234ab')"
76 4 "myfunc(-1.,'abc')"
77} {
78 do_test 2.2.$tn {
79 catchsql { INSERT INTO ft1(ft1, rank) VALUES('rank', $defn) }
80 } {1 {SQL logic error}}
81}
82
83#-------------------------------------------------------------------------
84# Assorted tests of the tcl interface for creating extension functions.
85#
86
87do_execsql_test 3.1 {
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');
91}
92
93proc argtest {cmd args} { return $args }
94sqlite3_fts5_create_function db argtest argtest
95
96do_execsql_test 3.2.1 {
97 SELECT argtest(t1, 123) FROM t1 WHERE t1 MATCH 'q'
98} {123 123}
99
100do_execsql_test 3.2.2 {
101 SELECT argtest(t1, 123, 456) FROM t1 WHERE t1 MATCH 'q'
102} {{123 456} {123 456}}
103
104proc rowidtest {cmd} { $cmd xRowid }
105sqlite3_fts5_create_function db rowidtest rowidtest
106
107do_execsql_test 3.3.1 {
108 SELECT rowidtest(t1) FROM t1 WHERE t1 MATCH 'q'
109} {1 2}
110
111proc insttest {cmd} {
112 set res [list]
113 for {set i 0} {$i < [$cmd xInstCount]} {incr i} {
114 lappend res [$cmd xInst $i]
115 }
116 set res
117}
118sqlite3_fts5_create_function db insttest insttest
119
120do_execsql_test 3.4.1 {
121 SELECT insttest(t1) FROM t1 WHERE t1 MATCH 'q'
122} {
123 {{0 0 0}}
124 {{0 0 5}}
125}
126
127if {[detail_is_full]} {
128 do_execsql_test 3.4.2 {
129 SELECT insttest(t1) FROM t1 WHERE t1 MATCH 'r+e OR w'
130 } {
131 {{1 0 1}}
132 {{0 0 2} {1 0 4}}
133 }
134}
135
136proc coltest {cmd} {
137 list [$cmd xColumnSize 0] [$cmd xColumnText 0]
138}
139sqlite3_fts5_create_function db coltest coltest
140
141do_execsql_test 3.5.1 {
142 SELECT coltest(t1) FROM t1 WHERE t1 MATCH 'q'
143} {
144 {6 {q w e r t y}}
145 {6 {y t r e w q}}
146}
147
148#-------------------------------------------------------------------------
149# Tests for remapping the "rank" column.
150#
151# 4.1.*: Mapped to a function with no arguments.
152# 4.2.*: Mapped to a function with one or more arguments.
153#
154
155do_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');
167}
168
169proc firstinst {cmd} {
170 foreach {p c o} [$cmd xInst 0] {}
171 expr $c*100 + $o
172}
173sqlite3_fts5_create_function db firstinst firstinst
174
175do_execsql_test 4.1.1 {
176 SELECT rowid, firstinst(t2) FROM t2 WHERE t2 MATCH 'a' ORDER BY rowid ASC
177} {
178 1 0 2 4 3 6 5 103
179 6 9 7 0 9 102 10 8
180}
181
182do_execsql_test 4.1.2 {
183 SELECT rowid, rank FROM t2
184 WHERE t2 MATCH 'a' AND rank MATCH 'firstinst()'
185 ORDER BY rowid ASC
186} {
187 1 0 2 4 3 6 5 103
188 6 9 7 0 9 102 10 8
189}
190
191do_execsql_test 4.1.3 {
192 SELECT rowid, rank FROM t2
193 WHERE t2 MATCH 'a' AND rank MATCH 'firstinst()'
194 ORDER BY rank DESC
195} {
196 5 103 9 102 6 9 10 8 3 6 2 4 1 0 7 0
197}
198
199do_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
202} {
203 1 0 2 4 3 6 5 103
204 6 9 7 0 9 102 10 8
205}
206
207do_execsql_test 4.1.5 {
208 SELECT rowid, rank FROM t2 WHERE t2 MATCH 'a' ORDER BY rank DESC
209} {
210 5 103 9 102 6 9 10 8 3 6 2 4 1 0 7 0
211}
212
213do_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
216} {
217 5 103 9 102 6 9 10 8 3 6 2 4 1 0 7 0
218}
219
220proc rowidplus {cmd ival} {
221 expr [$cmd xRowid] + $ival
222}
223sqlite3_fts5_create_function db rowidplus rowidplus
224
225if {[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'
229 } {
230 10 110
231 }
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'
235 } {
236 10 121
237 }
238
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)'
242 } {
243 10 122
244 }
245}
246
247proc rowidmod {cmd imod} {
248 expr [$cmd xRowid] % $imod
249}
250sqlite3_fts5_create_function db rowidmod rowidmod
251do_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()');
259}
260
261do_execsql_test 4.3.2 {
262 SELECT * FROM t3
263 WHERE t3 MATCH 'a' AND rank MATCH 'rowidmod(4)'
264 ORDER BY rank ASC
265} {
266 {a four} {a one} {a five} {a two} {a three}
267}
268
269do_execsql_test 4.3.3 {
270 SELECT *, rank FROM t3
271 WHERE t3 MATCH 'a' AND rank MATCH 'rowidmod(3)'
272 ORDER BY rank ASC
273} {
274 {a three} 0 {a one} 1 {a four} 1 {a two} 2 {a five} 2
275}
276
277do_execsql_test 4.3.4 {
278 SELECT * FROM t3('a', 'rowidmod(4)') ORDER BY rank ASC;
279} {
280 {a four} {a one} {a five} {a two} {a three}
281}
282
283do_execsql_test 4.3.5 {
284 SELECT *, rank FROM t3('a', 'rowidmod(3)') ORDER BY rank ASC
285} {
286 {a three} 0 {a one} 1 {a four} 1 {a two} 2 {a five} 2
287}
288
289do_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}}
292do_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: }}
295
296# Check that the second and subsequent rank= constraints are ignored.
297#
298do_catchsql_test 4.3.3 {
299 SELECT *, rank FROM t3
300 WHERE t3 MATCH 'a' AND
301 rank MATCH 'nosuch()' AND
302 rank MATCH 'rowidmod(3)'
303 ORDER BY rank ASC
304} {1 {unable to use function MATCH in the requested context}}
305
306} ;# foreach_detail_mode
307
308
309finish_test