sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / ext / fts5 / fts5_expr.c
| 1 | /* |
| 2 | ** 2014 May 31 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | */ |
| 14 | |
| 15 | |
| 16 | |
| 17 | #include "fts5Int.h" |
| 18 | #include "fts5parse.h" |
| 19 | |
| 20 | #ifndef SQLITE_FTS5_MAX_EXPR_DEPTH |
| 21 | # define SQLITE_FTS5_MAX_EXPR_DEPTH 256 |
| 22 | #endif |
| 23 | |
| 24 | /* |
| 25 | ** All token types in the generated fts5parse.h file are greater than 0. |
| 26 | */ |
| 27 | #define FTS5_EOF 0 |
| 28 | |
| 29 | #define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) |
| 30 | |
| 31 | typedef struct Fts5ExprTerm Fts5ExprTerm; |
| 32 | |
| 33 | /* |
| 34 | ** Functions generated by lemon from fts5parse.y. |
| 35 | */ |
| 36 | void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64)); |
| 37 | void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*)); |
| 38 | void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*); |
| 39 | #ifndef NDEBUG |
| 40 | #include <stdio.h> |
| 41 | void sqlite3Fts5ParserTrace(FILE*, char*); |
| 42 | #endif |
| 43 | int sqlite3Fts5ParserFallback(int); |
| 44 | |
| 45 | |
| 46 | struct Fts5Expr { |
| 47 | Fts5Index *pIndex; |
| 48 | Fts5Config *pConfig; |
| 49 | Fts5ExprNode *pRoot; |
| 50 | int bDesc; /* Iterate in descending rowid order */ |
| 51 | int nPhrase; /* Number of phrases in expression */ |
| 52 | Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */ |
| 53 | }; |
| 54 | |
| 55 | /* |
| 56 | ** eType: |
| 57 | ** Expression node type. Usually one of: |
| 58 | ** |
| 59 | ** FTS5_AND (nChild, apChild valid) |
| 60 | ** FTS5_OR (nChild, apChild valid) |
| 61 | ** FTS5_NOT (nChild, apChild valid) |
| 62 | ** FTS5_STRING (pNear valid) |
| 63 | ** FTS5_TERM (pNear valid) |
| 64 | ** |
| 65 | ** An expression node with eType==0 may also exist. It always matches zero |
| 66 | ** rows. This is created when a phrase containing no tokens is parsed. |
| 67 | ** e.g. "". |
| 68 | ** |
| 69 | ** iHeight: |
| 70 | ** Distance from this node to furthest leaf. This is always 0 for nodes |
| 71 | ** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one |
| 72 | ** greater than the largest child value. |
| 73 | */ |
| 74 | struct Fts5ExprNode { |
| 75 | int eType; /* Node type */ |
| 76 | int bEof; /* True at EOF */ |
| 77 | int bNomatch; /* True if entry is not a match */ |
| 78 | int iHeight; /* Distance to tree leaf nodes */ |
| 79 | |
| 80 | /* Next method for this node. */ |
| 81 | int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64); |
| 82 | |
| 83 | i64 iRowid; /* Current rowid */ |
| 84 | Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */ |
| 85 | |
| 86 | /* Child nodes. For a NOT node, this array always contains 2 entries. For |
| 87 | ** AND or OR nodes, it contains 2 or more entries. */ |
| 88 | int nChild; /* Number of child nodes */ |
| 89 | Fts5ExprNode *apChild[FLEXARRAY]; /* Array of child nodes */ |
| 90 | }; |
| 91 | |
| 92 | /* Size (in bytes) of an Fts5ExprNode object that holds up to N children */ |
| 93 | #define SZ_FTS5EXPRNODE(N) \ |
| 94 | (offsetof(Fts5ExprNode,apChild) + (N)*sizeof(Fts5ExprNode*)) |
| 95 | |
| 96 | #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING) |
| 97 | |
| 98 | /* |
| 99 | ** Invoke the xNext method of an Fts5ExprNode object. This macro should be |
| 100 | ** used as if it has the same signature as the xNext() methods themselves. |
| 101 | */ |
| 102 | #define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d)) |
| 103 | |
| 104 | /* |
| 105 | ** An instance of the following structure represents a single search term |
| 106 | ** or term prefix. |
| 107 | */ |
| 108 | struct Fts5ExprTerm { |
| 109 | u8 bPrefix; /* True for a prefix term */ |
| 110 | u8 bFirst; /* True if token must be first in column */ |
| 111 | char *pTerm; /* Term data */ |
| 112 | int nQueryTerm; /* Effective size of term in bytes */ |
| 113 | int nFullTerm; /* Size of term in bytes incl. tokendata */ |
| 114 | Fts5IndexIter *pIter; /* Iterator for this term */ |
| 115 | Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */ |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | ** A phrase. One or more terms that must appear in a contiguous sequence |
| 120 | ** within a document for it to match. |
| 121 | */ |
| 122 | struct Fts5ExprPhrase { |
| 123 | Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */ |
| 124 | Fts5Buffer poslist; /* Current position list */ |
| 125 | int nTerm; /* Number of entries in aTerm[] */ |
| 126 | Fts5ExprTerm aTerm[FLEXARRAY]; /* Terms that make up this phrase */ |
| 127 | }; |
| 128 | |
| 129 | /* Size (in bytes) of an Fts5ExprPhrase object that holds up to N terms */ |
| 130 | #define SZ_FTS5EXPRPHRASE(N) \ |
| 131 | (offsetof(Fts5ExprPhrase,aTerm) + (N)*sizeof(Fts5ExprTerm)) |
| 132 | |
| 133 | /* |
| 134 | ** One or more phrases that must appear within a certain token distance of |
| 135 | ** each other within each matching document. |
| 136 | */ |
| 137 | struct Fts5ExprNearset { |
| 138 | int nNear; /* NEAR parameter */ |
| 139 | Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */ |
| 140 | int nPhrase; /* Number of entries in aPhrase[] array */ |
| 141 | Fts5ExprPhrase *apPhrase[FLEXARRAY]; /* Array of phrase pointers */ |
| 142 | }; |
| 143 | |
| 144 | /* Size (in bytes) of an Fts5ExprNearset object covering up to N phrases */ |
| 145 | #define SZ_FTS5EXPRNEARSET(N) \ |
| 146 | (offsetof(Fts5ExprNearset,apPhrase)+(N)*sizeof(Fts5ExprPhrase*)) |
| 147 | |
| 148 | /* |
| 149 | ** Parse context. |
| 150 | */ |
| 151 | struct Fts5Parse { |
| 152 | Fts5Config *pConfig; |
| 153 | char *zErr; |
| 154 | int rc; |
| 155 | int nPhrase; /* Size of apPhrase array */ |
| 156 | Fts5ExprPhrase **apPhrase; /* Array of all phrases */ |
| 157 | Fts5ExprNode *pExpr; /* Result of a successful parse */ |
| 158 | int bPhraseToAnd; /* Convert "a+b" to "a AND b" */ |
| 159 | }; |
| 160 | |
| 161 | /* |
| 162 | ** Check that the Fts5ExprNode.iHeight variables are set correctly in |
| 163 | ** the expression tree passed as the only argument. |
| 164 | */ |
| 165 | #ifndef NDEBUG |
| 166 | static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){ |
| 167 | if( rc==SQLITE_OK ){ |
| 168 | if( p->eType==FTS5_TERM || p->eType==FTS5_STRING || p->eType==0 ){ |
| 169 | assert( p->iHeight==0 ); |
| 170 | }else{ |
| 171 | int ii; |
| 172 | int iMaxChild = 0; |
| 173 | for(ii=0; ii<p->nChild; ii++){ |
| 174 | Fts5ExprNode *pChild = p->apChild[ii]; |
| 175 | iMaxChild = MAX(iMaxChild, pChild->iHeight); |
| 176 | assert_expr_depth_ok(SQLITE_OK, pChild); |
| 177 | } |
| 178 | assert( p->iHeight==iMaxChild+1 ); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | #else |
| 183 | # define assert_expr_depth_ok(rc, p) |
| 184 | #endif |
| 185 | |
| 186 | void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){ |
| 187 | va_list ap; |
| 188 | va_start(ap, zFmt); |
| 189 | if( pParse->rc==SQLITE_OK ){ |
| 190 | assert( pParse->zErr==0 ); |
| 191 | pParse->zErr = sqlite3_vmprintf(zFmt, ap); |
| 192 | pParse->rc = SQLITE_ERROR; |
| 193 | } |
| 194 | va_end(ap); |
| 195 | } |
| 196 | |
| 197 | static int fts5ExprIsspace(char t){ |
| 198 | return t==' ' || t=='\t' || t=='\n' || t=='\r'; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | ** Read the first token from the nul-terminated string at *pz. |
| 203 | */ |
| 204 | static int fts5ExprGetToken( |
| 205 | Fts5Parse *pParse, |
| 206 | const char **pz, /* IN/OUT: Pointer into buffer */ |
| 207 | Fts5Token *pToken |
| 208 | ){ |
| 209 | const char *z = *pz; |
| 210 | int tok; |
| 211 | |
| 212 | /* Skip past any whitespace */ |
| 213 | while( fts5ExprIsspace(*z) ) z++; |
| 214 | |
| 215 | pToken->p = z; |
| 216 | pToken->n = 1; |
| 217 | switch( *z ){ |
| 218 | case '(': tok = FTS5_LP; break; |
| 219 | case ')': tok = FTS5_RP; break; |
| 220 | case '{': tok = FTS5_LCP; break; |
| 221 | case '}': tok = FTS5_RCP; break; |
| 222 | case ':': tok = FTS5_COLON; break; |
| 223 | case ',': tok = FTS5_COMMA; break; |
| 224 | case '+': tok = FTS5_PLUS; break; |
| 225 | case '*': tok = FTS5_STAR; break; |
| 226 | case '-': tok = FTS5_MINUS; break; |
| 227 | case '^': tok = FTS5_CARET; break; |
| 228 | case '\0': tok = FTS5_EOF; break; |
| 229 | |
| 230 | case '"': { |
| 231 | const char *z2; |
| 232 | tok = FTS5_STRING; |
| 233 | |
| 234 | for(z2=&z[1]; 1; z2++){ |
| 235 | if( z2[0]=='"' ){ |
| 236 | z2++; |
| 237 | if( z2[0]!='"' ) break; |
| 238 | } |
| 239 | if( z2[0]=='\0' ){ |
| 240 | sqlite3Fts5ParseError(pParse, "unterminated string"); |
| 241 | return FTS5_EOF; |
| 242 | } |
| 243 | } |
| 244 | pToken->n = (z2 - z); |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | default: { |
| 249 | const char *z2; |
| 250 | if( sqlite3Fts5IsBareword(z[0])==0 ){ |
| 251 | sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z); |
| 252 | return FTS5_EOF; |
| 253 | } |
| 254 | tok = FTS5_STRING; |
| 255 | for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++); |
| 256 | pToken->n = (z2 - z); |
| 257 | if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR; |
| 258 | if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT; |
| 259 | if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | *pz = &pToken->p[pToken->n]; |
| 265 | return tok; |
| 266 | } |
| 267 | |
| 268 | static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);} |
| 269 | static void fts5ParseFree(void *p){ sqlite3_free(p); } |
| 270 | |
| 271 | int sqlite3Fts5ExprNew( |
| 272 | Fts5Config *pConfig, /* FTS5 Configuration */ |
| 273 | int bPhraseToAnd, |
| 274 | int iCol, |
| 275 | const char *zExpr, /* Expression text */ |
| 276 | Fts5Expr **ppNew, |
| 277 | char **pzErr |
| 278 | ){ |
| 279 | Fts5Parse sParse; |
| 280 | Fts5Token token; |
| 281 | const char *z = zExpr; |
| 282 | int t; /* Next token type */ |
| 283 | void *pEngine; |
| 284 | Fts5Expr *pNew; |
| 285 | |
| 286 | *ppNew = 0; |
| 287 | *pzErr = 0; |
| 288 | memset(&sParse, 0, sizeof(sParse)); |
| 289 | sParse.bPhraseToAnd = bPhraseToAnd; |
| 290 | pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc); |
| 291 | if( pEngine==0 ){ return SQLITE_NOMEM; } |
| 292 | sParse.pConfig = pConfig; |
| 293 | |
| 294 | do { |
| 295 | t = fts5ExprGetToken(&sParse, &z, &token); |
| 296 | sqlite3Fts5Parser(pEngine, t, token, &sParse); |
| 297 | }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF ); |
| 298 | sqlite3Fts5ParserFree(pEngine, fts5ParseFree); |
| 299 | |
| 300 | assert( sParse.pExpr || sParse.rc!=SQLITE_OK ); |
| 301 | assert_expr_depth_ok(sParse.rc, sParse.pExpr); |
| 302 | |
| 303 | /* If the LHS of the MATCH expression was a user column, apply the |
| 304 | ** implicit column-filter. */ |
| 305 | if( sParse.rc==SQLITE_OK && iCol<pConfig->nCol ){ |
| 306 | int n = SZ_FTS5COLSET(1); |
| 307 | Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n); |
| 308 | if( pColset ){ |
| 309 | pColset->nCol = 1; |
| 310 | pColset->aiCol[0] = iCol; |
| 311 | sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 ); |
| 316 | if( sParse.rc==SQLITE_OK ){ |
| 317 | *ppNew = pNew = sqlite3_malloc64(sizeof(Fts5Expr)); |
| 318 | if( pNew==0 ){ |
| 319 | sParse.rc = SQLITE_NOMEM; |
| 320 | sqlite3Fts5ParseNodeFree(sParse.pExpr); |
| 321 | }else{ |
| 322 | pNew->pRoot = sParse.pExpr; |
| 323 | pNew->pIndex = 0; |
| 324 | pNew->pConfig = pConfig; |
| 325 | pNew->apExprPhrase = sParse.apPhrase; |
| 326 | pNew->nPhrase = sParse.nPhrase; |
| 327 | pNew->bDesc = 0; |
| 328 | sParse.apPhrase = 0; |
| 329 | } |
| 330 | }else{ |
| 331 | sqlite3Fts5ParseNodeFree(sParse.pExpr); |
| 332 | } |
| 333 | |
| 334 | sqlite3_free(sParse.apPhrase); |
| 335 | if( 0==*pzErr ){ |
| 336 | *pzErr = sParse.zErr; |
| 337 | }else{ |
| 338 | sqlite3_free(sParse.zErr); |
| 339 | } |
| 340 | return sParse.rc; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | ** Assuming that buffer z is at least nByte bytes in size and contains a |
| 345 | ** valid utf-8 string, return the number of characters in the string. |
| 346 | */ |
| 347 | static int fts5ExprCountChar(const char *z, int nByte){ |
| 348 | int nRet = 0; |
| 349 | int ii; |
| 350 | for(ii=0; ii<nByte; ii++){ |
| 351 | if( (z[ii] & 0xC0)!=0x80 ) nRet++; |
| 352 | } |
| 353 | return nRet; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | ** This function is only called when using the special 'trigram' tokenizer. |
| 358 | ** Argument zText contains the text of a LIKE or GLOB pattern matched |
| 359 | ** against column iCol. This function creates and compiles an FTS5 MATCH |
| 360 | ** expression that will match a superset of the rows matched by the LIKE or |
| 361 | ** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error |
| 362 | ** code. |
| 363 | */ |
| 364 | int sqlite3Fts5ExprPattern( |
| 365 | Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp |
| 366 | ){ |
| 367 | i64 nText = strlen(zText); |
| 368 | char *zExpr = (char*)sqlite3_malloc64(nText*4 + 1); |
| 369 | int rc = SQLITE_OK; |
| 370 | |
| 371 | if( zExpr==0 ){ |
| 372 | rc = SQLITE_NOMEM; |
| 373 | }else{ |
| 374 | char aSpec[3]; |
| 375 | int iOut = 0; |
| 376 | int i = 0; |
| 377 | int iFirst = 0; |
| 378 | |
| 379 | if( bGlob==0 ){ |
| 380 | aSpec[0] = '_'; |
| 381 | aSpec[1] = '%'; |
| 382 | aSpec[2] = 0; |
| 383 | }else{ |
| 384 | aSpec[0] = '*'; |
| 385 | aSpec[1] = '?'; |
| 386 | aSpec[2] = '['; |
| 387 | } |
| 388 | |
| 389 | while( i<=nText ){ |
| 390 | if( i==nText |
| 391 | || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2] |
| 392 | ){ |
| 393 | |
| 394 | if( fts5ExprCountChar(&zText[iFirst], i-iFirst)>=3 ){ |
| 395 | int jj; |
| 396 | zExpr[iOut++] = '"'; |
| 397 | for(jj=iFirst; jj<i; jj++){ |
| 398 | zExpr[iOut++] = zText[jj]; |
| 399 | if( zText[jj]=='"' ) zExpr[iOut++] = '"'; |
| 400 | } |
| 401 | zExpr[iOut++] = '"'; |
| 402 | zExpr[iOut++] = ' '; |
| 403 | } |
| 404 | if( i<nText && zText[i]==aSpec[2] ){ |
| 405 | i += 2; |
| 406 | if( zText[i-1]=='^' ) i++; |
| 407 | while( i<nText && zText[i]!=']' ) i++; |
| 408 | } |
| 409 | iFirst = i+1; |
| 410 | } |
| 411 | i++; |
| 412 | } |
| 413 | if( iOut>0 ){ |
| 414 | int bAnd = 0; |
| 415 | if( pConfig->eDetail!=FTS5_DETAIL_FULL ){ |
| 416 | bAnd = 1; |
| 417 | if( pConfig->eDetail==FTS5_DETAIL_NONE ){ |
| 418 | iCol = pConfig->nCol; |
| 419 | } |
| 420 | } |
| 421 | zExpr[iOut] = '\0'; |
| 422 | rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg); |
| 423 | }else{ |
| 424 | *pp = 0; |
| 425 | } |
| 426 | sqlite3_free(zExpr); |
| 427 | } |
| 428 | |
| 429 | return rc; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | ** Free the expression node object passed as the only argument. |
| 434 | */ |
| 435 | void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){ |
| 436 | if( p ){ |
| 437 | int i; |
| 438 | for(i=0; i<p->nChild; i++){ |
| 439 | sqlite3Fts5ParseNodeFree(p->apChild[i]); |
| 440 | } |
| 441 | sqlite3Fts5ParseNearsetFree(p->pNear); |
| 442 | sqlite3_free(p); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | ** Free the expression object passed as the only argument. |
| 448 | */ |
| 449 | void sqlite3Fts5ExprFree(Fts5Expr *p){ |
| 450 | if( p ){ |
| 451 | sqlite3Fts5ParseNodeFree(p->pRoot); |
| 452 | sqlite3_free(p->apExprPhrase); |
| 453 | sqlite3_free(p); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){ |
| 458 | Fts5Parse sParse; |
| 459 | memset(&sParse, 0, sizeof(sParse)); |
| 460 | |
| 461 | if( *pp1 && p2 ){ |
| 462 | Fts5Expr *p1 = *pp1; |
| 463 | int nPhrase = p1->nPhrase + p2->nPhrase; |
| 464 | |
| 465 | p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0); |
| 466 | p2->pRoot = 0; |
| 467 | |
| 468 | if( sParse.rc==SQLITE_OK ){ |
| 469 | Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc64( |
| 470 | p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*) |
| 471 | ); |
| 472 | if( ap==0 ){ |
| 473 | sParse.rc = SQLITE_NOMEM; |
| 474 | }else{ |
| 475 | int i; |
| 476 | memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*)); |
| 477 | for(i=0; i<p2->nPhrase; i++){ |
| 478 | ap[i] = p2->apExprPhrase[i]; |
| 479 | } |
| 480 | p1->nPhrase = nPhrase; |
| 481 | p1->apExprPhrase = ap; |
| 482 | } |
| 483 | } |
| 484 | sqlite3_free(p2->apExprPhrase); |
| 485 | sqlite3_free(p2); |
| 486 | }else if( p2 ){ |
| 487 | *pp1 = p2; |
| 488 | } |
| 489 | |
| 490 | return sParse.rc; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | ** Argument pTerm must be a synonym iterator. Return the current rowid |
| 495 | ** that it points to. |
| 496 | */ |
| 497 | static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){ |
| 498 | i64 iRet = 0; |
| 499 | int bRetValid = 0; |
| 500 | Fts5ExprTerm *p; |
| 501 | |
| 502 | assert( pTerm ); |
| 503 | assert( pTerm->pSynonym ); |
| 504 | assert( bDesc==0 || bDesc==1 ); |
| 505 | for(p=pTerm; p; p=p->pSynonym){ |
| 506 | if( 0==sqlite3Fts5IterEof(p->pIter) ){ |
| 507 | i64 iRowid = p->pIter->iRowid; |
| 508 | if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){ |
| 509 | iRet = iRowid; |
| 510 | bRetValid = 1; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | if( pbEof && bRetValid==0 ) *pbEof = 1; |
| 516 | return iRet; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | ** Argument pTerm must be a synonym iterator. |
| 521 | */ |
| 522 | static int fts5ExprSynonymList( |
| 523 | Fts5ExprTerm *pTerm, |
| 524 | i64 iRowid, |
| 525 | Fts5Buffer *pBuf, /* Use this buffer for space if required */ |
| 526 | u8 **pa, int *pn |
| 527 | ){ |
| 528 | Fts5PoslistReader aStatic[4]; |
| 529 | Fts5PoslistReader *aIter = aStatic; |
| 530 | int nIter = 0; |
| 531 | int nAlloc = 4; |
| 532 | int rc = SQLITE_OK; |
| 533 | Fts5ExprTerm *p; |
| 534 | |
| 535 | assert( pTerm->pSynonym ); |
| 536 | for(p=pTerm; p; p=p->pSynonym){ |
| 537 | Fts5IndexIter *pIter = p->pIter; |
| 538 | if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){ |
| 539 | if( pIter->nData==0 ) continue; |
| 540 | if( nIter==nAlloc ){ |
| 541 | sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2; |
| 542 | Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte); |
| 543 | if( aNew==0 ){ |
| 544 | rc = SQLITE_NOMEM; |
| 545 | goto synonym_poslist_out; |
| 546 | } |
| 547 | memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter); |
| 548 | nAlloc = nAlloc*2; |
| 549 | if( aIter!=aStatic ) sqlite3_free(aIter); |
| 550 | aIter = aNew; |
| 551 | } |
| 552 | sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]); |
| 553 | assert( aIter[nIter].bEof==0 ); |
| 554 | nIter++; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if( nIter==1 ){ |
| 559 | *pa = (u8*)aIter[0].a; |
| 560 | *pn = aIter[0].n; |
| 561 | }else{ |
| 562 | Fts5PoslistWriter writer = {0}; |
| 563 | i64 iPrev = -1; |
| 564 | fts5BufferZero(pBuf); |
| 565 | while( 1 ){ |
| 566 | int i; |
| 567 | i64 iMin = FTS5_LARGEST_INT64; |
| 568 | for(i=0; i<nIter; i++){ |
| 569 | if( aIter[i].bEof==0 ){ |
| 570 | if( aIter[i].iPos==iPrev ){ |
| 571 | if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue; |
| 572 | } |
| 573 | if( aIter[i].iPos<iMin ){ |
| 574 | iMin = aIter[i].iPos; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break; |
| 579 | rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin); |
| 580 | iPrev = iMin; |
| 581 | } |
| 582 | if( rc==SQLITE_OK ){ |
| 583 | *pa = pBuf->p; |
| 584 | *pn = pBuf->n; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | synonym_poslist_out: |
| 589 | if( aIter!=aStatic ) sqlite3_free(aIter); |
| 590 | return rc; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | /* |
| 595 | ** All individual term iterators in pPhrase are guaranteed to be valid and |
| 596 | ** pointing to the same rowid when this function is called. This function |
| 597 | ** checks if the current rowid really is a match, and if so populates |
| 598 | ** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch |
| 599 | ** is set to true if this is really a match, or false otherwise. |
| 600 | ** |
| 601 | ** SQLITE_OK is returned if an error occurs, or an SQLite error code |
| 602 | ** otherwise. It is not considered an error code if the current rowid is |
| 603 | ** not a match. |
| 604 | */ |
| 605 | static int fts5ExprPhraseIsMatch( |
| 606 | Fts5ExprNode *pNode, /* Node pPhrase belongs to */ |
| 607 | Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */ |
| 608 | int *pbMatch /* OUT: Set to true if really a match */ |
| 609 | ){ |
| 610 | Fts5PoslistWriter writer = {0}; |
| 611 | Fts5PoslistReader aStatic[4]; |
| 612 | Fts5PoslistReader *aIter = aStatic; |
| 613 | int i; |
| 614 | int rc = SQLITE_OK; |
| 615 | int bFirst = pPhrase->aTerm[0].bFirst; |
| 616 | |
| 617 | fts5BufferZero(&pPhrase->poslist); |
| 618 | |
| 619 | /* If the aStatic[] array is not large enough, allocate a large array |
| 620 | ** using sqlite3_malloc(). This approach could be improved upon. */ |
| 621 | if( pPhrase->nTerm>ArraySize(aStatic) ){ |
| 622 | sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm; |
| 623 | aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte); |
| 624 | if( !aIter ) return SQLITE_NOMEM; |
| 625 | } |
| 626 | memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm); |
| 627 | |
| 628 | /* Initialize a term iterator for each term in the phrase */ |
| 629 | for(i=0; i<pPhrase->nTerm; i++){ |
| 630 | Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; |
| 631 | int n = 0; |
| 632 | int bFlag = 0; |
| 633 | u8 *a = 0; |
| 634 | if( pTerm->pSynonym ){ |
| 635 | Fts5Buffer buf = {0, 0, 0}; |
| 636 | rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n); |
| 637 | if( rc ){ |
| 638 | sqlite3_free(a); |
| 639 | goto ismatch_out; |
| 640 | } |
| 641 | if( a==buf.p ) bFlag = 1; |
| 642 | }else{ |
| 643 | a = (u8*)pTerm->pIter->pData; |
| 644 | n = pTerm->pIter->nData; |
| 645 | } |
| 646 | sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]); |
| 647 | aIter[i].bFlag = (u8)bFlag; |
| 648 | if( aIter[i].bEof ) goto ismatch_out; |
| 649 | } |
| 650 | |
| 651 | while( 1 ){ |
| 652 | int bMatch; |
| 653 | i64 iPos = aIter[0].iPos; |
| 654 | do { |
| 655 | bMatch = 1; |
| 656 | for(i=0; i<pPhrase->nTerm; i++){ |
| 657 | Fts5PoslistReader *pPos = &aIter[i]; |
| 658 | i64 iAdj = iPos + i; |
| 659 | if( pPos->iPos!=iAdj ){ |
| 660 | bMatch = 0; |
| 661 | while( pPos->iPos<iAdj ){ |
| 662 | if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out; |
| 663 | } |
| 664 | if( pPos->iPos>iAdj ) iPos = pPos->iPos-i; |
| 665 | } |
| 666 | } |
| 667 | }while( bMatch==0 ); |
| 668 | |
| 669 | /* Append position iPos to the output */ |
| 670 | if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){ |
| 671 | rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos); |
| 672 | if( rc!=SQLITE_OK ) goto ismatch_out; |
| 673 | } |
| 674 | |
| 675 | for(i=0; i<pPhrase->nTerm; i++){ |
| 676 | if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | ismatch_out: |
| 681 | *pbMatch = (pPhrase->poslist.n>0); |
| 682 | for(i=0; i<pPhrase->nTerm; i++){ |
| 683 | if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a); |
| 684 | } |
| 685 | if( aIter!=aStatic ) sqlite3_free(aIter); |
| 686 | return rc; |
| 687 | } |
| 688 | |
| 689 | typedef struct Fts5LookaheadReader Fts5LookaheadReader; |
| 690 | struct Fts5LookaheadReader { |
| 691 | const u8 *a; /* Buffer containing position list */ |
| 692 | int n; /* Size of buffer a[] in bytes */ |
| 693 | int i; /* Current offset in position list */ |
| 694 | i64 iPos; /* Current position */ |
| 695 | i64 iLookahead; /* Next position */ |
| 696 | }; |
| 697 | |
| 698 | #define FTS5_LOOKAHEAD_EOF (((i64)1) << 62) |
| 699 | |
| 700 | static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){ |
| 701 | p->iPos = p->iLookahead; |
| 702 | if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){ |
| 703 | p->iLookahead = FTS5_LOOKAHEAD_EOF; |
| 704 | } |
| 705 | return (p->iPos==FTS5_LOOKAHEAD_EOF); |
| 706 | } |
| 707 | |
| 708 | static int fts5LookaheadReaderInit( |
| 709 | const u8 *a, int n, /* Buffer to read position list from */ |
| 710 | Fts5LookaheadReader *p /* Iterator object to initialize */ |
| 711 | ){ |
| 712 | memset(p, 0, sizeof(Fts5LookaheadReader)); |
| 713 | p->a = a; |
| 714 | p->n = n; |
| 715 | fts5LookaheadReaderNext(p); |
| 716 | return fts5LookaheadReaderNext(p); |
| 717 | } |
| 718 | |
| 719 | typedef struct Fts5NearTrimmer Fts5NearTrimmer; |
| 720 | struct Fts5NearTrimmer { |
| 721 | Fts5LookaheadReader reader; /* Input iterator */ |
| 722 | Fts5PoslistWriter writer; /* Writer context */ |
| 723 | Fts5Buffer *pOut; /* Output poslist */ |
| 724 | }; |
| 725 | |
| 726 | /* |
| 727 | ** The near-set object passed as the first argument contains more than |
| 728 | ** one phrase. All phrases currently point to the same row. The |
| 729 | ** Fts5ExprPhrase.poslist buffers are populated accordingly. This function |
| 730 | ** tests if the current row contains instances of each phrase sufficiently |
| 731 | ** close together to meet the NEAR constraint. Non-zero is returned if it |
| 732 | ** does, or zero otherwise. |
| 733 | ** |
| 734 | ** If in/out parameter (*pRc) is set to other than SQLITE_OK when this |
| 735 | ** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM) |
| 736 | ** occurs within this function (*pRc) is set accordingly before returning. |
| 737 | ** The return value is undefined in both these cases. |
| 738 | ** |
| 739 | ** If no error occurs and non-zero (a match) is returned, the position-list |
| 740 | ** of each phrase object is edited to contain only those entries that |
| 741 | ** meet the constraint before returning. |
| 742 | */ |
| 743 | static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){ |
| 744 | Fts5NearTrimmer aStatic[4]; |
| 745 | Fts5NearTrimmer *a = aStatic; |
| 746 | Fts5ExprPhrase **apPhrase = pNear->apPhrase; |
| 747 | |
| 748 | int i; |
| 749 | int rc = *pRc; |
| 750 | int bMatch; |
| 751 | |
| 752 | assert( pNear->nPhrase>1 ); |
| 753 | |
| 754 | /* If the aStatic[] array is not large enough, allocate a large array |
| 755 | ** using sqlite3_malloc(). This approach could be improved upon. */ |
| 756 | if( pNear->nPhrase>ArraySize(aStatic) ){ |
| 757 | sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase; |
| 758 | a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte); |
| 759 | }else{ |
| 760 | memset(aStatic, 0, sizeof(aStatic)); |
| 761 | } |
| 762 | if( rc!=SQLITE_OK ){ |
| 763 | *pRc = rc; |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | /* Initialize a lookahead iterator for each phrase. After passing the |
| 768 | ** buffer and buffer size to the lookaside-reader init function, zero |
| 769 | ** the phrase poslist buffer. The new poslist for the phrase (containing |
| 770 | ** the same entries as the original with some entries removed on account |
| 771 | ** of the NEAR constraint) is written over the original even as it is |
| 772 | ** being read. This is safe as the entries for the new poslist are a |
| 773 | ** subset of the old, so it is not possible for data yet to be read to |
| 774 | ** be overwritten. */ |
| 775 | for(i=0; i<pNear->nPhrase; i++){ |
| 776 | Fts5Buffer *pPoslist = &apPhrase[i]->poslist; |
| 777 | fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader); |
| 778 | pPoslist->n = 0; |
| 779 | a[i].pOut = pPoslist; |
| 780 | } |
| 781 | |
| 782 | while( 1 ){ |
| 783 | int iAdv; |
| 784 | i64 iMin; |
| 785 | i64 iMax; |
| 786 | |
| 787 | /* This block advances the phrase iterators until they point to a set of |
| 788 | ** entries that together comprise a match. */ |
| 789 | iMax = a[0].reader.iPos; |
| 790 | do { |
| 791 | bMatch = 1; |
| 792 | for(i=0; i<pNear->nPhrase; i++){ |
| 793 | Fts5LookaheadReader *pPos = &a[i].reader; |
| 794 | iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear; |
| 795 | if( pPos->iPos<iMin || pPos->iPos>iMax ){ |
| 796 | bMatch = 0; |
| 797 | while( pPos->iPos<iMin ){ |
| 798 | if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out; |
| 799 | } |
| 800 | if( pPos->iPos>iMax ) iMax = pPos->iPos; |
| 801 | } |
| 802 | } |
| 803 | }while( bMatch==0 ); |
| 804 | |
| 805 | /* Add an entry to each output position list */ |
| 806 | for(i=0; i<pNear->nPhrase; i++){ |
| 807 | i64 iPos = a[i].reader.iPos; |
| 808 | Fts5PoslistWriter *pWriter = &a[i].writer; |
| 809 | if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){ |
| 810 | sqlite3Fts5PoslistSafeAppend(a[i].pOut, &pWriter->iPrev, iPos); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | iAdv = 0; |
| 815 | iMin = a[0].reader.iLookahead; |
| 816 | for(i=0; i<pNear->nPhrase; i++){ |
| 817 | if( a[i].reader.iLookahead < iMin ){ |
| 818 | iMin = a[i].reader.iLookahead; |
| 819 | iAdv = i; |
| 820 | } |
| 821 | } |
| 822 | if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out; |
| 823 | } |
| 824 | |
| 825 | ismatch_out: { |
| 826 | int bRet = a[0].pOut->n>0; |
| 827 | *pRc = rc; |
| 828 | if( a!=aStatic ) sqlite3_free(a); |
| 829 | return bRet; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | ** Advance iterator pIter until it points to a value equal to or laster |
| 835 | ** than the initial value of *piLast. If this means the iterator points |
| 836 | ** to a value laster than *piLast, update *piLast to the new lastest value. |
| 837 | ** |
| 838 | ** If the iterator reaches EOF, set *pbEof to true before returning. If |
| 839 | ** an error occurs, set *pRc to an error code. If either *pbEof or *pRc |
| 840 | ** are set, return a non-zero value. Otherwise, return zero. |
| 841 | */ |
| 842 | static int fts5ExprAdvanceto( |
| 843 | Fts5IndexIter *pIter, /* Iterator to advance */ |
| 844 | int bDesc, /* True if iterator is "rowid DESC" */ |
| 845 | i64 *piLast, /* IN/OUT: Lastest rowid seen so far */ |
| 846 | int *pRc, /* OUT: Error code */ |
| 847 | int *pbEof /* OUT: Set to true if EOF */ |
| 848 | ){ |
| 849 | i64 iLast = *piLast; |
| 850 | i64 iRowid; |
| 851 | |
| 852 | iRowid = pIter->iRowid; |
| 853 | if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){ |
| 854 | int rc = sqlite3Fts5IterNextFrom(pIter, iLast); |
| 855 | if( rc || sqlite3Fts5IterEof(pIter) ){ |
| 856 | *pRc = rc; |
| 857 | *pbEof = 1; |
| 858 | return 1; |
| 859 | } |
| 860 | iRowid = pIter->iRowid; |
| 861 | assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) ); |
| 862 | } |
| 863 | *piLast = iRowid; |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static int fts5ExprSynonymAdvanceto( |
| 869 | Fts5ExprTerm *pTerm, /* Term iterator to advance */ |
| 870 | int bDesc, /* True if iterator is "rowid DESC" */ |
| 871 | i64 *piLast, /* IN/OUT: Lastest rowid seen so far */ |
| 872 | int *pRc /* OUT: Error code */ |
| 873 | ){ |
| 874 | int rc = SQLITE_OK; |
| 875 | i64 iLast = *piLast; |
| 876 | Fts5ExprTerm *p; |
| 877 | int bEof = 0; |
| 878 | |
| 879 | for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){ |
| 880 | if( sqlite3Fts5IterEof(p->pIter)==0 ){ |
| 881 | i64 iRowid = p->pIter->iRowid; |
| 882 | if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){ |
| 883 | rc = sqlite3Fts5IterNextFrom(p->pIter, iLast); |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | if( rc!=SQLITE_OK ){ |
| 889 | *pRc = rc; |
| 890 | bEof = 1; |
| 891 | }else{ |
| 892 | *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof); |
| 893 | } |
| 894 | return bEof; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | static int fts5ExprNearTest( |
| 899 | int *pRc, |
| 900 | Fts5Expr *pExpr, /* Expression that pNear is a part of */ |
| 901 | Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */ |
| 902 | ){ |
| 903 | Fts5ExprNearset *pNear = pNode->pNear; |
| 904 | int rc = *pRc; |
| 905 | |
| 906 | if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){ |
| 907 | Fts5ExprTerm *pTerm; |
| 908 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[0]; |
| 909 | pPhrase->poslist.n = 0; |
| 910 | for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){ |
| 911 | Fts5IndexIter *pIter = pTerm->pIter; |
| 912 | if( sqlite3Fts5IterEof(pIter)==0 ){ |
| 913 | if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){ |
| 914 | pPhrase->poslist.n = 1; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | return pPhrase->poslist.n; |
| 919 | }else{ |
| 920 | int i; |
| 921 | |
| 922 | /* Check that each phrase in the nearset matches the current row. |
| 923 | ** Populate the pPhrase->poslist buffers at the same time. If any |
| 924 | ** phrase is not a match, break out of the loop early. */ |
| 925 | for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){ |
| 926 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
| 927 | if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym |
| 928 | || pNear->pColset || pPhrase->aTerm[0].bFirst |
| 929 | ){ |
| 930 | int bMatch = 0; |
| 931 | rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch); |
| 932 | if( bMatch==0 ) break; |
| 933 | }else{ |
| 934 | Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter; |
| 935 | fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | *pRc = rc; |
| 940 | if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){ |
| 941 | return 1; |
| 942 | } |
| 943 | |
| 944 | /* Not a match. Clear the position lists of all phrases, as some may |
| 945 | ** have been populated before the loop above exited early, and others |
| 946 | ** may still contain data left over from a previous row. */ |
| 947 | for(i=0; i<pNear->nPhrase; i++){ |
| 948 | pNear->apPhrase[i]->poslist.n = 0; |
| 949 | } |
| 950 | return 0; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | |
| 955 | /* |
| 956 | ** Initialize all term iterators in the pNear object. If any term is found |
| 957 | ** to match no documents at all, return immediately without initializing any |
| 958 | ** further iterators. |
| 959 | ** |
| 960 | ** If an error occurs, return an SQLite error code. Otherwise, return |
| 961 | ** SQLITE_OK. It is not considered an error if some term matches zero |
| 962 | ** documents. |
| 963 | */ |
| 964 | static int fts5ExprNearInitAll( |
| 965 | Fts5Expr *pExpr, |
| 966 | Fts5ExprNode *pNode |
| 967 | ){ |
| 968 | Fts5ExprNearset *pNear = pNode->pNear; |
| 969 | int i; |
| 970 | |
| 971 | assert( pNode->bNomatch==0 ); |
| 972 | for(i=0; i<pNear->nPhrase; i++){ |
| 973 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
| 974 | if( pPhrase->nTerm==0 ){ |
| 975 | pNode->bEof = 1; |
| 976 | return SQLITE_OK; |
| 977 | }else{ |
| 978 | int j; |
| 979 | for(j=0; j<pPhrase->nTerm; j++){ |
| 980 | Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; |
| 981 | Fts5ExprTerm *p; |
| 982 | int bHit = 0; |
| 983 | |
| 984 | for(p=pTerm; p; p=p->pSynonym){ |
| 985 | int rc; |
| 986 | if( p->pIter ){ |
| 987 | sqlite3Fts5IterClose(p->pIter); |
| 988 | p->pIter = 0; |
| 989 | } |
| 990 | rc = sqlite3Fts5IndexQuery( |
| 991 | pExpr->pIndex, p->pTerm, p->nQueryTerm, |
| 992 | (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) | |
| 993 | (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0), |
| 994 | pNear->pColset, |
| 995 | &p->pIter |
| 996 | ); |
| 997 | assert( (rc==SQLITE_OK)==(p->pIter!=0) ); |
| 998 | if( rc!=SQLITE_OK ) return rc; |
| 999 | if( 0==sqlite3Fts5IterEof(p->pIter) ){ |
| 1000 | bHit = 1; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | if( bHit==0 ){ |
| 1005 | pNode->bEof = 1; |
| 1006 | return SQLITE_OK; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | pNode->bEof = 0; |
| 1013 | return SQLITE_OK; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | ** If pExpr is an ASC iterator, this function returns a value with the |
| 1018 | ** same sign as: |
| 1019 | ** |
| 1020 | ** (iLhs - iRhs) |
| 1021 | ** |
| 1022 | ** Otherwise, if this is a DESC iterator, the opposite is returned: |
| 1023 | ** |
| 1024 | ** (iRhs - iLhs) |
| 1025 | */ |
| 1026 | static int fts5RowidCmp( |
| 1027 | Fts5Expr *pExpr, |
| 1028 | i64 iLhs, |
| 1029 | i64 iRhs |
| 1030 | ){ |
| 1031 | assert( pExpr->bDesc==0 || pExpr->bDesc==1 ); |
| 1032 | if( pExpr->bDesc==0 ){ |
| 1033 | if( iLhs<iRhs ) return -1; |
| 1034 | return (iLhs > iRhs); |
| 1035 | }else{ |
| 1036 | if( iLhs>iRhs ) return -1; |
| 1037 | return (iLhs < iRhs); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | static void fts5ExprSetEof(Fts5ExprNode *pNode){ |
| 1042 | int i; |
| 1043 | pNode->bEof = 1; |
| 1044 | pNode->bNomatch = 0; |
| 1045 | for(i=0; i<pNode->nChild; i++){ |
| 1046 | fts5ExprSetEof(pNode->apChild[i]); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){ |
| 1051 | if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ |
| 1052 | Fts5ExprNearset *pNear = pNode->pNear; |
| 1053 | int i; |
| 1054 | for(i=0; i<pNear->nPhrase; i++){ |
| 1055 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
| 1056 | pPhrase->poslist.n = 0; |
| 1057 | } |
| 1058 | }else{ |
| 1059 | int i; |
| 1060 | for(i=0; i<pNode->nChild; i++){ |
| 1061 | fts5ExprNodeZeroPoslist(pNode->apChild[i]); |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | ** Zero the position lists of all phrases within the sub-tree headed by |
| 1068 | ** pNode that are currently positioned at rowid iRowid. Phrases positioned |
| 1069 | ** at other rowids are not modified - their position lists belong to rows |
| 1070 | ** that have not yet been tested by the parent node. |
| 1071 | */ |
| 1072 | static void fts5ExprNodeZeroPoslistAt(Fts5ExprNode *pNode, i64 iRowid){ |
| 1073 | if( pNode->iRowid!=iRowid ) return; |
| 1074 | if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ |
| 1075 | fts5ExprNodeZeroPoslist(pNode); |
| 1076 | }else{ |
| 1077 | int i; |
| 1078 | for(i=0; i<pNode->nChild; i++){ |
| 1079 | fts5ExprNodeZeroPoslistAt(pNode->apChild[i], iRowid); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | /* |
| 1087 | ** Compare the values currently indicated by the two nodes as follows: |
| 1088 | ** |
| 1089 | ** res = (*p1) - (*p2) |
| 1090 | ** |
| 1091 | ** Nodes that point to values that come later in the iteration order are |
| 1092 | ** considered to be larger. Nodes at EOF are the largest of all. |
| 1093 | ** |
| 1094 | ** This means that if the iteration order is ASC, then numerically larger |
| 1095 | ** rowids are considered larger. Or if it is the default DESC, numerically |
| 1096 | ** smaller rowids are larger. |
| 1097 | */ |
| 1098 | static int fts5NodeCompare( |
| 1099 | Fts5Expr *pExpr, |
| 1100 | Fts5ExprNode *p1, |
| 1101 | Fts5ExprNode *p2 |
| 1102 | ){ |
| 1103 | if( p2->bEof ) return -1; |
| 1104 | if( p1->bEof ) return +1; |
| 1105 | return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid); |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | ** All individual term iterators in pNear are guaranteed to be valid when |
| 1110 | ** this function is called. This function checks if all term iterators |
| 1111 | ** point to the same rowid, and if not, advances them until they do. |
| 1112 | ** If an EOF is reached before this happens, *pbEof is set to true before |
| 1113 | ** returning. |
| 1114 | ** |
| 1115 | ** SQLITE_OK is returned if an error occurs, or an SQLite error code |
| 1116 | ** otherwise. It is not considered an error code if an iterator reaches |
| 1117 | ** EOF. |
| 1118 | */ |
| 1119 | static int fts5ExprNodeTest_STRING( |
| 1120 | Fts5Expr *pExpr, /* Expression pPhrase belongs to */ |
| 1121 | Fts5ExprNode *pNode |
| 1122 | ){ |
| 1123 | Fts5ExprNearset *pNear = pNode->pNear; |
| 1124 | Fts5ExprPhrase *pLeft = pNear->apPhrase[0]; |
| 1125 | int rc = SQLITE_OK; |
| 1126 | i64 iLast; /* Lastest rowid any iterator points to */ |
| 1127 | int i, j; /* Phrase and token index, respectively */ |
| 1128 | int bMatch; /* True if all terms are at the same rowid */ |
| 1129 | const int bDesc = pExpr->bDesc; |
| 1130 | |
| 1131 | /* Check that this node should not be FTS5_TERM */ |
| 1132 | assert( pNear->nPhrase>1 |
| 1133 | || pNear->apPhrase[0]->nTerm>1 |
| 1134 | || pNear->apPhrase[0]->aTerm[0].pSynonym |
| 1135 | || pNear->apPhrase[0]->aTerm[0].bFirst |
| 1136 | ); |
| 1137 | |
| 1138 | /* Initialize iLast, the "lastest" rowid any iterator points to. If the |
| 1139 | ** iterator skips through rowids in the default ascending order, this means |
| 1140 | ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it |
| 1141 | ** means the minimum rowid. */ |
| 1142 | if( pLeft->aTerm[0].pSynonym ){ |
| 1143 | iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0); |
| 1144 | }else{ |
| 1145 | iLast = pLeft->aTerm[0].pIter->iRowid; |
| 1146 | } |
| 1147 | |
| 1148 | do { |
| 1149 | bMatch = 1; |
| 1150 | for(i=0; i<pNear->nPhrase; i++){ |
| 1151 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
| 1152 | for(j=0; j<pPhrase->nTerm; j++){ |
| 1153 | Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; |
| 1154 | if( pTerm->pSynonym ){ |
| 1155 | i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0); |
| 1156 | if( iRowid==iLast ) continue; |
| 1157 | bMatch = 0; |
| 1158 | if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){ |
| 1159 | pNode->bNomatch = 0; |
| 1160 | pNode->bEof = 1; |
| 1161 | return rc; |
| 1162 | } |
| 1163 | }else{ |
| 1164 | Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter; |
| 1165 | if( pIter->iRowid==iLast ) continue; |
| 1166 | bMatch = 0; |
| 1167 | if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){ |
| 1168 | return rc; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | }while( bMatch==0 ); |
| 1174 | |
| 1175 | pNode->iRowid = iLast; |
| 1176 | pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK); |
| 1177 | assert( pNode->bEof==0 || pNode->bNomatch==0 ); |
| 1178 | |
| 1179 | return rc; |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | ** Advance the first term iterator in the first phrase of pNear. Set output |
| 1184 | ** variable *pbEof to true if it reaches EOF or if an error occurs. |
| 1185 | ** |
| 1186 | ** Return SQLITE_OK if successful, or an SQLite error code if an error |
| 1187 | ** occurs. |
| 1188 | */ |
| 1189 | static int fts5ExprNodeNext_STRING( |
| 1190 | Fts5Expr *pExpr, /* Expression pPhrase belongs to */ |
| 1191 | Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */ |
| 1192 | int bFromValid, |
| 1193 | i64 iFrom |
| 1194 | ){ |
| 1195 | Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0]; |
| 1196 | int rc = SQLITE_OK; |
| 1197 | |
| 1198 | pNode->bNomatch = 0; |
| 1199 | if( pTerm->pSynonym ){ |
| 1200 | int bEof = 1; |
| 1201 | Fts5ExprTerm *p; |
| 1202 | |
| 1203 | /* Find the firstest rowid any synonym points to. */ |
| 1204 | i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0); |
| 1205 | |
| 1206 | /* Advance each iterator that currently points to iRowid. Or, if iFrom |
| 1207 | ** is valid - each iterator that points to a rowid before iFrom. */ |
| 1208 | for(p=pTerm; p; p=p->pSynonym){ |
| 1209 | if( sqlite3Fts5IterEof(p->pIter)==0 ){ |
| 1210 | i64 ii = p->pIter->iRowid; |
| 1211 | if( ii==iRowid |
| 1212 | || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc) |
| 1213 | ){ |
| 1214 | if( bFromValid ){ |
| 1215 | rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom); |
| 1216 | }else{ |
| 1217 | rc = sqlite3Fts5IterNext(p->pIter); |
| 1218 | } |
| 1219 | if( rc!=SQLITE_OK ) break; |
| 1220 | if( sqlite3Fts5IterEof(p->pIter)==0 ){ |
| 1221 | bEof = 0; |
| 1222 | } |
| 1223 | }else{ |
| 1224 | bEof = 0; |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | /* Set the EOF flag if either all synonym iterators are at EOF or an |
| 1230 | ** error has occurred. */ |
| 1231 | pNode->bEof = (rc || bEof); |
| 1232 | }else{ |
| 1233 | Fts5IndexIter *pIter = pTerm->pIter; |
| 1234 | |
| 1235 | assert( Fts5NodeIsString(pNode) ); |
| 1236 | if( bFromValid ){ |
| 1237 | rc = sqlite3Fts5IterNextFrom(pIter, iFrom); |
| 1238 | }else{ |
| 1239 | rc = sqlite3Fts5IterNext(pIter); |
| 1240 | } |
| 1241 | |
| 1242 | pNode->bEof = (rc || sqlite3Fts5IterEof(pIter)); |
| 1243 | } |
| 1244 | |
| 1245 | if( pNode->bEof==0 ){ |
| 1246 | assert( rc==SQLITE_OK ); |
| 1247 | rc = fts5ExprNodeTest_STRING(pExpr, pNode); |
| 1248 | } |
| 1249 | |
| 1250 | return rc; |
| 1251 | } |
| 1252 | |
| 1253 | |
| 1254 | static int fts5ExprNodeTest_TERM( |
| 1255 | Fts5Expr *pExpr, /* Expression that pNear is a part of */ |
| 1256 | Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */ |
| 1257 | ){ |
| 1258 | /* As this "NEAR" object is actually a single phrase that consists |
| 1259 | ** of a single term only, grab pointers into the poslist managed by the |
| 1260 | ** fts5_index.c iterator object. This is much faster than synthesizing |
| 1261 | ** a new poslist the way we have to for more complicated phrase or NEAR |
| 1262 | ** expressions. */ |
| 1263 | Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0]; |
| 1264 | Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter; |
| 1265 | |
| 1266 | assert( pNode->eType==FTS5_TERM ); |
| 1267 | assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 ); |
| 1268 | assert( pPhrase->aTerm[0].pSynonym==0 ); |
| 1269 | |
| 1270 | pPhrase->poslist.n = pIter->nData; |
| 1271 | if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){ |
| 1272 | pPhrase->poslist.p = (u8*)pIter->pData; |
| 1273 | } |
| 1274 | pNode->iRowid = pIter->iRowid; |
| 1275 | pNode->bNomatch = (pPhrase->poslist.n==0); |
| 1276 | return SQLITE_OK; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | ** xNext() method for a node of type FTS5_TERM. |
| 1281 | */ |
| 1282 | static int fts5ExprNodeNext_TERM( |
| 1283 | Fts5Expr *pExpr, |
| 1284 | Fts5ExprNode *pNode, |
| 1285 | int bFromValid, |
| 1286 | i64 iFrom |
| 1287 | ){ |
| 1288 | int rc; |
| 1289 | Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter; |
| 1290 | |
| 1291 | assert( pNode->bEof==0 ); |
| 1292 | if( bFromValid ){ |
| 1293 | rc = sqlite3Fts5IterNextFrom(pIter, iFrom); |
| 1294 | }else{ |
| 1295 | rc = sqlite3Fts5IterNext(pIter); |
| 1296 | } |
| 1297 | if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){ |
| 1298 | rc = fts5ExprNodeTest_TERM(pExpr, pNode); |
| 1299 | }else{ |
| 1300 | pNode->bEof = 1; |
| 1301 | pNode->bNomatch = 0; |
| 1302 | } |
| 1303 | return rc; |
| 1304 | } |
| 1305 | |
| 1306 | static void fts5ExprNodeTest_OR( |
| 1307 | Fts5Expr *pExpr, /* Expression of which pNode is a part */ |
| 1308 | Fts5ExprNode *pNode /* Expression node to test */ |
| 1309 | ){ |
| 1310 | Fts5ExprNode *pNext = pNode->apChild[0]; |
| 1311 | int i; |
| 1312 | |
| 1313 | for(i=1; i<pNode->nChild; i++){ |
| 1314 | Fts5ExprNode *pChild = pNode->apChild[i]; |
| 1315 | int cmp = fts5NodeCompare(pExpr, pNext, pChild); |
| 1316 | if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){ |
| 1317 | pNext = pChild; |
| 1318 | } |
| 1319 | } |
| 1320 | pNode->iRowid = pNext->iRowid; |
| 1321 | pNode->bEof = pNext->bEof; |
| 1322 | pNode->bNomatch = pNext->bNomatch; |
| 1323 | } |
| 1324 | |
| 1325 | static int fts5ExprNodeNext_OR( |
| 1326 | Fts5Expr *pExpr, |
| 1327 | Fts5ExprNode *pNode, |
| 1328 | int bFromValid, |
| 1329 | i64 iFrom |
| 1330 | ){ |
| 1331 | int i; |
| 1332 | i64 iLast = pNode->iRowid; |
| 1333 | |
| 1334 | for(i=0; i<pNode->nChild; i++){ |
| 1335 | Fts5ExprNode *p1 = pNode->apChild[i]; |
| 1336 | assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 ); |
| 1337 | if( p1->bEof==0 ){ |
| 1338 | if( (p1->iRowid==iLast) |
| 1339 | || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0) |
| 1340 | ){ |
| 1341 | int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom); |
| 1342 | if( rc!=SQLITE_OK ){ |
| 1343 | pNode->bNomatch = 0; |
| 1344 | return rc; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | fts5ExprNodeTest_OR(pExpr, pNode); |
| 1351 | return SQLITE_OK; |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | ** Argument pNode is an FTS5_AND node. |
| 1356 | */ |
| 1357 | static int fts5ExprNodeTest_AND( |
| 1358 | Fts5Expr *pExpr, /* Expression pPhrase belongs to */ |
| 1359 | Fts5ExprNode *pAnd /* FTS5_AND node to advance */ |
| 1360 | ){ |
| 1361 | int iChild; |
| 1362 | i64 iLast = pAnd->iRowid; |
| 1363 | int rc = SQLITE_OK; |
| 1364 | int bMatch; |
| 1365 | |
| 1366 | assert( pAnd->bEof==0 ); |
| 1367 | do { |
| 1368 | pAnd->bNomatch = 0; |
| 1369 | bMatch = 1; |
| 1370 | for(iChild=0; iChild<pAnd->nChild; iChild++){ |
| 1371 | Fts5ExprNode *pChild = pAnd->apChild[iChild]; |
| 1372 | int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid); |
| 1373 | if( cmp>0 ){ |
| 1374 | /* Advance pChild until it points to iLast or laster */ |
| 1375 | rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast); |
| 1376 | if( rc!=SQLITE_OK ){ |
| 1377 | pAnd->bNomatch = 0; |
| 1378 | return rc; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | /* If the child node is now at EOF, so is the parent AND node. Otherwise, |
| 1383 | ** the child node is guaranteed to have advanced at least as far as |
| 1384 | ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the |
| 1385 | ** new lastest rowid seen so far. */ |
| 1386 | assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 ); |
| 1387 | if( pChild->bEof ){ |
| 1388 | fts5ExprSetEof(pAnd); |
| 1389 | bMatch = 1; |
| 1390 | break; |
| 1391 | }else if( iLast!=pChild->iRowid ){ |
| 1392 | bMatch = 0; |
| 1393 | iLast = pChild->iRowid; |
| 1394 | } |
| 1395 | |
| 1396 | if( pChild->bNomatch ){ |
| 1397 | pAnd->bNomatch = 1; |
| 1398 | } |
| 1399 | } |
| 1400 | }while( bMatch==0 ); |
| 1401 | |
| 1402 | pAnd->iRowid = iLast; |
| 1403 | if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){ |
| 1404 | fts5ExprNodeZeroPoslistAt(pAnd, iLast); |
| 1405 | } |
| 1406 | return SQLITE_OK; |
| 1407 | } |
| 1408 | |
| 1409 | static int fts5ExprNodeNext_AND( |
| 1410 | Fts5Expr *pExpr, |
| 1411 | Fts5ExprNode *pNode, |
| 1412 | int bFromValid, |
| 1413 | i64 iFrom |
| 1414 | ){ |
| 1415 | int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom); |
| 1416 | if( rc==SQLITE_OK ){ |
| 1417 | rc = fts5ExprNodeTest_AND(pExpr, pNode); |
| 1418 | }else{ |
| 1419 | pNode->bNomatch = 0; |
| 1420 | } |
| 1421 | return rc; |
| 1422 | } |
| 1423 | |
| 1424 | static int fts5ExprNodeTest_NOT( |
| 1425 | Fts5Expr *pExpr, /* Expression pPhrase belongs to */ |
| 1426 | Fts5ExprNode *pNode /* FTS5_NOT node to advance */ |
| 1427 | ){ |
| 1428 | int rc = SQLITE_OK; |
| 1429 | Fts5ExprNode *p1 = pNode->apChild[0]; |
| 1430 | Fts5ExprNode *p2 = pNode->apChild[1]; |
| 1431 | assert( pNode->nChild==2 ); |
| 1432 | |
| 1433 | while( rc==SQLITE_OK && p1->bEof==0 ){ |
| 1434 | int cmp = fts5NodeCompare(pExpr, p1, p2); |
| 1435 | if( cmp>0 ){ |
| 1436 | rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid); |
| 1437 | cmp = fts5NodeCompare(pExpr, p1, p2); |
| 1438 | } |
| 1439 | assert( rc!=SQLITE_OK || cmp<=0 ); |
| 1440 | if( cmp || p2->bNomatch ) break; |
| 1441 | rc = fts5ExprNodeNext(pExpr, p1, 0, 0); |
| 1442 | } |
| 1443 | pNode->bEof = p1->bEof; |
| 1444 | pNode->bNomatch = p1->bNomatch; |
| 1445 | pNode->iRowid = p1->iRowid; |
| 1446 | if( p1->bEof ){ |
| 1447 | fts5ExprNodeZeroPoslist(p2); |
| 1448 | } |
| 1449 | return rc; |
| 1450 | } |
| 1451 | |
| 1452 | static int fts5ExprNodeNext_NOT( |
| 1453 | Fts5Expr *pExpr, |
| 1454 | Fts5ExprNode *pNode, |
| 1455 | int bFromValid, |
| 1456 | i64 iFrom |
| 1457 | ){ |
| 1458 | int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom); |
| 1459 | if( rc==SQLITE_OK ){ |
| 1460 | rc = fts5ExprNodeTest_NOT(pExpr, pNode); |
| 1461 | } |
| 1462 | if( rc!=SQLITE_OK ){ |
| 1463 | pNode->bNomatch = 0; |
| 1464 | } |
| 1465 | return rc; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | ** If pNode currently points to a match, this function returns SQLITE_OK |
| 1470 | ** without modifying it. Otherwise, pNode is advanced until it does point |
| 1471 | ** to a match or EOF is reached. |
| 1472 | */ |
| 1473 | static int fts5ExprNodeTest( |
| 1474 | Fts5Expr *pExpr, /* Expression of which pNode is a part */ |
| 1475 | Fts5ExprNode *pNode /* Expression node to test */ |
| 1476 | ){ |
| 1477 | int rc = SQLITE_OK; |
| 1478 | if( pNode->bEof==0 ){ |
| 1479 | switch( pNode->eType ){ |
| 1480 | |
| 1481 | case FTS5_STRING: { |
| 1482 | rc = fts5ExprNodeTest_STRING(pExpr, pNode); |
| 1483 | break; |
| 1484 | } |
| 1485 | |
| 1486 | case FTS5_TERM: { |
| 1487 | rc = fts5ExprNodeTest_TERM(pExpr, pNode); |
| 1488 | break; |
| 1489 | } |
| 1490 | |
| 1491 | case FTS5_AND: { |
| 1492 | rc = fts5ExprNodeTest_AND(pExpr, pNode); |
| 1493 | break; |
| 1494 | } |
| 1495 | |
| 1496 | case FTS5_OR: { |
| 1497 | fts5ExprNodeTest_OR(pExpr, pNode); |
| 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | default: assert( pNode->eType==FTS5_NOT ); { |
| 1502 | rc = fts5ExprNodeTest_NOT(pExpr, pNode); |
| 1503 | break; |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | return rc; |
| 1508 | } |
| 1509 | |
| 1510 | |
| 1511 | /* |
| 1512 | ** Set node pNode, which is part of expression pExpr, to point to the first |
| 1513 | ** match. If there are no matches, set the Node.bEof flag to indicate EOF. |
| 1514 | ** |
| 1515 | ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. |
| 1516 | ** It is not an error if there are no matches. |
| 1517 | */ |
| 1518 | static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){ |
| 1519 | int rc = SQLITE_OK; |
| 1520 | pNode->bEof = 0; |
| 1521 | pNode->bNomatch = 0; |
| 1522 | |
| 1523 | if( Fts5NodeIsString(pNode) ){ |
| 1524 | /* Initialize all term iterators in the NEAR object. */ |
| 1525 | rc = fts5ExprNearInitAll(pExpr, pNode); |
| 1526 | }else if( pNode->xNext==0 ){ |
| 1527 | pNode->bEof = 1; |
| 1528 | }else{ |
| 1529 | int i; |
| 1530 | int nEof = 0; |
| 1531 | for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){ |
| 1532 | Fts5ExprNode *pChild = pNode->apChild[i]; |
| 1533 | rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]); |
| 1534 | assert( pChild->bEof==0 || pChild->bEof==1 ); |
| 1535 | nEof += pChild->bEof; |
| 1536 | } |
| 1537 | pNode->iRowid = pNode->apChild[0]->iRowid; |
| 1538 | |
| 1539 | switch( pNode->eType ){ |
| 1540 | case FTS5_AND: |
| 1541 | if( nEof>0 ) fts5ExprSetEof(pNode); |
| 1542 | break; |
| 1543 | |
| 1544 | case FTS5_OR: |
| 1545 | if( pNode->nChild==nEof ) fts5ExprSetEof(pNode); |
| 1546 | break; |
| 1547 | |
| 1548 | default: |
| 1549 | assert( pNode->eType==FTS5_NOT ); |
| 1550 | pNode->bEof = pNode->apChild[0]->bEof; |
| 1551 | if( pNode->bEof ) fts5ExprNodeZeroPoslist(pNode->apChild[1]); |
| 1552 | break; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | if( rc==SQLITE_OK ){ |
| 1557 | rc = fts5ExprNodeTest(pExpr, pNode); |
| 1558 | } |
| 1559 | return rc; |
| 1560 | } |
| 1561 | |
| 1562 | |
| 1563 | /* |
| 1564 | ** Begin iterating through the set of documents in index pIdx matched by |
| 1565 | ** the MATCH expression passed as the first argument. If the "bDesc" |
| 1566 | ** parameter is passed a non-zero value, iteration is in descending rowid |
| 1567 | ** order. Or, if it is zero, in ascending order. |
| 1568 | ** |
| 1569 | ** If iterating in ascending rowid order (bDesc==0), the first document |
| 1570 | ** visited is that with the smallest rowid that is larger than or equal |
| 1571 | ** to parameter iFirst. Or, if iterating in ascending order (bDesc==1), |
| 1572 | ** then the first document visited must have a rowid smaller than or |
| 1573 | ** equal to iFirst. |
| 1574 | ** |
| 1575 | ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It |
| 1576 | ** is not considered an error if the query does not match any documents. |
| 1577 | */ |
| 1578 | int sqlite3Fts5ExprFirst( |
| 1579 | Fts5Expr *p, |
| 1580 | Fts5Index *pIdx, |
| 1581 | i64 iFirst, |
| 1582 | i64 iLast, |
| 1583 | int bDesc |
| 1584 | ){ |
| 1585 | Fts5ExprNode *pRoot = p->pRoot; |
| 1586 | int rc; /* Return code */ |
| 1587 | |
| 1588 | p->pIndex = pIdx; |
| 1589 | p->bDesc = bDesc; |
| 1590 | rc = fts5ExprNodeFirst(p, pRoot); |
| 1591 | |
| 1592 | /* If not at EOF but the current rowid occurs earlier than iFirst in |
| 1593 | ** the iteration order, move to document iFirst or later. */ |
| 1594 | if( rc==SQLITE_OK |
| 1595 | && 0==pRoot->bEof |
| 1596 | && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 |
| 1597 | ){ |
| 1598 | rc = fts5ExprNodeNext(p, pRoot, 1, iFirst); |
| 1599 | } |
| 1600 | |
| 1601 | /* If the iterator is not at a real match, skip forward until it is. */ |
| 1602 | while( pRoot->bNomatch && rc==SQLITE_OK ){ |
| 1603 | assert( pRoot->bEof==0 ); |
| 1604 | rc = fts5ExprNodeNext(p, pRoot, 0, 0); |
| 1605 | } |
| 1606 | if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){ |
| 1607 | pRoot->bEof = 1; |
| 1608 | } |
| 1609 | return rc; |
| 1610 | } |
| 1611 | |
| 1612 | /* |
| 1613 | ** Move to the next document |
| 1614 | ** |
| 1615 | ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It |
| 1616 | ** is not considered an error if the query does not match any documents. |
| 1617 | */ |
| 1618 | int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){ |
| 1619 | int rc; |
| 1620 | Fts5ExprNode *pRoot = p->pRoot; |
| 1621 | assert( pRoot->bEof==0 && pRoot->bNomatch==0 ); |
| 1622 | do { |
| 1623 | rc = fts5ExprNodeNext(p, pRoot, 0, 0); |
| 1624 | assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) ); |
| 1625 | }while( pRoot->bNomatch ); |
| 1626 | if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){ |
| 1627 | pRoot->bEof = 1; |
| 1628 | } |
| 1629 | return rc; |
| 1630 | } |
| 1631 | |
| 1632 | int sqlite3Fts5ExprEof(Fts5Expr *p){ |
| 1633 | return p->pRoot->bEof; |
| 1634 | } |
| 1635 | |
| 1636 | i64 sqlite3Fts5ExprRowid(Fts5Expr *p){ |
| 1637 | return p->pRoot->iRowid; |
| 1638 | } |
| 1639 | |
| 1640 | static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){ |
| 1641 | int rc = SQLITE_OK; |
| 1642 | *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n); |
| 1643 | return rc; |
| 1644 | } |
| 1645 | |
| 1646 | /* |
| 1647 | ** Free the phrase object passed as the only argument. |
| 1648 | */ |
| 1649 | static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){ |
| 1650 | if( pPhrase ){ |
| 1651 | int i; |
| 1652 | for(i=0; i<pPhrase->nTerm; i++){ |
| 1653 | Fts5ExprTerm *pSyn; |
| 1654 | Fts5ExprTerm *pNext; |
| 1655 | Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; |
| 1656 | sqlite3_free(pTerm->pTerm); |
| 1657 | sqlite3Fts5IterClose(pTerm->pIter); |
| 1658 | for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){ |
| 1659 | pNext = pSyn->pSynonym; |
| 1660 | sqlite3Fts5IterClose(pSyn->pIter); |
| 1661 | fts5BufferFree((Fts5Buffer*)&pSyn[1]); |
| 1662 | sqlite3_free(pSyn); |
| 1663 | } |
| 1664 | } |
| 1665 | if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist); |
| 1666 | sqlite3_free(pPhrase); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | ** Set the "bFirst" flag on the first token of the phrase passed as the |
| 1672 | ** only argument. |
| 1673 | */ |
| 1674 | void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){ |
| 1675 | if( pPhrase && pPhrase->nTerm ){ |
| 1676 | pPhrase->aTerm[0].bFirst = 1; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | /* |
| 1681 | ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated |
| 1682 | ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is |
| 1683 | ** appended to it and the results returned. |
| 1684 | ** |
| 1685 | ** If an OOM error occurs, both the pNear and pPhrase objects are freed and |
| 1686 | ** NULL returned. |
| 1687 | */ |
| 1688 | Fts5ExprNearset *sqlite3Fts5ParseNearset( |
| 1689 | Fts5Parse *pParse, /* Parse context */ |
| 1690 | Fts5ExprNearset *pNear, /* Existing nearset, or NULL */ |
| 1691 | Fts5ExprPhrase *pPhrase /* Recently parsed phrase */ |
| 1692 | ){ |
| 1693 | const int SZALLOC = 8; |
| 1694 | Fts5ExprNearset *pRet = 0; |
| 1695 | |
| 1696 | if( pParse->rc==SQLITE_OK ){ |
| 1697 | if( pNear==0 ){ |
| 1698 | sqlite3_int64 nByte; |
| 1699 | nByte = SZ_FTS5EXPRNEARSET(SZALLOC+1); |
| 1700 | pRet = sqlite3_malloc64(nByte); |
| 1701 | if( pRet==0 ){ |
| 1702 | pParse->rc = SQLITE_NOMEM; |
| 1703 | }else{ |
| 1704 | memset(pRet, 0, (size_t)nByte); |
| 1705 | } |
| 1706 | }else if( (pNear->nPhrase % SZALLOC)==0 ){ |
| 1707 | int nNew = pNear->nPhrase + SZALLOC; |
| 1708 | sqlite3_int64 nByte; |
| 1709 | |
| 1710 | nByte = SZ_FTS5EXPRNEARSET(nNew+1); |
| 1711 | pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte); |
| 1712 | if( pRet==0 ){ |
| 1713 | pParse->rc = SQLITE_NOMEM; |
| 1714 | } |
| 1715 | }else{ |
| 1716 | pRet = pNear; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | if( pRet==0 ){ |
| 1721 | assert( pParse->rc!=SQLITE_OK ); |
| 1722 | sqlite3Fts5ParseNearsetFree(pNear); |
| 1723 | sqlite3Fts5ParsePhraseFree(pPhrase); |
| 1724 | }else{ |
| 1725 | if( pRet->nPhrase>0 ){ |
| 1726 | Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1]; |
| 1727 | assert( pParse!=0 ); |
| 1728 | assert( pParse->apPhrase!=0 ); |
| 1729 | assert( pParse->nPhrase>=2 ); |
| 1730 | assert( pLast==pParse->apPhrase[pParse->nPhrase-2] ); |
| 1731 | if( pPhrase->nTerm==0 ){ |
| 1732 | fts5ExprPhraseFree(pPhrase); |
| 1733 | pRet->nPhrase--; |
| 1734 | pParse->nPhrase--; |
| 1735 | pPhrase = pLast; |
| 1736 | }else if( pLast->nTerm==0 ){ |
| 1737 | fts5ExprPhraseFree(pLast); |
| 1738 | pParse->apPhrase[pParse->nPhrase-2] = pPhrase; |
| 1739 | pParse->nPhrase--; |
| 1740 | pRet->nPhrase--; |
| 1741 | } |
| 1742 | } |
| 1743 | pRet->apPhrase[pRet->nPhrase++] = pPhrase; |
| 1744 | } |
| 1745 | return pRet; |
| 1746 | } |
| 1747 | |
| 1748 | typedef struct TokenCtx TokenCtx; |
| 1749 | struct TokenCtx { |
| 1750 | Fts5ExprPhrase *pPhrase; |
| 1751 | Fts5Config *pConfig; |
| 1752 | int rc; |
| 1753 | }; |
| 1754 | |
| 1755 | /* |
| 1756 | ** Callback for tokenizing terms used by ParseTerm(). |
| 1757 | */ |
| 1758 | static int fts5ParseTokenize( |
| 1759 | void *pContext, /* Pointer to Fts5InsertCtx object */ |
| 1760 | int tflags, /* Mask of FTS5_TOKEN_* flags */ |
| 1761 | const char *pToken, /* Buffer containing token */ |
| 1762 | int nToken, /* Size of token in bytes */ |
| 1763 | int iUnused1, /* Start offset of token */ |
| 1764 | int iUnused2 /* End offset of token */ |
| 1765 | ){ |
| 1766 | int rc = SQLITE_OK; |
| 1767 | const int SZALLOC = 8; |
| 1768 | TokenCtx *pCtx = (TokenCtx*)pContext; |
| 1769 | Fts5ExprPhrase *pPhrase = pCtx->pPhrase; |
| 1770 | |
| 1771 | UNUSED_PARAM2(iUnused1, iUnused2); |
| 1772 | |
| 1773 | /* If an error has already occurred, this is a no-op */ |
| 1774 | if( pCtx->rc!=SQLITE_OK ) return pCtx->rc; |
| 1775 | if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE; |
| 1776 | |
| 1777 | if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){ |
| 1778 | Fts5ExprTerm *pSyn; |
| 1779 | sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1; |
| 1780 | pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte); |
| 1781 | if( pSyn==0 ){ |
| 1782 | rc = SQLITE_NOMEM; |
| 1783 | }else{ |
| 1784 | memset(pSyn, 0, (size_t)nByte); |
| 1785 | pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer); |
| 1786 | pSyn->nFullTerm = pSyn->nQueryTerm = nToken; |
| 1787 | memcpy(pSyn->pTerm, pToken, nToken); |
| 1788 | if( pCtx->pConfig->bTokendata ){ |
| 1789 | pSyn->nQueryTerm = (int)strlen(pSyn->pTerm); |
| 1790 | } |
| 1791 | pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym; |
| 1792 | pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn; |
| 1793 | } |
| 1794 | }else{ |
| 1795 | Fts5ExprTerm *pTerm; |
| 1796 | if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){ |
| 1797 | Fts5ExprPhrase *pNew; |
| 1798 | int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0); |
| 1799 | |
| 1800 | pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase, |
| 1801 | SZ_FTS5EXPRPHRASE(nNew+1) |
| 1802 | ); |
| 1803 | if( pNew==0 ){ |
| 1804 | rc = SQLITE_NOMEM; |
| 1805 | }else{ |
| 1806 | if( pPhrase==0 ) memset(pNew, 0, SZ_FTS5EXPRPHRASE(1)); |
| 1807 | pCtx->pPhrase = pPhrase = pNew; |
| 1808 | pNew->nTerm = nNew - SZALLOC; |
| 1809 | } |
| 1810 | } |
| 1811 | |
| 1812 | if( rc==SQLITE_OK ){ |
| 1813 | pTerm = &pPhrase->aTerm[pPhrase->nTerm++]; |
| 1814 | memset(pTerm, 0, sizeof(Fts5ExprTerm)); |
| 1815 | pTerm->pTerm = sqlite3Fts5Strndup(&rc, pToken, nToken); |
| 1816 | pTerm->nFullTerm = pTerm->nQueryTerm = nToken; |
| 1817 | if( pCtx->pConfig->bTokendata && rc==SQLITE_OK ){ |
| 1818 | pTerm->nQueryTerm = (int)strlen(pTerm->pTerm); |
| 1819 | } |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | pCtx->rc = rc; |
| 1824 | return rc; |
| 1825 | } |
| 1826 | |
| 1827 | |
| 1828 | /* |
| 1829 | ** Free the phrase object passed as the only argument. |
| 1830 | */ |
| 1831 | void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){ |
| 1832 | fts5ExprPhraseFree(pPhrase); |
| 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | ** Free the phrase object passed as the second argument. |
| 1837 | */ |
| 1838 | void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){ |
| 1839 | if( pNear ){ |
| 1840 | int i; |
| 1841 | for(i=0; i<pNear->nPhrase; i++){ |
| 1842 | fts5ExprPhraseFree(pNear->apPhrase[i]); |
| 1843 | } |
| 1844 | sqlite3_free(pNear->pColset); |
| 1845 | sqlite3_free(pNear); |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){ |
| 1850 | assert( pParse->pExpr==0 ); |
| 1851 | pParse->pExpr = p; |
| 1852 | } |
| 1853 | |
| 1854 | static int parseGrowPhraseArray(Fts5Parse *pParse){ |
| 1855 | if( (pParse->nPhrase % 8)==0 ){ |
| 1856 | sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8); |
| 1857 | Fts5ExprPhrase **apNew; |
| 1858 | apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte); |
| 1859 | if( apNew==0 ){ |
| 1860 | pParse->rc = SQLITE_NOMEM; |
| 1861 | return SQLITE_NOMEM; |
| 1862 | } |
| 1863 | pParse->apPhrase = apNew; |
| 1864 | } |
| 1865 | return SQLITE_OK; |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | ** This function is called by the parser to process a string token. The |
| 1870 | ** string may or may not be quoted. In any case it is tokenized and a |
| 1871 | ** phrase object consisting of all tokens returned. |
| 1872 | */ |
| 1873 | Fts5ExprPhrase *sqlite3Fts5ParseTerm( |
| 1874 | Fts5Parse *pParse, /* Parse context */ |
| 1875 | Fts5ExprPhrase *pAppend, /* Phrase to append to */ |
| 1876 | Fts5Token *pToken, /* String to tokenize */ |
| 1877 | int bPrefix /* True if there is a trailing "*" */ |
| 1878 | ){ |
| 1879 | Fts5Config *pConfig = pParse->pConfig; |
| 1880 | TokenCtx sCtx; /* Context object passed to callback */ |
| 1881 | int rc; /* Tokenize return code */ |
| 1882 | char *z = 0; |
| 1883 | |
| 1884 | memset(&sCtx, 0, sizeof(TokenCtx)); |
| 1885 | sCtx.pPhrase = pAppend; |
| 1886 | sCtx.pConfig = pConfig; |
| 1887 | |
| 1888 | rc = fts5ParseStringFromToken(pToken, &z); |
| 1889 | if( rc==SQLITE_OK ){ |
| 1890 | int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0); |
| 1891 | int n; |
| 1892 | sqlite3Fts5Dequote(z); |
| 1893 | n = (int)strlen(z); |
| 1894 | rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize); |
| 1895 | } |
| 1896 | sqlite3_free(z); |
| 1897 | if( rc || (rc = sCtx.rc) ){ |
| 1898 | pParse->rc = rc; |
| 1899 | fts5ExprPhraseFree(sCtx.pPhrase); |
| 1900 | sCtx.pPhrase = 0; |
| 1901 | }else{ |
| 1902 | |
| 1903 | if( pAppend==0 ){ |
| 1904 | if( parseGrowPhraseArray(pParse) ){ |
| 1905 | fts5ExprPhraseFree(sCtx.pPhrase); |
| 1906 | return 0; |
| 1907 | } |
| 1908 | pParse->nPhrase++; |
| 1909 | } |
| 1910 | |
| 1911 | if( sCtx.pPhrase==0 ){ |
| 1912 | /* This happens when parsing a token or quoted phrase that contains |
| 1913 | ** no token characters at all. (e.g ... MATCH '""'). */ |
| 1914 | sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, SZ_FTS5EXPRPHRASE(1)); |
| 1915 | }else if( sCtx.pPhrase->nTerm ){ |
| 1916 | sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix; |
| 1917 | } |
| 1918 | assert( pParse->apPhrase!=0 ); |
| 1919 | pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase; |
| 1920 | } |
| 1921 | |
| 1922 | return sCtx.pPhrase; |
| 1923 | } |
| 1924 | |
| 1925 | /* |
| 1926 | ** Create a new FTS5 expression by cloning phrase iPhrase of the |
| 1927 | ** expression passed as the second argument. |
| 1928 | */ |
| 1929 | int sqlite3Fts5ExprClonePhrase( |
| 1930 | Fts5Expr *pExpr, |
| 1931 | int iPhrase, |
| 1932 | Fts5Expr **ppNew |
| 1933 | ){ |
| 1934 | int rc = SQLITE_OK; /* Return code */ |
| 1935 | Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */ |
| 1936 | Fts5Expr *pNew = 0; /* Expression to return via *ppNew */ |
| 1937 | TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */ |
| 1938 | if( !pExpr || iPhrase<0 || iPhrase>=pExpr->nPhrase ){ |
| 1939 | rc = SQLITE_RANGE; |
| 1940 | }else{ |
| 1941 | pOrig = pExpr->apExprPhrase[iPhrase]; |
| 1942 | pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr)); |
| 1943 | } |
| 1944 | if( rc==SQLITE_OK ){ |
| 1945 | pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc, |
| 1946 | sizeof(Fts5ExprPhrase*)); |
| 1947 | } |
| 1948 | if( rc==SQLITE_OK ){ |
| 1949 | pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRNODE(1)); |
| 1950 | } |
| 1951 | if( rc==SQLITE_OK ){ |
| 1952 | pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc, |
| 1953 | SZ_FTS5EXPRNEARSET(2)); |
| 1954 | } |
| 1955 | if( rc==SQLITE_OK && ALWAYS(pOrig!=0) ){ |
| 1956 | Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset; |
| 1957 | if( pColsetOrig ){ |
| 1958 | sqlite3_int64 nByte; |
| 1959 | Fts5Colset *pColset; |
| 1960 | nByte = SZ_FTS5COLSET(pColsetOrig->nCol); |
| 1961 | pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte); |
| 1962 | if( pColset ){ |
| 1963 | memcpy(pColset, pColsetOrig, (size_t)nByte); |
| 1964 | } |
| 1965 | pNew->pRoot->pNear->pColset = pColset; |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | if( rc==SQLITE_OK ){ |
| 1970 | if( pOrig->nTerm ){ |
| 1971 | int i; /* Used to iterate through phrase terms */ |
| 1972 | sCtx.pConfig = pExpr->pConfig; |
| 1973 | for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){ |
| 1974 | int tflags = 0; |
| 1975 | Fts5ExprTerm *p; |
| 1976 | for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){ |
| 1977 | rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0); |
| 1978 | tflags = FTS5_TOKEN_COLOCATED; |
| 1979 | } |
| 1980 | if( rc==SQLITE_OK ){ |
| 1981 | sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix; |
| 1982 | sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst; |
| 1983 | } |
| 1984 | } |
| 1985 | }else{ |
| 1986 | /* This happens when parsing a token or quoted phrase that contains |
| 1987 | ** no token characters at all. (e.g ... MATCH '""'). */ |
| 1988 | sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRPHRASE(1)); |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){ |
| 1993 | /* All the allocations succeeded. Put the expression object together. */ |
| 1994 | pNew->pIndex = pExpr->pIndex; |
| 1995 | pNew->pConfig = pExpr->pConfig; |
| 1996 | pNew->nPhrase = 1; |
| 1997 | pNew->apExprPhrase[0] = sCtx.pPhrase; |
| 1998 | pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase; |
| 1999 | pNew->pRoot->pNear->nPhrase = 1; |
| 2000 | sCtx.pPhrase->pNode = pNew->pRoot; |
| 2001 | |
| 2002 | if( pOrig->nTerm==1 |
| 2003 | && pOrig->aTerm[0].pSynonym==0 |
| 2004 | && pOrig->aTerm[0].bFirst==0 |
| 2005 | ){ |
| 2006 | pNew->pRoot->eType = FTS5_TERM; |
| 2007 | pNew->pRoot->xNext = fts5ExprNodeNext_TERM; |
| 2008 | }else{ |
| 2009 | pNew->pRoot->eType = FTS5_STRING; |
| 2010 | pNew->pRoot->xNext = fts5ExprNodeNext_STRING; |
| 2011 | } |
| 2012 | }else{ |
| 2013 | sqlite3Fts5ExprFree(pNew); |
| 2014 | fts5ExprPhraseFree(sCtx.pPhrase); |
| 2015 | pNew = 0; |
| 2016 | } |
| 2017 | |
| 2018 | *ppNew = pNew; |
| 2019 | return rc; |
| 2020 | } |
| 2021 | |
| 2022 | |
| 2023 | /* |
| 2024 | ** Token pTok has appeared in a MATCH expression where the NEAR operator |
| 2025 | ** is expected. If token pTok does not contain "NEAR", store an error |
| 2026 | ** in the pParse object. |
| 2027 | */ |
| 2028 | void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){ |
| 2029 | if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){ |
| 2030 | sqlite3Fts5ParseError( |
| 2031 | pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p |
| 2032 | ); |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | void sqlite3Fts5ParseSetDistance( |
| 2037 | Fts5Parse *pParse, |
| 2038 | Fts5ExprNearset *pNear, |
| 2039 | Fts5Token *p |
| 2040 | ){ |
| 2041 | if( pNear ){ |
| 2042 | int nNear = 0; |
| 2043 | int i; |
| 2044 | if( p->n ){ |
| 2045 | for(i=0; i<p->n; i++){ |
| 2046 | char c = (char)p->p[i]; |
| 2047 | if( c<'0' || c>'9' ){ |
| 2048 | sqlite3Fts5ParseError( |
| 2049 | pParse, "expected integer, got \"%.*s\"", p->n, p->p |
| 2050 | ); |
| 2051 | return; |
| 2052 | } |
| 2053 | if( nNear<214748363 ) nNear = nNear * 10 + (p->p[i] - '0'); |
| 2054 | /* ^^^^^^^^^^^^^^^--- Prevent integer overflow */ |
| 2055 | } |
| 2056 | }else{ |
| 2057 | nNear = FTS5_DEFAULT_NEARDIST; |
| 2058 | } |
| 2059 | pNear->nNear = nNear; |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | ** The second argument passed to this function may be NULL, or it may be |
| 2065 | ** an existing Fts5Colset object. This function returns a pointer to |
| 2066 | ** a new colset object containing the contents of (p) with new value column |
| 2067 | ** number iCol appended. |
| 2068 | ** |
| 2069 | ** If an OOM error occurs, store an error code in pParse and return NULL. |
| 2070 | ** The old colset object (if any) is not freed in this case. |
| 2071 | */ |
| 2072 | static Fts5Colset *fts5ParseColset( |
| 2073 | Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */ |
| 2074 | Fts5Colset *p, /* Existing colset object */ |
| 2075 | int iCol /* New column to add to colset object */ |
| 2076 | ){ |
| 2077 | int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */ |
| 2078 | Fts5Colset *pNew; /* New colset object to return */ |
| 2079 | |
| 2080 | assert( pParse->rc==SQLITE_OK ); |
| 2081 | assert( iCol>=0 && iCol<pParse->pConfig->nCol ); |
| 2082 | |
| 2083 | pNew = sqlite3_realloc64(p, SZ_FTS5COLSET(nCol+1)); |
| 2084 | if( pNew==0 ){ |
| 2085 | pParse->rc = SQLITE_NOMEM; |
| 2086 | }else{ |
| 2087 | int *aiCol = pNew->aiCol; |
| 2088 | int i, j; |
| 2089 | for(i=0; i<nCol; i++){ |
| 2090 | if( aiCol[i]==iCol ) return pNew; |
| 2091 | if( aiCol[i]>iCol ) break; |
| 2092 | } |
| 2093 | for(j=nCol; j>i; j--){ |
| 2094 | aiCol[j] = aiCol[j-1]; |
| 2095 | } |
| 2096 | aiCol[i] = iCol; |
| 2097 | pNew->nCol = nCol+1; |
| 2098 | |
| 2099 | #ifndef NDEBUG |
| 2100 | /* Check that the array is in order and contains no duplicate entries. */ |
| 2101 | for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] ); |
| 2102 | #endif |
| 2103 | } |
| 2104 | |
| 2105 | return pNew; |
| 2106 | } |
| 2107 | |
| 2108 | /* |
| 2109 | ** Allocate and return an Fts5Colset object specifying the inverse of |
| 2110 | ** the colset passed as the second argument. Free the colset passed |
| 2111 | ** as the second argument before returning. |
| 2112 | */ |
| 2113 | Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){ |
| 2114 | Fts5Colset *pRet; |
| 2115 | int nCol = pParse->pConfig->nCol; |
| 2116 | |
| 2117 | pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc, |
| 2118 | SZ_FTS5COLSET(nCol+1) |
| 2119 | ); |
| 2120 | if( pRet ){ |
| 2121 | int i; |
| 2122 | int iOld = 0; |
| 2123 | for(i=0; i<nCol; i++){ |
| 2124 | if( iOld>=p->nCol || p->aiCol[iOld]!=i ){ |
| 2125 | pRet->aiCol[pRet->nCol++] = i; |
| 2126 | }else{ |
| 2127 | iOld++; |
| 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | sqlite3_free(p); |
| 2133 | return pRet; |
| 2134 | } |
| 2135 | |
| 2136 | Fts5Colset *sqlite3Fts5ParseColset( |
| 2137 | Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */ |
| 2138 | Fts5Colset *pColset, /* Existing colset object */ |
| 2139 | Fts5Token *p |
| 2140 | ){ |
| 2141 | Fts5Colset *pRet = 0; |
| 2142 | int iCol; |
| 2143 | char *z; /* Dequoted copy of token p */ |
| 2144 | |
| 2145 | z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n); |
| 2146 | if( pParse->rc==SQLITE_OK ){ |
| 2147 | Fts5Config *pConfig = pParse->pConfig; |
| 2148 | sqlite3Fts5Dequote(z); |
| 2149 | for(iCol=0; iCol<pConfig->nCol; iCol++){ |
| 2150 | if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break; |
| 2151 | } |
| 2152 | if( iCol==pConfig->nCol ){ |
| 2153 | sqlite3Fts5ParseError(pParse, "no such column: %s", z); |
| 2154 | }else{ |
| 2155 | pRet = fts5ParseColset(pParse, pColset, iCol); |
| 2156 | } |
| 2157 | sqlite3_free(z); |
| 2158 | } |
| 2159 | |
| 2160 | if( pRet==0 ){ |
| 2161 | assert( pParse->rc!=SQLITE_OK ); |
| 2162 | sqlite3_free(pColset); |
| 2163 | } |
| 2164 | |
| 2165 | return pRet; |
| 2166 | } |
| 2167 | |
| 2168 | /* |
| 2169 | ** If argument pOrig is NULL, or if (*pRc) is set to anything other than |
| 2170 | ** SQLITE_OK when this function is called, NULL is returned. |
| 2171 | ** |
| 2172 | ** Otherwise, a copy of (*pOrig) is made into memory obtained from |
| 2173 | ** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation |
| 2174 | ** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned. |
| 2175 | */ |
| 2176 | static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){ |
| 2177 | Fts5Colset *pRet; |
| 2178 | if( pOrig ){ |
| 2179 | sqlite3_int64 nByte = SZ_FTS5COLSET(pOrig->nCol); |
| 2180 | pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte); |
| 2181 | if( pRet ){ |
| 2182 | memcpy(pRet, pOrig, (size_t)nByte); |
| 2183 | } |
| 2184 | }else{ |
| 2185 | pRet = 0; |
| 2186 | } |
| 2187 | return pRet; |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | ** Remove from colset pColset any columns that are not also in colset pMerge. |
| 2192 | */ |
| 2193 | static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){ |
| 2194 | int iIn = 0; /* Next input in pColset */ |
| 2195 | int iMerge = 0; /* Next input in pMerge */ |
| 2196 | int iOut = 0; /* Next output slot in pColset */ |
| 2197 | |
| 2198 | while( iIn<pColset->nCol && iMerge<pMerge->nCol ){ |
| 2199 | int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge]; |
| 2200 | if( iDiff==0 ){ |
| 2201 | pColset->aiCol[iOut++] = pMerge->aiCol[iMerge]; |
| 2202 | iMerge++; |
| 2203 | iIn++; |
| 2204 | }else if( iDiff>0 ){ |
| 2205 | iMerge++; |
| 2206 | }else{ |
| 2207 | iIn++; |
| 2208 | } |
| 2209 | } |
| 2210 | pColset->nCol = iOut; |
| 2211 | } |
| 2212 | |
| 2213 | /* |
| 2214 | ** Recursively apply colset pColset to expression node pNode and all of |
| 2215 | ** its decendents. If (*ppFree) is not NULL, it contains a spare copy |
| 2216 | ** of pColset. This function may use the spare copy and set (*ppFree) to |
| 2217 | ** zero, or it may create copies of pColset using fts5CloneColset(). |
| 2218 | */ |
| 2219 | static void fts5ParseSetColset( |
| 2220 | Fts5Parse *pParse, |
| 2221 | Fts5ExprNode *pNode, |
| 2222 | Fts5Colset *pColset, |
| 2223 | Fts5Colset **ppFree |
| 2224 | ){ |
| 2225 | if( pParse->rc==SQLITE_OK ){ |
| 2226 | assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING |
| 2227 | || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR |
| 2228 | || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF |
| 2229 | ); |
| 2230 | if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ |
| 2231 | Fts5ExprNearset *pNear = pNode->pNear; |
| 2232 | if( pNear->pColset ){ |
| 2233 | fts5MergeColset(pNear->pColset, pColset); |
| 2234 | if( pNear->pColset->nCol==0 ){ |
| 2235 | pNode->eType = FTS5_EOF; |
| 2236 | pNode->xNext = 0; |
| 2237 | } |
| 2238 | }else if( *ppFree ){ |
| 2239 | pNear->pColset = pColset; |
| 2240 | *ppFree = 0; |
| 2241 | }else{ |
| 2242 | pNear->pColset = fts5CloneColset(&pParse->rc, pColset); |
| 2243 | } |
| 2244 | }else{ |
| 2245 | int i; |
| 2246 | assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 ); |
| 2247 | for(i=0; i<pNode->nChild; i++){ |
| 2248 | fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree); |
| 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | ** Apply colset pColset to expression node pExpr and all of its descendents. |
| 2256 | */ |
| 2257 | void sqlite3Fts5ParseSetColset( |
| 2258 | Fts5Parse *pParse, |
| 2259 | Fts5ExprNode *pExpr, |
| 2260 | Fts5Colset *pColset |
| 2261 | ){ |
| 2262 | Fts5Colset *pFree = pColset; |
| 2263 | if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){ |
| 2264 | sqlite3Fts5ParseError(pParse, |
| 2265 | "fts5: column queries are not supported (detail=none)" |
| 2266 | ); |
| 2267 | }else{ |
| 2268 | fts5ParseSetColset(pParse, pExpr, pColset, &pFree); |
| 2269 | } |
| 2270 | sqlite3_free(pFree); |
| 2271 | } |
| 2272 | |
| 2273 | static void fts5ExprAssignXNext(Fts5ExprNode *pNode){ |
| 2274 | switch( pNode->eType ){ |
| 2275 | case FTS5_STRING: { |
| 2276 | Fts5ExprNearset *pNear = pNode->pNear; |
| 2277 | if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1 |
| 2278 | && pNear->apPhrase[0]->aTerm[0].pSynonym==0 |
| 2279 | && pNear->apPhrase[0]->aTerm[0].bFirst==0 |
| 2280 | ){ |
| 2281 | pNode->eType = FTS5_TERM; |
| 2282 | pNode->xNext = fts5ExprNodeNext_TERM; |
| 2283 | }else{ |
| 2284 | pNode->xNext = fts5ExprNodeNext_STRING; |
| 2285 | } |
| 2286 | break; |
| 2287 | }; |
| 2288 | |
| 2289 | case FTS5_OR: { |
| 2290 | pNode->xNext = fts5ExprNodeNext_OR; |
| 2291 | break; |
| 2292 | }; |
| 2293 | |
| 2294 | case FTS5_AND: { |
| 2295 | pNode->xNext = fts5ExprNodeNext_AND; |
| 2296 | break; |
| 2297 | }; |
| 2298 | |
| 2299 | default: assert( pNode->eType==FTS5_NOT ); { |
| 2300 | pNode->xNext = fts5ExprNodeNext_NOT; |
| 2301 | break; |
| 2302 | }; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | /* |
| 2307 | ** Add pSub as a child of p. |
| 2308 | */ |
| 2309 | static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){ |
| 2310 | int ii = p->nChild; |
| 2311 | if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){ |
| 2312 | int nByte = sizeof(Fts5ExprNode*) * pSub->nChild; |
| 2313 | memcpy(&p->apChild[p->nChild], pSub->apChild, nByte); |
| 2314 | p->nChild += pSub->nChild; |
| 2315 | sqlite3_free(pSub); |
| 2316 | }else{ |
| 2317 | p->apChild[p->nChild++] = pSub; |
| 2318 | } |
| 2319 | for( ; ii<p->nChild; ii++){ |
| 2320 | p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1); |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | /* |
| 2325 | ** This function is used when parsing LIKE or GLOB patterns against |
| 2326 | ** trigram indexes that specify either detail=column or detail=none. |
| 2327 | ** It converts a phrase: |
| 2328 | ** |
| 2329 | ** abc + def + ghi |
| 2330 | ** |
| 2331 | ** into an AND tree: |
| 2332 | ** |
| 2333 | ** abc AND def AND ghi |
| 2334 | */ |
| 2335 | static Fts5ExprNode *fts5ParsePhraseToAnd( |
| 2336 | Fts5Parse *pParse, |
| 2337 | Fts5ExprNearset *pNear |
| 2338 | ){ |
| 2339 | int nTerm = pNear->apPhrase[0]->nTerm; |
| 2340 | int ii; |
| 2341 | int nByte; |
| 2342 | Fts5ExprNode *pRet; |
| 2343 | |
| 2344 | assert( pNear->nPhrase==1 ); |
| 2345 | assert( pParse->bPhraseToAnd ); |
| 2346 | |
| 2347 | nByte = SZ_FTS5EXPRNODE(nTerm+1); |
| 2348 | pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); |
| 2349 | if( pRet ){ |
| 2350 | pRet->eType = FTS5_AND; |
| 2351 | pRet->nChild = nTerm; |
| 2352 | pRet->iHeight = 1; |
| 2353 | fts5ExprAssignXNext(pRet); |
| 2354 | pParse->nPhrase--; |
| 2355 | for(ii=0; ii<nTerm; ii++){ |
| 2356 | Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero( |
| 2357 | &pParse->rc, SZ_FTS5EXPRPHRASE(1) |
| 2358 | ); |
| 2359 | if( pPhrase ){ |
| 2360 | if( parseGrowPhraseArray(pParse) ){ |
| 2361 | fts5ExprPhraseFree(pPhrase); |
| 2362 | }else{ |
| 2363 | Fts5ExprTerm *p = &pNear->apPhrase[0]->aTerm[ii]; |
| 2364 | Fts5ExprTerm *pTo = &pPhrase->aTerm[0]; |
| 2365 | pParse->apPhrase[pParse->nPhrase++] = pPhrase; |
| 2366 | pPhrase->nTerm = 1; |
| 2367 | pTo->pTerm = sqlite3Fts5Strndup(&pParse->rc, p->pTerm, p->nFullTerm); |
| 2368 | pTo->nQueryTerm = p->nQueryTerm; |
| 2369 | pTo->nFullTerm = p->nFullTerm; |
| 2370 | pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING, |
| 2371 | 0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase) |
| 2372 | ); |
| 2373 | } |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | if( pParse->rc ){ |
| 2378 | sqlite3Fts5ParseNodeFree(pRet); |
| 2379 | pRet = 0; |
| 2380 | }else{ |
| 2381 | sqlite3Fts5ParseNearsetFree(pNear); |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | return pRet; |
| 2386 | } |
| 2387 | |
| 2388 | /* |
| 2389 | ** Allocate and return a new expression object. If anything goes wrong (i.e. |
| 2390 | ** OOM error), leave an error code in pParse and return NULL. |
| 2391 | */ |
| 2392 | Fts5ExprNode *sqlite3Fts5ParseNode( |
| 2393 | Fts5Parse *pParse, /* Parse context */ |
| 2394 | int eType, /* FTS5_STRING, AND, OR or NOT */ |
| 2395 | Fts5ExprNode *pLeft, /* Left hand child expression */ |
| 2396 | Fts5ExprNode *pRight, /* Right hand child expression */ |
| 2397 | Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */ |
| 2398 | ){ |
| 2399 | Fts5ExprNode *pRet = 0; |
| 2400 | |
| 2401 | if( pParse->rc==SQLITE_OK ){ |
| 2402 | int nChild = 0; /* Number of children of returned node */ |
| 2403 | sqlite3_int64 nByte; /* Bytes of space to allocate for this node */ |
| 2404 | |
| 2405 | assert( (eType!=FTS5_STRING && !pNear) |
| 2406 | || (eType==FTS5_STRING && !pLeft && !pRight) |
| 2407 | ); |
| 2408 | if( eType==FTS5_STRING && pNear==0 ) return 0; |
| 2409 | if( eType!=FTS5_STRING && pLeft==0 ) return pRight; |
| 2410 | if( eType!=FTS5_STRING && pRight==0 ) return pLeft; |
| 2411 | |
| 2412 | if( eType==FTS5_STRING |
| 2413 | && pParse->bPhraseToAnd |
| 2414 | && pNear->apPhrase[0]->nTerm>1 |
| 2415 | ){ |
| 2416 | pRet = fts5ParsePhraseToAnd(pParse, pNear); |
| 2417 | }else{ |
| 2418 | if( eType==FTS5_NOT ){ |
| 2419 | nChild = 2; |
| 2420 | }else if( eType==FTS5_AND || eType==FTS5_OR ){ |
| 2421 | nChild = 2; |
| 2422 | if( pLeft->eType==eType ) nChild += pLeft->nChild-1; |
| 2423 | if( pRight->eType==eType ) nChild += pRight->nChild-1; |
| 2424 | } |
| 2425 | |
| 2426 | nByte = SZ_FTS5EXPRNODE(nChild); |
| 2427 | pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); |
| 2428 | |
| 2429 | if( pRet ){ |
| 2430 | pRet->eType = eType; |
| 2431 | pRet->pNear = pNear; |
| 2432 | fts5ExprAssignXNext(pRet); |
| 2433 | if( eType==FTS5_STRING ){ |
| 2434 | int iPhrase; |
| 2435 | for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){ |
| 2436 | pNear->apPhrase[iPhrase]->pNode = pRet; |
| 2437 | if( pNear->apPhrase[iPhrase]->nTerm==0 ){ |
| 2438 | pRet->xNext = 0; |
| 2439 | pRet->eType = FTS5_EOF; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){ |
| 2444 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[0]; |
| 2445 | if( pNear->nPhrase!=1 |
| 2446 | || pPhrase->nTerm>1 |
| 2447 | || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst) |
| 2448 | ){ |
| 2449 | sqlite3Fts5ParseError(pParse, |
| 2450 | "fts5: %s queries are not supported (detail!=full)", |
| 2451 | pNear->nPhrase==1 ? "phrase": "NEAR" |
| 2452 | ); |
| 2453 | sqlite3Fts5ParseNodeFree(pRet); |
| 2454 | pRet = 0; |
| 2455 | pNear = 0; |
| 2456 | assert( pLeft==0 && pRight==0 ); |
| 2457 | } |
| 2458 | } |
| 2459 | }else{ |
| 2460 | assert( pNear==0 ); |
| 2461 | fts5ExprAddChildren(pRet, pLeft); |
| 2462 | fts5ExprAddChildren(pRet, pRight); |
| 2463 | pLeft = pRight = 0; |
| 2464 | if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH ){ |
| 2465 | sqlite3Fts5ParseError(pParse, |
| 2466 | "fts5 expression tree is too large (maximum depth %d)", |
| 2467 | SQLITE_FTS5_MAX_EXPR_DEPTH |
| 2468 | ); |
| 2469 | sqlite3Fts5ParseNodeFree(pRet); |
| 2470 | pRet = 0; |
| 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | if( pRet==0 ){ |
| 2478 | assert( pParse->rc!=SQLITE_OK ); |
| 2479 | sqlite3Fts5ParseNodeFree(pLeft); |
| 2480 | sqlite3Fts5ParseNodeFree(pRight); |
| 2481 | sqlite3Fts5ParseNearsetFree(pNear); |
| 2482 | } |
| 2483 | return pRet; |
| 2484 | } |
| 2485 | |
| 2486 | Fts5ExprNode *sqlite3Fts5ParseImplicitAnd( |
| 2487 | Fts5Parse *pParse, /* Parse context */ |
| 2488 | Fts5ExprNode *pLeft, /* Left hand child expression */ |
| 2489 | Fts5ExprNode *pRight /* Right hand child expression */ |
| 2490 | ){ |
| 2491 | Fts5ExprNode *pRet = 0; |
| 2492 | Fts5ExprNode *pPrev; |
| 2493 | |
| 2494 | if( pParse->rc ){ |
| 2495 | sqlite3Fts5ParseNodeFree(pLeft); |
| 2496 | sqlite3Fts5ParseNodeFree(pRight); |
| 2497 | }else{ |
| 2498 | |
| 2499 | assert( pLeft->eType==FTS5_STRING |
| 2500 | || pLeft->eType==FTS5_TERM |
| 2501 | || pLeft->eType==FTS5_EOF |
| 2502 | || pLeft->eType==FTS5_AND |
| 2503 | ); |
| 2504 | assert( pRight->eType==FTS5_STRING |
| 2505 | || pRight->eType==FTS5_TERM |
| 2506 | || pRight->eType==FTS5_EOF |
| 2507 | || (pRight->eType==FTS5_AND && pParse->bPhraseToAnd) |
| 2508 | ); |
| 2509 | |
| 2510 | if( pLeft->eType==FTS5_AND ){ |
| 2511 | pPrev = pLeft->apChild[pLeft->nChild-1]; |
| 2512 | }else{ |
| 2513 | pPrev = pLeft; |
| 2514 | } |
| 2515 | assert( pPrev->eType==FTS5_STRING |
| 2516 | || pPrev->eType==FTS5_TERM |
| 2517 | || pPrev->eType==FTS5_EOF |
| 2518 | ); |
| 2519 | |
| 2520 | if( pRight->eType==FTS5_EOF ){ |
| 2521 | assert( pParse->apPhrase!=0 ); |
| 2522 | assert( pParse->nPhrase>0 ); |
| 2523 | assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] ); |
| 2524 | sqlite3Fts5ParseNodeFree(pRight); |
| 2525 | pRet = pLeft; |
| 2526 | pParse->nPhrase--; |
| 2527 | } |
| 2528 | else if( pPrev->eType==FTS5_EOF ){ |
| 2529 | Fts5ExprPhrase **ap; |
| 2530 | |
| 2531 | if( pPrev==pLeft ){ |
| 2532 | pRet = pRight; |
| 2533 | }else{ |
| 2534 | pLeft->apChild[pLeft->nChild-1] = pRight; |
| 2535 | pRet = pLeft; |
| 2536 | } |
| 2537 | |
| 2538 | ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase]; |
| 2539 | assert( ap[0]==pPrev->pNear->apPhrase[0] ); |
| 2540 | memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase); |
| 2541 | pParse->nPhrase--; |
| 2542 | |
| 2543 | sqlite3Fts5ParseNodeFree(pPrev); |
| 2544 | } |
| 2545 | else{ |
| 2546 | pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0); |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | return pRet; |
| 2551 | } |
| 2552 | |
| 2553 | #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) |
| 2554 | static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){ |
| 2555 | sqlite3_int64 nByte = 0; |
| 2556 | Fts5ExprTerm *p; |
| 2557 | char *zQuoted; |
| 2558 | |
| 2559 | /* Determine the maximum amount of space required. */ |
| 2560 | for(p=pTerm; p; p=p->pSynonym){ |
| 2561 | nByte += pTerm->nQueryTerm * 2 + 3 + 2; |
| 2562 | } |
| 2563 | zQuoted = sqlite3_malloc64(nByte); |
| 2564 | |
| 2565 | if( zQuoted ){ |
| 2566 | int i = 0; |
| 2567 | for(p=pTerm; p; p=p->pSynonym){ |
| 2568 | char *zIn = p->pTerm; |
| 2569 | char *zEnd = &zIn[p->nQueryTerm]; |
| 2570 | zQuoted[i++] = '"'; |
| 2571 | while( zIn<zEnd ){ |
| 2572 | if( *zIn=='"' ) zQuoted[i++] = '"'; |
| 2573 | zQuoted[i++] = *zIn++; |
| 2574 | } |
| 2575 | zQuoted[i++] = '"'; |
| 2576 | if( p->pSynonym ) zQuoted[i++] = '|'; |
| 2577 | } |
| 2578 | if( pTerm->bPrefix ){ |
| 2579 | zQuoted[i++] = ' '; |
| 2580 | zQuoted[i++] = '*'; |
| 2581 | } |
| 2582 | zQuoted[i++] = '\0'; |
| 2583 | } |
| 2584 | return zQuoted; |
| 2585 | } |
| 2586 | |
| 2587 | static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){ |
| 2588 | char *zNew; |
| 2589 | va_list ap; |
| 2590 | va_start(ap, zFmt); |
| 2591 | zNew = sqlite3_vmprintf(zFmt, ap); |
| 2592 | va_end(ap); |
| 2593 | if( zApp && zNew ){ |
| 2594 | char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew); |
| 2595 | sqlite3_free(zNew); |
| 2596 | zNew = zNew2; |
| 2597 | } |
| 2598 | sqlite3_free(zApp); |
| 2599 | return zNew; |
| 2600 | } |
| 2601 | |
| 2602 | /* |
| 2603 | ** Compose a tcl-readable representation of expression pExpr. Return a |
| 2604 | ** pointer to a buffer containing that representation. It is the |
| 2605 | ** responsibility of the caller to at some point free the buffer using |
| 2606 | ** sqlite3_free(). |
| 2607 | */ |
| 2608 | static char *fts5ExprPrintTcl( |
| 2609 | Fts5Config *pConfig, |
| 2610 | const char *zNearsetCmd, |
| 2611 | Fts5ExprNode *pExpr |
| 2612 | ){ |
| 2613 | char *zRet = 0; |
| 2614 | if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){ |
| 2615 | Fts5ExprNearset *pNear = pExpr->pNear; |
| 2616 | int i; |
| 2617 | int iTerm; |
| 2618 | |
| 2619 | zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd); |
| 2620 | if( zRet==0 ) return 0; |
| 2621 | if( pNear->pColset ){ |
| 2622 | int *aiCol = pNear->pColset->aiCol; |
| 2623 | int nCol = pNear->pColset->nCol; |
| 2624 | if( nCol==1 ){ |
| 2625 | zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]); |
| 2626 | }else{ |
| 2627 | zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]); |
| 2628 | for(i=1; i<pNear->pColset->nCol; i++){ |
| 2629 | zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]); |
| 2630 | } |
| 2631 | zRet = fts5PrintfAppend(zRet, "} "); |
| 2632 | } |
| 2633 | if( zRet==0 ) return 0; |
| 2634 | } |
| 2635 | |
| 2636 | if( pNear->nPhrase>1 ){ |
| 2637 | zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear); |
| 2638 | if( zRet==0 ) return 0; |
| 2639 | } |
| 2640 | |
| 2641 | zRet = fts5PrintfAppend(zRet, "--"); |
| 2642 | if( zRet==0 ) return 0; |
| 2643 | |
| 2644 | for(i=0; i<pNear->nPhrase; i++){ |
| 2645 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
| 2646 | |
| 2647 | zRet = fts5PrintfAppend(zRet, " {"); |
| 2648 | for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){ |
| 2649 | Fts5ExprTerm *p = &pPhrase->aTerm[iTerm]; |
| 2650 | zRet = fts5PrintfAppend(zRet, "%s%.*s", iTerm==0?"":" ", |
| 2651 | p->nQueryTerm, p->pTerm |
| 2652 | ); |
| 2653 | if( pPhrase->aTerm[iTerm].bPrefix ){ |
| 2654 | zRet = fts5PrintfAppend(zRet, "*"); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | if( zRet ) zRet = fts5PrintfAppend(zRet, "}"); |
| 2659 | if( zRet==0 ) return 0; |
| 2660 | } |
| 2661 | |
| 2662 | }else if( pExpr->eType==0 ){ |
| 2663 | zRet = sqlite3_mprintf("{}"); |
| 2664 | }else{ |
| 2665 | char const *zOp = 0; |
| 2666 | int i; |
| 2667 | switch( pExpr->eType ){ |
| 2668 | case FTS5_AND: zOp = "AND"; break; |
| 2669 | case FTS5_NOT: zOp = "NOT"; break; |
| 2670 | default: |
| 2671 | assert( pExpr->eType==FTS5_OR ); |
| 2672 | zOp = "OR"; |
| 2673 | break; |
| 2674 | } |
| 2675 | |
| 2676 | zRet = sqlite3_mprintf("%s", zOp); |
| 2677 | for(i=0; zRet && i<pExpr->nChild; i++){ |
| 2678 | char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]); |
| 2679 | if( !z ){ |
| 2680 | sqlite3_free(zRet); |
| 2681 | zRet = 0; |
| 2682 | }else{ |
| 2683 | zRet = fts5PrintfAppend(zRet, " [%z]", z); |
| 2684 | } |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | return zRet; |
| 2689 | } |
| 2690 | |
| 2691 | static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){ |
| 2692 | char *zRet = 0; |
| 2693 | if( pExpr->eType==0 ){ |
| 2694 | return sqlite3_mprintf("\"\""); |
| 2695 | }else |
| 2696 | if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){ |
| 2697 | Fts5ExprNearset *pNear = pExpr->pNear; |
| 2698 | int i; |
| 2699 | int iTerm; |
| 2700 | |
| 2701 | if( pNear->pColset ){ |
| 2702 | int ii; |
| 2703 | Fts5Colset *pColset = pNear->pColset; |
| 2704 | if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{"); |
| 2705 | for(ii=0; ii<pColset->nCol; ii++){ |
| 2706 | zRet = fts5PrintfAppend(zRet, "%s%s", |
| 2707 | pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " " |
| 2708 | ); |
| 2709 | } |
| 2710 | if( zRet ){ |
| 2711 | zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : ""); |
| 2712 | } |
| 2713 | if( zRet==0 ) return 0; |
| 2714 | } |
| 2715 | |
| 2716 | if( pNear->nPhrase>1 ){ |
| 2717 | zRet = fts5PrintfAppend(zRet, "NEAR("); |
| 2718 | if( zRet==0 ) return 0; |
| 2719 | } |
| 2720 | |
| 2721 | for(i=0; i<pNear->nPhrase; i++){ |
| 2722 | Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
| 2723 | if( i!=0 ){ |
| 2724 | zRet = fts5PrintfAppend(zRet, " "); |
| 2725 | if( zRet==0 ) return 0; |
| 2726 | } |
| 2727 | for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){ |
| 2728 | char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]); |
| 2729 | if( zTerm ){ |
| 2730 | zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm); |
| 2731 | sqlite3_free(zTerm); |
| 2732 | } |
| 2733 | if( zTerm==0 || zRet==0 ){ |
| 2734 | sqlite3_free(zRet); |
| 2735 | return 0; |
| 2736 | } |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | if( pNear->nPhrase>1 ){ |
| 2741 | zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear); |
| 2742 | if( zRet==0 ) return 0; |
| 2743 | } |
| 2744 | |
| 2745 | }else{ |
| 2746 | char const *zOp = 0; |
| 2747 | int i; |
| 2748 | |
| 2749 | switch( pExpr->eType ){ |
| 2750 | case FTS5_AND: zOp = " AND "; break; |
| 2751 | case FTS5_NOT: zOp = " NOT "; break; |
| 2752 | default: |
| 2753 | assert( pExpr->eType==FTS5_OR ); |
| 2754 | zOp = " OR "; |
| 2755 | break; |
| 2756 | } |
| 2757 | |
| 2758 | for(i=0; i<pExpr->nChild; i++){ |
| 2759 | char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]); |
| 2760 | if( z==0 ){ |
| 2761 | sqlite3_free(zRet); |
| 2762 | zRet = 0; |
| 2763 | }else{ |
| 2764 | int e = pExpr->apChild[i]->eType; |
| 2765 | int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF); |
| 2766 | zRet = fts5PrintfAppend(zRet, "%s%s%z%s", |
| 2767 | (i==0 ? "" : zOp), |
| 2768 | (b?"(":""), z, (b?")":"") |
| 2769 | ); |
| 2770 | } |
| 2771 | if( zRet==0 ) break; |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | return zRet; |
| 2776 | } |
| 2777 | |
| 2778 | /* |
| 2779 | ** The implementation of user-defined scalar functions fts5_expr() (bTcl==0) |
| 2780 | ** and fts5_expr_tcl() (bTcl!=0). |
| 2781 | */ |
| 2782 | static void fts5ExprFunction( |
| 2783 | sqlite3_context *pCtx, /* Function call context */ |
| 2784 | int nArg, /* Number of args */ |
| 2785 | sqlite3_value **apVal, /* Function arguments */ |
| 2786 | int bTcl |
| 2787 | ){ |
| 2788 | Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx); |
| 2789 | sqlite3 *db = sqlite3_context_db_handle(pCtx); |
| 2790 | const char *zExpr = 0; |
| 2791 | char *zErr = 0; |
| 2792 | Fts5Expr *pExpr = 0; |
| 2793 | int rc; |
| 2794 | int i; |
| 2795 | |
| 2796 | const char **azConfig; /* Array of arguments for Fts5Config */ |
| 2797 | const char *zNearsetCmd = "nearset"; |
| 2798 | int nConfig; /* Size of azConfig[] */ |
| 2799 | Fts5Config *pConfig = 0; |
| 2800 | int iArg = 1; |
| 2801 | |
| 2802 | if( nArg<1 ){ |
| 2803 | zErr = sqlite3_mprintf("wrong number of arguments to function %s", |
| 2804 | bTcl ? "fts5_expr_tcl" : "fts5_expr" |
| 2805 | ); |
| 2806 | sqlite3_result_error(pCtx, zErr, -1); |
| 2807 | sqlite3_free(zErr); |
| 2808 | return; |
| 2809 | } |
| 2810 | |
| 2811 | if( bTcl && nArg>1 ){ |
| 2812 | zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]); |
| 2813 | iArg = 2; |
| 2814 | } |
| 2815 | |
| 2816 | nConfig = 3 + (nArg-iArg); |
| 2817 | azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig); |
| 2818 | if( azConfig==0 ){ |
| 2819 | sqlite3_result_error_nomem(pCtx); |
| 2820 | return; |
| 2821 | } |
| 2822 | azConfig[0] = 0; |
| 2823 | azConfig[1] = "main"; |
| 2824 | azConfig[2] = "tbl"; |
| 2825 | for(i=3; iArg<nArg; iArg++){ |
| 2826 | const char *z = (const char*)sqlite3_value_text(apVal[iArg]); |
| 2827 | azConfig[i++] = (z ? z : ""); |
| 2828 | } |
| 2829 | |
| 2830 | zExpr = (const char*)sqlite3_value_text(apVal[0]); |
| 2831 | if( zExpr==0 ) zExpr = ""; |
| 2832 | |
| 2833 | rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr); |
| 2834 | if( rc==SQLITE_OK ){ |
| 2835 | rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr); |
| 2836 | } |
| 2837 | if( rc==SQLITE_OK ){ |
| 2838 | char *zText; |
| 2839 | if( pExpr->pRoot->xNext==0 ){ |
| 2840 | zText = sqlite3_mprintf(""); |
| 2841 | }else if( bTcl ){ |
| 2842 | zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot); |
| 2843 | }else{ |
| 2844 | zText = fts5ExprPrint(pConfig, pExpr->pRoot); |
| 2845 | } |
| 2846 | if( zText==0 ){ |
| 2847 | rc = SQLITE_NOMEM; |
| 2848 | }else{ |
| 2849 | sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT); |
| 2850 | sqlite3_free(zText); |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | if( rc!=SQLITE_OK ){ |
| 2855 | if( zErr ){ |
| 2856 | sqlite3_result_error(pCtx, zErr, -1); |
| 2857 | sqlite3_free(zErr); |
| 2858 | }else{ |
| 2859 | sqlite3_result_error_code(pCtx, rc); |
| 2860 | } |
| 2861 | } |
| 2862 | sqlite3_free((void *)azConfig); |
| 2863 | sqlite3Fts5ConfigFree(pConfig); |
| 2864 | sqlite3Fts5ExprFree(pExpr); |
| 2865 | } |
| 2866 | |
| 2867 | static void fts5ExprFunctionHr( |
| 2868 | sqlite3_context *pCtx, /* Function call context */ |
| 2869 | int nArg, /* Number of args */ |
| 2870 | sqlite3_value **apVal /* Function arguments */ |
| 2871 | ){ |
| 2872 | fts5ExprFunction(pCtx, nArg, apVal, 0); |
| 2873 | } |
| 2874 | static void fts5ExprFunctionTcl( |
| 2875 | sqlite3_context *pCtx, /* Function call context */ |
| 2876 | int nArg, /* Number of args */ |
| 2877 | sqlite3_value **apVal /* Function arguments */ |
| 2878 | ){ |
| 2879 | fts5ExprFunction(pCtx, nArg, apVal, 1); |
| 2880 | } |
| 2881 | |
| 2882 | /* |
| 2883 | ** The implementation of an SQLite user-defined-function that accepts a |
| 2884 | ** single integer as an argument. If the integer is an alpha-numeric |
| 2885 | ** unicode code point, 1 is returned. Otherwise 0. |
| 2886 | */ |
| 2887 | static void fts5ExprIsAlnum( |
| 2888 | sqlite3_context *pCtx, /* Function call context */ |
| 2889 | int nArg, /* Number of args */ |
| 2890 | sqlite3_value **apVal /* Function arguments */ |
| 2891 | ){ |
| 2892 | int iCode; |
| 2893 | u8 aArr[32]; |
| 2894 | if( nArg!=1 ){ |
| 2895 | sqlite3_result_error(pCtx, |
| 2896 | "wrong number of arguments to function fts5_isalnum", -1 |
| 2897 | ); |
| 2898 | return; |
| 2899 | } |
| 2900 | memset(aArr, 0, sizeof(aArr)); |
| 2901 | sqlite3Fts5UnicodeCatParse("L*", aArr); |
| 2902 | sqlite3Fts5UnicodeCatParse("N*", aArr); |
| 2903 | sqlite3Fts5UnicodeCatParse("Co", aArr); |
| 2904 | iCode = sqlite3_value_int(apVal[0]); |
| 2905 | sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]); |
| 2906 | } |
| 2907 | |
| 2908 | static void fts5ExprFold( |
| 2909 | sqlite3_context *pCtx, /* Function call context */ |
| 2910 | int nArg, /* Number of args */ |
| 2911 | sqlite3_value **apVal /* Function arguments */ |
| 2912 | ){ |
| 2913 | if( nArg!=1 && nArg!=2 ){ |
| 2914 | sqlite3_result_error(pCtx, |
| 2915 | "wrong number of arguments to function fts5_fold", -1 |
| 2916 | ); |
| 2917 | }else{ |
| 2918 | int iCode; |
| 2919 | int bRemoveDiacritics = 0; |
| 2920 | iCode = sqlite3_value_int(apVal[0]); |
| 2921 | if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]); |
| 2922 | sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics)); |
| 2923 | } |
| 2924 | } |
| 2925 | #endif /* if SQLITE_TEST || SQLITE_FTS5_DEBUG */ |
| 2926 | |
| 2927 | /* |
| 2928 | ** This is called during initialization to register the fts5_expr() scalar |
| 2929 | ** UDF with the SQLite handle passed as the only argument. |
| 2930 | */ |
| 2931 | int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){ |
| 2932 | #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) |
| 2933 | struct Fts5ExprFunc { |
| 2934 | const char *z; |
| 2935 | void (*x)(sqlite3_context*,int,sqlite3_value**); |
| 2936 | } aFunc[] = { |
| 2937 | { "fts5_expr", fts5ExprFunctionHr }, |
| 2938 | { "fts5_expr_tcl", fts5ExprFunctionTcl }, |
| 2939 | { "fts5_isalnum", fts5ExprIsAlnum }, |
| 2940 | { "fts5_fold", fts5ExprFold }, |
| 2941 | }; |
| 2942 | int i; |
| 2943 | int rc = SQLITE_OK; |
| 2944 | void *pCtx = (void*)pGlobal; |
| 2945 | |
| 2946 | for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){ |
| 2947 | struct Fts5ExprFunc *p = &aFunc[i]; |
| 2948 | rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0); |
| 2949 | } |
| 2950 | #else |
| 2951 | int rc = SQLITE_OK; |
| 2952 | UNUSED_PARAM2(pGlobal,db); |
| 2953 | #endif |
| 2954 | |
| 2955 | /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and |
| 2956 | ** sqlite3Fts5ParserFallback() are unused */ |
| 2957 | #ifndef NDEBUG |
| 2958 | (void)sqlite3Fts5ParserTrace; |
| 2959 | #endif |
| 2960 | (void)sqlite3Fts5ParserFallback; |
| 2961 | |
| 2962 | return rc; |
| 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | ** Return the number of phrases in expression pExpr. |
| 2967 | */ |
| 2968 | int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){ |
| 2969 | return (pExpr ? pExpr->nPhrase : 0); |
| 2970 | } |
| 2971 | |
| 2972 | /* |
| 2973 | ** Return the number of terms in the iPhrase'th phrase in pExpr. |
| 2974 | */ |
| 2975 | int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){ |
| 2976 | if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0; |
| 2977 | return pExpr->apExprPhrase[iPhrase]->nTerm; |
| 2978 | } |
| 2979 | |
| 2980 | /* |
| 2981 | ** This function is used to access the current position list for phrase |
| 2982 | ** iPhrase. |
| 2983 | */ |
| 2984 | int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){ |
| 2985 | int nRet; |
| 2986 | Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase]; |
| 2987 | Fts5ExprNode *pNode = pPhrase->pNode; |
| 2988 | if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){ |
| 2989 | *pa = pPhrase->poslist.p; |
| 2990 | nRet = pPhrase->poslist.n; |
| 2991 | }else{ |
| 2992 | *pa = 0; |
| 2993 | nRet = 0; |
| 2994 | } |
| 2995 | return nRet; |
| 2996 | } |
| 2997 | |
| 2998 | struct Fts5PoslistPopulator { |
| 2999 | Fts5PoslistWriter writer; |
| 3000 | int bOk; /* True if ok to populate */ |
| 3001 | int bMiss; |
| 3002 | }; |
| 3003 | |
| 3004 | /* |
| 3005 | ** Clear the position lists associated with all phrases in the expression |
| 3006 | ** passed as the first argument. Argument bLive is true if the expression |
| 3007 | ** might be pointing to a real entry, otherwise it has just been reset. |
| 3008 | ** |
| 3009 | ** At present this function is only used for detail=col and detail=none |
| 3010 | ** fts5 tables. This implies that all phrases must be at most 1 token |
| 3011 | ** in size, as phrase matches are not supported without detail=full. |
| 3012 | */ |
| 3013 | Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){ |
| 3014 | Fts5PoslistPopulator *pRet; |
| 3015 | pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
| 3016 | if( pRet ){ |
| 3017 | int i; |
| 3018 | memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
| 3019 | for(i=0; i<pExpr->nPhrase; i++){ |
| 3020 | Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist; |
| 3021 | Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode; |
| 3022 | assert( pExpr->apExprPhrase[i]->nTerm<=1 ); |
| 3023 | if( bLive && |
| 3024 | (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof) |
| 3025 | ){ |
| 3026 | pRet[i].bMiss = 1; |
| 3027 | }else{ |
| 3028 | pBuf->n = 0; |
| 3029 | } |
| 3030 | } |
| 3031 | } |
| 3032 | return pRet; |
| 3033 | } |
| 3034 | |
| 3035 | struct Fts5ExprCtx { |
| 3036 | Fts5Expr *pExpr; |
| 3037 | Fts5PoslistPopulator *aPopulator; |
| 3038 | i64 iOff; |
| 3039 | }; |
| 3040 | typedef struct Fts5ExprCtx Fts5ExprCtx; |
| 3041 | |
| 3042 | /* |
| 3043 | ** TODO: Make this more efficient! |
| 3044 | */ |
| 3045 | static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){ |
| 3046 | int i; |
| 3047 | for(i=0; i<pColset->nCol; i++){ |
| 3048 | if( pColset->aiCol[i]==iCol ) return 1; |
| 3049 | } |
| 3050 | return 0; |
| 3051 | } |
| 3052 | |
| 3053 | /* |
| 3054 | ** pToken is a buffer nToken bytes in size that may or may not contain |
| 3055 | ** an embedded 0x00 byte. If it does, return the number of bytes in |
| 3056 | ** the buffer before the 0x00. If it does not, return nToken. |
| 3057 | */ |
| 3058 | static int fts5QueryTerm(const char *pToken, int nToken){ |
| 3059 | int ii; |
| 3060 | for(ii=0; ii<nToken && pToken[ii]; ii++){} |
| 3061 | return ii; |
| 3062 | } |
| 3063 | |
| 3064 | static int fts5ExprPopulatePoslistsCb( |
| 3065 | void *pCtx, /* Copy of 2nd argument to xTokenize() */ |
| 3066 | int tflags, /* Mask of FTS5_TOKEN_* flags */ |
| 3067 | const char *pToken, /* Pointer to buffer containing token */ |
| 3068 | int nToken, /* Size of token in bytes */ |
| 3069 | int iUnused1, /* Byte offset of token within input text */ |
| 3070 | int iUnused2 /* Byte offset of end of token within input text */ |
| 3071 | ){ |
| 3072 | Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx; |
| 3073 | Fts5Expr *pExpr = p->pExpr; |
| 3074 | int i; |
| 3075 | int nQuery = nToken; |
| 3076 | i64 iRowid = pExpr->pRoot->iRowid; |
| 3077 | |
| 3078 | UNUSED_PARAM2(iUnused1, iUnused2); |
| 3079 | |
| 3080 | if( nQuery>FTS5_MAX_TOKEN_SIZE ) nQuery = FTS5_MAX_TOKEN_SIZE; |
| 3081 | if( pExpr->pConfig->bTokendata ){ |
| 3082 | nQuery = fts5QueryTerm(pToken, nQuery); |
| 3083 | } |
| 3084 | if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++; |
| 3085 | for(i=0; i<pExpr->nPhrase; i++){ |
| 3086 | Fts5ExprTerm *pT; |
| 3087 | if( p->aPopulator[i].bOk==0 ) continue; |
| 3088 | for(pT=&pExpr->apExprPhrase[i]->aTerm[0]; pT; pT=pT->pSynonym){ |
| 3089 | if( (pT->nQueryTerm==nQuery || (pT->nQueryTerm<nQuery && pT->bPrefix)) |
| 3090 | && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0 |
| 3091 | ){ |
| 3092 | int rc = sqlite3Fts5PoslistWriterAppend( |
| 3093 | &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff |
| 3094 | ); |
| 3095 | if( rc==SQLITE_OK && (pExpr->pConfig->bTokendata || pT->bPrefix) ){ |
| 3096 | int iCol = p->iOff>>32; |
| 3097 | int iTokOff = p->iOff & 0x7FFFFFFF; |
| 3098 | rc = sqlite3Fts5IndexIterWriteTokendata( |
| 3099 | pT->pIter, pToken, nToken, iRowid, iCol, iTokOff |
| 3100 | ); |
| 3101 | } |
| 3102 | if( rc ) return rc; |
| 3103 | break; |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | return SQLITE_OK; |
| 3108 | } |
| 3109 | |
| 3110 | int sqlite3Fts5ExprPopulatePoslists( |
| 3111 | Fts5Config *pConfig, |
| 3112 | Fts5Expr *pExpr, |
| 3113 | Fts5PoslistPopulator *aPopulator, |
| 3114 | int iCol, |
| 3115 | const char *z, int n |
| 3116 | ){ |
| 3117 | int i; |
| 3118 | Fts5ExprCtx sCtx; |
| 3119 | sCtx.pExpr = pExpr; |
| 3120 | sCtx.aPopulator = aPopulator; |
| 3121 | sCtx.iOff = (((i64)iCol) << 32) - 1; |
| 3122 | |
| 3123 | for(i=0; i<pExpr->nPhrase; i++){ |
| 3124 | Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode; |
| 3125 | Fts5Colset *pColset = pNode->pNear->pColset; |
| 3126 | if( (pColset && 0==fts5ExprColsetTest(pColset, iCol)) |
| 3127 | || aPopulator[i].bMiss |
| 3128 | ){ |
| 3129 | aPopulator[i].bOk = 0; |
| 3130 | }else{ |
| 3131 | aPopulator[i].bOk = 1; |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | return sqlite3Fts5Tokenize(pConfig, |
| 3136 | FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb |
| 3137 | ); |
| 3138 | } |
| 3139 | |
| 3140 | static void fts5ExprClearPoslists(Fts5ExprNode *pNode){ |
| 3141 | if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){ |
| 3142 | pNode->pNear->apPhrase[0]->poslist.n = 0; |
| 3143 | }else{ |
| 3144 | int i; |
| 3145 | for(i=0; i<pNode->nChild; i++){ |
| 3146 | fts5ExprClearPoslists(pNode->apChild[i]); |
| 3147 | } |
| 3148 | } |
| 3149 | } |
| 3150 | |
| 3151 | static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){ |
| 3152 | pNode->iRowid = iRowid; |
| 3153 | pNode->bEof = 0; |
| 3154 | switch( pNode->eType ){ |
| 3155 | case 0: |
| 3156 | case FTS5_TERM: |
| 3157 | case FTS5_STRING: |
| 3158 | return (pNode->pNear->apPhrase[0]->poslist.n>0); |
| 3159 | |
| 3160 | case FTS5_AND: { |
| 3161 | int i; |
| 3162 | for(i=0; i<pNode->nChild; i++){ |
| 3163 | if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){ |
| 3164 | fts5ExprClearPoslists(pNode); |
| 3165 | return 0; |
| 3166 | } |
| 3167 | } |
| 3168 | break; |
| 3169 | } |
| 3170 | |
| 3171 | case FTS5_OR: { |
| 3172 | int i; |
| 3173 | int bRet = 0; |
| 3174 | for(i=0; i<pNode->nChild; i++){ |
| 3175 | if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){ |
| 3176 | bRet = 1; |
| 3177 | } |
| 3178 | } |
| 3179 | return bRet; |
| 3180 | } |
| 3181 | |
| 3182 | default: { |
| 3183 | assert( pNode->eType==FTS5_NOT ); |
| 3184 | if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid) |
| 3185 | || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid) |
| 3186 | ){ |
| 3187 | fts5ExprClearPoslists(pNode); |
| 3188 | return 0; |
| 3189 | } |
| 3190 | break; |
| 3191 | } |
| 3192 | } |
| 3193 | return 1; |
| 3194 | } |
| 3195 | |
| 3196 | void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){ |
| 3197 | fts5ExprCheckPoslists(pExpr->pRoot, iRowid); |
| 3198 | } |
| 3199 | |
| 3200 | /* |
| 3201 | ** This function is only called for detail=columns tables. |
| 3202 | */ |
| 3203 | int sqlite3Fts5ExprPhraseCollist( |
| 3204 | Fts5Expr *pExpr, |
| 3205 | int iPhrase, |
| 3206 | const u8 **ppCollist, |
| 3207 | int *pnCollist |
| 3208 | ){ |
| 3209 | Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase]; |
| 3210 | Fts5ExprNode *pNode = pPhrase->pNode; |
| 3211 | int rc = SQLITE_OK; |
| 3212 | |
| 3213 | assert( iPhrase>=0 && iPhrase<pExpr->nPhrase ); |
| 3214 | assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS ); |
| 3215 | |
| 3216 | if( pNode->bEof==0 |
| 3217 | && pNode->iRowid==pExpr->pRoot->iRowid |
| 3218 | && pPhrase->poslist.n>0 |
| 3219 | ){ |
| 3220 | Fts5ExprTerm *pTerm = &pPhrase->aTerm[0]; |
| 3221 | if( pTerm->pSynonym ){ |
| 3222 | Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1]; |
| 3223 | rc = fts5ExprSynonymList( |
| 3224 | pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist |
| 3225 | ); |
| 3226 | }else{ |
| 3227 | *ppCollist = pPhrase->aTerm[0].pIter->pData; |
| 3228 | *pnCollist = pPhrase->aTerm[0].pIter->nData; |
| 3229 | } |
| 3230 | }else{ |
| 3231 | *ppCollist = 0; |
| 3232 | *pnCollist = 0; |
| 3233 | } |
| 3234 | |
| 3235 | return rc; |
| 3236 | } |
| 3237 | |
| 3238 | /* |
| 3239 | ** Does the work of the fts5_api.xQueryToken() API method. |
| 3240 | */ |
| 3241 | int sqlite3Fts5ExprQueryToken( |
| 3242 | Fts5Expr *pExpr, |
| 3243 | int iPhrase, |
| 3244 | int iToken, |
| 3245 | const char **ppOut, |
| 3246 | int *pnOut |
| 3247 | ){ |
| 3248 | Fts5ExprPhrase *pPhrase = 0; |
| 3249 | |
| 3250 | if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){ |
| 3251 | return SQLITE_RANGE; |
| 3252 | } |
| 3253 | pPhrase = pExpr->apExprPhrase[iPhrase]; |
| 3254 | if( iToken<0 || iToken>=pPhrase->nTerm ){ |
| 3255 | return SQLITE_RANGE; |
| 3256 | } |
| 3257 | |
| 3258 | *ppOut = pPhrase->aTerm[iToken].pTerm; |
| 3259 | *pnOut = pPhrase->aTerm[iToken].nFullTerm; |
| 3260 | return SQLITE_OK; |
| 3261 | } |
| 3262 | |
| 3263 | /* |
| 3264 | ** Does the work of the fts5_api.xInstToken() API method. |
| 3265 | */ |
| 3266 | int sqlite3Fts5ExprInstToken( |
| 3267 | Fts5Expr *pExpr, |
| 3268 | i64 iRowid, |
| 3269 | int iPhrase, |
| 3270 | int iCol, |
| 3271 | int iOff, |
| 3272 | int iToken, |
| 3273 | const char **ppOut, |
| 3274 | int *pnOut |
| 3275 | ){ |
| 3276 | Fts5ExprPhrase *pPhrase = 0; |
| 3277 | Fts5ExprTerm *pTerm = 0; |
| 3278 | int rc = SQLITE_OK; |
| 3279 | |
| 3280 | if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){ |
| 3281 | return SQLITE_RANGE; |
| 3282 | } |
| 3283 | pPhrase = pExpr->apExprPhrase[iPhrase]; |
| 3284 | if( iToken<0 || iToken>=pPhrase->nTerm ){ |
| 3285 | return SQLITE_RANGE; |
| 3286 | } |
| 3287 | pTerm = &pPhrase->aTerm[iToken]; |
| 3288 | if( pExpr->pConfig->bTokendata || pTerm->bPrefix ){ |
| 3289 | rc = sqlite3Fts5IterToken( |
| 3290 | pTerm->pIter, pTerm->pTerm, pTerm->nQueryTerm, |
| 3291 | iRowid, iCol, iOff+iToken, ppOut, pnOut |
| 3292 | ); |
| 3293 | }else{ |
| 3294 | *ppOut = pTerm->pTerm; |
| 3295 | *pnOut = pTerm->nFullTerm; |
| 3296 | } |
| 3297 | return rc; |
| 3298 | } |
| 3299 | |
| 3300 | /* |
| 3301 | ** Clear the token mappings for all Fts5IndexIter objects managed by |
| 3302 | ** the expression passed as the only argument. |
| 3303 | */ |
| 3304 | void sqlite3Fts5ExprClearTokens(Fts5Expr *pExpr){ |
| 3305 | int ii; |
| 3306 | for(ii=0; ii<pExpr->nPhrase; ii++){ |
| 3307 | Fts5ExprTerm *pT; |
| 3308 | for(pT=&pExpr->apExprPhrase[ii]->aTerm[0]; pT; pT=pT->pSynonym){ |
| 3309 | sqlite3Fts5IndexIterClearTokendata(pT->pIter); |
| 3310 | } |
| 3311 | } |
| 3312 | } |