Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add tests for fts5 synonyms implemented by adding extra terms to queries. And fixes for the same. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | fts5-incompatible |
Files: | files | file ages | folders |
SHA1: |
dbcb73802b88f76be17f09f3eb83ffac |
User & Date: | dan 2015-09-01 18:08:36.324 |
Context
2015-09-01
| ||
18:44 | Fix a problem that occurs when more than 4 synonyms for a term appear within a single row. (check-in: cd359550bd user: dan tags: fts5-incompatible) | |
18:08 | Add tests for fts5 synonyms implemented by adding extra terms to queries. And fixes for the same. (check-in: dbcb73802b user: dan tags: fts5-incompatible) | |
2015-08-31
| ||
20:06 | Begin changes to allow synonym support by adding multiple terms to a query (an alternative to adding multiple terms to the FTS index). (check-in: ad7feaed4c user: dan tags: fts5-incompatible) | |
Changes
Changes to ext/fts5/fts5Int.h.
︙ | ︙ | |||
230 231 232 233 234 235 236 237 238 239 240 241 242 243 | typedef struct Fts5PoslistReader Fts5PoslistReader; struct Fts5PoslistReader { /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */ int iCol; /* If (iCol>=0), this column only */ const u8 *a; /* Position list to iterate through */ int n; /* Size of buffer at a[] in bytes */ int i; /* Current offset in a[] */ /* Output variables */ u8 bEof; /* Set to true at EOF */ i64 iPos; /* (iCol<<32) + iPos */ }; int sqlite3Fts5PoslistReaderInit( int iCol, /* If (iCol>=0), this column only */ | > > | 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | typedef struct Fts5PoslistReader Fts5PoslistReader; struct Fts5PoslistReader { /* Variables used only by sqlite3Fts5PoslistIterXXX() functions. */ int iCol; /* If (iCol>=0), this column only */ const u8 *a; /* Position list to iterate through */ int n; /* Size of buffer at a[] in bytes */ int i; /* Current offset in a[] */ u8 bFlag; /* For client use (any custom purpose) */ /* Output variables */ u8 bEof; /* Set to true at EOF */ i64 iPos; /* (iCol<<32) + iPos */ }; int sqlite3Fts5PoslistReaderInit( int iCol, /* If (iCol>=0), this column only */ |
︙ | ︙ |
Changes to ext/fts5/fts5_expr.c.
︙ | ︙ | |||
18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include "fts5parse.h" /* ** All token types in the generated fts5parse.h file are greater than 0. */ #define FTS5_EOF 0 typedef struct Fts5ExprTerm Fts5ExprTerm; /* ** Functions generated by lemon from fts5parse.y. */ void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64)); void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*)); | > > | 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include "fts5parse.h" /* ** All token types in the generated fts5parse.h file are greater than 0. */ #define FTS5_EOF 0 #define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) typedef struct Fts5ExprTerm Fts5ExprTerm; /* ** Functions generated by lemon from fts5parse.y. */ void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64)); void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*)); |
︙ | ︙ | |||
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | /* ** Argument pTerm must be a synonym iterator. */ static int fts5ExprSynonymPoslist( Fts5ExprTerm *pTerm, i64 iRowid, u8 **pa, int *pn ){ Fts5PoslistWriter writer = {0}; Fts5PoslistReader aStatic[4]; Fts5PoslistReader *aIter = aStatic; int nIter = 0; Fts5ExprTerm *p; assert( pTerm->pSynonym ); for(p=pTerm; p; p=p->pSynonym){ Fts5IndexIter *pIter = p->pIter; if( sqlite3Fts5IterEof(pIter)==0 && sqlite3Fts5IterRowid(pIter)==iRowid ){ i64 dummy; | > > > > | | > > | | | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | /* ** Argument pTerm must be a synonym iterator. */ static int fts5ExprSynonymPoslist( Fts5ExprTerm *pTerm, i64 iRowid, int *pbDel, /* OUT: Caller should sqlite3_free(*pa) */ u8 **pa, int *pn ){ Fts5PoslistWriter writer = {0}; Fts5PoslistReader aStatic[4]; Fts5PoslistReader *aIter = aStatic; int nIter = 0; int rc = SQLITE_OK; Fts5ExprTerm *p; assert( pTerm->pSynonym ); for(p=pTerm; p; p=p->pSynonym){ Fts5IndexIter *pIter = p->pIter; if( sqlite3Fts5IterEof(pIter)==0 && sqlite3Fts5IterRowid(pIter)==iRowid ){ const u8 *a; int n; i64 dummy; rc = sqlite3Fts5IterPoslist(pIter, &a, &n, &dummy); if( rc!=SQLITE_OK ) return rc; if( sqlite3Fts5PoslistReaderInit(-1, a, n, &aIter[nIter])==0 ){ nIter++; } } } assert( *pbDel==0 ); if( nIter==1 ){ *pa = (u8*)aIter[0].a; *pn = aIter[0].n; }else{ Fts5PoslistWriter writer = {0}; Fts5Buffer buf = {0,0,0}; i64 iPrev = -1; while( 1 ){ int i; i64 iMin = FTS5_LARGEST_INT64; for(i=0; i<nIter; i++){ if( aIter[i].bEof==0 ){ if( aIter[i].iPos==iPrev ){ if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue; } if( aIter[i].iPos<iMin ){ iMin = aIter[i].iPos; } } } if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break; rc = sqlite3Fts5PoslistWriterAppend(&buf, &writer, iMin); iPrev = iMin; } if( rc ){ sqlite3_free(buf.p); }else{ *pa = buf.p; *pn = buf.n; *pbDel = 1; } } return rc; } /* ** All individual term iterators in pPhrase are guaranteed to be valid and ** pointing to the same rowid when this function is called. This function ** checks if the current rowid really is a match, and if so populates |
︙ | ︙ | |||
443 444 445 446 447 448 449 450 451 452 453 454 | /* If the aStatic[] array is not large enough, allocate a large array ** using sqlite3_malloc(). This approach could be improved upon. */ if( pPhrase->nTerm>(sizeof(aStatic) / sizeof(aStatic[0])) ){ int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm; aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte); if( !aIter ) return SQLITE_NOMEM; } /* Initialize a term iterator for each term in the phrase */ for(i=0; i<pPhrase->nTerm; i++){ Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; i64 dummy; | > | > | | > | > | < | 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 | /* If the aStatic[] array is not large enough, allocate a large array ** using sqlite3_malloc(). This approach could be improved upon. */ if( pPhrase->nTerm>(sizeof(aStatic) / sizeof(aStatic[0])) ){ int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm; aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte); if( !aIter ) return SQLITE_NOMEM; } memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm); /* Initialize a term iterator for each term in the phrase */ for(i=0; i<pPhrase->nTerm; i++){ Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; i64 dummy; int n = 0; int bFlag = 0; const u8 *a = 0; if( pTerm->pSynonym ){ rc = fts5ExprSynonymPoslist(pTerm, pNode->iRowid, &bFlag, (u8**)&a, &n); }else{ rc = sqlite3Fts5IterPoslist(pTerm->pIter, &a, &n, &dummy); } if( rc!=SQLITE_OK ) goto ismatch_out; sqlite3Fts5PoslistReaderInit(iCol, a, n, &aIter[i]); aIter[i].bFlag = bFlag; if( aIter[i].bEof ) goto ismatch_out; } while( 1 ){ int bMatch; i64 iPos = aIter[0].iPos; do { bMatch = 1; |
︙ | ︙ | |||
491 492 493 494 495 496 497 498 499 500 501 502 503 504 | for(i=0; i<pPhrase->nTerm; i++){ if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out; } } ismatch_out: *pbMatch = (pPhrase->poslist.n>0); if( aIter!=aStatic ) sqlite3_free(aIter); return rc; } typedef struct Fts5LookaheadReader Fts5LookaheadReader; struct Fts5LookaheadReader { const u8 *a; /* Buffer containing position list */ | > > > | 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | for(i=0; i<pPhrase->nTerm; i++){ if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out; } } ismatch_out: *pbMatch = (pPhrase->poslist.n>0); for(i=0; i<pPhrase->nTerm; i++){ if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a); } if( aIter!=aStatic ) sqlite3_free(aIter); return rc; } typedef struct Fts5LookaheadReader Fts5LookaheadReader; struct Fts5LookaheadReader { const u8 *a; /* Buffer containing position list */ |
︙ | ︙ | |||
668 669 670 671 672 673 674 | if( pTerm->pSynonym ){ int bEof = 1; Fts5ExprTerm *p; /* Find the firstest rowid any synonym points to. */ i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc); | | > < | > > > > > > | > > > > > > | 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | if( pTerm->pSynonym ){ int bEof = 1; Fts5ExprTerm *p; /* Find the firstest rowid any synonym points to. */ i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc); /* Advance each iterator that currently points to iRowid. Or, if iFrom ** is valid - each iterator that points to a rowid before iFrom. */ for(p=pTerm; p; p=p->pSynonym){ if( sqlite3Fts5IterEof(p->pIter)==0 ){ i64 ii = sqlite3Fts5IterRowid(p->pIter); if( ii==iRowid || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc) ){ if( bFromValid ){ rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom); }else{ rc = sqlite3Fts5IterNext(p->pIter); } if( rc!=SQLITE_OK ) break; if( sqlite3Fts5IterEof(p->pIter)==0 ){ bEof = 0; } }else{ bEof = 0; } } } /* Set the EOF flag if either all synonym iterators are at EOF or an ** error has occurred. */ pNode->bEof = (rc || bEof); |
︙ | ︙ | |||
803 804 805 806 807 808 809 | int i; /* Check that each phrase in the nearset matches the current row. ** Populate the pPhrase->poslist buffers at the same time. If any ** phrase is not a match, break out of the loop early. */ for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){ Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; | | | 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 | int i; /* Check that each phrase in the nearset matches the current row. ** Populate the pPhrase->poslist buffers at the same time. If any ** phrase is not a match, break out of the loop early. */ for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){ Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){ int bMatch = 0; rc = fts5ExprPhraseIsMatch(pNode, pNear->pColset, pPhrase, &bMatch); if( bMatch==0 ) break; }else{ rc = sqlite3Fts5IterPoslistBuffer( pPhrase->aTerm[0].pIter, &pPhrase->poslist ); |
︙ | ︙ | |||
981 982 983 984 985 986 987 | rc = sqlite3Fts5IndexQuery( pExpr->pIndex, p->zTerm, strlen(p->zTerm), (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) | (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0), &p->pIter ); assert( rc==SQLITE_OK || p->pIter==0 ); | | | 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 | rc = sqlite3Fts5IndexQuery( pExpr->pIndex, p->zTerm, strlen(p->zTerm), (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) | (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0), &p->pIter ); assert( rc==SQLITE_OK || p->pIter==0 ); if( p->pIter && 0==sqlite3Fts5IterEof(p->pIter) ){ bEof = 0; } } if( bEof ){ pNode->bEof = 1; return rc; |
︙ | ︙ |
Changes to ext/fts5/test/fts5synonym.test.
︙ | ︙ | |||
19 20 21 22 23 24 25 | ifcapable !fts5 { finish_test return } foreach S { {zero 0} | | | | | | | | | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | ifcapable !fts5 { finish_test return } foreach S { {zero 0} {one 1 i} {two 2 ii} {three 3 iii} {four 4 iv} {five 5 v} {six 6 vi} {seven 7 vii} {eight 8 viii} {nine 9 ix} } { foreach s $S { set o [list] foreach x $S {if {$x!=$s} {lappend o $x}} set ::syn($s) $o } } |
︙ | ︙ | |||
182 183 184 185 186 187 188 189 190 | INSERT INTO ft VALUES('one one two three'); CREATE VIRTUAL TABLE vv USING fts5vocab(ft, row); SELECT * FROM vv; } { one 1 4 three 1 2 two 1 2 } do_execsql_test 3.2.1 { SELECT rowid FROM ft WHERE ft MATCH 'one two three'; SELECT rowid FROM ft WHERE ft MATCH 'one + one + two + three'; | > > > > > | | > > | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | INSERT INTO ft VALUES('one one two three'); CREATE VIRTUAL TABLE vv USING fts5vocab(ft, row); SELECT * FROM vv; } { one 1 4 three 1 2 two 1 2 } do_execsql_test 3.2.1 { SELECT rowid FROM ft WHERE ft MATCH 'one'; } {1} do_execsql_test 3.2.2 { SELECT rowid FROM ft WHERE ft MATCH 'one two three'; } {1} do_execsql_test 3.2.3 { SELECT rowid FROM ft WHERE ft MATCH 'one + one + two + three'; } {1} do_execsql_test 3.2.4 { SELECT rowid FROM ft WHERE ft MATCH 'one two two three'; } {1} do_execsql_test 3.2.5 { SELECT rowid FROM ft WHERE ft MATCH 'one + two + two + three'; } {} #------------------------------------------------------------------------- # Check that expressions with synonyms can be parsed and executed. # reset_db sqlite3_fts5_create_tokenizer db tcl tcl_create proc tcl_tokenize {tflags text} { foreach {w iStart iEnd} [do_tokenize_split $text] { sqlite3_fts5_token $w $iStart $iEnd if {$tflags=="query" && [info exists ::syn($w)]} { foreach s $::syn($w) { sqlite3_fts5_token -colo $s $iStart $iEnd } } } } foreach {tn expr res} { 1 {abc} {"abc"} 2 {one} {"one"|"i"|"1"} 3 {3} {"3"|"iii"|"three"} 4 {3*} {"3"|"iii"|"three" *} } { do_execsql_test 4.1.$tn {SELECT fts5_expr($expr, 'tokenize=tcl')} [list $res] } do_execsql_test 4.2.1 { CREATE VIRTUAL TABLE xx USING fts5(x, tokenize=tcl); INSERT INTO xx VALUES('one two'); INSERT INTO xx VALUES('three four'); } do_execsql_test 4.2.2 { SELECT rowid FROM xx WHERE xx MATCH '2' } {1} do_execsql_test 4.2.3 { SELECT rowid FROM xx WHERE xx MATCH '3' } {2} do_test 5.0 { execsql { CREATE VIRTUAL TABLE t1 USING fts5(a, b, tokenize=tcl) } foreach {rowid a b} { 1 {four v 4 i three} {1 3 five five 4 one} 2 {5 1 3 4 i} {2 2 v two 4} 3 {5 i 5 2 four 4 1} {iii ii five two 1} 4 {ii four 4 one 5 three five} {one 5 1 iii 4 3} 5 {three i v i four 4 1} {ii five five five iii} 6 {4 2 ii two 2 iii} {three 1 four 4 iv 1 iv} 7 {ii ii two three 2 5} {iii i ii iii iii one one} 8 {2 ii i two 3 three 2} {two iv v iii 3 five} 9 {i 2 iv 3 five four v} {iii 4 three i three ii 1} } { execsql { INSERT INTO t1(rowid, a, b) VALUES($rowid, $a, $b) } } } {} foreach {tn q res} { 1 {one} { 1 {four v 4 [i] three} {[1] 3 five five 4 [one]} 2 {5 [1] 3 4 [i]} {2 2 v two 4} 3 {5 [i] 5 2 four 4 [1]} {iii ii five two [1]} 4 {ii four 4 [one] 5 three five} {[one] 5 [1] iii 4 3} 5 {three [i] v [i] four 4 [1]} {ii five five five iii} 6 {4 2 ii two 2 iii} {three [1] four 4 iv [1] iv} 7 {ii ii two three 2 5} {iii [i] ii iii iii [one] [one]} 8 {2 ii [i] two 3 three 2} {two iv v iii 3 five} 9 {[i] 2 iv 3 five four v} {iii 4 three [i] three ii [1]} } 2 {five four} { 1 {[four] [v] [4] i three} {1 3 [five] [five] [4] one} 2 {[5] 1 3 [4] i} {2 2 [v] two [4]} 3 {[5] i [5] 2 [four] [4] 1} {iii ii [five] two 1} 4 {ii [four] [4] one [5] three [five]} {one [5] 1 iii [4] 3} 5 {three i [v] i [four] [4] 1} {ii [five] [five] [five] iii} 8 {2 ii i two 3 three 2} {two [iv] [v] iii 3 [five]} 9 {i 2 [iv] 3 [five] [four] [v]} {iii [4] three i three ii 1} } } { do_execsql_test 5.1.$tn { SELECT rowid, highlight(t1, 0, '[', ']'), highlight(t1, 1, '[', ']') FROM t1 WHERE t1 MATCH $q } $res } finish_test |