Snapshot of upstream SQLite 3.15.2
[sqlcipher.git] / ext / fts5 / fts5_expr.c
blobbde9eaed8bb4e07a5324cd5f357b4fc5d4852493
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"
21 ** All token types in the generated fts5parse.h file are greater than 0.
23 #define FTS5_EOF 0
25 #define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
27 typedef struct Fts5ExprTerm Fts5ExprTerm;
30 ** Functions generated by lemon from fts5parse.y.
32 void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
33 void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
34 void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
35 #ifndef NDEBUG
36 #include <stdio.h>
37 void sqlite3Fts5ParserTrace(FILE*, char*);
38 #endif
41 struct Fts5Expr {
42 Fts5Index *pIndex;
43 Fts5Config *pConfig;
44 Fts5ExprNode *pRoot;
45 int bDesc; /* Iterate in descending rowid order */
46 int nPhrase; /* Number of phrases in expression */
47 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
51 ** eType:
52 ** Expression node type. Always one of:
54 ** FTS5_AND (nChild, apChild valid)
55 ** FTS5_OR (nChild, apChild valid)
56 ** FTS5_NOT (nChild, apChild valid)
57 ** FTS5_STRING (pNear valid)
58 ** FTS5_TERM (pNear valid)
60 struct Fts5ExprNode {
61 int eType; /* Node type */
62 int bEof; /* True at EOF */
63 int bNomatch; /* True if entry is not a match */
65 /* Next method for this node. */
66 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
68 i64 iRowid; /* Current rowid */
69 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
71 /* Child nodes. For a NOT node, this array always contains 2 entries. For
72 ** AND or OR nodes, it contains 2 or more entries. */
73 int nChild; /* Number of child nodes */
74 Fts5ExprNode *apChild[1]; /* Array of child nodes */
77 #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
80 ** Invoke the xNext method of an Fts5ExprNode object. This macro should be
81 ** used as if it has the same signature as the xNext() methods themselves.
83 #define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
86 ** An instance of the following structure represents a single search term
87 ** or term prefix.
89 struct Fts5ExprTerm {
90 int bPrefix; /* True for a prefix term */
91 char *zTerm; /* nul-terminated term */
92 Fts5IndexIter *pIter; /* Iterator for this term */
93 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
97 ** A phrase. One or more terms that must appear in a contiguous sequence
98 ** within a document for it to match.
100 struct Fts5ExprPhrase {
101 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
102 Fts5Buffer poslist; /* Current position list */
103 int nTerm; /* Number of entries in aTerm[] */
104 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */
108 ** One or more phrases that must appear within a certain token distance of
109 ** each other within each matching document.
111 struct Fts5ExprNearset {
112 int nNear; /* NEAR parameter */
113 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
114 int nPhrase; /* Number of entries in aPhrase[] array */
115 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */
120 ** Parse context.
122 struct Fts5Parse {
123 Fts5Config *pConfig;
124 char *zErr;
125 int rc;
126 int nPhrase; /* Size of apPhrase array */
127 Fts5ExprPhrase **apPhrase; /* Array of all phrases */
128 Fts5ExprNode *pExpr; /* Result of a successful parse */
131 void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
132 va_list ap;
133 va_start(ap, zFmt);
134 if( pParse->rc==SQLITE_OK ){
135 pParse->zErr = sqlite3_vmprintf(zFmt, ap);
136 pParse->rc = SQLITE_ERROR;
138 va_end(ap);
141 static int fts5ExprIsspace(char t){
142 return t==' ' || t=='\t' || t=='\n' || t=='\r';
146 ** Read the first token from the nul-terminated string at *pz.
148 static int fts5ExprGetToken(
149 Fts5Parse *pParse,
150 const char **pz, /* IN/OUT: Pointer into buffer */
151 Fts5Token *pToken
153 const char *z = *pz;
154 int tok;
156 /* Skip past any whitespace */
157 while( fts5ExprIsspace(*z) ) z++;
159 pToken->p = z;
160 pToken->n = 1;
161 switch( *z ){
162 case '(': tok = FTS5_LP; break;
163 case ')': tok = FTS5_RP; break;
164 case '{': tok = FTS5_LCP; break;
165 case '}': tok = FTS5_RCP; break;
166 case ':': tok = FTS5_COLON; break;
167 case ',': tok = FTS5_COMMA; break;
168 case '+': tok = FTS5_PLUS; break;
169 case '*': tok = FTS5_STAR; break;
170 case '-': tok = FTS5_MINUS; break;
171 case '\0': tok = FTS5_EOF; break;
173 case '"': {
174 const char *z2;
175 tok = FTS5_STRING;
177 for(z2=&z[1]; 1; z2++){
178 if( z2[0]=='"' ){
179 z2++;
180 if( z2[0]!='"' ) break;
182 if( z2[0]=='\0' ){
183 sqlite3Fts5ParseError(pParse, "unterminated string");
184 return FTS5_EOF;
187 pToken->n = (z2 - z);
188 break;
191 default: {
192 const char *z2;
193 if( sqlite3Fts5IsBareword(z[0])==0 ){
194 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
195 return FTS5_EOF;
197 tok = FTS5_STRING;
198 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
199 pToken->n = (z2 - z);
200 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
201 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
202 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
203 break;
207 *pz = &pToken->p[pToken->n];
208 return tok;
211 static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc((int)t); }
212 static void fts5ParseFree(void *p){ sqlite3_free(p); }
214 int sqlite3Fts5ExprNew(
215 Fts5Config *pConfig, /* FTS5 Configuration */
216 const char *zExpr, /* Expression text */
217 Fts5Expr **ppNew,
218 char **pzErr
220 Fts5Parse sParse;
221 Fts5Token token;
222 const char *z = zExpr;
223 int t; /* Next token type */
224 void *pEngine;
225 Fts5Expr *pNew;
227 *ppNew = 0;
228 *pzErr = 0;
229 memset(&sParse, 0, sizeof(sParse));
230 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
231 if( pEngine==0 ){ return SQLITE_NOMEM; }
232 sParse.pConfig = pConfig;
234 do {
235 t = fts5ExprGetToken(&sParse, &z, &token);
236 sqlite3Fts5Parser(pEngine, t, token, &sParse);
237 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
238 sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
240 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
241 if( sParse.rc==SQLITE_OK ){
242 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
243 if( pNew==0 ){
244 sParse.rc = SQLITE_NOMEM;
245 sqlite3Fts5ParseNodeFree(sParse.pExpr);
246 }else{
247 if( !sParse.pExpr ){
248 const int nByte = sizeof(Fts5ExprNode);
249 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
250 if( pNew->pRoot ){
251 pNew->pRoot->bEof = 1;
253 }else{
254 pNew->pRoot = sParse.pExpr;
256 pNew->pIndex = 0;
257 pNew->pConfig = pConfig;
258 pNew->apExprPhrase = sParse.apPhrase;
259 pNew->nPhrase = sParse.nPhrase;
260 sParse.apPhrase = 0;
262 }else{
263 sqlite3Fts5ParseNodeFree(sParse.pExpr);
266 sqlite3_free(sParse.apPhrase);
267 *pzErr = sParse.zErr;
268 return sParse.rc;
272 ** Free the expression node object passed as the only argument.
274 void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
275 if( p ){
276 int i;
277 for(i=0; i<p->nChild; i++){
278 sqlite3Fts5ParseNodeFree(p->apChild[i]);
280 sqlite3Fts5ParseNearsetFree(p->pNear);
281 sqlite3_free(p);
286 ** Free the expression object passed as the only argument.
288 void sqlite3Fts5ExprFree(Fts5Expr *p){
289 if( p ){
290 sqlite3Fts5ParseNodeFree(p->pRoot);
291 sqlite3_free(p->apExprPhrase);
292 sqlite3_free(p);
297 ** Argument pTerm must be a synonym iterator. Return the current rowid
298 ** that it points to.
300 static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
301 i64 iRet = 0;
302 int bRetValid = 0;
303 Fts5ExprTerm *p;
305 assert( pTerm->pSynonym );
306 assert( bDesc==0 || bDesc==1 );
307 for(p=pTerm; p; p=p->pSynonym){
308 if( 0==sqlite3Fts5IterEof(p->pIter) ){
309 i64 iRowid = p->pIter->iRowid;
310 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
311 iRet = iRowid;
312 bRetValid = 1;
317 if( pbEof && bRetValid==0 ) *pbEof = 1;
318 return iRet;
322 ** Argument pTerm must be a synonym iterator.
324 static int fts5ExprSynonymList(
325 Fts5ExprTerm *pTerm,
326 i64 iRowid,
327 Fts5Buffer *pBuf, /* Use this buffer for space if required */
328 u8 **pa, int *pn
330 Fts5PoslistReader aStatic[4];
331 Fts5PoslistReader *aIter = aStatic;
332 int nIter = 0;
333 int nAlloc = 4;
334 int rc = SQLITE_OK;
335 Fts5ExprTerm *p;
337 assert( pTerm->pSynonym );
338 for(p=pTerm; p; p=p->pSynonym){
339 Fts5IndexIter *pIter = p->pIter;
340 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
341 if( pIter->nData==0 ) continue;
342 if( nIter==nAlloc ){
343 int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
344 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte);
345 if( aNew==0 ){
346 rc = SQLITE_NOMEM;
347 goto synonym_poslist_out;
349 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
350 nAlloc = nAlloc*2;
351 if( aIter!=aStatic ) sqlite3_free(aIter);
352 aIter = aNew;
354 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
355 assert( aIter[nIter].bEof==0 );
356 nIter++;
360 if( nIter==1 ){
361 *pa = (u8*)aIter[0].a;
362 *pn = aIter[0].n;
363 }else{
364 Fts5PoslistWriter writer = {0};
365 i64 iPrev = -1;
366 fts5BufferZero(pBuf);
367 while( 1 ){
368 int i;
369 i64 iMin = FTS5_LARGEST_INT64;
370 for(i=0; i<nIter; i++){
371 if( aIter[i].bEof==0 ){
372 if( aIter[i].iPos==iPrev ){
373 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
375 if( aIter[i].iPos<iMin ){
376 iMin = aIter[i].iPos;
380 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
381 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
382 iPrev = iMin;
384 if( rc==SQLITE_OK ){
385 *pa = pBuf->p;
386 *pn = pBuf->n;
390 synonym_poslist_out:
391 if( aIter!=aStatic ) sqlite3_free(aIter);
392 return rc;
397 ** All individual term iterators in pPhrase are guaranteed to be valid and
398 ** pointing to the same rowid when this function is called. This function
399 ** checks if the current rowid really is a match, and if so populates
400 ** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
401 ** is set to true if this is really a match, or false otherwise.
403 ** SQLITE_OK is returned if an error occurs, or an SQLite error code
404 ** otherwise. It is not considered an error code if the current rowid is
405 ** not a match.
407 static int fts5ExprPhraseIsMatch(
408 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
409 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
410 int *pbMatch /* OUT: Set to true if really a match */
412 Fts5PoslistWriter writer = {0};
413 Fts5PoslistReader aStatic[4];
414 Fts5PoslistReader *aIter = aStatic;
415 int i;
416 int rc = SQLITE_OK;
418 fts5BufferZero(&pPhrase->poslist);
420 /* If the aStatic[] array is not large enough, allocate a large array
421 ** using sqlite3_malloc(). This approach could be improved upon. */
422 if( pPhrase->nTerm>ArraySize(aStatic) ){
423 int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
424 aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte);
425 if( !aIter ) return SQLITE_NOMEM;
427 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
429 /* Initialize a term iterator for each term in the phrase */
430 for(i=0; i<pPhrase->nTerm; i++){
431 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
432 int n = 0;
433 int bFlag = 0;
434 u8 *a = 0;
435 if( pTerm->pSynonym ){
436 Fts5Buffer buf = {0, 0, 0};
437 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
438 if( rc ){
439 sqlite3_free(a);
440 goto ismatch_out;
442 if( a==buf.p ) bFlag = 1;
443 }else{
444 a = (u8*)pTerm->pIter->pData;
445 n = pTerm->pIter->nData;
447 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
448 aIter[i].bFlag = (u8)bFlag;
449 if( aIter[i].bEof ) goto ismatch_out;
452 while( 1 ){
453 int bMatch;
454 i64 iPos = aIter[0].iPos;
455 do {
456 bMatch = 1;
457 for(i=0; i<pPhrase->nTerm; i++){
458 Fts5PoslistReader *pPos = &aIter[i];
459 i64 iAdj = iPos + i;
460 if( pPos->iPos!=iAdj ){
461 bMatch = 0;
462 while( pPos->iPos<iAdj ){
463 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
465 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
468 }while( bMatch==0 );
470 /* Append position iPos to the output */
471 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
472 if( rc!=SQLITE_OK ) goto ismatch_out;
474 for(i=0; i<pPhrase->nTerm; i++){
475 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
479 ismatch_out:
480 *pbMatch = (pPhrase->poslist.n>0);
481 for(i=0; i<pPhrase->nTerm; i++){
482 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
484 if( aIter!=aStatic ) sqlite3_free(aIter);
485 return rc;
488 typedef struct Fts5LookaheadReader Fts5LookaheadReader;
489 struct Fts5LookaheadReader {
490 const u8 *a; /* Buffer containing position list */
491 int n; /* Size of buffer a[] in bytes */
492 int i; /* Current offset in position list */
493 i64 iPos; /* Current position */
494 i64 iLookahead; /* Next position */
497 #define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
499 static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
500 p->iPos = p->iLookahead;
501 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
502 p->iLookahead = FTS5_LOOKAHEAD_EOF;
504 return (p->iPos==FTS5_LOOKAHEAD_EOF);
507 static int fts5LookaheadReaderInit(
508 const u8 *a, int n, /* Buffer to read position list from */
509 Fts5LookaheadReader *p /* Iterator object to initialize */
511 memset(p, 0, sizeof(Fts5LookaheadReader));
512 p->a = a;
513 p->n = n;
514 fts5LookaheadReaderNext(p);
515 return fts5LookaheadReaderNext(p);
518 typedef struct Fts5NearTrimmer Fts5NearTrimmer;
519 struct Fts5NearTrimmer {
520 Fts5LookaheadReader reader; /* Input iterator */
521 Fts5PoslistWriter writer; /* Writer context */
522 Fts5Buffer *pOut; /* Output poslist */
526 ** The near-set object passed as the first argument contains more than
527 ** one phrase. All phrases currently point to the same row. The
528 ** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
529 ** tests if the current row contains instances of each phrase sufficiently
530 ** close together to meet the NEAR constraint. Non-zero is returned if it
531 ** does, or zero otherwise.
533 ** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
534 ** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
535 ** occurs within this function (*pRc) is set accordingly before returning.
536 ** The return value is undefined in both these cases.
538 ** If no error occurs and non-zero (a match) is returned, the position-list
539 ** of each phrase object is edited to contain only those entries that
540 ** meet the constraint before returning.
542 static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
543 Fts5NearTrimmer aStatic[4];
544 Fts5NearTrimmer *a = aStatic;
545 Fts5ExprPhrase **apPhrase = pNear->apPhrase;
547 int i;
548 int rc = *pRc;
549 int bMatch;
551 assert( pNear->nPhrase>1 );
553 /* If the aStatic[] array is not large enough, allocate a large array
554 ** using sqlite3_malloc(). This approach could be improved upon. */
555 if( pNear->nPhrase>ArraySize(aStatic) ){
556 int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
557 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
558 }else{
559 memset(aStatic, 0, sizeof(aStatic));
561 if( rc!=SQLITE_OK ){
562 *pRc = rc;
563 return 0;
566 /* Initialize a lookahead iterator for each phrase. After passing the
567 ** buffer and buffer size to the lookaside-reader init function, zero
568 ** the phrase poslist buffer. The new poslist for the phrase (containing
569 ** the same entries as the original with some entries removed on account
570 ** of the NEAR constraint) is written over the original even as it is
571 ** being read. This is safe as the entries for the new poslist are a
572 ** subset of the old, so it is not possible for data yet to be read to
573 ** be overwritten. */
574 for(i=0; i<pNear->nPhrase; i++){
575 Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
576 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
577 pPoslist->n = 0;
578 a[i].pOut = pPoslist;
581 while( 1 ){
582 int iAdv;
583 i64 iMin;
584 i64 iMax;
586 /* This block advances the phrase iterators until they point to a set of
587 ** entries that together comprise a match. */
588 iMax = a[0].reader.iPos;
589 do {
590 bMatch = 1;
591 for(i=0; i<pNear->nPhrase; i++){
592 Fts5LookaheadReader *pPos = &a[i].reader;
593 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
594 if( pPos->iPos<iMin || pPos->iPos>iMax ){
595 bMatch = 0;
596 while( pPos->iPos<iMin ){
597 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
599 if( pPos->iPos>iMax ) iMax = pPos->iPos;
602 }while( bMatch==0 );
604 /* Add an entry to each output position list */
605 for(i=0; i<pNear->nPhrase; i++){
606 i64 iPos = a[i].reader.iPos;
607 Fts5PoslistWriter *pWriter = &a[i].writer;
608 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
609 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
613 iAdv = 0;
614 iMin = a[0].reader.iLookahead;
615 for(i=0; i<pNear->nPhrase; i++){
616 if( a[i].reader.iLookahead < iMin ){
617 iMin = a[i].reader.iLookahead;
618 iAdv = i;
621 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
624 ismatch_out: {
625 int bRet = a[0].pOut->n>0;
626 *pRc = rc;
627 if( a!=aStatic ) sqlite3_free(a);
628 return bRet;
633 ** Advance iterator pIter until it points to a value equal to or laster
634 ** than the initial value of *piLast. If this means the iterator points
635 ** to a value laster than *piLast, update *piLast to the new lastest value.
637 ** If the iterator reaches EOF, set *pbEof to true before returning. If
638 ** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
639 ** are set, return a non-zero value. Otherwise, return zero.
641 static int fts5ExprAdvanceto(
642 Fts5IndexIter *pIter, /* Iterator to advance */
643 int bDesc, /* True if iterator is "rowid DESC" */
644 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
645 int *pRc, /* OUT: Error code */
646 int *pbEof /* OUT: Set to true if EOF */
648 i64 iLast = *piLast;
649 i64 iRowid;
651 iRowid = pIter->iRowid;
652 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
653 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
654 if( rc || sqlite3Fts5IterEof(pIter) ){
655 *pRc = rc;
656 *pbEof = 1;
657 return 1;
659 iRowid = pIter->iRowid;
660 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
662 *piLast = iRowid;
664 return 0;
667 static int fts5ExprSynonymAdvanceto(
668 Fts5ExprTerm *pTerm, /* Term iterator to advance */
669 int bDesc, /* True if iterator is "rowid DESC" */
670 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
671 int *pRc /* OUT: Error code */
673 int rc = SQLITE_OK;
674 i64 iLast = *piLast;
675 Fts5ExprTerm *p;
676 int bEof = 0;
678 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
679 if( sqlite3Fts5IterEof(p->pIter)==0 ){
680 i64 iRowid = p->pIter->iRowid;
681 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
682 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
687 if( rc!=SQLITE_OK ){
688 *pRc = rc;
689 bEof = 1;
690 }else{
691 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
693 return bEof;
697 static int fts5ExprNearTest(
698 int *pRc,
699 Fts5Expr *pExpr, /* Expression that pNear is a part of */
700 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
702 Fts5ExprNearset *pNear = pNode->pNear;
703 int rc = *pRc;
705 if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
706 Fts5ExprTerm *pTerm;
707 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
708 pPhrase->poslist.n = 0;
709 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
710 Fts5IndexIter *pIter = pTerm->pIter;
711 if( sqlite3Fts5IterEof(pIter)==0 ){
712 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
713 pPhrase->poslist.n = 1;
717 return pPhrase->poslist.n;
718 }else{
719 int i;
721 /* Check that each phrase in the nearset matches the current row.
722 ** Populate the pPhrase->poslist buffers at the same time. If any
723 ** phrase is not a match, break out of the loop early. */
724 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
725 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
726 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){
727 int bMatch = 0;
728 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
729 if( bMatch==0 ) break;
730 }else{
731 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
732 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
736 *pRc = rc;
737 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
738 return 1;
740 return 0;
746 ** Initialize all term iterators in the pNear object. If any term is found
747 ** to match no documents at all, return immediately without initializing any
748 ** further iterators.
750 static int fts5ExprNearInitAll(
751 Fts5Expr *pExpr,
752 Fts5ExprNode *pNode
754 Fts5ExprNearset *pNear = pNode->pNear;
755 int i, j;
756 int rc = SQLITE_OK;
757 int bEof = 1;
759 assert( pNode->bNomatch==0 );
760 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
761 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
762 for(j=0; j<pPhrase->nTerm; j++){
763 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
764 Fts5ExprTerm *p;
766 for(p=pTerm; p && rc==SQLITE_OK; p=p->pSynonym){
767 if( p->pIter ){
768 sqlite3Fts5IterClose(p->pIter);
769 p->pIter = 0;
771 rc = sqlite3Fts5IndexQuery(
772 pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
773 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
774 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
775 pNear->pColset,
776 &p->pIter
778 assert( rc==SQLITE_OK || p->pIter==0 );
779 if( p->pIter && 0==sqlite3Fts5IterEof(p->pIter) ){
780 bEof = 0;
784 if( bEof ) break;
786 if( bEof ) break;
789 pNode->bEof = bEof;
790 return rc;
794 ** If pExpr is an ASC iterator, this function returns a value with the
795 ** same sign as:
797 ** (iLhs - iRhs)
799 ** Otherwise, if this is a DESC iterator, the opposite is returned:
801 ** (iRhs - iLhs)
803 static int fts5RowidCmp(
804 Fts5Expr *pExpr,
805 i64 iLhs,
806 i64 iRhs
808 assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
809 if( pExpr->bDesc==0 ){
810 if( iLhs<iRhs ) return -1;
811 return (iLhs > iRhs);
812 }else{
813 if( iLhs>iRhs ) return -1;
814 return (iLhs < iRhs);
818 static void fts5ExprSetEof(Fts5ExprNode *pNode){
819 int i;
820 pNode->bEof = 1;
821 pNode->bNomatch = 0;
822 for(i=0; i<pNode->nChild; i++){
823 fts5ExprSetEof(pNode->apChild[i]);
827 static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
828 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
829 Fts5ExprNearset *pNear = pNode->pNear;
830 int i;
831 for(i=0; i<pNear->nPhrase; i++){
832 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
833 pPhrase->poslist.n = 0;
835 }else{
836 int i;
837 for(i=0; i<pNode->nChild; i++){
838 fts5ExprNodeZeroPoslist(pNode->apChild[i]);
846 ** Compare the values currently indicated by the two nodes as follows:
848 ** res = (*p1) - (*p2)
850 ** Nodes that point to values that come later in the iteration order are
851 ** considered to be larger. Nodes at EOF are the largest of all.
853 ** This means that if the iteration order is ASC, then numerically larger
854 ** rowids are considered larger. Or if it is the default DESC, numerically
855 ** smaller rowids are larger.
857 static int fts5NodeCompare(
858 Fts5Expr *pExpr,
859 Fts5ExprNode *p1,
860 Fts5ExprNode *p2
862 if( p2->bEof ) return -1;
863 if( p1->bEof ) return +1;
864 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
868 ** All individual term iterators in pNear are guaranteed to be valid when
869 ** this function is called. This function checks if all term iterators
870 ** point to the same rowid, and if not, advances them until they do.
871 ** If an EOF is reached before this happens, *pbEof is set to true before
872 ** returning.
874 ** SQLITE_OK is returned if an error occurs, or an SQLite error code
875 ** otherwise. It is not considered an error code if an iterator reaches
876 ** EOF.
878 static int fts5ExprNodeTest_STRING(
879 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
880 Fts5ExprNode *pNode
882 Fts5ExprNearset *pNear = pNode->pNear;
883 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
884 int rc = SQLITE_OK;
885 i64 iLast; /* Lastest rowid any iterator points to */
886 int i, j; /* Phrase and token index, respectively */
887 int bMatch; /* True if all terms are at the same rowid */
888 const int bDesc = pExpr->bDesc;
890 /* Check that this node should not be FTS5_TERM */
891 assert( pNear->nPhrase>1
892 || pNear->apPhrase[0]->nTerm>1
893 || pNear->apPhrase[0]->aTerm[0].pSynonym
896 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
897 ** iterator skips through rowids in the default ascending order, this means
898 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
899 ** means the minimum rowid. */
900 if( pLeft->aTerm[0].pSynonym ){
901 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
902 }else{
903 iLast = pLeft->aTerm[0].pIter->iRowid;
906 do {
907 bMatch = 1;
908 for(i=0; i<pNear->nPhrase; i++){
909 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
910 for(j=0; j<pPhrase->nTerm; j++){
911 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
912 if( pTerm->pSynonym ){
913 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
914 if( iRowid==iLast ) continue;
915 bMatch = 0;
916 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
917 pNode->bNomatch = 0;
918 pNode->bEof = 1;
919 return rc;
921 }else{
922 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
923 if( pIter->iRowid==iLast || pIter->bEof ) continue;
924 bMatch = 0;
925 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
926 return rc;
931 }while( bMatch==0 );
933 pNode->iRowid = iLast;
934 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
935 assert( pNode->bEof==0 || pNode->bNomatch==0 );
937 return rc;
941 ** Advance the first term iterator in the first phrase of pNear. Set output
942 ** variable *pbEof to true if it reaches EOF or if an error occurs.
944 ** Return SQLITE_OK if successful, or an SQLite error code if an error
945 ** occurs.
947 static int fts5ExprNodeNext_STRING(
948 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
949 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
950 int bFromValid,
951 i64 iFrom
953 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
954 int rc = SQLITE_OK;
956 pNode->bNomatch = 0;
957 if( pTerm->pSynonym ){
958 int bEof = 1;
959 Fts5ExprTerm *p;
961 /* Find the firstest rowid any synonym points to. */
962 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
964 /* Advance each iterator that currently points to iRowid. Or, if iFrom
965 ** is valid - each iterator that points to a rowid before iFrom. */
966 for(p=pTerm; p; p=p->pSynonym){
967 if( sqlite3Fts5IterEof(p->pIter)==0 ){
968 i64 ii = p->pIter->iRowid;
969 if( ii==iRowid
970 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
972 if( bFromValid ){
973 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
974 }else{
975 rc = sqlite3Fts5IterNext(p->pIter);
977 if( rc!=SQLITE_OK ) break;
978 if( sqlite3Fts5IterEof(p->pIter)==0 ){
979 bEof = 0;
981 }else{
982 bEof = 0;
987 /* Set the EOF flag if either all synonym iterators are at EOF or an
988 ** error has occurred. */
989 pNode->bEof = (rc || bEof);
990 }else{
991 Fts5IndexIter *pIter = pTerm->pIter;
993 assert( Fts5NodeIsString(pNode) );
994 if( bFromValid ){
995 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
996 }else{
997 rc = sqlite3Fts5IterNext(pIter);
1000 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
1003 if( pNode->bEof==0 ){
1004 assert( rc==SQLITE_OK );
1005 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
1008 return rc;
1012 static int fts5ExprNodeTest_TERM(
1013 Fts5Expr *pExpr, /* Expression that pNear is a part of */
1014 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
1016 /* As this "NEAR" object is actually a single phrase that consists
1017 ** of a single term only, grab pointers into the poslist managed by the
1018 ** fts5_index.c iterator object. This is much faster than synthesizing
1019 ** a new poslist the way we have to for more complicated phrase or NEAR
1020 ** expressions. */
1021 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
1022 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
1024 assert( pNode->eType==FTS5_TERM );
1025 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
1026 assert( pPhrase->aTerm[0].pSynonym==0 );
1028 pPhrase->poslist.n = pIter->nData;
1029 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
1030 pPhrase->poslist.p = (u8*)pIter->pData;
1032 pNode->iRowid = pIter->iRowid;
1033 pNode->bNomatch = (pPhrase->poslist.n==0);
1034 return SQLITE_OK;
1038 ** xNext() method for a node of type FTS5_TERM.
1040 static int fts5ExprNodeNext_TERM(
1041 Fts5Expr *pExpr,
1042 Fts5ExprNode *pNode,
1043 int bFromValid,
1044 i64 iFrom
1046 int rc;
1047 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
1049 assert( pNode->bEof==0 );
1050 if( bFromValid ){
1051 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
1052 }else{
1053 rc = sqlite3Fts5IterNext(pIter);
1055 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
1056 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
1057 }else{
1058 pNode->bEof = 1;
1059 pNode->bNomatch = 0;
1061 return rc;
1064 static void fts5ExprNodeTest_OR(
1065 Fts5Expr *pExpr, /* Expression of which pNode is a part */
1066 Fts5ExprNode *pNode /* Expression node to test */
1068 Fts5ExprNode *pNext = pNode->apChild[0];
1069 int i;
1071 for(i=1; i<pNode->nChild; i++){
1072 Fts5ExprNode *pChild = pNode->apChild[i];
1073 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
1074 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
1075 pNext = pChild;
1078 pNode->iRowid = pNext->iRowid;
1079 pNode->bEof = pNext->bEof;
1080 pNode->bNomatch = pNext->bNomatch;
1083 static int fts5ExprNodeNext_OR(
1084 Fts5Expr *pExpr,
1085 Fts5ExprNode *pNode,
1086 int bFromValid,
1087 i64 iFrom
1089 int i;
1090 i64 iLast = pNode->iRowid;
1092 for(i=0; i<pNode->nChild; i++){
1093 Fts5ExprNode *p1 = pNode->apChild[i];
1094 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
1095 if( p1->bEof==0 ){
1096 if( (p1->iRowid==iLast)
1097 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
1099 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
1100 if( rc!=SQLITE_OK ) return rc;
1105 fts5ExprNodeTest_OR(pExpr, pNode);
1106 return SQLITE_OK;
1110 ** Argument pNode is an FTS5_AND node.
1112 static int fts5ExprNodeTest_AND(
1113 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
1114 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
1116 int iChild;
1117 i64 iLast = pAnd->iRowid;
1118 int rc = SQLITE_OK;
1119 int bMatch;
1121 assert( pAnd->bEof==0 );
1122 do {
1123 pAnd->bNomatch = 0;
1124 bMatch = 1;
1125 for(iChild=0; iChild<pAnd->nChild; iChild++){
1126 Fts5ExprNode *pChild = pAnd->apChild[iChild];
1127 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
1128 if( cmp>0 ){
1129 /* Advance pChild until it points to iLast or laster */
1130 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
1131 if( rc!=SQLITE_OK ) return rc;
1134 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
1135 ** the child node is guaranteed to have advanced at least as far as
1136 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
1137 ** new lastest rowid seen so far. */
1138 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
1139 if( pChild->bEof ){
1140 fts5ExprSetEof(pAnd);
1141 bMatch = 1;
1142 break;
1143 }else if( iLast!=pChild->iRowid ){
1144 bMatch = 0;
1145 iLast = pChild->iRowid;
1148 if( pChild->bNomatch ){
1149 pAnd->bNomatch = 1;
1152 }while( bMatch==0 );
1154 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
1155 fts5ExprNodeZeroPoslist(pAnd);
1157 pAnd->iRowid = iLast;
1158 return SQLITE_OK;
1161 static int fts5ExprNodeNext_AND(
1162 Fts5Expr *pExpr,
1163 Fts5ExprNode *pNode,
1164 int bFromValid,
1165 i64 iFrom
1167 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
1168 if( rc==SQLITE_OK ){
1169 rc = fts5ExprNodeTest_AND(pExpr, pNode);
1171 return rc;
1174 static int fts5ExprNodeTest_NOT(
1175 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
1176 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
1178 int rc = SQLITE_OK;
1179 Fts5ExprNode *p1 = pNode->apChild[0];
1180 Fts5ExprNode *p2 = pNode->apChild[1];
1181 assert( pNode->nChild==2 );
1183 while( rc==SQLITE_OK && p1->bEof==0 ){
1184 int cmp = fts5NodeCompare(pExpr, p1, p2);
1185 if( cmp>0 ){
1186 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
1187 cmp = fts5NodeCompare(pExpr, p1, p2);
1189 assert( rc!=SQLITE_OK || cmp<=0 );
1190 if( cmp || p2->bNomatch ) break;
1191 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
1193 pNode->bEof = p1->bEof;
1194 pNode->bNomatch = p1->bNomatch;
1195 pNode->iRowid = p1->iRowid;
1196 if( p1->bEof ){
1197 fts5ExprNodeZeroPoslist(p2);
1199 return rc;
1202 static int fts5ExprNodeNext_NOT(
1203 Fts5Expr *pExpr,
1204 Fts5ExprNode *pNode,
1205 int bFromValid,
1206 i64 iFrom
1208 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
1209 if( rc==SQLITE_OK ){
1210 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
1212 return rc;
1216 ** If pNode currently points to a match, this function returns SQLITE_OK
1217 ** without modifying it. Otherwise, pNode is advanced until it does point
1218 ** to a match or EOF is reached.
1220 static int fts5ExprNodeTest(
1221 Fts5Expr *pExpr, /* Expression of which pNode is a part */
1222 Fts5ExprNode *pNode /* Expression node to test */
1224 int rc = SQLITE_OK;
1225 if( pNode->bEof==0 ){
1226 switch( pNode->eType ){
1228 case FTS5_STRING: {
1229 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
1230 break;
1233 case FTS5_TERM: {
1234 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
1235 break;
1238 case FTS5_AND: {
1239 rc = fts5ExprNodeTest_AND(pExpr, pNode);
1240 break;
1243 case FTS5_OR: {
1244 fts5ExprNodeTest_OR(pExpr, pNode);
1245 break;
1248 default: assert( pNode->eType==FTS5_NOT ); {
1249 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
1250 break;
1254 return rc;
1259 ** Set node pNode, which is part of expression pExpr, to point to the first
1260 ** match. If there are no matches, set the Node.bEof flag to indicate EOF.
1262 ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
1263 ** It is not an error if there are no matches.
1265 static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
1266 int rc = SQLITE_OK;
1267 pNode->bEof = 0;
1268 pNode->bNomatch = 0;
1270 if( Fts5NodeIsString(pNode) ){
1271 /* Initialize all term iterators in the NEAR object. */
1272 rc = fts5ExprNearInitAll(pExpr, pNode);
1273 }else if( pNode->xNext==0 ){
1274 pNode->bEof = 1;
1275 }else{
1276 int i;
1277 int nEof = 0;
1278 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
1279 Fts5ExprNode *pChild = pNode->apChild[i];
1280 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
1281 assert( pChild->bEof==0 || pChild->bEof==1 );
1282 nEof += pChild->bEof;
1284 pNode->iRowid = pNode->apChild[0]->iRowid;
1286 switch( pNode->eType ){
1287 case FTS5_AND:
1288 if( nEof>0 ) fts5ExprSetEof(pNode);
1289 break;
1291 case FTS5_OR:
1292 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
1293 break;
1295 default:
1296 assert( pNode->eType==FTS5_NOT );
1297 pNode->bEof = pNode->apChild[0]->bEof;
1298 break;
1302 if( rc==SQLITE_OK ){
1303 rc = fts5ExprNodeTest(pExpr, pNode);
1305 return rc;
1310 ** Begin iterating through the set of documents in index pIdx matched by
1311 ** the MATCH expression passed as the first argument. If the "bDesc"
1312 ** parameter is passed a non-zero value, iteration is in descending rowid
1313 ** order. Or, if it is zero, in ascending order.
1315 ** If iterating in ascending rowid order (bDesc==0), the first document
1316 ** visited is that with the smallest rowid that is larger than or equal
1317 ** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
1318 ** then the first document visited must have a rowid smaller than or
1319 ** equal to iFirst.
1321 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
1322 ** is not considered an error if the query does not match any documents.
1324 int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
1325 Fts5ExprNode *pRoot = p->pRoot;
1326 int rc; /* Return code */
1328 p->pIndex = pIdx;
1329 p->bDesc = bDesc;
1330 rc = fts5ExprNodeFirst(p, pRoot);
1332 /* If not at EOF but the current rowid occurs earlier than iFirst in
1333 ** the iteration order, move to document iFirst or later. */
1334 if( pRoot->bEof==0 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 ){
1335 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
1338 /* If the iterator is not at a real match, skip forward until it is. */
1339 while( pRoot->bNomatch ){
1340 assert( pRoot->bEof==0 && rc==SQLITE_OK );
1341 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
1343 return rc;
1347 ** Move to the next document
1349 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
1350 ** is not considered an error if the query does not match any documents.
1352 int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
1353 int rc;
1354 Fts5ExprNode *pRoot = p->pRoot;
1355 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
1356 do {
1357 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
1358 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
1359 }while( pRoot->bNomatch );
1360 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
1361 pRoot->bEof = 1;
1363 return rc;
1366 int sqlite3Fts5ExprEof(Fts5Expr *p){
1367 return p->pRoot->bEof;
1370 i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
1371 return p->pRoot->iRowid;
1374 static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
1375 int rc = SQLITE_OK;
1376 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
1377 return rc;
1381 ** Free the phrase object passed as the only argument.
1383 static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
1384 if( pPhrase ){
1385 int i;
1386 for(i=0; i<pPhrase->nTerm; i++){
1387 Fts5ExprTerm *pSyn;
1388 Fts5ExprTerm *pNext;
1389 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
1390 sqlite3_free(pTerm->zTerm);
1391 sqlite3Fts5IterClose(pTerm->pIter);
1392 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
1393 pNext = pSyn->pSynonym;
1394 sqlite3Fts5IterClose(pSyn->pIter);
1395 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
1396 sqlite3_free(pSyn);
1399 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
1400 sqlite3_free(pPhrase);
1405 ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
1406 ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
1407 ** appended to it and the results returned.
1409 ** If an OOM error occurs, both the pNear and pPhrase objects are freed and
1410 ** NULL returned.
1412 Fts5ExprNearset *sqlite3Fts5ParseNearset(
1413 Fts5Parse *pParse, /* Parse context */
1414 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
1415 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
1417 const int SZALLOC = 8;
1418 Fts5ExprNearset *pRet = 0;
1420 if( pParse->rc==SQLITE_OK ){
1421 if( pPhrase==0 ){
1422 return pNear;
1424 if( pNear==0 ){
1425 int nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*);
1426 pRet = sqlite3_malloc(nByte);
1427 if( pRet==0 ){
1428 pParse->rc = SQLITE_NOMEM;
1429 }else{
1430 memset(pRet, 0, nByte);
1432 }else if( (pNear->nPhrase % SZALLOC)==0 ){
1433 int nNew = pNear->nPhrase + SZALLOC;
1434 int nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*);
1436 pRet = (Fts5ExprNearset*)sqlite3_realloc(pNear, nByte);
1437 if( pRet==0 ){
1438 pParse->rc = SQLITE_NOMEM;
1440 }else{
1441 pRet = pNear;
1445 if( pRet==0 ){
1446 assert( pParse->rc!=SQLITE_OK );
1447 sqlite3Fts5ParseNearsetFree(pNear);
1448 sqlite3Fts5ParsePhraseFree(pPhrase);
1449 }else{
1450 if( pRet->nPhrase>0 ){
1451 Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
1452 assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
1453 if( pPhrase->nTerm==0 ){
1454 fts5ExprPhraseFree(pPhrase);
1455 pRet->nPhrase--;
1456 pParse->nPhrase--;
1457 pPhrase = pLast;
1458 }else if( pLast->nTerm==0 ){
1459 fts5ExprPhraseFree(pLast);
1460 pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
1461 pParse->nPhrase--;
1462 pRet->nPhrase--;
1465 pRet->apPhrase[pRet->nPhrase++] = pPhrase;
1467 return pRet;
1470 typedef struct TokenCtx TokenCtx;
1471 struct TokenCtx {
1472 Fts5ExprPhrase *pPhrase;
1473 int rc;
1477 ** Callback for tokenizing terms used by ParseTerm().
1479 static int fts5ParseTokenize(
1480 void *pContext, /* Pointer to Fts5InsertCtx object */
1481 int tflags, /* Mask of FTS5_TOKEN_* flags */
1482 const char *pToken, /* Buffer containing token */
1483 int nToken, /* Size of token in bytes */
1484 int iUnused1, /* Start offset of token */
1485 int iUnused2 /* End offset of token */
1487 int rc = SQLITE_OK;
1488 const int SZALLOC = 8;
1489 TokenCtx *pCtx = (TokenCtx*)pContext;
1490 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
1492 UNUSED_PARAM2(iUnused1, iUnused2);
1494 /* If an error has already occurred, this is a no-op */
1495 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
1496 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
1498 if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
1499 Fts5ExprTerm *pSyn;
1500 int nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
1501 pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte);
1502 if( pSyn==0 ){
1503 rc = SQLITE_NOMEM;
1504 }else{
1505 memset(pSyn, 0, nByte);
1506 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
1507 memcpy(pSyn->zTerm, pToken, nToken);
1508 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
1509 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
1511 }else{
1512 Fts5ExprTerm *pTerm;
1513 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
1514 Fts5ExprPhrase *pNew;
1515 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
1517 pNew = (Fts5ExprPhrase*)sqlite3_realloc(pPhrase,
1518 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew
1520 if( pNew==0 ){
1521 rc = SQLITE_NOMEM;
1522 }else{
1523 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase));
1524 pCtx->pPhrase = pPhrase = pNew;
1525 pNew->nTerm = nNew - SZALLOC;
1529 if( rc==SQLITE_OK ){
1530 pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
1531 memset(pTerm, 0, sizeof(Fts5ExprTerm));
1532 pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
1536 pCtx->rc = rc;
1537 return rc;
1542 ** Free the phrase object passed as the only argument.
1544 void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
1545 fts5ExprPhraseFree(pPhrase);
1549 ** Free the phrase object passed as the second argument.
1551 void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
1552 if( pNear ){
1553 int i;
1554 for(i=0; i<pNear->nPhrase; i++){
1555 fts5ExprPhraseFree(pNear->apPhrase[i]);
1557 sqlite3_free(pNear->pColset);
1558 sqlite3_free(pNear);
1562 void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
1563 assert( pParse->pExpr==0 );
1564 pParse->pExpr = p;
1568 ** This function is called by the parser to process a string token. The
1569 ** string may or may not be quoted. In any case it is tokenized and a
1570 ** phrase object consisting of all tokens returned.
1572 Fts5ExprPhrase *sqlite3Fts5ParseTerm(
1573 Fts5Parse *pParse, /* Parse context */
1574 Fts5ExprPhrase *pAppend, /* Phrase to append to */
1575 Fts5Token *pToken, /* String to tokenize */
1576 int bPrefix /* True if there is a trailing "*" */
1578 Fts5Config *pConfig = pParse->pConfig;
1579 TokenCtx sCtx; /* Context object passed to callback */
1580 int rc; /* Tokenize return code */
1581 char *z = 0;
1583 memset(&sCtx, 0, sizeof(TokenCtx));
1584 sCtx.pPhrase = pAppend;
1586 rc = fts5ParseStringFromToken(pToken, &z);
1587 if( rc==SQLITE_OK ){
1588 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0);
1589 int n;
1590 sqlite3Fts5Dequote(z);
1591 n = (int)strlen(z);
1592 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
1594 sqlite3_free(z);
1595 if( rc || (rc = sCtx.rc) ){
1596 pParse->rc = rc;
1597 fts5ExprPhraseFree(sCtx.pPhrase);
1598 sCtx.pPhrase = 0;
1599 }else{
1601 if( pAppend==0 ){
1602 if( (pParse->nPhrase % 8)==0 ){
1603 int nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
1604 Fts5ExprPhrase **apNew;
1605 apNew = (Fts5ExprPhrase**)sqlite3_realloc(pParse->apPhrase, nByte);
1606 if( apNew==0 ){
1607 pParse->rc = SQLITE_NOMEM;
1608 fts5ExprPhraseFree(sCtx.pPhrase);
1609 return 0;
1611 pParse->apPhrase = apNew;
1613 pParse->nPhrase++;
1616 if( sCtx.pPhrase==0 ){
1617 /* This happens when parsing a token or quoted phrase that contains
1618 ** no token characters at all. (e.g ... MATCH '""'). */
1619 sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase));
1620 }else if( sCtx.pPhrase->nTerm ){
1621 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix;
1623 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
1626 return sCtx.pPhrase;
1630 ** Create a new FTS5 expression by cloning phrase iPhrase of the
1631 ** expression passed as the second argument.
1633 int sqlite3Fts5ExprClonePhrase(
1634 Fts5Expr *pExpr,
1635 int iPhrase,
1636 Fts5Expr **ppNew
1638 int rc = SQLITE_OK; /* Return code */
1639 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
1640 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
1641 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
1643 pOrig = pExpr->apExprPhrase[iPhrase];
1644 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
1645 if( rc==SQLITE_OK ){
1646 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
1647 sizeof(Fts5ExprPhrase*));
1649 if( rc==SQLITE_OK ){
1650 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc,
1651 sizeof(Fts5ExprNode));
1653 if( rc==SQLITE_OK ){
1654 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
1655 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*));
1657 if( rc==SQLITE_OK ){
1658 Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
1659 if( pColsetOrig ){
1660 int nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int);
1661 Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
1662 if( pColset ){
1663 memcpy(pColset, pColsetOrig, nByte);
1665 pNew->pRoot->pNear->pColset = pColset;
1669 if( pOrig->nTerm ){
1670 int i; /* Used to iterate through phrase terms */
1671 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
1672 int tflags = 0;
1673 Fts5ExprTerm *p;
1674 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
1675 const char *zTerm = p->zTerm;
1676 rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
1677 0, 0);
1678 tflags = FTS5_TOKEN_COLOCATED;
1680 if( rc==SQLITE_OK ){
1681 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
1684 }else{
1685 /* This happens when parsing a token or quoted phrase that contains
1686 ** no token characters at all. (e.g ... MATCH '""'). */
1687 sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
1690 if( rc==SQLITE_OK ){
1691 /* All the allocations succeeded. Put the expression object together. */
1692 pNew->pIndex = pExpr->pIndex;
1693 pNew->pConfig = pExpr->pConfig;
1694 pNew->nPhrase = 1;
1695 pNew->apExprPhrase[0] = sCtx.pPhrase;
1696 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
1697 pNew->pRoot->pNear->nPhrase = 1;
1698 sCtx.pPhrase->pNode = pNew->pRoot;
1700 if( pOrig->nTerm==1 && pOrig->aTerm[0].pSynonym==0 ){
1701 pNew->pRoot->eType = FTS5_TERM;
1702 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
1703 }else{
1704 pNew->pRoot->eType = FTS5_STRING;
1705 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
1707 }else{
1708 sqlite3Fts5ExprFree(pNew);
1709 fts5ExprPhraseFree(sCtx.pPhrase);
1710 pNew = 0;
1713 *ppNew = pNew;
1714 return rc;
1719 ** Token pTok has appeared in a MATCH expression where the NEAR operator
1720 ** is expected. If token pTok does not contain "NEAR", store an error
1721 ** in the pParse object.
1723 void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
1724 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
1725 sqlite3Fts5ParseError(
1726 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
1731 void sqlite3Fts5ParseSetDistance(
1732 Fts5Parse *pParse,
1733 Fts5ExprNearset *pNear,
1734 Fts5Token *p
1736 if( pNear ){
1737 int nNear = 0;
1738 int i;
1739 if( p->n ){
1740 for(i=0; i<p->n; i++){
1741 char c = (char)p->p[i];
1742 if( c<'0' || c>'9' ){
1743 sqlite3Fts5ParseError(
1744 pParse, "expected integer, got \"%.*s\"", p->n, p->p
1746 return;
1748 nNear = nNear * 10 + (p->p[i] - '0');
1750 }else{
1751 nNear = FTS5_DEFAULT_NEARDIST;
1753 pNear->nNear = nNear;
1758 ** The second argument passed to this function may be NULL, or it may be
1759 ** an existing Fts5Colset object. This function returns a pointer to
1760 ** a new colset object containing the contents of (p) with new value column
1761 ** number iCol appended.
1763 ** If an OOM error occurs, store an error code in pParse and return NULL.
1764 ** The old colset object (if any) is not freed in this case.
1766 static Fts5Colset *fts5ParseColset(
1767 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
1768 Fts5Colset *p, /* Existing colset object */
1769 int iCol /* New column to add to colset object */
1771 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
1772 Fts5Colset *pNew; /* New colset object to return */
1774 assert( pParse->rc==SQLITE_OK );
1775 assert( iCol>=0 && iCol<pParse->pConfig->nCol );
1777 pNew = sqlite3_realloc(p, sizeof(Fts5Colset) + sizeof(int)*nCol);
1778 if( pNew==0 ){
1779 pParse->rc = SQLITE_NOMEM;
1780 }else{
1781 int *aiCol = pNew->aiCol;
1782 int i, j;
1783 for(i=0; i<nCol; i++){
1784 if( aiCol[i]==iCol ) return pNew;
1785 if( aiCol[i]>iCol ) break;
1787 for(j=nCol; j>i; j--){
1788 aiCol[j] = aiCol[j-1];
1790 aiCol[i] = iCol;
1791 pNew->nCol = nCol+1;
1793 #ifndef NDEBUG
1794 /* Check that the array is in order and contains no duplicate entries. */
1795 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
1796 #endif
1799 return pNew;
1803 ** Allocate and return an Fts5Colset object specifying the inverse of
1804 ** the colset passed as the second argument. Free the colset passed
1805 ** as the second argument before returning.
1807 Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
1808 Fts5Colset *pRet;
1809 int nCol = pParse->pConfig->nCol;
1811 pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
1812 sizeof(Fts5Colset) + sizeof(int)*nCol
1814 if( pRet ){
1815 int i;
1816 int iOld = 0;
1817 for(i=0; i<nCol; i++){
1818 if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
1819 pRet->aiCol[pRet->nCol++] = i;
1820 }else{
1821 iOld++;
1826 sqlite3_free(p);
1827 return pRet;
1830 Fts5Colset *sqlite3Fts5ParseColset(
1831 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
1832 Fts5Colset *pColset, /* Existing colset object */
1833 Fts5Token *p
1835 Fts5Colset *pRet = 0;
1836 int iCol;
1837 char *z; /* Dequoted copy of token p */
1839 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
1840 if( pParse->rc==SQLITE_OK ){
1841 Fts5Config *pConfig = pParse->pConfig;
1842 sqlite3Fts5Dequote(z);
1843 for(iCol=0; iCol<pConfig->nCol; iCol++){
1844 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
1846 if( iCol==pConfig->nCol ){
1847 sqlite3Fts5ParseError(pParse, "no such column: %s", z);
1848 }else{
1849 pRet = fts5ParseColset(pParse, pColset, iCol);
1851 sqlite3_free(z);
1854 if( pRet==0 ){
1855 assert( pParse->rc!=SQLITE_OK );
1856 sqlite3_free(pColset);
1859 return pRet;
1862 void sqlite3Fts5ParseSetColset(
1863 Fts5Parse *pParse,
1864 Fts5ExprNearset *pNear,
1865 Fts5Colset *pColset
1867 if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
1868 pParse->rc = SQLITE_ERROR;
1869 pParse->zErr = sqlite3_mprintf(
1870 "fts5: column queries are not supported (detail=none)"
1872 sqlite3_free(pColset);
1873 return;
1876 if( pNear ){
1877 pNear->pColset = pColset;
1878 }else{
1879 sqlite3_free(pColset);
1883 static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
1884 switch( pNode->eType ){
1885 case FTS5_STRING: {
1886 Fts5ExprNearset *pNear = pNode->pNear;
1887 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
1888 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
1890 pNode->eType = FTS5_TERM;
1891 pNode->xNext = fts5ExprNodeNext_TERM;
1892 }else{
1893 pNode->xNext = fts5ExprNodeNext_STRING;
1895 break;
1898 case FTS5_OR: {
1899 pNode->xNext = fts5ExprNodeNext_OR;
1900 break;
1903 case FTS5_AND: {
1904 pNode->xNext = fts5ExprNodeNext_AND;
1905 break;
1908 default: assert( pNode->eType==FTS5_NOT ); {
1909 pNode->xNext = fts5ExprNodeNext_NOT;
1910 break;
1915 static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
1916 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
1917 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
1918 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
1919 p->nChild += pSub->nChild;
1920 sqlite3_free(pSub);
1921 }else{
1922 p->apChild[p->nChild++] = pSub;
1927 ** Allocate and return a new expression object. If anything goes wrong (i.e.
1928 ** OOM error), leave an error code in pParse and return NULL.
1930 Fts5ExprNode *sqlite3Fts5ParseNode(
1931 Fts5Parse *pParse, /* Parse context */
1932 int eType, /* FTS5_STRING, AND, OR or NOT */
1933 Fts5ExprNode *pLeft, /* Left hand child expression */
1934 Fts5ExprNode *pRight, /* Right hand child expression */
1935 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
1937 Fts5ExprNode *pRet = 0;
1939 if( pParse->rc==SQLITE_OK ){
1940 int nChild = 0; /* Number of children of returned node */
1941 int nByte; /* Bytes of space to allocate for this node */
1943 assert( (eType!=FTS5_STRING && !pNear)
1944 || (eType==FTS5_STRING && !pLeft && !pRight)
1946 if( eType==FTS5_STRING && pNear==0 ) return 0;
1947 if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
1948 if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
1950 if( eType==FTS5_NOT ){
1951 nChild = 2;
1952 }else if( eType==FTS5_AND || eType==FTS5_OR ){
1953 nChild = 2;
1954 if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
1955 if( pRight->eType==eType ) nChild += pRight->nChild-1;
1958 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1);
1959 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
1961 if( pRet ){
1962 pRet->eType = eType;
1963 pRet->pNear = pNear;
1964 fts5ExprAssignXNext(pRet);
1965 if( eType==FTS5_STRING ){
1966 int iPhrase;
1967 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
1968 pNear->apPhrase[iPhrase]->pNode = pRet;
1969 if( pNear->apPhrase[iPhrase]->nTerm==0 ){
1970 pRet->xNext = 0;
1971 pRet->eType = FTS5_EOF;
1975 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL
1976 && (pNear->nPhrase!=1 || pNear->apPhrase[0]->nTerm>1)
1978 assert( pParse->rc==SQLITE_OK );
1979 pParse->rc = SQLITE_ERROR;
1980 assert( pParse->zErr==0 );
1981 pParse->zErr = sqlite3_mprintf(
1982 "fts5: %s queries are not supported (detail!=full)",
1983 pNear->nPhrase==1 ? "phrase": "NEAR"
1985 sqlite3_free(pRet);
1986 pRet = 0;
1989 }else{
1990 fts5ExprAddChildren(pRet, pLeft);
1991 fts5ExprAddChildren(pRet, pRight);
1996 if( pRet==0 ){
1997 assert( pParse->rc!=SQLITE_OK );
1998 sqlite3Fts5ParseNodeFree(pLeft);
1999 sqlite3Fts5ParseNodeFree(pRight);
2000 sqlite3Fts5ParseNearsetFree(pNear);
2002 return pRet;
2005 Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
2006 Fts5Parse *pParse, /* Parse context */
2007 Fts5ExprNode *pLeft, /* Left hand child expression */
2008 Fts5ExprNode *pRight /* Right hand child expression */
2010 Fts5ExprNode *pRet = 0;
2011 Fts5ExprNode *pPrev;
2013 if( pParse->rc ){
2014 sqlite3Fts5ParseNodeFree(pLeft);
2015 sqlite3Fts5ParseNodeFree(pRight);
2016 }else{
2018 assert( pLeft->eType==FTS5_STRING
2019 || pLeft->eType==FTS5_TERM
2020 || pLeft->eType==FTS5_EOF
2021 || pLeft->eType==FTS5_AND
2023 assert( pRight->eType==FTS5_STRING
2024 || pRight->eType==FTS5_TERM
2025 || pRight->eType==FTS5_EOF
2028 if( pLeft->eType==FTS5_AND ){
2029 pPrev = pLeft->apChild[pLeft->nChild-1];
2030 }else{
2031 pPrev = pLeft;
2033 assert( pPrev->eType==FTS5_STRING
2034 || pPrev->eType==FTS5_TERM
2035 || pPrev->eType==FTS5_EOF
2038 if( pRight->eType==FTS5_EOF ){
2039 assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
2040 sqlite3Fts5ParseNodeFree(pRight);
2041 pRet = pLeft;
2042 pParse->nPhrase--;
2044 else if( pPrev->eType==FTS5_EOF ){
2045 Fts5ExprPhrase **ap;
2047 if( pPrev==pLeft ){
2048 pRet = pRight;
2049 }else{
2050 pLeft->apChild[pLeft->nChild-1] = pRight;
2051 pRet = pLeft;
2054 ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
2055 assert( ap[0]==pPrev->pNear->apPhrase[0] );
2056 memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
2057 pParse->nPhrase--;
2059 sqlite3Fts5ParseNodeFree(pPrev);
2061 else{
2062 pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
2066 return pRet;
2069 static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
2070 int nByte = 0;
2071 Fts5ExprTerm *p;
2072 char *zQuoted;
2074 /* Determine the maximum amount of space required. */
2075 for(p=pTerm; p; p=p->pSynonym){
2076 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2;
2078 zQuoted = sqlite3_malloc(nByte);
2080 if( zQuoted ){
2081 int i = 0;
2082 for(p=pTerm; p; p=p->pSynonym){
2083 char *zIn = p->zTerm;
2084 zQuoted[i++] = '"';
2085 while( *zIn ){
2086 if( *zIn=='"' ) zQuoted[i++] = '"';
2087 zQuoted[i++] = *zIn++;
2089 zQuoted[i++] = '"';
2090 if( p->pSynonym ) zQuoted[i++] = '|';
2092 if( pTerm->bPrefix ){
2093 zQuoted[i++] = ' ';
2094 zQuoted[i++] = '*';
2096 zQuoted[i++] = '\0';
2098 return zQuoted;
2101 static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
2102 char *zNew;
2103 va_list ap;
2104 va_start(ap, zFmt);
2105 zNew = sqlite3_vmprintf(zFmt, ap);
2106 va_end(ap);
2107 if( zApp && zNew ){
2108 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
2109 sqlite3_free(zNew);
2110 zNew = zNew2;
2112 sqlite3_free(zApp);
2113 return zNew;
2117 ** Compose a tcl-readable representation of expression pExpr. Return a
2118 ** pointer to a buffer containing that representation. It is the
2119 ** responsibility of the caller to at some point free the buffer using
2120 ** sqlite3_free().
2122 static char *fts5ExprPrintTcl(
2123 Fts5Config *pConfig,
2124 const char *zNearsetCmd,
2125 Fts5ExprNode *pExpr
2127 char *zRet = 0;
2128 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
2129 Fts5ExprNearset *pNear = pExpr->pNear;
2130 int i;
2131 int iTerm;
2133 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
2134 if( zRet==0 ) return 0;
2135 if( pNear->pColset ){
2136 int *aiCol = pNear->pColset->aiCol;
2137 int nCol = pNear->pColset->nCol;
2138 if( nCol==1 ){
2139 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
2140 }else{
2141 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
2142 for(i=1; i<pNear->pColset->nCol; i++){
2143 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
2145 zRet = fts5PrintfAppend(zRet, "} ");
2147 if( zRet==0 ) return 0;
2150 if( pNear->nPhrase>1 ){
2151 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
2152 if( zRet==0 ) return 0;
2155 zRet = fts5PrintfAppend(zRet, "--");
2156 if( zRet==0 ) return 0;
2158 for(i=0; i<pNear->nPhrase; i++){
2159 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
2161 zRet = fts5PrintfAppend(zRet, " {");
2162 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
2163 char *zTerm = pPhrase->aTerm[iTerm].zTerm;
2164 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm);
2165 if( pPhrase->aTerm[iTerm].bPrefix ){
2166 zRet = fts5PrintfAppend(zRet, "*");
2170 if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
2171 if( zRet==0 ) return 0;
2174 }else{
2175 char const *zOp = 0;
2176 int i;
2177 switch( pExpr->eType ){
2178 case FTS5_AND: zOp = "AND"; break;
2179 case FTS5_NOT: zOp = "NOT"; break;
2180 default:
2181 assert( pExpr->eType==FTS5_OR );
2182 zOp = "OR";
2183 break;
2186 zRet = sqlite3_mprintf("%s", zOp);
2187 for(i=0; zRet && i<pExpr->nChild; i++){
2188 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
2189 if( !z ){
2190 sqlite3_free(zRet);
2191 zRet = 0;
2192 }else{
2193 zRet = fts5PrintfAppend(zRet, " [%z]", z);
2198 return zRet;
2201 static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
2202 char *zRet = 0;
2203 if( pExpr->eType==0 ){
2204 return sqlite3_mprintf("\"\"");
2205 }else
2206 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
2207 Fts5ExprNearset *pNear = pExpr->pNear;
2208 int i;
2209 int iTerm;
2211 if( pNear->pColset ){
2212 int iCol = pNear->pColset->aiCol[0];
2213 zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]);
2214 if( zRet==0 ) return 0;
2217 if( pNear->nPhrase>1 ){
2218 zRet = fts5PrintfAppend(zRet, "NEAR(");
2219 if( zRet==0 ) return 0;
2222 for(i=0; i<pNear->nPhrase; i++){
2223 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
2224 if( i!=0 ){
2225 zRet = fts5PrintfAppend(zRet, " ");
2226 if( zRet==0 ) return 0;
2228 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
2229 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
2230 if( zTerm ){
2231 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
2232 sqlite3_free(zTerm);
2234 if( zTerm==0 || zRet==0 ){
2235 sqlite3_free(zRet);
2236 return 0;
2241 if( pNear->nPhrase>1 ){
2242 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
2243 if( zRet==0 ) return 0;
2246 }else{
2247 char const *zOp = 0;
2248 int i;
2250 switch( pExpr->eType ){
2251 case FTS5_AND: zOp = " AND "; break;
2252 case FTS5_NOT: zOp = " NOT "; break;
2253 default:
2254 assert( pExpr->eType==FTS5_OR );
2255 zOp = " OR ";
2256 break;
2259 for(i=0; i<pExpr->nChild; i++){
2260 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
2261 if( z==0 ){
2262 sqlite3_free(zRet);
2263 zRet = 0;
2264 }else{
2265 int e = pExpr->apChild[i]->eType;
2266 int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
2267 zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
2268 (i==0 ? "" : zOp),
2269 (b?"(":""), z, (b?")":"")
2272 if( zRet==0 ) break;
2276 return zRet;
2280 ** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
2281 ** and fts5_expr_tcl() (bTcl!=0).
2283 static void fts5ExprFunction(
2284 sqlite3_context *pCtx, /* Function call context */
2285 int nArg, /* Number of args */
2286 sqlite3_value **apVal, /* Function arguments */
2287 int bTcl
2289 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
2290 sqlite3 *db = sqlite3_context_db_handle(pCtx);
2291 const char *zExpr = 0;
2292 char *zErr = 0;
2293 Fts5Expr *pExpr = 0;
2294 int rc;
2295 int i;
2297 const char **azConfig; /* Array of arguments for Fts5Config */
2298 const char *zNearsetCmd = "nearset";
2299 int nConfig; /* Size of azConfig[] */
2300 Fts5Config *pConfig = 0;
2301 int iArg = 1;
2303 if( nArg<1 ){
2304 zErr = sqlite3_mprintf("wrong number of arguments to function %s",
2305 bTcl ? "fts5_expr_tcl" : "fts5_expr"
2307 sqlite3_result_error(pCtx, zErr, -1);
2308 sqlite3_free(zErr);
2309 return;
2312 if( bTcl && nArg>1 ){
2313 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
2314 iArg = 2;
2317 nConfig = 3 + (nArg-iArg);
2318 azConfig = (const char**)sqlite3_malloc(sizeof(char*) * nConfig);
2319 if( azConfig==0 ){
2320 sqlite3_result_error_nomem(pCtx);
2321 return;
2323 azConfig[0] = 0;
2324 azConfig[1] = "main";
2325 azConfig[2] = "tbl";
2326 for(i=3; iArg<nArg; iArg++){
2327 azConfig[i++] = (const char*)sqlite3_value_text(apVal[iArg]);
2330 zExpr = (const char*)sqlite3_value_text(apVal[0]);
2332 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
2333 if( rc==SQLITE_OK ){
2334 rc = sqlite3Fts5ExprNew(pConfig, zExpr, &pExpr, &zErr);
2336 if( rc==SQLITE_OK ){
2337 char *zText;
2338 if( pExpr->pRoot->xNext==0 ){
2339 zText = sqlite3_mprintf("");
2340 }else if( bTcl ){
2341 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
2342 }else{
2343 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
2345 if( zText==0 ){
2346 rc = SQLITE_NOMEM;
2347 }else{
2348 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
2349 sqlite3_free(zText);
2353 if( rc!=SQLITE_OK ){
2354 if( zErr ){
2355 sqlite3_result_error(pCtx, zErr, -1);
2356 sqlite3_free(zErr);
2357 }else{
2358 sqlite3_result_error_code(pCtx, rc);
2361 sqlite3_free((void *)azConfig);
2362 sqlite3Fts5ConfigFree(pConfig);
2363 sqlite3Fts5ExprFree(pExpr);
2366 static void fts5ExprFunctionHr(
2367 sqlite3_context *pCtx, /* Function call context */
2368 int nArg, /* Number of args */
2369 sqlite3_value **apVal /* Function arguments */
2371 fts5ExprFunction(pCtx, nArg, apVal, 0);
2373 static void fts5ExprFunctionTcl(
2374 sqlite3_context *pCtx, /* Function call context */
2375 int nArg, /* Number of args */
2376 sqlite3_value **apVal /* Function arguments */
2378 fts5ExprFunction(pCtx, nArg, apVal, 1);
2382 ** The implementation of an SQLite user-defined-function that accepts a
2383 ** single integer as an argument. If the integer is an alpha-numeric
2384 ** unicode code point, 1 is returned. Otherwise 0.
2386 static void fts5ExprIsAlnum(
2387 sqlite3_context *pCtx, /* Function call context */
2388 int nArg, /* Number of args */
2389 sqlite3_value **apVal /* Function arguments */
2391 int iCode;
2392 if( nArg!=1 ){
2393 sqlite3_result_error(pCtx,
2394 "wrong number of arguments to function fts5_isalnum", -1
2396 return;
2398 iCode = sqlite3_value_int(apVal[0]);
2399 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeIsalnum(iCode));
2402 static void fts5ExprFold(
2403 sqlite3_context *pCtx, /* Function call context */
2404 int nArg, /* Number of args */
2405 sqlite3_value **apVal /* Function arguments */
2407 if( nArg!=1 && nArg!=2 ){
2408 sqlite3_result_error(pCtx,
2409 "wrong number of arguments to function fts5_fold", -1
2411 }else{
2412 int iCode;
2413 int bRemoveDiacritics = 0;
2414 iCode = sqlite3_value_int(apVal[0]);
2415 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
2416 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
2421 ** This is called during initialization to register the fts5_expr() scalar
2422 ** UDF with the SQLite handle passed as the only argument.
2424 int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
2425 struct Fts5ExprFunc {
2426 const char *z;
2427 void (*x)(sqlite3_context*,int,sqlite3_value**);
2428 } aFunc[] = {
2429 { "fts5_expr", fts5ExprFunctionHr },
2430 { "fts5_expr_tcl", fts5ExprFunctionTcl },
2431 { "fts5_isalnum", fts5ExprIsAlnum },
2432 { "fts5_fold", fts5ExprFold },
2434 int i;
2435 int rc = SQLITE_OK;
2436 void *pCtx = (void*)pGlobal;
2438 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
2439 struct Fts5ExprFunc *p = &aFunc[i];
2440 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
2443 /* Avoid a warning indicating that sqlite3Fts5ParserTrace() is unused */
2444 #ifndef NDEBUG
2445 (void)sqlite3Fts5ParserTrace;
2446 #endif
2448 return rc;
2452 ** Return the number of phrases in expression pExpr.
2454 int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
2455 return (pExpr ? pExpr->nPhrase : 0);
2459 ** Return the number of terms in the iPhrase'th phrase in pExpr.
2461 int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
2462 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
2463 return pExpr->apExprPhrase[iPhrase]->nTerm;
2467 ** This function is used to access the current position list for phrase
2468 ** iPhrase.
2470 int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
2471 int nRet;
2472 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
2473 Fts5ExprNode *pNode = pPhrase->pNode;
2474 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
2475 *pa = pPhrase->poslist.p;
2476 nRet = pPhrase->poslist.n;
2477 }else{
2478 *pa = 0;
2479 nRet = 0;
2481 return nRet;
2484 struct Fts5PoslistPopulator {
2485 Fts5PoslistWriter writer;
2486 int bOk; /* True if ok to populate */
2487 int bMiss;
2490 Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
2491 Fts5PoslistPopulator *pRet;
2492 pRet = sqlite3_malloc(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
2493 if( pRet ){
2494 int i;
2495 memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
2496 for(i=0; i<pExpr->nPhrase; i++){
2497 Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
2498 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
2499 assert( pExpr->apExprPhrase[i]->nTerm==1 );
2500 if( bLive &&
2501 (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
2503 pRet[i].bMiss = 1;
2504 }else{
2505 pBuf->n = 0;
2509 return pRet;
2512 struct Fts5ExprCtx {
2513 Fts5Expr *pExpr;
2514 Fts5PoslistPopulator *aPopulator;
2515 i64 iOff;
2517 typedef struct Fts5ExprCtx Fts5ExprCtx;
2520 ** TODO: Make this more efficient!
2522 static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
2523 int i;
2524 for(i=0; i<pColset->nCol; i++){
2525 if( pColset->aiCol[i]==iCol ) return 1;
2527 return 0;
2530 static int fts5ExprPopulatePoslistsCb(
2531 void *pCtx, /* Copy of 2nd argument to xTokenize() */
2532 int tflags, /* Mask of FTS5_TOKEN_* flags */
2533 const char *pToken, /* Pointer to buffer containing token */
2534 int nToken, /* Size of token in bytes */
2535 int iUnused1, /* Byte offset of token within input text */
2536 int iUnused2 /* Byte offset of end of token within input text */
2538 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
2539 Fts5Expr *pExpr = p->pExpr;
2540 int i;
2542 UNUSED_PARAM2(iUnused1, iUnused2);
2544 if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
2545 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
2546 for(i=0; i<pExpr->nPhrase; i++){
2547 Fts5ExprTerm *pTerm;
2548 if( p->aPopulator[i].bOk==0 ) continue;
2549 for(pTerm=&pExpr->apExprPhrase[i]->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
2550 int nTerm = (int)strlen(pTerm->zTerm);
2551 if( (nTerm==nToken || (nTerm<nToken && pTerm->bPrefix))
2552 && memcmp(pTerm->zTerm, pToken, nTerm)==0
2554 int rc = sqlite3Fts5PoslistWriterAppend(
2555 &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
2557 if( rc ) return rc;
2558 break;
2562 return SQLITE_OK;
2565 int sqlite3Fts5ExprPopulatePoslists(
2566 Fts5Config *pConfig,
2567 Fts5Expr *pExpr,
2568 Fts5PoslistPopulator *aPopulator,
2569 int iCol,
2570 const char *z, int n
2572 int i;
2573 Fts5ExprCtx sCtx;
2574 sCtx.pExpr = pExpr;
2575 sCtx.aPopulator = aPopulator;
2576 sCtx.iOff = (((i64)iCol) << 32) - 1;
2578 for(i=0; i<pExpr->nPhrase; i++){
2579 Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
2580 Fts5Colset *pColset = pNode->pNear->pColset;
2581 if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
2582 || aPopulator[i].bMiss
2584 aPopulator[i].bOk = 0;
2585 }else{
2586 aPopulator[i].bOk = 1;
2590 return sqlite3Fts5Tokenize(pConfig,
2591 FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
2595 static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
2596 if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
2597 pNode->pNear->apPhrase[0]->poslist.n = 0;
2598 }else{
2599 int i;
2600 for(i=0; i<pNode->nChild; i++){
2601 fts5ExprClearPoslists(pNode->apChild[i]);
2606 static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
2607 pNode->iRowid = iRowid;
2608 pNode->bEof = 0;
2609 switch( pNode->eType ){
2610 case FTS5_TERM:
2611 case FTS5_STRING:
2612 return (pNode->pNear->apPhrase[0]->poslist.n>0);
2614 case FTS5_AND: {
2615 int i;
2616 for(i=0; i<pNode->nChild; i++){
2617 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
2618 fts5ExprClearPoslists(pNode);
2619 return 0;
2622 break;
2625 case FTS5_OR: {
2626 int i;
2627 int bRet = 0;
2628 for(i=0; i<pNode->nChild; i++){
2629 if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
2630 bRet = 1;
2633 return bRet;
2636 default: {
2637 assert( pNode->eType==FTS5_NOT );
2638 if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
2639 || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
2641 fts5ExprClearPoslists(pNode);
2642 return 0;
2644 break;
2647 return 1;
2650 void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
2651 fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
2655 ** This function is only called for detail=columns tables.
2657 int sqlite3Fts5ExprPhraseCollist(
2658 Fts5Expr *pExpr,
2659 int iPhrase,
2660 const u8 **ppCollist,
2661 int *pnCollist
2663 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
2664 Fts5ExprNode *pNode = pPhrase->pNode;
2665 int rc = SQLITE_OK;
2667 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
2668 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
2670 if( pNode->bEof==0
2671 && pNode->iRowid==pExpr->pRoot->iRowid
2672 && pPhrase->poslist.n>0
2674 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
2675 if( pTerm->pSynonym ){
2676 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
2677 rc = fts5ExprSynonymList(
2678 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
2680 }else{
2681 *ppCollist = pPhrase->aTerm[0].pIter->pData;
2682 *pnCollist = pPhrase->aTerm[0].pIter->nData;
2684 }else{
2685 *ppCollist = 0;
2686 *pnCollist = 0;
2689 return rc;