fts5_expr.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278
  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. #include "fts5Int.h"
  15. #include "fts5parse.h"
  16. #ifndef SQLITE_FTS5_MAX_EXPR_DEPTH
  17. # define SQLITE_FTS5_MAX_EXPR_DEPTH 256
  18. #endif
  19. /*
  20. ** All token types in the generated fts5parse.h file are greater than 0.
  21. */
  22. #define FTS5_EOF 0
  23. #define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32))
  24. typedef struct Fts5ExprTerm Fts5ExprTerm;
  25. /*
  26. ** Functions generated by lemon from fts5parse.y.
  27. */
  28. void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64));
  29. void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*));
  30. void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*);
  31. #ifndef NDEBUG
  32. #include <stdio.h>
  33. void sqlite3Fts5ParserTrace(FILE*, char*);
  34. #endif
  35. int sqlite3Fts5ParserFallback(int);
  36. struct Fts5Expr {
  37. Fts5Index *pIndex;
  38. Fts5Config *pConfig;
  39. Fts5ExprNode *pRoot;
  40. int bDesc; /* Iterate in descending rowid order */
  41. int nPhrase; /* Number of phrases in expression */
  42. Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */
  43. };
  44. /*
  45. ** eType:
  46. ** Expression node type. Usually one of:
  47. **
  48. ** FTS5_AND (nChild, apChild valid)
  49. ** FTS5_OR (nChild, apChild valid)
  50. ** FTS5_NOT (nChild, apChild valid)
  51. ** FTS5_STRING (pNear valid)
  52. ** FTS5_TERM (pNear valid)
  53. **
  54. ** An expression node with eType==0 may also exist. It always matches zero
  55. ** rows. This is created when a phrase containing no tokens is parsed.
  56. ** e.g. "".
  57. **
  58. ** iHeight:
  59. ** Distance from this node to furthest leaf. This is always 0 for nodes
  60. ** of type FTS5_STRING and FTS5_TERM. For all other nodes it is one
  61. ** greater than the largest child value.
  62. */
  63. struct Fts5ExprNode {
  64. int eType; /* Node type */
  65. int bEof; /* True at EOF */
  66. int bNomatch; /* True if entry is not a match */
  67. int iHeight; /* Distance to tree leaf nodes */
  68. /* Next method for this node. */
  69. int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
  70. i64 iRowid; /* Current rowid */
  71. Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
  72. /* Child nodes. For a NOT node, this array always contains 2 entries. For
  73. ** AND or OR nodes, it contains 2 or more entries. */
  74. int nChild; /* Number of child nodes */
  75. Fts5ExprNode *apChild[FLEXARRAY]; /* Array of child nodes */
  76. };
  77. /* Size (in bytes) of an Fts5ExprNode object that holds up to N children */
  78. #define SZ_FTS5EXPRNODE(N) \
  79. (offsetof(Fts5ExprNode,apChild) + (N)*sizeof(Fts5ExprNode*))
  80. #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
  81. /*
  82. ** Invoke the xNext method of an Fts5ExprNode object. This macro should be
  83. ** used as if it has the same signature as the xNext() methods themselves.
  84. */
  85. #define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
  86. /*
  87. ** An instance of the following structure represents a single search term
  88. ** or term prefix.
  89. */
  90. struct Fts5ExprTerm {
  91. u8 bPrefix; /* True for a prefix term */
  92. u8 bFirst; /* True if token must be first in column */
  93. char *pTerm; /* Term data */
  94. int nQueryTerm; /* Effective size of term in bytes */
  95. int nFullTerm; /* Size of term in bytes incl. tokendata */
  96. Fts5IndexIter *pIter; /* Iterator for this term */
  97. Fts5ExprTerm *pSynonym; /* Pointer to first in list of synonyms */
  98. };
  99. /*
  100. ** A phrase. One or more terms that must appear in a contiguous sequence
  101. ** within a document for it to match.
  102. */
  103. struct Fts5ExprPhrase {
  104. Fts5ExprNode *pNode; /* FTS5_STRING node this phrase is part of */
  105. Fts5Buffer poslist; /* Current position list */
  106. int nTerm; /* Number of entries in aTerm[] */
  107. Fts5ExprTerm aTerm[FLEXARRAY]; /* Terms that make up this phrase */
  108. };
  109. /* Size (in bytes) of an Fts5ExprPhrase object that holds up to N terms */
  110. #define SZ_FTS5EXPRPHRASE(N) \
  111. (offsetof(Fts5ExprPhrase,aTerm) + (N)*sizeof(Fts5ExprTerm))
  112. /*
  113. ** One or more phrases that must appear within a certain token distance of
  114. ** each other within each matching document.
  115. */
  116. struct Fts5ExprNearset {
  117. int nNear; /* NEAR parameter */
  118. Fts5Colset *pColset; /* Columns to search (NULL -> all columns) */
  119. int nPhrase; /* Number of entries in aPhrase[] array */
  120. Fts5ExprPhrase *apPhrase[FLEXARRAY]; /* Array of phrase pointers */
  121. };
  122. /* Size (in bytes) of an Fts5ExprNearset object covering up to N phrases */
  123. #define SZ_FTS5EXPRNEARSET(N) \
  124. (offsetof(Fts5ExprNearset,apPhrase)+(N)*sizeof(Fts5ExprPhrase*))
  125. /*
  126. ** Parse context.
  127. */
  128. struct Fts5Parse {
  129. Fts5Config *pConfig;
  130. char *zErr;
  131. int rc;
  132. int nPhrase; /* Size of apPhrase array */
  133. Fts5ExprPhrase **apPhrase; /* Array of all phrases */
  134. Fts5ExprNode *pExpr; /* Result of a successful parse */
  135. int bPhraseToAnd; /* Convert "a+b" to "a AND b" */
  136. };
  137. /*
  138. ** Check that the Fts5ExprNode.iHeight variables are set correctly in
  139. ** the expression tree passed as the only argument.
  140. */
  141. #ifndef NDEBUG
  142. static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){
  143. if( rc==SQLITE_OK ){
  144. if( p->eType==FTS5_TERM || p->eType==FTS5_STRING || p->eType==0 ){
  145. assert( p->iHeight==0 );
  146. }else{
  147. int ii;
  148. int iMaxChild = 0;
  149. for(ii=0; ii<p->nChild; ii++){
  150. Fts5ExprNode *pChild = p->apChild[ii];
  151. iMaxChild = MAX(iMaxChild, pChild->iHeight);
  152. assert_expr_depth_ok(SQLITE_OK, pChild);
  153. }
  154. assert( p->iHeight==iMaxChild+1 );
  155. }
  156. }
  157. }
  158. #else
  159. # define assert_expr_depth_ok(rc, p)
  160. #endif
  161. void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){
  162. va_list ap;
  163. va_start(ap, zFmt);
  164. if( pParse->rc==SQLITE_OK ){
  165. assert( pParse->zErr==0 );
  166. pParse->zErr = sqlite3_vmprintf(zFmt, ap);
  167. pParse->rc = SQLITE_ERROR;
  168. }
  169. va_end(ap);
  170. }
  171. static int fts5ExprIsspace(char t){
  172. return t==' ' || t=='\t' || t=='\n' || t=='\r';
  173. }
  174. /*
  175. ** Read the first token from the nul-terminated string at *pz.
  176. */
  177. static int fts5ExprGetToken(
  178. Fts5Parse *pParse,
  179. const char **pz, /* IN/OUT: Pointer into buffer */
  180. Fts5Token *pToken
  181. ){
  182. const char *z = *pz;
  183. int tok;
  184. /* Skip past any whitespace */
  185. while( fts5ExprIsspace(*z) ) z++;
  186. pToken->p = z;
  187. pToken->n = 1;
  188. switch( *z ){
  189. case '(': tok = FTS5_LP; break;
  190. case ')': tok = FTS5_RP; break;
  191. case '{': tok = FTS5_LCP; break;
  192. case '}': tok = FTS5_RCP; break;
  193. case ':': tok = FTS5_COLON; break;
  194. case ',': tok = FTS5_COMMA; break;
  195. case '+': tok = FTS5_PLUS; break;
  196. case '*': tok = FTS5_STAR; break;
  197. case '-': tok = FTS5_MINUS; break;
  198. case '^': tok = FTS5_CARET; break;
  199. case '\0': tok = FTS5_EOF; break;
  200. case '"': {
  201. const char *z2;
  202. tok = FTS5_STRING;
  203. for(z2=&z[1]; 1; z2++){
  204. if( z2[0]=='"' ){
  205. z2++;
  206. if( z2[0]!='"' ) break;
  207. }
  208. if( z2[0]=='\0' ){
  209. sqlite3Fts5ParseError(pParse, "unterminated string");
  210. return FTS5_EOF;
  211. }
  212. }
  213. pToken->n = (z2 - z);
  214. break;
  215. }
  216. default: {
  217. const char *z2;
  218. if( sqlite3Fts5IsBareword(z[0])==0 ){
  219. sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z);
  220. return FTS5_EOF;
  221. }
  222. tok = FTS5_STRING;
  223. for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++);
  224. pToken->n = (z2 - z);
  225. if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR;
  226. if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT;
  227. if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND;
  228. break;
  229. }
  230. }
  231. *pz = &pToken->p[pToken->n];
  232. return tok;
  233. }
  234. static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);}
  235. static void fts5ParseFree(void *p){ sqlite3_free(p); }
  236. int sqlite3Fts5ExprNew(
  237. Fts5Config *pConfig, /* FTS5 Configuration */
  238. int bPhraseToAnd,
  239. int iCol,
  240. const char *zExpr, /* Expression text */
  241. Fts5Expr **ppNew,
  242. char **pzErr
  243. ){
  244. Fts5Parse sParse;
  245. Fts5Token token;
  246. const char *z = zExpr;
  247. int t; /* Next token type */
  248. void *pEngine;
  249. Fts5Expr *pNew;
  250. *ppNew = 0;
  251. *pzErr = 0;
  252. memset(&sParse, 0, sizeof(sParse));
  253. sParse.bPhraseToAnd = bPhraseToAnd;
  254. pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc);
  255. if( pEngine==0 ){ return SQLITE_NOMEM; }
  256. sParse.pConfig = pConfig;
  257. do {
  258. t = fts5ExprGetToken(&sParse, &z, &token);
  259. sqlite3Fts5Parser(pEngine, t, token, &sParse);
  260. }while( sParse.rc==SQLITE_OK && t!=FTS5_EOF );
  261. sqlite3Fts5ParserFree(pEngine, fts5ParseFree);
  262. assert( sParse.pExpr || sParse.rc!=SQLITE_OK );
  263. assert_expr_depth_ok(sParse.rc, sParse.pExpr);
  264. /* If the LHS of the MATCH expression was a user column, apply the
  265. ** implicit column-filter. */
  266. if( sParse.rc==SQLITE_OK && iCol<pConfig->nCol ){
  267. int n = SZ_FTS5COLSET(1);
  268. Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n);
  269. if( pColset ){
  270. pColset->nCol = 1;
  271. pColset->aiCol[0] = iCol;
  272. sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset);
  273. }
  274. }
  275. assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 );
  276. if( sParse.rc==SQLITE_OK ){
  277. *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
  278. if( pNew==0 ){
  279. sParse.rc = SQLITE_NOMEM;
  280. sqlite3Fts5ParseNodeFree(sParse.pExpr);
  281. }else{
  282. pNew->pRoot = sParse.pExpr;
  283. pNew->pIndex = 0;
  284. pNew->pConfig = pConfig;
  285. pNew->apExprPhrase = sParse.apPhrase;
  286. pNew->nPhrase = sParse.nPhrase;
  287. pNew->bDesc = 0;
  288. sParse.apPhrase = 0;
  289. }
  290. }else{
  291. sqlite3Fts5ParseNodeFree(sParse.pExpr);
  292. }
  293. sqlite3_free(sParse.apPhrase);
  294. if( 0==*pzErr ){
  295. *pzErr = sParse.zErr;
  296. }else{
  297. sqlite3_free(sParse.zErr);
  298. }
  299. return sParse.rc;
  300. }
  301. /*
  302. ** Assuming that buffer z is at least nByte bytes in size and contains a
  303. ** valid utf-8 string, return the number of characters in the string.
  304. */
  305. static int fts5ExprCountChar(const char *z, int nByte){
  306. int nRet = 0;
  307. int ii;
  308. for(ii=0; ii<nByte; ii++){
  309. if( (z[ii] & 0xC0)!=0x80 ) nRet++;
  310. }
  311. return nRet;
  312. }
  313. /*
  314. ** This function is only called when using the special 'trigram' tokenizer.
  315. ** Argument zText contains the text of a LIKE or GLOB pattern matched
  316. ** against column iCol. This function creates and compiles an FTS5 MATCH
  317. ** expression that will match a superset of the rows matched by the LIKE or
  318. ** GLOB. If successful, SQLITE_OK is returned. Otherwise, an SQLite error
  319. ** code.
  320. */
  321. int sqlite3Fts5ExprPattern(
  322. Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp
  323. ){
  324. i64 nText = strlen(zText);
  325. char *zExpr = (char*)sqlite3_malloc64(nText*4 + 1);
  326. int rc = SQLITE_OK;
  327. if( zExpr==0 ){
  328. rc = SQLITE_NOMEM;
  329. }else{
  330. char aSpec[3];
  331. int iOut = 0;
  332. int i = 0;
  333. int iFirst = 0;
  334. if( bGlob==0 ){
  335. aSpec[0] = '_';
  336. aSpec[1] = '%';
  337. aSpec[2] = 0;
  338. }else{
  339. aSpec[0] = '*';
  340. aSpec[1] = '?';
  341. aSpec[2] = '[';
  342. }
  343. while( i<=nText ){
  344. if( i==nText
  345. || zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2]
  346. ){
  347. if( fts5ExprCountChar(&zText[iFirst], i-iFirst)>=3 ){
  348. int jj;
  349. zExpr[iOut++] = '"';
  350. for(jj=iFirst; jj<i; jj++){
  351. zExpr[iOut++] = zText[jj];
  352. if( zText[jj]=='"' ) zExpr[iOut++] = '"';
  353. }
  354. zExpr[iOut++] = '"';
  355. zExpr[iOut++] = ' ';
  356. }
  357. if( zText[i]==aSpec[2] ){
  358. i += 2;
  359. if( zText[i-1]=='^' ) i++;
  360. while( i<nText && zText[i]!=']' ) i++;
  361. }
  362. iFirst = i+1;
  363. }
  364. i++;
  365. }
  366. if( iOut>0 ){
  367. int bAnd = 0;
  368. if( pConfig->eDetail!=FTS5_DETAIL_FULL ){
  369. bAnd = 1;
  370. if( pConfig->eDetail==FTS5_DETAIL_NONE ){
  371. iCol = pConfig->nCol;
  372. }
  373. }
  374. zExpr[iOut] = '\0';
  375. rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg);
  376. }else{
  377. *pp = 0;
  378. }
  379. sqlite3_free(zExpr);
  380. }
  381. return rc;
  382. }
  383. /*
  384. ** Free the expression node object passed as the only argument.
  385. */
  386. void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){
  387. if( p ){
  388. int i;
  389. for(i=0; i<p->nChild; i++){
  390. sqlite3Fts5ParseNodeFree(p->apChild[i]);
  391. }
  392. sqlite3Fts5ParseNearsetFree(p->pNear);
  393. sqlite3_free(p);
  394. }
  395. }
  396. /*
  397. ** Free the expression object passed as the only argument.
  398. */
  399. void sqlite3Fts5ExprFree(Fts5Expr *p){
  400. if( p ){
  401. sqlite3Fts5ParseNodeFree(p->pRoot);
  402. sqlite3_free(p->apExprPhrase);
  403. sqlite3_free(p);
  404. }
  405. }
  406. int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
  407. Fts5Parse sParse;
  408. memset(&sParse, 0, sizeof(sParse));
  409. if( *pp1 && p2 ){
  410. Fts5Expr *p1 = *pp1;
  411. int nPhrase = p1->nPhrase + p2->nPhrase;
  412. p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0);
  413. p2->pRoot = 0;
  414. if( sParse.rc==SQLITE_OK ){
  415. Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
  416. p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
  417. );
  418. if( ap==0 ){
  419. sParse.rc = SQLITE_NOMEM;
  420. }else{
  421. int i;
  422. memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
  423. for(i=0; i<p2->nPhrase; i++){
  424. ap[i] = p2->apExprPhrase[i];
  425. }
  426. p1->nPhrase = nPhrase;
  427. p1->apExprPhrase = ap;
  428. }
  429. }
  430. sqlite3_free(p2->apExprPhrase);
  431. sqlite3_free(p2);
  432. }else if( p2 ){
  433. *pp1 = p2;
  434. }
  435. return sParse.rc;
  436. }
  437. /*
  438. ** Argument pTerm must be a synonym iterator. Return the current rowid
  439. ** that it points to.
  440. */
  441. static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
  442. i64 iRet = 0;
  443. int bRetValid = 0;
  444. Fts5ExprTerm *p;
  445. assert( pTerm );
  446. assert( pTerm->pSynonym );
  447. assert( bDesc==0 || bDesc==1 );
  448. for(p=pTerm; p; p=p->pSynonym){
  449. if( 0==sqlite3Fts5IterEof(p->pIter) ){
  450. i64 iRowid = p->pIter->iRowid;
  451. if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
  452. iRet = iRowid;
  453. bRetValid = 1;
  454. }
  455. }
  456. }
  457. if( pbEof && bRetValid==0 ) *pbEof = 1;
  458. return iRet;
  459. }
  460. /*
  461. ** Argument pTerm must be a synonym iterator.
  462. */
  463. static int fts5ExprSynonymList(
  464. Fts5ExprTerm *pTerm,
  465. i64 iRowid,
  466. Fts5Buffer *pBuf, /* Use this buffer for space if required */
  467. u8 **pa, int *pn
  468. ){
  469. Fts5PoslistReader aStatic[4];
  470. Fts5PoslistReader *aIter = aStatic;
  471. int nIter = 0;
  472. int nAlloc = 4;
  473. int rc = SQLITE_OK;
  474. Fts5ExprTerm *p;
  475. assert( pTerm->pSynonym );
  476. for(p=pTerm; p; p=p->pSynonym){
  477. Fts5IndexIter *pIter = p->pIter;
  478. if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
  479. if( pIter->nData==0 ) continue;
  480. if( nIter==nAlloc ){
  481. sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
  482. Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
  483. if( aNew==0 ){
  484. rc = SQLITE_NOMEM;
  485. goto synonym_poslist_out;
  486. }
  487. memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
  488. nAlloc = nAlloc*2;
  489. if( aIter!=aStatic ) sqlite3_free(aIter);
  490. aIter = aNew;
  491. }
  492. sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
  493. assert( aIter[nIter].bEof==0 );
  494. nIter++;
  495. }
  496. }
  497. if( nIter==1 ){
  498. *pa = (u8*)aIter[0].a;
  499. *pn = aIter[0].n;
  500. }else{
  501. Fts5PoslistWriter writer = {0};
  502. i64 iPrev = -1;
  503. fts5BufferZero(pBuf);
  504. while( 1 ){
  505. int i;
  506. i64 iMin = FTS5_LARGEST_INT64;
  507. for(i=0; i<nIter; i++){
  508. if( aIter[i].bEof==0 ){
  509. if( aIter[i].iPos==iPrev ){
  510. if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue;
  511. }
  512. if( aIter[i].iPos<iMin ){
  513. iMin = aIter[i].iPos;
  514. }
  515. }
  516. }
  517. if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
  518. rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
  519. iPrev = iMin;
  520. }
  521. if( rc==SQLITE_OK ){
  522. *pa = pBuf->p;
  523. *pn = pBuf->n;
  524. }
  525. }
  526. synonym_poslist_out:
  527. if( aIter!=aStatic ) sqlite3_free(aIter);
  528. return rc;
  529. }
  530. /*
  531. ** All individual term iterators in pPhrase are guaranteed to be valid and
  532. ** pointing to the same rowid when this function is called. This function
  533. ** checks if the current rowid really is a match, and if so populates
  534. ** the pPhrase->poslist buffer accordingly. Output parameter *pbMatch
  535. ** is set to true if this is really a match, or false otherwise.
  536. **
  537. ** SQLITE_OK is returned if an error occurs, or an SQLite error code
  538. ** otherwise. It is not considered an error code if the current rowid is
  539. ** not a match.
  540. */
  541. static int fts5ExprPhraseIsMatch(
  542. Fts5ExprNode *pNode, /* Node pPhrase belongs to */
  543. Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
  544. int *pbMatch /* OUT: Set to true if really a match */
  545. ){
  546. Fts5PoslistWriter writer = {0};
  547. Fts5PoslistReader aStatic[4];
  548. Fts5PoslistReader *aIter = aStatic;
  549. int i;
  550. int rc = SQLITE_OK;
  551. int bFirst = pPhrase->aTerm[0].bFirst;
  552. fts5BufferZero(&pPhrase->poslist);
  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( pPhrase->nTerm>ArraySize(aStatic) ){
  556. sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
  557. aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte);
  558. if( !aIter ) return SQLITE_NOMEM;
  559. }
  560. memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
  561. /* Initialize a term iterator for each term in the phrase */
  562. for(i=0; i<pPhrase->nTerm; i++){
  563. Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
  564. int n = 0;
  565. int bFlag = 0;
  566. u8 *a = 0;
  567. if( pTerm->pSynonym ){
  568. Fts5Buffer buf = {0, 0, 0};
  569. rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
  570. if( rc ){
  571. sqlite3_free(a);
  572. goto ismatch_out;
  573. }
  574. if( a==buf.p ) bFlag = 1;
  575. }else{
  576. a = (u8*)pTerm->pIter->pData;
  577. n = pTerm->pIter->nData;
  578. }
  579. sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
  580. aIter[i].bFlag = (u8)bFlag;
  581. if( aIter[i].bEof ) goto ismatch_out;
  582. }
  583. while( 1 ){
  584. int bMatch;
  585. i64 iPos = aIter[0].iPos;
  586. do {
  587. bMatch = 1;
  588. for(i=0; i<pPhrase->nTerm; i++){
  589. Fts5PoslistReader *pPos = &aIter[i];
  590. i64 iAdj = iPos + i;
  591. if( pPos->iPos!=iAdj ){
  592. bMatch = 0;
  593. while( pPos->iPos<iAdj ){
  594. if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out;
  595. }
  596. if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
  597. }
  598. }
  599. }while( bMatch==0 );
  600. /* Append position iPos to the output */
  601. if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
  602. rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
  603. if( rc!=SQLITE_OK ) goto ismatch_out;
  604. }
  605. for(i=0; i<pPhrase->nTerm; i++){
  606. if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
  607. }
  608. }
  609. ismatch_out:
  610. *pbMatch = (pPhrase->poslist.n>0);
  611. for(i=0; i<pPhrase->nTerm; i++){
  612. if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a);
  613. }
  614. if( aIter!=aStatic ) sqlite3_free(aIter);
  615. return rc;
  616. }
  617. typedef struct Fts5LookaheadReader Fts5LookaheadReader;
  618. struct Fts5LookaheadReader {
  619. const u8 *a; /* Buffer containing position list */
  620. int n; /* Size of buffer a[] in bytes */
  621. int i; /* Current offset in position list */
  622. i64 iPos; /* Current position */
  623. i64 iLookahead; /* Next position */
  624. };
  625. #define FTS5_LOOKAHEAD_EOF (((i64)1) << 62)
  626. static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){
  627. p->iPos = p->iLookahead;
  628. if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){
  629. p->iLookahead = FTS5_LOOKAHEAD_EOF;
  630. }
  631. return (p->iPos==FTS5_LOOKAHEAD_EOF);
  632. }
  633. static int fts5LookaheadReaderInit(
  634. const u8 *a, int n, /* Buffer to read position list from */
  635. Fts5LookaheadReader *p /* Iterator object to initialize */
  636. ){
  637. memset(p, 0, sizeof(Fts5LookaheadReader));
  638. p->a = a;
  639. p->n = n;
  640. fts5LookaheadReaderNext(p);
  641. return fts5LookaheadReaderNext(p);
  642. }
  643. typedef struct Fts5NearTrimmer Fts5NearTrimmer;
  644. struct Fts5NearTrimmer {
  645. Fts5LookaheadReader reader; /* Input iterator */
  646. Fts5PoslistWriter writer; /* Writer context */
  647. Fts5Buffer *pOut; /* Output poslist */
  648. };
  649. /*
  650. ** The near-set object passed as the first argument contains more than
  651. ** one phrase. All phrases currently point to the same row. The
  652. ** Fts5ExprPhrase.poslist buffers are populated accordingly. This function
  653. ** tests if the current row contains instances of each phrase sufficiently
  654. ** close together to meet the NEAR constraint. Non-zero is returned if it
  655. ** does, or zero otherwise.
  656. **
  657. ** If in/out parameter (*pRc) is set to other than SQLITE_OK when this
  658. ** function is called, it is a no-op. Or, if an error (e.g. SQLITE_NOMEM)
  659. ** occurs within this function (*pRc) is set accordingly before returning.
  660. ** The return value is undefined in both these cases.
  661. **
  662. ** If no error occurs and non-zero (a match) is returned, the position-list
  663. ** of each phrase object is edited to contain only those entries that
  664. ** meet the constraint before returning.
  665. */
  666. static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){
  667. Fts5NearTrimmer aStatic[4];
  668. Fts5NearTrimmer *a = aStatic;
  669. Fts5ExprPhrase **apPhrase = pNear->apPhrase;
  670. int i;
  671. int rc = *pRc;
  672. int bMatch;
  673. assert( pNear->nPhrase>1 );
  674. /* If the aStatic[] array is not large enough, allocate a large array
  675. ** using sqlite3_malloc(). This approach could be improved upon. */
  676. if( pNear->nPhrase>ArraySize(aStatic) ){
  677. sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
  678. a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
  679. }else{
  680. memset(aStatic, 0, sizeof(aStatic));
  681. }
  682. if( rc!=SQLITE_OK ){
  683. *pRc = rc;
  684. return 0;
  685. }
  686. /* Initialize a lookahead iterator for each phrase. After passing the
  687. ** buffer and buffer size to the lookaside-reader init function, zero
  688. ** the phrase poslist buffer. The new poslist for the phrase (containing
  689. ** the same entries as the original with some entries removed on account
  690. ** of the NEAR constraint) is written over the original even as it is
  691. ** being read. This is safe as the entries for the new poslist are a
  692. ** subset of the old, so it is not possible for data yet to be read to
  693. ** be overwritten. */
  694. for(i=0; i<pNear->nPhrase; i++){
  695. Fts5Buffer *pPoslist = &apPhrase[i]->poslist;
  696. fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader);
  697. pPoslist->n = 0;
  698. a[i].pOut = pPoslist;
  699. }
  700. while( 1 ){
  701. int iAdv;
  702. i64 iMin;
  703. i64 iMax;
  704. /* This block advances the phrase iterators until they point to a set of
  705. ** entries that together comprise a match. */
  706. iMax = a[0].reader.iPos;
  707. do {
  708. bMatch = 1;
  709. for(i=0; i<pNear->nPhrase; i++){
  710. Fts5LookaheadReader *pPos = &a[i].reader;
  711. iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear;
  712. if( pPos->iPos<iMin || pPos->iPos>iMax ){
  713. bMatch = 0;
  714. while( pPos->iPos<iMin ){
  715. if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out;
  716. }
  717. if( pPos->iPos>iMax ) iMax = pPos->iPos;
  718. }
  719. }
  720. }while( bMatch==0 );
  721. /* Add an entry to each output position list */
  722. for(i=0; i<pNear->nPhrase; i++){
  723. i64 iPos = a[i].reader.iPos;
  724. Fts5PoslistWriter *pWriter = &a[i].writer;
  725. if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){
  726. sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos);
  727. }
  728. }
  729. iAdv = 0;
  730. iMin = a[0].reader.iLookahead;
  731. for(i=0; i<pNear->nPhrase; i++){
  732. if( a[i].reader.iLookahead < iMin ){
  733. iMin = a[i].reader.iLookahead;
  734. iAdv = i;
  735. }
  736. }
  737. if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out;
  738. }
  739. ismatch_out: {
  740. int bRet = a[0].pOut->n>0;
  741. *pRc = rc;
  742. if( a!=aStatic ) sqlite3_free(a);
  743. return bRet;
  744. }
  745. }
  746. /*
  747. ** Advance iterator pIter until it points to a value equal to or laster
  748. ** than the initial value of *piLast. If this means the iterator points
  749. ** to a value laster than *piLast, update *piLast to the new lastest value.
  750. **
  751. ** If the iterator reaches EOF, set *pbEof to true before returning. If
  752. ** an error occurs, set *pRc to an error code. If either *pbEof or *pRc
  753. ** are set, return a non-zero value. Otherwise, return zero.
  754. */
  755. static int fts5ExprAdvanceto(
  756. Fts5IndexIter *pIter, /* Iterator to advance */
  757. int bDesc, /* True if iterator is "rowid DESC" */
  758. i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
  759. int *pRc, /* OUT: Error code */
  760. int *pbEof /* OUT: Set to true if EOF */
  761. ){
  762. i64 iLast = *piLast;
  763. i64 iRowid;
  764. iRowid = pIter->iRowid;
  765. if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
  766. int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
  767. if( rc || sqlite3Fts5IterEof(pIter) ){
  768. *pRc = rc;
  769. *pbEof = 1;
  770. return 1;
  771. }
  772. iRowid = pIter->iRowid;
  773. assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
  774. }
  775. *piLast = iRowid;
  776. return 0;
  777. }
  778. static int fts5ExprSynonymAdvanceto(
  779. Fts5ExprTerm *pTerm, /* Term iterator to advance */
  780. int bDesc, /* True if iterator is "rowid DESC" */
  781. i64 *piLast, /* IN/OUT: Lastest rowid seen so far */
  782. int *pRc /* OUT: Error code */
  783. ){
  784. int rc = SQLITE_OK;
  785. i64 iLast = *piLast;
  786. Fts5ExprTerm *p;
  787. int bEof = 0;
  788. for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
  789. if( sqlite3Fts5IterEof(p->pIter)==0 ){
  790. i64 iRowid = p->pIter->iRowid;
  791. if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
  792. rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
  793. }
  794. }
  795. }
  796. if( rc!=SQLITE_OK ){
  797. *pRc = rc;
  798. bEof = 1;
  799. }else{
  800. *piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof);
  801. }
  802. return bEof;
  803. }
  804. static int fts5ExprNearTest(
  805. int *pRc,
  806. Fts5Expr *pExpr, /* Expression that pNear is a part of */
  807. Fts5ExprNode *pNode /* The "NEAR" node (FTS5_STRING) */
  808. ){
  809. Fts5ExprNearset *pNear = pNode->pNear;
  810. int rc = *pRc;
  811. if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){
  812. Fts5ExprTerm *pTerm;
  813. Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
  814. pPhrase->poslist.n = 0;
  815. for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
  816. Fts5IndexIter *pIter = pTerm->pIter;
  817. if( sqlite3Fts5IterEof(pIter)==0 ){
  818. if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
  819. pPhrase->poslist.n = 1;
  820. }
  821. }
  822. }
  823. return pPhrase->poslist.n;
  824. }else{
  825. int i;
  826. /* Check that each phrase in the nearset matches the current row.
  827. ** Populate the pPhrase->poslist buffers at the same time. If any
  828. ** phrase is not a match, break out of the loop early. */
  829. for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
  830. Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
  831. if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym
  832. || pNear->pColset || pPhrase->aTerm[0].bFirst
  833. ){
  834. int bMatch = 0;
  835. rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
  836. if( bMatch==0 ) break;
  837. }else{
  838. Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
  839. fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
  840. }
  841. }
  842. *pRc = rc;
  843. if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
  844. return 1;
  845. }
  846. return 0;
  847. }
  848. }
  849. /*
  850. ** Initialize all term iterators in the pNear object. If any term is found
  851. ** to match no documents at all, return immediately without initializing any
  852. ** further iterators.
  853. **
  854. ** If an error occurs, return an SQLite error code. Otherwise, return
  855. ** SQLITE_OK. It is not considered an error if some term matches zero
  856. ** documents.
  857. */
  858. static int fts5ExprNearInitAll(
  859. Fts5Expr *pExpr,
  860. Fts5ExprNode *pNode
  861. ){
  862. Fts5ExprNearset *pNear = pNode->pNear;
  863. int i;
  864. assert( pNode->bNomatch==0 );
  865. for(i=0; i<pNear->nPhrase; i++){
  866. Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
  867. if( pPhrase->nTerm==0 ){
  868. pNode->bEof = 1;
  869. return SQLITE_OK;
  870. }else{
  871. int j;
  872. for(j=0; j<pPhrase->nTerm; j++){
  873. Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
  874. Fts5ExprTerm *p;
  875. int bHit = 0;
  876. for(p=pTerm; p; p=p->pSynonym){
  877. int rc;
  878. if( p->pIter ){
  879. sqlite3Fts5IterClose(p->pIter);
  880. p->pIter = 0;
  881. }
  882. rc = sqlite3Fts5IndexQuery(
  883. pExpr->pIndex, p->pTerm, p->nQueryTerm,
  884. (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
  885. (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
  886. pNear->pColset,
  887. &p->pIter
  888. );
  889. assert( (rc==SQLITE_OK)==(p->pIter!=0) );
  890. if( rc!=SQLITE_OK ) return rc;
  891. if( 0==sqlite3Fts5IterEof(p->pIter) ){
  892. bHit = 1;
  893. }
  894. }
  895. if( bHit==0 ){
  896. pNode->bEof = 1;
  897. return SQLITE_OK;
  898. }
  899. }
  900. }
  901. }
  902. pNode->bEof = 0;
  903. return SQLITE_OK;
  904. }
  905. /*
  906. ** If pExpr is an ASC iterator, this function returns a value with the
  907. ** same sign as:
  908. **
  909. ** (iLhs - iRhs)
  910. **
  911. ** Otherwise, if this is a DESC iterator, the opposite is returned:
  912. **
  913. ** (iRhs - iLhs)
  914. */
  915. static int fts5RowidCmp(
  916. Fts5Expr *pExpr,
  917. i64 iLhs,
  918. i64 iRhs
  919. ){
  920. assert( pExpr->bDesc==0 || pExpr->bDesc==1 );
  921. if( pExpr->bDesc==0 ){
  922. if( iLhs<iRhs ) return -1;
  923. return (iLhs > iRhs);
  924. }else{
  925. if( iLhs>iRhs ) return -1;
  926. return (iLhs < iRhs);
  927. }
  928. }
  929. static void fts5ExprSetEof(Fts5ExprNode *pNode){
  930. int i;
  931. pNode->bEof = 1;
  932. pNode->bNomatch = 0;
  933. for(i=0; i<pNode->nChild; i++){
  934. fts5ExprSetEof(pNode->apChild[i]);
  935. }
  936. }
  937. static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){
  938. if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
  939. Fts5ExprNearset *pNear = pNode->pNear;
  940. int i;
  941. for(i=0; i<pNear->nPhrase; i++){
  942. Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
  943. pPhrase->poslist.n = 0;
  944. }
  945. }else{
  946. int i;
  947. for(i=0; i<pNode->nChild; i++){
  948. fts5ExprNodeZeroPoslist(pNode->apChild[i]);
  949. }
  950. }
  951. }
  952. /*
  953. ** Compare the values currently indicated by the two nodes as follows:
  954. **
  955. ** res = (*p1) - (*p2)
  956. **
  957. ** Nodes that point to values that come later in the iteration order are
  958. ** considered to be larger. Nodes at EOF are the largest of all.
  959. **
  960. ** This means that if the iteration order is ASC, then numerically larger
  961. ** rowids are considered larger. Or if it is the default DESC, numerically
  962. ** smaller rowids are larger.
  963. */
  964. static int fts5NodeCompare(
  965. Fts5Expr *pExpr,
  966. Fts5ExprNode *p1,
  967. Fts5ExprNode *p2
  968. ){
  969. if( p2->bEof ) return -1;
  970. if( p1->bEof ) return +1;
  971. return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
  972. }
  973. /*
  974. ** All individual term iterators in pNear are guaranteed to be valid when
  975. ** this function is called. This function checks if all term iterators
  976. ** point to the same rowid, and if not, advances them until they do.
  977. ** If an EOF is reached before this happens, *pbEof is set to true before
  978. ** returning.
  979. **
  980. ** SQLITE_OK is returned if an error occurs, or an SQLite error code
  981. ** otherwise. It is not considered an error code if an iterator reaches
  982. ** EOF.
  983. */
  984. static int fts5ExprNodeTest_STRING(
  985. Fts5Expr *pExpr, /* Expression pPhrase belongs to */
  986. Fts5ExprNode *pNode
  987. ){
  988. Fts5ExprNearset *pNear = pNode->pNear;
  989. Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
  990. int rc = SQLITE_OK;
  991. i64 iLast; /* Lastest rowid any iterator points to */
  992. int i, j; /* Phrase and token index, respectively */
  993. int bMatch; /* True if all terms are at the same rowid */
  994. const int bDesc = pExpr->bDesc;
  995. /* Check that this node should not be FTS5_TERM */
  996. assert( pNear->nPhrase>1
  997. || pNear->apPhrase[0]->nTerm>1
  998. || pNear->apPhrase[0]->aTerm[0].pSynonym
  999. || pNear->apPhrase[0]->aTerm[0].bFirst
  1000. );
  1001. /* Initialize iLast, the "lastest" rowid any iterator points to. If the
  1002. ** iterator skips through rowids in the default ascending order, this means
  1003. ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
  1004. ** means the minimum rowid. */
  1005. if( pLeft->aTerm[0].pSynonym ){
  1006. iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
  1007. }else{
  1008. iLast = pLeft->aTerm[0].pIter->iRowid;
  1009. }
  1010. do {
  1011. bMatch = 1;
  1012. for(i=0; i<pNear->nPhrase; i++){
  1013. Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
  1014. for(j=0; j<pPhrase->nTerm; j++){
  1015. Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
  1016. if( pTerm->pSynonym ){
  1017. i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
  1018. if( iRowid==iLast ) continue;
  1019. bMatch = 0;
  1020. if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
  1021. pNode->bNomatch = 0;
  1022. pNode->bEof = 1;
  1023. return rc;
  1024. }
  1025. }else{
  1026. Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
  1027. if( pIter->iRowid==iLast ) continue;
  1028. bMatch = 0;
  1029. if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
  1030. return rc;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. }while( bMatch==0 );
  1036. pNode->iRowid = iLast;
  1037. pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
  1038. assert( pNode->bEof==0 || pNode->bNomatch==0 );
  1039. return rc;
  1040. }
  1041. /*
  1042. ** Advance the first term iterator in the first phrase of pNear. Set output
  1043. ** variable *pbEof to true if it reaches EOF or if an error occurs.
  1044. **
  1045. ** Return SQLITE_OK if successful, or an SQLite error code if an error
  1046. ** occurs.
  1047. */
  1048. static int fts5ExprNodeNext_STRING(
  1049. Fts5Expr *pExpr, /* Expression pPhrase belongs to */
  1050. Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
  1051. int bFromValid,
  1052. i64 iFrom
  1053. ){
  1054. Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
  1055. int rc = SQLITE_OK;
  1056. pNode->bNomatch = 0;
  1057. if( pTerm->pSynonym ){
  1058. int bEof = 1;
  1059. Fts5ExprTerm *p;
  1060. /* Find the firstest rowid any synonym points to. */
  1061. i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
  1062. /* Advance each iterator that currently points to iRowid. Or, if iFrom
  1063. ** is valid - each iterator that points to a rowid before iFrom. */
  1064. for(p=pTerm; p; p=p->pSynonym){
  1065. if( sqlite3Fts5IterEof(p->pIter)==0 ){
  1066. i64 ii = p->pIter->iRowid;
  1067. if( ii==iRowid
  1068. || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
  1069. ){
  1070. if( bFromValid ){
  1071. rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
  1072. }else{
  1073. rc = sqlite3Fts5IterNext(p->pIter);
  1074. }
  1075. if( rc!=SQLITE_OK ) break;
  1076. if( sqlite3Fts5IterEof(p->pIter)==0 ){
  1077. bEof = 0;
  1078. }
  1079. }else{
  1080. bEof = 0;
  1081. }
  1082. }
  1083. }
  1084. /* Set the EOF flag if either all synonym iterators are at EOF or an
  1085. ** error has occurred. */
  1086. pNode->bEof = (rc || bEof);
  1087. }else{
  1088. Fts5IndexIter *pIter = pTerm->pIter;
  1089. assert( Fts5NodeIsString(pNode) );
  1090. if( bFromValid ){
  1091. rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
  1092. }else{
  1093. rc = sqlite3Fts5IterNext(pIter);
  1094. }
  1095. pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
  1096. }
  1097. if( pNode->bEof==0 ){
  1098. assert( rc==SQLITE_OK );
  1099. rc = fts5ExprNodeTest_STRING(pExpr, pNode);
  1100. }
  1101. return rc;
  1102. }
  1103. static int fts5ExprNodeTest_TERM(
  1104. Fts5Expr *pExpr, /* Expression that pNear is a part of */
  1105. Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
  1106. ){
  1107. /* As this "NEAR" object is actually a single phrase that consists
  1108. ** of a single term only, grab pointers into the poslist managed by the
  1109. ** fts5_index.c iterator object. This is much faster than synthesizing
  1110. ** a new poslist the way we have to for more complicated phrase or NEAR
  1111. ** expressions. */
  1112. Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
  1113. Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
  1114. assert( pNode->eType==FTS5_TERM );
  1115. assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
  1116. assert( pPhrase->aTerm[0].pSynonym==0 );
  1117. pPhrase->poslist.n = pIter->nData;
  1118. if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
  1119. pPhrase->poslist.p = (u8*)pIter->pData;
  1120. }
  1121. pNode->iRowid = pIter->iRowid;
  1122. pNode->bNomatch = (pPhrase->poslist.n==0);
  1123. return SQLITE_OK;
  1124. }
  1125. /*
  1126. ** xNext() method for a node of type FTS5_TERM.
  1127. */
  1128. static int fts5ExprNodeNext_TERM(
  1129. Fts5Expr *pExpr,
  1130. Fts5ExprNode *pNode,
  1131. int bFromValid,
  1132. i64 iFrom
  1133. ){
  1134. int rc;
  1135. Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
  1136. assert( pNode->bEof==0 );
  1137. if( bFromValid ){
  1138. rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
  1139. }else{
  1140. rc = sqlite3Fts5IterNext(pIter);
  1141. }
  1142. if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
  1143. rc = fts5ExprNodeTest_TERM(pExpr, pNode);
  1144. }else{
  1145. pNode->bEof = 1;
  1146. pNode->bNomatch = 0;
  1147. }
  1148. return rc;
  1149. }
  1150. static void fts5ExprNodeTest_OR(
  1151. Fts5Expr *pExpr, /* Expression of which pNode is a part */
  1152. Fts5ExprNode *pNode /* Expression node to test */
  1153. ){
  1154. Fts5ExprNode *pNext = pNode->apChild[0];
  1155. int i;
  1156. for(i=1; i<pNode->nChild; i++){
  1157. Fts5ExprNode *pChild = pNode->apChild[i];
  1158. int cmp = fts5NodeCompare(pExpr, pNext, pChild);
  1159. if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
  1160. pNext = pChild;
  1161. }
  1162. }
  1163. pNode->iRowid = pNext->iRowid;
  1164. pNode->bEof = pNext->bEof;
  1165. pNode->bNomatch = pNext->bNomatch;
  1166. }
  1167. static int fts5ExprNodeNext_OR(
  1168. Fts5Expr *pExpr,
  1169. Fts5ExprNode *pNode,
  1170. int bFromValid,
  1171. i64 iFrom
  1172. ){
  1173. int i;
  1174. i64 iLast = pNode->iRowid;
  1175. for(i=0; i<pNode->nChild; i++){
  1176. Fts5ExprNode *p1 = pNode->apChild[i];
  1177. assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
  1178. if( p1->bEof==0 ){
  1179. if( (p1->iRowid==iLast)
  1180. || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
  1181. ){
  1182. int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
  1183. if( rc!=SQLITE_OK ){
  1184. pNode->bNomatch = 0;
  1185. return rc;
  1186. }
  1187. }
  1188. }
  1189. }
  1190. fts5ExprNodeTest_OR(pExpr, pNode);
  1191. return SQLITE_OK;
  1192. }
  1193. /*
  1194. ** Argument pNode is an FTS5_AND node.
  1195. */
  1196. static int fts5ExprNodeTest_AND(
  1197. Fts5Expr *pExpr, /* Expression pPhrase belongs to */
  1198. Fts5ExprNode *pAnd /* FTS5_AND node to advance */
  1199. ){
  1200. int iChild;
  1201. i64 iLast = pAnd->iRowid;
  1202. int rc = SQLITE_OK;
  1203. int bMatch;
  1204. assert( pAnd->bEof==0 );
  1205. do {
  1206. pAnd->bNomatch = 0;
  1207. bMatch = 1;
  1208. for(iChild=0; iChild<pAnd->nChild; iChild++){
  1209. Fts5ExprNode *pChild = pAnd->apChild[iChild];
  1210. int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
  1211. if( cmp>0 ){
  1212. /* Advance pChild until it points to iLast or laster */
  1213. rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
  1214. if( rc!=SQLITE_OK ){
  1215. pAnd->bNomatch = 0;
  1216. return rc;
  1217. }
  1218. }
  1219. /* If the child node is now at EOF, so is the parent AND node. Otherwise,
  1220. ** the child node is guaranteed to have advanced at least as far as
  1221. ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
  1222. ** new lastest rowid seen so far. */
  1223. assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 );
  1224. if( pChild->bEof ){
  1225. fts5ExprSetEof(pAnd);
  1226. bMatch = 1;
  1227. break;
  1228. }else if( iLast!=pChild->iRowid ){
  1229. bMatch = 0;
  1230. iLast = pChild->iRowid;
  1231. }
  1232. if( pChild->bNomatch ){
  1233. pAnd->bNomatch = 1;
  1234. }
  1235. }
  1236. }while( bMatch==0 );
  1237. if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){
  1238. fts5ExprNodeZeroPoslist(pAnd);
  1239. }
  1240. pAnd->iRowid = iLast;
  1241. return SQLITE_OK;
  1242. }
  1243. static int fts5ExprNodeNext_AND(
  1244. Fts5Expr *pExpr,
  1245. Fts5ExprNode *pNode,
  1246. int bFromValid,
  1247. i64 iFrom
  1248. ){
  1249. int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
  1250. if( rc==SQLITE_OK ){
  1251. rc = fts5ExprNodeTest_AND(pExpr, pNode);
  1252. }else{
  1253. pNode->bNomatch = 0;
  1254. }
  1255. return rc;
  1256. }
  1257. static int fts5ExprNodeTest_NOT(
  1258. Fts5Expr *pExpr, /* Expression pPhrase belongs to */
  1259. Fts5ExprNode *pNode /* FTS5_NOT node to advance */
  1260. ){
  1261. int rc = SQLITE_OK;
  1262. Fts5ExprNode *p1 = pNode->apChild[0];
  1263. Fts5ExprNode *p2 = pNode->apChild[1];
  1264. assert( pNode->nChild==2 );
  1265. while( rc==SQLITE_OK && p1->bEof==0 ){
  1266. int cmp = fts5NodeCompare(pExpr, p1, p2);
  1267. if( cmp>0 ){
  1268. rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
  1269. cmp = fts5NodeCompare(pExpr, p1, p2);
  1270. }
  1271. assert( rc!=SQLITE_OK || cmp<=0 );
  1272. if( cmp || p2->bNomatch ) break;
  1273. rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
  1274. }
  1275. pNode->bEof = p1->bEof;
  1276. pNode->bNomatch = p1->bNomatch;
  1277. pNode->iRowid = p1->iRowid;
  1278. if( p1->bEof ){
  1279. fts5ExprNodeZeroPoslist(p2);
  1280. }
  1281. return rc;
  1282. }
  1283. static int fts5ExprNodeNext_NOT(
  1284. Fts5Expr *pExpr,
  1285. Fts5ExprNode *pNode,
  1286. int bFromValid,
  1287. i64 iFrom
  1288. ){
  1289. int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
  1290. if( rc==SQLITE_OK ){
  1291. rc = fts5ExprNodeTest_NOT(pExpr, pNode);
  1292. }
  1293. if( rc!=SQLITE_OK ){
  1294. pNode->bNomatch = 0;
  1295. }
  1296. return rc;
  1297. }
  1298. /*
  1299. ** If pNode currently points to a match, this function returns SQLITE_OK
  1300. ** without modifying it. Otherwise, pNode is advanced until it does point
  1301. ** to a match or EOF is reached.
  1302. */
  1303. static int fts5ExprNodeTest(
  1304. Fts5Expr *pExpr, /* Expression of which pNode is a part */
  1305. Fts5ExprNode *pNode /* Expression node to test */
  1306. ){
  1307. int rc = SQLITE_OK;
  1308. if( pNode->bEof==0 ){
  1309. switch( pNode->eType ){
  1310. case FTS5_STRING: {
  1311. rc = fts5ExprNodeTest_STRING(pExpr, pNode);
  1312. break;
  1313. }
  1314. case FTS5_TERM: {
  1315. rc = fts5ExprNodeTest_TERM(pExpr, pNode);
  1316. break;
  1317. }
  1318. case FTS5_AND: {
  1319. rc = fts5ExprNodeTest_AND(pExpr, pNode);
  1320. break;
  1321. }
  1322. case FTS5_OR: {
  1323. fts5ExprNodeTest_OR(pExpr, pNode);
  1324. break;
  1325. }
  1326. default: assert( pNode->eType==FTS5_NOT ); {
  1327. rc = fts5ExprNodeTest_NOT(pExpr, pNode);
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. return rc;
  1333. }
  1334. /*
  1335. ** Set node pNode, which is part of expression pExpr, to point to the first
  1336. ** match. If there are no matches, set the Node.bEof flag to indicate EOF.
  1337. **
  1338. ** Return an SQLite error code if an error occurs, or SQLITE_OK otherwise.
  1339. ** It is not an error if there are no matches.
  1340. */
  1341. static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
  1342. int rc = SQLITE_OK;
  1343. pNode->bEof = 0;
  1344. pNode->bNomatch = 0;
  1345. if( Fts5NodeIsString(pNode) ){
  1346. /* Initialize all term iterators in the NEAR object. */
  1347. rc = fts5ExprNearInitAll(pExpr, pNode);
  1348. }else if( pNode->xNext==0 ){
  1349. pNode->bEof = 1;
  1350. }else{
  1351. int i;
  1352. int nEof = 0;
  1353. for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
  1354. Fts5ExprNode *pChild = pNode->apChild[i];
  1355. rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
  1356. assert( pChild->bEof==0 || pChild->bEof==1 );
  1357. nEof += pChild->bEof;
  1358. }
  1359. pNode->iRowid = pNode->apChild[0]->iRowid;
  1360. switch( pNode->eType ){
  1361. case FTS5_AND:
  1362. if( nEof>0 ) fts5ExprSetEof(pNode);
  1363. break;
  1364. case FTS5_OR:
  1365. if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
  1366. break;
  1367. default:
  1368. assert( pNode->eType==FTS5_NOT );
  1369. pNode->bEof = pNode->apChild[0]->bEof;
  1370. break;
  1371. }
  1372. }
  1373. if( rc==SQLITE_OK ){
  1374. rc = fts5ExprNodeTest(pExpr, pNode);
  1375. }
  1376. return rc;
  1377. }
  1378. /*
  1379. ** Begin iterating through the set of documents in index pIdx matched by
  1380. ** the MATCH expression passed as the first argument. If the "bDesc"
  1381. ** parameter is passed a non-zero value, iteration is in descending rowid
  1382. ** order. Or, if it is zero, in ascending order.
  1383. **
  1384. ** If iterating in ascending rowid order (bDesc==0), the first document
  1385. ** visited is that with the smallest rowid that is larger than or equal
  1386. ** to parameter iFirst. Or, if iterating in ascending order (bDesc==1),
  1387. ** then the first document visited must have a rowid smaller than or
  1388. ** equal to iFirst.
  1389. **
  1390. ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
  1391. ** is not considered an error if the query does not match any documents.
  1392. */
  1393. int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
  1394. Fts5ExprNode *pRoot = p->pRoot;
  1395. int rc; /* Return code */
  1396. p->pIndex = pIdx;
  1397. p->bDesc = bDesc;
  1398. rc = fts5ExprNodeFirst(p, pRoot);
  1399. /* If not at EOF but the current rowid occurs earlier than iFirst in
  1400. ** the iteration order, move to document iFirst or later. */
  1401. if( rc==SQLITE_OK
  1402. && 0==pRoot->bEof
  1403. && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0
  1404. ){
  1405. rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
  1406. }
  1407. /* If the iterator is not at a real match, skip forward until it is. */
  1408. while( pRoot->bNomatch && rc==SQLITE_OK ){
  1409. assert( pRoot->bEof==0 );
  1410. rc = fts5ExprNodeNext(p, pRoot, 0, 0);
  1411. }
  1412. return rc;
  1413. }
  1414. /*
  1415. ** Move to the next document
  1416. **
  1417. ** Return SQLITE_OK if successful, or an SQLite error code otherwise. It
  1418. ** is not considered an error if the query does not match any documents.
  1419. */
  1420. int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
  1421. int rc;
  1422. Fts5ExprNode *pRoot = p->pRoot;
  1423. assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
  1424. do {
  1425. rc = fts5ExprNodeNext(p, pRoot, 0, 0);
  1426. assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
  1427. }while( pRoot->bNomatch );
  1428. if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
  1429. pRoot->bEof = 1;
  1430. }
  1431. return rc;
  1432. }
  1433. int sqlite3Fts5ExprEof(Fts5Expr *p){
  1434. return p->pRoot->bEof;
  1435. }
  1436. i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
  1437. return p->pRoot->iRowid;
  1438. }
  1439. static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){
  1440. int rc = SQLITE_OK;
  1441. *pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n);
  1442. return rc;
  1443. }
  1444. /*
  1445. ** Free the phrase object passed as the only argument.
  1446. */
  1447. static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){
  1448. if( pPhrase ){
  1449. int i;
  1450. for(i=0; i<pPhrase->nTerm; i++){
  1451. Fts5ExprTerm *pSyn;
  1452. Fts5ExprTerm *pNext;
  1453. Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
  1454. sqlite3_free(pTerm->pTerm);
  1455. sqlite3Fts5IterClose(pTerm->pIter);
  1456. for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
  1457. pNext = pSyn->pSynonym;
  1458. sqlite3Fts5IterClose(pSyn->pIter);
  1459. fts5BufferFree((Fts5Buffer*)&pSyn[1]);
  1460. sqlite3_free(pSyn);
  1461. }
  1462. }
  1463. if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
  1464. sqlite3_free(pPhrase);
  1465. }
  1466. }
  1467. /*
  1468. ** Set the "bFirst" flag on the first token of the phrase passed as the
  1469. ** only argument.
  1470. */
  1471. void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
  1472. if( pPhrase && pPhrase->nTerm ){
  1473. pPhrase->aTerm[0].bFirst = 1;
  1474. }
  1475. }
  1476. /*
  1477. ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
  1478. ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
  1479. ** appended to it and the results returned.
  1480. **
  1481. ** If an OOM error occurs, both the pNear and pPhrase objects are freed and
  1482. ** NULL returned.
  1483. */
  1484. Fts5ExprNearset *sqlite3Fts5ParseNearset(
  1485. Fts5Parse *pParse, /* Parse context */
  1486. Fts5ExprNearset *pNear, /* Existing nearset, or NULL */
  1487. Fts5ExprPhrase *pPhrase /* Recently parsed phrase */
  1488. ){
  1489. const int SZALLOC = 8;
  1490. Fts5ExprNearset *pRet = 0;
  1491. if( pParse->rc==SQLITE_OK ){
  1492. if( pNear==0 ){
  1493. sqlite3_int64 nByte;
  1494. nByte = SZ_FTS5EXPRNEARSET(SZALLOC+1);
  1495. pRet = sqlite3_malloc64(nByte);
  1496. if( pRet==0 ){
  1497. pParse->rc = SQLITE_NOMEM;
  1498. }else{
  1499. memset(pRet, 0, (size_t)nByte);
  1500. }
  1501. }else if( (pNear->nPhrase % SZALLOC)==0 ){
  1502. int nNew = pNear->nPhrase + SZALLOC;
  1503. sqlite3_int64 nByte;
  1504. nByte = SZ_FTS5EXPRNEARSET(nNew+1);
  1505. pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte);
  1506. if( pRet==0 ){
  1507. pParse->rc = SQLITE_NOMEM;
  1508. }
  1509. }else{
  1510. pRet = pNear;
  1511. }
  1512. }
  1513. if( pRet==0 ){
  1514. assert( pParse->rc!=SQLITE_OK );
  1515. sqlite3Fts5ParseNearsetFree(pNear);
  1516. sqlite3Fts5ParsePhraseFree(pPhrase);
  1517. }else{
  1518. if( pRet->nPhrase>0 ){
  1519. Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1];
  1520. assert( pParse!=0 );
  1521. assert( pParse->apPhrase!=0 );
  1522. assert( pParse->nPhrase>=2 );
  1523. assert( pLast==pParse->apPhrase[pParse->nPhrase-2] );
  1524. if( pPhrase->nTerm==0 ){
  1525. fts5ExprPhraseFree(pPhrase);
  1526. pRet->nPhrase--;
  1527. pParse->nPhrase--;
  1528. pPhrase = pLast;
  1529. }else if( pLast->nTerm==0 ){
  1530. fts5ExprPhraseFree(pLast);
  1531. pParse->apPhrase[pParse->nPhrase-2] = pPhrase;
  1532. pParse->nPhrase--;
  1533. pRet->nPhrase--;
  1534. }
  1535. }
  1536. pRet->apPhrase[pRet->nPhrase++] = pPhrase;
  1537. }
  1538. return pRet;
  1539. }
  1540. typedef struct TokenCtx TokenCtx;
  1541. struct TokenCtx {
  1542. Fts5ExprPhrase *pPhrase;
  1543. Fts5Config *pConfig;
  1544. int rc;
  1545. };
  1546. /*
  1547. ** Callback for tokenizing terms used by ParseTerm().
  1548. */
  1549. static int fts5ParseTokenize(
  1550. void *pContext, /* Pointer to Fts5InsertCtx object */
  1551. int tflags, /* Mask of FTS5_TOKEN_* flags */
  1552. const char *pToken, /* Buffer containing token */
  1553. int nToken, /* Size of token in bytes */
  1554. int iUnused1, /* Start offset of token */
  1555. int iUnused2 /* End offset of token */
  1556. ){
  1557. int rc = SQLITE_OK;
  1558. const int SZALLOC = 8;
  1559. TokenCtx *pCtx = (TokenCtx*)pContext;
  1560. Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
  1561. UNUSED_PARAM2(iUnused1, iUnused2);
  1562. /* If an error has already occurred, this is a no-op */
  1563. if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
  1564. if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE;
  1565. if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){
  1566. Fts5ExprTerm *pSyn;
  1567. sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
  1568. pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte);
  1569. if( pSyn==0 ){
  1570. rc = SQLITE_NOMEM;
  1571. }else{
  1572. memset(pSyn, 0, (size_t)nByte);
  1573. pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
  1574. pSyn->nFullTerm = pSyn->nQueryTerm = nToken;
  1575. if( pCtx->pConfig->bTokendata ){
  1576. pSyn->nQueryTerm = (int)strlen(pSyn->pTerm);
  1577. }
  1578. memcpy(pSyn->pTerm, pToken, nToken);
  1579. pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
  1580. pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
  1581. }
  1582. }else{
  1583. Fts5ExprTerm *pTerm;
  1584. if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){
  1585. Fts5ExprPhrase *pNew;
  1586. int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0);
  1587. pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase,
  1588. SZ_FTS5EXPRPHRASE(nNew+1)
  1589. );
  1590. if( pNew==0 ){
  1591. rc = SQLITE_NOMEM;
  1592. }else{
  1593. if( pPhrase==0 ) memset(pNew, 0, SZ_FTS5EXPRPHRASE(1));
  1594. pCtx->pPhrase = pPhrase = pNew;
  1595. pNew->nTerm = nNew - SZALLOC;
  1596. }
  1597. }
  1598. if( rc==SQLITE_OK ){
  1599. pTerm = &pPhrase->aTerm[pPhrase->nTerm++];
  1600. memset(pTerm, 0, sizeof(Fts5ExprTerm));
  1601. pTerm->pTerm = sqlite3Fts5Strndup(&rc, pToken, nToken);
  1602. pTerm->nFullTerm = pTerm->nQueryTerm = nToken;
  1603. if( pCtx->pConfig->bTokendata && rc==SQLITE_OK ){
  1604. pTerm->nQueryTerm = (int)strlen(pTerm->pTerm);
  1605. }
  1606. }
  1607. }
  1608. pCtx->rc = rc;
  1609. return rc;
  1610. }
  1611. /*
  1612. ** Free the phrase object passed as the only argument.
  1613. */
  1614. void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){
  1615. fts5ExprPhraseFree(pPhrase);
  1616. }
  1617. /*
  1618. ** Free the phrase object passed as the second argument.
  1619. */
  1620. void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){
  1621. if( pNear ){
  1622. int i;
  1623. for(i=0; i<pNear->nPhrase; i++){
  1624. fts5ExprPhraseFree(pNear->apPhrase[i]);
  1625. }
  1626. sqlite3_free(pNear->pColset);
  1627. sqlite3_free(pNear);
  1628. }
  1629. }
  1630. void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){
  1631. assert( pParse->pExpr==0 );
  1632. pParse->pExpr = p;
  1633. }
  1634. static int parseGrowPhraseArray(Fts5Parse *pParse){
  1635. if( (pParse->nPhrase % 8)==0 ){
  1636. sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8);
  1637. Fts5ExprPhrase **apNew;
  1638. apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte);
  1639. if( apNew==0 ){
  1640. pParse->rc = SQLITE_NOMEM;
  1641. return SQLITE_NOMEM;
  1642. }
  1643. pParse->apPhrase = apNew;
  1644. }
  1645. return SQLITE_OK;
  1646. }
  1647. /*
  1648. ** This function is called by the parser to process a string token. The
  1649. ** string may or may not be quoted. In any case it is tokenized and a
  1650. ** phrase object consisting of all tokens returned.
  1651. */
  1652. Fts5ExprPhrase *sqlite3Fts5ParseTerm(
  1653. Fts5Parse *pParse, /* Parse context */
  1654. Fts5ExprPhrase *pAppend, /* Phrase to append to */
  1655. Fts5Token *pToken, /* String to tokenize */
  1656. int bPrefix /* True if there is a trailing "*" */
  1657. ){
  1658. Fts5Config *pConfig = pParse->pConfig;
  1659. TokenCtx sCtx; /* Context object passed to callback */
  1660. int rc; /* Tokenize return code */
  1661. char *z = 0;
  1662. memset(&sCtx, 0, sizeof(TokenCtx));
  1663. sCtx.pPhrase = pAppend;
  1664. sCtx.pConfig = pConfig;
  1665. rc = fts5ParseStringFromToken(pToken, &z);
  1666. if( rc==SQLITE_OK ){
  1667. int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0);
  1668. int n;
  1669. sqlite3Fts5Dequote(z);
  1670. n = (int)strlen(z);
  1671. rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize);
  1672. }
  1673. sqlite3_free(z);
  1674. if( rc || (rc = sCtx.rc) ){
  1675. pParse->rc = rc;
  1676. fts5ExprPhraseFree(sCtx.pPhrase);
  1677. sCtx.pPhrase = 0;
  1678. }else{
  1679. if( pAppend==0 ){
  1680. if( parseGrowPhraseArray(pParse) ){
  1681. fts5ExprPhraseFree(sCtx.pPhrase);
  1682. return 0;
  1683. }
  1684. pParse->nPhrase++;
  1685. }
  1686. if( sCtx.pPhrase==0 ){
  1687. /* This happens when parsing a token or quoted phrase that contains
  1688. ** no token characters at all. (e.g ... MATCH '""'). */
  1689. sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, SZ_FTS5EXPRPHRASE(1));
  1690. }else if( sCtx.pPhrase->nTerm ){
  1691. sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix;
  1692. }
  1693. assert( pParse->apPhrase!=0 );
  1694. pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase;
  1695. }
  1696. return sCtx.pPhrase;
  1697. }
  1698. /*
  1699. ** Create a new FTS5 expression by cloning phrase iPhrase of the
  1700. ** expression passed as the second argument.
  1701. */
  1702. int sqlite3Fts5ExprClonePhrase(
  1703. Fts5Expr *pExpr,
  1704. int iPhrase,
  1705. Fts5Expr **ppNew
  1706. ){
  1707. int rc = SQLITE_OK; /* Return code */
  1708. Fts5ExprPhrase *pOrig = 0; /* The phrase extracted from pExpr */
  1709. Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
  1710. TokenCtx sCtx = {0,0,0}; /* Context object for fts5ParseTokenize */
  1711. if( !pExpr || iPhrase<0 || iPhrase>=pExpr->nPhrase ){
  1712. rc = SQLITE_RANGE;
  1713. }else{
  1714. pOrig = pExpr->apExprPhrase[iPhrase];
  1715. pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
  1716. }
  1717. if( rc==SQLITE_OK ){
  1718. pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
  1719. sizeof(Fts5ExprPhrase*));
  1720. }
  1721. if( rc==SQLITE_OK ){
  1722. pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRNODE(1));
  1723. }
  1724. if( rc==SQLITE_OK ){
  1725. pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc,
  1726. SZ_FTS5EXPRNEARSET(2));
  1727. }
  1728. if( rc==SQLITE_OK && ALWAYS(pOrig!=0) ){
  1729. Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset;
  1730. if( pColsetOrig ){
  1731. sqlite3_int64 nByte;
  1732. Fts5Colset *pColset;
  1733. nByte = SZ_FTS5COLSET(pColsetOrig->nCol);
  1734. pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte);
  1735. if( pColset ){
  1736. memcpy(pColset, pColsetOrig, (size_t)nByte);
  1737. }
  1738. pNew->pRoot->pNear->pColset = pColset;
  1739. }
  1740. }
  1741. if( rc==SQLITE_OK ){
  1742. if( pOrig->nTerm ){
  1743. int i; /* Used to iterate through phrase terms */
  1744. sCtx.pConfig = pExpr->pConfig;
  1745. for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){
  1746. int tflags = 0;
  1747. Fts5ExprTerm *p;
  1748. for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){
  1749. rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0);
  1750. tflags = FTS5_TOKEN_COLOCATED;
  1751. }
  1752. if( rc==SQLITE_OK ){
  1753. sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
  1754. sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
  1755. }
  1756. }
  1757. }else{
  1758. /* This happens when parsing a token or quoted phrase that contains
  1759. ** no token characters at all. (e.g ... MATCH '""'). */
  1760. sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, SZ_FTS5EXPRPHRASE(1));
  1761. }
  1762. }
  1763. if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){
  1764. /* All the allocations succeeded. Put the expression object together. */
  1765. pNew->pIndex = pExpr->pIndex;
  1766. pNew->pConfig = pExpr->pConfig;
  1767. pNew->nPhrase = 1;
  1768. pNew->apExprPhrase[0] = sCtx.pPhrase;
  1769. pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
  1770. pNew->pRoot->pNear->nPhrase = 1;
  1771. sCtx.pPhrase->pNode = pNew->pRoot;
  1772. if( pOrig->nTerm==1
  1773. && pOrig->aTerm[0].pSynonym==0
  1774. && pOrig->aTerm[0].bFirst==0
  1775. ){
  1776. pNew->pRoot->eType = FTS5_TERM;
  1777. pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
  1778. }else{
  1779. pNew->pRoot->eType = FTS5_STRING;
  1780. pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
  1781. }
  1782. }else{
  1783. sqlite3Fts5ExprFree(pNew);
  1784. fts5ExprPhraseFree(sCtx.pPhrase);
  1785. pNew = 0;
  1786. }
  1787. *ppNew = pNew;
  1788. return rc;
  1789. }
  1790. /*
  1791. ** Token pTok has appeared in a MATCH expression where the NEAR operator
  1792. ** is expected. If token pTok does not contain "NEAR", store an error
  1793. ** in the pParse object.
  1794. */
  1795. void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){
  1796. if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){
  1797. sqlite3Fts5ParseError(
  1798. pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p
  1799. );
  1800. }
  1801. }
  1802. void sqlite3Fts5ParseSetDistance(
  1803. Fts5Parse *pParse,
  1804. Fts5ExprNearset *pNear,
  1805. Fts5Token *p
  1806. ){
  1807. if( pNear ){
  1808. int nNear = 0;
  1809. int i;
  1810. if( p->n ){
  1811. for(i=0; i<p->n; i++){
  1812. char c = (char)p->p[i];
  1813. if( c<'0' || c>'9' ){
  1814. sqlite3Fts5ParseError(
  1815. pParse, "expected integer, got \"%.*s\"", p->n, p->p
  1816. );
  1817. return;
  1818. }
  1819. if( nNear<214748363 ) nNear = nNear * 10 + (p->p[i] - '0');
  1820. /* ^^^^^^^^^^^^^^^--- Prevent integer overflow */
  1821. }
  1822. }else{
  1823. nNear = FTS5_DEFAULT_NEARDIST;
  1824. }
  1825. pNear->nNear = nNear;
  1826. }
  1827. }
  1828. /*
  1829. ** The second argument passed to this function may be NULL, or it may be
  1830. ** an existing Fts5Colset object. This function returns a pointer to
  1831. ** a new colset object containing the contents of (p) with new value column
  1832. ** number iCol appended.
  1833. **
  1834. ** If an OOM error occurs, store an error code in pParse and return NULL.
  1835. ** The old colset object (if any) is not freed in this case.
  1836. */
  1837. static Fts5Colset *fts5ParseColset(
  1838. Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
  1839. Fts5Colset *p, /* Existing colset object */
  1840. int iCol /* New column to add to colset object */
  1841. ){
  1842. int nCol = p ? p->nCol : 0; /* Num. columns already in colset object */
  1843. Fts5Colset *pNew; /* New colset object to return */
  1844. assert( pParse->rc==SQLITE_OK );
  1845. assert( iCol>=0 && iCol<pParse->pConfig->nCol );
  1846. pNew = sqlite3_realloc64(p, SZ_FTS5COLSET(nCol+1));
  1847. if( pNew==0 ){
  1848. pParse->rc = SQLITE_NOMEM;
  1849. }else{
  1850. int *aiCol = pNew->aiCol;
  1851. int i, j;
  1852. for(i=0; i<nCol; i++){
  1853. if( aiCol[i]==iCol ) return pNew;
  1854. if( aiCol[i]>iCol ) break;
  1855. }
  1856. for(j=nCol; j>i; j--){
  1857. aiCol[j] = aiCol[j-1];
  1858. }
  1859. aiCol[i] = iCol;
  1860. pNew->nCol = nCol+1;
  1861. #ifndef NDEBUG
  1862. /* Check that the array is in order and contains no duplicate entries. */
  1863. for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] );
  1864. #endif
  1865. }
  1866. return pNew;
  1867. }
  1868. /*
  1869. ** Allocate and return an Fts5Colset object specifying the inverse of
  1870. ** the colset passed as the second argument. Free the colset passed
  1871. ** as the second argument before returning.
  1872. */
  1873. Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){
  1874. Fts5Colset *pRet;
  1875. int nCol = pParse->pConfig->nCol;
  1876. pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc,
  1877. SZ_FTS5COLSET(nCol+1)
  1878. );
  1879. if( pRet ){
  1880. int i;
  1881. int iOld = 0;
  1882. for(i=0; i<nCol; i++){
  1883. if( iOld>=p->nCol || p->aiCol[iOld]!=i ){
  1884. pRet->aiCol[pRet->nCol++] = i;
  1885. }else{
  1886. iOld++;
  1887. }
  1888. }
  1889. }
  1890. sqlite3_free(p);
  1891. return pRet;
  1892. }
  1893. Fts5Colset *sqlite3Fts5ParseColset(
  1894. Fts5Parse *pParse, /* Store SQLITE_NOMEM here if required */
  1895. Fts5Colset *pColset, /* Existing colset object */
  1896. Fts5Token *p
  1897. ){
  1898. Fts5Colset *pRet = 0;
  1899. int iCol;
  1900. char *z; /* Dequoted copy of token p */
  1901. z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n);
  1902. if( pParse->rc==SQLITE_OK ){
  1903. Fts5Config *pConfig = pParse->pConfig;
  1904. sqlite3Fts5Dequote(z);
  1905. for(iCol=0; iCol<pConfig->nCol; iCol++){
  1906. if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break;
  1907. }
  1908. if( iCol==pConfig->nCol ){
  1909. sqlite3Fts5ParseError(pParse, "no such column: %s", z);
  1910. }else{
  1911. pRet = fts5ParseColset(pParse, pColset, iCol);
  1912. }
  1913. sqlite3_free(z);
  1914. }
  1915. if( pRet==0 ){
  1916. assert( pParse->rc!=SQLITE_OK );
  1917. sqlite3_free(pColset);
  1918. }
  1919. return pRet;
  1920. }
  1921. /*
  1922. ** If argument pOrig is NULL, or if (*pRc) is set to anything other than
  1923. ** SQLITE_OK when this function is called, NULL is returned.
  1924. **
  1925. ** Otherwise, a copy of (*pOrig) is made into memory obtained from
  1926. ** sqlite3Fts5MallocZero() and a pointer to it returned. If the allocation
  1927. ** fails, (*pRc) is set to SQLITE_NOMEM and NULL is returned.
  1928. */
  1929. static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){
  1930. Fts5Colset *pRet;
  1931. if( pOrig ){
  1932. sqlite3_int64 nByte = SZ_FTS5COLSET(pOrig->nCol);
  1933. pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte);
  1934. if( pRet ){
  1935. memcpy(pRet, pOrig, (size_t)nByte);
  1936. }
  1937. }else{
  1938. pRet = 0;
  1939. }
  1940. return pRet;
  1941. }
  1942. /*
  1943. ** Remove from colset pColset any columns that are not also in colset pMerge.
  1944. */
  1945. static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){
  1946. int iIn = 0; /* Next input in pColset */
  1947. int iMerge = 0; /* Next input in pMerge */
  1948. int iOut = 0; /* Next output slot in pColset */
  1949. while( iIn<pColset->nCol && iMerge<pMerge->nCol ){
  1950. int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge];
  1951. if( iDiff==0 ){
  1952. pColset->aiCol[iOut++] = pMerge->aiCol[iMerge];
  1953. iMerge++;
  1954. iIn++;
  1955. }else if( iDiff>0 ){
  1956. iMerge++;
  1957. }else{
  1958. iIn++;
  1959. }
  1960. }
  1961. pColset->nCol = iOut;
  1962. }
  1963. /*
  1964. ** Recursively apply colset pColset to expression node pNode and all of
  1965. ** its decendents. If (*ppFree) is not NULL, it contains a spare copy
  1966. ** of pColset. This function may use the spare copy and set (*ppFree) to
  1967. ** zero, or it may create copies of pColset using fts5CloneColset().
  1968. */
  1969. static void fts5ParseSetColset(
  1970. Fts5Parse *pParse,
  1971. Fts5ExprNode *pNode,
  1972. Fts5Colset *pColset,
  1973. Fts5Colset **ppFree
  1974. ){
  1975. if( pParse->rc==SQLITE_OK ){
  1976. assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING
  1977. || pNode->eType==FTS5_AND || pNode->eType==FTS5_OR
  1978. || pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF
  1979. );
  1980. if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){
  1981. Fts5ExprNearset *pNear = pNode->pNear;
  1982. if( pNear->pColset ){
  1983. fts5MergeColset(pNear->pColset, pColset);
  1984. if( pNear->pColset->nCol==0 ){
  1985. pNode->eType = FTS5_EOF;
  1986. pNode->xNext = 0;
  1987. }
  1988. }else if( *ppFree ){
  1989. pNear->pColset = pColset;
  1990. *ppFree = 0;
  1991. }else{
  1992. pNear->pColset = fts5CloneColset(&pParse->rc, pColset);
  1993. }
  1994. }else{
  1995. int i;
  1996. assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 );
  1997. for(i=0; i<pNode->nChild; i++){
  1998. fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree);
  1999. }
  2000. }
  2001. }
  2002. }
  2003. /*
  2004. ** Apply colset pColset to expression node pExpr and all of its descendents.
  2005. */
  2006. void sqlite3Fts5ParseSetColset(
  2007. Fts5Parse *pParse,
  2008. Fts5ExprNode *pExpr,
  2009. Fts5Colset *pColset
  2010. ){
  2011. Fts5Colset *pFree = pColset;
  2012. if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){
  2013. sqlite3Fts5ParseError(pParse,
  2014. "fts5: column queries are not supported (detail=none)"
  2015. );
  2016. }else{
  2017. fts5ParseSetColset(pParse, pExpr, pColset, &pFree);
  2018. }
  2019. sqlite3_free(pFree);
  2020. }
  2021. static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
  2022. switch( pNode->eType ){
  2023. case FTS5_STRING: {
  2024. Fts5ExprNearset *pNear = pNode->pNear;
  2025. if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
  2026. && pNear->apPhrase[0]->aTerm[0].pSynonym==0
  2027. && pNear->apPhrase[0]->aTerm[0].bFirst==0
  2028. ){
  2029. pNode->eType = FTS5_TERM;
  2030. pNode->xNext = fts5ExprNodeNext_TERM;
  2031. }else{
  2032. pNode->xNext = fts5ExprNodeNext_STRING;
  2033. }
  2034. break;
  2035. };
  2036. case FTS5_OR: {
  2037. pNode->xNext = fts5ExprNodeNext_OR;
  2038. break;
  2039. };
  2040. case FTS5_AND: {
  2041. pNode->xNext = fts5ExprNodeNext_AND;
  2042. break;
  2043. };
  2044. default: assert( pNode->eType==FTS5_NOT ); {
  2045. pNode->xNext = fts5ExprNodeNext_NOT;
  2046. break;
  2047. };
  2048. }
  2049. }
  2050. /*
  2051. ** Add pSub as a child of p.
  2052. */
  2053. static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
  2054. int ii = p->nChild;
  2055. if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
  2056. int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
  2057. memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
  2058. p->nChild += pSub->nChild;
  2059. sqlite3_free(pSub);
  2060. }else{
  2061. p->apChild[p->nChild++] = pSub;
  2062. }
  2063. for( ; ii<p->nChild; ii++){
  2064. p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1);
  2065. }
  2066. }
  2067. /*
  2068. ** This function is used when parsing LIKE or GLOB patterns against
  2069. ** trigram indexes that specify either detail=column or detail=none.
  2070. ** It converts a phrase:
  2071. **
  2072. ** abc + def + ghi
  2073. **
  2074. ** into an AND tree:
  2075. **
  2076. ** abc AND def AND ghi
  2077. */
  2078. static Fts5ExprNode *fts5ParsePhraseToAnd(
  2079. Fts5Parse *pParse,
  2080. Fts5ExprNearset *pNear
  2081. ){
  2082. int nTerm = pNear->apPhrase[0]->nTerm;
  2083. int ii;
  2084. int nByte;
  2085. Fts5ExprNode *pRet;
  2086. assert( pNear->nPhrase==1 );
  2087. assert( pParse->bPhraseToAnd );
  2088. nByte = SZ_FTS5EXPRNODE(nTerm+1);
  2089. pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
  2090. if( pRet ){
  2091. pRet->eType = FTS5_AND;
  2092. pRet->nChild = nTerm;
  2093. pRet->iHeight = 1;
  2094. fts5ExprAssignXNext(pRet);
  2095. pParse->nPhrase--;
  2096. for(ii=0; ii<nTerm; ii++){
  2097. Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero(
  2098. &pParse->rc, SZ_FTS5EXPRPHRASE(1)
  2099. );
  2100. if( pPhrase ){
  2101. if( parseGrowPhraseArray(pParse) ){
  2102. fts5ExprPhraseFree(pPhrase);
  2103. }else{
  2104. Fts5ExprTerm *p = &pNear->apPhrase[0]->aTerm[ii];
  2105. Fts5ExprTerm *pTo = &pPhrase->aTerm[0];
  2106. pParse->apPhrase[pParse->nPhrase++] = pPhrase;
  2107. pPhrase->nTerm = 1;
  2108. pTo->pTerm = sqlite3Fts5Strndup(&pParse->rc, p->pTerm, p->nFullTerm);
  2109. pTo->nQueryTerm = p->nQueryTerm;
  2110. pTo->nFullTerm = p->nFullTerm;
  2111. pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING,
  2112. 0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase)
  2113. );
  2114. }
  2115. }
  2116. }
  2117. if( pParse->rc ){
  2118. sqlite3Fts5ParseNodeFree(pRet);
  2119. pRet = 0;
  2120. }else{
  2121. sqlite3Fts5ParseNearsetFree(pNear);
  2122. }
  2123. }
  2124. return pRet;
  2125. }
  2126. /*
  2127. ** Allocate and return a new expression object. If anything goes wrong (i.e.
  2128. ** OOM error), leave an error code in pParse and return NULL.
  2129. */
  2130. Fts5ExprNode *sqlite3Fts5ParseNode(
  2131. Fts5Parse *pParse, /* Parse context */
  2132. int eType, /* FTS5_STRING, AND, OR or NOT */
  2133. Fts5ExprNode *pLeft, /* Left hand child expression */
  2134. Fts5ExprNode *pRight, /* Right hand child expression */
  2135. Fts5ExprNearset *pNear /* For STRING expressions, the near cluster */
  2136. ){
  2137. Fts5ExprNode *pRet = 0;
  2138. if( pParse->rc==SQLITE_OK ){
  2139. int nChild = 0; /* Number of children of returned node */
  2140. sqlite3_int64 nByte; /* Bytes of space to allocate for this node */
  2141. assert( (eType!=FTS5_STRING && !pNear)
  2142. || (eType==FTS5_STRING && !pLeft && !pRight)
  2143. );
  2144. if( eType==FTS5_STRING && pNear==0 ) return 0;
  2145. if( eType!=FTS5_STRING && pLeft==0 ) return pRight;
  2146. if( eType!=FTS5_STRING && pRight==0 ) return pLeft;
  2147. if( eType==FTS5_STRING
  2148. && pParse->bPhraseToAnd
  2149. && pNear->apPhrase[0]->nTerm>1
  2150. ){
  2151. pRet = fts5ParsePhraseToAnd(pParse, pNear);
  2152. }else{
  2153. if( eType==FTS5_NOT ){
  2154. nChild = 2;
  2155. }else if( eType==FTS5_AND || eType==FTS5_OR ){
  2156. nChild = 2;
  2157. if( pLeft->eType==eType ) nChild += pLeft->nChild-1;
  2158. if( pRight->eType==eType ) nChild += pRight->nChild-1;
  2159. }
  2160. nByte = SZ_FTS5EXPRNODE(nChild);
  2161. pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
  2162. if( pRet ){
  2163. pRet->eType = eType;
  2164. pRet->pNear = pNear;
  2165. fts5ExprAssignXNext(pRet);
  2166. if( eType==FTS5_STRING ){
  2167. int iPhrase;
  2168. for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
  2169. pNear->apPhrase[iPhrase]->pNode = pRet;
  2170. if( pNear->apPhrase[iPhrase]->nTerm==0 ){
  2171. pRet->xNext = 0;
  2172. pRet->eType = FTS5_EOF;
  2173. }
  2174. }
  2175. if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
  2176. Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
  2177. if( pNear->nPhrase!=1
  2178. || pPhrase->nTerm>1
  2179. || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
  2180. ){
  2181. sqlite3Fts5ParseError(pParse,
  2182. "fts5: %s queries are not supported (detail!=full)",
  2183. pNear->nPhrase==1 ? "phrase": "NEAR"
  2184. );
  2185. sqlite3Fts5ParseNodeFree(pRet);
  2186. pRet = 0;
  2187. pNear = 0;
  2188. assert( pLeft==0 && pRight==0 );
  2189. }
  2190. }
  2191. }else{
  2192. assert( pNear==0 );
  2193. fts5ExprAddChildren(pRet, pLeft);
  2194. fts5ExprAddChildren(pRet, pRight);
  2195. pLeft = pRight = 0;
  2196. if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH ){
  2197. sqlite3Fts5ParseError(pParse,
  2198. "fts5 expression tree is too large (maximum depth %d)",
  2199. SQLITE_FTS5_MAX_EXPR_DEPTH
  2200. );
  2201. sqlite3Fts5ParseNodeFree(pRet);
  2202. pRet = 0;
  2203. }
  2204. }
  2205. }
  2206. }
  2207. }
  2208. if( pRet==0 ){
  2209. assert( pParse->rc!=SQLITE_OK );
  2210. sqlite3Fts5ParseNodeFree(pLeft);
  2211. sqlite3Fts5ParseNodeFree(pRight);
  2212. sqlite3Fts5ParseNearsetFree(pNear);
  2213. }
  2214. return pRet;
  2215. }
  2216. Fts5ExprNode *sqlite3Fts5ParseImplicitAnd(
  2217. Fts5Parse *pParse, /* Parse context */
  2218. Fts5ExprNode *pLeft, /* Left hand child expression */
  2219. Fts5ExprNode *pRight /* Right hand child expression */
  2220. ){
  2221. Fts5ExprNode *pRet = 0;
  2222. Fts5ExprNode *pPrev;
  2223. if( pParse->rc ){
  2224. sqlite3Fts5ParseNodeFree(pLeft);
  2225. sqlite3Fts5ParseNodeFree(pRight);
  2226. }else{
  2227. assert( pLeft->eType==FTS5_STRING
  2228. || pLeft->eType==FTS5_TERM
  2229. || pLeft->eType==FTS5_EOF
  2230. || pLeft->eType==FTS5_AND
  2231. );
  2232. assert( pRight->eType==FTS5_STRING
  2233. || pRight->eType==FTS5_TERM
  2234. || pRight->eType==FTS5_EOF
  2235. || (pRight->eType==FTS5_AND && pParse->bPhraseToAnd)
  2236. );
  2237. if( pLeft->eType==FTS5_AND ){
  2238. pPrev = pLeft->apChild[pLeft->nChild-1];
  2239. }else{
  2240. pPrev = pLeft;
  2241. }
  2242. assert( pPrev->eType==FTS5_STRING
  2243. || pPrev->eType==FTS5_TERM
  2244. || pPrev->eType==FTS5_EOF
  2245. );
  2246. if( pRight->eType==FTS5_EOF ){
  2247. assert( pParse->apPhrase!=0 );
  2248. assert( pParse->nPhrase>0 );
  2249. assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] );
  2250. sqlite3Fts5ParseNodeFree(pRight);
  2251. pRet = pLeft;
  2252. pParse->nPhrase--;
  2253. }
  2254. else if( pPrev->eType==FTS5_EOF ){
  2255. Fts5ExprPhrase **ap;
  2256. if( pPrev==pLeft ){
  2257. pRet = pRight;
  2258. }else{
  2259. pLeft->apChild[pLeft->nChild-1] = pRight;
  2260. pRet = pLeft;
  2261. }
  2262. ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase];
  2263. assert( ap[0]==pPrev->pNear->apPhrase[0] );
  2264. memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase);
  2265. pParse->nPhrase--;
  2266. sqlite3Fts5ParseNodeFree(pPrev);
  2267. }
  2268. else{
  2269. pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0);
  2270. }
  2271. }
  2272. return pRet;
  2273. }
  2274. #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
  2275. static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){
  2276. sqlite3_int64 nByte = 0;
  2277. Fts5ExprTerm *p;
  2278. char *zQuoted;
  2279. /* Determine the maximum amount of space required. */
  2280. for(p=pTerm; p; p=p->pSynonym){
  2281. nByte += pTerm->nQueryTerm * 2 + 3 + 2;
  2282. }
  2283. zQuoted = sqlite3_malloc64(nByte);
  2284. if( zQuoted ){
  2285. int i = 0;
  2286. for(p=pTerm; p; p=p->pSynonym){
  2287. char *zIn = p->pTerm;
  2288. char *zEnd = &zIn[p->nQueryTerm];
  2289. zQuoted[i++] = '"';
  2290. while( zIn<zEnd ){
  2291. if( *zIn=='"' ) zQuoted[i++] = '"';
  2292. zQuoted[i++] = *zIn++;
  2293. }
  2294. zQuoted[i++] = '"';
  2295. if( p->pSynonym ) zQuoted[i++] = '|';
  2296. }
  2297. if( pTerm->bPrefix ){
  2298. zQuoted[i++] = ' ';
  2299. zQuoted[i++] = '*';
  2300. }
  2301. zQuoted[i++] = '\0';
  2302. }
  2303. return zQuoted;
  2304. }
  2305. static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){
  2306. char *zNew;
  2307. va_list ap;
  2308. va_start(ap, zFmt);
  2309. zNew = sqlite3_vmprintf(zFmt, ap);
  2310. va_end(ap);
  2311. if( zApp && zNew ){
  2312. char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew);
  2313. sqlite3_free(zNew);
  2314. zNew = zNew2;
  2315. }
  2316. sqlite3_free(zApp);
  2317. return zNew;
  2318. }
  2319. /*
  2320. ** Compose a tcl-readable representation of expression pExpr. Return a
  2321. ** pointer to a buffer containing that representation. It is the
  2322. ** responsibility of the caller to at some point free the buffer using
  2323. ** sqlite3_free().
  2324. */
  2325. static char *fts5ExprPrintTcl(
  2326. Fts5Config *pConfig,
  2327. const char *zNearsetCmd,
  2328. Fts5ExprNode *pExpr
  2329. ){
  2330. char *zRet = 0;
  2331. if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
  2332. Fts5ExprNearset *pNear = pExpr->pNear;
  2333. int i;
  2334. int iTerm;
  2335. zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd);
  2336. if( zRet==0 ) return 0;
  2337. if( pNear->pColset ){
  2338. int *aiCol = pNear->pColset->aiCol;
  2339. int nCol = pNear->pColset->nCol;
  2340. if( nCol==1 ){
  2341. zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]);
  2342. }else{
  2343. zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]);
  2344. for(i=1; i<pNear->pColset->nCol; i++){
  2345. zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]);
  2346. }
  2347. zRet = fts5PrintfAppend(zRet, "} ");
  2348. }
  2349. if( zRet==0 ) return 0;
  2350. }
  2351. if( pNear->nPhrase>1 ){
  2352. zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear);
  2353. if( zRet==0 ) return 0;
  2354. }
  2355. zRet = fts5PrintfAppend(zRet, "--");
  2356. if( zRet==0 ) return 0;
  2357. for(i=0; i<pNear->nPhrase; i++){
  2358. Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
  2359. zRet = fts5PrintfAppend(zRet, " {");
  2360. for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){
  2361. Fts5ExprTerm *p = &pPhrase->aTerm[iTerm];
  2362. zRet = fts5PrintfAppend(zRet, "%s%.*s", iTerm==0?"":" ",
  2363. p->nQueryTerm, p->pTerm
  2364. );
  2365. if( pPhrase->aTerm[iTerm].bPrefix ){
  2366. zRet = fts5PrintfAppend(zRet, "*");
  2367. }
  2368. }
  2369. if( zRet ) zRet = fts5PrintfAppend(zRet, "}");
  2370. if( zRet==0 ) return 0;
  2371. }
  2372. }else if( pExpr->eType==0 ){
  2373. zRet = sqlite3_mprintf("{}");
  2374. }else{
  2375. char const *zOp = 0;
  2376. int i;
  2377. switch( pExpr->eType ){
  2378. case FTS5_AND: zOp = "AND"; break;
  2379. case FTS5_NOT: zOp = "NOT"; break;
  2380. default:
  2381. assert( pExpr->eType==FTS5_OR );
  2382. zOp = "OR";
  2383. break;
  2384. }
  2385. zRet = sqlite3_mprintf("%s", zOp);
  2386. for(i=0; zRet && i<pExpr->nChild; i++){
  2387. char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]);
  2388. if( !z ){
  2389. sqlite3_free(zRet);
  2390. zRet = 0;
  2391. }else{
  2392. zRet = fts5PrintfAppend(zRet, " [%z]", z);
  2393. }
  2394. }
  2395. }
  2396. return zRet;
  2397. }
  2398. static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){
  2399. char *zRet = 0;
  2400. if( pExpr->eType==0 ){
  2401. return sqlite3_mprintf("\"\"");
  2402. }else
  2403. if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){
  2404. Fts5ExprNearset *pNear = pExpr->pNear;
  2405. int i;
  2406. int iTerm;
  2407. if( pNear->pColset ){
  2408. int ii;
  2409. Fts5Colset *pColset = pNear->pColset;
  2410. if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{");
  2411. for(ii=0; ii<pColset->nCol; ii++){
  2412. zRet = fts5PrintfAppend(zRet, "%s%s",
  2413. pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " "
  2414. );
  2415. }
  2416. if( zRet ){
  2417. zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : "");
  2418. }
  2419. if( zRet==0 ) return 0;
  2420. }
  2421. if( pNear->nPhrase>1 ){
  2422. zRet = fts5PrintfAppend(zRet, "NEAR(");
  2423. if( zRet==0 ) return 0;
  2424. }
  2425. for(i=0; i<pNear->nPhrase; i++){
  2426. Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
  2427. if( i!=0 ){
  2428. zRet = fts5PrintfAppend(zRet, " ");
  2429. if( zRet==0 ) return 0;
  2430. }
  2431. for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){
  2432. char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]);
  2433. if( zTerm ){
  2434. zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm);
  2435. sqlite3_free(zTerm);
  2436. }
  2437. if( zTerm==0 || zRet==0 ){
  2438. sqlite3_free(zRet);
  2439. return 0;
  2440. }
  2441. }
  2442. }
  2443. if( pNear->nPhrase>1 ){
  2444. zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear);
  2445. if( zRet==0 ) return 0;
  2446. }
  2447. }else{
  2448. char const *zOp = 0;
  2449. int i;
  2450. switch( pExpr->eType ){
  2451. case FTS5_AND: zOp = " AND "; break;
  2452. case FTS5_NOT: zOp = " NOT "; break;
  2453. default:
  2454. assert( pExpr->eType==FTS5_OR );
  2455. zOp = " OR ";
  2456. break;
  2457. }
  2458. for(i=0; i<pExpr->nChild; i++){
  2459. char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]);
  2460. if( z==0 ){
  2461. sqlite3_free(zRet);
  2462. zRet = 0;
  2463. }else{
  2464. int e = pExpr->apChild[i]->eType;
  2465. int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF);
  2466. zRet = fts5PrintfAppend(zRet, "%s%s%z%s",
  2467. (i==0 ? "" : zOp),
  2468. (b?"(":""), z, (b?")":"")
  2469. );
  2470. }
  2471. if( zRet==0 ) break;
  2472. }
  2473. }
  2474. return zRet;
  2475. }
  2476. /*
  2477. ** The implementation of user-defined scalar functions fts5_expr() (bTcl==0)
  2478. ** and fts5_expr_tcl() (bTcl!=0).
  2479. */
  2480. static void fts5ExprFunction(
  2481. sqlite3_context *pCtx, /* Function call context */
  2482. int nArg, /* Number of args */
  2483. sqlite3_value **apVal, /* Function arguments */
  2484. int bTcl
  2485. ){
  2486. Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
  2487. sqlite3 *db = sqlite3_context_db_handle(pCtx);
  2488. const char *zExpr = 0;
  2489. char *zErr = 0;
  2490. Fts5Expr *pExpr = 0;
  2491. int rc;
  2492. int i;
  2493. const char **azConfig; /* Array of arguments for Fts5Config */
  2494. const char *zNearsetCmd = "nearset";
  2495. int nConfig; /* Size of azConfig[] */
  2496. Fts5Config *pConfig = 0;
  2497. int iArg = 1;
  2498. if( nArg<1 ){
  2499. zErr = sqlite3_mprintf("wrong number of arguments to function %s",
  2500. bTcl ? "fts5_expr_tcl" : "fts5_expr"
  2501. );
  2502. sqlite3_result_error(pCtx, zErr, -1);
  2503. sqlite3_free(zErr);
  2504. return;
  2505. }
  2506. if( bTcl && nArg>1 ){
  2507. zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]);
  2508. iArg = 2;
  2509. }
  2510. nConfig = 3 + (nArg-iArg);
  2511. azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig);
  2512. if( azConfig==0 ){
  2513. sqlite3_result_error_nomem(pCtx);
  2514. return;
  2515. }
  2516. azConfig[0] = 0;
  2517. azConfig[1] = "main";
  2518. azConfig[2] = "tbl";
  2519. for(i=3; iArg<nArg; iArg++){
  2520. const char *z = (const char*)sqlite3_value_text(apVal[iArg]);
  2521. azConfig[i++] = (z ? z : "");
  2522. }
  2523. zExpr = (const char*)sqlite3_value_text(apVal[0]);
  2524. if( zExpr==0 ) zExpr = "";
  2525. rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr);
  2526. if( rc==SQLITE_OK ){
  2527. rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr);
  2528. }
  2529. if( rc==SQLITE_OK ){
  2530. char *zText;
  2531. if( pExpr->pRoot->xNext==0 ){
  2532. zText = sqlite3_mprintf("");
  2533. }else if( bTcl ){
  2534. zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
  2535. }else{
  2536. zText = fts5ExprPrint(pConfig, pExpr->pRoot);
  2537. }
  2538. if( zText==0 ){
  2539. rc = SQLITE_NOMEM;
  2540. }else{
  2541. sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT);
  2542. sqlite3_free(zText);
  2543. }
  2544. }
  2545. if( rc!=SQLITE_OK ){
  2546. if( zErr ){
  2547. sqlite3_result_error(pCtx, zErr, -1);
  2548. sqlite3_free(zErr);
  2549. }else{
  2550. sqlite3_result_error_code(pCtx, rc);
  2551. }
  2552. }
  2553. sqlite3_free((void *)azConfig);
  2554. sqlite3Fts5ConfigFree(pConfig);
  2555. sqlite3Fts5ExprFree(pExpr);
  2556. }
  2557. static void fts5ExprFunctionHr(
  2558. sqlite3_context *pCtx, /* Function call context */
  2559. int nArg, /* Number of args */
  2560. sqlite3_value **apVal /* Function arguments */
  2561. ){
  2562. fts5ExprFunction(pCtx, nArg, apVal, 0);
  2563. }
  2564. static void fts5ExprFunctionTcl(
  2565. sqlite3_context *pCtx, /* Function call context */
  2566. int nArg, /* Number of args */
  2567. sqlite3_value **apVal /* Function arguments */
  2568. ){
  2569. fts5ExprFunction(pCtx, nArg, apVal, 1);
  2570. }
  2571. /*
  2572. ** The implementation of an SQLite user-defined-function that accepts a
  2573. ** single integer as an argument. If the integer is an alpha-numeric
  2574. ** unicode code point, 1 is returned. Otherwise 0.
  2575. */
  2576. static void fts5ExprIsAlnum(
  2577. sqlite3_context *pCtx, /* Function call context */
  2578. int nArg, /* Number of args */
  2579. sqlite3_value **apVal /* Function arguments */
  2580. ){
  2581. int iCode;
  2582. u8 aArr[32];
  2583. if( nArg!=1 ){
  2584. sqlite3_result_error(pCtx,
  2585. "wrong number of arguments to function fts5_isalnum", -1
  2586. );
  2587. return;
  2588. }
  2589. memset(aArr, 0, sizeof(aArr));
  2590. sqlite3Fts5UnicodeCatParse("L*", aArr);
  2591. sqlite3Fts5UnicodeCatParse("N*", aArr);
  2592. sqlite3Fts5UnicodeCatParse("Co", aArr);
  2593. iCode = sqlite3_value_int(apVal[0]);
  2594. sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]);
  2595. }
  2596. static void fts5ExprFold(
  2597. sqlite3_context *pCtx, /* Function call context */
  2598. int nArg, /* Number of args */
  2599. sqlite3_value **apVal /* Function arguments */
  2600. ){
  2601. if( nArg!=1 && nArg!=2 ){
  2602. sqlite3_result_error(pCtx,
  2603. "wrong number of arguments to function fts5_fold", -1
  2604. );
  2605. }else{
  2606. int iCode;
  2607. int bRemoveDiacritics = 0;
  2608. iCode = sqlite3_value_int(apVal[0]);
  2609. if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]);
  2610. sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics));
  2611. }
  2612. }
  2613. #endif /* if SQLITE_TEST || SQLITE_FTS5_DEBUG */
  2614. /*
  2615. ** This is called during initialization to register the fts5_expr() scalar
  2616. ** UDF with the SQLite handle passed as the only argument.
  2617. */
  2618. int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){
  2619. #if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG)
  2620. struct Fts5ExprFunc {
  2621. const char *z;
  2622. void (*x)(sqlite3_context*,int,sqlite3_value**);
  2623. } aFunc[] = {
  2624. { "fts5_expr", fts5ExprFunctionHr },
  2625. { "fts5_expr_tcl", fts5ExprFunctionTcl },
  2626. { "fts5_isalnum", fts5ExprIsAlnum },
  2627. { "fts5_fold", fts5ExprFold },
  2628. };
  2629. int i;
  2630. int rc = SQLITE_OK;
  2631. void *pCtx = (void*)pGlobal;
  2632. for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
  2633. struct Fts5ExprFunc *p = &aFunc[i];
  2634. rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
  2635. }
  2636. #else
  2637. int rc = SQLITE_OK;
  2638. UNUSED_PARAM2(pGlobal,db);
  2639. #endif
  2640. /* Avoid warnings indicating that sqlite3Fts5ParserTrace() and
  2641. ** sqlite3Fts5ParserFallback() are unused */
  2642. #ifndef NDEBUG
  2643. (void)sqlite3Fts5ParserTrace;
  2644. #endif
  2645. (void)sqlite3Fts5ParserFallback;
  2646. return rc;
  2647. }
  2648. /*
  2649. ** Return the number of phrases in expression pExpr.
  2650. */
  2651. int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){
  2652. return (pExpr ? pExpr->nPhrase : 0);
  2653. }
  2654. /*
  2655. ** Return the number of terms in the iPhrase'th phrase in pExpr.
  2656. */
  2657. int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){
  2658. if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0;
  2659. return pExpr->apExprPhrase[iPhrase]->nTerm;
  2660. }
  2661. /*
  2662. ** This function is used to access the current position list for phrase
  2663. ** iPhrase.
  2664. */
  2665. int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){
  2666. int nRet;
  2667. Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
  2668. Fts5ExprNode *pNode = pPhrase->pNode;
  2669. if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){
  2670. *pa = pPhrase->poslist.p;
  2671. nRet = pPhrase->poslist.n;
  2672. }else{
  2673. *pa = 0;
  2674. nRet = 0;
  2675. }
  2676. return nRet;
  2677. }
  2678. struct Fts5PoslistPopulator {
  2679. Fts5PoslistWriter writer;
  2680. int bOk; /* True if ok to populate */
  2681. int bMiss;
  2682. };
  2683. /*
  2684. ** Clear the position lists associated with all phrases in the expression
  2685. ** passed as the first argument. Argument bLive is true if the expression
  2686. ** might be pointing to a real entry, otherwise it has just been reset.
  2687. **
  2688. ** At present this function is only used for detail=col and detail=none
  2689. ** fts5 tables. This implies that all phrases must be at most 1 token
  2690. ** in size, as phrase matches are not supported without detail=full.
  2691. */
  2692. Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){
  2693. Fts5PoslistPopulator *pRet;
  2694. pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
  2695. if( pRet ){
  2696. int i;
  2697. memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase);
  2698. for(i=0; i<pExpr->nPhrase; i++){
  2699. Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist;
  2700. Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
  2701. assert( pExpr->apExprPhrase[i]->nTerm<=1 );
  2702. if( bLive &&
  2703. (pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof)
  2704. ){
  2705. pRet[i].bMiss = 1;
  2706. }else{
  2707. pBuf->n = 0;
  2708. }
  2709. }
  2710. }
  2711. return pRet;
  2712. }
  2713. struct Fts5ExprCtx {
  2714. Fts5Expr *pExpr;
  2715. Fts5PoslistPopulator *aPopulator;
  2716. i64 iOff;
  2717. };
  2718. typedef struct Fts5ExprCtx Fts5ExprCtx;
  2719. /*
  2720. ** TODO: Make this more efficient!
  2721. */
  2722. static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){
  2723. int i;
  2724. for(i=0; i<pColset->nCol; i++){
  2725. if( pColset->aiCol[i]==iCol ) return 1;
  2726. }
  2727. return 0;
  2728. }
  2729. /*
  2730. ** pToken is a buffer nToken bytes in size that may or may not contain
  2731. ** an embedded 0x00 byte. If it does, return the number of bytes in
  2732. ** the buffer before the 0x00. If it does not, return nToken.
  2733. */
  2734. static int fts5QueryTerm(const char *pToken, int nToken){
  2735. int ii;
  2736. for(ii=0; ii<nToken && pToken[ii]; ii++){}
  2737. return ii;
  2738. }
  2739. static int fts5ExprPopulatePoslistsCb(
  2740. void *pCtx, /* Copy of 2nd argument to xTokenize() */
  2741. int tflags, /* Mask of FTS5_TOKEN_* flags */
  2742. const char *pToken, /* Pointer to buffer containing token */
  2743. int nToken, /* Size of token in bytes */
  2744. int iUnused1, /* Byte offset of token within input text */
  2745. int iUnused2 /* Byte offset of end of token within input text */
  2746. ){
  2747. Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
  2748. Fts5Expr *pExpr = p->pExpr;
  2749. int i;
  2750. int nQuery = nToken;
  2751. i64 iRowid = pExpr->pRoot->iRowid;
  2752. UNUSED_PARAM2(iUnused1, iUnused2);
  2753. if( nQuery>FTS5_MAX_TOKEN_SIZE ) nQuery = FTS5_MAX_TOKEN_SIZE;
  2754. if( pExpr->pConfig->bTokendata ){
  2755. nQuery = fts5QueryTerm(pToken, nQuery);
  2756. }
  2757. if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
  2758. for(i=0; i<pExpr->nPhrase; i++){
  2759. Fts5ExprTerm *pT;
  2760. if( p->aPopulator[i].bOk==0 ) continue;
  2761. for(pT=&pExpr->apExprPhrase[i]->aTerm[0]; pT; pT=pT->pSynonym){
  2762. if( (pT->nQueryTerm==nQuery || (pT->nQueryTerm<nQuery && pT->bPrefix))
  2763. && memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0
  2764. ){
  2765. int rc = sqlite3Fts5PoslistWriterAppend(
  2766. &pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff
  2767. );
  2768. if( rc==SQLITE_OK && (pExpr->pConfig->bTokendata || pT->bPrefix) ){
  2769. int iCol = p->iOff>>32;
  2770. int iTokOff = p->iOff & 0x7FFFFFFF;
  2771. rc = sqlite3Fts5IndexIterWriteTokendata(
  2772. pT->pIter, pToken, nToken, iRowid, iCol, iTokOff
  2773. );
  2774. }
  2775. if( rc ) return rc;
  2776. break;
  2777. }
  2778. }
  2779. }
  2780. return SQLITE_OK;
  2781. }
  2782. int sqlite3Fts5ExprPopulatePoslists(
  2783. Fts5Config *pConfig,
  2784. Fts5Expr *pExpr,
  2785. Fts5PoslistPopulator *aPopulator,
  2786. int iCol,
  2787. const char *z, int n
  2788. ){
  2789. int i;
  2790. Fts5ExprCtx sCtx;
  2791. sCtx.pExpr = pExpr;
  2792. sCtx.aPopulator = aPopulator;
  2793. sCtx.iOff = (((i64)iCol) << 32) - 1;
  2794. for(i=0; i<pExpr->nPhrase; i++){
  2795. Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode;
  2796. Fts5Colset *pColset = pNode->pNear->pColset;
  2797. if( (pColset && 0==fts5ExprColsetTest(pColset, iCol))
  2798. || aPopulator[i].bMiss
  2799. ){
  2800. aPopulator[i].bOk = 0;
  2801. }else{
  2802. aPopulator[i].bOk = 1;
  2803. }
  2804. }
  2805. return sqlite3Fts5Tokenize(pConfig,
  2806. FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb
  2807. );
  2808. }
  2809. static void fts5ExprClearPoslists(Fts5ExprNode *pNode){
  2810. if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){
  2811. pNode->pNear->apPhrase[0]->poslist.n = 0;
  2812. }else{
  2813. int i;
  2814. for(i=0; i<pNode->nChild; i++){
  2815. fts5ExprClearPoslists(pNode->apChild[i]);
  2816. }
  2817. }
  2818. }
  2819. static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){
  2820. pNode->iRowid = iRowid;
  2821. pNode->bEof = 0;
  2822. switch( pNode->eType ){
  2823. case 0:
  2824. case FTS5_TERM:
  2825. case FTS5_STRING:
  2826. return (pNode->pNear->apPhrase[0]->poslist.n>0);
  2827. case FTS5_AND: {
  2828. int i;
  2829. for(i=0; i<pNode->nChild; i++){
  2830. if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){
  2831. fts5ExprClearPoslists(pNode);
  2832. return 0;
  2833. }
  2834. }
  2835. break;
  2836. }
  2837. case FTS5_OR: {
  2838. int i;
  2839. int bRet = 0;
  2840. for(i=0; i<pNode->nChild; i++){
  2841. if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){
  2842. bRet = 1;
  2843. }
  2844. }
  2845. return bRet;
  2846. }
  2847. default: {
  2848. assert( pNode->eType==FTS5_NOT );
  2849. if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid)
  2850. || 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid)
  2851. ){
  2852. fts5ExprClearPoslists(pNode);
  2853. return 0;
  2854. }
  2855. break;
  2856. }
  2857. }
  2858. return 1;
  2859. }
  2860. void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){
  2861. fts5ExprCheckPoslists(pExpr->pRoot, iRowid);
  2862. }
  2863. /*
  2864. ** This function is only called for detail=columns tables.
  2865. */
  2866. int sqlite3Fts5ExprPhraseCollist(
  2867. Fts5Expr *pExpr,
  2868. int iPhrase,
  2869. const u8 **ppCollist,
  2870. int *pnCollist
  2871. ){
  2872. Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
  2873. Fts5ExprNode *pNode = pPhrase->pNode;
  2874. int rc = SQLITE_OK;
  2875. assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
  2876. assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
  2877. if( pNode->bEof==0
  2878. && pNode->iRowid==pExpr->pRoot->iRowid
  2879. && pPhrase->poslist.n>0
  2880. ){
  2881. Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
  2882. if( pTerm->pSynonym ){
  2883. Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
  2884. rc = fts5ExprSynonymList(
  2885. pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
  2886. );
  2887. }else{
  2888. *ppCollist = pPhrase->aTerm[0].pIter->pData;
  2889. *pnCollist = pPhrase->aTerm[0].pIter->nData;
  2890. }
  2891. }else{
  2892. *ppCollist = 0;
  2893. *pnCollist = 0;
  2894. }
  2895. return rc;
  2896. }
  2897. /*
  2898. ** Does the work of the fts5_api.xQueryToken() API method.
  2899. */
  2900. int sqlite3Fts5ExprQueryToken(
  2901. Fts5Expr *pExpr,
  2902. int iPhrase,
  2903. int iToken,
  2904. const char **ppOut,
  2905. int *pnOut
  2906. ){
  2907. Fts5ExprPhrase *pPhrase = 0;
  2908. if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
  2909. return SQLITE_RANGE;
  2910. }
  2911. pPhrase = pExpr->apExprPhrase[iPhrase];
  2912. if( iToken<0 || iToken>=pPhrase->nTerm ){
  2913. return SQLITE_RANGE;
  2914. }
  2915. *ppOut = pPhrase->aTerm[iToken].pTerm;
  2916. *pnOut = pPhrase->aTerm[iToken].nFullTerm;
  2917. return SQLITE_OK;
  2918. }
  2919. /*
  2920. ** Does the work of the fts5_api.xInstToken() API method.
  2921. */
  2922. int sqlite3Fts5ExprInstToken(
  2923. Fts5Expr *pExpr,
  2924. i64 iRowid,
  2925. int iPhrase,
  2926. int iCol,
  2927. int iOff,
  2928. int iToken,
  2929. const char **ppOut,
  2930. int *pnOut
  2931. ){
  2932. Fts5ExprPhrase *pPhrase = 0;
  2933. Fts5ExprTerm *pTerm = 0;
  2934. int rc = SQLITE_OK;
  2935. if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){
  2936. return SQLITE_RANGE;
  2937. }
  2938. pPhrase = pExpr->apExprPhrase[iPhrase];
  2939. if( iToken<0 || iToken>=pPhrase->nTerm ){
  2940. return SQLITE_RANGE;
  2941. }
  2942. pTerm = &pPhrase->aTerm[iToken];
  2943. if( pExpr->pConfig->bTokendata || pTerm->bPrefix ){
  2944. rc = sqlite3Fts5IterToken(
  2945. pTerm->pIter, pTerm->pTerm, pTerm->nQueryTerm,
  2946. iRowid, iCol, iOff+iToken, ppOut, pnOut
  2947. );
  2948. }else{
  2949. *ppOut = pTerm->pTerm;
  2950. *pnOut = pTerm->nFullTerm;
  2951. }
  2952. return rc;
  2953. }
  2954. /*
  2955. ** Clear the token mappings for all Fts5IndexIter objects managed by
  2956. ** the expression passed as the only argument.
  2957. */
  2958. void sqlite3Fts5ExprClearTokens(Fts5Expr *pExpr){
  2959. int ii;
  2960. for(ii=0; ii<pExpr->nPhrase; ii++){
  2961. Fts5ExprTerm *pT;
  2962. for(pT=&pExpr->apExprPhrase[ii]->aTerm[0]; pT; pT=pT->pSynonym){
  2963. sqlite3Fts5IndexIterClearTokendata(pT->pIter);
  2964. }
  2965. }
  2966. }