From be8a7835ea596518d5113fbf8b60d0780430ca7e Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Tue, 13 Jun 2006 15:00:54 +0000 Subject: [PATCH] Add the tentative sqlite3_allocate_queryplan() API. (CVS 3228) FossilOrigin-Name: 7a3e97f76b1f4f97a04f7c5a9daa400402b2ff25 --- manifest | 22 +++++++++++----------- manifest.uuid | 2 +- src/sqlite.h.in | 16 +++++++++++++--- src/test8.c | 24 ++++++++++++++++++------ src/vdbe.c | 14 ++++++-------- src/vtab.c | 13 ++++++++++++- src/where.c | 16 +++++++++++----- test/vtab1.test | 10 +++++----- 8 files changed, 77 insertions(+), 40 deletions(-) diff --git a/manifest b/manifest index 493d3d9481..23ef221aef 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\ssimple\stests\sfor\sthe\sxFilter\sand\sxBestIndex\smethods.\s(CVS\s3227) -D 2006-06-13T14:16:59 +C Add\sthe\stentative\ssqlite3_allocate_queryplan()\sAPI.\s(CVS\s3228) +D 2006-06-13T15:00:55 F Makefile.in 56fd6261e83f60724e6dcd764e06ab68cbd53909 F Makefile.linux-gcc 74ba0eadf88748a9ce3fd03d2a3ede2e6715baec F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -72,7 +72,7 @@ F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261 F src/select.c 38eda11d950ed5e631ea9054f84a4a8b9e9b39d8 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/shell.c 55bf6335dae7146f7a300039f5d6bb35010f1996 -F src/sqlite.h.in 8a515d52a95aafff6eaaf13e6db38d3325b76af8 +F src/sqlite.h.in 92cbeeacf040bffe109057d3af2db9dc63db8a67 F src/sqlite3ext.h 127bd394c8eea481f2ac9b754bf399dbfc818b75 F src/sqliteInt.h 91cc3603fe2e1be18d52490a2fa65ed9640fb338 F src/table.c f64ec4fbfe333f8df925bc6ba494f55e05b0e75e @@ -84,7 +84,7 @@ F src/test4.c 8b784cd82de158a2317cb4ac4bc86f91ad315e25 F src/test5.c 7162f8526affb771c4ed256826eee7bb9eca265f F src/test6.c 60a02961ceb7b3edc25f5dc5c1ac2556622a76de F src/test7.c 03fa8d787f6aebc6d1f72504d52f33013ad2c8e3 -F src/test8.c 61d89b51c3cd3407f860134bff9011b081afc69c +F src/test8.c 34b04bd826b3f7fa843c5b60052928412591c5c7 F src/test_async.c e3deaedd4d86a56391b81808fde9e44fbd92f1d3 F src/test_md5.c 6c42bc0a3c0b54be34623ff77a0eec32b2fa96e3 F src/test_server.c a6460daed0b92ecbc2531b6dc73717470e7a648c @@ -94,15 +94,15 @@ F src/update.c 0186f09414a6578156d40666becc964f85c2a616 F src/utf.c ab81ac59084ff1c07d421eb1a0a84ec809603b44 F src/util.c ca6ee72772c0f5dc04d2e0ab1973fd3b6a9bf79d F src/vacuum.c 5b37d0f436f8e1ffacd17934e44720b38d2247f9 -F src/vdbe.c e2d6072b36a3b32310916dd8443d811eb1b65747 +F src/vdbe.c 1d5ae83b7122146c0193f52750e37aacd633bccc F src/vdbe.h f72e5c00af759b7ed6fd606d508036d732098cc3 F src/vdbeInt.h 6ccb7eaae76ebd761470f6a035501ff33aa92c20 F src/vdbeapi.c 7dc662e7c905ce666bb506dced932e0307115cbf F src/vdbeaux.c 0168d770d03f9815511780a49cd8360d9a5f1ec5 F src/vdbefifo.c 9efb94c8c3f4c979ebd0028219483f88e57584f5 F src/vdbemem.c 5f0afe3b92bb2c037f8d5d697f7c151fa50783a3 -F src/vtab.c 0e39af5822bb17b0007b0df7fad527aa0e3ebd38 -F src/where.c f2e17c6f50e137c14f05f501f0ba15235a711986 +F src/vtab.c 12d83f7de893d06592d6d37c285defefebbd2d48 +F src/where.c 0b1fcf590040175e20c6d8f2e13485b683d3d03c F tclinstaller.tcl 046e3624671962dc50f0481d7c25b38ef803eb42 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/all.test 5df90d015ca63fcef2a4b62c24f7316b66c4bfd4 @@ -287,7 +287,7 @@ F test/vacuum.test 37f998b841cb335397c26d9bbc3457182af2565f F test/vacuum2.test 5aea8c88a65cb29f7d175296e7c819c6158d838c F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102 F test/view.test 16e2774fe35e47a07ac4471b7f0bcc948b1aa6d5 -F test/vtab1.test 56b29fcdafec53841730933f99a927e23cd593a6 +F test/vtab1.test c94c8bdadfdd9c474146874562925830ab0413c5 F test/where.test ee7c9a6659b07e1ee61177f6e7ff71565ee2c9df F test/where2.test a16476a5913e75cf65b38f2daa6157a6b7791394 F test/where3.test 3b5ad2c58069e12be2bd86bc5e211a82810521aa @@ -363,7 +363,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 -P 55392922686691db3d41525a7f4cb7f976e73467 -R f0c782b5451cd45871a352c2a0318f22 +P 0f4657ea69314e49bc7c9faf9a653ef072f5082d +R b1fdb74ba8eeb04baf985a2eb223a6b1 U danielk1977 -Z c4c527997467541782ddbca206039900 +Z f48f6df0484e720f46ed17f757e9f5ce diff --git a/manifest.uuid b/manifest.uuid index 019fa4700a..7e48217efa 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -0f4657ea69314e49bc7c9faf9a653ef072f5082d \ No newline at end of file +7a3e97f76b1f4f97a04f7c5a9daa400402b2ff25 \ No newline at end of file diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 023c90fff5..f214380383 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -12,7 +12,7 @@ ** This header file defines the interface that the SQLite library ** presents to client programs. ** -** @(#) $Id: sqlite.h.in,v 1.172 2006/06/13 14:16:59 danielk1977 Exp $ +** @(#) $Id: sqlite.h.in,v 1.173 2006/06/13 15:00:55 danielk1977 Exp $ */ #ifndef _SQLITE3_H_ #define _SQLITE3_H_ @@ -1547,7 +1547,7 @@ struct sqlite3_module { int (*xDestroy)(sqlite3_vtab *pVTab); int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); int (*xClose)(sqlite3_vtab_cursor*); - int (*xFilter)(sqlite3_vtab_cursor*, int idx, + int (*xFilter)(sqlite3_vtab_cursor*, char *zPlan, int nPlan, int argc, sqlite3_value **argv); int (*xNext)(sqlite3_vtab_cursor*); int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); @@ -1626,7 +1626,10 @@ struct sqlite3_index_info { int argvIndex; /* if >0, constraint is part of argv to xFilter */ unsigned char omit; /* Do not code a test for this constraint */ } *const aConstraintUsage; - int idxNum; /* Index number passed to xFilter */ + + char *zPlan; /* xBestIndex blob passed to xFilter */ + int nPlan; /* Size of nPlan */ + int orderByConsumed; /* True if output is already ordered */ double estimatedCost; /* Estimated cost of using this index */ }; @@ -1682,6 +1685,13 @@ struct sqlite3_vtab_cursor { */ int sqlite3_declare_vtab(sqlite3*, const char *zCreateTable); +/* +** This function is called by the xBestIndex method of a module to +** allocate space to store the query-plan passed to the corresponding +** xFilter invocation(s). +*/ +char *sqlite3_allocate_queryplan(sqlite3_index_info *, int); + /* ** The interface to the virtual-table mechanism defined above (back up ** to a comment remarkably similar to this one) is currently considered diff --git a/src/test8.c b/src/test8.c index d95401ae53..5f005e03c1 100644 --- a/src/test8.c +++ b/src/test8.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test8.c,v 1.9 2006/06/13 14:16:59 danielk1977 Exp $ +** $Id: test8.c,v 1.10 2006/06/13 15:00:55 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -344,21 +344,19 @@ static int echoRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ static int echoFilter( sqlite3_vtab_cursor *pVtabCursor, - int idx, + char *zPlan, int nPlan, int argc, sqlite3_value **argv ){ int rc; - char zBuf[32]; int ii; echo_cursor *pCur = (echo_cursor *)pVtabCursor; echo_vtab *pVtab = (echo_vtab *)pVtabCursor->pVtab; sqlite3 *db = pVtab->db; - sprintf(zBuf, "%d", idx); appendToEchoModule(pVtab->interp, "xFilter"); - appendToEchoModule(pVtab->interp, zBuf); + appendToEchoModule(pVtab->interp, zPlan); for(ii=0; iiinterp, sqlite3_value_text(argv[ii])); } @@ -390,6 +388,8 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ int ii; char *zWhere = 0; char *zOrder = 0; + char *zPlan = 0; + int nPlan = 0; int nArg = 0; echo_vtab *pVtab = (echo_vtab *)tab; @@ -435,10 +435,22 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ appendToEchoModule(pVtab->interp, zWhere); appendToEchoModule(pVtab->interp, zOrder); + nPlan = 2; + if( zWhere ){ + nPlan += strlen(zWhere); + } + if( zOrder ){ + nPlan += strlen(zWhere); + } + zPlan = sqlite3_allocate_queryplan(pIdxInfo, nPlan); + if( zPlan ){ + sprintf(zPlan, "%s%s%s", + zWhere?zWhere:"", (zOrder&&zWhere)?" ":"", zOrder?zOrder:""); + } + sqliteFree(zWhere); sqliteFree(zOrder); - pIdxInfo->idxNum = 123; return SQLITE_OK; } diff --git a/src/vdbe.c b/src/vdbe.c index 26caf2edb0..22967d41f6 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -43,7 +43,7 @@ ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.556 2006/06/13 14:16:59 danielk1977 Exp $ +** $Id: vdbe.c,v 1.557 2006/06/13 15:00:55 danielk1977 Exp $ */ #include "sqliteInt.h" #include "os.h" @@ -4596,20 +4596,19 @@ case OP_VOpen: { ** P3 points to enough free space to use to marshall the arguments. ** ** This opcode invokes the xFilter method on the virtual table specified -** by P1. The index number parameter to xFilter is the top of the stack. +** by P1. The query plan parameter to xFilter is the top of the stack. ** Next down on the stack is the argc parameter. Beneath the ** next of stack are argc additional parameters which are passed to ** xFilter as argv. The topmost parameter (i.e. 3rd element popped from ** the stack) becomes argv[argc-1] when passed to xFilter. ** -** The index number, argc, and all argv stack values are popped from the +** The query plan, argc, and all argv stack values are popped from the ** stack before this instruction completes. ** ** A jump is made to P2 if the result set after filtering would be ** empty. */ case OP_VFilter: { - int iIndex; int nArg; const sqlite3_module *pModule; @@ -4620,21 +4619,20 @@ case OP_VFilter: { /* Grab the index number and argc parameters off the top of the stack. */ assert( (&pTos[-1])>=p->aStack ); - assert( pTos[0].flags==MEM_Int && pTos[-1].flags==MEM_Int ); - iIndex = pTos[0].i; + assert( pTos[0].flags&MEM_Blob && pTos[-1].flags==MEM_Int ); nArg = pTos[-1].i; /* Invoke the xFilter method if one is defined. */ if( pModule->xFilter ){ int res; int ii; - Mem **apArg = pOp->p3; + Mem **apArg = (Mem **)pOp->p3; for(ii = 0; iixFilter(pCur->pVtabCursor, iIndex, nArg, apArg); + res = pModule->xFilter(pCur->pVtabCursor, pTos->z, pTos->n, nArg, apArg); if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse; if( res==0 ){ diff --git a/src/vtab.c b/src/vtab.c index 8c8ba246d2..f717831ff4 100644 --- a/src/vtab.c +++ b/src/vtab.c @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code used to help implement virtual tables. ** -** $Id: vtab.c,v 1.6 2006/06/13 10:24:43 danielk1977 Exp $ +** $Id: vtab.c,v 1.7 2006/06/13 15:00:55 danielk1977 Exp $ */ #ifndef SQLITE_OMIT_VIRTUALTABLE #include "sqliteInt.h" @@ -277,6 +277,17 @@ int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ return rc; } +/* +** Resize pInfo->zPlan to nBytes bytes using realloc(). Set pInfo->nPlan +** to nBytes and return a pointer to the allocated memory. +*/ +char *sqlite3_allocate_queryplan(sqlite3_index_info *pInfo, int nBytes){ + pInfo->nPlan = nBytes; + sqlite3ReallocOrFree(&pInfo->zPlan, nBytes); + return pInfo->zPlan; +} + + /* ** This function is used to set the schema of a virtual table. It is only ** valid to call this function from within the xCreate() or xConnect() of a diff --git a/src/where.c b/src/where.c index 2a047d5c1a..99edb3f5c0 100644 --- a/src/where.c +++ b/src/where.c @@ -16,7 +16,7 @@ ** so is applicable. Because this module is responsible for selecting ** indices, you might also think of this module as the "query optimizer". ** -** $Id: where.c,v 1.213 2006/06/13 14:16:59 danielk1977 Exp $ +** $Id: where.c,v 1.214 2006/06/13 15:00:55 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -1005,7 +1005,8 @@ static double bestVirtualIndex( pIdxCons->usable = (pTerm->prereqRight & notReady)==0; } memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); - pIdxInfo->idxNum = 0; + pIdxInfo->zPlan = 0; + pIdxInfo->nPlan = 0; pIdxInfo->orderByConsumed = 0; pIdxInfo->estimatedCost = SQLITE_BIG_DBL; nOrderBy = pIdxInfo->nOrderBy; @@ -1468,6 +1469,9 @@ static void whereInfoFree(WhereInfo *pWInfo){ if( pWInfo ){ int i; for(i=0; inLevel; i++){ + if( pWInfo->a[i].pIdxInfo ){ + sqliteFree(pWInfo->a[i].pIdxInfo->zPlan); + } sqliteFree(pWInfo->a[i].pIdxInfo); } sqliteFree(pWInfo); @@ -1754,8 +1758,8 @@ WhereInfo *sqlite3WhereBegin( } #ifndef SQLITE_OMIT_VIRTUALTABLE else if( pLevel->pIdxInfo ){ - zMsg = sqlite3MPrintf("%z VIRTUAL TABLE INDEX %d", - pLevel->pIdxInfo->idxNum); + zMsg = sqlite3MPrintf("%z VIRTUAL TABLE INDEX %s", + pLevel->pIdxInfo->zPlan); } #endif if( pLevel->flags & WHERE_ORDERBY ){ @@ -1858,7 +1862,9 @@ WhereInfo *sqlite3WhereBegin( if( j==pIdxInfo->nConstraint ) break; } sqlite3VdbeAddOp(v, OP_Integer, i-1, 0); - sqlite3VdbeAddOp(v, OP_Integer, pIdxInfo->idxNum, 0); + sqlite3VdbeAddOp(v, OP_Blob, pIdxInfo->nPlan, 0); + sqlite3VdbeChangeP3(v, -1, pIdxInfo->zPlan, P3_DYNAMIC); + pIdxInfo->zPlan = 0; sqlite3VdbeAddOp(v, OP_VFilter, iCur, brk); zSpace = (char *)sqliteMalloc(sizeof(sqlite3_value*)*(i-1)); sqlite3VdbeChangeP3(v, -1, zSpace, P3_DYNAMIC); diff --git a/test/vtab1.test b/test/vtab1.test index 30242907dd..2c29418eb6 100644 --- a/test/vtab1.test +++ b/test/vtab1.test @@ -11,7 +11,7 @@ # This file implements regression tests for SQLite library. The # focus of this file is creating and dropping virtual tables. # -# $Id: vtab1.test,v 1.9 2006/06/13 14:16:59 danielk1977 Exp $ +# $Id: vtab1.test,v 1.10 2006/06/13 15:00:55 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -182,28 +182,28 @@ do_test vtab1-3.6 { SELECT * FROM t1; } set echo_module -} {xBestIndex {} {} xFilter 123} +} {xBestIndex {} {} xFilter {}} do_test vtab1-3.7 { set echo_module "" execsql { SELECT * FROM t1 WHERE b = 10; } set echo_module -} {xBestIndex {WHERE b = ?} {} xFilter 123 10} +} {xBestIndex {WHERE b = ?} {} xFilter {WHERE b = ?} 10} do_test vtab1-3.8 { set echo_module "" execsql { SELECT * FROM t1 WHERE b >= 5 AND b <= 10; } set echo_module -} {xBestIndex {WHERE b >= ? AND b <= ?} {} xFilter 123 5 10} +} {xBestIndex {WHERE b >= ? AND b <= ?} {} xFilter {WHERE b >= ? AND b <= ?} 5 10} do_test vtab1-3.9 { set echo_module "" execsql { SELECT * FROM t1 WHERE b BETWEEN 5 AND 10; } set echo_module -} {xBestIndex {WHERE b >= ? AND b <= ?} {} xFilter 123 5 10} +} {xBestIndex {WHERE b >= ? AND b <= ?} {} xFilter {WHERE b >= ? AND b <= ?} 5 10} finish_test -- 2.47.2