Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -1657,10 +1657,40 @@ ** Estimate the logarithm of the input value to base 2. */ static LogEst estLog(LogEst N){ return N<=10 ? 0 : sqlite3LogEst(N) - 33; } + +/* +** Convert OP_Column opcodes to OP_Copy in previously generated code. +** +** This routine runs over generated VDBE code and translates OP_Column +** opcodes into OP_Copy, and OP_Rowid into OP_Null, when the table is being +** accessed via co-routine instead of via table lookup. +*/ +static void translateColumnToCopy( + Vdbe *v, /* The VDBE containing code to translate */ + int iStart, /* Translate from this opcode to the end */ + int iTabCur, /* OP_Column/OP_Rowid references to this table */ + int iRegister /* The first column is in this register */ +){ + VdbeOp *pOp = sqlite3VdbeGetOp(v, iStart); + int iEnd = sqlite3VdbeCurrentAddr(v); + for(; iStartp1!=iTabCur ) continue; + if( pOp->opcode==OP_Column ){ + pOp->opcode = OP_Copy; + pOp->p1 = pOp->p2 + iRegister; + pOp->p2 = pOp->p3; + pOp->p3 = 0; + }else if( pOp->opcode==OP_Rowid ){ + pOp->opcode = OP_Null; + pOp->p1 = 0; + pOp->p3 = 0; + } + } +} /* ** Two routines for printing the content of an sqlite3_index_info ** structure. Used for testing and debugging only. If neither ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines @@ -1760,10 +1790,11 @@ Bitmask idxCols; /* Bitmap of columns used for indexing */ Bitmask extraCols; /* Bitmap of additional columns */ u8 sentWarning = 0; /* True if a warnning has been issued */ Expr *pPartial = 0; /* Partial Index Expression */ int iContinue = 0; /* Jump here to skip excluded rows */ + struct SrcList_item *pTabItem; /* FROM clause term being indexed */ /* Generate code to skip over the creation and initialization of the ** transient index on 2nd and subsequent iterations of the loop. */ v = pParse->pVdbe; assert( v!=0 ); @@ -1885,11 +1916,20 @@ sqlite3VdbeSetP4KeyInfo(pParse, pIdx); VdbeComment((v, "for %s", pTable->zName)); /* Fill the automatic index with content */ sqlite3ExprCachePush(pParse); - addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); + pTabItem = &pWC->pWInfo->pTabList->a[pLevel->iFrom]; + if( pTabItem->viaCoroutine ){ + int regYield = pTabItem->regReturn; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); + addrTop = sqlite3VdbeAddOp1(v, OP_Yield, regYield); + VdbeCoverage(v); + VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); + }else{ + addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v); + } if( pPartial ){ iContinue = sqlite3VdbeMakeLabel(v); sqlite3ExprIfFalse(pParse, pPartial, iContinue, SQLITE_JUMPIFNULL); pLoop->wsFlags |= WHERE_PARTIALIDX; } @@ -1896,11 +1936,17 @@ regRecord = sqlite3GetTempReg(pParse); sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0); sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); if( pPartial ) sqlite3VdbeResolveLabel(v, iContinue); - sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); + if( pTabItem->viaCoroutine ){ + translateColumnToCopy(v, addrTop, pLevel->iTabCur, pTabItem->regResult); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); + pTabItem->viaCoroutine = 0; + }else{ + sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v); + } sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); sqlite3VdbeJumpHere(v, addrTop); sqlite3ReleaseTempReg(pParse, regRecord); sqlite3ExprCachePop(pParse); @@ -5172,19 +5218,18 @@ rSize = pTab->nRowLogEst; rLogSize = estLog(rSize); #ifndef SQLITE_OMIT_AUTOMATIC_INDEX /* Automatic indexes */ - if( !pBuilder->pOrSet + if( !pBuilder->pOrSet /* Not part of an OR optimization */ && (pWInfo->wctrlFlags & WHERE_NO_AUTOINDEX)==0 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 - && pSrc->pIndex==0 - && !pSrc->viaCoroutine - && !pSrc->notIndexed - && HasRowid(pTab) - && !pSrc->isCorrelated - && !pSrc->isRecursive + && pSrc->pIndex==0 /* Has no INDEXED BY clause */ + && !pSrc->notIndexed /* Has no NOT INDEXED clause */ + && HasRowid(pTab) /* Is not a WITHOUT ROWID table. (FIXME: Why not?) */ + && !pSrc->isCorrelated /* Not a correlated subquery */ + && !pSrc->isRecursive /* Not a recursive common table expression. */ ){ /* Generate auto-index WhereLoops */ WhereTerm *pTerm; WhereTerm *pWCEnd = pWC->a + pWC->nTerm; for(pTerm=pWC->a; rc==SQLITE_OK && pTermpTab; assert( pTab!=0 ); pLoop = pLevel->pWLoop; /* For a co-routine, change all OP_Column references to the table of - ** the co-routine into OP_SCopy of result contained in a register. + ** the co-routine into OP_Copy of result contained in a register. ** OP_Rowid becomes OP_Null. */ if( pTabItem->viaCoroutine && !db->mallocFailed ){ - last = sqlite3VdbeCurrentAddr(v); - k = pLevel->addrBody; - pOp = sqlite3VdbeGetOp(v, k); - for(; kp1!=pLevel->iTabCur ) continue; - if( pOp->opcode==OP_Column ){ - pOp->opcode = OP_Copy; - pOp->p1 = pOp->p2 + pTabItem->regResult; - pOp->p2 = pOp->p3; - pOp->p3 = 0; - }else if( pOp->opcode==OP_Rowid ){ - pOp->opcode = OP_Null; - pOp->p1 = 0; - pOp->p3 = 0; - } - } + translateColumnToCopy(v, pLevel->addrBody, pLevel->iTabCur, + pTabItem->regResult); continue; } /* Close all of the cursors that were opened by sqlite3WhereBegin. ** Except, do not close cursors that will be reused by the OR optimization ADDED test/autoindex5.test Index: test/autoindex5.test ================================================================== --- /dev/null +++ test/autoindex5.test @@ -0,0 +1,108 @@ +# 2014-10-24 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#************************************************************************* +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing automatic index creation logic, +# and specifically ensuring that automatic indexes can be used with +# co-routine subqueries. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +# Schema is from the Debian security database +# +do_execsql_test autoindex5-1.0 { + CREATE TABLE source_package_status + (bug_name TEXT NOT NULL, + package INTEGER NOT NULL, + vulnerable INTEGER NOT NULL, + urgency TEXT NOT NULL, + PRIMARY KEY (bug_name, package)); + CREATE INDEX source_package_status_package + ON source_package_status(package); + + CREATE TABLE source_packages + (name TEXT NOT NULL, + release TEXT NOT NULL, + subrelease TEXT NOT NULL, + archive TEXT NOT NULL, + version TEXT NOT NULL, + version_id INTEGER NOT NULL DEFAULT 0, + PRIMARY KEY (name, release, subrelease, archive)); + + CREATE TABLE bugs + (name TEXT NOT NULL PRIMARY KEY, + cve_status TEXT NOT NULL + CHECK (cve_status IN + ('', 'CANDIDATE', 'ASSIGNED', 'RESERVED', 'REJECTED')), + not_for_us INTEGER NOT NULL CHECK (not_for_us IN (0, 1)), + description TEXT NOT NULL, + release_date TEXT NOT NULL, + source_file TEXT NOT NULL, + source_line INTEGER NOT NULL); + + CREATE TABLE package_notes + (id INTEGER NOT NULL PRIMARY KEY, + bug_name TEXT NOT NULL, + package TEXT NOT NULL, + fixed_version TEXT + CHECK (fixed_version IS NULL OR fixed_version <> ''), + fixed_version_id INTEGER NOT NULL DEFAULT 0, + release TEXT NOT NULL, + package_kind TEXT NOT NULL DEFAULT 'unknown', + urgency TEXT NOT NULL, + bug_origin TEXT NOT NULL DEFAULT ''); + CREATE INDEX package_notes_package + ON package_notes(package); + CREATE UNIQUE INDEX package_notes_bug + ON package_notes(bug_name, package, release); + + CREATE TABLE debian_bugs + (bug INTEGER NOT NULL, + note INTEGER NOT NULL, + PRIMARY KEY (bug, note)); + + + CREATE VIEW debian_cve AS + SELECT DISTINCT debian_bugs.bug, st.bug_name + FROM package_notes, debian_bugs, source_package_status AS st + WHERE package_notes.bug_name = st.bug_name + AND debian_bugs.note = package_notes.id + ORDER BY debian_bugs.bug; +} {} + +# The following query should use an automatic index for the view +# in FROM clause of the subquery of the second result column. +# +do_execsql_test autoindex5-1.1 { + EXPLAIN QUERY PLAN + SELECT + st.bug_name, + (SELECT ALL debian_cve.bug FROM debian_cve + WHERE debian_cve.bug_name = st.bug_name + ORDER BY debian_cve.bug), + sp.release + FROM + source_package_status AS st, + source_packages AS sp, + bugs + WHERE + sp.rowid = st.package + AND st.bug_name = bugs.name + AND ( st.bug_name LIKE 'CVE-%' OR st.bug_name LIKE 'TEMP-%' ) + AND ( sp.release = 'sid' OR sp.release = 'stretch' OR sp.release = 'jessie' + OR sp.release = 'wheezy' OR sp.release = 'squeeze' ) + ORDER BY sp.name, st.bug_name, sp.release, sp.subrelease; +} {/SEARCH SUBQUERY 2 USING AUTOMATIC COVERING INDEX .bug_name=/} + + +finish_test