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')}
31 do_test fts3expr-1.0 {
34 do_test fts3expr-1.1 {
38 do_test fts3expr-1.2 {
39 test_fts3expr "ab AND cd"
40 } {AND {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
41 do_test fts3expr-1.2.1 {
43 } {AND {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
44 do_test fts3expr-1.3 {
45 test_fts3expr "ab OR cd"
46 } {OR {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
47 do_test fts3expr-1.4 {
48 test_fts3expr "ab NOT cd"
49 } {NOT {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
50 do_test fts3expr-1.5 {
51 test_fts3expr "ab NEAR cd"
52 } {NEAR/10 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
53 do_test fts3expr-1.6.1 {
54 test_fts3expr "ab NEAR/5 cd"
55 } {NEAR/5 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
56 do_test fts3expr-1.6.2 {
57 test_fts3expr "ab NEAR/87654321 cd"
58 } {NEAR/87654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
59 do_test fts3expr-1.6.3 {
60 test_fts3expr "ab NEAR/7654321 cd"
61 } {NEAR/7654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
62 do_test fts3expr-1.6.4 {
63 test_fts3expr "ab NEAR/654321 cd"
64 } {NEAR/654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
65 do_test fts3expr-1.6.5 {
66 test_fts3expr "ab NEAR/54321 cd"
67 } {NEAR/54321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
68 do_test fts3expr-1.6.6 {
69 test_fts3expr "ab NEAR/4321 cd"
70 } {NEAR/4321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
71 do_test fts3expr-1.6.7 {
72 test_fts3expr "ab NEAR/321 cd"
73 } {NEAR/321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
74 do_test fts3expr-1.6.8 {
75 test_fts3expr "ab NEAR/21 cd"
76 } {NEAR/21 {PHRASE 3 0 ab} {PHRASE 3 0 cd}}
78 do_test fts3expr-1.7 {
79 test_fts3expr {"one two three"}
80 } {PHRASE 3 0 one two three}
81 do_test fts3expr-1.8.1 {
82 test_fts3expr {zero "one two three" four}
83 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
84 do_test fts3expr-1.8.2 {
85 test_fts3expr {zero AND "one two three" four}
86 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
87 do_test fts3expr-1.8.3 {
88 test_fts3expr {zero "one two three" AND four}
89 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
90 do_test fts3expr-1.8.4 {
91 test_fts3expr {zero AND "one two three" AND four}
92 } {AND {AND {PHRASE 3 0 zero} {PHRASE 3 0 one two three}} {PHRASE 3 0 four}}
93 do_test fts3expr-1.9.1 {
94 test_fts3expr {"one* two three"}
95 } {PHRASE 3 0 one+ two three}
96 do_test fts3expr-1.9.2 {
97 test_fts3expr {"one two* three"}
98 } {PHRASE 3 0 one two+ three}
99 do_test fts3expr-1.9.3 {
100 test_fts3expr {"one* two* three"}
101 } {PHRASE 3 0 one+ two+ three}
102 do_test fts3expr-1.9.4 {
103 test_fts3expr {"one two three*"}
104 } {PHRASE 3 0 one two three+}
105 do_test fts3expr-1.9.5 {
106 test_fts3expr {"one* two three*"}
107 } {PHRASE 3 0 one+ two three+}
108 do_test fts3expr-1.9.6 {
109 test_fts3expr {"one two* three*"}
110 } {PHRASE 3 0 one two+ three+}
111 do_test fts3expr-1.9.7 {
112 test_fts3expr {"one* two* three*"}
113 } {PHRASE 3 0 one+ two+ three+}
115 do_test fts3expr-1.10 {
116 test_fts3expr {one* two}
117 } {AND {PHRASE 3 0 one+} {PHRASE 3 0 two}}
118 do_test fts3expr-1.11 {
119 test_fts3expr {one two*}
120 } {AND {PHRASE 3 0 one} {PHRASE 3 0 two+}}
122 do_test fts3expr-1.14 {
123 test_fts3expr {a:one two}
124 } {AND {PHRASE 0 0 one} {PHRASE 3 0 two}}
125 do_test fts3expr-1.15.1 {
126 test_fts3expr {one b:two}
127 } {AND {PHRASE 3 0 one} {PHRASE 1 0 two}}
128 do_test fts3expr-1.15.2 {
129 test_fts3expr {one B:two}
130 } {AND {PHRASE 3 0 one} {PHRASE 1 0 two}}
132 do_test fts3expr-1.16 {
133 test_fts3expr {one AND two AND three AND four AND five}
137 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
144 do_test fts3expr-1.17 {
145 test_fts3expr {(one AND two) AND ((three AND four) AND five)}
147 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
149 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \
153 do_test fts3expr-1.18 {
154 test_fts3expr {(one AND two) OR ((three AND four) AND five)}
156 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
158 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \
162 do_test fts3expr-1.19 {
163 test_fts3expr {(one AND two) AND ((three AND four) OR five)}
165 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \
167 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \
171 do_test fts3expr-1.20 {
172 test_fts3expr {(one OR two) AND ((three OR four) AND five)}
174 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
176 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
180 do_test fts3expr-1.21 {
181 test_fts3expr {(one OR two) AND ((three NOT four) AND five)}
183 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
185 [list NOT {PHRASE 3 0 three} {PHRASE 3 0 four}] \
189 do_test fts3expr-1.22 {
190 test_fts3expr {(one OR two) NOT ((three OR four) AND five)}
192 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
194 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
198 do_test fts3expr-1.23 {
199 test_fts3expr {(((((one OR two))))) NOT (((((three OR four))) AND five))}
201 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \
203 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
207 do_test fts3expr-1.24 {
208 test_fts3expr {one NEAR two}
209 } [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}]
210 do_test fts3expr-1.25 {
211 test_fts3expr {(one NEAR two)}
212 } [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}]
213 do_test fts3expr-1.26 {
214 test_fts3expr {((((((one NEAR two))))))}
215 } [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}]
216 do_test fts3expr-1.27 {
217 test_fts3expr {(one NEAR two) OR ((three OR four) AND five)}
219 [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}] \
221 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
225 do_test fts3expr-1.28 {
226 test_fts3expr {(one NEAR/321 two) OR ((three OR four) AND five)}
228 [list NEAR/321 {PHRASE 3 0 one} {PHRASE 3 0 two}] \
230 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \
235 proc strip_phrase_data {L} {
236 if {[lindex $L 0] eq "PHRASE"} {
237 return [lrange $L 3 end]
241 [strip_phrase_data [lindex $L 1]] \
242 [strip_phrase_data [lindex $L 2]] \
245 proc test_fts3expr2 {expr} {
247 db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')}
250 do_test fts3expr-2.1 {
251 test_fts3expr2 "ab OR cd AND ef"
252 } {OR ab {AND cd ef}}
253 do_test fts3expr-2.2 {
254 test_fts3expr2 "cd AND ef OR ab"
255 } {OR {AND cd ef} ab}
256 do_test fts3expr-2.3 {
257 test_fts3expr2 "ab AND cd AND ef OR gh"
258 } {OR {AND {AND ab cd} ef} gh}
259 do_test fts3expr-2.4 {
260 test_fts3expr2 "ab AND cd OR ef AND gh"
261 } {OR {AND ab cd} {AND ef gh}}
262 do_test fts3expr-2.5 {
263 test_fts3expr2 "ab cd"
266 do_test fts3expr-3.1 {
267 test_fts3expr2 "(ab OR cd) AND ef"
268 } {AND {OR ab cd} ef}
269 do_test fts3expr-3.2 {
270 test_fts3expr2 "ef AND (ab OR cd)"
271 } {AND ef {OR ab cd}}
272 do_test fts3expr-3.3 {
273 test_fts3expr2 "(ab OR cd)"
275 do_test fts3expr-3.4 {
276 test_fts3expr2 "(((ab OR cd)))"
279 do_test fts3expr-3.5 {
280 test_fts3expr2 "one AND (two NEAR three)"
281 } {AND one {NEAR/10 two three}}
282 do_test fts3expr-3.6 {
283 test_fts3expr2 "one (two NEAR three)"
284 } {AND one {NEAR/10 two three}}
285 do_test fts3expr-3.7 {
286 test_fts3expr2 "(two NEAR three) one"
287 } {AND {NEAR/10 two three} one}
288 do_test fts3expr-3.8 {
289 test_fts3expr2 "(two NEAR three) AND one"
290 } {AND {NEAR/10 two three} one}
291 do_test fts3expr-3.9 {
292 test_fts3expr2 "(two NEAR three) (four five)"
293 } {AND {NEAR/10 two three} {AND four five}}
294 do_test fts3expr-3.10 {
295 test_fts3expr2 "(two NEAR three) AND (four five)"
296 } {AND {NEAR/10 two three} {AND four five}}
297 do_test fts3expr-3.11 {
298 test_fts3expr2 "(two NEAR three) (four NEAR five)"
299 } {AND {NEAR/10 two three} {NEAR/10 four five}}
300 do_test fts3expr-3.12 {
301 test_fts3expr2 "(two NEAR three) OR (four NEAR five)"
302 } {OR {NEAR/10 two three} {NEAR/10 four five}}
304 do_test fts3expr-3.13 {
305 test_fts3expr2 "(two NEAR/1a three)"
306 } {AND {AND {AND two near} 1a} three}
308 do_test fts3expr-3.14 {
309 test_fts3expr2 "(two NEAR// three)"
310 } {AND {AND two near} three}
311 do_test fts3expr-3.15 {
312 test_fts3expr2 "(two NEAR/: three)"
313 } {AND {AND two near} three}
315 do_test fts3expr-3.16 {
316 test_fts3expr2 "(two NEAR three)OR(four NEAR five)"
317 } {OR {NEAR/10 two three} {NEAR/10 four five}}
318 do_test fts3expr-3.17 {
319 test_fts3expr2 "(two NEAR three)OR\"four five\""
320 } {OR {NEAR/10 two three} {four five}}
321 do_test fts3expr-3.18 {
322 test_fts3expr2 "one \u0080wo"
327 #------------------------------------------------------------------------
328 # The following tests, fts3expr-4.*, test the parsers response to syntax
329 # errors in query expressions. This is done using a real fts3 table and
330 # MATCH clauses, not the parser test interface.
332 do_test fts3expr-4.1 {
333 execsql { CREATE VIRTUAL TABLE t1 USING fts3(a, b, c) }
336 # Mismatched parenthesis:
337 do_test fts3expr-4.2.1 {
338 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world))' }
339 } {1 {malformed MATCH expression: [example AND (hello OR world))]}}
340 do_test fts3expr-4.2.2 {
341 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example AND (hello OR world' }
342 } {1 {malformed MATCH expression: [example AND (hello OR world]}}
343 do_test fts3expr-4.2.3 {
344 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello' }
345 } {1 {malformed MATCH expression: [(hello]}}
346 do_test fts3expr-4.2.4 {
347 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(' }
348 } {1 {malformed MATCH expression: [(]}}
349 do_test fts3expr-4.2.5 {
350 catchsql { SELECT * FROM t1 WHERE t1 MATCH ')' }
351 } {1 {malformed MATCH expression: [)]}}
353 do_test fts3expr-4.2.6 {
354 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example (hello world' }
355 } {1 {malformed MATCH expression: [example (hello world]}}
357 # Unterminated quotation marks:
358 do_test fts3expr-4.3.1 {
359 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR "hello world' }
360 } {1 {malformed MATCH expression: [example OR "hello world]}}
361 do_test fts3expr-4.3.2 {
362 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example OR hello world"' }
363 } {1 {malformed MATCH expression: [example OR hello world"]}}
365 # Binary operators without the required operands.
366 do_test fts3expr-4.4.1 {
367 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'OR hello world' }
368 } {1 {malformed MATCH expression: [OR hello world]}}
369 do_test fts3expr-4.4.2 {
370 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'hello world OR' }
371 } {1 {malformed MATCH expression: [hello world OR]}}
372 do_test fts3expr-4.4.3 {
373 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (hello world OR) two' }
374 } {1 {malformed MATCH expression: [one (hello world OR) two]}}
375 do_test fts3expr-4.4.4 {
376 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one (OR hello world) two' }
377 } {1 {malformed MATCH expression: [one (OR hello world) two]}}
379 # NEAR operators with something other than phrases as arguments.
380 do_test fts3expr-4.5.1 {
381 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello OR world) NEAR one' }
382 } {1 {malformed MATCH expression: [(hello OR world) NEAR one]}}
383 do_test fts3expr-4.5.2 {
384 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'one NEAR (hello OR world)' }
385 } {1 {malformed MATCH expression: [one NEAR (hello OR world)]}}
387 #------------------------------------------------------------------------
388 # The following OOM tests are designed to cover cases in fts3_expr.c.
390 source $testdir/malloc_common.tcl
391 do_malloc_test fts3expr-malloc-1 -sqlbody {
392 SELECT fts3_exprtest('simple', 'a b c "d e f"', 'a', 'b', 'c')
394 do_malloc_test fts3expr-malloc-2 -tclprep {
395 set sqlite_fts3_enable_parentheses 0
397 SELECT fts3_exprtest('simple', 'a -b', 'a', 'b', 'c')
399 set sqlite_fts3_enable_parentheses 1
402 #------------------------------------------------------------------------
403 # The following tests are not very important. They cover error handling
404 # cases in the test code, which makes test coverage easier to measure.
406 do_test fts3expr-5.1 {
407 catchsql { SELECT fts3_exprtest('simple', 'a b') }
408 } {1 {Usage: fts3_exprtest(tokenizer, expr, col1, ...}}
409 do_test fts3expr-5.2 {
410 catchsql { SELECT fts3_exprtest('doesnotexist', 'a b', 'c') }
411 } {1 {No such tokenizer module}}
412 do_test fts3expr-5.3 {
413 catchsql { SELECT fts3_exprtest('simple', 'a b OR', 'c') }
414 } {1 {Error parsing expression}}
416 #------------------------------------------------------------------------
417 # The next set of tests verifies that things actually work as they are
418 # supposed to when using the new syntax.
420 do_test fts3expr-6.1 {
422 CREATE VIRTUAL TABLE t1 USING fts3(a);
424 for {set ii 1} {$ii < 32} {incr ii} {
426 if {$ii & 1} { lappend v one }
427 if {$ii & 2} { lappend v two }
428 if {$ii & 4} { lappend v three }
429 if {$ii & 8} { lappend v four }
430 if {$ii & 16} { lappend v five }
431 execsql { INSERT INTO t1 VALUES($v) }
434 execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'five four one' ORDER BY rowid}
437 foreach {id expr res} {
439 2 "five four NOT one" {24 26 28 30}
441 3 "five AND four OR one"
442 {1 3 5 7 9 11 13 15 17 19 21 23 24 25 26 27 28 29 30 31}
444 4 "five AND (four OR one)" {17 19 21 23 24 25 26 27 28 29 30 31}
446 5 "five NOT (four OR one)" {16 18 20 22}
448 6 "(five NOT (four OR one)) OR (five AND (four OR one))"
449 {16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
451 7 "(five OR one) AND two AND three" {7 15 22 23 30 31}
453 8 "five OR one AND two AND three"
454 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
456 9 "five OR one two three"
457 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
459 10 "five OR \"one two three\""
460 {7 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31}
462 11 "one two OR four five NOT three" {3 7 11 15 19 23 24 25 26 27 31}
464 12 "(one two OR four five) NOT three" {3 11 19 24 25 26 27}
466 13 "((((((one two OR four five)))))) NOT three" {3 11 19 24 25 26 27}
469 do_test fts3expr-6.1.$id {
470 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid }
474 set sqlite_fts3_enable_parentheses 0
475 foreach {id expr res} {
476 1 "one -two three" {5 13 21 29}
477 2 "-two one three" {5 13 21 29}
478 3 "one three -two" {5 13 21 29}
479 4 "-one -two three" {4 12 20 28}
480 5 "three -one -two" {4 12 20 28}
481 6 "-one three -two" {4 12 20 28}
483 do_test fts3expr-6.2.$id {
484 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid }
487 set sqlite_fts3_enable_parentheses 1
489 do_test fts3expr-7.1 {
491 CREATE VIRTUAL TABLE test USING fts3 (keyword);
492 INSERT INTO test VALUES ('abc');
493 SELECT * FROM test WHERE keyword MATCH '""';
498 set sqlite_fts3_enable_parentheses 0