From: danielk1977 Date: Thu, 1 Jan 2009 07:08:54 +0000 (+0000) Subject: Add pseudo-random tests of the fts3 expression parser. Revise the fix in (6091).... X-Git-Tag: version-3.6.10~92 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5973e6a30b13e481a26422279a0a5a9884f4ad9d;p=thirdparty%2Fsqlite.git Add pseudo-random tests of the fts3 expression parser. Revise the fix in (6091). (CVS 6092) FossilOrigin-Name: 11c2d4686197fb3f0d601651d5bbb3492af8f0dd --- diff --git a/ext/fts3/fts3_expr.c b/ext/fts3/fts3_expr.c index fe6cf7af78..c98dc72100 100644 --- a/ext/fts3/fts3_expr.c +++ b/ext/fts3/fts3_expr.c @@ -532,6 +532,12 @@ static int fts3ExprParse( int eType = p->eType; assert( eType!=FTSQUERY_PHRASE || !p->pPhrase->isNot ); isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft); + + /* The isRequirePhrase variable is set to true if a phrase or + ** an expression contained in parenthesis is required. If a + ** binary operator (AND, OR, NOT or NEAR) is encounted when + ** isRequirePhrase is set, this is a syntax error. + */ if( !isPhrase && isRequirePhrase ){ sqlite3Fts3ExprFree(p); rc = SQLITE_ERROR; @@ -554,14 +560,19 @@ static int fts3ExprParse( pPrev = pAnd; } + /* This test catches attempts to make either operand of a NEAR + ** operator something other than a phrase. For example, either of + ** the following: + ** + ** (bracketed expression) NEAR phrase + ** phrase NEAR (bracketed expression) + ** + ** Return an error in either case. + */ if( pPrev && ( - (pPrev->eType==FTSQUERY_NEAR && eType!=FTSQUERY_PHRASE) - || (eType==FTSQUERY_NEAR && pPrev->eType!=FTSQUERY_PHRASE && !isPhrase) + (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE) + || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR) )){ - /* This is an attempt to do "phrase NEAR (bracketed expression)" - ** or "(bracketed expression) NEAR phrase", both of which are - ** illegal. Return an error. - */ sqlite3Fts3ExprFree(p); rc = SQLITE_ERROR; goto exprparse_out; diff --git a/manifest b/manifest index e4bcd8dc65..a9efbb5463 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sbug\sparsing\s"\sAND\s(abc\sNEAR\sdef)"\sin\sfts3_expr.c.\s(CVS\s6091) -D 2009-01-01T04:19:51 +C Add\spseudo-random\stests\sof\sthe\sfts3\sexpression\sparser.\sRevise\sthe\sfix\sin\s(6091).\s(CVS\s6092) +D 2009-01-01T07:08:55 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 77635d0909c2067cee03889a1e04ce910d8fb809 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -55,7 +55,7 @@ F ext/fts3/README.tokenizers 998756696647400de63d5ba60e9655036cb966e9 F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d F ext/fts3/fts3.c 3aa6aef1eadc44606f6ed3c841062735a5210077 F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe -F ext/fts3/fts3_expr.c 4f00c773a3f7ebf2c7079b97c992f3f6ebb8cd6f +F ext/fts3/fts3_expr.c ac57b3ae142da80527cccdc4ab4e100fac1737fb F ext/fts3/fts3_expr.h 4dad4d87cf5d41ea924a815fe89a6f87dc76f277 F ext/fts3/fts3_hash.c e15e84d18f8df149ab290029872d4559c4c7c15a F ext/fts3/fts3_hash.h 004b759e1602ff16dfa02fea3ca1c77336ad6798 @@ -368,7 +368,8 @@ F test/fts3b.test b3a25180a633873d37d86e1ccd00ed690d37237a F test/fts3c.test 4c7ef29b37aca3e8ebb6a39b57910caa6506034e F test/fts3d.test d92a47fe8ed59c9e53d2d8e6d2685bb380aadadc F test/fts3e.test 1f6c6ac9cc8b772ca256e6b22aaeed50c9350851 -F test/fts3expr.test fa7bd31721cb3520521d43d5e4df3fbb9faa6426 +F test/fts3expr.test 40a2fff17a2487e9389234f7304b174064155acc +F test/fts3expr2.test 8501de895a4c0631e7226c9bac055cd49c9f6646 F test/fts3near.test e8a9b4e16c63a795918b334b74d4aec14815bf8b F test/func.test a50f0a4b69ac251debe1dce3ba29da7476dc8c52 F test/fuzz.test 62fc19dd36a427777fd671b569df07166548628a @@ -689,7 +690,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P 73958060aaf641d93bede3a42851e5b3451f5432 -R b18c53d48a15a59b2ef3ca97050c92ee +P d1a6a2edd799d65ff88510df951e909919e35b6b +R 7818503c72e44fd985399b599f4925a2 U danielk1977 -Z ecd0fc691e306c5b472f9b36b016cb1f +Z e4f3adbf5a486d51a8277e421fd369a1 diff --git a/manifest.uuid b/manifest.uuid index 985de7b7e0..08bc41a06a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d1a6a2edd799d65ff88510df951e909919e35b6b \ No newline at end of file +11c2d4686197fb3f0d601651d5bbb3492af8f0dd \ No newline at end of file diff --git a/test/fts3expr.test b/test/fts3expr.test index 31a328a6c1..3e70d5efc0 100644 --- a/test/fts3expr.test +++ b/test/fts3expr.test @@ -11,7 +11,7 @@ # This file implements regression tests for SQLite library. The # focus of this script is testing the FTS3 module. # -# $Id: fts3expr.test,v 1.2 2009/01/01 04:19:51 danielk1977 Exp $ +# $Id: fts3expr.test,v 1.3 2009/01/01 07:08:55 danielk1977 Exp $ # set testdir [file dirname $argv0] @@ -122,6 +122,28 @@ do_test fts3expr-3.4 { do_test fts3expr-3.5 { test_fts3expr2 "one AND (two NEAR three)" } {AND one {NEAR/10 two three}} +do_test fts3expr-3.6 { + test_fts3expr2 "one (two NEAR three)" +} {AND one {NEAR/10 two three}} +do_test fts3expr-3.7 { + test_fts3expr2 "(two NEAR three) one" +} {AND {NEAR/10 two three} one} +do_test fts3expr-3.8 { + test_fts3expr2 "(two NEAR three) AND one" +} {AND {NEAR/10 two three} one} +do_test fts3expr-3.9 { + test_fts3expr2 "(two NEAR three) (four five)" +} {AND {NEAR/10 two three} {AND four five}} +do_test fts3expr-3.10 { + test_fts3expr2 "(two NEAR three) AND (four five)" +} {AND {NEAR/10 two three} {AND four five}} +do_test fts3expr-3.11 { + test_fts3expr2 "(two NEAR three) (four NEAR five)" +} {AND {NEAR/10 two three} {NEAR/10 four five}} +do_test fts3expr-3.12 { + test_fts3expr2 "(two NEAR three) OR (four NEAR five)" +} {OR {NEAR/10 two three} {NEAR/10 four five}} + #------------------------------------------------------------------------ # The following tests, fts3expr-4.*, test the parsers response to syntax diff --git a/test/fts3expr2.test b/test/fts3expr2.test new file mode 100644 index 0000000000..48f7194ef3 --- /dev/null +++ b/test/fts3expr2.test @@ -0,0 +1,167 @@ +# 2009 January 1 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# This file implements regression tests for SQLite library. The +# focus of this script is testing the FTS3 module syntax parser. +# +# $Id: fts3expr2.test,v 1.1 2009/01/01 07:08:55 danielk1977 Exp $ +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# If SQLITE_ENABLE_FTS3 is defined, omit this file. +ifcapable !fts3 { + finish_test + return +} + +# Test overview: +# +# The tests in this file are pseudo-randomly generated. They test +# the fts3 match expression parser via the test interface +# SQL function "fts3_exprtest" (see comments in fts3_expr.c). +# +# Each test case works as follows: +# +# 1. A random expression tree is generated using proc [random_expr_tree]. +# 2. The expression tree is converted to the text of an equivalent +# fts3 expression using proc [tree_to_expr]. +# 3. The test SQL function "fts3_exprtest" is used to parse the +# expression text generated in step (2), returning a parsed expression +# tree. +# 4. Test that the tree returned in step (3) matches that generated in +# step (1). +# +# In step (2), 4 different fts3 expressions are created from each +# expression tree by varying the following boolean properties: +# +# * Whether or not superflous parenthesis are included. i.e. if +# "a OR b AND (c OR d)" or "a OR (b AND (c OR d))" is generated. +# +# * Whether or not explict AND operators are used. i.e. if +# "a OR b AND c" or "a OR b c" is generated. +# + +set sqlite_fts3_enable_parentheses 1 + +proc strip_phrase_data {L} { + if {[lindex $L 0] eq "PHRASE"} { + return [list P [lrange $L 3 end]] + } + return [list \ + [lindex $L 0] \ + [strip_phrase_data [lindex $L 1]] \ + [strip_phrase_data [lindex $L 2]] \ + ] +} +proc test_fts3expr2 {expr} { + strip_phrase_data [ + db one {SELECT fts3_exprtest('simple', $expr, 'a', 'b', 'c')} + ] +} + +proc rnd {nMax} { expr {int(rand()*$nMax)} } + +proc random_phrase {} { + set phrases [list one two three four "one two" "three four"] + list P [lindex $phrases [rnd [llength $phrases]]] +} + +# Generate and return a pseudo-random expression tree. Using the same +# format returned by the [test_fts3expr2] proc. +# +proc random_expr_tree {iHeight} { + if {$iHeight==0 || [rnd 3]==0} { + return [random_phrase] + } + + set operators [list NEAR NOT AND OR] + set op [lindex $operators [rnd 4]] + + if {$op eq "NEAR"} { + set iDistance [rnd 15] + return [list $op/$iDistance [random_phrase] [random_phrase]] + } + + set iNH [expr {$iHeight - 1}] + return [list $op [random_expr_tree $iNH] [random_expr_tree $iNH]] +} + +# Given an expression tree, generate a corresponding expression. +# +proc tree_to_expr {tree all_brackets implicit_and} { + set prec(NOT) 2 + set prec(AND) 3 + set prec() 3 + set prec(OR) 4 + + set op [lindex $tree 0] + + if {$op eq "P"} { + set phrase [lindex $tree 1] + if {[llength $phrase]>1} { + return "\"$phrase\"" + } else { + return $phrase + } + } + + if {$op eq "NEAR/10"} { + set op "NEAR" + } + if {$op eq "AND" && $implicit_and} { + set op "" + } + + set lhs [lindex $tree 1] + set rhs [lindex $tree 2] + set zLeft [tree_to_expr $lhs $all_brackets $implicit_and] + set zRight [tree_to_expr $rhs $all_brackets $implicit_and] + + set iPrec 5 + set iLeftPrec 0 + set iRightPrec 0 + + catch {set iPrec $prec($op)} + catch {set iLeftPrec $prec([lindex $lhs 0])} + catch {set iRightPrec $prec([lindex $rhs 0])} + + if {$iLeftPrec > $iPrec || $all_brackets} { + set zLeft "($zLeft)" + } + if {$iRightPrec >= $iPrec || $all_brackets} { + set zRight "($zRight)" + } + + return "$zLeft $op $zRight" +} + +proc do_exprparse_test {name expr tree} { + uplevel do_test $name [list "test_fts3expr2 {$expr}"] [list $tree] +} + +for {set iTest 1} {$iTest<500} {incr iTest} { + set t [random_expr_tree 4] + + set e1 [tree_to_expr $t 0 0] + set e2 [tree_to_expr $t 0 1] + set e3 [tree_to_expr $t 1 0] + set e4 [tree_to_expr $t 1 1] + + do_exprparse_test fts3expr2-$iTest.1 $e1 $t + do_exprparse_test fts3expr2-$iTest.2 $e2 $t + do_exprparse_test fts3expr2-$iTest.3 $e3 $t + do_exprparse_test fts3expr2-$iTest.4 $e4 $t +} + +set sqlite_fts3_enable_parentheses 0 +finish_test +