Index: src/delete.c ================================================================== --- src/delete.c +++ src/delete.c @@ -371,11 +371,11 @@ sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); } }else #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ { - u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK; + u16 wcf = WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK|WHERE_SEEK_TABLE; if( sNC.ncFlags & NC_VarSelect ) bComplex = 1; wcf |= (bComplex ? 0 : WHERE_ONEPASS_MULTIROW); if( HasRowid(pTab) ){ /* For a rowid table, initialize the RowSet to an empty set */ pPk = 0; Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -2541,10 +2541,11 @@ #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */ #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */ #define WHERE_REOPEN_IDX 0x1000 /* Try to use OP_ReopenIdx */ #define WHERE_ONEPASS_MULTIROW 0x2000 /* ONEPASS is ok with multiple rows */ #define WHERE_USE_LIMIT 0x4000 /* There is a constant LIMIT clause */ +#define WHERE_SEEK_TABLE 0x8000 /* Always seek the main table */ /* Allowed return values from sqlite3WhereIsDistinct() */ #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */ #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */ Index: src/wherecode.c ================================================================== --- src/wherecode.c +++ src/wherecode.c @@ -1322,11 +1322,13 @@ disableTerm(pLevel, pRangeStart); disableTerm(pLevel, pRangeEnd); if( omitTable ){ /* pIdx is a covering index. No need to access the main table. */ }else if( HasRowid(pIdx->pTable) ){ - if( pWInfo->eOnePass!=ONEPASS_OFF ){ + if( pWInfo->eOnePass!=ONEPASS_OFF + || (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 + ){ iRowidReg = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg); VdbeCoverage(v); @@ -1518,11 +1520,12 @@ ** sub-WHERE clause is to to invoke the main loop body as a subroutine. */ wctrlFlags = WHERE_OMIT_OPEN_CLOSE | WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY - | WHERE_NO_AUTOINDEX; + | WHERE_NO_AUTOINDEX + | (pWInfo->wctrlFlags & WHERE_SEEK_TABLE); for(ii=0; iinTerm; ii++){ WhereTerm *pOrTerm = &pOrWc->a[ii]; if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ WhereInfo *pSubWInfo; /* Info for single OR-term scan */ Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ Index: test/intpkey.test ================================================================== --- test/intpkey.test +++ test/intpkey.test @@ -612,8 +612,28 @@ } {} do_execsql_test intpkey-16.1 { PRAGMA table_info=t16a; } {0 id INTEGER 0 {} 1 1 b TEXT 0 {} 0 2 c INT 0 {} 0} - +# 2016-05-06 ticket https://www.sqlite.org/src/tktview/16c9801ceba4923939085 +# When the schema contains an index on the IPK and no other index +# and a WHERE clause on a delete uses an OR where both sides referencing +# the IPK, then it is possible that the OP_Delete will fail because there +# deferred seek of the OP_Seek is not resolved prior to reaching the OP_Delete. +# +do_execsql_test intpkey-17.0 { + CREATE TABLE t17(x INTEGER PRIMARY KEY, y TEXT); + INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); + CREATE INDEX t17x ON t17(x); + DELETE FROM t17 WHERE x=99 OR x<130; + SELECT * FROM t17; +} {248 giraffe} +do_execsql_test intpkey-17.1 { + DROP INDEX t17x; + DELETE FROM t17; + INSERT INTO t17(x,y) VALUES(123,'elephant'),(248,'giraffe'); + CREATE INDEX t17x ON t17(abs(x)); + DELETE FROM t17 WHERE abs(x) IS NULL OR abs(x)<130; + SELECT * FROM t17; +} {248 giraffe} finish_test