Fixes default log output to console for macOS
[sqlcipher.git] / ext / fts5 / fts5_expr.c
blob960a4d46871665f59d11831e75f990af0f2d8744
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.
11 ******************************************************************************
17 #include "fts5Int.h"
18 #include "fts5parse.h"
20 #ifndef SQLITE_FTS5_MAX_EXPR_DEPTH
21 # define SQLITE_FTS5_MAX_EXPR_DEPTH 256
22 #endif
25 ** All token types in the generated fts5parse.h file are greater than 0.
27 #define FTS5_EOF 0
29 #define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
31 typedef struct Fts5ExprTerm Fts5ExprTerm;
34 ** Functions generated by lemon from fts5parse.y.
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);
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 */
56 ** eType:
57 ** Expression node type. Always one of:
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)
65 ** iHeight:
66 ** Distance from this node to furthest leaf. This is always 0 for nodes
67 ** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one
68 ** greater than the largest child value.
70 struct Fts5ExprNode {
71 int eType; /* Node type */
72 int bEof; /* True at EOF */
73 int bNomatch; /* True if entry is not a match */
74 int iHeight; /* Distance to tree leaf nodes */
76 /* Next method for this node. */
77 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
79 i64 iRowid; /* Current rowid */
80 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
82 /* Child nodes. For a NOT node, this array always contains 2 entries. For
83 ** AND or OR nodes, it contains 2 or more entries. */
84 int nChild; /* Number of child nodes */
85 Fts5ExprNode *apChild[1]; /* Array of child nodes */
88 #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
91 ** Invoke the xNext method of an Fts5ExprNode object. This macro should be
92 ** used as if it has the same signature as the xNext() methods themselves.
94 #define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
97 ** An instance of the following structure represents a single search term
98 ** or term prefix.
100 struct Fts5ExprTerm {
101 u8 bPrefix; /* True for a prefix term */
102 u8 bFirst; /* True if token must be first in column */
103 char *pTerm; /* Term data */
104 int nQueryTerm; /* Effective size of term in bytes */
105 int nFullTerm; /* Size of term in bytes incl. tokendata */
106 Fts5IndexIter *pIter; /* Iterator for this term */
107 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
111 ** A phrase. One or more terms that must appear in a contiguous sequence
112 ** within a document for it to match.
114 struct Fts5ExprPhrase {
115 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
116 Fts5Buffer poslist; /* Current position list */
117 int nTerm; /* Number of entries in aTerm[] */
118 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
122 ** One or more phrases that must appear within a certain token distance of
123 ** each other within each matching document.
125 struct Fts5ExprNearset {
126 int nNear; /* NEAR parameter */
127 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
128 int nPhrase; /* Number of entries in aPhrase[] array */
129 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
134 ** Parse context.
136 struct Fts5Parse {
137 Fts5Config *pConfig;
138 char *zErr;
139 int rc;
140 int nPhrase; /* Size of apPhrase array */
141 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
142 Fts5ExprNode *pExpr; /* Result of a successful parse */
143 int bPhraseToAnd; /* Convert "a+b" to "a AND b" */
147 ** Check that the Fts5ExprNode.iHeight variables are set correctly in
148 ** the expression tree passed as the only argument.
150 #ifndef NDEBUG
151 static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){
152 if( rc==SQLITE_OK ){
153 if( p->eType==FTS5_TERM || p->eType==FTS5_STRING || p->eType==0 ){
154 assert( p->iHeight==0 );
155 }else{
156 int ii;
157 int iMaxChild = 0;
158 for(ii=0; ii<p->nChild; ii++){
159 Fts5ExprNode *pChild = p->apChild[ii];
160 iMaxChild = MAX(iMaxChild, pChild->iHeight);
161 assert_expr_depth_ok(SQLITE_OK, pChild);
163 assert( p->iHeight==iMaxChild+1 );
167 #else
168 # define assert_expr_depth_ok(rc, p)
169 #endif
171 void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
172 va_list ap;
173 va_start(ap, zFmt);
174 if( pParse->rc==SQLITE_OK ){
175 assert( pParse->zErr==0 );
176 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
177 pParse->rc = SQLITE_ERROR;
179 va_end(ap);
182 static int fts5ExprIsspace(char t){
183 return t==' ' || t=='\t' || t=='\n' || t=='\r';
187 ** Read the first token from the nul-terminated string at *pz.
189 static int fts5ExprGetToken(
190 Fts5Parse *pParse,
191 const char **pz, /* IN/OUT: Pointer into buffer */
192 Fts5Token *pToken
194 const char *z = *pz;
195 int tok;
197 /* Skip past any whitespace */
198 while( fts5ExprIsspace(*z) ) z++;
200 pToken->p = z;
201 pToken->n = 1;
202 switch( *z ){
203 case '(': tok = FTS5_LP; break;
204 case ')': tok = FTS5_RP; break;
205 case '{': tok = FTS5_LCP; break;
206 case '}': tok = FTS5_RCP; break;
207 case ':': tok = FTS5_COLON; break;
208 case ',': tok = FTS5_COMMA; break;
209 case '+': tok = FTS5_PLUS; break;
210 case '*': tok = FTS5_STAR; break;
211 case '-': tok = FTS5_MINUS; break;
212 case '^': tok = FTS5_CARET; break;
213 case '\0': tok = FTS5_EOF; break;
215 case '"': {
216 const char *z2;
217 tok = FTS5_STRING;
219 for(z2=&z[1]; 1; z2++){
220 if( z2[0]=='"' ){
221 z2++;
222 if( z2[0]!='"' ) break;
224 if( z2[0]=='\0' ){
225 sqlite3Fts5ParseError(pParse, "unterminated string");
226 return FTS5_EOF;
229 pToken->n = (z2 - z);
230 break;
233 default: {
234 const char *z2;
235 if( sqlite3Fts5IsBareword(z[0])==0 ){
236 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
237 return FTS5_EOF;
239 tok = FTS5_STRING;
240 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
241 pToken->n = (z2 - z);
242 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
243 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
244 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
245 break;
249 *pz = &pToken->p[pToken->n];
250 return tok;
253 static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
254 static void fts5ParseFree(void *p){ sqlite3_free(p); }
256 int sqlite3Fts5ExprNew(
257 Fts5Config *pConfig, /* FTS5 Configuration */
258 int bPhraseToAnd,
259 int iCol,
260 const char *zExpr, /* Expression text */
261 Fts5Expr **ppNew,
262 char **pzErr
264 Fts5Parse sParse;
265 Fts5Token token;
266 const char *z = zExpr;
267 int t; /* Next token type */
268 void *pEngine;
269 Fts5Expr *pNew;
271 *ppNew = 0;
272 *pzErr = 0;
273 memset(&sParse, 0, sizeof(sParse));
274 sParse.bPhraseToAnd = bPhraseToAnd;
275 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
276 if( pEngine==0 ){ return SQLITE_NOMEM; }
277 sParse.pConfig = pConfig;
279 do {
280 t = fts5ExprGetToken(&sParse, &z, &token);
281 sqlite3Fts5Parser(pEngine, t, token, &sParse);
282 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
283 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
285 assert_expr_depth_ok(sParse.rc, sParse.pExpr);
287 /* If the LHS of the MATCH expression was a user column, apply the
288 ** implicit column-filter. */
289 if( iCol<pConfig->nCol && sParse.pExpr && sParse.rc==SQLITE_OK ){
290 int n = sizeof(Fts5Colset);
291 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
292 if( pColset ){
293 pColset->nCol = 1;
294 pColset->aiCol[0] = iCol;
295 sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
299 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
300 if( sParse.rc==SQLITE_OK ){
301 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
302 if( pNew==0 ){
303 sParse.rc = SQLITE_NOMEM;
304 sqlite3Fts5ParseNodeFree(sParse.pExpr);
305 }else{
306 if( !sParse.pExpr ){
307 const int nByte = sizeof(Fts5ExprNode);
308 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
309 if( pNew->pRoot ){
310 pNew->pRoot->bEof = 1;
312 }else{
313 pNew->pRoot = sParse.pExpr;
315 pNew->pIndex = 0;
316 pNew->pConfig = pConfig;
317 pNew->apExprPhrase = sParse.apPhrase;
318 pNew->nPhrase = sParse.nPhrase;
319 pNew->bDesc = 0;
320 sParse.apPhrase = 0;
322 }else{
323 sqlite3Fts5ParseNodeFree(sParse.pExpr);
326 sqlite3_free(sParse.apPhrase);
327 if( 0==*pzErr ){
328 *pzErr = sParse.zErr;
329 }else{
330 sqlite3_free(sParse.zErr);
332 return sParse.rc;
336 ** Assuming that buffer z is at least nByte bytes in size and contains a
337 ** valid utf-8 string, return the number of characters in the string.
339 static int fts5ExprCountChar(const char *z, int nByte){
340 int nRet = 0;
341 int ii;
342 for(ii=0; ii<nByte; ii++){
343 if( (z[ii] & 0xC0)!=0x80 ) nRet++;
345 return nRet;
349 ** This function is only called when using the special 'trigram' tokenizer.
350 ** Argument zText contains the text of a LIKE or GLOB pattern matched
351 ** against column iCol. This function creates and compiles an FTS5 MATCH
352 ** expression that will match a superset of the rows matched by the LIKE or
353 ** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error
354 ** code.
356 int sqlite3Fts5ExprPattern(
357 Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp
359 i64 nText = strlen(zText);
360 char *zExpr = (char*)sqlite3_malloc64(nText*4 + 1);
361 int rc = SQLITE_OK;
363 if( zExpr==0 ){
364 rc = SQLITE_NOMEM;
365 }else{
366 char aSpec[3];
367 int iOut = 0;
368 int i = 0;
369 int iFirst = 0;
371 if( bGlob==0 ){
372 aSpec[0] = '_';
373 aSpec[1] = '%';
374 aSpec[2] = 0;
375 }else{
376 aSpec[0] = '*';
377 aSpec[1] = '?';
378 aSpec[2] = '[';
381 while( i<=nText ){
382 if( i==nText
383 || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2]
386 if( fts5ExprCountChar(&zText[iFirst], i-iFirst)>=3 ){
387 int jj;
388 zExpr[iOut++] = '"';
389 for(jj=iFirst; jj<i; jj++){
390 zExpr[iOut++] = zText[jj];
391 if( zText[jj]=='"' ) zExpr[iOut++] = '"';
393 zExpr[iOut++] = '"';
394 zExpr[iOut++] = ' ';
396 if( zText[i]==aSpec[2] ){
397 i += 2;
398 if( zText[i-1]=='^' ) i++;
399 while( i<nText && zText[i]!=']' ) i++;
401 iFirst = i+1;
403 i++;
405 if( iOut>0 ){
406 int bAnd = 0;
407 if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
408 bAnd = 1;
409 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
410 iCol = pConfig->nCol;
413 zExpr[iOut] = '\0';
414 rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg);
415 }else{
416 *pp = 0;
418 sqlite3_free(zExpr);
421 return rc;
425 ** Free the expression node object passed as the only argument.
427 void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
428 if( p ){
429 int i;
430 for(i=0; i<p->nChild; i++){
431 sqlite3Fts5ParseNodeFree(p->apChild[i]);
433 sqlite3Fts5ParseNearsetFree(p->pNear);
434 sqlite3_free(p);
439 ** Free the expression object passed as the only argument.
441 void sqlite3Fts5ExprFree(Fts5Expr *p){
442 if( p ){
443 sqlite3Fts5ParseNodeFree(p->pRoot);
444 sqlite3_free(p->apExprPhrase);
445 sqlite3_free(p);
449 int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
450 Fts5Parse sParse;
451 memset(&sParse, 0, sizeof(sParse));
453 if( *pp1 && p2 ){
454 Fts5Expr *p1 = *pp1;
455 int nPhrase = p1->nPhrase + p2->nPhrase;
457 p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0);
458 p2->pRoot = 0;
460 if( sParse.rc==SQLITE_OK ){
461 Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
462 p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
464 if( ap==0 ){
465 sParse.rc = SQLITE_NOMEM;
466 }else{
467 int i;
468 memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
469 for(i=0; i<p2->nPhrase; i++){
470 ap[i] = p2->apExprPhrase[i];
472 p1->nPhrase = nPhrase;
473 p1->apExprPhrase = ap;
476 sqlite3_free(p2->apExprPhrase);
477 sqlite3_free(p2);
478 }else if( p2 ){
479 *pp1 = p2;
482 return sParse.rc;
486 ** Argument pTerm must be a synonym iterator. Return the current rowid
487 ** that it points to.
489 static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
490 i64 iRet = 0;
491 int bRetValid = 0;
492 Fts5ExprTerm *p;
494 assert( pTerm );
495 assert( pTerm->pSynonym );
496 assert( bDesc==0 || bDesc==1 );
497 for(p=pTerm; p; p=p->pSynonym){
498 if( 0==sqlite3Fts5IterEof(p->pIter) ){
499 i64 iRowid = p->pIter->iRowid;
500 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
501 iRet = iRowid;
502 bRetValid = 1;
507 if( pbEof && bRetValid==0 ) *pbEof = 1;
508 return iRet;
512 ** Argument pTerm must be a synonym iterator.
514 static int fts5ExprSynonymList(
515 Fts5ExprTerm *pTerm,
516 i64 iRowid,
517 Fts5Buffer *pBuf, /* Use this buffer for space if required */
518 u8 **pa, int *pn
520 Fts5PoslistReader aStatic[4];
521 Fts5PoslistReader *aIter = aStatic;
522 int nIter = 0;
523 int nAlloc = 4;
524 int rc = SQLITE_OK;
525 Fts5ExprTerm *p;
527 assert( pTerm->pSynonym );
528 for(p=pTerm; p; p=p->pSynonym){
529 Fts5IndexIter *pIter = p->pIter;
530 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
531 if( pIter->nData==0 ) continue;
532 if( nIter==nAlloc ){
533 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
534 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
535 if( aNew==0 ){
536 rc = SQLITE_NOMEM;
537 goto synonym_poslist_out;
539 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
540 nAlloc = nAlloc*2;
541 if( aIter!=aStatic ) sqlite3_free(aIter);
542 aIter = aNew;
544 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
545 assert( aIter[nIter].bEof==0 );
546 nIter++;
550 if( nIter==1 ){
551 *pa = (u8*)aIter[0].a;
552 *pn = aIter[0].n;
553 }else{
554 Fts5PoslistWriter writer = {0};
555 i64 iPrev = -1;
556 fts5BufferZero(pBuf);
557 while( 1 ){
558 int i;
559 i64 iMin = FTS5_LARGEST_INT64;
560 for(i=0; i<nIter; i++){
561 if( aIter[i].bEof==0 ){
562 if( aIter[i].iPos==iPrev ){
563 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
565 if( aIter[i].iPos<iMin ){
566 iMin = aIter[i].iPos;
570 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
571 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
572 iPrev = iMin;
574 if( rc==SQLITE_OK ){
575 *pa = pBuf->p;
576 *pn = pBuf->n;
580 synonym_poslist_out:
581 if( aIter!=aStatic ) sqlite3_free(aIter);
582 return rc;
587 ** All individual term iterators in pPhrase are guaranteed to be valid and
588 ** pointing to the same rowid when this function is called. This function
589 ** checks if the current rowid really is a match, and if so populates
590 ** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
591 ** is set to true if this is really a match, or false otherwise.
593 ** SQLITE_OK is returned if an error occurs, or an SQLite error code
594 ** otherwise. It is not considered an error code if the current rowid is
595 ** not a match.
597 static int fts5ExprPhraseIsMatch(
598 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
599 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
600 int *pbMatch /* OUT: Set to true if really a match */
602 Fts5PoslistWriter writer = {0};
603 Fts5PoslistReader aStatic[4];
604 Fts5PoslistReader *aIter = aStatic;
605 int i;
606 int rc = SQLITE_OK;
607 int bFirst = pPhrase->aTerm[0].bFirst;
609 fts5BufferZero(&pPhrase->poslist);
611 /* If the aStatic[] array is not large enough, allocate a large array
612 ** using sqlite3_malloc(). This approach could be improved upon. */
613 if( pPhrase->nTerm>ArraySize(aStatic) ){
614 sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
615 aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
616 if( !aIter ) return SQLITE_NOMEM;
618 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
620 /* Initialize a term iterator for each term in the phrase */
621 for(i=0; i<pPhrase->nTerm; i++){
622 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
623 int n = 0;
624 int bFlag = 0;
625 u8 *a = 0;
626 if( pTerm->pSynonym ){
627 Fts5Buffer buf = {0, 0, 0};
628 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
629 if( rc ){
630 sqlite3_free(a);
631 goto ismatch_out;
633 if( a==buf.p ) bFlag = 1;
634 }else{
635 a = (u8*)pTerm->pIter->pData;
636 n = pTerm->pIter->nData;
638 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
639 aIter[i].bFlag = (u8)bFlag;
640 if( aIter[i].bEof ) goto ismatch_out;
643 while( 1 ){
644 int bMatch;
645 i64 iPos = aIter[0].iPos;
646 do {
647 bMatch = 1;
648 for(i=0; i<pPhrase->nTerm; i++){
649 Fts5PoslistReader *pPos = &aIter[i];
650 i64 iAdj = iPos + i;
651 if( pPos->iPos!=iAdj ){
652 bMatch = 0;
653 while( pPos->iPos<iAdj ){
654 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
656 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
659 }while( bMatch==0 );
661 /* Append position iPos to the output */
662 if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
663 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
664 if( rc!=SQLITE_OK ) goto ismatch_out;
667 for(i=0; i<pPhrase->nTerm; i++){
668 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
672 ismatch_out:
673 *pbMatch = (pPhrase->poslist.n>0);
674 for(i=0; i<pPhrase->nTerm; i++){
675 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
677 if( aIter!=aStatic ) sqlite3_free(aIter);
678 return rc;
681 typedef struct Fts5LookaheadReader Fts5LookaheadReader;
682 struct Fts5LookaheadReader {
683 const u8 *a; /* Buffer containing position list */
684 int n; /* Size of buffer a[] in bytes */
685 int i; /* Current offset in position list */
686 i64 iPos; /* Current position */
687 i64 iLookahead; /* Next position */
690 #define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
692 static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
693 p->iPos = p->iLookahead;
694 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
695 p->iLookahead = FTS5_LOOKAHEAD_EOF;
697 return (p->iPos==FTS5_LOOKAHEAD_EOF);
700 static int fts5LookaheadReaderInit(
701 const u8 *a, int n, /* Buffer to read position list from */
702 Fts5LookaheadReader *p /* Iterator object to initialize */
704 memset(p, 0, sizeof(Fts5LookaheadReader));
705 p->a = a;
706 p->n = n;
707 fts5LookaheadReaderNext(p);
708 return fts5LookaheadReaderNext(p);
711 typedef struct Fts5NearTrimmer Fts5NearTrimmer;
712 struct Fts5NearTrimmer {
713 Fts5LookaheadReader reader; /* Input iterator */
714 Fts5PoslistWriter writer; /* Writer context */
715 Fts5Buffer *pOut; /* Output poslist */
719 ** The near-set object passed as the first argument contains more than
720 ** one phrase. All phrases currently point to the same row. The
721 ** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
722 ** tests if the current row contains instances of each phrase sufficiently
723 ** close together to meet the NEAR constraint. Non-zero is returned if it
724 ** does, or zero otherwise.
726 ** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
727 ** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
728 ** occurs within this function (*pRc) is set accordingly before returning.
729 ** The return value is undefined in both these cases.
731 ** If no error occurs and non-zero (a match) is returned, the position-list
732 ** of each phrase object is edited to contain only those entries that
733 ** meet the constraint before returning.
735 static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
736 Fts5NearTrimmer aStatic[4];
737 Fts5NearTrimmer *a = aStatic;
738 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
740 int i;
741 int rc = *pRc;
742 int bMatch;
744 assert( pNear->nPhrase>1 );
746 /* If the aStatic[] array is not large enough, allocate a large array
747 ** using sqlite3_malloc(). This approach could be improved upon. */
748 if( pNear->nPhrase>ArraySize(aStatic) ){
749 sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
750 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
751 }else{
752 memset(aStatic, 0, sizeof(aStatic));
754 if( rc!=SQLITE_OK ){
755 *pRc = rc;
756 return 0;
759 /* Initialize a lookahead iterator for each phrase. After passing the
760 ** buffer and buffer size to the lookaside-reader init function, zero
761 ** the phrase poslist buffer. The new poslist for the phrase (containing
762 ** the same entries as the original with some entries removed on account
763 ** of the NEAR constraint) is written over the original even as it is
764 ** being read. This is safe as the entries for the new poslist are a
765 ** subset of the old, so it is not possible for data yet to be read to
766 ** be overwritten. */
767 for(i=0; i<pNear->nPhrase; i++){
768 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
769 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
770 pPoslist->n = 0;
771 a[i].pOut = pPoslist;
774 while( 1 ){
775 int iAdv;
776 i64 iMin;
777 i64 iMax;
779 /* This block advances the phrase iterators until they point to a set of
780 ** entries that together comprise a match. */
781 iMax = a[0].reader.iPos;
782 do {
783 bMatch = 1;
784 for(i=0; i<pNear->nPhrase; i++){
785 Fts5LookaheadReader *pPos = &a[i].reader;
786 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
787 if( pPos->iPos<iMin || pPos->iPos>iMax ){
788 bMatch = 0;
789 while( pPos->iPos<iMin ){
790 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
792 if( pPos->iPos>iMax ) iMax = pPos->iPos;
795 }while( bMatch==0 );
797 /* Add an entry to each output position list */
798 for(i=0; i<pNear->nPhrase; i++){
799 i64 iPos = a[i].reader.iPos;
800 Fts5PoslistWriter *pWriter = &a[i].writer;
801 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
802 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
806 iAdv = 0;
807 iMin = a[0].reader.iLookahead;
808 for(i=0; i<pNear->nPhrase; i++){
809 if( a[i].reader.iLookahead < iMin ){
810 iMin = a[i].reader.iLookahead;
811 iAdv = i;
814 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
817 ismatch_out: {
818 int bRet = a[0].pOut->n>0;
819 *pRc = rc;
820 if( a!=aStatic ) sqlite3_free(a);
821 return bRet;
826 ** Advance iterator pIter until it points to a value equal to or laster
827 ** than the initial value of *piLast. If this means the iterator points
828 ** to a value laster than *piLast, update *piLast to the new lastest value.
830 ** If the iterator reaches EOF, set *pbEof to true before returning. If
831 ** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
832 ** are set, return a non-zero value. Otherwise, return zero.
834 static int fts5ExprAdvanceto(
835 Fts5IndexIter *pIter, /* Iterator to advance */
836 int bDesc, /* True if iterator is "rowid DESC" */
837 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
838 int *pRc, /* OUT: Error code */
839 int *pbEof /* OUT: Set to true if EOF */
841 i64 iLast = *piLast;
842 i64 iRowid;
844 iRowid = pIter->iRowid;
845 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
846 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
847 if( rc || sqlite3Fts5IterEof(pIter) ){
848 *pRc = rc;
849 *pbEof = 1;
850 return 1;
852 iRowid = pIter->iRowid;
853 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
855 *piLast = iRowid;
857 return 0;
860 static int fts5ExprSynonymAdvanceto(
861 Fts5ExprTerm *pTerm, /* Term iterator to advance */
862 int bDesc, /* True if iterator is "rowid DESC" */
863 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
864 int *pRc /* OUT: Error code */
866 int rc = SQLITE_OK;
867 i64 iLast = *piLast;
868 Fts5ExprTerm *p;
869 int bEof = 0;
871 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
872 if( sqlite3Fts5IterEof(p->pIter)==0 ){
873 i64 iRowid = p->pIter->iRowid;
874 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
875 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
880 if( rc!=SQLITE_OK ){
881 *pRc = rc;
882 bEof = 1;
883 }else{
884 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
886 return bEof;
890 static int fts5ExprNearTest(
891 int *pRc,
892 Fts5Expr *pExpr, /* Expression that pNear is a part of */
893 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
895 Fts5ExprNearset *pNear = pNode->pNear;
896 int rc = *pRc;
898 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
899 Fts5ExprTerm *pTerm;
900 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
901 pPhrase->poslist.n = 0;
902 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
903 Fts5IndexIter *pIter = pTerm->pIter;
904 if( sqlite3Fts5IterEof(pIter)==0 ){
905 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
906 pPhrase->poslist.n = 1;
910 return pPhrase->poslist.n;
911 }else{
912 int i;
914 /* Check that each phrase in the nearset matches the current row.
915 ** Populate the pPhrase->poslist buffers at the same time. If any
916 ** phrase is not a match, break out of the loop early. */
917 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
918 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
919 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
920 || pNear->pColset || pPhrase->aTerm[0].bFirst
922 int bMatch = 0;
923 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
924 if( bMatch==0 ) break;
925 }else{
926 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
927 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
931 *pRc = rc;
932 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
933 return 1;
935 return 0;
941 ** Initialize all term iterators in the pNear object. If any term is found
942 ** to match no documents at all, return immediately without initializing any
943 ** further iterators.
945 ** If an error occurs, return an SQLite error code. Otherwise, return
946 ** SQLITE_OK. It is not considered an error if some term matches zero
947 ** documents.
949 static int fts5ExprNearInitAll(
950 Fts5Expr *pExpr,
951 Fts5ExprNode *pNode
953 Fts5ExprNearset *pNear = pNode->pNear;
954 int i;
956 assert( pNode->bNomatch==0 );
957 for(i=0; i<pNear->nPhrase; i++){
958 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
959 if( pPhrase->nTerm==0 ){
960 pNode->bEof = 1;
961 return SQLITE_OK;
962 }else{
963 int j;
964 for(j=0; j<pPhrase->nTerm; j++){
965 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
966 Fts5ExprTerm *p;
967 int bHit = 0;
969 for(p=pTerm; p; p=p->pSynonym){
970 int rc;
971 if( p->pIter ){
972 sqlite3Fts5IterClose(p->pIter);
973 p->pIter = 0;
975 rc = sqlite3Fts5IndexQuery(
976 pExpr->pIndex, p->pTerm, p->nQueryTerm,
977 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
978 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
979 pNear->pColset,
980 &p->pIter
982 assert( (rc==SQLITE_OK)==(p->pIter!=0) );
983 if( rc!=SQLITE_OK ) return rc;
984 if( 0==sqlite3Fts5IterEof(p->pIter) ){
985 bHit = 1;
989 if( bHit==0 ){
990 pNode->bEof = 1;
991 return SQLITE_OK;
997 pNode->bEof = 0;
998 return SQLITE_OK;
1002 ** If pExpr is an ASC iterator, this function returns a value with the
1003 ** same sign as:
1005 ** (iLhs - iRhs)
1007 ** Otherwise, if this is a DESC iterator, the opposite is returned:
1009 ** (iRhs - iLhs)
1011 static int fts5RowidCmp(
1012 Fts5Expr *pExpr,
1013 i64 iLhs,
1014 i64 iRhs
1016 assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
1017 if( pExpr->bDesc==0 ){
1018 if( iLhs<iRhs ) return -1;
1019 return (iLhs > iRhs);
1020 }else{
1021 if( iLhs>iRhs ) return -1;
1022 return (iLhs < iRhs);
1026 static void fts5ExprSetEof(Fts5ExprNode *pNode){
1027 int i;
1028 pNode->bEof = 1;
1029 pNode->bNomatch = 0;
1030 for(i=0; i<pNode->nChild; i++){
1031 fts5ExprSetEof(pNode->apChild[i]);
1035 static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
1036 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
1037 Fts5ExprNearset *pNear = pNode->pNear;
1038 int i;
1039 for(i=0; i<pNear->nPhrase; i++){
1040 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
1041 pPhrase->poslist.n = 0;
1043 }else{
1044 int i;
1045 for(i=0; i<pNode->nChild; i++){
1046 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
1054 ** Compare the values currently indicated by the two nodes as follows:
1056 ** res = (*p1) - (*p2)
1058 ** Nodes that point to values that come later in the iteration order are
1059 ** considered to be larger. Nodes at EOF are the largest of all.
1061 ** This means that if the iteration order is ASC, then numerically larger
1062 ** rowids are considered larger. Or if it is the default DESC, numerically
1063 ** smaller rowids are larger.
1065 static int fts5NodeCompare(
1066 Fts5Expr *pExpr,
1067 Fts5ExprNode *p1,
1068 Fts5ExprNode *p2
1070 if( p2->bEof ) return -1;
1071 if( p1->bEof ) return +1;
1072 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
1076 ** All individual term iterators in pNear are guaranteed to be valid when
1077 ** this function is called. This function checks if all term iterators
1078 ** point to the same rowid, and if not, advances them until they do.
1079 ** If an EOF is reached before this happens, *pbEof is set to true before
1080 ** returning.
1082 ** SQLITE_OK is returned if an error occurs, or an SQLite error code
1083 ** otherwise. It is not considered an error code if an iterator reaches
1084 ** EOF.
1086 static int fts5ExprNodeTest_STRING(
1087 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
1088 Fts5ExprNode *pNode
1090 Fts5ExprNearset *pNear = pNode->pNear;
1091 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
1092 int rc = SQLITE_OK;
1093 i64 iLast; /* Lastest rowid any iterator points to */
1094 int i, j; /* Phrase and token index, respectively */
1095 int bMatch; /* True if all terms are at the same rowid */
1096 const int bDesc = pExpr->bDesc;
1098 /* Check that this node should not be FTS5_TERM */
1099 assert( pNear->nPhrase>1
1100 || pNear->apPhrase[0]->nTerm>1
1101 || pNear->apPhrase[0]->aTerm[0].pSynonym
1102 || pNear->apPhrase[0]->aTerm[0].bFirst
1105 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
1106 ** iterator skips through rowids in the default ascending order, this means
1107 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
1108 ** means the minimum rowid. */
1109 if( pLeft->aTerm[0].pSynonym ){
1110 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
1111 }else{
1112 iLast = pLeft->aTerm[0].pIter->iRowid;
1115 do {
1116 bMatch = 1;
1117 for(i=0; i<pNear->nPhrase; i++){
1118 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
1119 for(j=0; j<pPhrase->nTerm; j++){
1120 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
1121 if( pTerm->pSynonym ){
1122 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
1123 if( iRowid==iLast ) continue;
1124 bMatch = 0;
1125 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
1126 pNode->bNomatch = 0;
1127 pNode->bEof = 1;
1128 return rc;
1130 }else{
1131 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
1132 if( pIter->iRowid==iLast || pIter->bEof ) continue;
1133 bMatch = 0;
1134 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
1135 return rc;
1140 }while( bMatch==0 );
1142 pNode->iRowid = iLast;
1143 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
1144 assert( pNode->bEof==0 || pNode->bNomatch==0 );
1146 return rc;
1150 ** Advance the first term iterator in the first phrase of pNear. Set output
1151 ** variable *pbEof to true if it reaches EOF or if an error occurs.
1153 ** Return SQLITE_OK if successful, or an SQLite error code if an error
1154 ** occurs.
1156 static int fts5ExprNodeNext_STRING(
1157 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
1158 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
1159 int bFromValid,
1160 i64 iFrom
1162 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
1163 int rc = SQLITE_OK;
1165 pNode->bNomatch = 0;
1166 if( pTerm->pSynonym ){
1167 int bEof = 1;
1168 Fts5ExprTerm *p;
1170 /* Find the firstest rowid any synonym points to. */
1171 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
1173 /* Advance each iterator that currently points to iRowid. Or, if iFrom
1174 ** is valid - each iterator that points to a rowid before iFrom. */
1175 for(p=pTerm; p; p=p->pSynonym){
1176 if( sqlite3Fts5IterEof(p->pIter)==0 ){
1177 i64 ii = p->pIter->iRowid;
1178 if( ii==iRowid
1179 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
1181 if( bFromValid ){
1182 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
1183 }else{
1184 rc = sqlite3Fts5IterNext(p->pIter);
1186 if( rc!=SQLITE_OK ) break;
1187 if( sqlite3Fts5IterEof(p->pIter)==0 ){
1188 bEof = 0;
1190 }else{
1191 bEof = 0;
1196 /* Set the EOF flag if either all synonym iterators are at EOF or an
1197 ** error has occurred. */
1198 pNode->bEof = (rc || bEof);
1199 }else{
1200 Fts5IndexIter *pIter = pTerm->pIter;
1202 assert( Fts5NodeIsString(pNode) );
1203 if( bFromValid ){
1204 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
1205 }else{
1206 rc = sqlite3Fts5IterNext(pIter);
1209 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
1212 if( pNode->bEof==0 ){
1213 assert( rc==SQLITE_OK );
1214 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
1217 return rc;
1221 static int fts5ExprNodeTest_TERM(
1222 Fts5Expr *pExpr, /* Expression that pNear is a part of */
1223 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
1225 /* As this "NEAR" object is actually a single phrase that consists
1226 ** of a single term only, grab pointers into the poslist managed by the
1227 ** fts5_index.c iterator object. This is much faster than synthesizing
1228 ** a new poslist the way we have to for more complicated phrase or NEAR
1229 ** expressions. */
1230 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
1231 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
1233 assert( pNode->eType==FTS5_TERM );
1234 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
1235 assert( pPhrase->aTerm[0].pSynonym==0 );
1237 pPhrase->poslist.n = pIter->nData;
1238 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
1239 pPhrase->poslist.p = (u8*)pIter->pData;
1241 pNode->iRowid = pIter->iRowid;
1242 pNode->bNomatch = (pPhrase->poslist.n==0);
1243 return SQLITE_OK;
1247 ** xNext() method for a node of type FTS5_TERM.
1249 static int fts5ExprNodeNext_TERM(
1250 Fts5Expr *pExpr,
1251 Fts5ExprNode *pNode,
1252 int bFromValid,
1253 i64 iFrom
1255 int rc;
1256 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
1258 assert( pNode->bEof==0 );
1259 if( bFromValid ){
1260 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
1261 }else{
1262 rc = sqlite3Fts5IterNext(pIter);
1264 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
1265 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
1266 }else{
1267 pNode->bEof = 1;
1268 pNode->bNomatch = 0;
1270 return rc;
1273 static void fts5ExprNodeTest_OR(
1274 Fts5Expr *pExpr, /* Expression of which pNode is a part */
1275 Fts5ExprNode *pNode /* Expression node to test */
1277 Fts5ExprNode *pNext = pNode->apChild[0];
1278 int i;
1280 for(i=1; i<pNode->nChild; i++){
1281 Fts5ExprNode *pChild = pNode->apChild[i];
1282 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
1283 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
1284 pNext = pChild;
1287 pNode->iRowid = pNext->iRowid;
1288 pNode->bEof = pNext->bEof;
1289 pNode->bNomatch = pNext->bNomatch;
1292 static int fts5ExprNodeNext_OR(
1293 Fts5Expr *pExpr,
1294 Fts5ExprNode *pNode,
1295 int bFromValid,
1296 i64 iFrom
1298 int i;
1299 i64 iLast = pNode->iRowid;
1301 for(i=0; i<pNode->nChild; i++){
1302 Fts5ExprNode *p1 = pNode->apChild[i];
1303 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
1304 if( p1->bEof==0 ){
1305 if( (p1->iRowid==iLast)
1306 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
1308 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
1309 if( rc!=SQLITE_OK ){
1310 pNode->bNomatch = 0;
1311 return rc;
1317 fts5ExprNodeTest_OR(pExpr, pNode);
1318 return SQLITE_OK;
1322 ** Argument pNode is an FTS5_AND node.
1324 static int fts5ExprNodeTest_AND(
1325 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
1326 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
1328 int iChild;
1329 i64 iLast = pAnd->iRowid;
1330 int rc = SQLITE_OK;
1331 int bMatch;
1333 assert( pAnd->bEof==0 );
1334 do {
1335 pAnd->bNomatch = 0;
1336 bMatch = 1;
1337 for(iChild=0; iChild<pAnd->nChild; iChild++){
1338 Fts5ExprNode *pChild = pAnd->apChild[iChild];
1339 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
1340 if( cmp>0 ){
1341 /* Advance pChild until it points to iLast or laster */
1342 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
1343 if( rc!=SQLITE_OK ){
1344 pAnd->bNomatch = 0;
1345 return rc;
1349 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
1350 ** the child node is guaranteed to have advanced at least as far as
1351 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
1352 ** new lastest rowid seen so far. */
1353 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
1354 if( pChild->bEof ){
1355 fts5ExprSetEof(pAnd);
1356 bMatch = 1;
1357 break;
1358 }else if( iLast!=pChild->iRowid ){
1359 bMatch = 0;
1360 iLast = pChild->iRowid;
1363 if( pChild->bNomatch ){
1364 pAnd->bNomatch = 1;
1367 }while( bMatch==0 );
1369 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
1370 fts5ExprNodeZeroPoslist(pAnd);
1372 pAnd->iRowid = iLast;
1373 return SQLITE_OK;
1376 static int fts5ExprNodeNext_AND(
1377 Fts5Expr *pExpr,
1378 Fts5ExprNode *pNode,
1379 int bFromValid,
1380 i64 iFrom
1382 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
1383 if( rc==SQLITE_OK ){
1384 rc = fts5ExprNodeTest_AND(pExpr, pNode);
1385 }else{
1386 pNode->bNomatch = 0;
1388 return rc;
1391 static int fts5ExprNodeTest_NOT(
1392 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
1393 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
1395 int rc = SQLITE_OK;
1396 Fts5ExprNode *p1 = pNode->apChild[0];
1397 Fts5ExprNode *p2 = pNode->apChild[1];
1398 assert( pNode->nChild==2 );
1400 while( rc==SQLITE_OK && p1->bEof==0 ){
1401 int cmp = fts5NodeCompare(pExpr, p1, p2);
1402 if( cmp>0 ){
1403 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
1404 cmp = fts5NodeCompare(pExpr, p1, p2);
1406 assert( rc!=SQLITE_OK || cmp<=0 );
1407 if( cmp || p2->bNomatch ) break;
1408 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
1410 pNode->bEof = p1->bEof;
1411 pNode->bNomatch = p1->bNomatch;
1412 pNode->iRowid = p1->iRowid;
1413 if( p1->bEof ){
1414 fts5ExprNodeZeroPoslist(p2);
1416 return rc;
1419 static int fts5ExprNodeNext_NOT(
1420 Fts5Expr *pExpr,
1421 Fts5ExprNode *pNode,
1422 int bFromValid,
1423 i64 iFrom
1425 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
1426 if( rc==SQLITE_OK ){
1427 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
1429 if( rc!=SQLITE_OK ){
1430 pNode->bNomatch = 0;
1432 return rc;
1436 ** If pNode currently points to a match, this function returns SQLITE_OK
1437 ** without modifying it. Otherwise, pNode is advanced until it does point
1438 ** to a match or EOF is reached.
1440 static int fts5ExprNodeTest(
1441 Fts5Expr *pExpr, /* Expression of which pNode is a part */
1442 Fts5ExprNode *pNode /* Expression node to test */
1444 int rc = SQLITE_OK;
1445 if( pNode->bEof==0 ){
1446 switch( pNode->eType ){
1448 case FTS5_STRING: {
1449 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
1450 break;
1453 case FTS5_TERM: {
1454 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
1455 break;
1458 case FTS5_AND: {
1459 rc = fts5ExprNodeTest_AND(pExpr, pNode);
1460 break;
1463 case FTS5_OR: {
1464 fts5ExprNodeTest_OR(pExpr, pNode);
1465 break;
1468 default: assert( pNode->eType==FTS5_NOT ); {
1469 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
1470 break;
1474 return rc;
1479 ** Set node pNode, which is part of expression pExpr, to point to the first
1480 ** match. If there are no matches, set the Node.bEof flag to indicate EOF.
1482 ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
1483 ** It is not an error if there are no matches.
1485 static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
1486 int rc = SQLITE_OK;
1487 pNode->bEof = 0;
1488 pNode->bNomatch = 0;
1490 if( Fts5NodeIsString(pNode) ){
1491 /* Initialize all term iterators in the NEAR object. */
1492 rc = fts5ExprNearInitAll(pExpr, pNode);
1493 }else if( pNode->xNext==0 ){
1494 pNode->bEof = 1;
1495 }else{
1496 int i;
1497 int nEof = 0;
1498 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
1499 Fts5ExprNode *pChild = pNode->apChild[i];
1500 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
1501 assert( pChild->bEof==0 || pChild->bEof==1 );
1502 nEof += pChild->bEof;
1504 pNode->iRowid = pNode->apChild[0]->iRowid;
1506 switch( pNode->eType ){
1507 case FTS5_AND:
1508 if( nEof>0 ) fts5ExprSetEof(pNode);
1509 break;
1511 case FTS5_OR:
1512 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
1513 break;
1515 default:
1516 assert( pNode->eType==FTS5_NOT );
1517 pNode->bEof = pNode->apChild[0]->bEof;
1518 break;
1522 if( rc==SQLITE_OK ){
1523 rc = fts5ExprNodeTest(pExpr, pNode);
1525 return rc;
1530 ** Begin iterating through the set of documents in index pIdx matched by
1531 ** the MATCH expression passed as the first argument. If the "bDesc"
1532 ** parameter is passed a non-zero value, iteration is in descending rowid
1533 ** order. Or, if it is zero, in ascending order.
1535 ** If iterating in ascending rowid order (bDesc==0), the first document
1536 ** visited is that with the smallest rowid that is larger than or equal
1537 ** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
1538 ** then the first document visited must have a rowid smaller than or
1539 ** equal to iFirst.
1541 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
1542 ** is not considered an error if the query does not match any documents.
1544 int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
1545 Fts5ExprNode *pRoot = p->pRoot;
1546 int rc; /* Return code */
1548 p->pIndex = pIdx;
1549 p->bDesc = bDesc;
1550 rc = fts5ExprNodeFirst(p, pRoot);
1552 /* If not at EOF but the current rowid occurs earlier than iFirst in
1553 ** the iteration order, move to document iFirst or later. */
1554 if( rc==SQLITE_OK
1555 && 0==pRoot->bEof
1556 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
1558 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
1561 /* If the iterator is not at a real match, skip forward until it is. */
1562 while( pRoot->bNomatch && rc==SQLITE_OK ){
1563 assert( pRoot->bEof==0 );
1564 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
1566 return rc;
1570 ** Move to the next document
1572 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
1573 ** is not considered an error if the query does not match any documents.
1575 int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
1576 int rc;
1577 Fts5ExprNode *pRoot = p->pRoot;
1578 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
1579 do {
1580 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
1581 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
1582 }while( pRoot->bNomatch );
1583 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
1584 pRoot->bEof = 1;
1586 return rc;
1589 int sqlite3Fts5ExprEof(Fts5Expr *p){
1590 return p->pRoot->bEof;
1593 i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
1594 return p->pRoot->iRowid;
1597 static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
1598 int rc = SQLITE_OK;
1599 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
1600 return rc;
1604 ** Free the phrase object passed as the only argument.
1606 static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
1607 if( pPhrase ){
1608 int i;
1609 for(i=0; i<pPhrase->nTerm; i++){
1610 Fts5ExprTerm *pSyn;
1611 Fts5ExprTerm *pNext;
1612 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
1613 sqlite3_free(pTerm->pTerm);
1614 sqlite3Fts5IterClose(pTerm->pIter);
1615 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
1616 pNext = pSyn->pSynonym;
1617 sqlite3Fts5IterClose(pSyn->pIter);
1618 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
1619 sqlite3_free(pSyn);
1622 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
1623 sqlite3_free(pPhrase);
1628 ** Set the "bFirst" flag on the first token of the phrase passed as the
1629 ** only argument.
1631 void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
1632 if( pPhrase && pPhrase->nTerm ){
1633 pPhrase->aTerm[0].bFirst = 1;
1638 ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
1639 ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
1640 ** appended to it and the results returned.
1642 ** If an OOM error occurs, both the pNear and pPhrase objects are freed and
1643 ** NULL returned.
1645 Fts5ExprNearset *sqlite3Fts5ParseNearset(
1646 Fts5Parse *pParse, /* Parse context */
1647 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
1648 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
1650 const int SZALLOC = 8;
1651 Fts5ExprNearset *pRet = 0;
1653 if( pParse->rc==SQLITE_OK ){
1654 if( pPhrase==0 ){
1655 return pNear;
1657 if( pNear==0 ){
1658 sqlite3_int64 nByte;
1659 nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
1660 pRet = sqlite3_malloc64(nByte);
1661 if( pRet==0 ){
1662 pParse->rc = SQLITE_NOMEM;
1663 }else{
1664 memset(pRet, 0, (size_t)nByte);
1666 }else if( (pNear->nPhrase % SZALLOC)==0 ){
1667 int nNew = pNear->nPhrase + SZALLOC;
1668 sqlite3_int64 nByte;
1670 nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
1671 pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
1672 if( pRet==0 ){
1673 pParse->rc = SQLITE_NOMEM;
1675 }else{
1676 pRet = pNear;
1680 if( pRet==0 ){
1681 assert( pParse->rc!=SQLITE_OK );
1682 sqlite3Fts5ParseNearsetFree(pNear);
1683 sqlite3Fts5ParsePhraseFree(pPhrase);
1684 }else{
1685 if( pRet->nPhrase>0 ){
1686 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
1687 assert( pParse!=0 );
1688 assert( pParse->apPhrase!=0 );
1689 assert( pParse->nPhrase>=2 );
1690 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
1691 if( pPhrase->nTerm==0 ){
1692 fts5ExprPhraseFree(pPhrase);
1693 pRet->nPhrase--;
1694 pParse->nPhrase--;
1695 pPhrase = pLast;
1696 }else if( pLast->nTerm==0 ){
1697 fts5ExprPhraseFree(pLast);
1698 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
1699 pParse->nPhrase--;
1700 pRet->nPhrase--;
1703 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
1705 return pRet;
1708 typedef struct TokenCtx TokenCtx;
1709 struct TokenCtx {
1710 Fts5ExprPhrase *pPhrase;
1711 Fts5Config *pConfig;
1712 int rc;
1716 ** Callback for tokenizing terms used by ParseTerm().
1718 static int fts5ParseTokenize(
1719 void *pContext, /* Pointer to Fts5InsertCtx object */
1720 int tflags, /* Mask of FTS5_TOKEN_* flags */
1721 const char *pToken, /* Buffer containing token */
1722 int nToken, /* Size of token in bytes */
1723 int iUnused1, /* Start offset of token */
1724 int iUnused2 /* End offset of token */
1726 int rc = SQLITE_OK;
1727 const int SZALLOC = 8;
1728 TokenCtx *pCtx = (TokenCtx*)pContext;
1729 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
1731 UNUSED_PARAM2(iUnused1, iUnused2);
1733 /* If an error has already occurred, this is a no-op */
1734 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
1735 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
1737 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
1738 Fts5ExprTerm *pSyn;
1739 sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
1740 pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
1741 if( pSyn==0 ){
1742 rc = SQLITE_NOMEM;
1743 }else{
1744 memset(pSyn, 0, (size_t)nByte);
1745 pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
1746 pSyn->nFullTerm = pSyn->nQueryTerm = nToken;
1747 if( pCtx->pConfig->bTokendata ){
1748 pSyn->nQueryTerm = (int)strlen(pSyn->pTerm);
1750 memcpy(pSyn->pTerm, pToken, nToken);
1751 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
1752 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
1754 }else{
1755 Fts5ExprTerm *pTerm;
1756 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
1757 Fts5ExprPhrase *pNew;
1758 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
1760 pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
1761 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
1763 if( pNew==0 ){
1764 rc = SQLITE_NOMEM;
1765 }else{
1766 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
1767 pCtx->pPhrase = pPhrase = pNew;
1768 pNew->nTerm = nNew - SZALLOC;
1772 if( rc==SQLITE_OK ){
1773 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
1774 memset(pTerm, 0, sizeof(Fts5ExprTerm));
1775 pTerm->pTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
1776 pTerm->nFullTerm = pTerm->nQueryTerm = nToken;
1777 if( pCtx->pConfig->bTokendata && rc==SQLITE_OK ){
1778 pTerm->nQueryTerm = (int)strlen(pTerm->pTerm);
1783 pCtx->rc = rc;
1784 return rc;
1789 ** Free the phrase object passed as the only argument.
1791 void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
1792 fts5ExprPhraseFree(pPhrase);
1796 ** Free the phrase object passed as the second argument.
1798 void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
1799 if( pNear ){
1800 int i;
1801 for(i=0; i<pNear->nPhrase; i++){
1802 fts5ExprPhraseFree(pNear->apPhrase[i]);
1804 sqlite3_free(pNear->pColset);
1805 sqlite3_free(pNear);
1809 void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
1810 assert( pParse->pExpr==0 );
1811 pParse->pExpr = p;
1814 static int parseGrowPhraseArray(Fts5Parse *pParse){
1815 if( (pParse->nPhrase % 8)==0 ){
1816 sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
1817 Fts5ExprPhrase **apNew;
1818 apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
1819 if( apNew==0 ){
1820 pParse->rc = SQLITE_NOMEM;
1821 return SQLITE_NOMEM;
1823 pParse->apPhrase = apNew;
1825 return SQLITE_OK;
1829 ** This function is called by the parser to process a string token. The
1830 ** string may or may not be quoted. In any case it is tokenized and a
1831 ** phrase object consisting of all tokens returned.
1833 Fts5ExprPhrase *sqlite3Fts5ParseTerm(
1834 Fts5Parse *pParse, /* Parse context */
1835 Fts5ExprPhrase *pAppend, /* Phrase to append to */
1836 Fts5Token *pToken, /* String to tokenize */
1837 int bPrefix /* True if there is a trailing "*" */
1839 Fts5Config *pConfig = pParse->pConfig;
1840 TokenCtx sCtx; /* Context object passed to callback */
1841 int rc; /* Tokenize return code */
1842 char *z = 0;
1844 memset(&sCtx, 0, sizeof(TokenCtx));
1845 sCtx.pPhrase = pAppend;
1846 sCtx.pConfig = pConfig;
1848 rc = fts5ParseStringFromToken(pToken, &z);
1849 if( rc==SQLITE_OK ){
1850 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
1851 int n;
1852 sqlite3Fts5Dequote(z);
1853 n = (int)strlen(z);
1854 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
1856 sqlite3_free(z);
1857 if( rc || (rc = sCtx.rc) ){
1858 pParse->rc = rc;
1859 fts5ExprPhraseFree(sCtx.pPhrase);
1860 sCtx.pPhrase = 0;
1861 }else{
1863 if( pAppend==0 ){
1864 if( parseGrowPhraseArray(pParse) ){
1865 fts5ExprPhraseFree(sCtx.pPhrase);
1866 return 0;
1868 pParse->nPhrase++;
1871 if( sCtx.pPhrase==0 ){
1872 /* This happens when parsing a token or quoted phrase that contains
1873 ** no token characters at all. (e.g ... MATCH '""'). */
1874 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
1875 }else if( sCtx.pPhrase->nTerm ){
1876 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
1878 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
1881 return sCtx.pPhrase;
1885 ** Create a new FTS5 expression by cloning phrase iPhrase of the
1886 ** expression passed as the second argument.
1888 int sqlite3Fts5ExprClonePhrase(
1889 Fts5Expr *pExpr,
1890 int iPhrase,
1891 Fts5Expr **ppNew
1893 int rc = SQLITE_OK; /* Return code */
1894 Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */
1895 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
1896 TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */
1897 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
1898 rc = SQLITE_RANGE;
1899 }else{
1900 pOrig = pExpr->apExprPhrase[iPhrase];
1901 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
1903 if( rc==SQLITE_OK ){
1904 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
1905 sizeof(Fts5ExprPhrase*));
1907 if( rc==SQLITE_OK ){
1908 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
1909 sizeof(Fts5ExprNode));
1911 if( rc==SQLITE_OK ){
1912 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
1913 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
1915 if( rc==SQLITE_OK && ALWAYS(pOrig!=0) ){
1916 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
1917 if( pColsetOrig ){
1918 sqlite3_int64 nByte;
1919 Fts5Colset *pColset;
1920 nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
1921 pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
1922 if( pColset ){
1923 memcpy(pColset, pColsetOrig, (size_t)nByte);
1925 pNew->pRoot->pNear->pColset = pColset;
1929 if( rc==SQLITE_OK ){
1930 if( pOrig->nTerm ){
1931 int i; /* Used to iterate through phrase terms */
1932 sCtx.pConfig = pExpr->pConfig;
1933 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
1934 int tflags = 0;
1935 Fts5ExprTerm *p;
1936 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
1937 rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0);
1938 tflags = FTS5_TOKEN_COLOCATED;
1940 if( rc==SQLITE_OK ){
1941 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
1942 sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
1945 }else{
1946 /* This happens when parsing a token or quoted phrase that contains
1947 ** no token characters at all. (e.g ... MATCH '""'). */
1948 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
1952 if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){
1953 /* All the allocations succeeded. Put the expression object together. */
1954 pNew->pIndex = pExpr->pIndex;
1955 pNew->pConfig = pExpr->pConfig;
1956 pNew->nPhrase = 1;
1957 pNew->apExprPhrase[0] = sCtx.pPhrase;
1958 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
1959 pNew->pRoot->pNear->nPhrase = 1;
1960 sCtx.pPhrase->pNode = pNew->pRoot;
1962 if( pOrig->nTerm==1
1963 && pOrig->aTerm[0].pSynonym==0
1964 && pOrig->aTerm[0].bFirst==0
1966 pNew->pRoot->eType = FTS5_TERM;
1967 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
1968 }else{
1969 pNew->pRoot->eType = FTS5_STRING;
1970 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
1972 }else{
1973 sqlite3Fts5ExprFree(pNew);
1974 fts5ExprPhraseFree(sCtx.pPhrase);
1975 pNew = 0;
1978 *ppNew = pNew;
1979 return rc;
1984 ** Token pTok has appeared in a MATCH expression where the NEAR operator
1985 ** is expected. If token pTok does not contain "NEAR", store an error
1986 ** in the pParse object.
1988 void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
1989 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
1990 sqlite3Fts5ParseError(
1991 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
1996 void sqlite3Fts5ParseSetDistance(
1997 Fts5Parse *pParse,
1998 Fts5ExprNearset *pNear,
1999 Fts5Token *p
2001 if( pNear ){
2002 int nNear = 0;
2003 int i;
2004 if( p->n ){
2005 for(i=0; i<p->n; i++){
2006 char c = (char)p->p[i];
2007 if( c<'0' || c>'9' ){
2008 sqlite3Fts5ParseError(
2009 pParse, "expected integer, got \"%.*s\"", p->n, p->p
2011 return;
2013 nNear = nNear * 10 + (p->p[i] - '0');
2015 }else{
2016 nNear = FTS5_DEFAULT_NEARDIST;
2018 pNear->nNear = nNear;
2023 ** The second argument passed to this function may be NULL, or it may be
2024 ** an existing Fts5Colset object. This function returns a pointer to
2025 ** a new colset object containing the contents of (p) with new value column
2026 ** number iCol appended.
2028 ** If an OOM error occurs, store an error code in pParse and return NULL.
2029 ** The old colset object (if any) is not freed in this case.
2031 static Fts5Colset *fts5ParseColset(
2032 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
2033 Fts5Colset *p, /* Existing colset object */
2034 int iCol /* New column to add to colset object */
2036 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
2037 Fts5Colset *pNew; /* New colset object to return */
2039 assert( pParse->rc==SQLITE_OK );
2040 assert( iCol>=0 && iCol<pParse->pConfig->nCol );
2042 pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
2043 if( pNew==0 ){
2044 pParse->rc = SQLITE_NOMEM;
2045 }else{
2046 int *aiCol = pNew->aiCol;
2047 int i, j;
2048 for(i=0; i<nCol; i++){
2049 if( aiCol[i]==iCol ) return pNew;
2050 if( aiCol[i]>iCol ) break;
2052 for(j=nCol; j>i; j--){
2053 aiCol[j] = aiCol[j-1];
2055 aiCol[i] = iCol;
2056 pNew->nCol = nCol+1;
2058 #ifndef NDEBUG
2059 /* Check that the array is in order and contains no duplicate entries. */
2060 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
2061 #endif
2064 return pNew;
2068 ** Allocate and return an Fts5Colset object specifying the inverse of
2069 ** the colset passed as the second argument. Free the colset passed
2070 ** as the second argument before returning.
2072 Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
2073 Fts5Colset *pRet;
2074 int nCol = pParse->pConfig->nCol;
2076 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
2077 sizeof(Fts5Colset) + sizeof(int)*nCol
2079 if( pRet ){
2080 int i;
2081 int iOld = 0;
2082 for(i=0; i<nCol; i++){
2083 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
2084 pRet->aiCol[pRet->nCol++] = i;
2085 }else{
2086 iOld++;
2091 sqlite3_free(p);
2092 return pRet;
2095 Fts5Colset *sqlite3Fts5ParseColset(
2096 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
2097 Fts5Colset *pColset, /* Existing colset object */
2098 Fts5Token *p
2100 Fts5Colset *pRet = 0;
2101 int iCol;
2102 char *z; /* Dequoted copy of token p */
2104 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
2105 if( pParse->rc==SQLITE_OK ){
2106 Fts5Config *pConfig = pParse->pConfig;
2107 sqlite3Fts5Dequote(z);
2108 for(iCol=0; iCol<pConfig->nCol; iCol++){
2109 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
2111 if( iCol==pConfig->nCol ){
2112 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
2113 }else{
2114 pRet = fts5ParseColset(pParse, pColset, iCol);
2116 sqlite3_free(z);
2119 if( pRet==0 ){
2120 assert( pParse->rc!=SQLITE_OK );
2121 sqlite3_free(pColset);
2124 return pRet;
2128 ** If argument pOrig is NULL, or if (*pRc) is set to anything other than
2129 ** SQLITE_OK when this function is called, NULL is returned.
2131 ** Otherwise, a copy of (*pOrig) is made into memory obtained from
2132 ** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
2133 ** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
2135 static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
2136 Fts5Colset *pRet;
2137 if( pOrig ){
2138 sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int);
2139 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
2140 if( pRet ){
2141 memcpy(pRet, pOrig, (size_t)nByte);
2143 }else{
2144 pRet = 0;
2146 return pRet;
2150 ** Remove from colset pColset any columns that are not also in colset pMerge.
2152 static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
2153 int iIn = 0; /* Next input in pColset */
2154 int iMerge = 0; /* Next input in pMerge */
2155 int iOut = 0; /* Next output slot in pColset */
2157 while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
2158 int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
2159 if( iDiff==0 ){
2160 pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
2161 iMerge++;
2162 iIn++;
2163 }else if( iDiff>0 ){
2164 iMerge++;
2165 }else{
2166 iIn++;
2169 pColset->nCol = iOut;
2173 ** Recursively apply colset pColset to expression node pNode and all of
2174 ** its decendents. If (*ppFree) is not NULL, it contains a spare copy
2175 ** of pColset. This function may use the spare copy and set (*ppFree) to
2176 ** zero, or it may create copies of pColset using fts5CloneColset().
2178 static void fts5ParseSetColset(
2179 Fts5Parse *pParse,
2180 Fts5ExprNode *pNode,
2181 Fts5Colset *pColset,
2182 Fts5Colset **ppFree
2184 if( pParse->rc==SQLITE_OK ){
2185 assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
2186 || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR
2187 || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF
2189 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
2190 Fts5ExprNearset *pNear = pNode->pNear;
2191 if( pNear->pColset ){
2192 fts5MergeColset(pNear->pColset, pColset);
2193 if( pNear->pColset->nCol==0 ){
2194 pNode->eType = FTS5_EOF;
2195 pNode->xNext = 0;
2197 }else if( *ppFree ){
2198 pNear->pColset = pColset;
2199 *ppFree = 0;
2200 }else{
2201 pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
2203 }else{
2204 int i;
2205 assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
2206 for(i=0; i<pNode->nChild; i++){
2207 fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
2214 ** Apply colset pColset to expression node pExpr and all of its descendents.
2216 void sqlite3Fts5ParseSetColset(
2217 Fts5Parse *pParse,
2218 Fts5ExprNode *pExpr,
2219 Fts5Colset *pColset
2221 Fts5Colset *pFree = pColset;
2222 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
2223 sqlite3Fts5ParseError(pParse,
2224 "fts5: column queries are not supported (detail=none)"
2226 }else{
2227 fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
2229 sqlite3_free(pFree);
2232 static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
2233 switch( pNode->eType ){
2234 case FTS5_STRING: {
2235 Fts5ExprNearset *pNear = pNode->pNear;
2236 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
2237 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
2238 && pNear->apPhrase[0]->aTerm[0].bFirst==0
2240 pNode->eType = FTS5_TERM;
2241 pNode->xNext = fts5ExprNodeNext_TERM;
2242 }else{
2243 pNode->xNext = fts5ExprNodeNext_STRING;
2245 break;
2248 case FTS5_OR: {
2249 pNode->xNext = fts5ExprNodeNext_OR;
2250 break;
2253 case FTS5_AND: {
2254 pNode->xNext = fts5ExprNodeNext_AND;
2255 break;
2258 default: assert( pNode->eType==FTS5_NOT ); {
2259 pNode->xNext = fts5ExprNodeNext_NOT;
2260 break;
2265 static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
2266 int ii = p->nChild;
2267 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
2268 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
2269 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
2270 p->nChild += pSub->nChild;
2271 sqlite3_free(pSub);
2272 }else{
2273 p->apChild[p->nChild++] = pSub;
2275 for( ; ii<p->nChild; ii++){
2276 p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1);
2281 ** This function is used when parsing LIKE or GLOB patterns against
2282 ** trigram indexes that specify either detail=column or detail=none.
2283 ** It converts a phrase:
2285 ** abc + def + ghi
2287 ** into an AND tree:
2289 ** abc AND def AND ghi
2291 static Fts5ExprNode *fts5ParsePhraseToAnd(
2292 Fts5Parse *pParse,
2293 Fts5ExprNearset *pNear
2295 int nTerm = pNear->apPhrase[0]->nTerm;
2296 int ii;
2297 int nByte;
2298 Fts5ExprNode *pRet;
2300 assert( pNear->nPhrase==1 );
2301 assert( pParse->bPhraseToAnd );
2303 nByte = sizeof(Fts5ExprNode) + nTerm*sizeof(Fts5ExprNode*);
2304 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
2305 if( pRet ){
2306 pRet->eType = FTS5_AND;
2307 pRet->nChild = nTerm;
2308 pRet->iHeight = 1;
2309 fts5ExprAssignXNext(pRet);
2310 pParse->nPhrase--;
2311 for(ii=0; ii<nTerm; ii++){
2312 Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero(
2313 &pParse->rc, sizeof(Fts5ExprPhrase)
2315 if( pPhrase ){
2316 if( parseGrowPhraseArray(pParse) ){
2317 fts5ExprPhraseFree(pPhrase);
2318 }else{
2319 Fts5ExprTerm *p = &pNear->apPhrase[0]->aTerm[ii];
2320 Fts5ExprTerm *pTo = &pPhrase->aTerm[0];
2321 pParse->apPhrase[pParse->nPhrase++] = pPhrase;
2322 pPhrase->nTerm = 1;
2323 pTo->pTerm = sqlite3Fts5Strndup(&pParse->rc, p->pTerm, p->nFullTerm);
2324 pTo->nQueryTerm = p->nQueryTerm;
2325 pTo->nFullTerm = p->nFullTerm;
2326 pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING,
2327 0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase)
2333 if( pParse->rc ){
2334 sqlite3Fts5ParseNodeFree(pRet);
2335 pRet = 0;
2336 }else{
2337 sqlite3Fts5ParseNearsetFree(pNear);
2341 return pRet;
2345 ** Allocate and return a new expression object. If anything goes wrong (i.e.
2346 ** OOM error), leave an error code in pParse and return NULL.
2348 Fts5ExprNode *sqlite3Fts5ParseNode(
2349 Fts5Parse *pParse, /* Parse context */
2350 int eType, /* FTS5_STRING, AND, OR or NOT */
2351 Fts5ExprNode *pLeft, /* Left hand child expression */
2352 Fts5ExprNode *pRight, /* Right hand child expression */
2353 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
2355 Fts5ExprNode *pRet = 0;
2357 if( pParse->rc==SQLITE_OK ){
2358 int nChild = 0; /* Number of children of returned node */
2359 sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
2361 assert( (eType!=FTS5_STRING && !pNear)
2362 || (eType==FTS5_STRING && !pLeft && !pRight)
2364 if( eType==FTS5_STRING && pNear==0 ) return 0;
2365 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
2366 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
2368 if( eType==FTS5_STRING
2369 && pParse->bPhraseToAnd
2370 && pNear->apPhrase[0]->nTerm>1
2372 pRet = fts5ParsePhraseToAnd(pParse, pNear);
2373 }else{
2374 if( eType==FTS5_NOT ){
2375 nChild = 2;
2376 }else if( eType==FTS5_AND || eType==FTS5_OR ){
2377 nChild = 2;
2378 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
2379 if( pRight->eType==eType ) nChild += pRight->nChild-1;
2382 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
2383 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
2385 if( pRet ){
2386 pRet->eType = eType;
2387 pRet->pNear = pNear;
2388 fts5ExprAssignXNext(pRet);
2389 if( eType==FTS5_STRING ){
2390 int iPhrase;
2391 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
2392 pNear->apPhrase[iPhrase]->pNode = pRet;
2393 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
2394 pRet->xNext = 0;
2395 pRet->eType = FTS5_EOF;
2399 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
2400 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
2401 if( pNear->nPhrase!=1
2402 || pPhrase->nTerm>1
2403 || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
2405 sqlite3Fts5ParseError(pParse,
2406 "fts5: %s queries are not supported (detail!=full)",
2407 pNear->nPhrase==1 ? "phrase": "NEAR"
2409 sqlite3_free(pRet);
2410 pRet = 0;
2413 }else{
2414 fts5ExprAddChildren(pRet, pLeft);
2415 fts5ExprAddChildren(pRet, pRight);
2416 if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH ){
2417 sqlite3Fts5ParseError(pParse,
2418 "fts5 expression tree is too large (maximum depth %d)",
2419 SQLITE_FTS5_MAX_EXPR_DEPTH
2421 sqlite3_free(pRet);
2422 pRet = 0;
2429 if( pRet==0 ){
2430 assert( pParse->rc!=SQLITE_OK );
2431 sqlite3Fts5ParseNodeFree(pLeft);
2432 sqlite3Fts5ParseNodeFree(pRight);
2433 sqlite3Fts5ParseNearsetFree(pNear);
2435 return pRet;
2438 Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
2439 Fts5Parse *pParse, /* Parse context */
2440 Fts5ExprNode *pLeft, /* Left hand child expression */
2441 Fts5ExprNode *pRight /* Right hand child expression */
2443 Fts5ExprNode *pRet = 0;
2444 Fts5ExprNode *pPrev;
2446 if( pParse->rc ){
2447 sqlite3Fts5ParseNodeFree(pLeft);
2448 sqlite3Fts5ParseNodeFree(pRight);
2449 }else{
2451 assert( pLeft->eType==FTS5_STRING
2452 || pLeft->eType==FTS5_TERM
2453 || pLeft->eType==FTS5_EOF
2454 || pLeft->eType==FTS5_AND
2456 assert( pRight->eType==FTS5_STRING
2457 || pRight->eType==FTS5_TERM
2458 || pRight->eType==FTS5_EOF
2459 || (pRight->eType==FTS5_AND && pParse->bPhraseToAnd)
2462 if( pLeft->eType==FTS5_AND ){
2463 pPrev = pLeft->apChild[pLeft->nChild-1];
2464 }else{
2465 pPrev = pLeft;
2467 assert( pPrev->eType==FTS5_STRING
2468 || pPrev->eType==FTS5_TERM
2469 || pPrev->eType==FTS5_EOF
2472 if( pRight->eType==FTS5_EOF ){
2473 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
2474 sqlite3Fts5ParseNodeFree(pRight);
2475 pRet = pLeft;
2476 pParse->nPhrase--;
2478 else if( pPrev->eType==FTS5_EOF ){
2479 Fts5ExprPhrase **ap;
2481 if( pPrev==pLeft ){
2482 pRet = pRight;
2483 }else{
2484 pLeft->apChild[pLeft->nChild-1] = pRight;
2485 pRet = pLeft;
2488 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
2489 assert( ap[0]==pPrev->pNear->apPhrase[0] );
2490 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
2491 pParse->nPhrase--;
2493 sqlite3Fts5ParseNodeFree(pPrev);
2495 else{
2496 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
2500 return pRet;
2503 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
2504 static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
2505 sqlite3_int64 nByte = 0;
2506 Fts5ExprTerm *p;
2507 char *zQuoted;
2509 /* Determine the maximum amount of space required. */
2510 for(p=pTerm; p; p=p->pSynonym){
2511 nByte += pTerm->nQueryTerm * 2 + 3 + 2;
2513 zQuoted = sqlite3_malloc64(nByte);
2515 if( zQuoted ){
2516 int i = 0;
2517 for(p=pTerm; p; p=p->pSynonym){
2518 char *zIn = p->pTerm;
2519 char *zEnd = &zIn[p->nQueryTerm];
2520 zQuoted[i++] = '"';
2521 while( zIn<zEnd ){
2522 if( *zIn=='"' ) zQuoted[i++] = '"';
2523 zQuoted[i++] = *zIn++;
2525 zQuoted[i++] = '"';
2526 if( p->pSynonym ) zQuoted[i++] = '|';
2528 if( pTerm->bPrefix ){
2529 zQuoted[i++] = ' ';
2530 zQuoted[i++] = '*';
2532 zQuoted[i++] = '\0';
2534 return zQuoted;
2537 static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
2538 char *zNew;
2539 va_list ap;
2540 va_start(ap, zFmt);
2541 zNew = sqlite3_vmprintf(zFmt, ap);
2542 va_end(ap);
2543 if( zApp && zNew ){
2544 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
2545 sqlite3_free(zNew);
2546 zNew = zNew2;
2548 sqlite3_free(zApp);
2549 return zNew;
2553 ** Compose a tcl-readable representation of expression pExpr. Return a
2554 ** pointer to a buffer containing that representation. It is the
2555 ** responsibility of the caller to at some point free the buffer using
2556 ** sqlite3_free().
2558 static char *fts5ExprPrintTcl(
2559 Fts5Config *pConfig,
2560 const char *zNearsetCmd,
2561 Fts5ExprNode *pExpr
2563 char *zRet = 0;
2564 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
2565 Fts5ExprNearset *pNear = pExpr->pNear;
2566 int i;
2567 int iTerm;
2569 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
2570 if( zRet==0 ) return 0;
2571 if( pNear->pColset ){
2572 int *aiCol = pNear->pColset->aiCol;
2573 int nCol = pNear->pColset->nCol;
2574 if( nCol==1 ){
2575 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
2576 }else{
2577 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
2578 for(i=1; i<pNear->pColset->nCol; i++){
2579 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
2581 zRet = fts5PrintfAppend(zRet, "} ");
2583 if( zRet==0 ) return 0;
2586 if( pNear->nPhrase>1 ){
2587 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
2588 if( zRet==0 ) return 0;
2591 zRet = fts5PrintfAppend(zRet, "--");
2592 if( zRet==0 ) return 0;
2594 for(i=0; i<pNear->nPhrase; i++){
2595 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
2597 zRet = fts5PrintfAppend(zRet, " {");
2598 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
2599 Fts5ExprTerm *p = &pPhrase->aTerm[iTerm];
2600 zRet = fts5PrintfAppend(zRet, "%s%.*s", iTerm==0?"":" ",
2601 p->nQueryTerm, p->pTerm
2603 if( pPhrase->aTerm[iTerm].bPrefix ){
2604 zRet = fts5PrintfAppend(zRet, "*");
2608 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
2609 if( zRet==0 ) return 0;
2612 }else if( pExpr->eType==0 ){
2613 zRet = sqlite3_mprintf("{}");
2614 }else{
2615 char const *zOp = 0;
2616 int i;
2617 switch( pExpr->eType ){
2618 case FTS5_AND: zOp = "AND"; break;
2619 case FTS5_NOT: zOp = "NOT"; break;
2620 default:
2621 assert( pExpr->eType==FTS5_OR );
2622 zOp = "OR";
2623 break;
2626 zRet = sqlite3_mprintf("%s", zOp);
2627 for(i=0; zRet && i<pExpr->nChild; i++){
2628 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
2629 if( !z ){
2630 sqlite3_free(zRet);
2631 zRet = 0;
2632 }else{
2633 zRet = fts5PrintfAppend(zRet, " [%z]", z);
2638 return zRet;
2641 static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
2642 char *zRet = 0;
2643 if( pExpr->eType==0 ){
2644 return sqlite3_mprintf("\"\"");
2645 }else
2646 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
2647 Fts5ExprNearset *pNear = pExpr->pNear;
2648 int i;
2649 int iTerm;
2651 if( pNear->pColset ){
2652 int ii;
2653 Fts5Colset *pColset = pNear->pColset;
2654 if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{");
2655 for(ii=0; ii<pColset->nCol; ii++){
2656 zRet = fts5PrintfAppend(zRet, "%s%s",
2657 pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " "
2660 if( zRet ){
2661 zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : "");
2663 if( zRet==0 ) return 0;
2666 if( pNear->nPhrase>1 ){
2667 zRet = fts5PrintfAppend(zRet, "NEAR(");
2668 if( zRet==0 ) return 0;
2671 for(i=0; i<pNear->nPhrase; i++){
2672 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
2673 if( i!=0 ){
2674 zRet = fts5PrintfAppend(zRet, " ");
2675 if( zRet==0 ) return 0;
2677 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
2678 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
2679 if( zTerm ){
2680 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
2681 sqlite3_free(zTerm);
2683 if( zTerm==0 || zRet==0 ){
2684 sqlite3_free(zRet);
2685 return 0;
2690 if( pNear->nPhrase>1 ){
2691 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
2692 if( zRet==0 ) return 0;
2695 }else{
2696 char const *zOp = 0;
2697 int i;
2699 switch( pExpr->eType ){
2700 case FTS5_AND: zOp = " AND "; break;
2701 case FTS5_NOT: zOp = " NOT "; break;
2702 default:
2703 assert( pExpr->eType==FTS5_OR );
2704 zOp = " OR ";
2705 break;
2708 for(i=0; i<pExpr->nChild; i++){
2709 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
2710 if( z==0 ){
2711 sqlite3_free(zRet);
2712 zRet = 0;
2713 }else{
2714 int e = pExpr->apChild[i]->eType;
2715 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
2716 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
2717 (i==0 ? "" : zOp),
2718 (b?"(":""), z, (b?")":"")
2721 if( zRet==0 ) break;
2725 return zRet;
2729 ** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
2730 ** and fts5_expr_tcl() (bTcl!=0).
2732 static void fts5ExprFunction(
2733 sqlite3_context *pCtx, /* Function call context */
2734 int nArg, /* Number of args */
2735 sqlite3_value **apVal, /* Function arguments */
2736 int bTcl
2738 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
2739 sqlite3 *db = sqlite3_context_db_handle(pCtx);
2740 const char *zExpr = 0;
2741 char *zErr = 0;
2742 Fts5Expr *pExpr = 0;
2743 int rc;
2744 int i;
2746 const char **azConfig; /* Array of arguments for Fts5Config */
2747 const char *zNearsetCmd = "nearset";
2748 int nConfig; /* Size of azConfig[] */
2749 Fts5Config *pConfig = 0;
2750 int iArg = 1;
2752 if( nArg<1 ){
2753 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
2754 bTcl ? "fts5_expr_tcl" : "fts5_expr"
2756 sqlite3_result_error(pCtx, zErr, -1);
2757 sqlite3_free(zErr);
2758 return;
2761 if( bTcl && nArg>1 ){
2762 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
2763 iArg = 2;
2766 nConfig = 3 + (nArg-iArg);
2767 azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
2768 if( azConfig==0 ){
2769 sqlite3_result_error_nomem(pCtx);
2770 return;
2772 azConfig[0] = 0;
2773 azConfig[1] = "main";
2774 azConfig[2] = "tbl";
2775 for(i=3; iArg<nArg; iArg++){
2776 const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
2777 azConfig[i++] = (z ? z : "");
2780 zExpr = (const char*)sqlite3_value_text(apVal[0]);
2781 if( zExpr==0 ) zExpr = "";
2783 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
2784 if( rc==SQLITE_OK ){
2785 rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr);
2787 if( rc==SQLITE_OK ){
2788 char *zText;
2789 if( pExpr->pRoot->xNext==0 ){
2790 zText = sqlite3_mprintf("");
2791 }else if( bTcl ){
2792 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
2793 }else{
2794 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
2796 if( zText==0 ){
2797 rc = SQLITE_NOMEM;
2798 }else{
2799 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
2800 sqlite3_free(zText);
2804 if( rc!=SQLITE_OK ){
2805 if( zErr ){
2806 sqlite3_result_error(pCtx, zErr, -1);
2807 sqlite3_free(zErr);
2808 }else{
2809 sqlite3_result_error_code(pCtx, rc);
2812 sqlite3_free((void *)azConfig);
2813 sqlite3Fts5ConfigFree(pConfig);
2814 sqlite3Fts5ExprFree(pExpr);
2817 static void fts5ExprFunctionHr(
2818 sqlite3_context *pCtx, /* Function call context */
2819 int nArg, /* Number of args */
2820 sqlite3_value **apVal /* Function arguments */
2822 fts5ExprFunction(pCtx, nArg, apVal, 0);
2824 static void fts5ExprFunctionTcl(
2825 sqlite3_context *pCtx, /* Function call context */
2826 int nArg, /* Number of args */
2827 sqlite3_value **apVal /* Function arguments */
2829 fts5ExprFunction(pCtx, nArg, apVal, 1);
2833 ** The implementation of an SQLite user-defined-function that accepts a
2834 ** single integer as an argument. If the integer is an alpha-numeric
2835 ** unicode code point, 1 is returned. Otherwise 0.
2837 static void fts5ExprIsAlnum(
2838 sqlite3_context *pCtx, /* Function call context */
2839 int nArg, /* Number of args */
2840 sqlite3_value **apVal /* Function arguments */
2842 int iCode;
2843 u8 aArr[32];
2844 if( nArg!=1 ){
2845 sqlite3_result_error(pCtx,
2846 "wrong number of arguments to function fts5_isalnum", -1
2848 return;
2850 memset(aArr, 0, sizeof(aArr));
2851 sqlite3Fts5UnicodeCatParse("L*", aArr);
2852 sqlite3Fts5UnicodeCatParse("N*", aArr);
2853 sqlite3Fts5UnicodeCatParse("Co", aArr);
2854 iCode = sqlite3_value_int(apVal[0]);
2855 sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
2858 static void fts5ExprFold(
2859 sqlite3_context *pCtx, /* Function call context */
2860 int nArg, /* Number of args */
2861 sqlite3_value **apVal /* Function arguments */
2863 if( nArg!=1 && nArg!=2 ){
2864 sqlite3_result_error(pCtx,
2865 "wrong number of arguments to function fts5_fold", -1
2867 }else{
2868 int iCode;
2869 int bRemoveDiacritics = 0;
2870 iCode = sqlite3_value_int(apVal[0]);
2871 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
2872 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
2875 #endif /* if SQLITE_TEST || SQLITE_FTS5_DEBUG */
2878 ** This is called during initialization to register the fts5_expr() scalar
2879 ** UDF with the SQLite handle passed as the only argument.
2881 int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
2882 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
2883 struct Fts5ExprFunc {
2884 const char *z;
2885 void (*x)(sqlite3_context*,int,sqlite3_value**);
2886 } aFunc[] = {
2887 { "fts5_expr", fts5ExprFunctionHr },
2888 { "fts5_expr_tcl", fts5ExprFunctionTcl },
2889 { "fts5_isalnum", fts5ExprIsAlnum },
2890 { "fts5_fold", fts5ExprFold },
2892 int i;
2893 int rc = SQLITE_OK;
2894 void *pCtx = (void*)pGlobal;
2896 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
2897 struct Fts5ExprFunc *p = &aFunc[i];
2898 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
2900 #else
2901 int rc = SQLITE_OK;
2902 UNUSED_PARAM2(pGlobal,db);
2903 #endif
2905 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
2906 ** sqlite3Fts5ParserFallback() are unused */
2907 #ifndef NDEBUG
2908 (void)sqlite3Fts5ParserTrace;
2909 #endif
2910 (void)sqlite3Fts5ParserFallback;
2912 return rc;
2916 ** Return the number of phrases in expression pExpr.
2918 int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
2919 return (pExpr ? pExpr->nPhrase : 0);
2923 ** Return the number of terms in the iPhrase'th phrase in pExpr.
2925 int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
2926 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
2927 return pExpr->apExprPhrase[iPhrase]->nTerm;
2931 ** This function is used to access the current position list for phrase
2932 ** iPhrase.
2934 int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
2935 int nRet;
2936 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
2937 Fts5ExprNode *pNode = pPhrase->pNode;
2938 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
2939 *pa = pPhrase->poslist.p;
2940 nRet = pPhrase->poslist.n;
2941 }else{
2942 *pa = 0;
2943 nRet = 0;
2945 return nRet;
2948 struct Fts5PoslistPopulator {
2949 Fts5PoslistWriter writer;
2950 int bOk; /* True if ok to populate */
2951 int bMiss;
2955 ** Clear the position lists associated with all phrases in the expression
2956 ** passed as the first argument. Argument bLive is true if the expression
2957 ** might be pointing to a real entry, otherwise it has just been reset.
2959 ** At present this function is only used for detail=col and detail=none
2960 ** fts5 tables. This implies that all phrases must be at most 1 token
2961 ** in size, as phrase matches are not supported without detail=full.
2963 Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
2964 Fts5PoslistPopulator *pRet;
2965 pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
2966 if( pRet ){
2967 int i;
2968 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
2969 for(i=0; i<pExpr->nPhrase; i++){
2970 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
2971 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
2972 assert( pExpr->apExprPhrase[i]->nTerm<=1 );
2973 if( bLive &&
2974 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
2976 pRet[i].bMiss = 1;
2977 }else{
2978 pBuf->n = 0;
2982 return pRet;
2985 struct Fts5ExprCtx {
2986 Fts5Expr *pExpr;
2987 Fts5PoslistPopulator *aPopulator;
2988 i64 iOff;
2990 typedef struct Fts5ExprCtx Fts5ExprCtx;
2993 ** TODO: Make this more efficient!
2995 static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
2996 int i;
2997 for(i=0; i<pColset->nCol; i++){
2998 if( pColset->aiCol[i]==iCol ) return 1;
3000 return 0;
3004 ** pToken is a buffer nToken bytes in size that may or may not contain
3005 ** an embedded 0x00 byte. If it does, return the number of bytes in
3006 ** the buffer before the 0x00. If it does not, return nToken.
3008 static int fts5QueryTerm(const char *pToken, int nToken){
3009 int ii;
3010 for(ii=0; ii<nToken && pToken[ii]; ii++){}
3011 return ii;
3014 static int fts5ExprPopulatePoslistsCb(
3015 void *pCtx, /* Copy of 2nd argument to xTokenize() */
3016 int tflags, /* Mask of FTS5_TOKEN_* flags */
3017 const char *pToken, /* Pointer to buffer containing token */
3018 int nToken, /* Size of token in bytes */
3019 int iUnused1, /* Byte offset of token within input text */
3020 int iUnused2 /* Byte offset of end of token within input text */
3022 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
3023 Fts5Expr *pExpr = p->pExpr;
3024 int i;
3025 int nQuery = nToken;
3026 i64 iRowid = pExpr->pRoot->iRowid;
3028 UNUSED_PARAM2(iUnused1, iUnused2);
3030 if( nQuery>FTS5_MAX_TOKEN_SIZE ) nQuery = FTS5_MAX_TOKEN_SIZE;
3031 if( pExpr->pConfig->bTokendata ){
3032 nQuery = fts5QueryTerm(pToken, nQuery);
3034 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
3035 for(i=0; i<pExpr->nPhrase; i++){
3036 Fts5ExprTerm *pT;
3037 if( p->aPopulator[i].bOk==0 ) continue;
3038 for(pT=&pExpr->apExprPhrase[i]->aTerm[0]; pT; pT=pT->pSynonym){
3039 if( (pT->nQueryTerm==nQuery || (pT->nQueryTerm<nQuery && pT->bPrefix))
3040 && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0
3042 int rc = sqlite3Fts5PoslistWriterAppend(
3043 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
3045 if( rc==SQLITE_OK && pExpr->pConfig->bTokendata && !pT->bPrefix ){
3046 int iCol = p->iOff>>32;
3047 int iTokOff = p->iOff & 0x7FFFFFFF;
3048 rc = sqlite3Fts5IndexIterWriteTokendata(
3049 pT->pIter, pToken, nToken, iRowid, iCol, iTokOff
3052 if( rc ) return rc;
3053 break;
3057 return SQLITE_OK;
3060 int sqlite3Fts5ExprPopulatePoslists(
3061 Fts5Config *pConfig,
3062 Fts5Expr *pExpr,
3063 Fts5PoslistPopulator *aPopulator,
3064 int iCol,
3065 const char *z, int n
3067 int i;
3068 Fts5ExprCtx sCtx;
3069 sCtx.pExpr = pExpr;
3070 sCtx.aPopulator = aPopulator;
3071 sCtx.iOff = (((i64)iCol) << 32) - 1;
3073 for(i=0; i<pExpr->nPhrase; i++){
3074 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
3075 Fts5Colset *pColset = pNode->pNear->pColset;
3076 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
3077 || aPopulator[i].bMiss
3079 aPopulator[i].bOk = 0;
3080 }else{
3081 aPopulator[i].bOk = 1;
3085 return sqlite3Fts5Tokenize(pConfig,
3086 FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
3090 static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
3091 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
3092 pNode->pNear->apPhrase[0]->poslist.n = 0;
3093 }else{
3094 int i;
3095 for(i=0; i<pNode->nChild; i++){
3096 fts5ExprClearPoslists(pNode->apChild[i]);
3101 static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
3102 pNode->iRowid = iRowid;
3103 pNode->bEof = 0;
3104 switch( pNode->eType ){
3105 case FTS5_TERM:
3106 case FTS5_STRING:
3107 return (pNode->pNear->apPhrase[0]->poslist.n>0);
3109 case FTS5_AND: {
3110 int i;
3111 for(i=0; i<pNode->nChild; i++){
3112 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
3113 fts5ExprClearPoslists(pNode);
3114 return 0;
3117 break;
3120 case FTS5_OR: {
3121 int i;
3122 int bRet = 0;
3123 for(i=0; i<pNode->nChild; i++){
3124 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
3125 bRet = 1;
3128 return bRet;
3131 default: {
3132 assert( pNode->eType==FTS5_NOT );
3133 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
3134 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
3136 fts5ExprClearPoslists(pNode);
3137 return 0;
3139 break;
3142 return 1;
3145 void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
3146 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
3150 ** This function is only called for detail=columns tables.
3152 int sqlite3Fts5ExprPhraseCollist(
3153 Fts5Expr *pExpr,
3154 int iPhrase,
3155 const u8 **ppCollist,
3156 int *pnCollist
3158 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
3159 Fts5ExprNode *pNode = pPhrase->pNode;
3160 int rc = SQLITE_OK;
3162 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
3163 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
3165 if( pNode->bEof==0
3166 && pNode->iRowid==pExpr->pRoot->iRowid
3167 && pPhrase->poslist.n>0
3169 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
3170 if( pTerm->pSynonym ){
3171 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
3172 rc = fts5ExprSynonymList(
3173 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
3175 }else{
3176 *ppCollist = pPhrase->aTerm[0].pIter->pData;
3177 *pnCollist = pPhrase->aTerm[0].pIter->nData;
3179 }else{
3180 *ppCollist = 0;
3181 *pnCollist = 0;
3184 return rc;
3188 ** Does the work of the fts5_api.xQueryToken() API method.
3190 int sqlite3Fts5ExprQueryToken(
3191 Fts5Expr *pExpr,
3192 int iPhrase,
3193 int iToken,
3194 const char **ppOut,
3195 int *pnOut
3197 Fts5ExprPhrase *pPhrase = 0;
3199 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
3200 return SQLITE_RANGE;
3202 pPhrase = pExpr->apExprPhrase[iPhrase];
3203 if( iToken<0 || iToken>=pPhrase->nTerm ){
3204 return SQLITE_RANGE;
3207 *ppOut = pPhrase->aTerm[iToken].pTerm;
3208 *pnOut = pPhrase->aTerm[iToken].nFullTerm;
3209 return SQLITE_OK;
3213 ** Does the work of the fts5_api.xInstToken() API method.
3215 int sqlite3Fts5ExprInstToken(
3216 Fts5Expr *pExpr,
3217 i64 iRowid,
3218 int iPhrase,
3219 int iCol,
3220 int iOff,
3221 int iToken,
3222 const char **ppOut,
3223 int *pnOut
3225 Fts5ExprPhrase *pPhrase = 0;
3226 Fts5ExprTerm *pTerm = 0;
3227 int rc = SQLITE_OK;
3229 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
3230 return SQLITE_RANGE;
3232 pPhrase = pExpr->apExprPhrase[iPhrase];
3233 if( iToken<0 || iToken>=pPhrase->nTerm ){
3234 return SQLITE_RANGE;
3236 pTerm = &pPhrase->aTerm[iToken];
3237 if( pTerm->bPrefix==0 ){
3238 if( pExpr->pConfig->bTokendata ){
3239 rc = sqlite3Fts5IterToken(
3240 pTerm->pIter, iRowid, iCol, iOff+iToken, ppOut, pnOut
3242 }else{
3243 *ppOut = pTerm->pTerm;
3244 *pnOut = pTerm->nFullTerm;
3247 return rc;
3251 ** Clear the token mappings for all Fts5IndexIter objects mannaged by
3252 ** the expression passed as the only argument.
3254 void sqlite3Fts5ExprClearTokens(Fts5Expr *pExpr){
3255 int ii;
3256 for(ii=0; ii<pExpr->nPhrase; ii++){
3257 Fts5ExprTerm *pT;
3258 for(pT=&pExpr->apExprPhrase[ii]->aTerm[0]; pT; pT=pT->pSynonym){
3259 sqlite3Fts5IndexIterClearTokendata(pT->pIter);