|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "fts5Int.h" |
|
|
#include "fts5parse.h" |
|
|
|
|
|
#ifndef SQLITE_FTS5_MAX_EXPR_DEPTH |
|
|
# define SQLITE_FTS5_MAX_EXPR_DEPTH 256 |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define FTS5_EOF 0 |
|
|
|
|
|
#define FTS5_LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) |
|
|
|
|
|
typedef struct Fts5ExprTerm Fts5ExprTerm; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void *sqlite3Fts5ParserAlloc(void *(*mallocProc)(u64)); |
|
|
void sqlite3Fts5ParserFree(void*, void (*freeProc)(void*)); |
|
|
void sqlite3Fts5Parser(void*, int, Fts5Token, Fts5Parse*); |
|
|
#ifndef NDEBUG |
|
|
#include <stdio.h> |
|
|
void sqlite3Fts5ParserTrace(FILE*, char*); |
|
|
#endif |
|
|
int sqlite3Fts5ParserFallback(int); |
|
|
|
|
|
|
|
|
struct Fts5Expr { |
|
|
Fts5Index *pIndex; |
|
|
Fts5Config *pConfig; |
|
|
Fts5ExprNode *pRoot; |
|
|
int bDesc; |
|
|
int nPhrase; |
|
|
Fts5ExprPhrase **apExprPhrase; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5ExprNode { |
|
|
int eType; |
|
|
int bEof; |
|
|
int bNomatch; |
|
|
int iHeight; |
|
|
|
|
|
|
|
|
int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64); |
|
|
|
|
|
i64 iRowid; |
|
|
Fts5ExprNearset *pNear; |
|
|
|
|
|
|
|
|
|
|
|
int nChild; |
|
|
Fts5ExprNode *apChild[1]; |
|
|
}; |
|
|
|
|
|
#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5ExprTerm { |
|
|
u8 bPrefix; |
|
|
u8 bFirst; |
|
|
char *pTerm; |
|
|
int nQueryTerm; |
|
|
int nFullTerm; |
|
|
Fts5IndexIter *pIter; |
|
|
Fts5ExprTerm *pSynonym; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5ExprPhrase { |
|
|
Fts5ExprNode *pNode; |
|
|
Fts5Buffer poslist; |
|
|
int nTerm; |
|
|
Fts5ExprTerm aTerm[1]; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5ExprNearset { |
|
|
int nNear; |
|
|
Fts5Colset *pColset; |
|
|
int nPhrase; |
|
|
Fts5ExprPhrase *apPhrase[1]; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Fts5Parse { |
|
|
Fts5Config *pConfig; |
|
|
char *zErr; |
|
|
int rc; |
|
|
int nPhrase; |
|
|
Fts5ExprPhrase **apPhrase; |
|
|
Fts5ExprNode *pExpr; |
|
|
int bPhraseToAnd; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG |
|
|
static void assert_expr_depth_ok(int rc, Fts5ExprNode *p){ |
|
|
if( rc==SQLITE_OK ){ |
|
|
if( p->eType==FTS5_TERM || p->eType==FTS5_STRING || p->eType==0 ){ |
|
|
assert( p->iHeight==0 ); |
|
|
}else{ |
|
|
int ii; |
|
|
int iMaxChild = 0; |
|
|
for(ii=0; ii<p->nChild; ii++){ |
|
|
Fts5ExprNode *pChild = p->apChild[ii]; |
|
|
iMaxChild = MAX(iMaxChild, pChild->iHeight); |
|
|
assert_expr_depth_ok(SQLITE_OK, pChild); |
|
|
} |
|
|
assert( p->iHeight==iMaxChild+1 ); |
|
|
} |
|
|
} |
|
|
} |
|
|
#else |
|
|
# define assert_expr_depth_ok(rc, p) |
|
|
#endif |
|
|
|
|
|
void sqlite3Fts5ParseError(Fts5Parse *pParse, const char *zFmt, ...){ |
|
|
va_list ap; |
|
|
va_start(ap, zFmt); |
|
|
if( pParse->rc==SQLITE_OK ){ |
|
|
assert( pParse->zErr==0 ); |
|
|
pParse->zErr = sqlite3_vmprintf(zFmt, ap); |
|
|
pParse->rc = SQLITE_ERROR; |
|
|
} |
|
|
va_end(ap); |
|
|
} |
|
|
|
|
|
static int fts5ExprIsspace(char t){ |
|
|
return t==' ' || t=='\t' || t=='\n' || t=='\r'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprGetToken( |
|
|
Fts5Parse *pParse, |
|
|
const char **pz, |
|
|
Fts5Token *pToken |
|
|
){ |
|
|
const char *z = *pz; |
|
|
int tok; |
|
|
|
|
|
|
|
|
while( fts5ExprIsspace(*z) ) z++; |
|
|
|
|
|
pToken->p = z; |
|
|
pToken->n = 1; |
|
|
switch( *z ){ |
|
|
case '(': tok = FTS5_LP; break; |
|
|
case ')': tok = FTS5_RP; break; |
|
|
case '{': tok = FTS5_LCP; break; |
|
|
case '}': tok = FTS5_RCP; break; |
|
|
case ':': tok = FTS5_COLON; break; |
|
|
case ',': tok = FTS5_COMMA; break; |
|
|
case '+': tok = FTS5_PLUS; break; |
|
|
case '*': tok = FTS5_STAR; break; |
|
|
case '-': tok = FTS5_MINUS; break; |
|
|
case '^': tok = FTS5_CARET; break; |
|
|
case '\0': tok = FTS5_EOF; break; |
|
|
|
|
|
case '"': { |
|
|
const char *z2; |
|
|
tok = FTS5_STRING; |
|
|
|
|
|
for(z2=&z[1]; 1; z2++){ |
|
|
if( z2[0]=='"' ){ |
|
|
z2++; |
|
|
if( z2[0]!='"' ) break; |
|
|
} |
|
|
if( z2[0]=='\0' ){ |
|
|
sqlite3Fts5ParseError(pParse, "unterminated string"); |
|
|
return FTS5_EOF; |
|
|
} |
|
|
} |
|
|
pToken->n = (z2 - z); |
|
|
break; |
|
|
} |
|
|
|
|
|
default: { |
|
|
const char *z2; |
|
|
if( sqlite3Fts5IsBareword(z[0])==0 ){ |
|
|
sqlite3Fts5ParseError(pParse, "fts5: syntax error near \"%.1s\"", z); |
|
|
return FTS5_EOF; |
|
|
} |
|
|
tok = FTS5_STRING; |
|
|
for(z2=&z[1]; sqlite3Fts5IsBareword(*z2); z2++); |
|
|
pToken->n = (z2 - z); |
|
|
if( pToken->n==2 && memcmp(pToken->p, "OR", 2)==0 ) tok = FTS5_OR; |
|
|
if( pToken->n==3 && memcmp(pToken->p, "NOT", 3)==0 ) tok = FTS5_NOT; |
|
|
if( pToken->n==3 && memcmp(pToken->p, "AND", 3)==0 ) tok = FTS5_AND; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
*pz = &pToken->p[pToken->n]; |
|
|
return tok; |
|
|
} |
|
|
|
|
|
static void *fts5ParseAlloc(u64 t){ return sqlite3_malloc64((sqlite3_int64)t);} |
|
|
static void fts5ParseFree(void *p){ sqlite3_free(p); } |
|
|
|
|
|
int sqlite3Fts5ExprNew( |
|
|
Fts5Config *pConfig, |
|
|
int bPhraseToAnd, |
|
|
int iCol, |
|
|
const char *zExpr, |
|
|
Fts5Expr **ppNew, |
|
|
char **pzErr |
|
|
){ |
|
|
Fts5Parse sParse; |
|
|
Fts5Token token; |
|
|
const char *z = zExpr; |
|
|
int t; |
|
|
void *pEngine; |
|
|
Fts5Expr *pNew; |
|
|
|
|
|
*ppNew = 0; |
|
|
*pzErr = 0; |
|
|
memset(&sParse, 0, sizeof(sParse)); |
|
|
sParse.bPhraseToAnd = bPhraseToAnd; |
|
|
pEngine = sqlite3Fts5ParserAlloc(fts5ParseAlloc); |
|
|
if( pEngine==0 ){ return SQLITE_NOMEM; } |
|
|
sParse.pConfig = pConfig; |
|
|
|
|
|
do { |
|
|
t = fts5ExprGetToken(&sParse, &z, &token); |
|
|
sqlite3Fts5Parser(pEngine, t, token, &sParse); |
|
|
}while( sParse.rc==SQLITE_OK && t!=FTS5_EOF ); |
|
|
sqlite3Fts5ParserFree(pEngine, fts5ParseFree); |
|
|
|
|
|
assert( sParse.pExpr || sParse.rc!=SQLITE_OK ); |
|
|
assert_expr_depth_ok(sParse.rc, sParse.pExpr); |
|
|
|
|
|
|
|
|
|
|
|
if( sParse.rc==SQLITE_OK && iCol<pConfig->nCol ){ |
|
|
int n = sizeof(Fts5Colset); |
|
|
Fts5Colset *pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&sParse.rc, n); |
|
|
if( pColset ){ |
|
|
pColset->nCol = 1; |
|
|
pColset->aiCol[0] = iCol; |
|
|
sqlite3Fts5ParseSetColset(&sParse, sParse.pExpr, pColset); |
|
|
} |
|
|
} |
|
|
|
|
|
assert( sParse.rc!=SQLITE_OK || sParse.zErr==0 ); |
|
|
if( sParse.rc==SQLITE_OK ){ |
|
|
*ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr)); |
|
|
if( pNew==0 ){ |
|
|
sParse.rc = SQLITE_NOMEM; |
|
|
sqlite3Fts5ParseNodeFree(sParse.pExpr); |
|
|
}else{ |
|
|
pNew->pRoot = sParse.pExpr; |
|
|
pNew->pIndex = 0; |
|
|
pNew->pConfig = pConfig; |
|
|
pNew->apExprPhrase = sParse.apPhrase; |
|
|
pNew->nPhrase = sParse.nPhrase; |
|
|
pNew->bDesc = 0; |
|
|
sParse.apPhrase = 0; |
|
|
} |
|
|
}else{ |
|
|
sqlite3Fts5ParseNodeFree(sParse.pExpr); |
|
|
} |
|
|
|
|
|
sqlite3_free(sParse.apPhrase); |
|
|
if( 0==*pzErr ){ |
|
|
*pzErr = sParse.zErr; |
|
|
}else{ |
|
|
sqlite3_free(sParse.zErr); |
|
|
} |
|
|
return sParse.rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprCountChar(const char *z, int nByte){ |
|
|
int nRet = 0; |
|
|
int ii; |
|
|
for(ii=0; ii<nByte; ii++){ |
|
|
if( (z[ii] & 0xC0)!=0x80 ) nRet++; |
|
|
} |
|
|
return nRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprPattern( |
|
|
Fts5Config *pConfig, int bGlob, int iCol, const char *zText, Fts5Expr **pp |
|
|
){ |
|
|
i64 nText = strlen(zText); |
|
|
char *zExpr = (char*)sqlite3_malloc64(nText*4 + 1); |
|
|
int rc = SQLITE_OK; |
|
|
|
|
|
if( zExpr==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
char aSpec[3]; |
|
|
int iOut = 0; |
|
|
int i = 0; |
|
|
int iFirst = 0; |
|
|
|
|
|
if( bGlob==0 ){ |
|
|
aSpec[0] = '_'; |
|
|
aSpec[1] = '%'; |
|
|
aSpec[2] = 0; |
|
|
}else{ |
|
|
aSpec[0] = '*'; |
|
|
aSpec[1] = '?'; |
|
|
aSpec[2] = '['; |
|
|
} |
|
|
|
|
|
while( i<=nText ){ |
|
|
if( i==nText |
|
|
|| zText[i]==aSpec[0] || zText[i]==aSpec[1] || zText[i]==aSpec[2] |
|
|
){ |
|
|
|
|
|
if( fts5ExprCountChar(&zText[iFirst], i-iFirst)>=3 ){ |
|
|
int jj; |
|
|
zExpr[iOut++] = '"'; |
|
|
for(jj=iFirst; jj<i; jj++){ |
|
|
zExpr[iOut++] = zText[jj]; |
|
|
if( zText[jj]=='"' ) zExpr[iOut++] = '"'; |
|
|
} |
|
|
zExpr[iOut++] = '"'; |
|
|
zExpr[iOut++] = ' '; |
|
|
} |
|
|
if( zText[i]==aSpec[2] ){ |
|
|
i += 2; |
|
|
if( zText[i-1]=='^' ) i++; |
|
|
while( i<nText && zText[i]!=']' ) i++; |
|
|
} |
|
|
iFirst = i+1; |
|
|
} |
|
|
i++; |
|
|
} |
|
|
if( iOut>0 ){ |
|
|
int bAnd = 0; |
|
|
if( pConfig->eDetail!=FTS5_DETAIL_FULL ){ |
|
|
bAnd = 1; |
|
|
if( pConfig->eDetail==FTS5_DETAIL_NONE ){ |
|
|
iCol = pConfig->nCol; |
|
|
} |
|
|
} |
|
|
zExpr[iOut] = '\0'; |
|
|
rc = sqlite3Fts5ExprNew(pConfig, bAnd, iCol, zExpr, pp,pConfig->pzErrmsg); |
|
|
}else{ |
|
|
*pp = 0; |
|
|
} |
|
|
sqlite3_free(zExpr); |
|
|
} |
|
|
|
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseNodeFree(Fts5ExprNode *p){ |
|
|
if( p ){ |
|
|
int i; |
|
|
for(i=0; i<p->nChild; i++){ |
|
|
sqlite3Fts5ParseNodeFree(p->apChild[i]); |
|
|
} |
|
|
sqlite3Fts5ParseNearsetFree(p->pNear); |
|
|
sqlite3_free(p); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ExprFree(Fts5Expr *p){ |
|
|
if( p ){ |
|
|
sqlite3Fts5ParseNodeFree(p->pRoot); |
|
|
sqlite3_free(p->apExprPhrase); |
|
|
sqlite3_free(p); |
|
|
} |
|
|
} |
|
|
|
|
|
int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){ |
|
|
Fts5Parse sParse; |
|
|
memset(&sParse, 0, sizeof(sParse)); |
|
|
|
|
|
if( *pp1 && p2 ){ |
|
|
Fts5Expr *p1 = *pp1; |
|
|
int nPhrase = p1->nPhrase + p2->nPhrase; |
|
|
|
|
|
p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0); |
|
|
p2->pRoot = 0; |
|
|
|
|
|
if( sParse.rc==SQLITE_OK ){ |
|
|
Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc( |
|
|
p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*) |
|
|
); |
|
|
if( ap==0 ){ |
|
|
sParse.rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
int i; |
|
|
memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*)); |
|
|
for(i=0; i<p2->nPhrase; i++){ |
|
|
ap[i] = p2->apExprPhrase[i]; |
|
|
} |
|
|
p1->nPhrase = nPhrase; |
|
|
p1->apExprPhrase = ap; |
|
|
} |
|
|
} |
|
|
sqlite3_free(p2->apExprPhrase); |
|
|
sqlite3_free(p2); |
|
|
}else if( p2 ){ |
|
|
*pp1 = p2; |
|
|
} |
|
|
|
|
|
return sParse.rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){ |
|
|
i64 iRet = 0; |
|
|
int bRetValid = 0; |
|
|
Fts5ExprTerm *p; |
|
|
|
|
|
assert( pTerm ); |
|
|
assert( pTerm->pSynonym ); |
|
|
assert( bDesc==0 || bDesc==1 ); |
|
|
for(p=pTerm; p; p=p->pSynonym){ |
|
|
if( 0==sqlite3Fts5IterEof(p->pIter) ){ |
|
|
i64 iRowid = p->pIter->iRowid; |
|
|
if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){ |
|
|
iRet = iRowid; |
|
|
bRetValid = 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if( pbEof && bRetValid==0 ) *pbEof = 1; |
|
|
return iRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprSynonymList( |
|
|
Fts5ExprTerm *pTerm, |
|
|
i64 iRowid, |
|
|
Fts5Buffer *pBuf, |
|
|
u8 **pa, int *pn |
|
|
){ |
|
|
Fts5PoslistReader aStatic[4]; |
|
|
Fts5PoslistReader *aIter = aStatic; |
|
|
int nIter = 0; |
|
|
int nAlloc = 4; |
|
|
int rc = SQLITE_OK; |
|
|
Fts5ExprTerm *p; |
|
|
|
|
|
assert( pTerm->pSynonym ); |
|
|
for(p=pTerm; p; p=p->pSynonym){ |
|
|
Fts5IndexIter *pIter = p->pIter; |
|
|
if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){ |
|
|
if( pIter->nData==0 ) continue; |
|
|
if( nIter==nAlloc ){ |
|
|
sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * nAlloc * 2; |
|
|
Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc64(nByte); |
|
|
if( aNew==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
goto synonym_poslist_out; |
|
|
} |
|
|
memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter); |
|
|
nAlloc = nAlloc*2; |
|
|
if( aIter!=aStatic ) sqlite3_free(aIter); |
|
|
aIter = aNew; |
|
|
} |
|
|
sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]); |
|
|
assert( aIter[nIter].bEof==0 ); |
|
|
nIter++; |
|
|
} |
|
|
} |
|
|
|
|
|
if( nIter==1 ){ |
|
|
*pa = (u8*)aIter[0].a; |
|
|
*pn = aIter[0].n; |
|
|
}else{ |
|
|
Fts5PoslistWriter writer = {0}; |
|
|
i64 iPrev = -1; |
|
|
fts5BufferZero(pBuf); |
|
|
while( 1 ){ |
|
|
int i; |
|
|
i64 iMin = FTS5_LARGEST_INT64; |
|
|
for(i=0; i<nIter; i++){ |
|
|
if( aIter[i].bEof==0 ){ |
|
|
if( aIter[i].iPos==iPrev ){ |
|
|
if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) continue; |
|
|
} |
|
|
if( aIter[i].iPos<iMin ){ |
|
|
iMin = aIter[i].iPos; |
|
|
} |
|
|
} |
|
|
} |
|
|
if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break; |
|
|
rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin); |
|
|
iPrev = iMin; |
|
|
} |
|
|
if( rc==SQLITE_OK ){ |
|
|
*pa = pBuf->p; |
|
|
*pn = pBuf->n; |
|
|
} |
|
|
} |
|
|
|
|
|
synonym_poslist_out: |
|
|
if( aIter!=aStatic ) sqlite3_free(aIter); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprPhraseIsMatch( |
|
|
Fts5ExprNode *pNode, |
|
|
Fts5ExprPhrase *pPhrase, |
|
|
int *pbMatch |
|
|
){ |
|
|
Fts5PoslistWriter writer = {0}; |
|
|
Fts5PoslistReader aStatic[4]; |
|
|
Fts5PoslistReader *aIter = aStatic; |
|
|
int i; |
|
|
int rc = SQLITE_OK; |
|
|
int bFirst = pPhrase->aTerm[0].bFirst; |
|
|
|
|
|
fts5BufferZero(&pPhrase->poslist); |
|
|
|
|
|
|
|
|
|
|
|
if( pPhrase->nTerm>ArraySize(aStatic) ){ |
|
|
sqlite3_int64 nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm; |
|
|
aIter = (Fts5PoslistReader*)sqlite3_malloc64(nByte); |
|
|
if( !aIter ) return SQLITE_NOMEM; |
|
|
} |
|
|
memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm); |
|
|
|
|
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){ |
|
|
Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; |
|
|
int n = 0; |
|
|
int bFlag = 0; |
|
|
u8 *a = 0; |
|
|
if( pTerm->pSynonym ){ |
|
|
Fts5Buffer buf = {0, 0, 0}; |
|
|
rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n); |
|
|
if( rc ){ |
|
|
sqlite3_free(a); |
|
|
goto ismatch_out; |
|
|
} |
|
|
if( a==buf.p ) bFlag = 1; |
|
|
}else{ |
|
|
a = (u8*)pTerm->pIter->pData; |
|
|
n = pTerm->pIter->nData; |
|
|
} |
|
|
sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]); |
|
|
aIter[i].bFlag = (u8)bFlag; |
|
|
if( aIter[i].bEof ) goto ismatch_out; |
|
|
} |
|
|
|
|
|
while( 1 ){ |
|
|
int bMatch; |
|
|
i64 iPos = aIter[0].iPos; |
|
|
do { |
|
|
bMatch = 1; |
|
|
for(i=0; i<pPhrase->nTerm; i++){ |
|
|
Fts5PoslistReader *pPos = &aIter[i]; |
|
|
i64 iAdj = iPos + i; |
|
|
if( pPos->iPos!=iAdj ){ |
|
|
bMatch = 0; |
|
|
while( pPos->iPos<iAdj ){ |
|
|
if( sqlite3Fts5PoslistReaderNext(pPos) ) goto ismatch_out; |
|
|
} |
|
|
if( pPos->iPos>iAdj ) iPos = pPos->iPos-i; |
|
|
} |
|
|
} |
|
|
}while( bMatch==0 ); |
|
|
|
|
|
|
|
|
if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){ |
|
|
rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos); |
|
|
if( rc!=SQLITE_OK ) goto ismatch_out; |
|
|
} |
|
|
|
|
|
for(i=0; i<pPhrase->nTerm; i++){ |
|
|
if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out; |
|
|
} |
|
|
} |
|
|
|
|
|
ismatch_out: |
|
|
*pbMatch = (pPhrase->poslist.n>0); |
|
|
for(i=0; i<pPhrase->nTerm; i++){ |
|
|
if( aIter[i].bFlag ) sqlite3_free((u8*)aIter[i].a); |
|
|
} |
|
|
if( aIter!=aStatic ) sqlite3_free(aIter); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
typedef struct Fts5LookaheadReader Fts5LookaheadReader; |
|
|
struct Fts5LookaheadReader { |
|
|
const u8 *a; |
|
|
int n; |
|
|
int i; |
|
|
i64 iPos; |
|
|
i64 iLookahead; |
|
|
}; |
|
|
|
|
|
#define FTS5_LOOKAHEAD_EOF (((i64)1) << 62) |
|
|
|
|
|
static int fts5LookaheadReaderNext(Fts5LookaheadReader *p){ |
|
|
p->iPos = p->iLookahead; |
|
|
if( sqlite3Fts5PoslistNext64(p->a, p->n, &p->i, &p->iLookahead) ){ |
|
|
p->iLookahead = FTS5_LOOKAHEAD_EOF; |
|
|
} |
|
|
return (p->iPos==FTS5_LOOKAHEAD_EOF); |
|
|
} |
|
|
|
|
|
static int fts5LookaheadReaderInit( |
|
|
const u8 *a, int n, |
|
|
Fts5LookaheadReader *p |
|
|
){ |
|
|
memset(p, 0, sizeof(Fts5LookaheadReader)); |
|
|
p->a = a; |
|
|
p->n = n; |
|
|
fts5LookaheadReaderNext(p); |
|
|
return fts5LookaheadReaderNext(p); |
|
|
} |
|
|
|
|
|
typedef struct Fts5NearTrimmer Fts5NearTrimmer; |
|
|
struct Fts5NearTrimmer { |
|
|
Fts5LookaheadReader reader; |
|
|
Fts5PoslistWriter writer; |
|
|
Fts5Buffer *pOut; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNearIsMatch(int *pRc, Fts5ExprNearset *pNear){ |
|
|
Fts5NearTrimmer aStatic[4]; |
|
|
Fts5NearTrimmer *a = aStatic; |
|
|
Fts5ExprPhrase **apPhrase = pNear->apPhrase; |
|
|
|
|
|
int i; |
|
|
int rc = *pRc; |
|
|
int bMatch; |
|
|
|
|
|
assert( pNear->nPhrase>1 ); |
|
|
|
|
|
|
|
|
|
|
|
if( pNear->nPhrase>ArraySize(aStatic) ){ |
|
|
sqlite3_int64 nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase; |
|
|
a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte); |
|
|
}else{ |
|
|
memset(aStatic, 0, sizeof(aStatic)); |
|
|
} |
|
|
if( rc!=SQLITE_OK ){ |
|
|
*pRc = rc; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
Fts5Buffer *pPoslist = &apPhrase[i]->poslist; |
|
|
fts5LookaheadReaderInit(pPoslist->p, pPoslist->n, &a[i].reader); |
|
|
pPoslist->n = 0; |
|
|
a[i].pOut = pPoslist; |
|
|
} |
|
|
|
|
|
while( 1 ){ |
|
|
int iAdv; |
|
|
i64 iMin; |
|
|
i64 iMax; |
|
|
|
|
|
|
|
|
|
|
|
iMax = a[0].reader.iPos; |
|
|
do { |
|
|
bMatch = 1; |
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
Fts5LookaheadReader *pPos = &a[i].reader; |
|
|
iMin = iMax - pNear->apPhrase[i]->nTerm - pNear->nNear; |
|
|
if( pPos->iPos<iMin || pPos->iPos>iMax ){ |
|
|
bMatch = 0; |
|
|
while( pPos->iPos<iMin ){ |
|
|
if( fts5LookaheadReaderNext(pPos) ) goto ismatch_out; |
|
|
} |
|
|
if( pPos->iPos>iMax ) iMax = pPos->iPos; |
|
|
} |
|
|
} |
|
|
}while( bMatch==0 ); |
|
|
|
|
|
|
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
i64 iPos = a[i].reader.iPos; |
|
|
Fts5PoslistWriter *pWriter = &a[i].writer; |
|
|
if( a[i].pOut->n==0 || iPos!=pWriter->iPrev ){ |
|
|
sqlite3Fts5PoslistWriterAppend(a[i].pOut, pWriter, iPos); |
|
|
} |
|
|
} |
|
|
|
|
|
iAdv = 0; |
|
|
iMin = a[0].reader.iLookahead; |
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
if( a[i].reader.iLookahead < iMin ){ |
|
|
iMin = a[i].reader.iLookahead; |
|
|
iAdv = i; |
|
|
} |
|
|
} |
|
|
if( fts5LookaheadReaderNext(&a[iAdv].reader) ) goto ismatch_out; |
|
|
} |
|
|
|
|
|
ismatch_out: { |
|
|
int bRet = a[0].pOut->n>0; |
|
|
*pRc = rc; |
|
|
if( a!=aStatic ) sqlite3_free(a); |
|
|
return bRet; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprAdvanceto( |
|
|
Fts5IndexIter *pIter, |
|
|
int bDesc, |
|
|
i64 *piLast, |
|
|
int *pRc, |
|
|
int *pbEof |
|
|
){ |
|
|
i64 iLast = *piLast; |
|
|
i64 iRowid; |
|
|
|
|
|
iRowid = pIter->iRowid; |
|
|
if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){ |
|
|
int rc = sqlite3Fts5IterNextFrom(pIter, iLast); |
|
|
if( rc || sqlite3Fts5IterEof(pIter) ){ |
|
|
*pRc = rc; |
|
|
*pbEof = 1; |
|
|
return 1; |
|
|
} |
|
|
iRowid = pIter->iRowid; |
|
|
assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) ); |
|
|
} |
|
|
*piLast = iRowid; |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
static int fts5ExprSynonymAdvanceto( |
|
|
Fts5ExprTerm *pTerm, |
|
|
int bDesc, |
|
|
i64 *piLast, |
|
|
int *pRc |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
i64 iLast = *piLast; |
|
|
Fts5ExprTerm *p; |
|
|
int bEof = 0; |
|
|
|
|
|
for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){ |
|
|
if( sqlite3Fts5IterEof(p->pIter)==0 ){ |
|
|
i64 iRowid = p->pIter->iRowid; |
|
|
if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){ |
|
|
rc = sqlite3Fts5IterNextFrom(p->pIter, iLast); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
|
|
*pRc = rc; |
|
|
bEof = 1; |
|
|
}else{ |
|
|
*piLast = fts5ExprSynonymRowid(pTerm, bDesc, &bEof); |
|
|
} |
|
|
return bEof; |
|
|
} |
|
|
|
|
|
|
|
|
static int fts5ExprNearTest( |
|
|
int *pRc, |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode |
|
|
){ |
|
|
Fts5ExprNearset *pNear = pNode->pNear; |
|
|
int rc = *pRc; |
|
|
|
|
|
if( pExpr->pConfig->eDetail!=FTS5_DETAIL_FULL ){ |
|
|
Fts5ExprTerm *pTerm; |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[0]; |
|
|
pPhrase->poslist.n = 0; |
|
|
for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){ |
|
|
Fts5IndexIter *pIter = pTerm->pIter; |
|
|
if( sqlite3Fts5IterEof(pIter)==0 ){ |
|
|
if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){ |
|
|
pPhrase->poslist.n = 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
return pPhrase->poslist.n; |
|
|
}else{ |
|
|
int i; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){ |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
|
|
if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym |
|
|
|| pNear->pColset || pPhrase->aTerm[0].bFirst |
|
|
){ |
|
|
int bMatch = 0; |
|
|
rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch); |
|
|
if( bMatch==0 ) break; |
|
|
}else{ |
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter; |
|
|
fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData); |
|
|
} |
|
|
} |
|
|
|
|
|
*pRc = rc; |
|
|
if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){ |
|
|
return 1; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNearInitAll( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode |
|
|
){ |
|
|
Fts5ExprNearset *pNear = pNode->pNear; |
|
|
int i; |
|
|
|
|
|
assert( pNode->bNomatch==0 ); |
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
|
|
if( pPhrase->nTerm==0 ){ |
|
|
pNode->bEof = 1; |
|
|
return SQLITE_OK; |
|
|
}else{ |
|
|
int j; |
|
|
for(j=0; j<pPhrase->nTerm; j++){ |
|
|
Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; |
|
|
Fts5ExprTerm *p; |
|
|
int bHit = 0; |
|
|
|
|
|
for(p=pTerm; p; p=p->pSynonym){ |
|
|
int rc; |
|
|
if( p->pIter ){ |
|
|
sqlite3Fts5IterClose(p->pIter); |
|
|
p->pIter = 0; |
|
|
} |
|
|
rc = sqlite3Fts5IndexQuery( |
|
|
pExpr->pIndex, p->pTerm, p->nQueryTerm, |
|
|
(pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) | |
|
|
(pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0), |
|
|
pNear->pColset, |
|
|
&p->pIter |
|
|
); |
|
|
assert( (rc==SQLITE_OK)==(p->pIter!=0) ); |
|
|
if( rc!=SQLITE_OK ) return rc; |
|
|
if( 0==sqlite3Fts5IterEof(p->pIter) ){ |
|
|
bHit = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
if( bHit==0 ){ |
|
|
pNode->bEof = 1; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
pNode->bEof = 0; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5RowidCmp( |
|
|
Fts5Expr *pExpr, |
|
|
i64 iLhs, |
|
|
i64 iRhs |
|
|
){ |
|
|
assert( pExpr->bDesc==0 || pExpr->bDesc==1 ); |
|
|
if( pExpr->bDesc==0 ){ |
|
|
if( iLhs<iRhs ) return -1; |
|
|
return (iLhs > iRhs); |
|
|
}else{ |
|
|
if( iLhs>iRhs ) return -1; |
|
|
return (iLhs < iRhs); |
|
|
} |
|
|
} |
|
|
|
|
|
static void fts5ExprSetEof(Fts5ExprNode *pNode){ |
|
|
int i; |
|
|
pNode->bEof = 1; |
|
|
pNode->bNomatch = 0; |
|
|
for(i=0; i<pNode->nChild; i++){ |
|
|
fts5ExprSetEof(pNode->apChild[i]); |
|
|
} |
|
|
} |
|
|
|
|
|
static void fts5ExprNodeZeroPoslist(Fts5ExprNode *pNode){ |
|
|
if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ |
|
|
Fts5ExprNearset *pNear = pNode->pNear; |
|
|
int i; |
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
|
|
pPhrase->poslist.n = 0; |
|
|
} |
|
|
}else{ |
|
|
int i; |
|
|
for(i=0; i<pNode->nChild; i++){ |
|
|
fts5ExprNodeZeroPoslist(pNode->apChild[i]); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5NodeCompare( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *p1, |
|
|
Fts5ExprNode *p2 |
|
|
){ |
|
|
if( p2->bEof ) return -1; |
|
|
if( p1->bEof ) return +1; |
|
|
return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNodeTest_STRING( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode |
|
|
){ |
|
|
Fts5ExprNearset *pNear = pNode->pNear; |
|
|
Fts5ExprPhrase *pLeft = pNear->apPhrase[0]; |
|
|
int rc = SQLITE_OK; |
|
|
i64 iLast; |
|
|
int i, j; |
|
|
int bMatch; |
|
|
const int bDesc = pExpr->bDesc; |
|
|
|
|
|
|
|
|
assert( pNear->nPhrase>1 |
|
|
|| pNear->apPhrase[0]->nTerm>1 |
|
|
|| pNear->apPhrase[0]->aTerm[0].pSynonym |
|
|
|| pNear->apPhrase[0]->aTerm[0].bFirst |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if( pLeft->aTerm[0].pSynonym ){ |
|
|
iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0); |
|
|
}else{ |
|
|
iLast = pLeft->aTerm[0].pIter->iRowid; |
|
|
} |
|
|
|
|
|
do { |
|
|
bMatch = 1; |
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
|
|
for(j=0; j<pPhrase->nTerm; j++){ |
|
|
Fts5ExprTerm *pTerm = &pPhrase->aTerm[j]; |
|
|
if( pTerm->pSynonym ){ |
|
|
i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0); |
|
|
if( iRowid==iLast ) continue; |
|
|
bMatch = 0; |
|
|
if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){ |
|
|
pNode->bNomatch = 0; |
|
|
pNode->bEof = 1; |
|
|
return rc; |
|
|
} |
|
|
}else{ |
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter; |
|
|
if( pIter->iRowid==iLast ) continue; |
|
|
bMatch = 0; |
|
|
if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){ |
|
|
return rc; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}while( bMatch==0 ); |
|
|
|
|
|
pNode->iRowid = iLast; |
|
|
pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK); |
|
|
assert( pNode->bEof==0 || pNode->bNomatch==0 ); |
|
|
|
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNodeNext_STRING( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode, |
|
|
int bFromValid, |
|
|
i64 iFrom |
|
|
){ |
|
|
Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0]; |
|
|
int rc = SQLITE_OK; |
|
|
|
|
|
pNode->bNomatch = 0; |
|
|
if( pTerm->pSynonym ){ |
|
|
int bEof = 1; |
|
|
Fts5ExprTerm *p; |
|
|
|
|
|
|
|
|
i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0); |
|
|
|
|
|
|
|
|
|
|
|
for(p=pTerm; p; p=p->pSynonym){ |
|
|
if( sqlite3Fts5IterEof(p->pIter)==0 ){ |
|
|
i64 ii = p->pIter->iRowid; |
|
|
if( ii==iRowid |
|
|
|| (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc) |
|
|
){ |
|
|
if( bFromValid ){ |
|
|
rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom); |
|
|
}else{ |
|
|
rc = sqlite3Fts5IterNext(p->pIter); |
|
|
} |
|
|
if( rc!=SQLITE_OK ) break; |
|
|
if( sqlite3Fts5IterEof(p->pIter)==0 ){ |
|
|
bEof = 0; |
|
|
} |
|
|
}else{ |
|
|
bEof = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pNode->bEof = (rc || bEof); |
|
|
}else{ |
|
|
Fts5IndexIter *pIter = pTerm->pIter; |
|
|
|
|
|
assert( Fts5NodeIsString(pNode) ); |
|
|
if( bFromValid ){ |
|
|
rc = sqlite3Fts5IterNextFrom(pIter, iFrom); |
|
|
}else{ |
|
|
rc = sqlite3Fts5IterNext(pIter); |
|
|
} |
|
|
|
|
|
pNode->bEof = (rc || sqlite3Fts5IterEof(pIter)); |
|
|
} |
|
|
|
|
|
if( pNode->bEof==0 ){ |
|
|
assert( rc==SQLITE_OK ); |
|
|
rc = fts5ExprNodeTest_STRING(pExpr, pNode); |
|
|
} |
|
|
|
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
static int fts5ExprNodeTest_TERM( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode |
|
|
){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0]; |
|
|
Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter; |
|
|
|
|
|
assert( pNode->eType==FTS5_TERM ); |
|
|
assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 ); |
|
|
assert( pPhrase->aTerm[0].pSynonym==0 ); |
|
|
|
|
|
pPhrase->poslist.n = pIter->nData; |
|
|
if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){ |
|
|
pPhrase->poslist.p = (u8*)pIter->pData; |
|
|
} |
|
|
pNode->iRowid = pIter->iRowid; |
|
|
pNode->bNomatch = (pPhrase->poslist.n==0); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNodeNext_TERM( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode, |
|
|
int bFromValid, |
|
|
i64 iFrom |
|
|
){ |
|
|
int rc; |
|
|
Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter; |
|
|
|
|
|
assert( pNode->bEof==0 ); |
|
|
if( bFromValid ){ |
|
|
rc = sqlite3Fts5IterNextFrom(pIter, iFrom); |
|
|
}else{ |
|
|
rc = sqlite3Fts5IterNext(pIter); |
|
|
} |
|
|
if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){ |
|
|
rc = fts5ExprNodeTest_TERM(pExpr, pNode); |
|
|
}else{ |
|
|
pNode->bEof = 1; |
|
|
pNode->bNomatch = 0; |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
static void fts5ExprNodeTest_OR( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode |
|
|
){ |
|
|
Fts5ExprNode *pNext = pNode->apChild[0]; |
|
|
int i; |
|
|
|
|
|
for(i=1; i<pNode->nChild; i++){ |
|
|
Fts5ExprNode *pChild = pNode->apChild[i]; |
|
|
int cmp = fts5NodeCompare(pExpr, pNext, pChild); |
|
|
if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){ |
|
|
pNext = pChild; |
|
|
} |
|
|
} |
|
|
pNode->iRowid = pNext->iRowid; |
|
|
pNode->bEof = pNext->bEof; |
|
|
pNode->bNomatch = pNext->bNomatch; |
|
|
} |
|
|
|
|
|
static int fts5ExprNodeNext_OR( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode, |
|
|
int bFromValid, |
|
|
i64 iFrom |
|
|
){ |
|
|
int i; |
|
|
i64 iLast = pNode->iRowid; |
|
|
|
|
|
for(i=0; i<pNode->nChild; i++){ |
|
|
Fts5ExprNode *p1 = pNode->apChild[i]; |
|
|
assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 ); |
|
|
if( p1->bEof==0 ){ |
|
|
if( (p1->iRowid==iLast) |
|
|
|| (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0) |
|
|
){ |
|
|
int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom); |
|
|
if( rc!=SQLITE_OK ){ |
|
|
pNode->bNomatch = 0; |
|
|
return rc; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
fts5ExprNodeTest_OR(pExpr, pNode); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNodeTest_AND( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pAnd |
|
|
){ |
|
|
int iChild; |
|
|
i64 iLast = pAnd->iRowid; |
|
|
int rc = SQLITE_OK; |
|
|
int bMatch; |
|
|
|
|
|
assert( pAnd->bEof==0 ); |
|
|
do { |
|
|
pAnd->bNomatch = 0; |
|
|
bMatch = 1; |
|
|
for(iChild=0; iChild<pAnd->nChild; iChild++){ |
|
|
Fts5ExprNode *pChild = pAnd->apChild[iChild]; |
|
|
int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid); |
|
|
if( cmp>0 ){ |
|
|
|
|
|
rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast); |
|
|
if( rc!=SQLITE_OK ){ |
|
|
pAnd->bNomatch = 0; |
|
|
return rc; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert( pChild->bEof || fts5RowidCmp(pExpr, iLast, pChild->iRowid)<=0 ); |
|
|
if( pChild->bEof ){ |
|
|
fts5ExprSetEof(pAnd); |
|
|
bMatch = 1; |
|
|
break; |
|
|
}else if( iLast!=pChild->iRowid ){ |
|
|
bMatch = 0; |
|
|
iLast = pChild->iRowid; |
|
|
} |
|
|
|
|
|
if( pChild->bNomatch ){ |
|
|
pAnd->bNomatch = 1; |
|
|
} |
|
|
} |
|
|
}while( bMatch==0 ); |
|
|
|
|
|
if( pAnd->bNomatch && pAnd!=pExpr->pRoot ){ |
|
|
fts5ExprNodeZeroPoslist(pAnd); |
|
|
} |
|
|
pAnd->iRowid = iLast; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
static int fts5ExprNodeNext_AND( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode, |
|
|
int bFromValid, |
|
|
i64 iFrom |
|
|
){ |
|
|
int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom); |
|
|
if( rc==SQLITE_OK ){ |
|
|
rc = fts5ExprNodeTest_AND(pExpr, pNode); |
|
|
}else{ |
|
|
pNode->bNomatch = 0; |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
static int fts5ExprNodeTest_NOT( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
Fts5ExprNode *p1 = pNode->apChild[0]; |
|
|
Fts5ExprNode *p2 = pNode->apChild[1]; |
|
|
assert( pNode->nChild==2 ); |
|
|
|
|
|
while( rc==SQLITE_OK && p1->bEof==0 ){ |
|
|
int cmp = fts5NodeCompare(pExpr, p1, p2); |
|
|
if( cmp>0 ){ |
|
|
rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid); |
|
|
cmp = fts5NodeCompare(pExpr, p1, p2); |
|
|
} |
|
|
assert( rc!=SQLITE_OK || cmp<=0 ); |
|
|
if( cmp || p2->bNomatch ) break; |
|
|
rc = fts5ExprNodeNext(pExpr, p1, 0, 0); |
|
|
} |
|
|
pNode->bEof = p1->bEof; |
|
|
pNode->bNomatch = p1->bNomatch; |
|
|
pNode->iRowid = p1->iRowid; |
|
|
if( p1->bEof ){ |
|
|
fts5ExprNodeZeroPoslist(p2); |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
static int fts5ExprNodeNext_NOT( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode, |
|
|
int bFromValid, |
|
|
i64 iFrom |
|
|
){ |
|
|
int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom); |
|
|
if( rc==SQLITE_OK ){ |
|
|
rc = fts5ExprNodeTest_NOT(pExpr, pNode); |
|
|
} |
|
|
if( rc!=SQLITE_OK ){ |
|
|
pNode->bNomatch = 0; |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNodeTest( |
|
|
Fts5Expr *pExpr, |
|
|
Fts5ExprNode *pNode |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
if( pNode->bEof==0 ){ |
|
|
switch( pNode->eType ){ |
|
|
|
|
|
case FTS5_STRING: { |
|
|
rc = fts5ExprNodeTest_STRING(pExpr, pNode); |
|
|
break; |
|
|
} |
|
|
|
|
|
case FTS5_TERM: { |
|
|
rc = fts5ExprNodeTest_TERM(pExpr, pNode); |
|
|
break; |
|
|
} |
|
|
|
|
|
case FTS5_AND: { |
|
|
rc = fts5ExprNodeTest_AND(pExpr, pNode); |
|
|
break; |
|
|
} |
|
|
|
|
|
case FTS5_OR: { |
|
|
fts5ExprNodeTest_OR(pExpr, pNode); |
|
|
break; |
|
|
} |
|
|
|
|
|
default: assert( pNode->eType==FTS5_NOT ); { |
|
|
rc = fts5ExprNodeTest_NOT(pExpr, pNode); |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){ |
|
|
int rc = SQLITE_OK; |
|
|
pNode->bEof = 0; |
|
|
pNode->bNomatch = 0; |
|
|
|
|
|
if( Fts5NodeIsString(pNode) ){ |
|
|
|
|
|
rc = fts5ExprNearInitAll(pExpr, pNode); |
|
|
}else if( pNode->xNext==0 ){ |
|
|
pNode->bEof = 1; |
|
|
}else{ |
|
|
int i; |
|
|
int nEof = 0; |
|
|
for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){ |
|
|
Fts5ExprNode *pChild = pNode->apChild[i]; |
|
|
rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]); |
|
|
assert( pChild->bEof==0 || pChild->bEof==1 ); |
|
|
nEof += pChild->bEof; |
|
|
} |
|
|
pNode->iRowid = pNode->apChild[0]->iRowid; |
|
|
|
|
|
switch( pNode->eType ){ |
|
|
case FTS5_AND: |
|
|
if( nEof>0 ) fts5ExprSetEof(pNode); |
|
|
break; |
|
|
|
|
|
case FTS5_OR: |
|
|
if( pNode->nChild==nEof ) fts5ExprSetEof(pNode); |
|
|
break; |
|
|
|
|
|
default: |
|
|
assert( pNode->eType==FTS5_NOT ); |
|
|
pNode->bEof = pNode->apChild[0]->bEof; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc==SQLITE_OK ){ |
|
|
rc = fts5ExprNodeTest(pExpr, pNode); |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){ |
|
|
Fts5ExprNode *pRoot = p->pRoot; |
|
|
int rc; |
|
|
|
|
|
p->pIndex = pIdx; |
|
|
p->bDesc = bDesc; |
|
|
rc = fts5ExprNodeFirst(p, pRoot); |
|
|
|
|
|
|
|
|
|
|
|
if( rc==SQLITE_OK |
|
|
&& 0==pRoot->bEof |
|
|
&& fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 |
|
|
){ |
|
|
rc = fts5ExprNodeNext(p, pRoot, 1, iFirst); |
|
|
} |
|
|
|
|
|
|
|
|
while( pRoot->bNomatch && rc==SQLITE_OK ){ |
|
|
assert( pRoot->bEof==0 ); |
|
|
rc = fts5ExprNodeNext(p, pRoot, 0, 0); |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){ |
|
|
int rc; |
|
|
Fts5ExprNode *pRoot = p->pRoot; |
|
|
assert( pRoot->bEof==0 && pRoot->bNomatch==0 ); |
|
|
do { |
|
|
rc = fts5ExprNodeNext(p, pRoot, 0, 0); |
|
|
assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) ); |
|
|
}while( pRoot->bNomatch ); |
|
|
if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){ |
|
|
pRoot->bEof = 1; |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
int sqlite3Fts5ExprEof(Fts5Expr *p){ |
|
|
return p->pRoot->bEof; |
|
|
} |
|
|
|
|
|
i64 sqlite3Fts5ExprRowid(Fts5Expr *p){ |
|
|
return p->pRoot->iRowid; |
|
|
} |
|
|
|
|
|
static int fts5ParseStringFromToken(Fts5Token *pToken, char **pz){ |
|
|
int rc = SQLITE_OK; |
|
|
*pz = sqlite3Fts5Strndup(&rc, pToken->p, pToken->n); |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void fts5ExprPhraseFree(Fts5ExprPhrase *pPhrase){ |
|
|
if( pPhrase ){ |
|
|
int i; |
|
|
for(i=0; i<pPhrase->nTerm; i++){ |
|
|
Fts5ExprTerm *pSyn; |
|
|
Fts5ExprTerm *pNext; |
|
|
Fts5ExprTerm *pTerm = &pPhrase->aTerm[i]; |
|
|
sqlite3_free(pTerm->pTerm); |
|
|
sqlite3Fts5IterClose(pTerm->pIter); |
|
|
for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){ |
|
|
pNext = pSyn->pSynonym; |
|
|
sqlite3Fts5IterClose(pSyn->pIter); |
|
|
fts5BufferFree((Fts5Buffer*)&pSyn[1]); |
|
|
sqlite3_free(pSyn); |
|
|
} |
|
|
} |
|
|
if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist); |
|
|
sqlite3_free(pPhrase); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){ |
|
|
if( pPhrase && pPhrase->nTerm ){ |
|
|
pPhrase->aTerm[0].bFirst = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fts5ExprNearset *sqlite3Fts5ParseNearset( |
|
|
Fts5Parse *pParse, |
|
|
Fts5ExprNearset *pNear, |
|
|
Fts5ExprPhrase *pPhrase |
|
|
){ |
|
|
const int SZALLOC = 8; |
|
|
Fts5ExprNearset *pRet = 0; |
|
|
|
|
|
if( pParse->rc==SQLITE_OK ){ |
|
|
if( pNear==0 ){ |
|
|
sqlite3_int64 nByte; |
|
|
nByte = sizeof(Fts5ExprNearset) + SZALLOC * sizeof(Fts5ExprPhrase*); |
|
|
pRet = sqlite3_malloc64(nByte); |
|
|
if( pRet==0 ){ |
|
|
pParse->rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
memset(pRet, 0, (size_t)nByte); |
|
|
} |
|
|
}else if( (pNear->nPhrase % SZALLOC)==0 ){ |
|
|
int nNew = pNear->nPhrase + SZALLOC; |
|
|
sqlite3_int64 nByte; |
|
|
|
|
|
nByte = sizeof(Fts5ExprNearset) + nNew * sizeof(Fts5ExprPhrase*); |
|
|
pRet = (Fts5ExprNearset*)sqlite3_realloc64(pNear, nByte); |
|
|
if( pRet==0 ){ |
|
|
pParse->rc = SQLITE_NOMEM; |
|
|
} |
|
|
}else{ |
|
|
pRet = pNear; |
|
|
} |
|
|
} |
|
|
|
|
|
if( pRet==0 ){ |
|
|
assert( pParse->rc!=SQLITE_OK ); |
|
|
sqlite3Fts5ParseNearsetFree(pNear); |
|
|
sqlite3Fts5ParsePhraseFree(pPhrase); |
|
|
}else{ |
|
|
if( pRet->nPhrase>0 ){ |
|
|
Fts5ExprPhrase *pLast = pRet->apPhrase[pRet->nPhrase-1]; |
|
|
assert( pParse!=0 ); |
|
|
assert( pParse->apPhrase!=0 ); |
|
|
assert( pParse->nPhrase>=2 ); |
|
|
assert( pLast==pParse->apPhrase[pParse->nPhrase-2] ); |
|
|
if( pPhrase->nTerm==0 ){ |
|
|
fts5ExprPhraseFree(pPhrase); |
|
|
pRet->nPhrase--; |
|
|
pParse->nPhrase--; |
|
|
pPhrase = pLast; |
|
|
}else if( pLast->nTerm==0 ){ |
|
|
fts5ExprPhraseFree(pLast); |
|
|
pParse->apPhrase[pParse->nPhrase-2] = pPhrase; |
|
|
pParse->nPhrase--; |
|
|
pRet->nPhrase--; |
|
|
} |
|
|
} |
|
|
pRet->apPhrase[pRet->nPhrase++] = pPhrase; |
|
|
} |
|
|
return pRet; |
|
|
} |
|
|
|
|
|
typedef struct TokenCtx TokenCtx; |
|
|
struct TokenCtx { |
|
|
Fts5ExprPhrase *pPhrase; |
|
|
Fts5Config *pConfig; |
|
|
int rc; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ParseTokenize( |
|
|
void *pContext, |
|
|
int tflags, |
|
|
const char *pToken, |
|
|
int nToken, |
|
|
int iUnused1, |
|
|
int iUnused2 |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
const int SZALLOC = 8; |
|
|
TokenCtx *pCtx = (TokenCtx*)pContext; |
|
|
Fts5ExprPhrase *pPhrase = pCtx->pPhrase; |
|
|
|
|
|
UNUSED_PARAM2(iUnused1, iUnused2); |
|
|
|
|
|
|
|
|
if( pCtx->rc!=SQLITE_OK ) return pCtx->rc; |
|
|
if( nToken>FTS5_MAX_TOKEN_SIZE ) nToken = FTS5_MAX_TOKEN_SIZE; |
|
|
|
|
|
if( pPhrase && pPhrase->nTerm>0 && (tflags & FTS5_TOKEN_COLOCATED) ){ |
|
|
Fts5ExprTerm *pSyn; |
|
|
sqlite3_int64 nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1; |
|
|
pSyn = (Fts5ExprTerm*)sqlite3_malloc64(nByte); |
|
|
if( pSyn==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
memset(pSyn, 0, (size_t)nByte); |
|
|
pSyn->pTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer); |
|
|
pSyn->nFullTerm = pSyn->nQueryTerm = nToken; |
|
|
if( pCtx->pConfig->bTokendata ){ |
|
|
pSyn->nQueryTerm = (int)strlen(pSyn->pTerm); |
|
|
} |
|
|
memcpy(pSyn->pTerm, pToken, nToken); |
|
|
pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym; |
|
|
pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn; |
|
|
} |
|
|
}else{ |
|
|
Fts5ExprTerm *pTerm; |
|
|
if( pPhrase==0 || (pPhrase->nTerm % SZALLOC)==0 ){ |
|
|
Fts5ExprPhrase *pNew; |
|
|
int nNew = SZALLOC + (pPhrase ? pPhrase->nTerm : 0); |
|
|
|
|
|
pNew = (Fts5ExprPhrase*)sqlite3_realloc64(pPhrase, |
|
|
sizeof(Fts5ExprPhrase) + sizeof(Fts5ExprTerm) * nNew |
|
|
); |
|
|
if( pNew==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
if( pPhrase==0 ) memset(pNew, 0, sizeof(Fts5ExprPhrase)); |
|
|
pCtx->pPhrase = pPhrase = pNew; |
|
|
pNew->nTerm = nNew - SZALLOC; |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc==SQLITE_OK ){ |
|
|
pTerm = &pPhrase->aTerm[pPhrase->nTerm++]; |
|
|
memset(pTerm, 0, sizeof(Fts5ExprTerm)); |
|
|
pTerm->pTerm = sqlite3Fts5Strndup(&rc, pToken, nToken); |
|
|
pTerm->nFullTerm = pTerm->nQueryTerm = nToken; |
|
|
if( pCtx->pConfig->bTokendata && rc==SQLITE_OK ){ |
|
|
pTerm->nQueryTerm = (int)strlen(pTerm->pTerm); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
pCtx->rc = rc; |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ParsePhraseFree(Fts5ExprPhrase *pPhrase){ |
|
|
fts5ExprPhraseFree(pPhrase); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseNearsetFree(Fts5ExprNearset *pNear){ |
|
|
if( pNear ){ |
|
|
int i; |
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
fts5ExprPhraseFree(pNear->apPhrase[i]); |
|
|
} |
|
|
sqlite3_free(pNear->pColset); |
|
|
sqlite3_free(pNear); |
|
|
} |
|
|
} |
|
|
|
|
|
void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p){ |
|
|
assert( pParse->pExpr==0 ); |
|
|
pParse->pExpr = p; |
|
|
} |
|
|
|
|
|
static int parseGrowPhraseArray(Fts5Parse *pParse){ |
|
|
if( (pParse->nPhrase % 8)==0 ){ |
|
|
sqlite3_int64 nByte = sizeof(Fts5ExprPhrase*) * (pParse->nPhrase + 8); |
|
|
Fts5ExprPhrase **apNew; |
|
|
apNew = (Fts5ExprPhrase**)sqlite3_realloc64(pParse->apPhrase, nByte); |
|
|
if( apNew==0 ){ |
|
|
pParse->rc = SQLITE_NOMEM; |
|
|
return SQLITE_NOMEM; |
|
|
} |
|
|
pParse->apPhrase = apNew; |
|
|
} |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fts5ExprPhrase *sqlite3Fts5ParseTerm( |
|
|
Fts5Parse *pParse, |
|
|
Fts5ExprPhrase *pAppend, |
|
|
Fts5Token *pToken, |
|
|
int bPrefix |
|
|
){ |
|
|
Fts5Config *pConfig = pParse->pConfig; |
|
|
TokenCtx sCtx; |
|
|
int rc; |
|
|
char *z = 0; |
|
|
|
|
|
memset(&sCtx, 0, sizeof(TokenCtx)); |
|
|
sCtx.pPhrase = pAppend; |
|
|
sCtx.pConfig = pConfig; |
|
|
|
|
|
rc = fts5ParseStringFromToken(pToken, &z); |
|
|
if( rc==SQLITE_OK ){ |
|
|
int flags = FTS5_TOKENIZE_QUERY | (bPrefix ? FTS5_TOKENIZE_PREFIX : 0); |
|
|
int n; |
|
|
sqlite3Fts5Dequote(z); |
|
|
n = (int)strlen(z); |
|
|
rc = sqlite3Fts5Tokenize(pConfig, flags, z, n, &sCtx, fts5ParseTokenize); |
|
|
} |
|
|
sqlite3_free(z); |
|
|
if( rc || (rc = sCtx.rc) ){ |
|
|
pParse->rc = rc; |
|
|
fts5ExprPhraseFree(sCtx.pPhrase); |
|
|
sCtx.pPhrase = 0; |
|
|
}else{ |
|
|
|
|
|
if( pAppend==0 ){ |
|
|
if( parseGrowPhraseArray(pParse) ){ |
|
|
fts5ExprPhraseFree(sCtx.pPhrase); |
|
|
return 0; |
|
|
} |
|
|
pParse->nPhrase++; |
|
|
} |
|
|
|
|
|
if( sCtx.pPhrase==0 ){ |
|
|
|
|
|
|
|
|
sCtx.pPhrase = sqlite3Fts5MallocZero(&pParse->rc, sizeof(Fts5ExprPhrase)); |
|
|
}else if( sCtx.pPhrase->nTerm ){ |
|
|
sCtx.pPhrase->aTerm[sCtx.pPhrase->nTerm-1].bPrefix = (u8)bPrefix; |
|
|
} |
|
|
assert( pParse->apPhrase!=0 ); |
|
|
pParse->apPhrase[pParse->nPhrase-1] = sCtx.pPhrase; |
|
|
} |
|
|
|
|
|
return sCtx.pPhrase; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprClonePhrase( |
|
|
Fts5Expr *pExpr, |
|
|
int iPhrase, |
|
|
Fts5Expr **ppNew |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
Fts5ExprPhrase *pOrig = 0; |
|
|
Fts5Expr *pNew = 0; |
|
|
TokenCtx sCtx = {0,0,0}; |
|
|
if( !pExpr || iPhrase<0 || iPhrase>=pExpr->nPhrase ){ |
|
|
rc = SQLITE_RANGE; |
|
|
}else{ |
|
|
pOrig = pExpr->apExprPhrase[iPhrase]; |
|
|
pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr)); |
|
|
} |
|
|
if( rc==SQLITE_OK ){ |
|
|
pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc, |
|
|
sizeof(Fts5ExprPhrase*)); |
|
|
} |
|
|
if( rc==SQLITE_OK ){ |
|
|
pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&rc, |
|
|
sizeof(Fts5ExprNode)); |
|
|
} |
|
|
if( rc==SQLITE_OK ){ |
|
|
pNew->pRoot->pNear = (Fts5ExprNearset*)sqlite3Fts5MallocZero(&rc, |
|
|
sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*)); |
|
|
} |
|
|
if( rc==SQLITE_OK && ALWAYS(pOrig!=0) ){ |
|
|
Fts5Colset *pColsetOrig = pOrig->pNode->pNear->pColset; |
|
|
if( pColsetOrig ){ |
|
|
sqlite3_int64 nByte; |
|
|
Fts5Colset *pColset; |
|
|
nByte = sizeof(Fts5Colset) + (pColsetOrig->nCol-1) * sizeof(int); |
|
|
pColset = (Fts5Colset*)sqlite3Fts5MallocZero(&rc, nByte); |
|
|
if( pColset ){ |
|
|
memcpy(pColset, pColsetOrig, (size_t)nByte); |
|
|
} |
|
|
pNew->pRoot->pNear->pColset = pColset; |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc==SQLITE_OK ){ |
|
|
if( pOrig->nTerm ){ |
|
|
int i; |
|
|
sCtx.pConfig = pExpr->pConfig; |
|
|
for(i=0; rc==SQLITE_OK && i<pOrig->nTerm; i++){ |
|
|
int tflags = 0; |
|
|
Fts5ExprTerm *p; |
|
|
for(p=&pOrig->aTerm[i]; p && rc==SQLITE_OK; p=p->pSynonym){ |
|
|
rc = fts5ParseTokenize((void*)&sCtx,tflags,p->pTerm,p->nFullTerm,0,0); |
|
|
tflags = FTS5_TOKEN_COLOCATED; |
|
|
} |
|
|
if( rc==SQLITE_OK ){ |
|
|
sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix; |
|
|
sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst; |
|
|
} |
|
|
} |
|
|
}else{ |
|
|
|
|
|
|
|
|
sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase)); |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc==SQLITE_OK && ALWAYS(sCtx.pPhrase) ){ |
|
|
|
|
|
pNew->pIndex = pExpr->pIndex; |
|
|
pNew->pConfig = pExpr->pConfig; |
|
|
pNew->nPhrase = 1; |
|
|
pNew->apExprPhrase[0] = sCtx.pPhrase; |
|
|
pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase; |
|
|
pNew->pRoot->pNear->nPhrase = 1; |
|
|
sCtx.pPhrase->pNode = pNew->pRoot; |
|
|
|
|
|
if( pOrig->nTerm==1 |
|
|
&& pOrig->aTerm[0].pSynonym==0 |
|
|
&& pOrig->aTerm[0].bFirst==0 |
|
|
){ |
|
|
pNew->pRoot->eType = FTS5_TERM; |
|
|
pNew->pRoot->xNext = fts5ExprNodeNext_TERM; |
|
|
}else{ |
|
|
pNew->pRoot->eType = FTS5_STRING; |
|
|
pNew->pRoot->xNext = fts5ExprNodeNext_STRING; |
|
|
} |
|
|
}else{ |
|
|
sqlite3Fts5ExprFree(pNew); |
|
|
fts5ExprPhraseFree(sCtx.pPhrase); |
|
|
pNew = 0; |
|
|
} |
|
|
|
|
|
*ppNew = pNew; |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token *pTok){ |
|
|
if( pTok->n!=4 || memcmp("NEAR", pTok->p, 4) ){ |
|
|
sqlite3Fts5ParseError( |
|
|
pParse, "fts5: syntax error near \"%.*s\"", pTok->n, pTok->p |
|
|
); |
|
|
} |
|
|
} |
|
|
|
|
|
void sqlite3Fts5ParseSetDistance( |
|
|
Fts5Parse *pParse, |
|
|
Fts5ExprNearset *pNear, |
|
|
Fts5Token *p |
|
|
){ |
|
|
if( pNear ){ |
|
|
int nNear = 0; |
|
|
int i; |
|
|
if( p->n ){ |
|
|
for(i=0; i<p->n; i++){ |
|
|
char c = (char)p->p[i]; |
|
|
if( c<'0' || c>'9' ){ |
|
|
sqlite3Fts5ParseError( |
|
|
pParse, "expected integer, got \"%.*s\"", p->n, p->p |
|
|
); |
|
|
return; |
|
|
} |
|
|
nNear = nNear * 10 + (p->p[i] - '0'); |
|
|
} |
|
|
}else{ |
|
|
nNear = FTS5_DEFAULT_NEARDIST; |
|
|
} |
|
|
pNear->nNear = nNear; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Fts5Colset *fts5ParseColset( |
|
|
Fts5Parse *pParse, |
|
|
Fts5Colset *p, |
|
|
int iCol |
|
|
){ |
|
|
int nCol = p ? p->nCol : 0; |
|
|
Fts5Colset *pNew; |
|
|
|
|
|
assert( pParse->rc==SQLITE_OK ); |
|
|
assert( iCol>=0 && iCol<pParse->pConfig->nCol ); |
|
|
|
|
|
pNew = sqlite3_realloc64(p, sizeof(Fts5Colset) + sizeof(int)*nCol); |
|
|
if( pNew==0 ){ |
|
|
pParse->rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
int *aiCol = pNew->aiCol; |
|
|
int i, j; |
|
|
for(i=0; i<nCol; i++){ |
|
|
if( aiCol[i]==iCol ) return pNew; |
|
|
if( aiCol[i]>iCol ) break; |
|
|
} |
|
|
for(j=nCol; j>i; j--){ |
|
|
aiCol[j] = aiCol[j-1]; |
|
|
} |
|
|
aiCol[i] = iCol; |
|
|
pNew->nCol = nCol+1; |
|
|
|
|
|
#ifndef NDEBUG |
|
|
|
|
|
for(i=1; i<pNew->nCol; i++) assert( pNew->aiCol[i]>pNew->aiCol[i-1] ); |
|
|
#endif |
|
|
} |
|
|
|
|
|
return pNew; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fts5Colset *sqlite3Fts5ParseColsetInvert(Fts5Parse *pParse, Fts5Colset *p){ |
|
|
Fts5Colset *pRet; |
|
|
int nCol = pParse->pConfig->nCol; |
|
|
|
|
|
pRet = (Fts5Colset*)sqlite3Fts5MallocZero(&pParse->rc, |
|
|
sizeof(Fts5Colset) + sizeof(int)*nCol |
|
|
); |
|
|
if( pRet ){ |
|
|
int i; |
|
|
int iOld = 0; |
|
|
for(i=0; i<nCol; i++){ |
|
|
if( iOld>=p->nCol || p->aiCol[iOld]!=i ){ |
|
|
pRet->aiCol[pRet->nCol++] = i; |
|
|
}else{ |
|
|
iOld++; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
sqlite3_free(p); |
|
|
return pRet; |
|
|
} |
|
|
|
|
|
Fts5Colset *sqlite3Fts5ParseColset( |
|
|
Fts5Parse *pParse, |
|
|
Fts5Colset *pColset, |
|
|
Fts5Token *p |
|
|
){ |
|
|
Fts5Colset *pRet = 0; |
|
|
int iCol; |
|
|
char *z; |
|
|
|
|
|
z = sqlite3Fts5Strndup(&pParse->rc, p->p, p->n); |
|
|
if( pParse->rc==SQLITE_OK ){ |
|
|
Fts5Config *pConfig = pParse->pConfig; |
|
|
sqlite3Fts5Dequote(z); |
|
|
for(iCol=0; iCol<pConfig->nCol; iCol++){ |
|
|
if( 0==sqlite3_stricmp(pConfig->azCol[iCol], z) ) break; |
|
|
} |
|
|
if( iCol==pConfig->nCol ){ |
|
|
sqlite3Fts5ParseError(pParse, "no such column: %s", z); |
|
|
}else{ |
|
|
pRet = fts5ParseColset(pParse, pColset, iCol); |
|
|
} |
|
|
sqlite3_free(z); |
|
|
} |
|
|
|
|
|
if( pRet==0 ){ |
|
|
assert( pParse->rc!=SQLITE_OK ); |
|
|
sqlite3_free(pColset); |
|
|
} |
|
|
|
|
|
return pRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Fts5Colset *fts5CloneColset(int *pRc, Fts5Colset *pOrig){ |
|
|
Fts5Colset *pRet; |
|
|
if( pOrig ){ |
|
|
sqlite3_int64 nByte = sizeof(Fts5Colset) + (pOrig->nCol-1) * sizeof(int); |
|
|
pRet = (Fts5Colset*)sqlite3Fts5MallocZero(pRc, nByte); |
|
|
if( pRet ){ |
|
|
memcpy(pRet, pOrig, (size_t)nByte); |
|
|
} |
|
|
}else{ |
|
|
pRet = 0; |
|
|
} |
|
|
return pRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void fts5MergeColset(Fts5Colset *pColset, Fts5Colset *pMerge){ |
|
|
int iIn = 0; |
|
|
int iMerge = 0; |
|
|
int iOut = 0; |
|
|
|
|
|
while( iIn<pColset->nCol && iMerge<pMerge->nCol ){ |
|
|
int iDiff = pColset->aiCol[iIn] - pMerge->aiCol[iMerge]; |
|
|
if( iDiff==0 ){ |
|
|
pColset->aiCol[iOut++] = pMerge->aiCol[iMerge]; |
|
|
iMerge++; |
|
|
iIn++; |
|
|
}else if( iDiff>0 ){ |
|
|
iMerge++; |
|
|
}else{ |
|
|
iIn++; |
|
|
} |
|
|
} |
|
|
pColset->nCol = iOut; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void fts5ParseSetColset( |
|
|
Fts5Parse *pParse, |
|
|
Fts5ExprNode *pNode, |
|
|
Fts5Colset *pColset, |
|
|
Fts5Colset **ppFree |
|
|
){ |
|
|
if( pParse->rc==SQLITE_OK ){ |
|
|
assert( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING |
|
|
|| pNode->eType==FTS5_AND || pNode->eType==FTS5_OR |
|
|
|| pNode->eType==FTS5_NOT || pNode->eType==FTS5_EOF |
|
|
); |
|
|
if( pNode->eType==FTS5_STRING || pNode->eType==FTS5_TERM ){ |
|
|
Fts5ExprNearset *pNear = pNode->pNear; |
|
|
if( pNear->pColset ){ |
|
|
fts5MergeColset(pNear->pColset, pColset); |
|
|
if( pNear->pColset->nCol==0 ){ |
|
|
pNode->eType = FTS5_EOF; |
|
|
pNode->xNext = 0; |
|
|
} |
|
|
}else if( *ppFree ){ |
|
|
pNear->pColset = pColset; |
|
|
*ppFree = 0; |
|
|
}else{ |
|
|
pNear->pColset = fts5CloneColset(&pParse->rc, pColset); |
|
|
} |
|
|
}else{ |
|
|
int i; |
|
|
assert( pNode->eType!=FTS5_EOF || pNode->nChild==0 ); |
|
|
for(i=0; i<pNode->nChild; i++){ |
|
|
fts5ParseSetColset(pParse, pNode->apChild[i], pColset, ppFree); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ParseSetColset( |
|
|
Fts5Parse *pParse, |
|
|
Fts5ExprNode *pExpr, |
|
|
Fts5Colset *pColset |
|
|
){ |
|
|
Fts5Colset *pFree = pColset; |
|
|
if( pParse->pConfig->eDetail==FTS5_DETAIL_NONE ){ |
|
|
sqlite3Fts5ParseError(pParse, |
|
|
"fts5: column queries are not supported (detail=none)" |
|
|
); |
|
|
}else{ |
|
|
fts5ParseSetColset(pParse, pExpr, pColset, &pFree); |
|
|
} |
|
|
sqlite3_free(pFree); |
|
|
} |
|
|
|
|
|
static void fts5ExprAssignXNext(Fts5ExprNode *pNode){ |
|
|
switch( pNode->eType ){ |
|
|
case FTS5_STRING: { |
|
|
Fts5ExprNearset *pNear = pNode->pNear; |
|
|
if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1 |
|
|
&& pNear->apPhrase[0]->aTerm[0].pSynonym==0 |
|
|
&& pNear->apPhrase[0]->aTerm[0].bFirst==0 |
|
|
){ |
|
|
pNode->eType = FTS5_TERM; |
|
|
pNode->xNext = fts5ExprNodeNext_TERM; |
|
|
}else{ |
|
|
pNode->xNext = fts5ExprNodeNext_STRING; |
|
|
} |
|
|
break; |
|
|
}; |
|
|
|
|
|
case FTS5_OR: { |
|
|
pNode->xNext = fts5ExprNodeNext_OR; |
|
|
break; |
|
|
}; |
|
|
|
|
|
case FTS5_AND: { |
|
|
pNode->xNext = fts5ExprNodeNext_AND; |
|
|
break; |
|
|
}; |
|
|
|
|
|
default: assert( pNode->eType==FTS5_NOT ); { |
|
|
pNode->xNext = fts5ExprNodeNext_NOT; |
|
|
break; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){ |
|
|
int ii = p->nChild; |
|
|
if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){ |
|
|
int nByte = sizeof(Fts5ExprNode*) * pSub->nChild; |
|
|
memcpy(&p->apChild[p->nChild], pSub->apChild, nByte); |
|
|
p->nChild += pSub->nChild; |
|
|
sqlite3_free(pSub); |
|
|
}else{ |
|
|
p->apChild[p->nChild++] = pSub; |
|
|
} |
|
|
for( ; ii<p->nChild; ii++){ |
|
|
p->iHeight = MAX(p->iHeight, p->apChild[ii]->iHeight + 1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Fts5ExprNode *fts5ParsePhraseToAnd( |
|
|
Fts5Parse *pParse, |
|
|
Fts5ExprNearset *pNear |
|
|
){ |
|
|
int nTerm = pNear->apPhrase[0]->nTerm; |
|
|
int ii; |
|
|
int nByte; |
|
|
Fts5ExprNode *pRet; |
|
|
|
|
|
assert( pNear->nPhrase==1 ); |
|
|
assert( pParse->bPhraseToAnd ); |
|
|
|
|
|
nByte = sizeof(Fts5ExprNode) + nTerm*sizeof(Fts5ExprNode*); |
|
|
pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); |
|
|
if( pRet ){ |
|
|
pRet->eType = FTS5_AND; |
|
|
pRet->nChild = nTerm; |
|
|
pRet->iHeight = 1; |
|
|
fts5ExprAssignXNext(pRet); |
|
|
pParse->nPhrase--; |
|
|
for(ii=0; ii<nTerm; ii++){ |
|
|
Fts5ExprPhrase *pPhrase = (Fts5ExprPhrase*)sqlite3Fts5MallocZero( |
|
|
&pParse->rc, sizeof(Fts5ExprPhrase) |
|
|
); |
|
|
if( pPhrase ){ |
|
|
if( parseGrowPhraseArray(pParse) ){ |
|
|
fts5ExprPhraseFree(pPhrase); |
|
|
}else{ |
|
|
Fts5ExprTerm *p = &pNear->apPhrase[0]->aTerm[ii]; |
|
|
Fts5ExprTerm *pTo = &pPhrase->aTerm[0]; |
|
|
pParse->apPhrase[pParse->nPhrase++] = pPhrase; |
|
|
pPhrase->nTerm = 1; |
|
|
pTo->pTerm = sqlite3Fts5Strndup(&pParse->rc, p->pTerm, p->nFullTerm); |
|
|
pTo->nQueryTerm = p->nQueryTerm; |
|
|
pTo->nFullTerm = p->nFullTerm; |
|
|
pRet->apChild[ii] = sqlite3Fts5ParseNode(pParse, FTS5_STRING, |
|
|
0, 0, sqlite3Fts5ParseNearset(pParse, 0, pPhrase) |
|
|
); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if( pParse->rc ){ |
|
|
sqlite3Fts5ParseNodeFree(pRet); |
|
|
pRet = 0; |
|
|
}else{ |
|
|
sqlite3Fts5ParseNearsetFree(pNear); |
|
|
} |
|
|
} |
|
|
|
|
|
return pRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fts5ExprNode *sqlite3Fts5ParseNode( |
|
|
Fts5Parse *pParse, |
|
|
int eType, |
|
|
Fts5ExprNode *pLeft, |
|
|
Fts5ExprNode *pRight, |
|
|
Fts5ExprNearset *pNear |
|
|
){ |
|
|
Fts5ExprNode *pRet = 0; |
|
|
|
|
|
if( pParse->rc==SQLITE_OK ){ |
|
|
int nChild = 0; |
|
|
sqlite3_int64 nByte; |
|
|
|
|
|
assert( (eType!=FTS5_STRING && !pNear) |
|
|
|| (eType==FTS5_STRING && !pLeft && !pRight) |
|
|
); |
|
|
if( eType==FTS5_STRING && pNear==0 ) return 0; |
|
|
if( eType!=FTS5_STRING && pLeft==0 ) return pRight; |
|
|
if( eType!=FTS5_STRING && pRight==0 ) return pLeft; |
|
|
|
|
|
if( eType==FTS5_STRING |
|
|
&& pParse->bPhraseToAnd |
|
|
&& pNear->apPhrase[0]->nTerm>1 |
|
|
){ |
|
|
pRet = fts5ParsePhraseToAnd(pParse, pNear); |
|
|
}else{ |
|
|
if( eType==FTS5_NOT ){ |
|
|
nChild = 2; |
|
|
}else if( eType==FTS5_AND || eType==FTS5_OR ){ |
|
|
nChild = 2; |
|
|
if( pLeft->eType==eType ) nChild += pLeft->nChild-1; |
|
|
if( pRight->eType==eType ) nChild += pRight->nChild-1; |
|
|
} |
|
|
|
|
|
nByte = sizeof(Fts5ExprNode) + sizeof(Fts5ExprNode*)*(nChild-1); |
|
|
pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte); |
|
|
|
|
|
if( pRet ){ |
|
|
pRet->eType = eType; |
|
|
pRet->pNear = pNear; |
|
|
fts5ExprAssignXNext(pRet); |
|
|
if( eType==FTS5_STRING ){ |
|
|
int iPhrase; |
|
|
for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){ |
|
|
pNear->apPhrase[iPhrase]->pNode = pRet; |
|
|
if( pNear->apPhrase[iPhrase]->nTerm==0 ){ |
|
|
pRet->xNext = 0; |
|
|
pRet->eType = FTS5_EOF; |
|
|
} |
|
|
} |
|
|
|
|
|
if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){ |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[0]; |
|
|
if( pNear->nPhrase!=1 |
|
|
|| pPhrase->nTerm>1 |
|
|
|| (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst) |
|
|
){ |
|
|
sqlite3Fts5ParseError(pParse, |
|
|
"fts5: %s queries are not supported (detail!=full)", |
|
|
pNear->nPhrase==1 ? "phrase": "NEAR" |
|
|
); |
|
|
sqlite3Fts5ParseNodeFree(pRet); |
|
|
pRet = 0; |
|
|
pNear = 0; |
|
|
assert( pLeft==0 && pRight==0 ); |
|
|
} |
|
|
} |
|
|
}else{ |
|
|
assert( pNear==0 ); |
|
|
fts5ExprAddChildren(pRet, pLeft); |
|
|
fts5ExprAddChildren(pRet, pRight); |
|
|
pLeft = pRight = 0; |
|
|
if( pRet->iHeight>SQLITE_FTS5_MAX_EXPR_DEPTH ){ |
|
|
sqlite3Fts5ParseError(pParse, |
|
|
"fts5 expression tree is too large (maximum depth %d)", |
|
|
SQLITE_FTS5_MAX_EXPR_DEPTH |
|
|
); |
|
|
sqlite3Fts5ParseNodeFree(pRet); |
|
|
pRet = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if( pRet==0 ){ |
|
|
assert( pParse->rc!=SQLITE_OK ); |
|
|
sqlite3Fts5ParseNodeFree(pLeft); |
|
|
sqlite3Fts5ParseNodeFree(pRight); |
|
|
sqlite3Fts5ParseNearsetFree(pNear); |
|
|
} |
|
|
return pRet; |
|
|
} |
|
|
|
|
|
Fts5ExprNode *sqlite3Fts5ParseImplicitAnd( |
|
|
Fts5Parse *pParse, |
|
|
Fts5ExprNode *pLeft, |
|
|
Fts5ExprNode *pRight |
|
|
){ |
|
|
Fts5ExprNode *pRet = 0; |
|
|
Fts5ExprNode *pPrev; |
|
|
|
|
|
if( pParse->rc ){ |
|
|
sqlite3Fts5ParseNodeFree(pLeft); |
|
|
sqlite3Fts5ParseNodeFree(pRight); |
|
|
}else{ |
|
|
|
|
|
assert( pLeft->eType==FTS5_STRING |
|
|
|| pLeft->eType==FTS5_TERM |
|
|
|| pLeft->eType==FTS5_EOF |
|
|
|| pLeft->eType==FTS5_AND |
|
|
); |
|
|
assert( pRight->eType==FTS5_STRING |
|
|
|| pRight->eType==FTS5_TERM |
|
|
|| pRight->eType==FTS5_EOF |
|
|
|| (pRight->eType==FTS5_AND && pParse->bPhraseToAnd) |
|
|
); |
|
|
|
|
|
if( pLeft->eType==FTS5_AND ){ |
|
|
pPrev = pLeft->apChild[pLeft->nChild-1]; |
|
|
}else{ |
|
|
pPrev = pLeft; |
|
|
} |
|
|
assert( pPrev->eType==FTS5_STRING |
|
|
|| pPrev->eType==FTS5_TERM |
|
|
|| pPrev->eType==FTS5_EOF |
|
|
); |
|
|
|
|
|
if( pRight->eType==FTS5_EOF ){ |
|
|
assert( pParse->apPhrase!=0 ); |
|
|
assert( pParse->nPhrase>0 ); |
|
|
assert( pParse->apPhrase[pParse->nPhrase-1]==pRight->pNear->apPhrase[0] ); |
|
|
sqlite3Fts5ParseNodeFree(pRight); |
|
|
pRet = pLeft; |
|
|
pParse->nPhrase--; |
|
|
} |
|
|
else if( pPrev->eType==FTS5_EOF ){ |
|
|
Fts5ExprPhrase **ap; |
|
|
|
|
|
if( pPrev==pLeft ){ |
|
|
pRet = pRight; |
|
|
}else{ |
|
|
pLeft->apChild[pLeft->nChild-1] = pRight; |
|
|
pRet = pLeft; |
|
|
} |
|
|
|
|
|
ap = &pParse->apPhrase[pParse->nPhrase-1-pRight->pNear->nPhrase]; |
|
|
assert( ap[0]==pPrev->pNear->apPhrase[0] ); |
|
|
memmove(ap, &ap[1], sizeof(Fts5ExprPhrase*)*pRight->pNear->nPhrase); |
|
|
pParse->nPhrase--; |
|
|
|
|
|
sqlite3Fts5ParseNodeFree(pPrev); |
|
|
} |
|
|
else{ |
|
|
pRet = sqlite3Fts5ParseNode(pParse, FTS5_AND, pLeft, pRight, 0); |
|
|
} |
|
|
} |
|
|
|
|
|
return pRet; |
|
|
} |
|
|
|
|
|
#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) |
|
|
static char *fts5ExprTermPrint(Fts5ExprTerm *pTerm){ |
|
|
sqlite3_int64 nByte = 0; |
|
|
Fts5ExprTerm *p; |
|
|
char *zQuoted; |
|
|
|
|
|
|
|
|
for(p=pTerm; p; p=p->pSynonym){ |
|
|
nByte += pTerm->nQueryTerm * 2 + 3 + 2; |
|
|
} |
|
|
zQuoted = sqlite3_malloc64(nByte); |
|
|
|
|
|
if( zQuoted ){ |
|
|
int i = 0; |
|
|
for(p=pTerm; p; p=p->pSynonym){ |
|
|
char *zIn = p->pTerm; |
|
|
char *zEnd = &zIn[p->nQueryTerm]; |
|
|
zQuoted[i++] = '"'; |
|
|
while( zIn<zEnd ){ |
|
|
if( *zIn=='"' ) zQuoted[i++] = '"'; |
|
|
zQuoted[i++] = *zIn++; |
|
|
} |
|
|
zQuoted[i++] = '"'; |
|
|
if( p->pSynonym ) zQuoted[i++] = '|'; |
|
|
} |
|
|
if( pTerm->bPrefix ){ |
|
|
zQuoted[i++] = ' '; |
|
|
zQuoted[i++] = '*'; |
|
|
} |
|
|
zQuoted[i++] = '\0'; |
|
|
} |
|
|
return zQuoted; |
|
|
} |
|
|
|
|
|
static char *fts5PrintfAppend(char *zApp, const char *zFmt, ...){ |
|
|
char *zNew; |
|
|
va_list ap; |
|
|
va_start(ap, zFmt); |
|
|
zNew = sqlite3_vmprintf(zFmt, ap); |
|
|
va_end(ap); |
|
|
if( zApp && zNew ){ |
|
|
char *zNew2 = sqlite3_mprintf("%s%s", zApp, zNew); |
|
|
sqlite3_free(zNew); |
|
|
zNew = zNew2; |
|
|
} |
|
|
sqlite3_free(zApp); |
|
|
return zNew; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static char *fts5ExprPrintTcl( |
|
|
Fts5Config *pConfig, |
|
|
const char *zNearsetCmd, |
|
|
Fts5ExprNode *pExpr |
|
|
){ |
|
|
char *zRet = 0; |
|
|
if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){ |
|
|
Fts5ExprNearset *pNear = pExpr->pNear; |
|
|
int i; |
|
|
int iTerm; |
|
|
|
|
|
zRet = fts5PrintfAppend(zRet, "%s ", zNearsetCmd); |
|
|
if( zRet==0 ) return 0; |
|
|
if( pNear->pColset ){ |
|
|
int *aiCol = pNear->pColset->aiCol; |
|
|
int nCol = pNear->pColset->nCol; |
|
|
if( nCol==1 ){ |
|
|
zRet = fts5PrintfAppend(zRet, "-col %d ", aiCol[0]); |
|
|
}else{ |
|
|
zRet = fts5PrintfAppend(zRet, "-col {%d", aiCol[0]); |
|
|
for(i=1; i<pNear->pColset->nCol; i++){ |
|
|
zRet = fts5PrintfAppend(zRet, " %d", aiCol[i]); |
|
|
} |
|
|
zRet = fts5PrintfAppend(zRet, "} "); |
|
|
} |
|
|
if( zRet==0 ) return 0; |
|
|
} |
|
|
|
|
|
if( pNear->nPhrase>1 ){ |
|
|
zRet = fts5PrintfAppend(zRet, "-near %d ", pNear->nNear); |
|
|
if( zRet==0 ) return 0; |
|
|
} |
|
|
|
|
|
zRet = fts5PrintfAppend(zRet, "--"); |
|
|
if( zRet==0 ) return 0; |
|
|
|
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
|
|
|
|
|
zRet = fts5PrintfAppend(zRet, " {"); |
|
|
for(iTerm=0; zRet && iTerm<pPhrase->nTerm; iTerm++){ |
|
|
Fts5ExprTerm *p = &pPhrase->aTerm[iTerm]; |
|
|
zRet = fts5PrintfAppend(zRet, "%s%.*s", iTerm==0?"":" ", |
|
|
p->nQueryTerm, p->pTerm |
|
|
); |
|
|
if( pPhrase->aTerm[iTerm].bPrefix ){ |
|
|
zRet = fts5PrintfAppend(zRet, "*"); |
|
|
} |
|
|
} |
|
|
|
|
|
if( zRet ) zRet = fts5PrintfAppend(zRet, "}"); |
|
|
if( zRet==0 ) return 0; |
|
|
} |
|
|
|
|
|
}else if( pExpr->eType==0 ){ |
|
|
zRet = sqlite3_mprintf("{}"); |
|
|
}else{ |
|
|
char const *zOp = 0; |
|
|
int i; |
|
|
switch( pExpr->eType ){ |
|
|
case FTS5_AND: zOp = "AND"; break; |
|
|
case FTS5_NOT: zOp = "NOT"; break; |
|
|
default: |
|
|
assert( pExpr->eType==FTS5_OR ); |
|
|
zOp = "OR"; |
|
|
break; |
|
|
} |
|
|
|
|
|
zRet = sqlite3_mprintf("%s", zOp); |
|
|
for(i=0; zRet && i<pExpr->nChild; i++){ |
|
|
char *z = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->apChild[i]); |
|
|
if( !z ){ |
|
|
sqlite3_free(zRet); |
|
|
zRet = 0; |
|
|
}else{ |
|
|
zRet = fts5PrintfAppend(zRet, " [%z]", z); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return zRet; |
|
|
} |
|
|
|
|
|
static char *fts5ExprPrint(Fts5Config *pConfig, Fts5ExprNode *pExpr){ |
|
|
char *zRet = 0; |
|
|
if( pExpr->eType==0 ){ |
|
|
return sqlite3_mprintf("\"\""); |
|
|
}else |
|
|
if( pExpr->eType==FTS5_STRING || pExpr->eType==FTS5_TERM ){ |
|
|
Fts5ExprNearset *pNear = pExpr->pNear; |
|
|
int i; |
|
|
int iTerm; |
|
|
|
|
|
if( pNear->pColset ){ |
|
|
int ii; |
|
|
Fts5Colset *pColset = pNear->pColset; |
|
|
if( pColset->nCol>1 ) zRet = fts5PrintfAppend(zRet, "{"); |
|
|
for(ii=0; ii<pColset->nCol; ii++){ |
|
|
zRet = fts5PrintfAppend(zRet, "%s%s", |
|
|
pConfig->azCol[pColset->aiCol[ii]], ii==pColset->nCol-1 ? "" : " " |
|
|
); |
|
|
} |
|
|
if( zRet ){ |
|
|
zRet = fts5PrintfAppend(zRet, "%s : ", pColset->nCol>1 ? "}" : ""); |
|
|
} |
|
|
if( zRet==0 ) return 0; |
|
|
} |
|
|
|
|
|
if( pNear->nPhrase>1 ){ |
|
|
zRet = fts5PrintfAppend(zRet, "NEAR("); |
|
|
if( zRet==0 ) return 0; |
|
|
} |
|
|
|
|
|
for(i=0; i<pNear->nPhrase; i++){ |
|
|
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i]; |
|
|
if( i!=0 ){ |
|
|
zRet = fts5PrintfAppend(zRet, " "); |
|
|
if( zRet==0 ) return 0; |
|
|
} |
|
|
for(iTerm=0; iTerm<pPhrase->nTerm; iTerm++){ |
|
|
char *zTerm = fts5ExprTermPrint(&pPhrase->aTerm[iTerm]); |
|
|
if( zTerm ){ |
|
|
zRet = fts5PrintfAppend(zRet, "%s%s", iTerm==0?"":" + ", zTerm); |
|
|
sqlite3_free(zTerm); |
|
|
} |
|
|
if( zTerm==0 || zRet==0 ){ |
|
|
sqlite3_free(zRet); |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if( pNear->nPhrase>1 ){ |
|
|
zRet = fts5PrintfAppend(zRet, ", %d)", pNear->nNear); |
|
|
if( zRet==0 ) return 0; |
|
|
} |
|
|
|
|
|
}else{ |
|
|
char const *zOp = 0; |
|
|
int i; |
|
|
|
|
|
switch( pExpr->eType ){ |
|
|
case FTS5_AND: zOp = " AND "; break; |
|
|
case FTS5_NOT: zOp = " NOT "; break; |
|
|
default: |
|
|
assert( pExpr->eType==FTS5_OR ); |
|
|
zOp = " OR "; |
|
|
break; |
|
|
} |
|
|
|
|
|
for(i=0; i<pExpr->nChild; i++){ |
|
|
char *z = fts5ExprPrint(pConfig, pExpr->apChild[i]); |
|
|
if( z==0 ){ |
|
|
sqlite3_free(zRet); |
|
|
zRet = 0; |
|
|
}else{ |
|
|
int e = pExpr->apChild[i]->eType; |
|
|
int b = (e!=FTS5_STRING && e!=FTS5_TERM && e!=FTS5_EOF); |
|
|
zRet = fts5PrintfAppend(zRet, "%s%s%z%s", |
|
|
(i==0 ? "" : zOp), |
|
|
(b?"(":""), z, (b?")":"") |
|
|
); |
|
|
} |
|
|
if( zRet==0 ) break; |
|
|
} |
|
|
} |
|
|
|
|
|
return zRet; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void fts5ExprFunction( |
|
|
sqlite3_context *pCtx, |
|
|
int nArg, |
|
|
sqlite3_value **apVal, |
|
|
int bTcl |
|
|
){ |
|
|
Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx); |
|
|
sqlite3 *db = sqlite3_context_db_handle(pCtx); |
|
|
const char *zExpr = 0; |
|
|
char *zErr = 0; |
|
|
Fts5Expr *pExpr = 0; |
|
|
int rc; |
|
|
int i; |
|
|
|
|
|
const char **azConfig; |
|
|
const char *zNearsetCmd = "nearset"; |
|
|
int nConfig; |
|
|
Fts5Config *pConfig = 0; |
|
|
int iArg = 1; |
|
|
|
|
|
if( nArg<1 ){ |
|
|
zErr = sqlite3_mprintf("wrong number of arguments to function %s", |
|
|
bTcl ? "fts5_expr_tcl" : "fts5_expr" |
|
|
); |
|
|
sqlite3_result_error(pCtx, zErr, -1); |
|
|
sqlite3_free(zErr); |
|
|
return; |
|
|
} |
|
|
|
|
|
if( bTcl && nArg>1 ){ |
|
|
zNearsetCmd = (const char*)sqlite3_value_text(apVal[1]); |
|
|
iArg = 2; |
|
|
} |
|
|
|
|
|
nConfig = 3 + (nArg-iArg); |
|
|
azConfig = (const char**)sqlite3_malloc64(sizeof(char*) * nConfig); |
|
|
if( azConfig==0 ){ |
|
|
sqlite3_result_error_nomem(pCtx); |
|
|
return; |
|
|
} |
|
|
azConfig[0] = 0; |
|
|
azConfig[1] = "main"; |
|
|
azConfig[2] = "tbl"; |
|
|
for(i=3; iArg<nArg; iArg++){ |
|
|
const char *z = (const char*)sqlite3_value_text(apVal[iArg]); |
|
|
azConfig[i++] = (z ? z : ""); |
|
|
} |
|
|
|
|
|
zExpr = (const char*)sqlite3_value_text(apVal[0]); |
|
|
if( zExpr==0 ) zExpr = ""; |
|
|
|
|
|
rc = sqlite3Fts5ConfigParse(pGlobal, db, nConfig, azConfig, &pConfig, &zErr); |
|
|
if( rc==SQLITE_OK ){ |
|
|
rc = sqlite3Fts5ExprNew(pConfig, 0, pConfig->nCol, zExpr, &pExpr, &zErr); |
|
|
} |
|
|
if( rc==SQLITE_OK ){ |
|
|
char *zText; |
|
|
if( pExpr->pRoot->xNext==0 ){ |
|
|
zText = sqlite3_mprintf(""); |
|
|
}else if( bTcl ){ |
|
|
zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot); |
|
|
}else{ |
|
|
zText = fts5ExprPrint(pConfig, pExpr->pRoot); |
|
|
} |
|
|
if( zText==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
sqlite3_result_text(pCtx, zText, -1, SQLITE_TRANSIENT); |
|
|
sqlite3_free(zText); |
|
|
} |
|
|
} |
|
|
|
|
|
if( rc!=SQLITE_OK ){ |
|
|
if( zErr ){ |
|
|
sqlite3_result_error(pCtx, zErr, -1); |
|
|
sqlite3_free(zErr); |
|
|
}else{ |
|
|
sqlite3_result_error_code(pCtx, rc); |
|
|
} |
|
|
} |
|
|
sqlite3_free((void *)azConfig); |
|
|
sqlite3Fts5ConfigFree(pConfig); |
|
|
sqlite3Fts5ExprFree(pExpr); |
|
|
} |
|
|
|
|
|
static void fts5ExprFunctionHr( |
|
|
sqlite3_context *pCtx, |
|
|
int nArg, |
|
|
sqlite3_value **apVal |
|
|
){ |
|
|
fts5ExprFunction(pCtx, nArg, apVal, 0); |
|
|
} |
|
|
static void fts5ExprFunctionTcl( |
|
|
sqlite3_context *pCtx, |
|
|
int nArg, |
|
|
sqlite3_value **apVal |
|
|
){ |
|
|
fts5ExprFunction(pCtx, nArg, apVal, 1); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void fts5ExprIsAlnum( |
|
|
sqlite3_context *pCtx, |
|
|
int nArg, |
|
|
sqlite3_value **apVal |
|
|
){ |
|
|
int iCode; |
|
|
u8 aArr[32]; |
|
|
if( nArg!=1 ){ |
|
|
sqlite3_result_error(pCtx, |
|
|
"wrong number of arguments to function fts5_isalnum", -1 |
|
|
); |
|
|
return; |
|
|
} |
|
|
memset(aArr, 0, sizeof(aArr)); |
|
|
sqlite3Fts5UnicodeCatParse("L*", aArr); |
|
|
sqlite3Fts5UnicodeCatParse("N*", aArr); |
|
|
sqlite3Fts5UnicodeCatParse("Co", aArr); |
|
|
iCode = sqlite3_value_int(apVal[0]); |
|
|
sqlite3_result_int(pCtx, aArr[sqlite3Fts5UnicodeCategory((u32)iCode)]); |
|
|
} |
|
|
|
|
|
static void fts5ExprFold( |
|
|
sqlite3_context *pCtx, |
|
|
int nArg, |
|
|
sqlite3_value **apVal |
|
|
){ |
|
|
if( nArg!=1 && nArg!=2 ){ |
|
|
sqlite3_result_error(pCtx, |
|
|
"wrong number of arguments to function fts5_fold", -1 |
|
|
); |
|
|
}else{ |
|
|
int iCode; |
|
|
int bRemoveDiacritics = 0; |
|
|
iCode = sqlite3_value_int(apVal[0]); |
|
|
if( nArg==2 ) bRemoveDiacritics = sqlite3_value_int(apVal[1]); |
|
|
sqlite3_result_int(pCtx, sqlite3Fts5UnicodeFold(iCode, bRemoveDiacritics)); |
|
|
} |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprInit(Fts5Global *pGlobal, sqlite3 *db){ |
|
|
#if defined(SQLITE_TEST) || defined(SQLITE_FTS5_DEBUG) |
|
|
struct Fts5ExprFunc { |
|
|
const char *z; |
|
|
void (*x)(sqlite3_context*,int,sqlite3_value**); |
|
|
} aFunc[] = { |
|
|
{ "fts5_expr", fts5ExprFunctionHr }, |
|
|
{ "fts5_expr_tcl", fts5ExprFunctionTcl }, |
|
|
{ "fts5_isalnum", fts5ExprIsAlnum }, |
|
|
{ "fts5_fold", fts5ExprFold }, |
|
|
}; |
|
|
int i; |
|
|
int rc = SQLITE_OK; |
|
|
void *pCtx = (void*)pGlobal; |
|
|
|
|
|
for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){ |
|
|
struct Fts5ExprFunc *p = &aFunc[i]; |
|
|
rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0); |
|
|
} |
|
|
#else |
|
|
int rc = SQLITE_OK; |
|
|
UNUSED_PARAM2(pGlobal,db); |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef NDEBUG |
|
|
(void)sqlite3Fts5ParserTrace; |
|
|
#endif |
|
|
(void)sqlite3Fts5ParserFallback; |
|
|
|
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprPhraseCount(Fts5Expr *pExpr){ |
|
|
return (pExpr ? pExpr->nPhrase : 0); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){ |
|
|
if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0; |
|
|
return pExpr->apExprPhrase[iPhrase]->nTerm; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){ |
|
|
int nRet; |
|
|
Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase]; |
|
|
Fts5ExprNode *pNode = pPhrase->pNode; |
|
|
if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){ |
|
|
*pa = pPhrase->poslist.p; |
|
|
nRet = pPhrase->poslist.n; |
|
|
}else{ |
|
|
*pa = 0; |
|
|
nRet = 0; |
|
|
} |
|
|
return nRet; |
|
|
} |
|
|
|
|
|
struct Fts5PoslistPopulator { |
|
|
Fts5PoslistWriter writer; |
|
|
int bOk; |
|
|
int bMiss; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fts5PoslistPopulator *sqlite3Fts5ExprClearPoslists(Fts5Expr *pExpr, int bLive){ |
|
|
Fts5PoslistPopulator *pRet; |
|
|
pRet = sqlite3_malloc64(sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
|
|
if( pRet ){ |
|
|
int i; |
|
|
memset(pRet, 0, sizeof(Fts5PoslistPopulator)*pExpr->nPhrase); |
|
|
for(i=0; i<pExpr->nPhrase; i++){ |
|
|
Fts5Buffer *pBuf = &pExpr->apExprPhrase[i]->poslist; |
|
|
Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode; |
|
|
assert( pExpr->apExprPhrase[i]->nTerm<=1 ); |
|
|
if( bLive && |
|
|
(pBuf->n==0 || pNode->iRowid!=pExpr->pRoot->iRowid || pNode->bEof) |
|
|
){ |
|
|
pRet[i].bMiss = 1; |
|
|
}else{ |
|
|
pBuf->n = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
return pRet; |
|
|
} |
|
|
|
|
|
struct Fts5ExprCtx { |
|
|
Fts5Expr *pExpr; |
|
|
Fts5PoslistPopulator *aPopulator; |
|
|
i64 iOff; |
|
|
}; |
|
|
typedef struct Fts5ExprCtx Fts5ExprCtx; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5ExprColsetTest(Fts5Colset *pColset, int iCol){ |
|
|
int i; |
|
|
for(i=0; i<pColset->nCol; i++){ |
|
|
if( pColset->aiCol[i]==iCol ) return 1; |
|
|
} |
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int fts5QueryTerm(const char *pToken, int nToken){ |
|
|
int ii; |
|
|
for(ii=0; ii<nToken && pToken[ii]; ii++){} |
|
|
return ii; |
|
|
} |
|
|
|
|
|
static int fts5ExprPopulatePoslistsCb( |
|
|
void *pCtx, |
|
|
int tflags, |
|
|
const char *pToken, |
|
|
int nToken, |
|
|
int iUnused1, |
|
|
int iUnused2 |
|
|
){ |
|
|
Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx; |
|
|
Fts5Expr *pExpr = p->pExpr; |
|
|
int i; |
|
|
int nQuery = nToken; |
|
|
i64 iRowid = pExpr->pRoot->iRowid; |
|
|
|
|
|
UNUSED_PARAM2(iUnused1, iUnused2); |
|
|
|
|
|
if( nQuery>FTS5_MAX_TOKEN_SIZE ) nQuery = FTS5_MAX_TOKEN_SIZE; |
|
|
if( pExpr->pConfig->bTokendata ){ |
|
|
nQuery = fts5QueryTerm(pToken, nQuery); |
|
|
} |
|
|
if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++; |
|
|
for(i=0; i<pExpr->nPhrase; i++){ |
|
|
Fts5ExprTerm *pT; |
|
|
if( p->aPopulator[i].bOk==0 ) continue; |
|
|
for(pT=&pExpr->apExprPhrase[i]->aTerm[0]; pT; pT=pT->pSynonym){ |
|
|
if( (pT->nQueryTerm==nQuery || (pT->nQueryTerm<nQuery && pT->bPrefix)) |
|
|
&& memcmp(pT->pTerm, pToken, pT->nQueryTerm)==0 |
|
|
){ |
|
|
int rc = sqlite3Fts5PoslistWriterAppend( |
|
|
&pExpr->apExprPhrase[i]->poslist, &p->aPopulator[i].writer, p->iOff |
|
|
); |
|
|
if( rc==SQLITE_OK && pExpr->pConfig->bTokendata && !pT->bPrefix ){ |
|
|
int iCol = p->iOff>>32; |
|
|
int iTokOff = p->iOff & 0x7FFFFFFF; |
|
|
rc = sqlite3Fts5IndexIterWriteTokendata( |
|
|
pT->pIter, pToken, nToken, iRowid, iCol, iTokOff |
|
|
); |
|
|
} |
|
|
if( rc ) return rc; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
int sqlite3Fts5ExprPopulatePoslists( |
|
|
Fts5Config *pConfig, |
|
|
Fts5Expr *pExpr, |
|
|
Fts5PoslistPopulator *aPopulator, |
|
|
int iCol, |
|
|
const char *z, int n |
|
|
){ |
|
|
int i; |
|
|
Fts5ExprCtx sCtx; |
|
|
sCtx.pExpr = pExpr; |
|
|
sCtx.aPopulator = aPopulator; |
|
|
sCtx.iOff = (((i64)iCol) << 32) - 1; |
|
|
|
|
|
for(i=0; i<pExpr->nPhrase; i++){ |
|
|
Fts5ExprNode *pNode = pExpr->apExprPhrase[i]->pNode; |
|
|
Fts5Colset *pColset = pNode->pNear->pColset; |
|
|
if( (pColset && 0==fts5ExprColsetTest(pColset, iCol)) |
|
|
|| aPopulator[i].bMiss |
|
|
){ |
|
|
aPopulator[i].bOk = 0; |
|
|
}else{ |
|
|
aPopulator[i].bOk = 1; |
|
|
} |
|
|
} |
|
|
|
|
|
return sqlite3Fts5Tokenize(pConfig, |
|
|
FTS5_TOKENIZE_DOCUMENT, z, n, (void*)&sCtx, fts5ExprPopulatePoslistsCb |
|
|
); |
|
|
} |
|
|
|
|
|
static void fts5ExprClearPoslists(Fts5ExprNode *pNode){ |
|
|
if( pNode->eType==FTS5_TERM || pNode->eType==FTS5_STRING ){ |
|
|
pNode->pNear->apPhrase[0]->poslist.n = 0; |
|
|
}else{ |
|
|
int i; |
|
|
for(i=0; i<pNode->nChild; i++){ |
|
|
fts5ExprClearPoslists(pNode->apChild[i]); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
static int fts5ExprCheckPoslists(Fts5ExprNode *pNode, i64 iRowid){ |
|
|
pNode->iRowid = iRowid; |
|
|
pNode->bEof = 0; |
|
|
switch( pNode->eType ){ |
|
|
case 0: |
|
|
case FTS5_TERM: |
|
|
case FTS5_STRING: |
|
|
return (pNode->pNear->apPhrase[0]->poslist.n>0); |
|
|
|
|
|
case FTS5_AND: { |
|
|
int i; |
|
|
for(i=0; i<pNode->nChild; i++){ |
|
|
if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid)==0 ){ |
|
|
fts5ExprClearPoslists(pNode); |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
break; |
|
|
} |
|
|
|
|
|
case FTS5_OR: { |
|
|
int i; |
|
|
int bRet = 0; |
|
|
for(i=0; i<pNode->nChild; i++){ |
|
|
if( fts5ExprCheckPoslists(pNode->apChild[i], iRowid) ){ |
|
|
bRet = 1; |
|
|
} |
|
|
} |
|
|
return bRet; |
|
|
} |
|
|
|
|
|
default: { |
|
|
assert( pNode->eType==FTS5_NOT ); |
|
|
if( 0==fts5ExprCheckPoslists(pNode->apChild[0], iRowid) |
|
|
|| 0!=fts5ExprCheckPoslists(pNode->apChild[1], iRowid) |
|
|
){ |
|
|
fts5ExprClearPoslists(pNode); |
|
|
return 0; |
|
|
} |
|
|
break; |
|
|
} |
|
|
} |
|
|
return 1; |
|
|
} |
|
|
|
|
|
void sqlite3Fts5ExprCheckPoslists(Fts5Expr *pExpr, i64 iRowid){ |
|
|
fts5ExprCheckPoslists(pExpr->pRoot, iRowid); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprPhraseCollist( |
|
|
Fts5Expr *pExpr, |
|
|
int iPhrase, |
|
|
const u8 **ppCollist, |
|
|
int *pnCollist |
|
|
){ |
|
|
Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase]; |
|
|
Fts5ExprNode *pNode = pPhrase->pNode; |
|
|
int rc = SQLITE_OK; |
|
|
|
|
|
assert( iPhrase>=0 && iPhrase<pExpr->nPhrase ); |
|
|
assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS ); |
|
|
|
|
|
if( pNode->bEof==0 |
|
|
&& pNode->iRowid==pExpr->pRoot->iRowid |
|
|
&& pPhrase->poslist.n>0 |
|
|
){ |
|
|
Fts5ExprTerm *pTerm = &pPhrase->aTerm[0]; |
|
|
if( pTerm->pSynonym ){ |
|
|
Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1]; |
|
|
rc = fts5ExprSynonymList( |
|
|
pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist |
|
|
); |
|
|
}else{ |
|
|
*ppCollist = pPhrase->aTerm[0].pIter->pData; |
|
|
*pnCollist = pPhrase->aTerm[0].pIter->nData; |
|
|
} |
|
|
}else{ |
|
|
*ppCollist = 0; |
|
|
*pnCollist = 0; |
|
|
} |
|
|
|
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprQueryToken( |
|
|
Fts5Expr *pExpr, |
|
|
int iPhrase, |
|
|
int iToken, |
|
|
const char **ppOut, |
|
|
int *pnOut |
|
|
){ |
|
|
Fts5ExprPhrase *pPhrase = 0; |
|
|
|
|
|
if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){ |
|
|
return SQLITE_RANGE; |
|
|
} |
|
|
pPhrase = pExpr->apExprPhrase[iPhrase]; |
|
|
if( iToken<0 || iToken>=pPhrase->nTerm ){ |
|
|
return SQLITE_RANGE; |
|
|
} |
|
|
|
|
|
*ppOut = pPhrase->aTerm[iToken].pTerm; |
|
|
*pnOut = pPhrase->aTerm[iToken].nFullTerm; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int sqlite3Fts5ExprInstToken( |
|
|
Fts5Expr *pExpr, |
|
|
i64 iRowid, |
|
|
int iPhrase, |
|
|
int iCol, |
|
|
int iOff, |
|
|
int iToken, |
|
|
const char **ppOut, |
|
|
int *pnOut |
|
|
){ |
|
|
Fts5ExprPhrase *pPhrase = 0; |
|
|
Fts5ExprTerm *pTerm = 0; |
|
|
int rc = SQLITE_OK; |
|
|
|
|
|
if( iPhrase<0 || iPhrase>=pExpr->nPhrase ){ |
|
|
return SQLITE_RANGE; |
|
|
} |
|
|
pPhrase = pExpr->apExprPhrase[iPhrase]; |
|
|
if( iToken<0 || iToken>=pPhrase->nTerm ){ |
|
|
return SQLITE_RANGE; |
|
|
} |
|
|
pTerm = &pPhrase->aTerm[iToken]; |
|
|
if( pTerm->bPrefix==0 ){ |
|
|
if( pExpr->pConfig->bTokendata ){ |
|
|
rc = sqlite3Fts5IterToken( |
|
|
pTerm->pIter, iRowid, iCol, iOff+iToken, ppOut, pnOut |
|
|
); |
|
|
}else{ |
|
|
*ppOut = pTerm->pTerm; |
|
|
*pnOut = pTerm->nFullTerm; |
|
|
} |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sqlite3Fts5ExprClearTokens(Fts5Expr *pExpr){ |
|
|
int ii; |
|
|
for(ii=0; ii<pExpr->nPhrase; ii++){ |
|
|
Fts5ExprTerm *pT; |
|
|
for(pT=&pExpr->apExprPhrase[ii]->aTerm[0]; pT; pT=pT->pSynonym){ |
|
|
sqlite3Fts5IndexIterClearTokendata(pT->pIter); |
|
|
} |
|
|
} |
|
|
} |
|
|
|