Index: ext/session/session1.test ================================================================== --- ext/session/session1.test +++ ext/session/session1.test @@ -579,7 +579,26 @@ } do_iterator_test 11.2 * { UPDATE t1 SET b = 0.0; } { } + +reset_db +do_execsql_test 12.1 { + CREATE TABLE t1(r INTEGER PRIMARY KEY, a, b); + CREATE INDEX i1 ON t1(a); + INSERT INTO t1 VALUES(1, 1, 1); + INSERT INTO t1 VALUES(2, 1, 2); + INSERT INTO t1 VALUES(3, 1, 3); +} + +do_iterator_test 12.2 * { + UPDATE t1 SET b='one' WHERE a=1; +} { + {UPDATE t1 0 X.. {i 1 {} {} i 1} {{} {} {} {} t one}} + {UPDATE t1 0 X.. {i 2 {} {} i 2} {{} {} {} {} t one}} + {UPDATE t1 0 X.. {i 3 {} {} i 3} {{} {} {} {} t one}} +} + + finish_test Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -2590,11 +2590,11 @@ #define WHERE_DISTINCTBY 0x0080 /* pOrderby is really a DISTINCT clause */ #define WHERE_WANT_DISTINCT 0x0100 /* All output needs to be distinct */ #define WHERE_SORTBYGROUP 0x0200 /* Support sqlite3WhereIsSorted() */ #define WHERE_SEEK_TABLE 0x0400 /* Do not defer seeks on main table */ #define WHERE_ORDERBY_LIMIT 0x0800 /* ORDERBY+LIMIT on the inner loop */ - /* 0x1000 not currently used */ +#define WHERE_SEEK_UNIQ_TABLE 0x1000 /* Do not defer seeks if unique */ /* 0x2000 not currently used */ #define WHERE_USE_LIMIT 0x4000 /* Use the LIMIT in cost estimates */ /* 0x8000 not currently used */ /* Allowed return values from sqlite3WhereIsDistinct() Index: src/update.c ================================================================== --- src/update.c +++ src/update.c @@ -390,11 +390,11 @@ ** Do not consider a single-pass strategy for a multi-row update if ** there are any triggers or foreign keys to process, or rows may ** be deleted as a result of REPLACE conflict handling. Any of these ** things might disturb a cursor being used to scan through the table ** or index, causing a single-pass approach to malfunction. */ - flags = WHERE_ONEPASS_DESIRED | WHERE_SEEK_TABLE; + flags = WHERE_ONEPASS_DESIRED|WHERE_SEEK_UNIQ_TABLE; if( !pParse->nested && !pTrigger && !hasFK && !chngKey && !bReplace ){ flags |= WHERE_ONEPASS_MULTIROW; } pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, flags, iIdxCur); if( pWInfo==0 ) goto update_cleanup; Index: src/wherecode.c ================================================================== --- src/wherecode.c +++ src/wherecode.c @@ -1589,11 +1589,14 @@ /* Seek the table cursor, if required */ if( omitTable ){ /* pIdx is a covering index. No need to access the main table. */ }else if( HasRowid(pIdx->pTable) ){ - if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE)!=0 ){ + if( (pWInfo->wctrlFlags & WHERE_SEEK_TABLE) || ( + (pWInfo->wctrlFlags & WHERE_SEEK_UNIQ_TABLE) + && (pWInfo->eOnePass==ONEPASS_SINGLE) + )){ iRowidReg = ++pParse->nMem; sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg); VdbeCoverage(v); Index: test/update2.test ================================================================== --- test/update2.test +++ test/update2.test @@ -173,8 +173,33 @@ 5 a 15 5 6 a 16 6 7 a 17 7 } } + +#------------------------------------------------------------------------- +# +do_execsql_test 5.0 { + CREATE TABLE x1(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX x1c ON x1(b, c); + INSERT INTO x1 VALUES(1, 'a', 1); + INSERT INTO x1 VALUES(2, 'a', 2); + INSERT INTO x1 VALUES(3, 'a', 3); +} + +do_execsql_test 5.1.1 { + UPDATE x1 SET c=c+1 WHERE b='a'; +} + +do_execsql_test 5.1.2 { + SELECT * FROM x1; +} {1 a 2 2 a 3 3 a 4} + +do_test 5.2 { + catch { array unset A } + db eval { EXPLAIN UPDATE x1 SET c=c+1 WHERE b='a' } { incr A($opcode) } + set A(NotExists) +} {1} + finish_test