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 FTS3 module.
14 # $Id: fts3expr.test,v 1.9 2009/07/28 16:44:26 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # If SQLITE_ENABLE_FTS3 is defined, omit this file.
26 set sqlite_fts3_enable_parentheses 1
28 proc test_fts3expr {expr} {
29 db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')}
32 do_test fts3expr-1.0 {
35 do_test fts3expr-1.1 {
39 do_test fts3expr-1.2 {
40 test_fts3expr "ab AND cd"
41 } {AND {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
42 do_test fts3expr-1.2.1 {
44 } {AND {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
45 do_test fts3expr-1.3 {
46 test_fts3expr "ab OR cd"
47 } {OR {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
48 do_test fts3expr-1.4 {
49 test_fts3expr "ab NOT cd"
50 } {NOT {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
51 do_test fts3expr-1.5 {
52 test_fts3expr "ab NEAR cd"
53 } {NEAR/10 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
54 do_test fts3expr-1.6.1 {
55 test_fts3expr "ab NEAR/5 cd"
56 } {NEAR/5 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
57 do_test fts3expr-1.6.2 {
58 test_fts3expr "ab NEAR/87654321 cd"
59 } {NEAR/87654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
60 do_test fts3expr-1.6.3 {
61 test_fts3expr "ab NEAR/7654321 cd"
62 } {NEAR/7654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
63 do_test fts3expr-1.6.4 {
64 test_fts3expr "ab NEAR/654321 cd"
65 } {NEAR/654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
66 do_test fts3expr-1.6.5 {
67 test_fts3expr "ab NEAR/54321 cd"
68 } {NEAR/54321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
69 do_test fts3expr-1.6.6 {
70 test_fts3expr "ab NEAR/4321 cd"
71 } {NEAR/4321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
72 do_test fts3expr-1.6.7 {
73 test_fts3expr "ab NEAR/321 cd"
74 } {NEAR/321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
75 do_test fts3expr-1.6.8 {
76 test_fts3expr "ab NEAR/21 cd"
77 } {NEAR/21 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
79 do_test fts3expr-1.7 {
80 test_fts3expr {"one two three"}
81 } {PHRASE 3 0 one two three}
82 do_test fts3expr-1.8.1 {
83 test_fts3expr {zero "one two three" four}
84 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
85 do_test fts3expr-1.8.2 {
86 test_fts3expr {zero AND "one two three" four}
87 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
88 do_test fts3expr-1.8.3 {
89 test_fts3expr {zero "one two three" AND four}
90 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
91 do_test fts3expr-1.8.4 {
92 test_fts3expr {zero AND "one two three" AND four}
93 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
94 do_test fts3expr-1.9.1 {
95 test_fts3expr {"one* two three"}
96 } {PHRASE 3 0 one+ two three}
97 do_test fts3expr-1.9.2 {
98 test_fts3expr {"one two* three"}
99 } {PHRASE 3 0 one two+ three}
100 do_test fts3expr-1.9.3 {
101 test_fts3expr {"one* two* three"}
102 } {PHRASE 3 0 one+ two+ three}
103 do_test fts3expr-1.9.4 {
104 test_fts3expr {"one two three*"}
105 } {PHRASE 3 0 one two three+}
106 do_test fts3expr-1.9.5 {
107 test_fts3expr {"one* two three*"}
108 } {PHRASE 3 0 one+ two three+}
109 do_test fts3expr-1.9.6 {
110 test_fts3expr {"one two* three*"}
111 } {PHRASE 3 0 one two+ three+}
112 do_test fts3expr-1.9.7 {
113 test_fts3expr {"one* two* three*"}
114 } {PHRASE 3 0 one+ two+ three+}
116 do_test fts3expr-1.10 {
117 test_fts3expr {one* two}
118 } {AND {PHRASE 3 0 one+} {PHRASE 3 0 two}}
119 do_test fts3expr-1.11 {
120 test_fts3expr {one two*}
121 } {AND {PHRASE 3 0 one} {PHRASE 3 0 two+}}
123 do_test fts3expr-1.14 {
124 test_fts3expr {a:one two}
125 } {AND {PHRASE 0 0 one} {PHRASE 3 0 two}}
126 do_test fts3expr-1.15.1 {
127 test_fts3expr {one b:two}
128 } {AND {PHRASE 3 0 one} {PHRASE 1 0 two}}
129 do_test fts3expr-1.15.2 {
130 test_fts3expr {one B:two}
131 } {AND {PHRASE 3 0 one} {PHRASE 1 0 two}}
133 do_test fts3expr-1.16 {
134 test_fts3expr {one AND two AND three AND four AND five}
138 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
145 do_test fts3expr-1.17 {
146 test_fts3expr {(one AND two) AND ((three AND four) AND five)}
148 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
150 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \
154 do_test fts3expr-1.18 {
155 test_fts3expr {(one AND two) OR ((three AND four) AND five)}
157 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
159 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \
163 do_test fts3expr-1.19 {
164 test_fts3expr {(one AND two) AND ((three AND four) OR five)}
166 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
168 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \
172 do_test fts3expr-1.20 {
173 test_fts3expr {(one OR two) AND ((three OR four) AND five)}
175 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
177 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
181 do_test fts3expr-1.21 {
182 test_fts3expr {(one OR two) AND ((three NOT four) AND five)}
184 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
186 [list NOT {PHRASE 3 0 three} {PHRASE 3 0 four}] \
190 do_test fts3expr-1.22 {
191 test_fts3expr {(one OR two) NOT ((three OR four) AND five)}
193 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
195 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
199 do_test fts3expr-1.23 {
200 test_fts3expr {(((((one OR two))))) NOT (((((three OR four))) AND five))}
202 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
204 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
208 do_test fts3expr-1.24 {
209 test_fts3expr {one NEAR two}
210 } [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}]
211 do_test fts3expr-1.25 {
212 test_fts3expr {(one NEAR two)}
213 } [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}]
214 do_test fts3expr-1.26 {
215 test_fts3expr {((((((one NEAR two))))))}
216 } [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}]
217 do_test fts3expr-1.27 {
218 test_fts3expr {(one NEAR two) OR ((three OR four) AND five)}
220 [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}] \
222 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
226 do_test fts3expr-1.28 {
227 test_fts3expr {(one NEAR/321 two) OR ((three OR four) AND five)}
229 [list NEAR/321 {PHRASE 3 0 one} {PHRASE 3 0 two}] \
231 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
236 proc strip_phrase_data {L} {
237 if {[lindex $L 0] eq "PHRASE"} {
238 return [lrange $L 3 end]
242 [strip_phrase_data [lindex $L 1]] \
243 [strip_phrase_data [lindex $L 2]] \
246 proc test_fts3expr2 {expr} {
248 db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')}
251 do_test fts3expr-2.1 {
252 test_fts3expr2 "ab OR cd AND ef"
253 } {OR ab {AND cd ef}}
254 do_test fts3expr-2.2 {
255 test_fts3expr2 "cd AND ef OR ab"
256 } {OR {AND cd ef} ab}
257 do_test fts3expr-2.3 {
258 test_fts3expr2 "ab AND cd AND ef OR gh"
259 } {OR {AND {AND ab cd} ef} gh}
260 do_test fts3expr-2.4 {
261 test_fts3expr2 "ab AND cd OR ef AND gh"
262 } {OR {AND ab cd} {AND ef gh}}
263 do_test fts3expr-2.5 {
264 test_fts3expr2 "ab cd"
267 do_test fts3expr-3.1 {
268 test_fts3expr2 "(ab OR cd) AND ef"
269 } {AND {OR ab cd} ef}
270 do_test fts3expr-3.2 {
271 test_fts3expr2 "ef AND (ab OR cd)"
272 } {AND ef {OR ab cd}}
273 do_test fts3expr-3.3 {
274 test_fts3expr2 "(ab OR cd)"
276 do_test fts3expr-3.4 {
277 test_fts3expr2 "(((ab OR cd)))"
280 do_test fts3expr-3.5 {
281 test_fts3expr2 "one AND (two NEAR three)"
282 } {AND one {NEAR/10 two three}}
283 do_test fts3expr-3.6 {
284 test_fts3expr2 "one (two NEAR three)"
285 } {AND one {NEAR/10 two three}}
286 do_test fts3expr-3.7 {
287 test_fts3expr2 "(two NEAR three) one"
288 } {AND {NEAR/10 two three} one}
289 do_test fts3expr-3.8 {
290 test_fts3expr2 "(two NEAR three) AND one"
291 } {AND {NEAR/10 two three} one}
292 do_test fts3expr-3.9 {
293 test_fts3expr2 "(two NEAR three) (four five)"
294 } {AND {NEAR/10 two three} {AND four five}}
295 do_test fts3expr-3.10 {
296 test_fts3expr2 "(two NEAR three) AND (four five)"
297 } {AND {NEAR/10 two three} {AND four five}}
298 do_test fts3expr-3.11 {
299 test_fts3expr2 "(two NEAR three) (four NEAR five)"
300 } {AND {NEAR/10 two three} {NEAR/10 four five}}
301 do_test fts3expr-3.12 {
302 test_fts3expr2 "(two NEAR three) OR (four NEAR five)"
303 } {OR {NEAR/10 two three} {NEAR/10 four five}}
305 do_test fts3expr-3.13 {
306 test_fts3expr2 "(two NEAR/1a three)"
307 } {AND {AND {AND two near} 1a} three}
309 do_test fts3expr-3.14 {
310 test_fts3expr2 "(two NEAR// three)"
311 } {AND {AND two near} three}
312 do_test fts3expr-3.15 {
313 test_fts3expr2 "(two NEAR/: three)"
314 } {AND {AND two near} three}
316 do_test fts3expr-3.16 {
317 test_fts3expr2 "(two NEAR three)OR(four NEAR five)"
318 } {OR {NEAR/10 two three} {NEAR/10 four five}}
319 do_test fts3expr-3.17 {
320 test_fts3expr2 "(two NEAR three)OR\"four five\""
321 } {OR {NEAR/10 two three} {four five}}
322 do_test fts3expr-3.18 {
323 test_fts3expr2 "one \u0080wo"
328 #------------------------------------------------------------------------
329 # The following tests, fts3expr-4.*, test the parsers response to syntax
330 # errors in query expressions. This is done using a real fts3 table and
331 # MATCH clauses, not the parser test interface.
333 do_test fts3expr-4.1 {
334 execsql { CREATE VIRTUAL TABLE t1 USING fts3(a, b, c) }
337 # Mismatched parenthesis:
338 do_test fts3expr-4.2.1 {
339 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world))' }
340 } {1 {malformed MATCH expression: [example AND (hello OR world))]}}
341 do_test fts3expr-4.2.2 {
342 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world' }
343 } {1 {malformed MATCH expression: [example AND (hello OR world]}}
344 do_test fts3expr-4.2.3 {
345 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello' }
346 } {1 {malformed MATCH expression: [(hello]}}
347 do_test fts3expr-4.2.4 {
348 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(' }
349 } {1 {malformed MATCH expression: [(]}}
350 do_test fts3expr-4.2.5 {
351 catchsql { SELECT * FROM t1 WHERE t1 MATCH ')' }
352 } {1 {malformed MATCH expression: [)]}}
354 do_test fts3expr-4.2.6 {
355 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example (hello world' }
356 } {1 {malformed MATCH expression: [example (hello world]}}
358 # Unterminated quotation marks:
359 do_test fts3expr-4.3.1 {
360 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR "hello world' }
361 } {1 {malformed MATCH expression: [example OR "hello world]}}
362 do_test fts3expr-4.3.2 {
363 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR hello world"' }
364 } {1 {malformed MATCH expression: [example OR hello world"]}}
366 # Binary operators without the required operands.
367 do_test fts3expr-4.4.1 {
368 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'OR hello world' }
369 } {1 {malformed MATCH expression: [OR hello world]}}
370 do_test fts3expr-4.4.2 {
371 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'hello world OR' }
372 } {1 {malformed MATCH expression: [hello world OR]}}
373 do_test fts3expr-4.4.3 {
374 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (hello world OR) two' }
375 } {1 {malformed MATCH expression: [one (hello world OR) two]}}
376 do_test fts3expr-4.4.4 {
377 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (OR hello world) two' }
378 } {1 {malformed MATCH expression: [one (OR hello world) two]}}
380 # NEAR operators with something other than phrases as arguments.
381 do_test fts3expr-4.5.1 {
382 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello OR world) NEAR one' }
383 } {1 {malformed MATCH expression: [(hello OR world) NEAR one]}}
384 do_test fts3expr-4.5.2 {
385 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one NEAR (hello OR world)' }
386 } {1 {malformed MATCH expression: [one NEAR (hello OR world)]}}
388 #------------------------------------------------------------------------
389 # The following OOM tests are designed to cover cases in fts3_expr.c.
391 source $testdir/malloc_common.tcl
392 do_malloc_test fts3expr-malloc-1 -sqlbody {
393 SELECT fts3_exprtest('simple', 'a b c "d e f"', 'a', 'b', 'c')
395 do_malloc_test fts3expr-malloc-2 -tclprep {
396 set sqlite_fts3_enable_parentheses 0
398 SELECT fts3_exprtest('simple', 'a -b', 'a', 'b', 'c')
400 set sqlite_fts3_enable_parentheses 1
403 #------------------------------------------------------------------------
404 # The following tests are not very important. They cover error handling
405 # cases in the test code, which makes test coverage easier to measure.
407 do_test fts3expr-5.1 {
408 catchsql { SELECT fts3_exprtest('simple', 'a b') }
409 } {1 {Usage: fts3_exprtest(tokenizer, expr, col1, ...}}
410 do_test fts3expr-5.2 {
411 catchsql { SELECT fts3_exprtest('doesnotexist', 'a b', 'c') }
412 } {1 {No such tokenizer module}}
413 do_test fts3expr-5.3 {
414 catchsql { SELECT fts3_exprtest('simple', 'a b OR', 'c') }
415 } {1 {Error parsing expression}}
417 #------------------------------------------------------------------------
418 # The next set of tests verifies that things actually work as they are
419 # supposed to when using the new syntax.
421 do_test fts3expr-6.1 {
423 CREATE VIRTUAL TABLE t1 USING fts3(a);
425 for {set ii 1} {$ii < 32} {incr ii} {
427 if {$ii & 1} { lappend v one }
428 if {$ii & 2} { lappend v two }
429 if {$ii & 4} { lappend v three }
430 if {$ii & 8} { lappend v four }
431 if {$ii & 16} { lappend v five }
432 execsql { INSERT INTO t1 VALUES($v) }
435 execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'five four one' ORDER BY rowid}
438 foreach {id expr res} {
440 2 "five four NOT one" {24 26 28 30}
442 3 "five AND four OR one"
443 {1 3 5 7 9 11 13 15 17 19 21 23 24 25 26 27 28 29 30 31}
445 4 "five AND (four OR one)" {17 19 21 23 24 25 26 27 28 29 30 31}
447 5 "five NOT (four OR one)" {16 18 20 22}
449 6 "(five NOT (four OR one)) OR (five AND (four OR one))"
450 {16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
452 7 "(five OR one) AND two AND three" {7 15 22 23 30 31}
454 8 "five OR one AND two AND three"
455 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
457 9 "five OR one two three"
458 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
460 10 "five OR \"one two three\""
461 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
463 11 "one two OR four five NOT three" {3 7 11 15 19 23 24 25 26 27 31}
465 12 "(one two OR four five) NOT three" {3 11 19 24 25 26 27}
467 13 "((((((one two OR four five)))))) NOT three" {3 11 19 24 25 26 27}
470 do_test fts3expr-6.1.$id {
471 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid }
475 set sqlite_fts3_enable_parentheses 0
476 foreach {id expr res} {
477 1 "one -two three" {5 13 21 29}
478 2 "-two one three" {5 13 21 29}
479 3 "one three -two" {5 13 21 29}
480 4 "-one -two three" {4 12 20 28}
481 5 "three -one -two" {4 12 20 28}
482 6 "-one three -two" {4 12 20 28}
484 do_test fts3expr-6.2.$id {
485 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid }
488 set sqlite_fts3_enable_parentheses 1
490 do_test fts3expr-7.1 {
492 CREATE VIRTUAL TABLE test USING fts3 (keyword);
493 INSERT INTO test VALUES ('abc');
494 SELECT * FROM test WHERE keyword MATCH '""';
499 do_test fts3expr-8.0 { test_fts3expr "(blah)" } {PHRASE 3 0 blah}
500 do_test fts3expr-8.1 { test_fts3expr "(blah.)" } {PHRASE 3 0 blah}
501 do_test fts3expr-8.2 { test_fts3expr "(blah,)" } {PHRASE 3 0 blah}
502 do_test fts3expr-8.3 { test_fts3expr "(blah!)" } {PHRASE 3 0 blah}
503 do_test fts3expr-8.4 { test_fts3expr "(blah-)" } {PHRASE 3 0 blah}
505 do_test fts3expr-8.5 { test_fts3expr "((blah.))" } {PHRASE 3 0 blah}
506 do_test fts3expr-8.6 { test_fts3expr "(((blah,)))" } {PHRASE 3 0 blah}
507 do_test fts3expr-8.7 { test_fts3expr "((((blah!))))" } {PHRASE 3 0 blah}
509 do_test fts3expr-8.8 { test_fts3expr "(,(blah-),)" } {PHRASE 3 0 blah}
511 set sqlite_fts3_enable_parentheses 0
513 do_test fts3expr-9.1 {
514 test_fts3expr "f (e NEAR/2 a)"
515 } {AND {PHRASE 3 0 f} {NEAR/2 {PHRASE 3 0 e} {PHRASE 3 0 a}}}