OLD | NEW |
| (Empty) |
1 /* | |
2 ** 2014 May 31 | |
3 ** | |
4 ** The author disclaims copyright to this source code. In place of | |
5 ** a legal notice, here is a blessing: | |
6 ** | |
7 ** May you do good and not evil. | |
8 ** May you find forgiveness for yourself and forgive others. | |
9 ** May you share freely, never taking more than you give. | |
10 ** | |
11 ****************************************************************************** | |
12 ** | |
13 */ | |
14 | |
15 | |
16 | |
17 #include "fts5Int.h" | |
18 #include "fts5parse.h" | |
19 | |
20 /* | |
21 ** All token types in the generated fts5parse.h file are greater than 0. | |
22 */ | |
23 #define FTS5_EOF 0 | |
24 | |
25 #define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) | |
26 | |
27 typedef struct Fts5ExprTerm Fts5ExprTerm; | |
28 | |
29 /* | |
30 ** Functions generated by lemon from fts5parse.y. | |
31 */ | |
32 void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64)); | |
33 void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*)); | |
34 void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*); | |
35 #ifndef NDEBUG | |
36 #include <stdio.h> | |
37 void sqlite3Fts5ParserTrace(FILE*, char*); | |
38 #endif | |
39 | |
40 | |
41 struct Fts5Expr { | |
42 Fts5Index *pIndex; | |
43 Fts5ExprNode *pRoot; | |
44 int bDesc; /* Iterate in descending rowid order */ | |
45 int nPhrase; /* Number of phrases in expression */ | |
46 Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */ | |
47 }; | |
48 | |
49 /* | |
50 ** eType: | |
51 ** Expression node type. Always one of: | |
52 ** | |
53 ** FTS5_AND (nChild, apChild valid) | |
54 ** FTS5_OR (nChild, apChild valid) | |
55 ** FTS5_NOT (nChild, apChild valid) | |
56 ** FTS5_STRING (pNear valid) | |
57 ** FTS5_TERM (pNear valid) | |
58 */ | |
59 struct Fts5ExprNode { | |
60 int eType; /* Node type */ | |
61 int bEof; /* True at EOF */ | |
62 int bNomatch; /* True if entry is not a match */ | |
63 | |
64 i64 iRowid; /* Current rowid */ | |
65 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */ | |
66 | |
67 /* Child nodes. For a NOT node, this array always contains 2 entries. For | |
68 ** AND or OR nodes, it contains 2 or more entries. */ | |
69 int nChild; /* Number of child nodes */ | |
70 Fts5ExprNode *apChild[1]; /* Array of child nodes */ | |
71 }; | |
72 | |
73 #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING) | |
74 | |
75 /* | |
76 ** An instance of the following structure represents a single search term | |
77 ** or term prefix. | |
78 */ | |
79 struct Fts5ExprTerm { | |
80 int bPrefix; /* True for a prefix term */ | |
81 char *zTerm; /* nul-terminated term */ | |
82 Fts5IndexIter *pIter; /* Iterator for this term */ | |
83 Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */ | |
84 }; | |
85 | |
86 /* | |
87 ** A phrase. One or more terms that must appear in a contiguous sequence | |
88 ** within a document for it to match. | |
89 */ | |
90 struct Fts5ExprPhrase { | |
91 Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */ | |
92 Fts5Buffer poslist; /* Current position list */ | |
93 int nTerm; /* Number of entries in aTerm[] */ | |
94 Fts5ExprTerm aTerm[1]; /* Terms that make up this phrase */ | |
95 }; | |
96 | |
97 /* | |
98 ** One or more phrases that must appear within a certain token distance of | |
99 ** each other within each matching document. | |
100 */ | |
101 struct Fts5ExprNearset { | |
102 int nNear; /* NEAR parameter */ | |
103 Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */ | |
104 int nPhrase; /* Number of entries in aPhrase[] array */ | |
105 Fts5ExprPhrase *apPhrase[1]; /* Array of phrase pointers */ | |
106 }; | |
107 | |
108 | |
109 /* | |
110 ** Parse context. | |
111 */ | |
112 struct Fts5Parse { | |
113 Fts5Config *pConfig; | |
114 char *zErr; | |
115 int rc; | |
116 int nPhrase; /* Size of apPhrase array */ | |
117 Fts5ExprPhrase **apPhrase; /* Array of all phrases */ | |
118 Fts5ExprNode *pExpr; /* Result of a successful parse */ | |
119 }; | |
120 | |
121 void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){ | |
122 va_list ap; | |
123 va_start(ap, zFmt); | |
124 if( pParse->rc==SQLITE_OK ){ | |
125 pParse->zErr = sqlite3_vmprintf(zFmt, ap); | |
126 pParse->rc = SQLITE_ERROR; | |
127 } | |
128 va_end(ap); | |
129 } | |
130 | |
131 static int fts5ExprIsspace(char t){ | |
132 return t==' ' || t=='\t' || t=='\n' || t=='\r'; | |
133 } | |
134 | |
135 /* | |
136 ** Read the first token from the nul-terminated string at *pz. | |
137 */ | |
138 static int fts5ExprGetToken( | |
139 Fts5Parse *pParse, | |
140 const char **pz, /* IN/OUT: Pointer into buffer */ | |
141 Fts5Token *pToken | |
142 ){ | |
143 const char *z = *pz; | |
144 int tok; | |
145 | |
146 /* Skip past any whitespace */ | |
147 while( fts5ExprIsspace(*z) ) z++; | |
148 | |
149 pToken->p = z; | |
150 pToken->n = 1; | |
151 switch( *z ){ | |
152 case '(': tok = FTS5_LP; break; | |
153 case ')': tok = FTS5_RP; break; | |
154 case '{': tok = FTS5_LCP; break; | |
155 case '}': tok = FTS5_RCP; break; | |
156 case ':': tok = FTS5_COLON; break; | |
157 case ',': tok = FTS5_COMMA; break; | |
158 case '+': tok = FTS5_PLUS; break; | |
159 case '*': tok = FTS5_STAR; break; | |
160 case '\0': tok = FTS5_EOF; break; | |
161 | |
162 case '"': { | |
163 const char *z2; | |
164 tok = FTS5_STRING; | |
165 | |
166 for(z2=&z[1]; 1; z2++){ | |
167 if( z2[0]=='"' ){ | |
168 z2++; | |
169 if( z2[0]!='"' ) break; | |
170 } | |
171 if( z2[0]=='\0' ){ | |
172 sqlite3Fts5ParseError(pParse, "unterminated string"); | |
173 return FTS5_EOF; | |
174 } | |
175 } | |
176 pToken->n = (z2 - z); | |
177 break; | |
178 } | |
179 | |
180 default: { | |
181 const char *z2; | |
182 if( sqlite3Fts5IsBareword(z[0])==0 ){ | |
183 sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z); | |
184 return FTS5_EOF; | |
185 } | |
186 tok = FTS5_STRING; | |
187 for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++); | |
188 pToken->n = (z2 - z); | |
189 if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR; | |
190 if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT; | |
191 if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND; | |
192 break; | |
193 } | |
194 } | |
195 | |
196 *pz = &pToken->p[pToken->n]; | |
197 return tok; | |
198 } | |
199 | |
200 static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc((int)t); } | |
201 static void fts5ParseFree(void *p){ sqlite3_free(p); } | |
202 | |
203 int sqlite3Fts5ExprNew( | |
204 Fts5Config *pConfig, /* FTS5 Configuration */ | |
205 const char *zExpr, /* Expression text */ | |
206 Fts5Expr **ppNew, | |
207 char **pzErr | |
208 ){ | |
209 Fts5Parse sParse; | |
210 Fts5Token token; | |
211 const char *z = zExpr; | |
212 int t; /* Next token type */ | |
213 void *pEngine; | |
214 Fts5Expr *pNew; | |
215 | |
216 *ppNew = 0; | |
217 *pzErr = 0; | |
218 memset(&sParse, 0, sizeof(sParse)); | |
219 pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc); | |
220 if( pEngine==0 ){ return SQLITE_NOMEM; } | |
221 sParse.pConfig = pConfig; | |
222 | |
223 do { | |
224 t = fts5ExprGetToken(&sParse, &z, &token); | |
225 sqlite3Fts5Parser(pEngine, t, token, &sParse); | |
226 }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF ); | |
227 sqlite3Fts5ParserFree(pEngine, fts5ParseFree); | |
228 | |
229 assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 ); | |
230 if( sParse.rc==SQLITE_OK ){ | |
231 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr)); | |
232 if( pNew==0 ){ | |
233 sParse.rc = SQLITE_NOMEM; | |
234 sqlite3Fts5ParseNodeFree(sParse.pExpr); | |
235 }else{ | |
236 pNew->pRoot = sParse.pExpr; | |
237 pNew->pIndex = 0; | |
238 pNew->apExprPhrase = sParse.apPhrase; | |
239 pNew->nPhrase = sParse.nPhrase; | |
240 sParse.apPhrase = 0; | |
241 } | |
242 } | |
243 | |
244 sqlite3_free(sParse.apPhrase); | |
245 *pzErr = sParse.zErr; | |
246 return sParse.rc; | |
247 } | |
248 | |
249 /* | |
250 ** Free the expression node object passed as the only argument. | |
251 */ | |
252 void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){ | |
253 if( p ){ | |
254 int i; | |
255 for(i=0; i<p->nChild; i++){ | |
256 sqlite3Fts5ParseNodeFree(p->apChild[i]); | |
257 } | |
258 sqlite3Fts5ParseNearsetFree(p->pNear); | |
259 sqlite3_free(p); | |
260 } | |
261 } | |
262 | |
263 /* | |
264 ** Free the expression object passed as the only argument. | |
265 */ | |
266 void sqlite3Fts5ExprFree(Fts5Expr *p){ | |
267 if( p ){ | |
268 sqlite3Fts5ParseNodeFree(p->pRoot); | |
269 sqlite3_free(p->apExprPhrase); | |
270 sqlite3_free(p); | |
271 } | |
272 } | |
273 | |
274 /* | |
275 ** Argument pTerm must be a synonym iterator. Return the current rowid | |
276 ** that it points to. | |
277 */ | |
278 static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){ | |
279 i64 iRet = 0; | |
280 int bRetValid = 0; | |
281 Fts5ExprTerm *p; | |
282 | |
283 assert( pTerm->pSynonym ); | |
284 assert( bDesc==0 || bDesc==1 ); | |
285 for(p=pTerm; p; p=p->pSynonym){ | |
286 if( 0==sqlite3Fts5IterEof(p->pIter) ){ | |
287 i64 iRowid = sqlite3Fts5IterRowid(p->pIter); | |
288 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){ | |
289 iRet = iRowid; | |
290 bRetValid = 1; | |
291 } | |
292 } | |
293 } | |
294 | |
295 if( pbEof && bRetValid==0 ) *pbEof = 1; | |
296 return iRet; | |
297 } | |
298 | |
299 /* | |
300 ** Argument pTerm must be a synonym iterator. | |
301 */ | |
302 static int fts5ExprSynonymPoslist( | |
303 Fts5ExprTerm *pTerm, | |
304 Fts5Colset *pColset, | |
305 i64 iRowid, | |
306 int *pbDel, /* OUT: Caller should sqlite3_free(*pa) */ | |
307 u8 **pa, int *pn | |
308 ){ | |
309 Fts5PoslistReader aStatic[4]; | |
310 Fts5PoslistReader *aIter = aStatic; | |
311 int nIter = 0; | |
312 int nAlloc = 4; | |
313 int rc = SQLITE_OK; | |
314 Fts5ExprTerm *p; | |
315 | |
316 assert( pTerm->pSynonym ); | |
317 for(p=pTerm; p; p=p->pSynonym){ | |
318 Fts5IndexIter *pIter = p->pIter; | |
319 if( sqlite3Fts5IterEof(pIter)==0 && sqlite3Fts5IterRowid(pIter)==iRowid ){ | |
320 const u8 *a; | |
321 int n; | |
322 i64 dummy; | |
323 rc = sqlite3Fts5IterPoslist(pIter, pColset, &a, &n, &dummy); | |
324 if( rc!=SQLITE_OK ) goto synonym_poslist_out; | |
325 if( nIter==nAlloc ){ | |
326 int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2; | |
327 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte); | |
328 if( aNew==0 ){ | |
329 rc = SQLITE_NOMEM; | |
330 goto synonym_poslist_out; | |
331 } | |
332 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter); | |
333 nAlloc = nAlloc*2; | |
334 if( aIter!=aStatic ) sqlite3_free(aIter); | |
335 aIter = aNew; | |
336 } | |
337 sqlite3Fts5PoslistReaderInit(a, n, &aIter[nIter]); | |
338 assert( aIter[nIter].bEof==0 ); | |
339 nIter++; | |
340 } | |
341 } | |
342 | |
343 assert( *pbDel==0 ); | |
344 if( nIter==1 ){ | |
345 *pa = (u8*)aIter[0].a; | |
346 *pn = aIter[0].n; | |
347 }else{ | |
348 Fts5PoslistWriter writer = {0}; | |
349 Fts5Buffer buf = {0,0,0}; | |
350 i64 iPrev = -1; | |
351 while( 1 ){ | |
352 int i; | |
353 i64 iMin = FTS5_LARGEST_INT64; | |
354 for(i=0; i<nIter; i++){ | |
355 if( aIter[i].bEof==0 ){ | |
356 if( aIter[i].iPos==iPrev ){ | |
357 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue; | |
358 } | |
359 if( aIter[i].iPos<iMin ){ | |
360 iMin = aIter[i].iPos; | |
361 } | |
362 } | |
363 } | |
364 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break; | |
365 rc = sqlite3Fts5PoslistWriterAppend(&buf, &writer, iMin); | |
366 iPrev = iMin; | |
367 } | |
368 if( rc ){ | |
369 sqlite3_free(buf.p); | |
370 }else{ | |
371 *pa = buf.p; | |
372 *pn = buf.n; | |
373 *pbDel = 1; | |
374 } | |
375 } | |
376 | |
377 synonym_poslist_out: | |
378 if( aIter!=aStatic ) sqlite3_free(aIter); | |
379 return rc; | |
380 } | |
381 | |
382 | |
383 /* | |
384 ** All individual term iterators in pPhrase are guaranteed to be valid and | |
385 ** pointing to the same rowid when this function is called. This function | |
386 ** checks if the current rowid really is a match, and if so populates | |
387 ** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch | |
388 ** is set to true if this is really a match, or false otherwise. | |
389 ** | |
390 ** SQLITE_OK is returned if an error occurs, or an SQLite error code | |
391 ** otherwise. It is not considered an error code if the current rowid is | |
392 ** not a match. | |
393 */ | |
394 static int fts5ExprPhraseIsMatch( | |
395 Fts5ExprNode *pNode, /* Node pPhrase belongs to */ | |
396 Fts5Colset *pColset, /* Restrict matches to these columns */ | |
397 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */ | |
398 int *pbMatch /* OUT: Set to true if really a match */ | |
399 ){ | |
400 Fts5PoslistWriter writer = {0}; | |
401 Fts5PoslistReader aStatic[4]; | |
402 Fts5PoslistReader *aIter = aStatic; | |
403 int i; | |
404 int rc = SQLITE_OK; | |
405 | |
406 fts5BufferZero(&pPhrase->poslist); | |
407 | |
408 /* If the aStatic[] array is not large enough, allocate a large array | |
409 ** using sqlite3_malloc(). This approach could be improved upon. */ | |
410 if( pPhrase->nTerm>(int)ArraySize(aStatic) ){ | |
411 int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm; | |
412 aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte); | |
413 if( !aIter ) return SQLITE_NOMEM; | |
414 } | |
415 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm); | |
416 | |
417 /* Initialize a term iterator for each term in the phrase */ | |
418 for(i=0; i<pPhrase->nTerm; i++){ | |
419 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; | |
420 i64 dummy; | |
421 int n = 0; | |
422 int bFlag = 0; | |
423 const u8 *a = 0; | |
424 if( pTerm->pSynonym ){ | |
425 rc = fts5ExprSynonymPoslist( | |
426 pTerm, pColset, pNode->iRowid, &bFlag, (u8**)&a, &n | |
427 ); | |
428 }else{ | |
429 rc = sqlite3Fts5IterPoslist(pTerm->pIter, pColset, &a, &n, &dummy); | |
430 } | |
431 if( rc!=SQLITE_OK ) goto ismatch_out; | |
432 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]); | |
433 aIter[i].bFlag = (u8)bFlag; | |
434 if( aIter[i].bEof ) goto ismatch_out; | |
435 } | |
436 | |
437 while( 1 ){ | |
438 int bMatch; | |
439 i64 iPos = aIter[0].iPos; | |
440 do { | |
441 bMatch = 1; | |
442 for(i=0; i<pPhrase->nTerm; i++){ | |
443 Fts5PoslistReader *pPos = &aIter[i]; | |
444 i64 iAdj = iPos + i; | |
445 if( pPos->iPos!=iAdj ){ | |
446 bMatch = 0; | |
447 while( pPos->iPos<iAdj ){ | |
448 if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out; | |
449 } | |
450 if( pPos->iPos>iAdj ) iPos = pPos->iPos-i; | |
451 } | |
452 } | |
453 }while( bMatch==0 ); | |
454 | |
455 /* Append position iPos to the output */ | |
456 rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos); | |
457 if( rc!=SQLITE_OK ) goto ismatch_out; | |
458 | |
459 for(i=0; i<pPhrase->nTerm; i++){ | |
460 if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out; | |
461 } | |
462 } | |
463 | |
464 ismatch_out: | |
465 *pbMatch = (pPhrase->poslist.n>0); | |
466 for(i=0; i<pPhrase->nTerm; i++){ | |
467 if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a); | |
468 } | |
469 if( aIter!=aStatic ) sqlite3_free(aIter); | |
470 return rc; | |
471 } | |
472 | |
473 typedef struct Fts5LookaheadReader Fts5LookaheadReader; | |
474 struct Fts5LookaheadReader { | |
475 const u8 *a; /* Buffer containing position list */ | |
476 int n; /* Size of buffer a[] in bytes */ | |
477 int i; /* Current offset in position list */ | |
478 i64 iPos; /* Current position */ | |
479 i64 iLookahead; /* Next position */ | |
480 }; | |
481 | |
482 #define FTS5_LOOKAHEAD_EOF (((i64)1) << 62) | |
483 | |
484 static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){ | |
485 p->iPos = p->iLookahead; | |
486 if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){ | |
487 p->iLookahead = FTS5_LOOKAHEAD_EOF; | |
488 } | |
489 return (p->iPos==FTS5_LOOKAHEAD_EOF); | |
490 } | |
491 | |
492 static int fts5LookaheadReaderInit( | |
493 const u8 *a, int n, /* Buffer to read position list from */ | |
494 Fts5LookaheadReader *p /* Iterator object to initialize */ | |
495 ){ | |
496 memset(p, 0, sizeof(Fts5LookaheadReader)); | |
497 p->a = a; | |
498 p->n = n; | |
499 fts5LookaheadReaderNext(p); | |
500 return fts5LookaheadReaderNext(p); | |
501 } | |
502 | |
503 #if 0 | |
504 static int fts5LookaheadReaderEof(Fts5LookaheadReader *p){ | |
505 return (p->iPos==FTS5_LOOKAHEAD_EOF); | |
506 } | |
507 #endif | |
508 | |
509 typedef struct Fts5NearTrimmer Fts5NearTrimmer; | |
510 struct Fts5NearTrimmer { | |
511 Fts5LookaheadReader reader; /* Input iterator */ | |
512 Fts5PoslistWriter writer; /* Writer context */ | |
513 Fts5Buffer *pOut; /* Output poslist */ | |
514 }; | |
515 | |
516 /* | |
517 ** The near-set object passed as the first argument contains more than | |
518 ** one phrase. All phrases currently point to the same row. The | |
519 ** Fts5ExprPhrase.poslist buffers are populated accordingly. This function | |
520 ** tests if the current row contains instances of each phrase sufficiently | |
521 ** close together to meet the NEAR constraint. Non-zero is returned if it | |
522 ** does, or zero otherwise. | |
523 ** | |
524 ** If in/out parameter (*pRc) is set to other than SQLITE_OK when this | |
525 ** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM) | |
526 ** occurs within this function (*pRc) is set accordingly before returning. | |
527 ** The return value is undefined in both these cases. | |
528 ** | |
529 ** If no error occurs and non-zero (a match) is returned, the position-list | |
530 ** of each phrase object is edited to contain only those entries that | |
531 ** meet the constraint before returning. | |
532 */ | |
533 static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){ | |
534 Fts5NearTrimmer aStatic[4]; | |
535 Fts5NearTrimmer *a = aStatic; | |
536 Fts5ExprPhrase **apPhrase = pNear->apPhrase; | |
537 | |
538 int i; | |
539 int rc = *pRc; | |
540 int bMatch; | |
541 | |
542 assert( pNear->nPhrase>1 ); | |
543 | |
544 /* If the aStatic[] array is not large enough, allocate a large array | |
545 ** using sqlite3_malloc(). This approach could be improved upon. */ | |
546 if( pNear->nPhrase>(int)ArraySize(aStatic) ){ | |
547 int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase; | |
548 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte); | |
549 }else{ | |
550 memset(aStatic, 0, sizeof(aStatic)); | |
551 } | |
552 if( rc!=SQLITE_OK ){ | |
553 *pRc = rc; | |
554 return 0; | |
555 } | |
556 | |
557 /* Initialize a lookahead iterator for each phrase. After passing the | |
558 ** buffer and buffer size to the lookaside-reader init function, zero | |
559 ** the phrase poslist buffer. The new poslist for the phrase (containing | |
560 ** the same entries as the original with some entries removed on account | |
561 ** of the NEAR constraint) is written over the original even as it is | |
562 ** being read. This is safe as the entries for the new poslist are a | |
563 ** subset of the old, so it is not possible for data yet to be read to | |
564 ** be overwritten. */ | |
565 for(i=0; i<pNear->nPhrase; i++){ | |
566 Fts5Buffer *pPoslist = &apPhrase[i]->poslist; | |
567 fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader); | |
568 pPoslist->n = 0; | |
569 a[i].pOut = pPoslist; | |
570 } | |
571 | |
572 while( 1 ){ | |
573 int iAdv; | |
574 i64 iMin; | |
575 i64 iMax; | |
576 | |
577 /* This block advances the phrase iterators until they point to a set of | |
578 ** entries that together comprise a match. */ | |
579 iMax = a[0].reader.iPos; | |
580 do { | |
581 bMatch = 1; | |
582 for(i=0; i<pNear->nPhrase; i++){ | |
583 Fts5LookaheadReader *pPos = &a[i].reader; | |
584 iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear; | |
585 if( pPos->iPos<iMin || pPos->iPos>iMax ){ | |
586 bMatch = 0; | |
587 while( pPos->iPos<iMin ){ | |
588 if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out; | |
589 } | |
590 if( pPos->iPos>iMax ) iMax = pPos->iPos; | |
591 } | |
592 } | |
593 }while( bMatch==0 ); | |
594 | |
595 /* Add an entry to each output position list */ | |
596 for(i=0; i<pNear->nPhrase; i++){ | |
597 i64 iPos = a[i].reader.iPos; | |
598 Fts5PoslistWriter *pWriter = &a[i].writer; | |
599 if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){ | |
600 sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos); | |
601 } | |
602 } | |
603 | |
604 iAdv = 0; | |
605 iMin = a[0].reader.iLookahead; | |
606 for(i=0; i<pNear->nPhrase; i++){ | |
607 if( a[i].reader.iLookahead < iMin ){ | |
608 iMin = a[i].reader.iLookahead; | |
609 iAdv = i; | |
610 } | |
611 } | |
612 if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out; | |
613 } | |
614 | |
615 ismatch_out: { | |
616 int bRet = a[0].pOut->n>0; | |
617 *pRc = rc; | |
618 if( a!=aStatic ) sqlite3_free(a); | |
619 return bRet; | |
620 } | |
621 } | |
622 | |
623 /* | |
624 ** Advance the first term iterator in the first phrase of pNear. Set output | |
625 ** variable *pbEof to true if it reaches EOF or if an error occurs. | |
626 ** | |
627 ** Return SQLITE_OK if successful, or an SQLite error code if an error | |
628 ** occurs. | |
629 */ | |
630 static int fts5ExprNearAdvanceFirst( | |
631 Fts5Expr *pExpr, /* Expression pPhrase belongs to */ | |
632 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */ | |
633 int bFromValid, | |
634 i64 iFrom | |
635 ){ | |
636 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0]; | |
637 int rc = SQLITE_OK; | |
638 | |
639 if( pTerm->pSynonym ){ | |
640 int bEof = 1; | |
641 Fts5ExprTerm *p; | |
642 | |
643 /* Find the firstest rowid any synonym points to. */ | |
644 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0); | |
645 | |
646 /* Advance each iterator that currently points to iRowid. Or, if iFrom | |
647 ** is valid - each iterator that points to a rowid before iFrom. */ | |
648 for(p=pTerm; p; p=p->pSynonym){ | |
649 if( sqlite3Fts5IterEof(p->pIter)==0 ){ | |
650 i64 ii = sqlite3Fts5IterRowid(p->pIter); | |
651 if( ii==iRowid | |
652 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc) | |
653 ){ | |
654 if( bFromValid ){ | |
655 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom); | |
656 }else{ | |
657 rc = sqlite3Fts5IterNext(p->pIter); | |
658 } | |
659 if( rc!=SQLITE_OK ) break; | |
660 if( sqlite3Fts5IterEof(p->pIter)==0 ){ | |
661 bEof = 0; | |
662 } | |
663 }else{ | |
664 bEof = 0; | |
665 } | |
666 } | |
667 } | |
668 | |
669 /* Set the EOF flag if either all synonym iterators are at EOF or an | |
670 ** error has occurred. */ | |
671 pNode->bEof = (rc || bEof); | |
672 }else{ | |
673 Fts5IndexIter *pIter = pTerm->pIter; | |
674 | |
675 assert( Fts5NodeIsString(pNode) ); | |
676 if( bFromValid ){ | |
677 rc = sqlite3Fts5IterNextFrom(pIter, iFrom); | |
678 }else{ | |
679 rc = sqlite3Fts5IterNext(pIter); | |
680 } | |
681 | |
682 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter)); | |
683 } | |
684 | |
685 return rc; | |
686 } | |
687 | |
688 /* | |
689 ** Advance iterator pIter until it points to a value equal to or laster | |
690 ** than the initial value of *piLast. If this means the iterator points | |
691 ** to a value laster than *piLast, update *piLast to the new lastest value. | |
692 ** | |
693 ** If the iterator reaches EOF, set *pbEof to true before returning. If | |
694 ** an error occurs, set *pRc to an error code. If either *pbEof or *pRc | |
695 ** are set, return a non-zero value. Otherwise, return zero. | |
696 */ | |
697 static int fts5ExprAdvanceto( | |
698 Fts5IndexIter *pIter, /* Iterator to advance */ | |
699 int bDesc, /* True if iterator is "rowid DESC" */ | |
700 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */ | |
701 int *pRc, /* OUT: Error code */ | |
702 int *pbEof /* OUT: Set to true if EOF */ | |
703 ){ | |
704 i64 iLast = *piLast; | |
705 i64 iRowid; | |
706 | |
707 iRowid = sqlite3Fts5IterRowid(pIter); | |
708 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){ | |
709 int rc = sqlite3Fts5IterNextFrom(pIter, iLast); | |
710 if( rc || sqlite3Fts5IterEof(pIter) ){ | |
711 *pRc = rc; | |
712 *pbEof = 1; | |
713 return 1; | |
714 } | |
715 iRowid = sqlite3Fts5IterRowid(pIter); | |
716 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) ); | |
717 } | |
718 *piLast = iRowid; | |
719 | |
720 return 0; | |
721 } | |
722 | |
723 static int fts5ExprSynonymAdvanceto( | |
724 Fts5ExprTerm *pTerm, /* Term iterator to advance */ | |
725 int bDesc, /* True if iterator is "rowid DESC" */ | |
726 i64 *piLast, /* IN/OUT: Lastest rowid seen so far */ | |
727 int *pRc /* OUT: Error code */ | |
728 ){ | |
729 int rc = SQLITE_OK; | |
730 i64 iLast = *piLast; | |
731 Fts5ExprTerm *p; | |
732 int bEof = 0; | |
733 | |
734 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){ | |
735 if( sqlite3Fts5IterEof(p->pIter)==0 ){ | |
736 i64 iRowid = sqlite3Fts5IterRowid(p->pIter); | |
737 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){ | |
738 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast); | |
739 } | |
740 } | |
741 } | |
742 | |
743 if( rc!=SQLITE_OK ){ | |
744 *pRc = rc; | |
745 bEof = 1; | |
746 }else{ | |
747 *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof); | |
748 } | |
749 return bEof; | |
750 } | |
751 | |
752 | |
753 static int fts5ExprNearTest( | |
754 int *pRc, | |
755 Fts5Expr *pExpr, /* Expression that pNear is a part of */ | |
756 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */ | |
757 ){ | |
758 Fts5ExprNearset *pNear = pNode->pNear; | |
759 int rc = *pRc; | |
760 int i; | |
761 | |
762 /* Check that each phrase in the nearset matches the current row. | |
763 ** Populate the pPhrase->poslist buffers at the same time. If any | |
764 ** phrase is not a match, break out of the loop early. */ | |
765 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){ | |
766 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; | |
767 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){ | |
768 int bMatch = 0; | |
769 rc = fts5ExprPhraseIsMatch(pNode, pNear->pColset, pPhrase, &bMatch); | |
770 if( bMatch==0 ) break; | |
771 }else{ | |
772 rc = sqlite3Fts5IterPoslistBuffer( | |
773 pPhrase->aTerm[0].pIter, &pPhrase->poslist | |
774 ); | |
775 } | |
776 } | |
777 | |
778 *pRc = rc; | |
779 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){ | |
780 return 1; | |
781 } | |
782 | |
783 return 0; | |
784 } | |
785 | |
786 static int fts5ExprTokenTest( | |
787 Fts5Expr *pExpr, /* Expression that pNear is a part of */ | |
788 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */ | |
789 ){ | |
790 /* As this "NEAR" object is actually a single phrase that consists | |
791 ** of a single term only, grab pointers into the poslist managed by the | |
792 ** fts5_index.c iterator object. This is much faster than synthesizing | |
793 ** a new poslist the way we have to for more complicated phrase or NEAR | |
794 ** expressions. */ | |
795 Fts5ExprNearset *pNear = pNode->pNear; | |
796 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0]; | |
797 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter; | |
798 Fts5Colset *pColset = pNear->pColset; | |
799 int rc; | |
800 | |
801 assert( pNode->eType==FTS5_TERM ); | |
802 assert( pNear->nPhrase==1 && pPhrase->nTerm==1 ); | |
803 assert( pPhrase->aTerm[0].pSynonym==0 ); | |
804 | |
805 rc = sqlite3Fts5IterPoslist(pIter, pColset, | |
806 (const u8**)&pPhrase->poslist.p, &pPhrase->poslist.n, &pNode->iRowid | |
807 ); | |
808 pNode->bNomatch = (pPhrase->poslist.n==0); | |
809 return rc; | |
810 } | |
811 | |
812 /* | |
813 ** All individual term iterators in pNear are guaranteed to be valid when | |
814 ** this function is called. This function checks if all term iterators | |
815 ** point to the same rowid, and if not, advances them until they do. | |
816 ** If an EOF is reached before this happens, *pbEof is set to true before | |
817 ** returning. | |
818 ** | |
819 ** SQLITE_OK is returned if an error occurs, or an SQLite error code | |
820 ** otherwise. It is not considered an error code if an iterator reaches | |
821 ** EOF. | |
822 */ | |
823 static int fts5ExprNearNextMatch( | |
824 Fts5Expr *pExpr, /* Expression pPhrase belongs to */ | |
825 Fts5ExprNode *pNode | |
826 ){ | |
827 Fts5ExprNearset *pNear = pNode->pNear; | |
828 Fts5ExprPhrase *pLeft = pNear->apPhrase[0]; | |
829 int rc = SQLITE_OK; | |
830 i64 iLast; /* Lastest rowid any iterator points to */ | |
831 int i, j; /* Phrase and token index, respectively */ | |
832 int bMatch; /* True if all terms are at the same rowid */ | |
833 const int bDesc = pExpr->bDesc; | |
834 | |
835 /* Check that this node should not be FTS5_TERM */ | |
836 assert( pNear->nPhrase>1 | |
837 || pNear->apPhrase[0]->nTerm>1 | |
838 || pNear->apPhrase[0]->aTerm[0].pSynonym | |
839 ); | |
840 | |
841 /* Initialize iLast, the "lastest" rowid any iterator points to. If the | |
842 ** iterator skips through rowids in the default ascending order, this means | |
843 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it | |
844 ** means the minimum rowid. */ | |
845 if( pLeft->aTerm[0].pSynonym ){ | |
846 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0); | |
847 }else{ | |
848 iLast = sqlite3Fts5IterRowid(pLeft->aTerm[0].pIter); | |
849 } | |
850 | |
851 do { | |
852 bMatch = 1; | |
853 for(i=0; i<pNear->nPhrase; i++){ | |
854 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; | |
855 for(j=0; j<pPhrase->nTerm; j++){ | |
856 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; | |
857 if( pTerm->pSynonym ){ | |
858 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0); | |
859 if( iRowid==iLast ) continue; | |
860 bMatch = 0; | |
861 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){ | |
862 pNode->bEof = 1; | |
863 return rc; | |
864 } | |
865 }else{ | |
866 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter; | |
867 i64 iRowid = sqlite3Fts5IterRowid(pIter); | |
868 if( iRowid==iLast ) continue; | |
869 bMatch = 0; | |
870 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){ | |
871 return rc; | |
872 } | |
873 } | |
874 } | |
875 } | |
876 }while( bMatch==0 ); | |
877 | |
878 pNode->iRowid = iLast; | |
879 pNode->bNomatch = (0==fts5ExprNearTest(&rc, pExpr, pNode)); | |
880 | |
881 return rc; | |
882 } | |
883 | |
884 /* | |
885 ** Initialize all term iterators in the pNear object. If any term is found | |
886 ** to match no documents at all, return immediately without initializing any | |
887 ** further iterators. | |
888 */ | |
889 static int fts5ExprNearInitAll( | |
890 Fts5Expr *pExpr, | |
891 Fts5ExprNode *pNode | |
892 ){ | |
893 Fts5ExprNearset *pNear = pNode->pNear; | |
894 int i, j; | |
895 int rc = SQLITE_OK; | |
896 | |
897 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){ | |
898 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; | |
899 for(j=0; j<pPhrase->nTerm; j++){ | |
900 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; | |
901 Fts5ExprTerm *p; | |
902 int bEof = 1; | |
903 | |
904 for(p=pTerm; p && rc==SQLITE_OK; p=p->pSynonym){ | |
905 if( p->pIter ){ | |
906 sqlite3Fts5IterClose(p->pIter); | |
907 p->pIter = 0; | |
908 } | |
909 rc = sqlite3Fts5IndexQuery( | |
910 pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm), | |
911 (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) | | |
912 (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0), | |
913 pNear->pColset, | |
914 &p->pIter | |
915 ); | |
916 assert( rc==SQLITE_OK || p->pIter==0 ); | |
917 if( p->pIter && 0==sqlite3Fts5IterEof(p->pIter) ){ | |
918 bEof = 0; | |
919 } | |
920 } | |
921 | |
922 if( bEof ){ | |
923 pNode->bEof = 1; | |
924 return rc; | |
925 } | |
926 } | |
927 } | |
928 | |
929 return rc; | |
930 } | |
931 | |
932 /* fts5ExprNodeNext() calls fts5ExprNodeNextMatch(). And vice-versa. */ | |
933 static int fts5ExprNodeNextMatch(Fts5Expr*, Fts5ExprNode*); | |
934 | |
935 | |
936 /* | |
937 ** If pExpr is an ASC iterator, this function returns a value with the | |
938 ** same sign as: | |
939 ** | |
940 ** (iLhs - iRhs) | |
941 ** | |
942 ** Otherwise, if this is a DESC iterator, the opposite is returned: | |
943 ** | |
944 ** (iRhs - iLhs) | |
945 */ | |
946 static int fts5RowidCmp( | |
947 Fts5Expr *pExpr, | |
948 i64 iLhs, | |
949 i64 iRhs | |
950 ){ | |
951 assert( pExpr->bDesc==0 || pExpr->bDesc==1 ); | |
952 if( pExpr->bDesc==0 ){ | |
953 if( iLhs<iRhs ) return -1; | |
954 return (iLhs > iRhs); | |
955 }else{ | |
956 if( iLhs>iRhs ) return -1; | |
957 return (iLhs < iRhs); | |
958 } | |
959 } | |
960 | |
961 static void fts5ExprSetEof(Fts5ExprNode *pNode){ | |
962 int i; | |
963 pNode->bEof = 1; | |
964 for(i=0; i<pNode->nChild; i++){ | |
965 fts5ExprSetEof(pNode->apChild[i]); | |
966 } | |
967 } | |
968 | |
969 static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){ | |
970 if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ | |
971 Fts5ExprNearset *pNear = pNode->pNear; | |
972 int i; | |
973 for(i=0; i<pNear->nPhrase; i++){ | |
974 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; | |
975 pPhrase->poslist.n = 0; | |
976 } | |
977 }else{ | |
978 int i; | |
979 for(i=0; i<pNode->nChild; i++){ | |
980 fts5ExprNodeZeroPoslist(pNode->apChild[i]); | |
981 } | |
982 } | |
983 } | |
984 | |
985 | |
986 static int fts5ExprNodeNext(Fts5Expr*, Fts5ExprNode*, int, i64); | |
987 | |
988 /* | |
989 ** Argument pNode is an FTS5_AND node. | |
990 */ | |
991 static int fts5ExprAndNextRowid( | |
992 Fts5Expr *pExpr, /* Expression pPhrase belongs to */ | |
993 Fts5ExprNode *pAnd /* FTS5_AND node to advance */ | |
994 ){ | |
995 int iChild; | |
996 i64 iLast = pAnd->iRowid; | |
997 int rc = SQLITE_OK; | |
998 int bMatch; | |
999 | |
1000 assert( pAnd->bEof==0 ); | |
1001 do { | |
1002 pAnd->bNomatch = 0; | |
1003 bMatch = 1; | |
1004 for(iChild=0; iChild<pAnd->nChild; iChild++){ | |
1005 Fts5ExprNode *pChild = pAnd->apChild[iChild]; | |
1006 if( 0 && pChild->eType==FTS5_STRING ){ | |
1007 /* TODO */ | |
1008 }else{ | |
1009 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid); | |
1010 if( cmp>0 ){ | |
1011 /* Advance pChild until it points to iLast or laster */ | |
1012 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast); | |
1013 if( rc!=SQLITE_OK ) return rc; | |
1014 } | |
1015 } | |
1016 | |
1017 /* If the child node is now at EOF, so is the parent AND node. Otherwise, | |
1018 ** the child node is guaranteed to have advanced at least as far as | |
1019 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the | |
1020 ** new lastest rowid seen so far. */ | |
1021 assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 ); | |
1022 if( pChild->bEof ){ | |
1023 fts5ExprSetEof(pAnd); | |
1024 bMatch = 1; | |
1025 break; | |
1026 }else if( iLast!=pChild->iRowid ){ | |
1027 bMatch = 0; | |
1028 iLast = pChild->iRowid; | |
1029 } | |
1030 | |
1031 if( pChild->bNomatch ){ | |
1032 pAnd->bNomatch = 1; | |
1033 } | |
1034 } | |
1035 }while( bMatch==0 ); | |
1036 | |
1037 if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){ | |
1038 fts5ExprNodeZeroPoslist(pAnd); | |
1039 } | |
1040 pAnd->iRowid = iLast; | |
1041 return SQLITE_OK; | |
1042 } | |
1043 | |
1044 | |
1045 /* | |
1046 ** Compare the values currently indicated by the two nodes as follows: | |
1047 ** | |
1048 ** res = (*p1) - (*p2) | |
1049 ** | |
1050 ** Nodes that point to values that come later in the iteration order are | |
1051 ** considered to be larger. Nodes at EOF are the largest of all. | |
1052 ** | |
1053 ** This means that if the iteration order is ASC, then numerically larger | |
1054 ** rowids are considered larger. Or if it is the default DESC, numerically | |
1055 ** smaller rowids are larger. | |
1056 */ | |
1057 static int fts5NodeCompare( | |
1058 Fts5Expr *pExpr, | |
1059 Fts5ExprNode *p1, | |
1060 Fts5ExprNode *p2 | |
1061 ){ | |
1062 if( p2->bEof ) return -1; | |
1063 if( p1->bEof ) return +1; | |
1064 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid); | |
1065 } | |
1066 | |
1067 /* | |
1068 ** Advance node iterator pNode, part of expression pExpr. If argument | |
1069 ** bFromValid is zero, then pNode is advanced exactly once. Or, if argument | |
1070 ** bFromValid is non-zero, then pNode is advanced until it is at or past | |
1071 ** rowid value iFrom. Whether "past" means "less than" or "greater than" | |
1072 ** depends on whether this is an ASC or DESC iterator. | |
1073 */ | |
1074 static int fts5ExprNodeNext( | |
1075 Fts5Expr *pExpr, | |
1076 Fts5ExprNode *pNode, | |
1077 int bFromValid, | |
1078 i64 iFrom | |
1079 ){ | |
1080 int rc = SQLITE_OK; | |
1081 | |
1082 if( pNode->bEof==0 ){ | |
1083 switch( pNode->eType ){ | |
1084 case FTS5_STRING: { | |
1085 rc = fts5ExprNearAdvanceFirst(pExpr, pNode, bFromValid, iFrom); | |
1086 break; | |
1087 }; | |
1088 | |
1089 case FTS5_TERM: { | |
1090 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter; | |
1091 if( bFromValid ){ | |
1092 rc = sqlite3Fts5IterNextFrom(pIter, iFrom); | |
1093 }else{ | |
1094 rc = sqlite3Fts5IterNext(pIter); | |
1095 } | |
1096 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){ | |
1097 assert( rc==SQLITE_OK ); | |
1098 rc = fts5ExprTokenTest(pExpr, pNode); | |
1099 }else{ | |
1100 pNode->bEof = 1; | |
1101 } | |
1102 return rc; | |
1103 }; | |
1104 | |
1105 case FTS5_AND: { | |
1106 Fts5ExprNode *pLeft = pNode->apChild[0]; | |
1107 rc = fts5ExprNodeNext(pExpr, pLeft, bFromValid, iFrom); | |
1108 break; | |
1109 } | |
1110 | |
1111 case FTS5_OR: { | |
1112 int i; | |
1113 i64 iLast = pNode->iRowid; | |
1114 | |
1115 for(i=0; rc==SQLITE_OK && i<pNode->nChild; i++){ | |
1116 Fts5ExprNode *p1 = pNode->apChild[i]; | |
1117 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 ); | |
1118 if( p1->bEof==0 ){ | |
1119 if( (p1->iRowid==iLast) | |
1120 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0) | |
1121 ){ | |
1122 rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom); | |
1123 } | |
1124 } | |
1125 } | |
1126 | |
1127 break; | |
1128 } | |
1129 | |
1130 default: assert( pNode->eType==FTS5_NOT ); { | |
1131 assert( pNode->nChild==2 ); | |
1132 rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom); | |
1133 break; | |
1134 } | |
1135 } | |
1136 | |
1137 if( rc==SQLITE_OK ){ | |
1138 rc = fts5ExprNodeNextMatch(pExpr, pNode); | |
1139 } | |
1140 } | |
1141 | |
1142 /* Assert that if bFromValid was true, either: | |
1143 ** | |
1144 ** a) an error occurred, or | |
1145 ** b) the node is now at EOF, or | |
1146 ** c) the node is now at or past rowid iFrom. | |
1147 */ | |
1148 assert( bFromValid==0 | |
1149 || rc!=SQLITE_OK /* a */ | |
1150 || pNode->bEof /* b */ | |
1151 || pNode->iRowid==iFrom || pExpr->bDesc==(pNode->iRowid<iFrom) /* c */ | |
1152 ); | |
1153 | |
1154 return rc; | |
1155 } | |
1156 | |
1157 | |
1158 /* | |
1159 ** If pNode currently points to a match, this function returns SQLITE_OK | |
1160 ** without modifying it. Otherwise, pNode is advanced until it does point | |
1161 ** to a match or EOF is reached. | |
1162 */ | |
1163 static int fts5ExprNodeNextMatch( | |
1164 Fts5Expr *pExpr, /* Expression of which pNode is a part */ | |
1165 Fts5ExprNode *pNode /* Expression node to test */ | |
1166 ){ | |
1167 int rc = SQLITE_OK; | |
1168 if( pNode->bEof==0 ){ | |
1169 switch( pNode->eType ){ | |
1170 | |
1171 case FTS5_STRING: { | |
1172 /* Advance the iterators until they all point to the same rowid */ | |
1173 rc = fts5ExprNearNextMatch(pExpr, pNode); | |
1174 break; | |
1175 } | |
1176 | |
1177 case FTS5_TERM: { | |
1178 rc = fts5ExprTokenTest(pExpr, pNode); | |
1179 break; | |
1180 } | |
1181 | |
1182 case FTS5_AND: { | |
1183 rc = fts5ExprAndNextRowid(pExpr, pNode); | |
1184 break; | |
1185 } | |
1186 | |
1187 case FTS5_OR: { | |
1188 Fts5ExprNode *pNext = pNode->apChild[0]; | |
1189 int i; | |
1190 | |
1191 for(i=1; i<pNode->nChild; i++){ | |
1192 Fts5ExprNode *pChild = pNode->apChild[i]; | |
1193 int cmp = fts5NodeCompare(pExpr, pNext, pChild); | |
1194 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){ | |
1195 pNext = pChild; | |
1196 } | |
1197 } | |
1198 pNode->iRowid = pNext->iRowid; | |
1199 pNode->bEof = pNext->bEof; | |
1200 pNode->bNomatch = pNext->bNomatch; | |
1201 break; | |
1202 } | |
1203 | |
1204 default: assert( pNode->eType==FTS5_NOT ); { | |
1205 Fts5ExprNode *p1 = pNode->apChild[0]; | |
1206 Fts5ExprNode *p2 = pNode->apChild[1]; | |
1207 assert( pNode->nChild==2 ); | |
1208 | |
1209 while( rc==SQLITE_OK && p1->bEof==0 ){ | |
1210 int cmp = fts5NodeCompare(pExpr, p1, p2); | |
1211 if( cmp>0 ){ | |
1212 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid); | |
1213 cmp = fts5NodeCompare(pExpr, p1, p2); | |
1214 } | |
1215 assert( rc!=SQLITE_OK || cmp<=0 ); | |
1216 if( cmp || p2->bNomatch ) break; | |
1217 rc = fts5ExprNodeNext(pExpr, p1, 0, 0); | |
1218 } | |
1219 pNode->bEof = p1->bEof; | |
1220 pNode->iRowid = p1->iRowid; | |
1221 break; | |
1222 } | |
1223 } | |
1224 } | |
1225 return rc; | |
1226 } | |
1227 | |
1228 | |
1229 /* | |
1230 ** Set node pNode, which is part of expression pExpr, to point to the first | |
1231 ** match. If there are no matches, set the Node.bEof flag to indicate EOF. | |
1232 ** | |
1233 ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise. | |
1234 ** It is not an error if there are no matches. | |
1235 */ | |
1236 static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){ | |
1237 int rc = SQLITE_OK; | |
1238 pNode->bEof = 0; | |
1239 | |
1240 if( Fts5NodeIsString(pNode) ){ | |
1241 /* Initialize all term iterators in the NEAR object. */ | |
1242 rc = fts5ExprNearInitAll(pExpr, pNode); | |
1243 }else{ | |
1244 int i; | |
1245 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){ | |
1246 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]); | |
1247 } | |
1248 pNode->iRowid = pNode->apChild[0]->iRowid; | |
1249 } | |
1250 | |
1251 if( rc==SQLITE_OK ){ | |
1252 rc = fts5ExprNodeNextMatch(pExpr, pNode); | |
1253 } | |
1254 return rc; | |
1255 } | |
1256 | |
1257 | |
1258 /* | |
1259 ** Begin iterating through the set of documents in index pIdx matched by | |
1260 ** the MATCH expression passed as the first argument. If the "bDesc" | |
1261 ** parameter is passed a non-zero value, iteration is in descending rowid | |
1262 ** order. Or, if it is zero, in ascending order. | |
1263 ** | |
1264 ** If iterating in ascending rowid order (bDesc==0), the first document | |
1265 ** visited is that with the smallest rowid that is larger than or equal | |
1266 ** to parameter iFirst. Or, if iterating in ascending order (bDesc==1), | |
1267 ** then the first document visited must have a rowid smaller than or | |
1268 ** equal to iFirst. | |
1269 ** | |
1270 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It | |
1271 ** is not considered an error if the query does not match any documents. | |
1272 */ | |
1273 int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){ | |
1274 Fts5ExprNode *pRoot = p->pRoot; | |
1275 int rc = SQLITE_OK; | |
1276 if( pRoot ){ | |
1277 p->pIndex = pIdx; | |
1278 p->bDesc = bDesc; | |
1279 rc = fts5ExprNodeFirst(p, pRoot); | |
1280 | |
1281 /* If not at EOF but the current rowid occurs earlier than iFirst in | |
1282 ** the iteration order, move to document iFirst or later. */ | |
1283 if( pRoot->bEof==0 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 ){ | |
1284 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst); | |
1285 } | |
1286 | |
1287 /* If the iterator is not at a real match, skip forward until it is. */ | |
1288 while( pRoot->bNomatch && rc==SQLITE_OK && pRoot->bEof==0 ){ | |
1289 rc = fts5ExprNodeNext(p, pRoot, 0, 0); | |
1290 } | |
1291 } | |
1292 return rc; | |
1293 } | |
1294 | |
1295 /* | |
1296 ** Move to the next document | |
1297 ** | |
1298 ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It | |
1299 ** is not considered an error if the query does not match any documents. | |
1300 */ | |
1301 int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){ | |
1302 int rc; | |
1303 Fts5ExprNode *pRoot = p->pRoot; | |
1304 do { | |
1305 rc = fts5ExprNodeNext(p, pRoot, 0, 0); | |
1306 }while( pRoot->bNomatch && pRoot->bEof==0 && rc==SQLITE_OK ); | |
1307 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){ | |
1308 pRoot->bEof = 1; | |
1309 } | |
1310 return rc; | |
1311 } | |
1312 | |
1313 int sqlite3Fts5ExprEof(Fts5Expr *p){ | |
1314 return (p->pRoot==0 || p->pRoot->bEof); | |
1315 } | |
1316 | |
1317 i64 sqlite3Fts5ExprRowid(Fts5Expr *p){ | |
1318 return p->pRoot->iRowid; | |
1319 } | |
1320 | |
1321 static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){ | |
1322 int rc = SQLITE_OK; | |
1323 *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n); | |
1324 return rc; | |
1325 } | |
1326 | |
1327 /* | |
1328 ** Free the phrase object passed as the only argument. | |
1329 */ | |
1330 static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){ | |
1331 if( pPhrase ){ | |
1332 int i; | |
1333 for(i=0; i<pPhrase->nTerm; i++){ | |
1334 Fts5ExprTerm *pSyn; | |
1335 Fts5ExprTerm *pNext; | |
1336 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; | |
1337 sqlite3_free(pTerm->zTerm); | |
1338 sqlite3Fts5IterClose(pTerm->pIter); | |
1339 | |
1340 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){ | |
1341 pNext = pSyn->pSynonym; | |
1342 sqlite3Fts5IterClose(pSyn->pIter); | |
1343 sqlite3_free(pSyn); | |
1344 } | |
1345 } | |
1346 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist); | |
1347 sqlite3_free(pPhrase); | |
1348 } | |
1349 } | |
1350 | |
1351 /* | |
1352 ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated | |
1353 ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is | |
1354 ** appended to it and the results returned. | |
1355 ** | |
1356 ** If an OOM error occurs, both the pNear and pPhrase objects are freed and | |
1357 ** NULL returned. | |
1358 */ | |
1359 Fts5ExprNearset *sqlite3Fts5ParseNearset( | |
1360 Fts5Parse *pParse, /* Parse context */ | |
1361 Fts5ExprNearset *pNear, /* Existing nearset, or NULL */ | |
1362 Fts5ExprPhrase *pPhrase /* Recently parsed phrase */ | |
1363 ){ | |
1364 const int SZALLOC = 8; | |
1365 Fts5ExprNearset *pRet = 0; | |
1366 | |
1367 if( pParse->rc==SQLITE_OK ){ | |
1368 if( pPhrase==0 ){ | |
1369 return pNear; | |
1370 } | |
1371 if( pNear==0 ){ | |
1372 int nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*); | |
1373 pRet = sqlite3_malloc(nByte); | |
1374 if( pRet==0 ){ | |
1375 pParse->rc = SQLITE_NOMEM; | |
1376 }else{ | |
1377 memset(pRet, 0, nByte); | |
1378 } | |
1379 }else if( (pNear->nPhrase % SZALLOC)==0 ){ | |
1380 int nNew = pNear->nPhrase + SZALLOC; | |
1381 int nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*); | |
1382 | |
1383 pRet = (Fts5ExprNearset*)sqlite3_realloc(pNear, nByte); | |
1384 if( pRet==0 ){ | |
1385 pParse->rc = SQLITE_NOMEM; | |
1386 } | |
1387 }else{ | |
1388 pRet = pNear; | |
1389 } | |
1390 } | |
1391 | |
1392 if( pRet==0 ){ | |
1393 assert( pParse->rc!=SQLITE_OK ); | |
1394 sqlite3Fts5ParseNearsetFree(pNear); | |
1395 sqlite3Fts5ParsePhraseFree(pPhrase); | |
1396 }else{ | |
1397 pRet->apPhrase[pRet->nPhrase++] = pPhrase; | |
1398 } | |
1399 return pRet; | |
1400 } | |
1401 | |
1402 typedef struct TokenCtx TokenCtx; | |
1403 struct TokenCtx { | |
1404 Fts5ExprPhrase *pPhrase; | |
1405 int rc; | |
1406 }; | |
1407 | |
1408 /* | |
1409 ** Callback for tokenizing terms used by ParseTerm(). | |
1410 */ | |
1411 static int fts5ParseTokenize( | |
1412 void *pContext, /* Pointer to Fts5InsertCtx object */ | |
1413 int tflags, /* Mask of FTS5_TOKEN_* flags */ | |
1414 const char *pToken, /* Buffer containing token */ | |
1415 int nToken, /* Size of token in bytes */ | |
1416 int iUnused1, /* Start offset of token */ | |
1417 int iUnused2 /* End offset of token */ | |
1418 ){ | |
1419 int rc = SQLITE_OK; | |
1420 const int SZALLOC = 8; | |
1421 TokenCtx *pCtx = (TokenCtx*)pContext; | |
1422 Fts5ExprPhrase *pPhrase = pCtx->pPhrase; | |
1423 | |
1424 /* If an error has already occurred, this is a no-op */ | |
1425 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc; | |
1426 | |
1427 assert( pPhrase==0 || pPhrase->nTerm>0 ); | |
1428 if( pPhrase && (tflags & FTS5_TOKEN_COLOCATED) ){ | |
1429 Fts5ExprTerm *pSyn; | |
1430 int nByte = sizeof(Fts5ExprTerm) + nToken+1; | |
1431 pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte); | |
1432 if( pSyn==0 ){ | |
1433 rc = SQLITE_NOMEM; | |
1434 }else{ | |
1435 memset(pSyn, 0, nByte); | |
1436 pSyn->zTerm = (char*)&pSyn[1]; | |
1437 memcpy(pSyn->zTerm, pToken, nToken); | |
1438 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym; | |
1439 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn; | |
1440 } | |
1441 }else{ | |
1442 Fts5ExprTerm *pTerm; | |
1443 if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){ | |
1444 Fts5ExprPhrase *pNew; | |
1445 int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0); | |
1446 | |
1447 pNew = (Fts5ExprPhrase*)sqlite3_realloc(pPhrase, | |
1448 sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew | |
1449 ); | |
1450 if( pNew==0 ){ | |
1451 rc = SQLITE_NOMEM; | |
1452 }else{ | |
1453 if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase)); | |
1454 pCtx->pPhrase = pPhrase = pNew; | |
1455 pNew->nTerm = nNew - SZALLOC; | |
1456 } | |
1457 } | |
1458 | |
1459 if( rc==SQLITE_OK ){ | |
1460 pTerm = &pPhrase->aTerm[pPhrase->nTerm++]; | |
1461 memset(pTerm, 0, sizeof(Fts5ExprTerm)); | |
1462 pTerm->zTerm = sqlite3Fts5Strndup(&rc, pToken, nToken); | |
1463 } | |
1464 } | |
1465 | |
1466 pCtx->rc = rc; | |
1467 return rc; | |
1468 } | |
1469 | |
1470 | |
1471 /* | |
1472 ** Free the phrase object passed as the only argument. | |
1473 */ | |
1474 void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){ | |
1475 fts5ExprPhraseFree(pPhrase); | |
1476 } | |
1477 | |
1478 /* | |
1479 ** Free the phrase object passed as the second argument. | |
1480 */ | |
1481 void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){ | |
1482 if( pNear ){ | |
1483 int i; | |
1484 for(i=0; i<pNear->nPhrase; i++){ | |
1485 fts5ExprPhraseFree(pNear->apPhrase[i]); | |
1486 } | |
1487 sqlite3_free(pNear->pColset); | |
1488 sqlite3_free(pNear); | |
1489 } | |
1490 } | |
1491 | |
1492 void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){ | |
1493 assert( pParse->pExpr==0 ); | |
1494 pParse->pExpr = p; | |
1495 } | |
1496 | |
1497 /* | |
1498 ** This function is called by the parser to process a string token. The | |
1499 ** string may or may not be quoted. In any case it is tokenized and a | |
1500 ** phrase object consisting of all tokens returned. | |
1501 */ | |
1502 Fts5ExprPhrase *sqlite3Fts5ParseTerm( | |
1503 Fts5Parse *pParse, /* Parse context */ | |
1504 Fts5ExprPhrase *pAppend, /* Phrase to append to */ | |
1505 Fts5Token *pToken, /* String to tokenize */ | |
1506 int bPrefix /* True if there is a trailing "*" */ | |
1507 ){ | |
1508 Fts5Config *pConfig = pParse->pConfig; | |
1509 TokenCtx sCtx; /* Context object passed to callback */ | |
1510 int rc; /* Tokenize return code */ | |
1511 char *z = 0; | |
1512 | |
1513 memset(&sCtx, 0, sizeof(TokenCtx)); | |
1514 sCtx.pPhrase = pAppend; | |
1515 | |
1516 rc = fts5ParseStringFromToken(pToken, &z); | |
1517 if( rc==SQLITE_OK ){ | |
1518 int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_QUERY : 0); | |
1519 int n; | |
1520 sqlite3Fts5Dequote(z); | |
1521 n = (int)strlen(z); | |
1522 rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize); | |
1523 } | |
1524 sqlite3_free(z); | |
1525 if( rc || (rc = sCtx.rc) ){ | |
1526 pParse->rc = rc; | |
1527 fts5ExprPhraseFree(sCtx.pPhrase); | |
1528 sCtx.pPhrase = 0; | |
1529 }else if( sCtx.pPhrase ){ | |
1530 | |
1531 if( pAppend==0 ){ | |
1532 if( (pParse->nPhrase % 8)==0 ){ | |
1533 int nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8); | |
1534 Fts5ExprPhrase **apNew; | |
1535 apNew = (Fts5ExprPhrase**)sqlite3_realloc(pParse->apPhrase, nByte); | |
1536 if( apNew==0 ){ | |
1537 pParse->rc = SQLITE_NOMEM; | |
1538 fts5ExprPhraseFree(sCtx.pPhrase); | |
1539 return 0; | |
1540 } | |
1541 pParse->apPhrase = apNew; | |
1542 } | |
1543 pParse->nPhrase++; | |
1544 } | |
1545 | |
1546 pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase; | |
1547 assert( sCtx.pPhrase->nTerm>0 ); | |
1548 sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = bPrefix; | |
1549 } | |
1550 | |
1551 return sCtx.pPhrase; | |
1552 } | |
1553 | |
1554 /* | |
1555 ** Create a new FTS5 expression by cloning phrase iPhrase of the | |
1556 ** expression passed as the second argument. | |
1557 */ | |
1558 int sqlite3Fts5ExprClonePhrase( | |
1559 Fts5Config *pConfig, | |
1560 Fts5Expr *pExpr, | |
1561 int iPhrase, | |
1562 Fts5Expr **ppNew | |
1563 ){ | |
1564 int rc = SQLITE_OK; /* Return code */ | |
1565 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */ | |
1566 int i; /* Used to iterate through phrase terms */ | |
1567 | |
1568 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */ | |
1569 | |
1570 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */ | |
1571 | |
1572 | |
1573 pOrig = pExpr->apExprPhrase[iPhrase]; | |
1574 | |
1575 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr)); | |
1576 if( rc==SQLITE_OK ){ | |
1577 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc, | |
1578 sizeof(Fts5ExprPhrase*)); | |
1579 } | |
1580 if( rc==SQLITE_OK ){ | |
1581 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, | |
1582 sizeof(Fts5ExprNode)); | |
1583 } | |
1584 if( rc==SQLITE_OK ){ | |
1585 pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc, | |
1586 sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*)); | |
1587 } | |
1588 | |
1589 for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){ | |
1590 int tflags = 0; | |
1591 Fts5ExprTerm *p; | |
1592 for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){ | |
1593 const char *zTerm = p->zTerm; | |
1594 rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm), | |
1595 0, 0); | |
1596 tflags = FTS5_TOKEN_COLOCATED; | |
1597 } | |
1598 if( rc==SQLITE_OK ){ | |
1599 sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix; | |
1600 } | |
1601 } | |
1602 | |
1603 if( rc==SQLITE_OK ){ | |
1604 /* All the allocations succeeded. Put the expression object together. */ | |
1605 pNew->pIndex = pExpr->pIndex; | |
1606 pNew->nPhrase = 1; | |
1607 pNew->apExprPhrase[0] = sCtx.pPhrase; | |
1608 pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase; | |
1609 pNew->pRoot->pNear->nPhrase = 1; | |
1610 sCtx.pPhrase->pNode = pNew->pRoot; | |
1611 | |
1612 if( pOrig->nTerm==1 && pOrig->aTerm[0].pSynonym==0 ){ | |
1613 pNew->pRoot->eType = FTS5_TERM; | |
1614 }else{ | |
1615 pNew->pRoot->eType = FTS5_STRING; | |
1616 } | |
1617 }else{ | |
1618 sqlite3Fts5ExprFree(pNew); | |
1619 fts5ExprPhraseFree(sCtx.pPhrase); | |
1620 pNew = 0; | |
1621 } | |
1622 | |
1623 *ppNew = pNew; | |
1624 return rc; | |
1625 } | |
1626 | |
1627 | |
1628 /* | |
1629 ** Token pTok has appeared in a MATCH expression where the NEAR operator | |
1630 ** is expected. If token pTok does not contain "NEAR", store an error | |
1631 ** in the pParse object. | |
1632 */ | |
1633 void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){ | |
1634 if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){ | |
1635 sqlite3Fts5ParseError( | |
1636 pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p | |
1637 ); | |
1638 } | |
1639 } | |
1640 | |
1641 void sqlite3Fts5ParseSetDistance( | |
1642 Fts5Parse *pParse, | |
1643 Fts5ExprNearset *pNear, | |
1644 Fts5Token *p | |
1645 ){ | |
1646 int nNear = 0; | |
1647 int i; | |
1648 if( p->n ){ | |
1649 for(i=0; i<p->n; i++){ | |
1650 char c = (char)p->p[i]; | |
1651 if( c<'0' || c>'9' ){ | |
1652 sqlite3Fts5ParseError( | |
1653 pParse, "expected integer, got \"%.*s\"", p->n, p->p | |
1654 ); | |
1655 return; | |
1656 } | |
1657 nNear = nNear * 10 + (p->p[i] - '0'); | |
1658 } | |
1659 }else{ | |
1660 nNear = FTS5_DEFAULT_NEARDIST; | |
1661 } | |
1662 pNear->nNear = nNear; | |
1663 } | |
1664 | |
1665 /* | |
1666 ** The second argument passed to this function may be NULL, or it may be | |
1667 ** an existing Fts5Colset object. This function returns a pointer to | |
1668 ** a new colset object containing the contents of (p) with new value column | |
1669 ** number iCol appended. | |
1670 ** | |
1671 ** If an OOM error occurs, store an error code in pParse and return NULL. | |
1672 ** The old colset object (if any) is not freed in this case. | |
1673 */ | |
1674 static Fts5Colset *fts5ParseColset( | |
1675 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */ | |
1676 Fts5Colset *p, /* Existing colset object */ | |
1677 int iCol /* New column to add to colset object */ | |
1678 ){ | |
1679 int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */ | |
1680 Fts5Colset *pNew; /* New colset object to return */ | |
1681 | |
1682 assert( pParse->rc==SQLITE_OK ); | |
1683 assert( iCol>=0 && iCol<pParse->pConfig->nCol ); | |
1684 | |
1685 pNew = sqlite3_realloc(p, sizeof(Fts5Colset) + sizeof(int)*nCol); | |
1686 if( pNew==0 ){ | |
1687 pParse->rc = SQLITE_NOMEM; | |
1688 }else{ | |
1689 int *aiCol = pNew->aiCol; | |
1690 int i, j; | |
1691 for(i=0; i<nCol; i++){ | |
1692 if( aiCol[i]==iCol ) return pNew; | |
1693 if( aiCol[i]>iCol ) break; | |
1694 } | |
1695 for(j=nCol; j>i; j--){ | |
1696 aiCol[j] = aiCol[j-1]; | |
1697 } | |
1698 aiCol[i] = iCol; | |
1699 pNew->nCol = nCol+1; | |
1700 | |
1701 #ifndef NDEBUG | |
1702 /* Check that the array is in order and contains no duplicate entries. */ | |
1703 for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] ); | |
1704 #endif | |
1705 } | |
1706 | |
1707 return pNew; | |
1708 } | |
1709 | |
1710 Fts5Colset *sqlite3Fts5ParseColset( | |
1711 Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */ | |
1712 Fts5Colset *pColset, /* Existing colset object */ | |
1713 Fts5Token *p | |
1714 ){ | |
1715 Fts5Colset *pRet = 0; | |
1716 int iCol; | |
1717 char *z; /* Dequoted copy of token p */ | |
1718 | |
1719 z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n); | |
1720 if( pParse->rc==SQLITE_OK ){ | |
1721 Fts5Config *pConfig = pParse->pConfig; | |
1722 sqlite3Fts5Dequote(z); | |
1723 for(iCol=0; iCol<pConfig->nCol; iCol++){ | |
1724 if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break; | |
1725 } | |
1726 if( iCol==pConfig->nCol ){ | |
1727 sqlite3Fts5ParseError(pParse, "no such column: %s", z); | |
1728 }else{ | |
1729 pRet = fts5ParseColset(pParse, pColset, iCol); | |
1730 } | |
1731 sqlite3_free(z); | |
1732 } | |
1733 | |
1734 if( pRet==0 ){ | |
1735 assert( pParse->rc!=SQLITE_OK ); | |
1736 sqlite3_free(pColset); | |
1737 } | |
1738 | |
1739 return pRet; | |
1740 } | |
1741 | |
1742 void sqlite3Fts5ParseSetColset( | |
1743 Fts5Parse *pParse, | |
1744 Fts5ExprNearset *pNear, | |
1745 Fts5Colset *pColset | |
1746 ){ | |
1747 if( pNear ){ | |
1748 pNear->pColset = pColset; | |
1749 }else{ | |
1750 sqlite3_free(pColset); | |
1751 } | |
1752 } | |
1753 | |
1754 static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){ | |
1755 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){ | |
1756 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild; | |
1757 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte); | |
1758 p->nChild += pSub->nChild; | |
1759 sqlite3_free(pSub); | |
1760 }else{ | |
1761 p->apChild[p->nChild++] = pSub; | |
1762 } | |
1763 } | |
1764 | |
1765 /* | |
1766 ** Allocate and return a new expression object. If anything goes wrong (i.e. | |
1767 ** OOM error), leave an error code in pParse and return NULL. | |
1768 */ | |
1769 Fts5ExprNode *sqlite3Fts5ParseNode( | |
1770 Fts5Parse *pParse, /* Parse context */ | |
1771 int eType, /* FTS5_STRING, AND, OR or NOT */ | |
1772 Fts5ExprNode *pLeft, /* Left hand child expression */ | |
1773 Fts5ExprNode *pRight, /* Right hand child expression */ | |
1774 Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */ | |
1775 ){ | |
1776 Fts5ExprNode *pRet = 0; | |
1777 | |
1778 if( pParse->rc==SQLITE_OK ){ | |
1779 int nChild = 0; /* Number of children of returned node */ | |
1780 int nByte; /* Bytes of space to allocate for this node */ | |
1781 | |
1782 assert( (eType!=FTS5_STRING && !pNear) | |
1783 || (eType==FTS5_STRING && !pLeft && !pRight) | |
1784 ); | |
1785 if( eType==FTS5_STRING && pNear==0 ) return 0; | |
1786 if( eType!=FTS5_STRING && pLeft==0 ) return pRight; | |
1787 if( eType!=FTS5_STRING && pRight==0 ) return pLeft; | |
1788 | |
1789 if( eType==FTS5_NOT ){ | |
1790 nChild = 2; | |
1791 }else if( eType==FTS5_AND || eType==FTS5_OR ){ | |
1792 nChild = 2; | |
1793 if( pLeft->eType==eType ) nChild += pLeft->nChild-1; | |
1794 if( pRight->eType==eType ) nChild += pRight->nChild-1; | |
1795 } | |
1796 | |
1797 nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1); | |
1798 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); | |
1799 | |
1800 if( pRet ){ | |
1801 pRet->eType = eType; | |
1802 pRet->pNear = pNear; | |
1803 if( eType==FTS5_STRING ){ | |
1804 int iPhrase; | |
1805 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){ | |
1806 pNear->apPhrase[iPhrase]->pNode = pRet; | |
1807 } | |
1808 if( pNear->nPhrase==1 | |
1809 && pNear->apPhrase[0]->nTerm==1 | |
1810 && pNear->apPhrase[0]->aTerm[0].pSynonym==0 | |
1811 ){ | |
1812 pRet->eType = FTS5_TERM; | |
1813 } | |
1814 }else{ | |
1815 fts5ExprAddChildren(pRet, pLeft); | |
1816 fts5ExprAddChildren(pRet, pRight); | |
1817 } | |
1818 } | |
1819 } | |
1820 | |
1821 if( pRet==0 ){ | |
1822 assert( pParse->rc!=SQLITE_OK ); | |
1823 sqlite3Fts5ParseNodeFree(pLeft); | |
1824 sqlite3Fts5ParseNodeFree(pRight); | |
1825 sqlite3Fts5ParseNearsetFree(pNear); | |
1826 } | |
1827 return pRet; | |
1828 } | |
1829 | |
1830 static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){ | |
1831 int nByte = 0; | |
1832 Fts5ExprTerm *p; | |
1833 char *zQuoted; | |
1834 | |
1835 /* Determine the maximum amount of space required. */ | |
1836 for(p=pTerm; p; p=p->pSynonym){ | |
1837 nByte += (int)strlen(pTerm->zTerm) * 2 + 3 + 2; | |
1838 } | |
1839 zQuoted = sqlite3_malloc(nByte); | |
1840 | |
1841 if( zQuoted ){ | |
1842 int i = 0; | |
1843 for(p=pTerm; p; p=p->pSynonym){ | |
1844 char *zIn = p->zTerm; | |
1845 zQuoted[i++] = '"'; | |
1846 while( *zIn ){ | |
1847 if( *zIn=='"' ) zQuoted[i++] = '"'; | |
1848 zQuoted[i++] = *zIn++; | |
1849 } | |
1850 zQuoted[i++] = '"'; | |
1851 if( p->pSynonym ) zQuoted[i++] = '|'; | |
1852 } | |
1853 if( pTerm->bPrefix ){ | |
1854 zQuoted[i++] = ' '; | |
1855 zQuoted[i++] = '*'; | |
1856 } | |
1857 zQuoted[i++] = '\0'; | |
1858 } | |
1859 return zQuoted; | |
1860 } | |
1861 | |
1862 static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){ | |
1863 char *zNew; | |
1864 va_list ap; | |
1865 va_start(ap, zFmt); | |
1866 zNew = sqlite3_vmprintf(zFmt, ap); | |
1867 va_end(ap); | |
1868 if( zApp && zNew ){ | |
1869 char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew); | |
1870 sqlite3_free(zNew); | |
1871 zNew = zNew2; | |
1872 } | |
1873 sqlite3_free(zApp); | |
1874 return zNew; | |
1875 } | |
1876 | |
1877 /* | |
1878 ** Compose a tcl-readable representation of expression pExpr. Return a | |
1879 ** pointer to a buffer containing that representation. It is the | |
1880 ** responsibility of the caller to at some point free the buffer using | |
1881 ** sqlite3_free(). | |
1882 */ | |
1883 static char *fts5ExprPrintTcl( | |
1884 Fts5Config *pConfig, | |
1885 const char *zNearsetCmd, | |
1886 Fts5ExprNode *pExpr | |
1887 ){ | |
1888 char *zRet = 0; | |
1889 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){ | |
1890 Fts5ExprNearset *pNear = pExpr->pNear; | |
1891 int i; | |
1892 int iTerm; | |
1893 | |
1894 zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd); | |
1895 if( zRet==0 ) return 0; | |
1896 if( pNear->pColset ){ | |
1897 int *aiCol = pNear->pColset->aiCol; | |
1898 int nCol = pNear->pColset->nCol; | |
1899 if( nCol==1 ){ | |
1900 zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]); | |
1901 }else{ | |
1902 zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]); | |
1903 for(i=1; i<pNear->pColset->nCol; i++){ | |
1904 zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]); | |
1905 } | |
1906 zRet = fts5PrintfAppend(zRet, "} "); | |
1907 } | |
1908 if( zRet==0 ) return 0; | |
1909 } | |
1910 | |
1911 if( pNear->nPhrase>1 ){ | |
1912 zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear); | |
1913 if( zRet==0 ) return 0; | |
1914 } | |
1915 | |
1916 zRet = fts5PrintfAppend(zRet, "--"); | |
1917 if( zRet==0 ) return 0; | |
1918 | |
1919 for(i=0; i<pNear->nPhrase; i++){ | |
1920 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; | |
1921 | |
1922 zRet = fts5PrintfAppend(zRet, " {"); | |
1923 for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){ | |
1924 char *zTerm = pPhrase->aTerm[iTerm].zTerm; | |
1925 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" ", zTerm); | |
1926 } | |
1927 | |
1928 if( zRet ) zRet = fts5PrintfAppend(zRet, "}"); | |
1929 if( zRet==0 ) return 0; | |
1930 } | |
1931 | |
1932 }else{ | |
1933 char const *zOp = 0; | |
1934 int i; | |
1935 switch( pExpr->eType ){ | |
1936 case FTS5_AND: zOp = "AND"; break; | |
1937 case FTS5_NOT: zOp = "NOT"; break; | |
1938 default: | |
1939 assert( pExpr->eType==FTS5_OR ); | |
1940 zOp = "OR"; | |
1941 break; | |
1942 } | |
1943 | |
1944 zRet = sqlite3_mprintf("%s", zOp); | |
1945 for(i=0; zRet && i<pExpr->nChild; i++){ | |
1946 char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]); | |
1947 if( !z ){ | |
1948 sqlite3_free(zRet); | |
1949 zRet = 0; | |
1950 }else{ | |
1951 zRet = fts5PrintfAppend(zRet, " [%z]", z); | |
1952 } | |
1953 } | |
1954 } | |
1955 | |
1956 return zRet; | |
1957 } | |
1958 | |
1959 static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){ | |
1960 char *zRet = 0; | |
1961 if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){ | |
1962 Fts5ExprNearset *pNear = pExpr->pNear; | |
1963 int i; | |
1964 int iTerm; | |
1965 | |
1966 if( pNear->pColset ){ | |
1967 int iCol = pNear->pColset->aiCol[0]; | |
1968 zRet = fts5PrintfAppend(zRet, "%s : ", pConfig->azCol[iCol]); | |
1969 if( zRet==0 ) return 0; | |
1970 } | |
1971 | |
1972 if( pNear->nPhrase>1 ){ | |
1973 zRet = fts5PrintfAppend(zRet, "NEAR("); | |
1974 if( zRet==0 ) return 0; | |
1975 } | |
1976 | |
1977 for(i=0; i<pNear->nPhrase; i++){ | |
1978 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; | |
1979 if( i!=0 ){ | |
1980 zRet = fts5PrintfAppend(zRet, " "); | |
1981 if( zRet==0 ) return 0; | |
1982 } | |
1983 for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){ | |
1984 char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]); | |
1985 if( zTerm ){ | |
1986 zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm); | |
1987 sqlite3_free(zTerm); | |
1988 } | |
1989 if( zTerm==0 || zRet==0 ){ | |
1990 sqlite3_free(zRet); | |
1991 return 0; | |
1992 } | |
1993 } | |
1994 } | |
1995 | |
1996 if( pNear->nPhrase>1 ){ | |
1997 zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear); | |
1998 if( zRet==0 ) return 0; | |
1999 } | |
2000 | |
2001 }else{ | |
2002 char const *zOp = 0; | |
2003 int i; | |
2004 | |
2005 switch( pExpr->eType ){ | |
2006 case FTS5_AND: zOp = " AND "; break; | |
2007 case FTS5_NOT: zOp = " NOT "; break; | |
2008 default: | |
2009 assert( pExpr->eType==FTS5_OR ); | |
2010 zOp = " OR "; | |
2011 break; | |
2012 } | |
2013 | |
2014 for(i=0; i<pExpr->nChild; i++){ | |
2015 char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]); | |
2016 if( z==0 ){ | |
2017 sqlite3_free(zRet); | |
2018 zRet = 0; | |
2019 }else{ | |
2020 int e = pExpr->apChild[i]->eType; | |
2021 int b = (e!=FTS5_STRING && e!=FTS5_TERM); | |
2022 zRet = fts5PrintfAppend(zRet, "%s%s%z%s", | |
2023 (i==0 ? "" : zOp), | |
2024 (b?"(":""), z, (b?")":"") | |
2025 ); | |
2026 } | |
2027 if( zRet==0 ) break; | |
2028 } | |
2029 } | |
2030 | |
2031 return zRet; | |
2032 } | |
2033 | |
2034 /* | |
2035 ** The implementation of user-defined scalar functions fts5_expr() (bTcl==0) | |
2036 ** and fts5_expr_tcl() (bTcl!=0). | |
2037 */ | |
2038 static void fts5ExprFunction( | |
2039 sqlite3_context *pCtx, /* Function call context */ | |
2040 int nArg, /* Number of args */ | |
2041 sqlite3_value **apVal, /* Function arguments */ | |
2042 int bTcl | |
2043 ){ | |
2044 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx); | |
2045 sqlite3 *db = sqlite3_context_db_handle(pCtx); | |
2046 const char *zExpr = 0; | |
2047 char *zErr = 0; | |
2048 Fts5Expr *pExpr = 0; | |
2049 int rc; | |
2050 int i; | |
2051 | |
2052 const char **azConfig; /* Array of arguments for Fts5Config */ | |
2053 const char *zNearsetCmd = "nearset"; | |
2054 int nConfig; /* Size of azConfig[] */ | |
2055 Fts5Config *pConfig = 0; | |
2056 int iArg = 1; | |
2057 | |
2058 if( nArg<1 ){ | |
2059 zErr = sqlite3_mprintf("wrong number of arguments to function %s", | |
2060 bTcl ? "fts5_expr_tcl" : "fts5_expr" | |
2061 ); | |
2062 sqlite3_result_error(pCtx, zErr, -1); | |
2063 sqlite3_free(zErr); | |
2064 return; | |
2065 } | |
2066 | |
2067 if( bTcl && nArg>1 ){ | |
2068 zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]); | |
2069 iArg = 2; | |
2070 } | |
2071 | |
2072 nConfig = 3 + (nArg-iArg); | |
2073 azConfig = (const char**)sqlite3_malloc(sizeof(char*) * nConfig); | |
2074 if( azConfig==0 ){ | |
2075 sqlite3_result_error_nomem(pCtx); | |
2076 return; | |
2077 } | |
2078 azConfig[0] = 0; | |
2079 azConfig[1] = "main"; | |
2080 azConfig[2] = "tbl"; | |
2081 for(i=3; iArg<nArg; iArg++){ | |
2082 azConfig[i++] = (const char*)sqlite3_value_text(apVal[iArg]); | |
2083 } | |
2084 | |
2085 zExpr = (const char*)sqlite3_value_text(apVal[0]); | |
2086 | |
2087 rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr); | |
2088 if( rc==SQLITE_OK ){ | |
2089 rc = sqlite3Fts5ExprNew(pConfig, zExpr, &pExpr, &zErr); | |
2090 } | |
2091 if( rc==SQLITE_OK ){ | |
2092 char *zText; | |
2093 if( pExpr->pRoot==0 ){ | |
2094 zText = sqlite3_mprintf(""); | |
2095 }else if( bTcl ){ | |
2096 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot); | |
2097 }else{ | |
2098 zText = fts5ExprPrint(pConfig, pExpr->pRoot); | |
2099 } | |
2100 if( zText==0 ){ | |
2101 rc = SQLITE_NOMEM; | |
2102 }else{ | |
2103 sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT); | |
2104 sqlite3_free(zText); | |
2105 } | |
2106 } | |
2107 | |
2108 if( rc!=SQLITE_OK ){ | |
2109 if( zErr ){ | |
2110 sqlite3_result_error(pCtx, zErr, -1); | |
2111 sqlite3_free(zErr); | |
2112 }else{ | |
2113 sqlite3_result_error_code(pCtx, rc); | |
2114 } | |
2115 } | |
2116 sqlite3_free((void *)azConfig); | |
2117 sqlite3Fts5ConfigFree(pConfig); | |
2118 sqlite3Fts5ExprFree(pExpr); | |
2119 } | |
2120 | |
2121 static void fts5ExprFunctionHr( | |
2122 sqlite3_context *pCtx, /* Function call context */ | |
2123 int nArg, /* Number of args */ | |
2124 sqlite3_value **apVal /* Function arguments */ | |
2125 ){ | |
2126 fts5ExprFunction(pCtx, nArg, apVal, 0); | |
2127 } | |
2128 static void fts5ExprFunctionTcl( | |
2129 sqlite3_context *pCtx, /* Function call context */ | |
2130 int nArg, /* Number of args */ | |
2131 sqlite3_value **apVal /* Function arguments */ | |
2132 ){ | |
2133 fts5ExprFunction(pCtx, nArg, apVal, 1); | |
2134 } | |
2135 | |
2136 /* | |
2137 ** The implementation of an SQLite user-defined-function that accepts a | |
2138 ** single integer as an argument. If the integer is an alpha-numeric | |
2139 ** unicode code point, 1 is returned. Otherwise 0. | |
2140 */ | |
2141 static void fts5ExprIsAlnum( | |
2142 sqlite3_context *pCtx, /* Function call context */ | |
2143 int nArg, /* Number of args */ | |
2144 sqlite3_value **apVal /* Function arguments */ | |
2145 ){ | |
2146 int iCode; | |
2147 if( nArg!=1 ){ | |
2148 sqlite3_result_error(pCtx, | |
2149 "wrong number of arguments to function fts5_isalnum", -1 | |
2150 ); | |
2151 return; | |
2152 } | |
2153 iCode = sqlite3_value_int(apVal[0]); | |
2154 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeIsalnum(iCode)); | |
2155 } | |
2156 | |
2157 static void fts5ExprFold( | |
2158 sqlite3_context *pCtx, /* Function call context */ | |
2159 int nArg, /* Number of args */ | |
2160 sqlite3_value **apVal /* Function arguments */ | |
2161 ){ | |
2162 if( nArg!=1 && nArg!=2 ){ | |
2163 sqlite3_result_error(pCtx, | |
2164 "wrong number of arguments to function fts5_fold", -1 | |
2165 ); | |
2166 }else{ | |
2167 int iCode; | |
2168 int bRemoveDiacritics = 0; | |
2169 iCode = sqlite3_value_int(apVal[0]); | |
2170 if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]); | |
2171 sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics)); | |
2172 } | |
2173 } | |
2174 | |
2175 /* | |
2176 ** This is called during initialization to register the fts5_expr() scalar | |
2177 ** UDF with the SQLite handle passed as the only argument. | |
2178 */ | |
2179 int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){ | |
2180 struct Fts5ExprFunc { | |
2181 const char *z; | |
2182 void (*x)(sqlite3_context*,int,sqlite3_value**); | |
2183 } aFunc[] = { | |
2184 { "fts5_expr", fts5ExprFunctionHr }, | |
2185 { "fts5_expr_tcl", fts5ExprFunctionTcl }, | |
2186 { "fts5_isalnum", fts5ExprIsAlnum }, | |
2187 { "fts5_fold", fts5ExprFold }, | |
2188 }; | |
2189 int i; | |
2190 int rc = SQLITE_OK; | |
2191 void *pCtx = (void*)pGlobal; | |
2192 | |
2193 for(i=0; rc==SQLITE_OK && i<(int)ArraySize(aFunc); i++){ | |
2194 struct Fts5ExprFunc *p = &aFunc[i]; | |
2195 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0); | |
2196 } | |
2197 | |
2198 /* Avoid a warning indicating that sqlite3Fts5ParserTrace() is unused */ | |
2199 #ifndef NDEBUG | |
2200 (void)sqlite3Fts5ParserTrace; | |
2201 #endif | |
2202 | |
2203 return rc; | |
2204 } | |
2205 | |
2206 /* | |
2207 ** Return the number of phrases in expression pExpr. | |
2208 */ | |
2209 int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){ | |
2210 return (pExpr ? pExpr->nPhrase : 0); | |
2211 } | |
2212 | |
2213 /* | |
2214 ** Return the number of terms in the iPhrase'th phrase in pExpr. | |
2215 */ | |
2216 int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){ | |
2217 if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0; | |
2218 return pExpr->apExprPhrase[iPhrase]->nTerm; | |
2219 } | |
2220 | |
2221 /* | |
2222 ** This function is used to access the current position list for phrase | |
2223 ** iPhrase. | |
2224 */ | |
2225 int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){ | |
2226 int nRet; | |
2227 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase]; | |
2228 Fts5ExprNode *pNode = pPhrase->pNode; | |
2229 if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){ | |
2230 *pa = pPhrase->poslist.p; | |
2231 nRet = pPhrase->poslist.n; | |
2232 }else{ | |
2233 *pa = 0; | |
2234 nRet = 0; | |
2235 } | |
2236 return nRet; | |
2237 } | |
OLD | NEW |