1# 2006 September 9 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 FTS3 module. 13# 14# $Id: fts3expr.test,v 1.9 2009/07/28 16:44:26 danielk1977 Exp $ 15# 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# If SQLITE_ENABLE_FTS3 is defined, omit this file. 21ifcapable !fts3 { 22 finish_test 23 return 24} 25 26set sqlite_fts3_enable_parentheses 1 27 28proc test_fts3expr {expr} { 29 db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')} 30} 31 32do_test fts3expr-1.0 { 33 test_fts3expr "abcd" 34} {PHRASE 3 0 abcd} 35do_test fts3expr-1.1 { 36 test_fts3expr " tag " 37} {PHRASE 3 0 tag} 38 39do_test fts3expr-1.2 { 40 test_fts3expr "ab AND cd" 41} {AND {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 42do_test fts3expr-1.2.1 { 43 test_fts3expr "ab cd" 44} {AND {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 45do_test fts3expr-1.3 { 46 test_fts3expr "ab OR cd" 47} {OR {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 48do_test fts3expr-1.4 { 49 test_fts3expr "ab NOT cd" 50} {NOT {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 51do_test fts3expr-1.5 { 52 test_fts3expr "ab NEAR cd" 53} {NEAR/10 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 54do_test fts3expr-1.6.1 { 55 test_fts3expr "ab NEAR/5 cd" 56} {NEAR/5 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 57do_test fts3expr-1.6.2 { 58 test_fts3expr "ab NEAR/87654321 cd" 59} {NEAR/87654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 60do_test fts3expr-1.6.3 { 61 test_fts3expr "ab NEAR/7654321 cd" 62} {NEAR/7654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 63do_test fts3expr-1.6.4 { 64 test_fts3expr "ab NEAR/654321 cd" 65} {NEAR/654321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 66do_test fts3expr-1.6.5 { 67 test_fts3expr "ab NEAR/54321 cd" 68} {NEAR/54321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 69do_test fts3expr-1.6.6 { 70 test_fts3expr "ab NEAR/4321 cd" 71} {NEAR/4321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 72do_test fts3expr-1.6.7 { 73 test_fts3expr "ab NEAR/321 cd" 74} {NEAR/321 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 75do_test fts3expr-1.6.8 { 76 test_fts3expr "ab NEAR/21 cd" 77} {NEAR/21 {PHRASE 3 0 ab} {PHRASE 3 0 cd}} 78 79do_test fts3expr-1.7 { 80 test_fts3expr {"one two three"} 81} {PHRASE 3 0 one two three} 82do_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}} 85do_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}} 88do_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}} 91do_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}} 94do_test fts3expr-1.9.1 { 95 test_fts3expr {"one* two three"} 96} {PHRASE 3 0 one+ two three} 97do_test fts3expr-1.9.2 { 98 test_fts3expr {"one two* three"} 99} {PHRASE 3 0 one two+ three} 100do_test fts3expr-1.9.3 { 101 test_fts3expr {"one* two* three"} 102} {PHRASE 3 0 one+ two+ three} 103do_test fts3expr-1.9.4 { 104 test_fts3expr {"one two three*"} 105} {PHRASE 3 0 one two three+} 106do_test fts3expr-1.9.5 { 107 test_fts3expr {"one* two three*"} 108} {PHRASE 3 0 one+ two three+} 109do_test fts3expr-1.9.6 { 110 test_fts3expr {"one two* three*"} 111} {PHRASE 3 0 one two+ three+} 112do_test fts3expr-1.9.7 { 113 test_fts3expr {"one* two* three*"} 114} {PHRASE 3 0 one+ two+ three+} 115 116do_test fts3expr-1.10 { 117 test_fts3expr {one* two} 118} {AND {PHRASE 3 0 one+} {PHRASE 3 0 two}} 119do_test fts3expr-1.11 { 120 test_fts3expr {one two*} 121} {AND {PHRASE 3 0 one} {PHRASE 3 0 two+}} 122 123do_test fts3expr-1.14 { 124 test_fts3expr {a:one two} 125} {AND {PHRASE 0 0 one} {PHRASE 3 0 two}} 126do_test fts3expr-1.15.1 { 127 test_fts3expr {one b:two} 128} {AND {PHRASE 3 0 one} {PHRASE 1 0 two}} 129do_test fts3expr-1.15.2 { 130 test_fts3expr {one B:two} 131} {AND {PHRASE 3 0 one} {PHRASE 1 0 two}} 132 133do_test fts3expr-1.16 { 134 test_fts3expr {one AND two AND three AND four AND five} 135} [list AND \ 136 [list AND \ 137 [list AND \ 138 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 139 {PHRASE 3 0 three} \ 140 ] \ 141 {PHRASE 3 0 four} \ 142 ] \ 143 {PHRASE 3 0 five} \ 144 ] 145do_test fts3expr-1.17 { 146 test_fts3expr {(one AND two) AND ((three AND four) AND five)} 147} [list AND \ 148 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 149 [list AND \ 150 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 151 {PHRASE 3 0 five} \ 152 ] \ 153 ] 154do_test fts3expr-1.18 { 155 test_fts3expr {(one AND two) OR ((three AND four) AND five)} 156} [list OR \ 157 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 158 [list AND \ 159 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 160 {PHRASE 3 0 five} \ 161 ] \ 162 ] 163do_test fts3expr-1.19 { 164 test_fts3expr {(one AND two) AND ((three AND four) OR five)} 165} [list AND \ 166 [list AND {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 167 [list OR \ 168 [list AND {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 169 {PHRASE 3 0 five} \ 170 ] \ 171 ] 172do_test fts3expr-1.20 { 173 test_fts3expr {(one OR two) AND ((three OR four) AND five)} 174} [list AND \ 175 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 176 [list AND \ 177 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 178 {PHRASE 3 0 five} \ 179 ] \ 180 ] 181do_test fts3expr-1.21 { 182 test_fts3expr {(one OR two) AND ((three NOT four) AND five)} 183} [list AND \ 184 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 185 [list AND \ 186 [list NOT {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 187 {PHRASE 3 0 five} \ 188 ] \ 189 ] 190do_test fts3expr-1.22 { 191 test_fts3expr {(one OR two) NOT ((three OR four) AND five)} 192} [list NOT \ 193 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 194 [list AND \ 195 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 196 {PHRASE 3 0 five} \ 197 ] \ 198 ] 199do_test fts3expr-1.23 { 200 test_fts3expr {(((((one OR two))))) NOT (((((three OR four))) AND five))} 201} [list NOT \ 202 [list OR {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 203 [list AND \ 204 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 205 {PHRASE 3 0 five} \ 206 ] \ 207 ] 208do_test fts3expr-1.24 { 209 test_fts3expr {one NEAR two} 210} [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}] 211do_test fts3expr-1.25 { 212 test_fts3expr {(one NEAR two)} 213} [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}] 214do_test fts3expr-1.26 { 215 test_fts3expr {((((((one NEAR two))))))} 216} [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}] 217do_test fts3expr-1.27 { 218 test_fts3expr {(one NEAR two) OR ((three OR four) AND five)} 219} [list OR \ 220 [list NEAR/10 {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 221 [list AND \ 222 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 223 {PHRASE 3 0 five} \ 224 ] \ 225 ] 226do_test fts3expr-1.28 { 227 test_fts3expr {(one NEAR/321 two) OR ((three OR four) AND five)} 228} [list OR \ 229 [list NEAR/321 {PHRASE 3 0 one} {PHRASE 3 0 two}] \ 230 [list AND \ 231 [list OR {PHRASE 3 0 three} {PHRASE 3 0 four}] \ 232 {PHRASE 3 0 five} \ 233 ] \ 234 ] 235 236proc strip_phrase_data {L} { 237 if {[lindex $L 0] eq "PHRASE"} { 238 return [lrange $L 3 end] 239 } 240 return [list \ 241 [lindex $L 0] \ 242 [strip_phrase_data [lindex $L 1]] \ 243 [strip_phrase_data [lindex $L 2]] \ 244 ] 245} 246proc test_fts3expr2 {expr} { 247 strip_phrase_data [ 248 db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')} 249 ] 250} 251do_test fts3expr-2.1 { 252 test_fts3expr2 "ab OR cd AND ef" 253} {OR ab {AND cd ef}} 254do_test fts3expr-2.2 { 255 test_fts3expr2 "cd AND ef OR ab" 256} {OR {AND cd ef} ab} 257do_test fts3expr-2.3 { 258 test_fts3expr2 "ab AND cd AND ef OR gh" 259} {OR {AND {AND ab cd} ef} gh} 260do_test fts3expr-2.4 { 261 test_fts3expr2 "ab AND cd OR ef AND gh" 262} {OR {AND ab cd} {AND ef gh}} 263do_test fts3expr-2.5 { 264 test_fts3expr2 "ab cd" 265} {AND ab cd} 266 267do_test fts3expr-3.1 { 268 test_fts3expr2 "(ab OR cd) AND ef" 269} {AND {OR ab cd} ef} 270do_test fts3expr-3.2 { 271 test_fts3expr2 "ef AND (ab OR cd)" 272} {AND ef {OR ab cd}} 273do_test fts3expr-3.3 { 274 test_fts3expr2 "(ab OR cd)" 275} {OR ab cd} 276do_test fts3expr-3.4 { 277 test_fts3expr2 "(((ab OR cd)))" 278} {OR ab cd} 279 280do_test fts3expr-3.5 { 281 test_fts3expr2 "one AND (two NEAR three)" 282} {AND one {NEAR/10 two three}} 283do_test fts3expr-3.6 { 284 test_fts3expr2 "one (two NEAR three)" 285} {AND one {NEAR/10 two three}} 286do_test fts3expr-3.7 { 287 test_fts3expr2 "(two NEAR three) one" 288} {AND {NEAR/10 two three} one} 289do_test fts3expr-3.8 { 290 test_fts3expr2 "(two NEAR three) AND one" 291} {AND {NEAR/10 two three} one} 292do_test fts3expr-3.9 { 293 test_fts3expr2 "(two NEAR three) (four five)" 294} {AND {NEAR/10 two three} {AND four five}} 295do_test fts3expr-3.10 { 296 test_fts3expr2 "(two NEAR three) AND (four five)" 297} {AND {NEAR/10 two three} {AND four five}} 298do_test fts3expr-3.11 { 299 test_fts3expr2 "(two NEAR three) (four NEAR five)" 300} {AND {NEAR/10 two three} {NEAR/10 four five}} 301do_test fts3expr-3.12 { 302 test_fts3expr2 "(two NEAR three) OR (four NEAR five)" 303} {OR {NEAR/10 two three} {NEAR/10 four five}} 304 305do_test fts3expr-3.13 { 306 test_fts3expr2 "(two NEAR/1a three)" 307} {AND {AND {AND two near} 1a} three} 308 309do_test fts3expr-3.14 { 310 test_fts3expr2 "(two NEAR// three)" 311} {AND {AND two near} three} 312do_test fts3expr-3.15 { 313 test_fts3expr2 "(two NEAR/: three)" 314} {AND {AND two near} three} 315 316do_test fts3expr-3.16 { 317 test_fts3expr2 "(two NEAR three)OR(four NEAR five)" 318} {OR {NEAR/10 two three} {NEAR/10 four five}} 319do_test fts3expr-3.17 { 320 test_fts3expr2 "(two NEAR three)OR\"four five\"" 321} {OR {NEAR/10 two three} {four five}} 322do_test fts3expr-3.18 { 323 test_fts3expr2 "one \u0080wo" 324} "AND one \u0080wo" 325 326 327 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. 332# 333do_test fts3expr-4.1 { 334 execsql { CREATE VIRTUAL TABLE t1 USING fts3(a, b, c) } 335} {} 336 337# Mismatched parenthesis: 338do_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))]}} 341do_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]}} 344do_test fts3expr-4.2.3 { 345 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(hello' } 346} {1 {malformed MATCH expression: [(hello]}} 347do_test fts3expr-4.2.4 { 348 catchsql { SELECT * FROM t1 WHERE t1 MATCH '(' } 349} {1 {malformed MATCH expression: [(]}} 350do_test fts3expr-4.2.5 { 351 catchsql { SELECT * FROM t1 WHERE t1 MATCH ')' } 352} {1 {malformed MATCH expression: [)]}} 353 354do_test fts3expr-4.2.6 { 355 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'example (hello world' } 356} {1 {malformed MATCH expression: [example (hello world]}} 357 358# Unterminated quotation marks: 359do_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]}} 362do_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"]}} 365 366# Binary operators without the required operands. 367do_test fts3expr-4.4.1 { 368 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'OR hello world' } 369} {1 {malformed MATCH expression: [OR hello world]}} 370do_test fts3expr-4.4.2 { 371 catchsql { SELECT * FROM t1 WHERE t1 MATCH 'hello world OR' } 372} {1 {malformed MATCH expression: [hello world OR]}} 373do_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]}} 376do_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]}} 379 380# NEAR operators with something other than phrases as arguments. 381do_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]}} 384do_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)]}} 387 388#------------------------------------------------------------------------ 389# The following OOM tests are designed to cover cases in fts3_expr.c. 390# 391source $testdir/malloc_common.tcl 392do_malloc_test fts3expr-malloc-1 -sqlbody { 393 SELECT fts3_exprtest('simple', 'a b c "d e f"', 'a', 'b', 'c') 394} 395do_malloc_test fts3expr-malloc-2 -tclprep { 396 set sqlite_fts3_enable_parentheses 0 397} -sqlbody { 398 SELECT fts3_exprtest('simple', 'a -b', 'a', 'b', 'c') 399} -cleanup { 400 set sqlite_fts3_enable_parentheses 1 401} 402 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. 406# 407do_test fts3expr-5.1 { 408 catchsql { SELECT fts3_exprtest('simple', 'a b') } 409} {1 {Usage: fts3_exprtest(tokenizer, expr, col1, ...}} 410do_test fts3expr-5.2 { 411 catchsql { SELECT fts3_exprtest('doesnotexist', 'a b', 'c') } 412} {1 {unknown tokenizer: doesnotexist}} 413do_test fts3expr-5.3 { 414 catchsql { SELECT fts3_exprtest('simple', 'a b OR', 'c') } 415} {1 {Error parsing expression}} 416 417#------------------------------------------------------------------------ 418# The next set of tests verifies that things actually work as they are 419# supposed to when using the new syntax. 420# 421do_test fts3expr-6.1 { 422 execsql { 423 CREATE VIRTUAL TABLE t1 USING fts3(a); 424 } 425 for {set ii 1} {$ii < 32} {incr ii} { 426 set v [list] 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) } 433 } 434 435 execsql {SELECT rowid FROM t1 WHERE t1 MATCH 'five four one' ORDER BY rowid} 436} {25 27 29 31} 437 438foreach {id expr res} { 439 440 2 "five four NOT one" {24 26 28 30} 441 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} 444 445 4 "five AND (four OR one)" {17 19 21 23 24 25 26 27 28 29 30 31} 446 447 5 "five NOT (four OR one)" {16 18 20 22} 448 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} 451 452 7 "(five OR one) AND two AND three" {7 15 22 23 30 31} 453 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} 456 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} 459 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} 462 463 11 "one two OR four five NOT three" {3 7 11 15 19 23 24 25 26 27 31} 464 465 12 "(one two OR four five) NOT three" {3 11 19 24 25 26 27} 466 467 13 "((((((one two OR four five)))))) NOT three" {3 11 19 24 25 26 27} 468 469} { 470 do_test fts3expr-6.1.$id { 471 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid } 472 } $res 473} 474 475set sqlite_fts3_enable_parentheses 0 476foreach {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} 483} { 484 do_test fts3expr-6.2.$id { 485 execsql { SELECT rowid FROM t1 WHERE t1 MATCH $expr ORDER BY rowid } 486 } $res 487} 488set sqlite_fts3_enable_parentheses 1 489 490do_test fts3expr-7.1 { 491 execsql { 492 CREATE VIRTUAL TABLE test USING fts3 (keyword); 493 INSERT INTO test VALUES ('abc'); 494 SELECT * FROM test WHERE keyword MATCH '""'; 495 } 496} {} 497 498 499do_test fts3expr-8.0 { test_fts3expr "(blah)" } {PHRASE 3 0 blah} 500do_test fts3expr-8.1 { test_fts3expr "(blah.)" } {PHRASE 3 0 blah} 501do_test fts3expr-8.2 { test_fts3expr "(blah,)" } {PHRASE 3 0 blah} 502do_test fts3expr-8.3 { test_fts3expr "(blah!)" } {PHRASE 3 0 blah} 503do_test fts3expr-8.4 { test_fts3expr "(blah-)" } {PHRASE 3 0 blah} 504 505do_test fts3expr-8.5 { test_fts3expr "((blah.))" } {PHRASE 3 0 blah} 506do_test fts3expr-8.6 { test_fts3expr "(((blah,)))" } {PHRASE 3 0 blah} 507do_test fts3expr-8.7 { test_fts3expr "((((blah!))))" } {PHRASE 3 0 blah} 508 509do_test fts3expr-8.8 { test_fts3expr "(,(blah-),)" } {PHRASE 3 0 blah} 510 511set sqlite_fts3_enable_parentheses 0 512 513do_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}}} 516 517do_test fts3expr-10.1 { test_fts3expr "abc *" } {PHRASE 3 0 abc} 518do_test fts3expr-10.2 { test_fts3expr "*" } {} 519do_test fts3expr-10.3 { test_fts3expr "abc*" } {PHRASE 3 0 abc+} 520 521finish_test 522