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"
21 ** All token types in the generated fts5parse.h file are greater than 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
*);
37 void sqlite3Fts5ParserTrace(FILE*, char*);
45 int bDesc
; /* Iterate in descending rowid order */
46 int nPhrase
; /* Number of phrases in expression */
47 Fts5ExprPhrase
**apExprPhrase
; /* Pointers to phrase objects */
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)
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
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 */
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
, ...){
134 if( pParse
->rc
==SQLITE_OK
){
135 pParse
->zErr
= sqlite3_vmprintf(zFmt
, ap
);
136 pParse
->rc
= SQLITE_ERROR
;
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(
150 const char **pz
, /* IN/OUT: Pointer into buffer */
156 /* Skip past any whitespace */
157 while( fts5ExprIsspace(*z
) ) 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;
177 for(z2
=&z
[1]; 1; z2
++){
180 if( z2
[0]!='"' ) break;
183 sqlite3Fts5ParseError(pParse
, "unterminated string");
187 pToken
->n
= (z2
- z
);
193 if( sqlite3Fts5IsBareword(z
[0])==0 ){
194 sqlite3Fts5ParseError(pParse
, "fts5: syntax error near \"%.1s\"", z
);
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
;
207 *pz
= &pToken
->p
[pToken
->n
];
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 */
222 const char *z
= zExpr
;
223 int t
; /* Next token type */
229 memset(&sParse
, 0, sizeof(sParse
));
230 pEngine
= sqlite3Fts5ParserAlloc(fts5ParseAlloc
);
231 if( pEngine
==0 ){ return SQLITE_NOMEM
; }
232 sParse
.pConfig
= pConfig
;
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
));
244 sParse
.rc
= SQLITE_NOMEM
;
245 sqlite3Fts5ParseNodeFree(sParse
.pExpr
);
248 const int nByte
= sizeof(Fts5ExprNode
);
249 pNew
->pRoot
= (Fts5ExprNode
*)sqlite3Fts5MallocZero(&sParse
.rc
, nByte
);
251 pNew
->pRoot
->bEof
= 1;
254 pNew
->pRoot
= sParse
.pExpr
;
257 pNew
->pConfig
= pConfig
;
258 pNew
->apExprPhrase
= sParse
.apPhrase
;
259 pNew
->nPhrase
= sParse
.nPhrase
;
263 sqlite3Fts5ParseNodeFree(sParse
.pExpr
);
266 sqlite3_free(sParse
.apPhrase
);
267 *pzErr
= sParse
.zErr
;
272 ** Free the expression node object passed as the only argument.
274 void sqlite3Fts5ParseNodeFree(Fts5ExprNode
*p
){
277 for(i
=0; i
<p
->nChild
; i
++){
278 sqlite3Fts5ParseNodeFree(p
->apChild
[i
]);
280 sqlite3Fts5ParseNearsetFree(p
->pNear
);
286 ** Free the expression object passed as the only argument.
288 void sqlite3Fts5ExprFree(Fts5Expr
*p
){
290 sqlite3Fts5ParseNodeFree(p
->pRoot
);
291 sqlite3_free(p
->apExprPhrase
);
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
){
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
)) ){
317 if( pbEof
&& bRetValid
==0 ) *pbEof
= 1;
322 ** Argument pTerm must be a synonym iterator.
324 static int fts5ExprSynonymList(
327 Fts5Buffer
*pBuf
, /* Use this buffer for space if required */
330 Fts5PoslistReader aStatic
[4];
331 Fts5PoslistReader
*aIter
= aStatic
;
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;
343 int nByte
= sizeof(Fts5PoslistReader
) * nAlloc
* 2;
344 Fts5PoslistReader
*aNew
= (Fts5PoslistReader
*)sqlite3_malloc(nByte
);
347 goto synonym_poslist_out
;
349 memcpy(aNew
, aIter
, sizeof(Fts5PoslistReader
) * nIter
);
351 if( aIter
!=aStatic
) sqlite3_free(aIter
);
354 sqlite3Fts5PoslistReaderInit(pIter
->pData
, pIter
->nData
, &aIter
[nIter
]);
355 assert( aIter
[nIter
].bEof
==0 );
361 *pa
= (u8
*)aIter
[0].a
;
364 Fts5PoslistWriter writer
= {0};
366 fts5BufferZero(pBuf
);
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
);
391 if( aIter
!=aStatic
) sqlite3_free(aIter
);
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
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
;
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
];
435 if( pTerm
->pSynonym
){
436 Fts5Buffer buf
= {0, 0, 0};
437 rc
= fts5ExprSynonymList(pTerm
, pNode
->iRowid
, &buf
, &a
, &n
);
442 if( a
==buf
.p
) bFlag
= 1;
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
;
454 i64 iPos
= aIter
[0].iPos
;
457 for(i
=0; i
<pPhrase
->nTerm
; i
++){
458 Fts5PoslistReader
*pPos
= &aIter
[i
];
460 if( pPos
->iPos
!=iAdj
){
462 while( pPos
->iPos
<iAdj
){
463 if( sqlite3Fts5PoslistReaderNext(pPos
) ) goto ismatch_out
;
465 if( pPos
->iPos
>iAdj
) iPos
= pPos
->iPos
-i
;
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
;
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
);
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
));
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
;
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
);
559 memset(aStatic
, 0, sizeof(aStatic
));
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
);
578 a
[i
].pOut
= pPoslist
;
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
;
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
){
596 while( pPos
->iPos
<iMin
){
597 if( fts5LookaheadReaderNext(pPos
) ) goto ismatch_out
;
599 if( pPos
->iPos
>iMax
) iMax
= pPos
->iPos
;
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
);
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
;
621 if( fts5LookaheadReaderNext(&a
[iAdv
].reader
) ) goto ismatch_out
;
625 int bRet
= a
[0].pOut
->n
>0;
627 if( a
!=aStatic
) sqlite3_free(a
);
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 */
651 iRowid
= pIter
->iRowid
;
652 if( (bDesc
==0 && iLast
>iRowid
) || (bDesc
&& iLast
<iRowid
) ){
653 int rc
= sqlite3Fts5IterNextFrom(pIter
, iLast
);
654 if( rc
|| sqlite3Fts5IterEof(pIter
) ){
659 iRowid
= pIter
->iRowid
;
660 assert( (bDesc
==0 && iRowid
>=iLast
) || (bDesc
==1 && iRowid
<=iLast
) );
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 */
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
);
691 *piLast
= fts5ExprSynonymRowid(pTerm
, bDesc
, &bEof
);
697 static int fts5ExprNearTest(
699 Fts5Expr
*pExpr
, /* Expression that pNear is a part of */
700 Fts5ExprNode
*pNode
/* The "NEAR" node (FTS5_STRING) */
702 Fts5ExprNearset
*pNear
= pNode
->pNear
;
705 if( pExpr
->pConfig
->eDetail
!=FTS5_DETAIL_FULL
){
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
;
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
){
728 rc
= fts5ExprPhraseIsMatch(pNode
, pPhrase
, &bMatch
);
729 if( bMatch
==0 ) break;
731 Fts5IndexIter
*pIter
= pPhrase
->aTerm
[0].pIter
;
732 fts5BufferSet(&rc
, &pPhrase
->poslist
, pIter
->nData
, pIter
->pData
);
737 if( i
==pNear
->nPhrase
&& (i
==1 || fts5ExprNearIsMatch(pRc
, pNear
)) ){
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(
754 Fts5ExprNearset
*pNear
= pNode
->pNear
;
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
];
766 for(p
=pTerm
; p
&& rc
==SQLITE_OK
; p
=p
->pSynonym
){
768 sqlite3Fts5IterClose(p
->pIter
);
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),
778 assert( rc
==SQLITE_OK
|| p
->pIter
==0 );
779 if( p
->pIter
&& 0==sqlite3Fts5IterEof(p
->pIter
) ){
794 ** If pExpr is an ASC iterator, this function returns a value with the
799 ** Otherwise, if this is a DESC iterator, the opposite is returned:
803 static int fts5RowidCmp(
808 assert( pExpr
->bDesc
==0 || pExpr
->bDesc
==1 );
809 if( pExpr
->bDesc
==0 ){
810 if( iLhs
<iRhs
) return -1;
811 return (iLhs
> iRhs
);
813 if( iLhs
>iRhs
) return -1;
814 return (iLhs
< iRhs
);
818 static void fts5ExprSetEof(Fts5ExprNode
*pNode
){
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
;
831 for(i
=0; i
<pNear
->nPhrase
; i
++){
832 Fts5ExprPhrase
*pPhrase
= pNear
->apPhrase
[i
];
833 pPhrase
->poslist
.n
= 0;
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(
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
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
878 static int fts5ExprNodeTest_STRING(
879 Fts5Expr
*pExpr
, /* Expression pPhrase belongs to */
882 Fts5ExprNearset
*pNear
= pNode
->pNear
;
883 Fts5ExprPhrase
*pLeft
= pNear
->apPhrase
[0];
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);
903 iLast
= pLeft
->aTerm
[0].pIter
->iRowid
;
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;
916 if( fts5ExprSynonymAdvanceto(pTerm
, bDesc
, &iLast
, &rc
) ){
922 Fts5IndexIter
*pIter
= pPhrase
->aTerm
[j
].pIter
;
923 if( pIter
->iRowid
==iLast
|| pIter
->bEof
) continue;
925 if( fts5ExprAdvanceto(pIter
, bDesc
, &iLast
, &rc
, &pNode
->bEof
) ){
933 pNode
->iRowid
= iLast
;
934 pNode
->bNomatch
= ((0==fts5ExprNearTest(&rc
, pExpr
, pNode
)) && rc
==SQLITE_OK
);
935 assert( pNode
->bEof
==0 || pNode
->bNomatch
==0 );
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
947 static int fts5ExprNodeNext_STRING(
948 Fts5Expr
*pExpr
, /* Expression pPhrase belongs to */
949 Fts5ExprNode
*pNode
, /* FTS5_STRING or FTS5_TERM node */
953 Fts5ExprTerm
*pTerm
= &pNode
->pNear
->apPhrase
[0]->aTerm
[0];
957 if( pTerm
->pSynonym
){
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
;
970 || (bFromValid
&& ii
!=iFrom
&& (ii
>iFrom
)==pExpr
->bDesc
)
973 rc
= sqlite3Fts5IterNextFrom(p
->pIter
, iFrom
);
975 rc
= sqlite3Fts5IterNext(p
->pIter
);
977 if( rc
!=SQLITE_OK
) break;
978 if( sqlite3Fts5IterEof(p
->pIter
)==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
);
991 Fts5IndexIter
*pIter
= pTerm
->pIter
;
993 assert( Fts5NodeIsString(pNode
) );
995 rc
= sqlite3Fts5IterNextFrom(pIter
, iFrom
);
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
);
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
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);
1038 ** xNext() method for a node of type FTS5_TERM.
1040 static int fts5ExprNodeNext_TERM(
1042 Fts5ExprNode
*pNode
,
1047 Fts5IndexIter
*pIter
= pNode
->pNear
->apPhrase
[0]->aTerm
[0].pIter
;
1049 assert( pNode
->bEof
==0 );
1051 rc
= sqlite3Fts5IterNextFrom(pIter
, iFrom
);
1053 rc
= sqlite3Fts5IterNext(pIter
);
1055 if( rc
==SQLITE_OK
&& sqlite3Fts5IterEof(pIter
)==0 ){
1056 rc
= fts5ExprNodeTest_TERM(pExpr
, pNode
);
1059 pNode
->bNomatch
= 0;
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];
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) ){
1078 pNode
->iRowid
= pNext
->iRowid
;
1079 pNode
->bEof
= pNext
->bEof
;
1080 pNode
->bNomatch
= pNext
->bNomatch
;
1083 static int fts5ExprNodeNext_OR(
1085 Fts5ExprNode
*pNode
,
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 );
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
);
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 */
1117 i64 iLast
= pAnd
->iRowid
;
1121 assert( pAnd
->bEof
==0 );
1125 for(iChild
=0; iChild
<pAnd
->nChild
; iChild
++){
1126 Fts5ExprNode
*pChild
= pAnd
->apChild
[iChild
];
1127 int cmp
= fts5RowidCmp(pExpr
, iLast
, pChild
->iRowid
);
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 );
1140 fts5ExprSetEof(pAnd
);
1143 }else if( iLast
!=pChild
->iRowid
){
1145 iLast
= pChild
->iRowid
;
1148 if( pChild
->bNomatch
){
1152 }while( bMatch
==0 );
1154 if( pAnd
->bNomatch
&& pAnd
!=pExpr
->pRoot
){
1155 fts5ExprNodeZeroPoslist(pAnd
);
1157 pAnd
->iRowid
= iLast
;
1161 static int fts5ExprNodeNext_AND(
1163 Fts5ExprNode
*pNode
,
1167 int rc
= fts5ExprNodeNext(pExpr
, pNode
->apChild
[0], bFromValid
, iFrom
);
1168 if( rc
==SQLITE_OK
){
1169 rc
= fts5ExprNodeTest_AND(pExpr
, pNode
);
1174 static int fts5ExprNodeTest_NOT(
1175 Fts5Expr
*pExpr
, /* Expression pPhrase belongs to */
1176 Fts5ExprNode
*pNode
/* FTS5_NOT node to advance */
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
);
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
;
1197 fts5ExprNodeZeroPoslist(p2
);
1202 static int fts5ExprNodeNext_NOT(
1204 Fts5ExprNode
*pNode
,
1208 int rc
= fts5ExprNodeNext(pExpr
, pNode
->apChild
[0], bFromValid
, iFrom
);
1209 if( rc
==SQLITE_OK
){
1210 rc
= fts5ExprNodeTest_NOT(pExpr
, pNode
);
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 */
1225 if( pNode
->bEof
==0 ){
1226 switch( pNode
->eType
){
1229 rc
= fts5ExprNodeTest_STRING(pExpr
, pNode
);
1234 rc
= fts5ExprNodeTest_TERM(pExpr
, pNode
);
1239 rc
= fts5ExprNodeTest_AND(pExpr
, pNode
);
1244 fts5ExprNodeTest_OR(pExpr
, pNode
);
1248 default: assert( pNode
->eType
==FTS5_NOT
); {
1249 rc
= fts5ExprNodeTest_NOT(pExpr
, pNode
);
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
){
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 ){
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
){
1288 if( nEof
>0 ) fts5ExprSetEof(pNode
);
1292 if( pNode
->nChild
==nEof
) fts5ExprSetEof(pNode
);
1296 assert( pNode
->eType
==FTS5_NOT
);
1297 pNode
->bEof
= pNode
->apChild
[0]->bEof
;
1302 if( rc
==SQLITE_OK
){
1303 rc
= fts5ExprNodeTest(pExpr
, pNode
);
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
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 */
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);
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
){
1354 Fts5ExprNode
*pRoot
= p
->pRoot
;
1355 assert( pRoot
->bEof
==0 && pRoot
->bNomatch
==0 );
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 ){
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
){
1376 *pz
= sqlite3Fts5Strndup(&rc
, pToken
->p
, pToken
->n
);
1381 ** Free the phrase object passed as the only argument.
1383 static void fts5ExprPhraseFree(Fts5ExprPhrase
*pPhrase
){
1386 for(i
=0; i
<pPhrase
->nTerm
; i
++){
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]);
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
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
){
1425 int nByte
= sizeof(Fts5ExprNearset
) + SZALLOC
* sizeof(Fts5ExprPhrase
*);
1426 pRet
= sqlite3_malloc(nByte
);
1428 pParse
->rc
= SQLITE_NOMEM
;
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
);
1438 pParse
->rc
= SQLITE_NOMEM
;
1446 assert( pParse
->rc
!=SQLITE_OK
);
1447 sqlite3Fts5ParseNearsetFree(pNear
);
1448 sqlite3Fts5ParsePhraseFree(pPhrase
);
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
);
1458 }else if( pLast
->nTerm
==0 ){
1459 fts5ExprPhraseFree(pLast
);
1460 pParse
->apPhrase
[pParse
->nPhrase
-2] = pPhrase
;
1465 pRet
->apPhrase
[pRet
->nPhrase
++] = pPhrase
;
1470 typedef struct TokenCtx TokenCtx
;
1472 Fts5ExprPhrase
*pPhrase
;
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 */
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
) ){
1500 int nByte
= sizeof(Fts5ExprTerm
) + sizeof(Fts5Buffer
) + nToken
+1;
1501 pSyn
= (Fts5ExprTerm
*)sqlite3_malloc(nByte
);
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
;
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
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
);
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
){
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 );
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 */
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);
1590 sqlite3Fts5Dequote(z
);
1592 rc
= sqlite3Fts5Tokenize(pConfig
, flags
, z
, n
, &sCtx
, fts5ParseTokenize
);
1595 if( rc
|| (rc
= sCtx
.rc
) ){
1597 fts5ExprPhraseFree(sCtx
.pPhrase
);
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
);
1607 pParse
->rc
= SQLITE_NOMEM
;
1608 fts5ExprPhraseFree(sCtx
.pPhrase
);
1611 pParse
->apPhrase
= apNew
;
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(
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
;
1660 int nByte
= sizeof(Fts5Colset
) + (pColsetOrig
->nCol
-1) * sizeof(int);
1661 Fts5Colset
*pColset
= (Fts5Colset
*)sqlite3Fts5MallocZero(&rc
, nByte
);
1663 memcpy(pColset
, pColsetOrig
, nByte
);
1665 pNew
->pRoot
->pNear
->pColset
= pColset
;
1670 int i
; /* Used to iterate through phrase terms */
1671 for(i
=0; rc
==SQLITE_OK
&& i
<pOrig
->nTerm
; i
++){
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
),
1678 tflags
= FTS5_TOKEN_COLOCATED
;
1680 if( rc
==SQLITE_OK
){
1681 sCtx
.pPhrase
->aTerm
[i
].bPrefix
= pOrig
->aTerm
[i
].bPrefix
;
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
;
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
;
1704 pNew
->pRoot
->eType
= FTS5_STRING
;
1705 pNew
->pRoot
->xNext
= fts5ExprNodeNext_STRING
;
1708 sqlite3Fts5ExprFree(pNew
);
1709 fts5ExprPhraseFree(sCtx
.pPhrase
);
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(
1733 Fts5ExprNearset
*pNear
,
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
1748 nNear
= nNear
* 10 + (p
->p
[i
] - '0');
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
);
1779 pParse
->rc
= SQLITE_NOMEM
;
1781 int *aiCol
= pNew
->aiCol
;
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];
1791 pNew
->nCol
= nCol
+1;
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] );
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
){
1809 int nCol
= pParse
->pConfig
->nCol
;
1811 pRet
= (Fts5Colset
*)sqlite3Fts5MallocZero(&pParse
->rc
,
1812 sizeof(Fts5Colset
) + sizeof(int)*nCol
1817 for(i
=0; i
<nCol
; i
++){
1818 if( iOld
>=p
->nCol
|| p
->aiCol
[iOld
]!=i
){
1819 pRet
->aiCol
[pRet
->nCol
++] = i
;
1830 Fts5Colset
*sqlite3Fts5ParseColset(
1831 Fts5Parse
*pParse
, /* Store SQLITE_NOMEM here if required */
1832 Fts5Colset
*pColset
, /* Existing colset object */
1835 Fts5Colset
*pRet
= 0;
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
);
1849 pRet
= fts5ParseColset(pParse
, pColset
, iCol
);
1855 assert( pParse
->rc
!=SQLITE_OK
);
1856 sqlite3_free(pColset
);
1862 void sqlite3Fts5ParseSetColset(
1864 Fts5ExprNearset
*pNear
,
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
);
1877 pNear
->pColset
= pColset
;
1879 sqlite3_free(pColset
);
1883 static void fts5ExprAssignXNext(Fts5ExprNode
*pNode
){
1884 switch( pNode
->eType
){
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
;
1893 pNode
->xNext
= fts5ExprNodeNext_STRING
;
1899 pNode
->xNext
= fts5ExprNodeNext_OR
;
1904 pNode
->xNext
= fts5ExprNodeNext_AND
;
1908 default: assert( pNode
->eType
==FTS5_NOT
); {
1909 pNode
->xNext
= fts5ExprNodeNext_NOT
;
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
;
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
){
1952 }else if( eType
==FTS5_AND
|| eType
==FTS5_OR
){
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
);
1962 pRet
->eType
= eType
;
1963 pRet
->pNear
= pNear
;
1964 fts5ExprAssignXNext(pRet
);
1965 if( eType
==FTS5_STRING
){
1967 for(iPhrase
=0; iPhrase
<pNear
->nPhrase
; iPhrase
++){
1968 pNear
->apPhrase
[iPhrase
]->pNode
= pRet
;
1969 if( pNear
->apPhrase
[iPhrase
]->nTerm
==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"
1990 fts5ExprAddChildren(pRet
, pLeft
);
1991 fts5ExprAddChildren(pRet
, pRight
);
1997 assert( pParse
->rc
!=SQLITE_OK
);
1998 sqlite3Fts5ParseNodeFree(pLeft
);
1999 sqlite3Fts5ParseNodeFree(pRight
);
2000 sqlite3Fts5ParseNearsetFree(pNear
);
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
;
2014 sqlite3Fts5ParseNodeFree(pLeft
);
2015 sqlite3Fts5ParseNodeFree(pRight
);
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];
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
);
2044 else if( pPrev
->eType
==FTS5_EOF
){
2045 Fts5ExprPhrase
**ap
;
2050 pLeft
->apChild
[pLeft
->nChild
-1] = pRight
;
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
);
2059 sqlite3Fts5ParseNodeFree(pPrev
);
2062 pRet
= sqlite3Fts5ParseNode(pParse
, FTS5_AND
, pLeft
, pRight
, 0);
2069 static char *fts5ExprTermPrint(Fts5ExprTerm
*pTerm
){
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
);
2082 for(p
=pTerm
; p
; p
=p
->pSynonym
){
2083 char *zIn
= p
->zTerm
;
2086 if( *zIn
=='"' ) zQuoted
[i
++] = '"';
2087 zQuoted
[i
++] = *zIn
++;
2090 if( p
->pSynonym
) zQuoted
[i
++] = '|';
2092 if( pTerm
->bPrefix
){
2096 zQuoted
[i
++] = '\0';
2101 static char *fts5PrintfAppend(char *zApp
, const char *zFmt
, ...){
2105 zNew
= sqlite3_vmprintf(zFmt
, ap
);
2108 char *zNew2
= sqlite3_mprintf("%s%s", zApp
, 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
2122 static char *fts5ExprPrintTcl(
2123 Fts5Config
*pConfig
,
2124 const char *zNearsetCmd
,
2128 if( pExpr
->eType
==FTS5_STRING
|| pExpr
->eType
==FTS5_TERM
){
2129 Fts5ExprNearset
*pNear
= pExpr
->pNear
;
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
;
2139 zRet
= fts5PrintfAppend(zRet
, "-col %d ", aiCol
[0]);
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;
2175 char const *zOp
= 0;
2177 switch( pExpr
->eType
){
2178 case FTS5_AND
: zOp
= "AND"; break;
2179 case FTS5_NOT
: zOp
= "NOT"; break;
2181 assert( pExpr
->eType
==FTS5_OR
);
2186 zRet
= sqlite3_mprintf("%s", zOp
);
2187 for(i
=0; zRet
&& i
<pExpr
->nChild
; i
++){
2188 char *z
= fts5ExprPrintTcl(pConfig
, zNearsetCmd
, pExpr
->apChild
[i
]);
2193 zRet
= fts5PrintfAppend(zRet
, " [%z]", z
);
2201 static char *fts5ExprPrint(Fts5Config
*pConfig
, Fts5ExprNode
*pExpr
){
2203 if( pExpr
->eType
==0 ){
2204 return sqlite3_mprintf("\"\"");
2206 if( pExpr
->eType
==FTS5_STRING
|| pExpr
->eType
==FTS5_TERM
){
2207 Fts5ExprNearset
*pNear
= pExpr
->pNear
;
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
];
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
]);
2231 zRet
= fts5PrintfAppend(zRet
, "%s%s", iTerm
==0?"":" + ", zTerm
);
2232 sqlite3_free(zTerm
);
2234 if( zTerm
==0 || zRet
==0 ){
2241 if( pNear
->nPhrase
>1 ){
2242 zRet
= fts5PrintfAppend(zRet
, ", %d)", pNear
->nNear
);
2243 if( zRet
==0 ) return 0;
2247 char const *zOp
= 0;
2250 switch( pExpr
->eType
){
2251 case FTS5_AND
: zOp
= " AND "; break;
2252 case FTS5_NOT
: zOp
= " NOT "; break;
2254 assert( pExpr
->eType
==FTS5_OR
);
2259 for(i
=0; i
<pExpr
->nChild
; i
++){
2260 char *z
= fts5ExprPrint(pConfig
, pExpr
->apChild
[i
]);
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",
2269 (b
?"(":""), z
, (b
?")":"")
2272 if( zRet
==0 ) break;
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 */
2289 Fts5Global
*pGlobal
= (Fts5Global
*)sqlite3_user_data(pCtx
);
2290 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
2291 const char *zExpr
= 0;
2293 Fts5Expr
*pExpr
= 0;
2297 const char **azConfig
; /* Array of arguments for Fts5Config */
2298 const char *zNearsetCmd
= "nearset";
2299 int nConfig
; /* Size of azConfig[] */
2300 Fts5Config
*pConfig
= 0;
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);
2312 if( bTcl
&& nArg
>1 ){
2313 zNearsetCmd
= (const char*)sqlite3_value_text(apVal
[1]);
2317 nConfig
= 3 + (nArg
-iArg
);
2318 azConfig
= (const char**)sqlite3_malloc(sizeof(char*) * nConfig
);
2320 sqlite3_result_error_nomem(pCtx
);
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
){
2338 if( pExpr
->pRoot
->xNext
==0 ){
2339 zText
= sqlite3_mprintf("");
2341 zText
= fts5ExprPrintTcl(pConfig
, zNearsetCmd
, pExpr
->pRoot
);
2343 zText
= fts5ExprPrint(pConfig
, pExpr
->pRoot
);
2348 sqlite3_result_text(pCtx
, zText
, -1, SQLITE_TRANSIENT
);
2349 sqlite3_free(zText
);
2353 if( rc
!=SQLITE_OK
){
2355 sqlite3_result_error(pCtx
, zErr
, -1);
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 */
2393 sqlite3_result_error(pCtx
,
2394 "wrong number of arguments to function fts5_isalnum", -1
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
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
{
2427 void (*x
)(sqlite3_context
*,int,sqlite3_value
**);
2429 { "fts5_expr", fts5ExprFunctionHr
},
2430 { "fts5_expr_tcl", fts5ExprFunctionTcl
},
2431 { "fts5_isalnum", fts5ExprIsAlnum
},
2432 { "fts5_fold", fts5ExprFold
},
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 */
2445 (void)sqlite3Fts5ParserTrace
;
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
2470 int sqlite3Fts5ExprPoslist(Fts5Expr
*pExpr
, int iPhrase
, const u8
**pa
){
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
;
2484 struct Fts5PoslistPopulator
{
2485 Fts5PoslistWriter writer
;
2486 int bOk
; /* True if ok to populate */
2490 Fts5PoslistPopulator
*sqlite3Fts5ExprClearPoslists(Fts5Expr
*pExpr
, int bLive
){
2491 Fts5PoslistPopulator
*pRet
;
2492 pRet
= sqlite3_malloc(sizeof(Fts5PoslistPopulator
)*pExpr
->nPhrase
);
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 );
2501 (pBuf
->n
==0 || pNode
->iRowid
!=pExpr
->pRoot
->iRowid
|| pNode
->bEof
)
2512 struct Fts5ExprCtx
{
2514 Fts5PoslistPopulator
*aPopulator
;
2517 typedef struct Fts5ExprCtx Fts5ExprCtx
;
2520 ** TODO: Make this more efficient!
2522 static int fts5ExprColsetTest(Fts5Colset
*pColset
, int iCol
){
2524 for(i
=0; i
<pColset
->nCol
; i
++){
2525 if( pColset
->aiCol
[i
]==iCol
) return 1;
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
;
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
2565 int sqlite3Fts5ExprPopulatePoslists(
2566 Fts5Config
*pConfig
,
2568 Fts5PoslistPopulator
*aPopulator
,
2570 const char *z
, int n
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;
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;
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
;
2609 switch( pNode
->eType
){
2612 return (pNode
->pNear
->apPhrase
[0]->poslist
.n
>0);
2616 for(i
=0; i
<pNode
->nChild
; i
++){
2617 if( fts5ExprCheckPoslists(pNode
->apChild
[i
], iRowid
)==0 ){
2618 fts5ExprClearPoslists(pNode
);
2628 for(i
=0; i
<pNode
->nChild
; i
++){
2629 if( fts5ExprCheckPoslists(pNode
->apChild
[i
], iRowid
) ){
2637 assert( pNode
->eType
==FTS5_NOT
);
2638 if( 0==fts5ExprCheckPoslists(pNode
->apChild
[0], iRowid
)
2639 || 0!=fts5ExprCheckPoslists(pNode
->apChild
[1], iRowid
)
2641 fts5ExprClearPoslists(pNode
);
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(
2660 const u8
**ppCollist
,
2663 Fts5ExprPhrase
*pPhrase
= pExpr
->apExprPhrase
[iPhrase
];
2664 Fts5ExprNode
*pNode
= pPhrase
->pNode
;
2667 assert( iPhrase
>=0 && iPhrase
<pExpr
->nPhrase
);
2668 assert( pExpr
->pConfig
->eDetail
==FTS5_DETAIL_COLUMNS
);
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
2681 *ppCollist
= pPhrase
->aTerm
[0].pIter
->pData
;
2682 *pnCollist
= pPhrase
->aTerm
[0].pIter
->nData
;