Index: ext/fts5/fts5.c ================================================================== --- ext/fts5/fts5.c +++ ext/fts5/fts5.c @@ -54,32 +54,49 @@ sqlite3_vtab base; /* Base class used by SQLite core */ Fts5Config *pConfig; /* Virtual table configuration */ Fts5Index *pIndex; /* Full-text index */ Fts5Storage *pStorage; /* Document store */ Fts5Global *pGlobal; /* Global (connection wide) data */ + Fts5Cursor *pSortCsr; /* Sort data from this cursor */ }; struct Fts5MatchPhrase { Fts5Buffer *pPoslist; /* Pointer to current poslist */ int nTerm; /* Size of phrase in terms */ }; + +/* +** Variable pStmt is set to a compiled SQL statement of the form: +** +** SELECT rowid, FROM ORDER BY +rank; +** +*/ +struct Fts5Sorter { + sqlite3_stmt *pStmt; + i64 iRowid; /* Current rowid */ + u8 *aPoslist; /* Position lists for current row */ + int nIdx; /* Number of entries in aIdx[] */ + int aIdx[0]; /* Offsets into aPoslist for current row */ +}; /* ** Virtual-table cursor object. */ struct Fts5Cursor { sqlite3_vtab_cursor base; /* Base class used by SQLite core */ int idxNum; /* idxNum passed to xFilter() */ sqlite3_stmt *pStmt; /* Statement used to read %_content */ Fts5Expr *pExpr; /* Expression for MATCH queries */ + Fts5Sorter *pSorter; /* Sorter for "ORDER BY rank" queries */ int csrflags; /* Mask of cursor flags (see below) */ Fts5Cursor *pNext; /* Next cursor in Fts5Cursor.pCsr list */ + Fts5Auxiliary *pRank; /* Rank callback (or NULL) */ /* Variables used by auxiliary functions */ i64 iCsrId; /* Cursor id */ Fts5Auxiliary *pAux; /* Currently executing extension function */ - Fts5Auxdata *pAuxdata; /* First in linked list of aux-data */ + Fts5Auxdata *pAuxdata; /* First in linked list of saved aux-data */ int *aColumnSize; /* Values for xColumnSize() */ }; /* ** Values for Fts5Cursor.csrflags @@ -225,13 +242,15 @@ } /* ** The three query plans xBestIndex may choose between. */ -#define FTS5_PLAN_SCAN 1 /* No usable constraint */ -#define FTS5_PLAN_MATCH 2 /* ( MATCH ?) */ -#define FTS5_PLAN_ROWID 3 /* (rowid = ?) */ +#define FTS5_PLAN_SCAN 1 /* No usable constraint */ +#define FTS5_PLAN_MATCH 2 /* ( MATCH ?) */ +#define FTS5_PLAN_SORTED_MATCH 3 /* ( MATCH ? ORDER BY rank) */ +#define FTS5_PLAN_ROWID 4 /* (rowid = ?) */ +#define FTS5_PLAN_SOURCE 5 /* A source cursor for SORTED_MATCH */ #define FTS5_PLAN(idxNum) ((idxNum) & 0x7) #define FTS5_ORDER_DESC 8 /* ORDER BY rowid DESC */ #define FTS5_ORDER_ASC 16 /* ORDER BY rowid ASC */ @@ -282,13 +301,24 @@ pInfo->aConstraintUsage[iCons].omit = 1; }else{ pInfo->estimatedCost = 10000000.0; } - if( pInfo->nOrderBy==1 && pInfo->aOrderBy[0].iColumn<0 ){ - pInfo->orderByConsumed = 1; - ePlan |= pInfo->aOrderBy[0].desc ? FTS5_ORDER_DESC : FTS5_ORDER_ASC; + if( pInfo->nOrderBy==1 ){ + int iSort = pInfo->aOrderBy[0].iColumn; + if( iSort<0 ){ + /* ORDER BY rowid [ASC|DESC] */ + pInfo->orderByConsumed = 1; + }else if( iSort==(pConfig->nCol+1) && ePlan==FTS5_PLAN_MATCH ){ + /* ORDER BY rank [ASC|DESC] */ + pInfo->orderByConsumed = 1; + ePlan = FTS5_PLAN_SORTED_MATCH; + } + + if( pInfo->orderByConsumed ){ + ePlan |= pInfo->aOrderBy[0].desc ? FTS5_ORDER_DESC : FTS5_ORDER_ASC; + } } pInfo->idxNum = ePlan; return SQLITE_OK; } @@ -339,11 +369,19 @@ if( pCsr->pStmt ){ int eStmt = fts5StmtType(pCsr->idxNum); sqlite3Fts5StorageStmtRelease(pTab->pStorage, eStmt, pCsr->pStmt); } - sqlite3Fts5ExprFree(pCsr->pExpr); + if( pCsr->pSorter ){ + Fts5Sorter *pSorter = pCsr->pSorter; + sqlite3_finalize(pSorter->pStmt); + sqlite3_free(pSorter); + } + + if( pCsr->idxNum!=FTS5_PLAN_SOURCE ){ + sqlite3Fts5ExprFree(pCsr->pExpr); + } for(pData=pCsr->pAuxdata; pData; pData=pNext){ pNext = pData->pNext; if( pData->xDelete ) pData->xDelete(pData->pPtr); sqlite3_free(pData); @@ -355,10 +393,25 @@ sqlite3_free(pCsr); return SQLITE_OK; } +static int fts5SorterNext(Fts5Cursor *pCsr){ + Fts5Sorter *pSorter = pCsr->pSorter; + int rc; + + rc = sqlite3_step(pSorter->pStmt); + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + CsrFlagSet(pCsr, FTS5CSR_EOF); + }else if( rc==SQLITE_ROW ){ + rc = SQLITE_OK; + pSorter->iRowid = sqlite3_column_int64(pSorter->pStmt, 0); + } + + return rc; +} /* ** Advance the cursor to the next row in the table that matches the ** search criteria. ** @@ -369,26 +422,78 @@ static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){ Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; int ePlan = FTS5_PLAN(pCsr->idxNum); int rc = SQLITE_OK; - if( ePlan!=FTS5_PLAN_MATCH ){ - rc = sqlite3_step(pCsr->pStmt); - if( rc!=SQLITE_ROW ){ - CsrFlagSet(pCsr, FTS5CSR_EOF); - rc = sqlite3_reset(pCsr->pStmt); - }else{ - rc = SQLITE_OK; - } - }else{ - rc = sqlite3Fts5ExprNext(pCsr->pExpr); - if( sqlite3Fts5ExprEof(pCsr->pExpr) ){ - CsrFlagSet(pCsr, FTS5CSR_EOF); - } - CsrFlagSet(pCsr, FTS5CSR_REQUIRE_CONTENT | FTS5CSR_REQUIRE_DOCSIZE ); - } - + switch( ePlan ){ + case FTS5_PLAN_MATCH: + case FTS5_PLAN_SOURCE: + rc = sqlite3Fts5ExprNext(pCsr->pExpr); + if( sqlite3Fts5ExprEof(pCsr->pExpr) ){ + CsrFlagSet(pCsr, FTS5CSR_EOF); + } + CsrFlagSet(pCsr, FTS5CSR_REQUIRE_CONTENT | FTS5CSR_REQUIRE_DOCSIZE ); + break; + + case FTS5_PLAN_SORTED_MATCH: { + rc = fts5SorterNext(pCsr); + break; + } + + default: + rc = sqlite3_step(pCsr->pStmt); + if( rc!=SQLITE_ROW ){ + CsrFlagSet(pCsr, FTS5CSR_EOF); + rc = sqlite3_reset(pCsr->pStmt); + }else{ + rc = SQLITE_OK; + } + break; + } + + return rc; +} + +static int fts5CursorFirstSorted(Fts5Table *pTab, Fts5Cursor *pCsr, int bAsc){ + Fts5Config *pConfig = pTab->pConfig; + Fts5Sorter *pSorter; + int nPhrase; + int nByte; + int rc = SQLITE_OK; + char *zSql; + + nPhrase = sqlite3Fts5ExprPhraseCount(pCsr->pExpr); + nByte = sizeof(Fts5Sorter) + sizeof(int) * nPhrase; + pSorter = (Fts5Sorter*)sqlite3_malloc(nByte); + if( pSorter==0 ) return SQLITE_NOMEM; + memset(pSorter, 0, nByte); + + zSql = sqlite3_mprintf("SELECT rowid, %Q FROM %Q.%Q ORDER BY +%s %s", + pConfig->zName, pConfig->zDb, pConfig->zName, FTS5_RANK_NAME, + bAsc ? "ASC" : "DESC" + ); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(pConfig->db, zSql, -1, &pSorter->pStmt, 0); + sqlite3_free(zSql); + } + + pCsr->pSorter = pSorter; + if( rc==SQLITE_OK ){ + assert( pTab->pSortCsr==0 ); + pTab->pSortCsr = pCsr; + rc = fts5SorterNext(pCsr); + pTab->pSortCsr = 0; + } + + if( rc!=SQLITE_OK ){ + sqlite3_finalize(pSorter->pStmt); + sqlite3_free(pSorter); + pCsr->pSorter = 0; + } + return rc; } static int fts5CursorFirst(Fts5Table *pTab, Fts5Cursor *pCsr, int bAsc){ int rc; @@ -412,34 +517,47 @@ int nVal, /* Number of elements in apVal */ sqlite3_value **apVal /* Arguments for the indexing scheme */ ){ Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab); Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; - int rc = SQLITE_OK; - int ePlan = FTS5_PLAN(idxNum); - int eStmt = fts5StmtType(idxNum); int bAsc = ((idxNum & FTS5_ORDER_ASC) ? 1 : 0); + int rc = SQLITE_OK; - pCsr->idxNum = idxNum; assert( pCsr->pStmt==0 ); assert( pCsr->pExpr==0 ); assert( pCsr->csrflags==0 ); - - rc = sqlite3Fts5StorageStmt(pTab->pStorage, eStmt, &pCsr->pStmt); - if( rc==SQLITE_OK ){ - if( ePlan==FTS5_PLAN_MATCH ){ - char **pzErr = &pTab->base.zErrMsg; - const char *zExpr = (const char*)sqlite3_value_text(apVal[0]); - rc = sqlite3Fts5ExprNew(pTab->pConfig, zExpr, &pCsr->pExpr, pzErr); - if( rc==SQLITE_OK ){ - rc = fts5CursorFirst(pTab, pCsr, bAsc); - } - }else{ - if( ePlan==FTS5_PLAN_ROWID ){ - sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]); - } - rc = fts5NextMethod(pCursor); + assert( pCsr->pRank==0 ); + + if( pTab->pSortCsr ){ + pCsr->idxNum = FTS5_PLAN_SOURCE; + pCsr->pRank = pTab->pSortCsr->pRank; + pCsr->pExpr = pTab->pSortCsr->pExpr; + rc = fts5CursorFirst(pTab, pCsr, bAsc); + }else{ + int ePlan = FTS5_PLAN(idxNum); + int eStmt = fts5StmtType(idxNum); + pCsr->idxNum = idxNum; + rc = sqlite3Fts5StorageStmt(pTab->pStorage, eStmt, &pCsr->pStmt); + if( rc==SQLITE_OK ){ + if( ePlan==FTS5_PLAN_MATCH || ePlan==FTS5_PLAN_SORTED_MATCH ){ + char **pzErr = &pTab->base.zErrMsg; + const char *zExpr = (const char*)sqlite3_value_text(apVal[0]); + pCsr->pRank = pTab->pGlobal->pAux; + rc = sqlite3Fts5ExprNew(pTab->pConfig, zExpr, &pCsr->pExpr, pzErr); + if( rc==SQLITE_OK ){ + if( ePlan==FTS5_PLAN_MATCH ){ + rc = fts5CursorFirst(pTab, pCsr, bAsc); + }else{ + rc = fts5CursorFirstSorted(pTab, pCsr, bAsc); + } + } + }else{ + if( ePlan==FTS5_PLAN_ROWID ){ + sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]); + } + rc = fts5NextMethod(pCursor); + } } } return rc; } @@ -462,14 +580,23 @@ static int fts5RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; int ePlan = FTS5_PLAN(pCsr->idxNum); assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 ); - if( ePlan!=FTS5_PLAN_MATCH ){ - *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); - }else{ - *pRowid = sqlite3Fts5ExprRowid(pCsr->pExpr); + switch( ePlan ){ + case FTS5_PLAN_SOURCE: + case FTS5_PLAN_MATCH: + *pRowid = sqlite3Fts5ExprRowid(pCsr->pExpr); + break; + + case FTS5_PLAN_SORTED_MATCH: + *pRowid = pCsr->pSorter->iRowid; + break; + + default: + *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); + break; } return SQLITE_OK; } @@ -492,40 +619,10 @@ if( rc==SQLITE_OK ){ rc = SQLITE_CORRUPT_VTAB; } } } - return rc; -} - -/* -** This is the xColumn method, called by SQLite to request a value from -** the row that the supplied cursor currently points to. -*/ -static int fts5ColumnMethod( - sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ - sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ - int iCol /* Index of column to read value from */ -){ - Fts5Config *pConfig = ((Fts5Table*)(pCursor->pVtab))->pConfig; - Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; - int rc = SQLITE_OK; - - assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 ); - - if( iCol==pConfig->nCol ){ - /* User is requesting the value of the special column with the same name - ** as the table. Return the cursor integer id number. This value is only - ** useful in that it may be passed as the first argument to an FTS5 - ** auxiliary function. */ - sqlite3_result_int64(pCtx, pCsr->iCsrId); - }else{ - rc = fts5SeekCursor(pCsr); - if( rc==SQLITE_OK ){ - sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1)); - } - } return rc; } /* ** This function is called to handle an FTS INSERT command. In other words, @@ -571,11 +668,19 @@ Fts5Config *pConfig = pTab->pConfig; int eType0; /* value_type() of apVal[0] */ int eConflict; /* ON CONFLICT for this DML */ int rc = SQLITE_OK; /* Return code */ - assert( nArg==1 || nArg==(2 + pConfig->nCol + 1) ); + /* A delete specifies a single argument - the rowid of the row to remove. + ** Update and insert operations pass: + ** + ** 1. The "old" rowid, or NULL. + ** 2. The "new" rowid. + ** 3. Values for each of the nCol matchable columns. + ** 4. Values for the two hidden columns ( and "rank"). + */ + assert( nArg==1 || nArg==(2 + pConfig->nCol + 2) ); if( nArg>1 && SQLITE_NULL!=sqlite3_value_type(apVal[2 + pConfig->nCol]) ){ return fts5SpecialCommand(pTab, apVal[2 + pConfig->nCol]); } @@ -837,10 +942,23 @@ } fts5CloseMethod((sqlite3_vtab_cursor*)pNew); return rc; } + +static void fts5ApiInvoke( + Fts5Auxiliary *pAux, + Fts5Cursor *pCsr, + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( pCsr->pAux==0 ); + pCsr->pAux = pAux; + pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc, argv); + pCsr->pAux = 0; +} static void fts5ApiCallback( sqlite3_context *context, int argc, sqlite3_value **argv @@ -859,15 +977,51 @@ } if( pCsr==0 ){ char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId); sqlite3_result_error(context, zErr, -1); }else{ - assert( pCsr->pAux==0 ); - pCsr->pAux = pAux; - pAux->xFunc(&sFts5Api, (Fts5Context*)pCsr, context, argc-1, &argv[1]); - pCsr->pAux = 0; + fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]); + } +} + +/* +** This is the xColumn method, called by SQLite to request a value from +** the row that the supplied cursor currently points to. +*/ +static int fts5ColumnMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */ + int iCol /* Index of column to read value from */ +){ + Fts5Config *pConfig = ((Fts5Table*)(pCursor->pVtab))->pConfig; + Fts5Cursor *pCsr = (Fts5Cursor*)pCursor; + int rc = SQLITE_OK; + + assert( CsrFlagTest(pCsr, FTS5CSR_EOF)==0 ); + + if( iCol==pConfig->nCol ){ + if( FTS5_PLAN(pCsr->idxNum)==FTS5_PLAN_SOURCE ){ + /* todo */ + }else{ + /* User is requesting the value of the special column with the same name + ** as the table. Return the cursor integer id number. This value is only + ** useful in that it may be passed as the first argument to an FTS5 + ** auxiliary function. */ + sqlite3_result_int64(pCtx, pCsr->iCsrId); + } + }else if( iCol==pConfig->nCol+1 ){ + /* The value of the "rank" column. */ + if( pCsr->pRank ){ + fts5ApiInvoke(pCsr->pRank, pCsr, pCtx, 0, 0); + } + }else{ + rc = fts5SeekCursor(pCsr); + if( rc==SQLITE_OK ){ + sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1)); + } } + return rc; } /* ** This routine implements the xFindFunction method for the FTS3 Index: ext/fts5/fts5Int.h ================================================================== --- ext/fts5/fts5Int.h +++ ext/fts5/fts5Int.h @@ -26,10 +26,13 @@ */ #define FTS5_MAX_PREFIX_INDEXES 31 #define FTS5_DEFAULT_NEARDIST 10 +/* Name of rank column */ +#define FTS5_RANK_NAME "rank" + /************************************************************************** ** Interface to code in fts5_config.c. fts5_config.c contains contains code ** to parse the arguments passed to the CREATE VIRTUAL TABLE statement. */ @@ -392,11 +395,10 @@ void sqlite3Fts5ParseSetDistance(Fts5Parse*, Fts5ExprNearset*, Fts5Token*); void sqlite3Fts5ParseSetColumn(Fts5Parse*, Fts5ExprNearset*, Fts5Token*); void sqlite3Fts5ParseFinished(Fts5Parse *pParse, Fts5ExprNode *p); void sqlite3Fts5ParseNear(Fts5Parse *pParse, Fts5Token*); - /* ** End of interface to code in fts5_expr.c. **************************************************************************/ @@ -421,9 +423,20 @@ ** Interface to code in fts5_aux.c. */ int sqlite3Fts5AuxInit(Fts5Global*); /* -** End of interface to code in fts5_expr.c. +** End of interface to code in fts5_aux.c. +**************************************************************************/ + +/************************************************************************** +** Interface to code in fts5_sorter.c. +*/ +typedef struct Fts5Sorter Fts5Sorter; + +int sqlite3Fts5SorterNew(Fts5Expr *pExpr, Fts5Sorter **pp); + +/* +** End of interface to code in fts5_sorter.c. **************************************************************************/ #endif Index: ext/fts5/fts5_aux.c ================================================================== --- ext/fts5/fts5_aux.c +++ ext/fts5/fts5_aux.c @@ -954,14 +954,14 @@ const char *zFunc; /* Function name (nul-terminated) */ void *pUserData; /* User-data pointer */ fts5_extension_function xFunc;/* Callback function */ void (*xDestroy)(void*); /* Destructor function */ } aBuiltin [] = { - { "bm25", 0, fts5Bm25Function, 0 }, { "bm25debug", (void*)1, fts5Bm25Function, 0 }, { "snippet", 0, fts5SnippetFunction, 0 }, { "fts5_test", 0, fts5TestFunction, 0 }, + { "bm25", 0, fts5Bm25Function, 0 }, }; int rc = SQLITE_OK; /* Return code */ int i; /* To iterate through builtin functions */ Index: ext/fts5/fts5_config.c ================================================================== --- ext/fts5/fts5_config.c +++ ext/fts5/fts5_config.c @@ -163,11 +163,14 @@ pRet->db = db; pRet->azCol = (char**)sqlite3_malloc(sizeof(char*) * nArg); pRet->zDb = fts5Strdup(azArg[1]); pRet->zName = fts5Strdup(azArg[2]); - if( pRet->azCol==0 || pRet->zDb==0 || pRet->zName==0 ){ + if( sqlite3_stricmp(pRet->zName, FTS5_RANK_NAME)==0 ){ + *pzErr = sqlite3_mprintf("reserved fts5 table name: %s", pRet->zName); + rc = SQLITE_ERROR; + }else if( pRet->azCol==0 || pRet->zDb==0 || pRet->zName==0 ){ rc = SQLITE_NOMEM; }else{ int i; for(i=3; rc==SQLITE_OK && iazCol[pRet->nCol++] = zDup; + if( sqlite3_stricmp(zDup, FTS5_RANK_NAME)==0 ){ + *pzErr = sqlite3_mprintf("reserved fts5 column name: %s", zDup); + rc = SQLITE_ERROR; + } } } } } @@ -247,11 +255,13 @@ sqlite3_free(zOld); } if( zSql ){ zOld = zSql; - zSql = sqlite3_mprintf("%s, %Q HIDDEN)", zOld, pConfig->zName); + zSql = sqlite3_mprintf("%s, %Q HIDDEN, %s HIDDEN)", + zOld, pConfig->zName, FTS5_RANK_NAME + ); sqlite3_free(zOld); } if( zSql==0 ){ rc = SQLITE_NOMEM; Index: ext/fts5/fts5_expr.c ================================================================== --- ext/fts5/fts5_expr.c +++ ext/fts5/fts5_expr.c @@ -32,11 +32,11 @@ struct Fts5Expr { Fts5Index *pIndex; Fts5ExprNode *pRoot; int bAsc; int nPhrase; /* Number of phrases in expression */ - Fts5ExprPhrase **apPhrase; /* Pointers to phrase objects */ + Fts5ExprPhrase **apExprPhrase; /* Pointers to phrase objects */ }; /* ** eType: ** Expression node type. Always one of: @@ -214,11 +214,11 @@ if( pNew==0 ){ sParse.rc = SQLITE_NOMEM; }else{ pNew->pRoot = sParse.pExpr; pNew->pIndex = 0; - pNew->apPhrase = sParse.apPhrase; + pNew->apExprPhrase = sParse.apPhrase; pNew->nPhrase = sParse.nPhrase; sParse.apPhrase = 0; } } @@ -273,11 +273,11 @@ Fts5ExprPhrase **apPhrase; Fts5ExprNode *pNode; Fts5ExprNearset *pNear; Fts5ExprPhrase *pCopy; - pOrig = pExpr->apPhrase[iPhrase]; + pOrig = pExpr->apExprPhrase[iPhrase]; pNew = (Fts5Expr*)fts5ExprMalloc(&rc, sizeof(Fts5Expr)); apPhrase = (Fts5ExprPhrase**)fts5ExprMalloc(&rc, sizeof(Fts5ExprPhrase*)); pNode = (Fts5ExprNode*)fts5ExprMalloc(&rc, sizeof(Fts5ExprNode)); pNear = (Fts5ExprNearset*)fts5ExprMalloc(&rc, sizeof(Fts5ExprNearset) + sizeof(Fts5ExprPhrase*) @@ -294,12 +294,12 @@ if( rc==SQLITE_OK ){ /* All the allocations succeeded. Put the expression object together. */ pNew->pIndex = pExpr->pIndex; pNew->pRoot = pNode; pNew->nPhrase = 1; - pNew->apPhrase = apPhrase; - pNew->apPhrase[0] = pCopy; + pNew->apExprPhrase = apPhrase; + pNew->apExprPhrase[0] = pCopy; pNode->eType = FTS5_STRING; pNode->pNear = pNear; pNear->iCol = -1; @@ -343,11 +343,11 @@ ** Free the expression object passed as the only argument. */ void sqlite3Fts5ExprFree(Fts5Expr *p){ if( p ){ sqlite3Fts5ParseNodeFree(p->pRoot); - sqlite3_free(p->apPhrase); + sqlite3_free(p->apExprPhrase); sqlite3_free(p); } } /* @@ -1586,20 +1586,20 @@ /* ** Return the number of terms in the iPhrase'th phrase in pExpr. */ int sqlite3Fts5ExprPhraseSize(Fts5Expr *pExpr, int iPhrase){ if( iPhrase<0 || iPhrase>=pExpr->nPhrase ) return 0; - return pExpr->apPhrase[iPhrase]->nTerm; + return pExpr->apExprPhrase[iPhrase]->nTerm; } /* ** This function is used to access the current position list for phrase ** iPhrase. */ int sqlite3Fts5ExprPoslist(Fts5Expr *pExpr, int iPhrase, const u8 **pa){ if( iPhrase>=0 && iPhrasenPhrase ){ - Fts5ExprPhrase *pPhrase = pExpr->apPhrase[iPhrase]; + Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase]; Fts5ExprNode *pNode = pPhrase->pNode; if( pNode->bEof==0 && pNode->iRowid==pExpr->pRoot->iRowid ){ *pa = pPhrase->poslist.p; return pPhrase->poslist.n; } Index: test/fts5aa.test ================================================================== --- test/fts5aa.test +++ test/fts5aa.test @@ -273,8 +273,17 @@ do_execsql_test 10.3.$rowid.2 { INSERT INTO t1(t1) VALUES('integrity-check') } } do_execsql_test 10.4.1 { DELETE FROM t1 } do_execsql_test 10.4.2 { INSERT INTO t1(t1) VALUES('integrity-check') } + +#------------------------------------------------------------------------- +# +do_catchsql_test 11.1 { + CREATE VIRTUAL TABLE t2 USING fts5(a, b, c, rank); +} {1 {reserved fts5 column name: rank}} +do_catchsql_test 11.2 { + CREATE VIRTUAL TABLE rank USING fts5(a, b, c); +} {1 {reserved fts5 table name: rank}} finish_test Index: test/fts5ae.test ================================================================== --- test/fts5ae.test +++ test/fts5ae.test @@ -259,13 +259,21 @@ 3 {c} {1 0} 4 {d} {2 3} 5 {g AND (e OR f)} {5 4} 6 {j AND (h OR i)} {5 6} } { - do_execsql_test 8.2.$tn { + do_execsql_test 8.2.$tn.1 { SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY bm25(t8) DESC; } $res + + do_execsql_test 8.2.$tn.2 { + SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY +rank DESC; + } $res + + do_execsql_test 8.3.$tn.3 { + SELECT rowid FROM t8 WHERE t8 MATCH $q ORDER BY rank DESC; + } $res } finish_test