4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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 ******************************************************************************
18 #include "fts5parse.h"
20 #ifndef SQLITE_FTS5_MAX_EXPR_DEPTH
21 # define SQLITE_FTS5_MAX_EXPR_DEPTH 256
25 ** All token types in the generated fts5parse.h file are greater than 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
*);
41 void sqlite3Fts5ParserTrace(FILE*, char*);
43 int sqlite3Fts5ParserFallback(int);
50 int bDesc
; /* Iterate in descending rowid order */
51 int nPhrase
; /* Number of phrases in expression */
52 Fts5ExprPhrase
**apExprPhrase
; /* Pointers to phrase objects */
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)
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.
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
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 */
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.
151 static void assert_expr_depth_ok(int rc
, Fts5ExprNode
*p
){
153 if( p
->eType
==FTS5_TERM
|| p
->eType
==FTS5_STRING
|| p
->eType
==0 ){
154 assert( p
->iHeight
==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 );
168 # define assert_expr_depth_ok(rc, p)
171 void sqlite3Fts5ParseError(Fts5Parse
*pParse
, const char *zFmt
, ...){
174 if( pParse
->rc
==SQLITE_OK
){
175 assert( pParse
->zErr
==0 );
176 pParse
->zErr
= sqlite3_vmprintf(zFmt
, ap
);
177 pParse
->rc
= SQLITE_ERROR
;
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(
191 const char **pz
, /* IN/OUT: Pointer into buffer */
197 /* Skip past any whitespace */
198 while( fts5ExprIsspace(*z
) ) 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;
219 for(z2
=&z
[1]; 1; z2
++){
222 if( z2
[0]!='"' ) break;
225 sqlite3Fts5ParseError(pParse
, "unterminated string");
229 pToken
->n
= (z2
- z
);
235 if( sqlite3Fts5IsBareword(z
[0])==0 ){
236 sqlite3Fts5ParseError(pParse
, "fts5: syntax error near \"%.1s\"", z
);
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
;
249 *pz
= &pToken
->p
[pToken
->n
];
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 */
260 const char *zExpr
, /* Expression text */
266 const char *z
= zExpr
;
267 int t
; /* Next token type */
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
;
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
);
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
));
303 sParse
.rc
= SQLITE_NOMEM
;
304 sqlite3Fts5ParseNodeFree(sParse
.pExpr
);
307 const int nByte
= sizeof(Fts5ExprNode
);
308 pNew
->pRoot
= (Fts5ExprNode
*)sqlite3Fts5MallocZero(&sParse
.rc
, nByte
);
310 pNew
->pRoot
->bEof
= 1;
313 pNew
->pRoot
= sParse
.pExpr
;
316 pNew
->pConfig
= pConfig
;
317 pNew
->apExprPhrase
= sParse
.apPhrase
;
318 pNew
->nPhrase
= sParse
.nPhrase
;
323 sqlite3Fts5ParseNodeFree(sParse
.pExpr
);
326 sqlite3_free(sParse
.apPhrase
);
328 *pzErr
= sParse
.zErr
;
330 sqlite3_free(sParse
.zErr
);
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
){
342 for(ii
=0; ii
<nByte
; ii
++){
343 if( (z
[ii
] & 0xC0)!=0x80 ) 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
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);
383 || zText
[i
]==aSpec
[0] || zText
[i
]==aSpec
[1] || zText
[i
]==aSpec
[2]
386 if( fts5ExprCountChar(&zText
[iFirst
], i
-iFirst
)>=3 ){
389 for(jj
=iFirst
; jj
<i
; jj
++){
390 zExpr
[iOut
++] = zText
[jj
];
391 if( zText
[jj
]=='"' ) zExpr
[iOut
++] = '"';
396 if( zText
[i
]==aSpec
[2] ){
398 if( zText
[i
-1]=='^' ) i
++;
399 while( i
<nText
&& zText
[i
]!=']' ) i
++;
407 if( pConfig
->eDetail
!=FTS5_DETAIL_FULL
){
409 if( pConfig
->eDetail
==FTS5_DETAIL_NONE
){
410 iCol
= pConfig
->nCol
;
414 rc
= sqlite3Fts5ExprNew(pConfig
, bAnd
, iCol
, zExpr
, pp
,pConfig
->pzErrmsg
);
425 ** Free the expression node object passed as the only argument.
427 void sqlite3Fts5ParseNodeFree(Fts5ExprNode
*p
){
430 for(i
=0; i
<p
->nChild
; i
++){
431 sqlite3Fts5ParseNodeFree(p
->apChild
[i
]);
433 sqlite3Fts5ParseNearsetFree(p
->pNear
);
439 ** Free the expression object passed as the only argument.
441 void sqlite3Fts5ExprFree(Fts5Expr
*p
){
443 sqlite3Fts5ParseNodeFree(p
->pRoot
);
444 sqlite3_free(p
->apExprPhrase
);
449 int sqlite3Fts5ExprAnd(Fts5Expr
**pp1
, Fts5Expr
*p2
){
451 memset(&sParse
, 0, sizeof(sParse
));
455 int nPhrase
= p1
->nPhrase
+ p2
->nPhrase
;
457 p1
->pRoot
= sqlite3Fts5ParseNode(&sParse
, FTS5_AND
, p1
->pRoot
, p2
->pRoot
,0);
460 if( sParse
.rc
==SQLITE_OK
){
461 Fts5ExprPhrase
**ap
= (Fts5ExprPhrase
**)sqlite3_realloc(
462 p1
->apExprPhrase
, nPhrase
* sizeof(Fts5ExprPhrase
*)
465 sParse
.rc
= SQLITE_NOMEM
;
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
);
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
){
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
)) ){
507 if( pbEof
&& bRetValid
==0 ) *pbEof
= 1;
512 ** Argument pTerm must be a synonym iterator.
514 static int fts5ExprSynonymList(
517 Fts5Buffer
*pBuf
, /* Use this buffer for space if required */
520 Fts5PoslistReader aStatic
[4];
521 Fts5PoslistReader
*aIter
= aStatic
;
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;
533 sqlite3_int64 nByte
= sizeof(Fts5PoslistReader
) * nAlloc
* 2;
534 Fts5PoslistReader
*aNew
= (Fts5PoslistReader
*)sqlite3_malloc64(nByte
);
537 goto synonym_poslist_out
;
539 memcpy(aNew
, aIter
, sizeof(Fts5PoslistReader
) * nIter
);
541 if( aIter
!=aStatic
) sqlite3_free(aIter
);
544 sqlite3Fts5PoslistReaderInit(pIter
->pData
, pIter
->nData
, &aIter
[nIter
]);
545 assert( aIter
[nIter
].bEof
==0 );
551 *pa
= (u8
*)aIter
[0].a
;
554 Fts5PoslistWriter writer
= {0};
556 fts5BufferZero(pBuf
);
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
);
581 if( aIter
!=aStatic
) sqlite3_free(aIter
);
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
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
;
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
];
626 if( pTerm
->pSynonym
){
627 Fts5Buffer buf
= {0, 0, 0};
628 rc
= fts5ExprSynonymList(pTerm
, pNode
->iRowid
, &buf
, &a
, &n
);
633 if( a
==buf
.p
) bFlag
= 1;
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
;
645 i64 iPos
= aIter
[0].iPos
;
648 for(i
=0; i
<pPhrase
->nTerm
; i
++){
649 Fts5PoslistReader
*pPos
= &aIter
[i
];
651 if( pPos
->iPos
!=iAdj
){
653 while( pPos
->iPos
<iAdj
){
654 if( sqlite3Fts5PoslistReaderNext(pPos
) ) goto ismatch_out
;
656 if( pPos
->iPos
>iAdj
) iPos
= pPos
->iPos
-i
;
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
;
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
);
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
));
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
;
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
);
752 memset(aStatic
, 0, sizeof(aStatic
));
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
);
771 a
[i
].pOut
= pPoslist
;
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
;
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
){
789 while( pPos
->iPos
<iMin
){
790 if( fts5LookaheadReaderNext(pPos
) ) goto ismatch_out
;
792 if( pPos
->iPos
>iMax
) iMax
= pPos
->iPos
;
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
);
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
;
814 if( fts5LookaheadReaderNext(&a
[iAdv
].reader
) ) goto ismatch_out
;
818 int bRet
= a
[0].pOut
->n
>0;
820 if( a
!=aStatic
) sqlite3_free(a
);
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 */
844 iRowid
= pIter
->iRowid
;
845 if( (bDesc
==0 && iLast
>iRowid
) || (bDesc
&& iLast
<iRowid
) ){
846 int rc
= sqlite3Fts5IterNextFrom(pIter
, iLast
);
847 if( rc
|| sqlite3Fts5IterEof(pIter
) ){
852 iRowid
= pIter
->iRowid
;
853 assert( (bDesc
==0 && iRowid
>=iLast
) || (bDesc
==1 && iRowid
<=iLast
) );
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 */
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
);
884 *piLast
= fts5ExprSynonymRowid(pTerm
, bDesc
, &bEof
);
890 static int fts5ExprNearTest(
892 Fts5Expr
*pExpr
, /* Expression that pNear is a part of */
893 Fts5ExprNode
*pNode
/* The "NEAR" node (FTS5_STRING) */
895 Fts5ExprNearset
*pNear
= pNode
->pNear
;
898 if( pExpr
->pConfig
->eDetail
!=FTS5_DETAIL_FULL
){
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
;
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
923 rc
= fts5ExprPhraseIsMatch(pNode
, pPhrase
, &bMatch
);
924 if( bMatch
==0 ) break;
926 Fts5IndexIter
*pIter
= pPhrase
->aTerm
[0].pIter
;
927 fts5BufferSet(&rc
, &pPhrase
->poslist
, pIter
->nData
, pIter
->pData
);
932 if( i
==pNear
->nPhrase
&& (i
==1 || fts5ExprNearIsMatch(pRc
, pNear
)) ){
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
949 static int fts5ExprNearInitAll(
953 Fts5ExprNearset
*pNear
= pNode
->pNear
;
956 assert( pNode
->bNomatch
==0 );
957 for(i
=0; i
<pNear
->nPhrase
; i
++){
958 Fts5ExprPhrase
*pPhrase
= pNear
->apPhrase
[i
];
959 if( pPhrase
->nTerm
==0 ){
964 for(j
=0; j
<pPhrase
->nTerm
; j
++){
965 Fts5ExprTerm
*pTerm
= &pPhrase
->aTerm
[j
];
969 for(p
=pTerm
; p
; p
=p
->pSynonym
){
972 sqlite3Fts5IterClose(p
->pIter
);
975 rc
= sqlite3Fts5IndexQuery(
976 pExpr
->pIndex
, p
->pTerm
, p
->nQueryTerm
,
977 (pTerm
->bPrefix
? FTS5INDEX_QUERY_PREFIX
: 0) |
978 (pExpr
->bDesc
? FTS5INDEX_QUERY_DESC
: 0),
982 assert( (rc
==SQLITE_OK
)==(p
->pIter
!=0) );
983 if( rc
!=SQLITE_OK
) return rc
;
984 if( 0==sqlite3Fts5IterEof(p
->pIter
) ){
1002 ** If pExpr is an ASC iterator, this function returns a value with the
1007 ** Otherwise, if this is a DESC iterator, the opposite is returned:
1011 static int fts5RowidCmp(
1016 assert( pExpr
->bDesc
==0 || pExpr
->bDesc
==1 );
1017 if( pExpr
->bDesc
==0 ){
1018 if( iLhs
<iRhs
) return -1;
1019 return (iLhs
> iRhs
);
1021 if( iLhs
>iRhs
) return -1;
1022 return (iLhs
< iRhs
);
1026 static void fts5ExprSetEof(Fts5ExprNode
*pNode
){
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
;
1039 for(i
=0; i
<pNear
->nPhrase
; i
++){
1040 Fts5ExprPhrase
*pPhrase
= pNear
->apPhrase
[i
];
1041 pPhrase
->poslist
.n
= 0;
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(
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
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
1086 static int fts5ExprNodeTest_STRING(
1087 Fts5Expr
*pExpr
, /* Expression pPhrase belongs to */
1090 Fts5ExprNearset
*pNear
= pNode
->pNear
;
1091 Fts5ExprPhrase
*pLeft
= pNear
->apPhrase
[0];
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);
1112 iLast
= pLeft
->aTerm
[0].pIter
->iRowid
;
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;
1125 if( fts5ExprSynonymAdvanceto(pTerm
, bDesc
, &iLast
, &rc
) ){
1126 pNode
->bNomatch
= 0;
1131 Fts5IndexIter
*pIter
= pPhrase
->aTerm
[j
].pIter
;
1132 if( pIter
->iRowid
==iLast
|| pIter
->bEof
) continue;
1134 if( fts5ExprAdvanceto(pIter
, bDesc
, &iLast
, &rc
, &pNode
->bEof
) ){
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 );
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
1156 static int fts5ExprNodeNext_STRING(
1157 Fts5Expr
*pExpr
, /* Expression pPhrase belongs to */
1158 Fts5ExprNode
*pNode
, /* FTS5_STRING or FTS5_TERM node */
1162 Fts5ExprTerm
*pTerm
= &pNode
->pNear
->apPhrase
[0]->aTerm
[0];
1165 pNode
->bNomatch
= 0;
1166 if( pTerm
->pSynonym
){
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
;
1179 || (bFromValid
&& ii
!=iFrom
&& (ii
>iFrom
)==pExpr
->bDesc
)
1182 rc
= sqlite3Fts5IterNextFrom(p
->pIter
, iFrom
);
1184 rc
= sqlite3Fts5IterNext(p
->pIter
);
1186 if( rc
!=SQLITE_OK
) break;
1187 if( sqlite3Fts5IterEof(p
->pIter
)==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
);
1200 Fts5IndexIter
*pIter
= pTerm
->pIter
;
1202 assert( Fts5NodeIsString(pNode
) );
1204 rc
= sqlite3Fts5IterNextFrom(pIter
, iFrom
);
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
);
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
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);
1247 ** xNext() method for a node of type FTS5_TERM.
1249 static int fts5ExprNodeNext_TERM(
1251 Fts5ExprNode
*pNode
,
1256 Fts5IndexIter
*pIter
= pNode
->pNear
->apPhrase
[0]->aTerm
[0].pIter
;
1258 assert( pNode
->bEof
==0 );
1260 rc
= sqlite3Fts5IterNextFrom(pIter
, iFrom
);
1262 rc
= sqlite3Fts5IterNext(pIter
);
1264 if( rc
==SQLITE_OK
&& sqlite3Fts5IterEof(pIter
)==0 ){
1265 rc
= fts5ExprNodeTest_TERM(pExpr
, pNode
);
1268 pNode
->bNomatch
= 0;
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];
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) ){
1287 pNode
->iRowid
= pNext
->iRowid
;
1288 pNode
->bEof
= pNext
->bEof
;
1289 pNode
->bNomatch
= pNext
->bNomatch
;
1292 static int fts5ExprNodeNext_OR(
1294 Fts5ExprNode
*pNode
,
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 );
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;
1317 fts5ExprNodeTest_OR(pExpr
, pNode
);
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 */
1329 i64 iLast
= pAnd
->iRowid
;
1333 assert( pAnd
->bEof
==0 );
1337 for(iChild
=0; iChild
<pAnd
->nChild
; iChild
++){
1338 Fts5ExprNode
*pChild
= pAnd
->apChild
[iChild
];
1339 int cmp
= fts5RowidCmp(pExpr
, iLast
, pChild
->iRowid
);
1341 /* Advance pChild until it points to iLast or laster */
1342 rc
= fts5ExprNodeNext(pExpr
, pChild
, 1, iLast
);
1343 if( rc
!=SQLITE_OK
){
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 );
1355 fts5ExprSetEof(pAnd
);
1358 }else if( iLast
!=pChild
->iRowid
){
1360 iLast
= pChild
->iRowid
;
1363 if( pChild
->bNomatch
){
1367 }while( bMatch
==0 );
1369 if( pAnd
->bNomatch
&& pAnd
!=pExpr
->pRoot
){
1370 fts5ExprNodeZeroPoslist(pAnd
);
1372 pAnd
->iRowid
= iLast
;
1376 static int fts5ExprNodeNext_AND(
1378 Fts5ExprNode
*pNode
,
1382 int rc
= fts5ExprNodeNext(pExpr
, pNode
->apChild
[0], bFromValid
, iFrom
);
1383 if( rc
==SQLITE_OK
){
1384 rc
= fts5ExprNodeTest_AND(pExpr
, pNode
);
1386 pNode
->bNomatch
= 0;
1391 static int fts5ExprNodeTest_NOT(
1392 Fts5Expr
*pExpr
, /* Expression pPhrase belongs to */
1393 Fts5ExprNode
*pNode
/* FTS5_NOT node to advance */
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
);
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
;
1414 fts5ExprNodeZeroPoslist(p2
);
1419 static int fts5ExprNodeNext_NOT(
1421 Fts5ExprNode
*pNode
,
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;
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 */
1445 if( pNode
->bEof
==0 ){
1446 switch( pNode
->eType
){
1449 rc
= fts5ExprNodeTest_STRING(pExpr
, pNode
);
1454 rc
= fts5ExprNodeTest_TERM(pExpr
, pNode
);
1459 rc
= fts5ExprNodeTest_AND(pExpr
, pNode
);
1464 fts5ExprNodeTest_OR(pExpr
, pNode
);
1468 default: assert( pNode
->eType
==FTS5_NOT
); {
1469 rc
= fts5ExprNodeTest_NOT(pExpr
, pNode
);
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
){
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 ){
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
){
1508 if( nEof
>0 ) fts5ExprSetEof(pNode
);
1512 if( pNode
->nChild
==nEof
) fts5ExprSetEof(pNode
);
1516 assert( pNode
->eType
==FTS5_NOT
);
1517 pNode
->bEof
= pNode
->apChild
[0]->bEof
;
1522 if( rc
==SQLITE_OK
){
1523 rc
= fts5ExprNodeTest(pExpr
, pNode
);
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
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 */
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. */
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);
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
){
1577 Fts5ExprNode
*pRoot
= p
->pRoot
;
1578 assert( pRoot
->bEof
==0 && pRoot
->bNomatch
==0 );
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 ){
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
){
1599 *pz
= sqlite3Fts5Strndup(&rc
, pToken
->p
, pToken
->n
);
1604 ** Free the phrase object passed as the only argument.
1606 static void fts5ExprPhraseFree(Fts5ExprPhrase
*pPhrase
){
1609 for(i
=0; i
<pPhrase
->nTerm
; i
++){
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]);
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
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
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
){
1658 sqlite3_int64 nByte
;
1659 nByte
= sizeof(Fts5ExprNearset
) + SZALLOC
* sizeof(Fts5ExprPhrase
*);
1660 pRet
= sqlite3_malloc64(nByte
);
1662 pParse
->rc
= SQLITE_NOMEM
;
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
);
1673 pParse
->rc
= SQLITE_NOMEM
;
1681 assert( pParse
->rc
!=SQLITE_OK
);
1682 sqlite3Fts5ParseNearsetFree(pNear
);
1683 sqlite3Fts5ParsePhraseFree(pPhrase
);
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
);
1696 }else if( pLast
->nTerm
==0 ){
1697 fts5ExprPhraseFree(pLast
);
1698 pParse
->apPhrase
[pParse
->nPhrase
-2] = pPhrase
;
1703 pRet
->apPhrase
[pRet
->nPhrase
++] = pPhrase
;
1708 typedef struct TokenCtx TokenCtx
;
1710 Fts5ExprPhrase
*pPhrase
;
1711 Fts5Config
*pConfig
;
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 */
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
) ){
1739 sqlite3_int64 nByte
= sizeof(Fts5ExprTerm
) + sizeof(Fts5Buffer
) + nToken
+1;
1740 pSyn
= (Fts5ExprTerm
*)sqlite3_malloc64(nByte
);
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
;
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
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
);
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
){
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 );
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
);
1820 pParse
->rc
= SQLITE_NOMEM
;
1821 return SQLITE_NOMEM
;
1823 pParse
->apPhrase
= apNew
;
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 */
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);
1852 sqlite3Fts5Dequote(z
);
1854 rc
= sqlite3Fts5Tokenize(pConfig
, flags
, z
, n
, &sCtx
, fts5ParseTokenize
);
1857 if( rc
|| (rc
= sCtx
.rc
) ){
1859 fts5ExprPhraseFree(sCtx
.pPhrase
);
1864 if( parseGrowPhraseArray(pParse
) ){
1865 fts5ExprPhraseFree(sCtx
.pPhrase
);
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(
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
){
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
;
1918 sqlite3_int64 nByte
;
1919 Fts5Colset
*pColset
;
1920 nByte
= sizeof(Fts5Colset
) + (pColsetOrig
->nCol
-1) * sizeof(int);
1921 pColset
= (Fts5Colset
*)sqlite3Fts5MallocZero(&rc
, nByte
);
1923 memcpy(pColset
, pColsetOrig
, (size_t)nByte
);
1925 pNew
->pRoot
->pNear
->pColset
= pColset
;
1929 if( rc
==SQLITE_OK
){
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
++){
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
;
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
;
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
;
1963 && pOrig
->aTerm
[0].pSynonym
==0
1964 && pOrig
->aTerm
[0].bFirst
==0
1966 pNew
->pRoot
->eType
= FTS5_TERM
;
1967 pNew
->pRoot
->xNext
= fts5ExprNodeNext_TERM
;
1969 pNew
->pRoot
->eType
= FTS5_STRING
;
1970 pNew
->pRoot
->xNext
= fts5ExprNodeNext_STRING
;
1973 sqlite3Fts5ExprFree(pNew
);
1974 fts5ExprPhraseFree(sCtx
.pPhrase
);
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(
1998 Fts5ExprNearset
*pNear
,
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
2013 nNear
= nNear
* 10 + (p
->p
[i
] - '0');
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
);
2044 pParse
->rc
= SQLITE_NOMEM
;
2046 int *aiCol
= pNew
->aiCol
;
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];
2056 pNew
->nCol
= nCol
+1;
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] );
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
){
2074 int nCol
= pParse
->pConfig
->nCol
;
2076 pRet
= (Fts5Colset
*)sqlite3Fts5MallocZero(&pParse
->rc
,
2077 sizeof(Fts5Colset
) + sizeof(int)*nCol
2082 for(i
=0; i
<nCol
; i
++){
2083 if( iOld
>=p
->nCol
|| p
->aiCol
[iOld
]!=i
){
2084 pRet
->aiCol
[pRet
->nCol
++] = i
;
2095 Fts5Colset
*sqlite3Fts5ParseColset(
2096 Fts5Parse
*pParse
, /* Store SQLITE_NOMEM here if required */
2097 Fts5Colset
*pColset
, /* Existing colset object */
2100 Fts5Colset
*pRet
= 0;
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
);
2114 pRet
= fts5ParseColset(pParse
, pColset
, iCol
);
2120 assert( pParse
->rc
!=SQLITE_OK
);
2121 sqlite3_free(pColset
);
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
){
2138 sqlite3_int64 nByte
= sizeof(Fts5Colset
) + (pOrig
->nCol
-1) * sizeof(int);
2139 pRet
= (Fts5Colset
*)sqlite3Fts5MallocZero(pRc
, nByte
);
2141 memcpy(pRet
, pOrig
, (size_t)nByte
);
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
];
2160 pColset
->aiCol
[iOut
++] = pMerge
->aiCol
[iMerge
];
2163 }else if( iDiff
>0 ){
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(
2180 Fts5ExprNode
*pNode
,
2181 Fts5Colset
*pColset
,
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
;
2197 }else if( *ppFree
){
2198 pNear
->pColset
= pColset
;
2201 pNear
->pColset
= fts5CloneColset(&pParse
->rc
, pColset
);
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(
2218 Fts5ExprNode
*pExpr
,
2221 Fts5Colset
*pFree
= pColset
;
2222 if( pParse
->pConfig
->eDetail
==FTS5_DETAIL_NONE
){
2223 sqlite3Fts5ParseError(pParse
,
2224 "fts5: column queries are not supported (detail=none)"
2227 fts5ParseSetColset(pParse
, pExpr
, pColset
, &pFree
);
2229 sqlite3_free(pFree
);
2232 static void fts5ExprAssignXNext(Fts5ExprNode
*pNode
){
2233 switch( pNode
->eType
){
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
;
2243 pNode
->xNext
= fts5ExprNodeNext_STRING
;
2249 pNode
->xNext
= fts5ExprNodeNext_OR
;
2254 pNode
->xNext
= fts5ExprNodeNext_AND
;
2258 default: assert( pNode
->eType
==FTS5_NOT
); {
2259 pNode
->xNext
= fts5ExprNodeNext_NOT
;
2265 static void fts5ExprAddChildren(Fts5ExprNode
*p
, Fts5ExprNode
*pSub
){
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
;
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:
2287 ** into an AND tree:
2289 ** abc AND def AND ghi
2291 static Fts5ExprNode
*fts5ParsePhraseToAnd(
2293 Fts5ExprNearset
*pNear
2295 int nTerm
= pNear
->apPhrase
[0]->nTerm
;
2300 assert( pNear
->nPhrase
==1 );
2301 assert( pParse
->bPhraseToAnd
);
2303 nByte
= sizeof(Fts5ExprNode
) + nTerm
*sizeof(Fts5ExprNode
*);
2304 pRet
= (Fts5ExprNode
*)sqlite3Fts5MallocZero(&pParse
->rc
, nByte
);
2306 pRet
->eType
= FTS5_AND
;
2307 pRet
->nChild
= nTerm
;
2309 fts5ExprAssignXNext(pRet
);
2311 for(ii
=0; ii
<nTerm
; ii
++){
2312 Fts5ExprPhrase
*pPhrase
= (Fts5ExprPhrase
*)sqlite3Fts5MallocZero(
2313 &pParse
->rc
, sizeof(Fts5ExprPhrase
)
2316 if( parseGrowPhraseArray(pParse
) ){
2317 fts5ExprPhraseFree(pPhrase
);
2319 Fts5ExprTerm
*p
= &pNear
->apPhrase
[0]->aTerm
[ii
];
2320 Fts5ExprTerm
*pTo
= &pPhrase
->aTerm
[0];
2321 pParse
->apPhrase
[pParse
->nPhrase
++] = pPhrase
;
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
)
2334 sqlite3Fts5ParseNodeFree(pRet
);
2337 sqlite3Fts5ParseNearsetFree(pNear
);
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
);
2374 if( eType
==FTS5_NOT
){
2376 }else if( eType
==FTS5_AND
|| eType
==FTS5_OR
){
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
);
2386 pRet
->eType
= eType
;
2387 pRet
->pNear
= pNear
;
2388 fts5ExprAssignXNext(pRet
);
2389 if( eType
==FTS5_STRING
){
2391 for(iPhrase
=0; iPhrase
<pNear
->nPhrase
; iPhrase
++){
2392 pNear
->apPhrase
[iPhrase
]->pNode
= pRet
;
2393 if( pNear
->apPhrase
[iPhrase
]->nTerm
==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
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"
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
2430 assert( pParse
->rc
!=SQLITE_OK
);
2431 sqlite3Fts5ParseNodeFree(pLeft
);
2432 sqlite3Fts5ParseNodeFree(pRight
);
2433 sqlite3Fts5ParseNearsetFree(pNear
);
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
;
2447 sqlite3Fts5ParseNodeFree(pLeft
);
2448 sqlite3Fts5ParseNodeFree(pRight
);
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];
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
);
2478 else if( pPrev
->eType
==FTS5_EOF
){
2479 Fts5ExprPhrase
**ap
;
2484 pLeft
->apChild
[pLeft
->nChild
-1] = pRight
;
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
);
2493 sqlite3Fts5ParseNodeFree(pPrev
);
2496 pRet
= sqlite3Fts5ParseNode(pParse
, FTS5_AND
, pLeft
, pRight
, 0);
2503 #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
2504 static char *fts5ExprTermPrint(Fts5ExprTerm
*pTerm
){
2505 sqlite3_int64 nByte
= 0;
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
);
2517 for(p
=pTerm
; p
; p
=p
->pSynonym
){
2518 char *zIn
= p
->pTerm
;
2519 char *zEnd
= &zIn
[p
->nQueryTerm
];
2522 if( *zIn
=='"' ) zQuoted
[i
++] = '"';
2523 zQuoted
[i
++] = *zIn
++;
2526 if( p
->pSynonym
) zQuoted
[i
++] = '|';
2528 if( pTerm
->bPrefix
){
2532 zQuoted
[i
++] = '\0';
2537 static char *fts5PrintfAppend(char *zApp
, const char *zFmt
, ...){
2541 zNew
= sqlite3_vmprintf(zFmt
, ap
);
2544 char *zNew2
= sqlite3_mprintf("%s%s", zApp
, 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
2558 static char *fts5ExprPrintTcl(
2559 Fts5Config
*pConfig
,
2560 const char *zNearsetCmd
,
2564 if( pExpr
->eType
==FTS5_STRING
|| pExpr
->eType
==FTS5_TERM
){
2565 Fts5ExprNearset
*pNear
= pExpr
->pNear
;
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
;
2575 zRet
= fts5PrintfAppend(zRet
, "-col %d ", aiCol
[0]);
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("{}");
2615 char const *zOp
= 0;
2617 switch( pExpr
->eType
){
2618 case FTS5_AND
: zOp
= "AND"; break;
2619 case FTS5_NOT
: zOp
= "NOT"; break;
2621 assert( pExpr
->eType
==FTS5_OR
);
2626 zRet
= sqlite3_mprintf("%s", zOp
);
2627 for(i
=0; zRet
&& i
<pExpr
->nChild
; i
++){
2628 char *z
= fts5ExprPrintTcl(pConfig
, zNearsetCmd
, pExpr
->apChild
[i
]);
2633 zRet
= fts5PrintfAppend(zRet
, " [%z]", z
);
2641 static char *fts5ExprPrint(Fts5Config
*pConfig
, Fts5ExprNode
*pExpr
){
2643 if( pExpr
->eType
==0 ){
2644 return sqlite3_mprintf("\"\"");
2646 if( pExpr
->eType
==FTS5_STRING
|| pExpr
->eType
==FTS5_TERM
){
2647 Fts5ExprNearset
*pNear
= pExpr
->pNear
;
2651 if( pNear
->pColset
){
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 ? "" : " "
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
];
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
]);
2680 zRet
= fts5PrintfAppend(zRet
, "%s%s", iTerm
==0?"":" + ", zTerm
);
2681 sqlite3_free(zTerm
);
2683 if( zTerm
==0 || zRet
==0 ){
2690 if( pNear
->nPhrase
>1 ){
2691 zRet
= fts5PrintfAppend(zRet
, ", %d)", pNear
->nNear
);
2692 if( zRet
==0 ) return 0;
2696 char const *zOp
= 0;
2699 switch( pExpr
->eType
){
2700 case FTS5_AND
: zOp
= " AND "; break;
2701 case FTS5_NOT
: zOp
= " NOT "; break;
2703 assert( pExpr
->eType
==FTS5_OR
);
2708 for(i
=0; i
<pExpr
->nChild
; i
++){
2709 char *z
= fts5ExprPrint(pConfig
, pExpr
->apChild
[i
]);
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",
2718 (b
?"(":""), z
, (b
?")":"")
2721 if( zRet
==0 ) break;
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 */
2738 Fts5Global
*pGlobal
= (Fts5Global
*)sqlite3_user_data(pCtx
);
2739 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
2740 const char *zExpr
= 0;
2742 Fts5Expr
*pExpr
= 0;
2746 const char **azConfig
; /* Array of arguments for Fts5Config */
2747 const char *zNearsetCmd
= "nearset";
2748 int nConfig
; /* Size of azConfig[] */
2749 Fts5Config
*pConfig
= 0;
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);
2761 if( bTcl
&& nArg
>1 ){
2762 zNearsetCmd
= (const char*)sqlite3_value_text(apVal
[1]);
2766 nConfig
= 3 + (nArg
-iArg
);
2767 azConfig
= (const char**)sqlite3_malloc64(sizeof(char*) * nConfig
);
2769 sqlite3_result_error_nomem(pCtx
);
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
){
2789 if( pExpr
->pRoot
->xNext
==0 ){
2790 zText
= sqlite3_mprintf("");
2792 zText
= fts5ExprPrintTcl(pConfig
, zNearsetCmd
, pExpr
->pRoot
);
2794 zText
= fts5ExprPrint(pConfig
, pExpr
->pRoot
);
2799 sqlite3_result_text(pCtx
, zText
, -1, SQLITE_TRANSIENT
);
2800 sqlite3_free(zText
);
2804 if( rc
!=SQLITE_OK
){
2806 sqlite3_result_error(pCtx
, zErr
, -1);
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 */
2845 sqlite3_result_error(pCtx
,
2846 "wrong number of arguments to function fts5_isalnum", -1
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
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
{
2885 void (*x
)(sqlite3_context
*,int,sqlite3_value
**);
2887 { "fts5_expr", fts5ExprFunctionHr
},
2888 { "fts5_expr_tcl", fts5ExprFunctionTcl
},
2889 { "fts5_isalnum", fts5ExprIsAlnum
},
2890 { "fts5_fold", fts5ExprFold
},
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);
2902 UNUSED_PARAM2(pGlobal
,db
);
2905 /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
2906 ** sqlite3Fts5ParserFallback() are unused */
2908 (void)sqlite3Fts5ParserTrace
;
2910 (void)sqlite3Fts5ParserFallback
;
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
2934 int sqlite3Fts5ExprPoslist(Fts5Expr
*pExpr
, int iPhrase
, const u8
**pa
){
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
;
2948 struct Fts5PoslistPopulator
{
2949 Fts5PoslistWriter writer
;
2950 int bOk
; /* True if ok to populate */
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
);
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 );
2974 (pBuf
->n
==0 || pNode
->iRowid
!=pExpr
->pRoot
->iRowid
|| pNode
->bEof
)
2985 struct Fts5ExprCtx
{
2987 Fts5PoslistPopulator
*aPopulator
;
2990 typedef struct Fts5ExprCtx Fts5ExprCtx
;
2993 ** TODO: Make this more efficient!
2995 static int fts5ExprColsetTest(Fts5Colset
*pColset
, int iCol
){
2997 for(i
=0; i
<pColset
->nCol
; i
++){
2998 if( pColset
->aiCol
[i
]==iCol
) return 1;
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
){
3010 for(ii
=0; ii
<nToken
&& pToken
[ii
]; 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
;
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
++){
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
3060 int sqlite3Fts5ExprPopulatePoslists(
3061 Fts5Config
*pConfig
,
3063 Fts5PoslistPopulator
*aPopulator
,
3065 const char *z
, int n
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;
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;
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
;
3104 switch( pNode
->eType
){
3107 return (pNode
->pNear
->apPhrase
[0]->poslist
.n
>0);
3111 for(i
=0; i
<pNode
->nChild
; i
++){
3112 if( fts5ExprCheckPoslists(pNode
->apChild
[i
], iRowid
)==0 ){
3113 fts5ExprClearPoslists(pNode
);
3123 for(i
=0; i
<pNode
->nChild
; i
++){
3124 if( fts5ExprCheckPoslists(pNode
->apChild
[i
], iRowid
) ){
3132 assert( pNode
->eType
==FTS5_NOT
);
3133 if( 0==fts5ExprCheckPoslists(pNode
->apChild
[0], iRowid
)
3134 || 0!=fts5ExprCheckPoslists(pNode
->apChild
[1], iRowid
)
3136 fts5ExprClearPoslists(pNode
);
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(
3155 const u8
**ppCollist
,
3158 Fts5ExprPhrase
*pPhrase
= pExpr
->apExprPhrase
[iPhrase
];
3159 Fts5ExprNode
*pNode
= pPhrase
->pNode
;
3162 assert( iPhrase
>=0 && iPhrase
<pExpr
->nPhrase
);
3163 assert( pExpr
->pConfig
->eDetail
==FTS5_DETAIL_COLUMNS
);
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
3176 *ppCollist
= pPhrase
->aTerm
[0].pIter
->pData
;
3177 *pnCollist
= pPhrase
->aTerm
[0].pIter
->nData
;
3188 ** Does the work of the fts5_api.xQueryToken() API method.
3190 int sqlite3Fts5ExprQueryToken(
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
;
3213 ** Does the work of the fts5_api.xInstToken() API method.
3215 int sqlite3Fts5ExprInstToken(
3225 Fts5ExprPhrase
*pPhrase
= 0;
3226 Fts5ExprTerm
*pTerm
= 0;
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
3243 *ppOut
= pTerm
->pTerm
;
3244 *pnOut
= pTerm
->nFullTerm
;
3251 ** Clear the token mappings for all Fts5IndexIter objects mannaged by
3252 ** the expression passed as the only argument.
3254 void sqlite3Fts5ExprClearTokens(Fts5Expr
*pExpr
){
3256 for(ii
=0; ii
<pExpr
->nPhrase
; ii
++){
3258 for(pT
=&pExpr
->apExprPhrase
[ii
]->aTerm
[0]; pT
; pT
=pT
->pSynonym
){
3259 sqlite3Fts5IndexIterClearTokendata(pT
->pIter
);