/ Check-in [a7268769]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge in performance enhancements for INSERT operations, especially INSERTs on tables that have no affinity columns or that have many indices or INSERTs with content coming from a SELECT. Add the SQLITE_TESTCTRL_VDBE_COVERAGE test control and the SQLITE_VDBE_COVERAGE compile-time option used for measure coverage of branches in VDBE programs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a72687699ba2af2e7383be7371d4121750c7e34f
User & Date: drh 2014-02-17 23:52:13
References
2014-05-23
10:15 New ticket [e9654505] Inifinite loop in INSERT statement.. artifact: cc3683f6 user: drh
Context
2014-02-18
01:07
Improvements to "NOT IN (SELECT ...)" processing. Only test for NULL values on the RHS on the first iteration, then remember the result. There has been logic to do this for year, but it didn't work right and ended up repeating the NULL test on every iteration. This inefficiency was found using the VDBE coverage testing tools. check-in: 915f6f1c user: drh tags: trunk
2014-02-17
23:52
Merge in performance enhancements for INSERT operations, especially INSERTs on tables that have no affinity columns or that have many indices or INSERTs with content coming from a SELECT. Add the SQLITE_TESTCTRL_VDBE_COVERAGE test control and the SQLITE_VDBE_COVERAGE compile-time option used for measure coverage of branches in VDBE programs. check-in: a7268769 user: drh tags: trunk
22:40
Add logic to do test coverage measurements on the VDBE code. Closed-Leaf check-in: ce184c7b user: drh tags: insert-optimization
01:13
Fix the VDBE_PROFILE logic. Add a script to process the output file. check-in: 7adb3da2 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/alter.c.

   600    600     if( ALWAYS(v) ){
   601    601       int r1 = sqlite3GetTempReg(pParse);
   602    602       int r2 = sqlite3GetTempReg(pParse);
   603    603       int j1;
   604    604       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
   605    605       sqlite3VdbeUsesBtree(v, iDb);
   606    606       sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
   607         -    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
          607  +    j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1); VdbeCoverage(v);
   608    608       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
   609    609       sqlite3VdbeJumpHere(v, j1);
   610    610       sqlite3ReleaseTempReg(pParse, r1);
   611    611       sqlite3ReleaseTempReg(pParse, r2);
   612    612     }
   613    613   }
   614    614   

Changes to src/analyze.c.

  1073   1073       **   Rewind csr
  1074   1074       **   if eof(csr) goto end_of_scan;
  1075   1075       **   regChng = 0
  1076   1076       **   goto next_push_0;
  1077   1077       **
  1078   1078       */
  1079   1079       addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
         1080  +    VdbeCoverage(v);
  1080   1081       sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
  1081   1082       addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
  1082   1083   
  1083   1084       /*
  1084   1085       **  next_row:
  1085   1086       **   regChng = 0
  1086   1087       **   if( idx(0) != regPrev(0) ) goto chng_addr_0
................................................................................
  1094   1095       for(i=0; i<nCol; i++){
  1095   1096         char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
  1096   1097         sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
  1097   1098         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
  1098   1099         aGotoChng[i] = 
  1099   1100         sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
  1100   1101         sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
         1102  +      VdbeCoverage(v);
  1101   1103       }
  1102   1104       sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
  1103   1105       aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
  1104   1106   
  1105   1107       /*
  1106   1108       **  chng_addr_0:
  1107   1109       **   regPrev(0) = idx(0)
................................................................................
  1140   1142         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
  1141   1143       }
  1142   1144   #endif
  1143   1145       assert( regChng==(regStat4+1) );
  1144   1146       sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
  1145   1147       sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
  1146   1148       sqlite3VdbeChangeP5(v, 2+IsStat34);
  1147         -    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
         1149  +    sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
  1148   1150   
  1149   1151       /* Add the entry to the stat1 table. */
  1150   1152       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
  1151   1153       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
  1152   1154       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1153   1155       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1154   1156       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
................................................................................
  1167   1169         u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
  1168   1170   
  1169   1171         pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
  1170   1172   
  1171   1173         addrNext = sqlite3VdbeCurrentAddr(v);
  1172   1174         callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
  1173   1175         addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
         1176  +      VdbeCoverage(v);
  1174   1177         callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
  1175   1178         callStatGet(v, regStat4, STAT_GET_NLT, regLt);
  1176   1179         callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
  1177   1180         sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
         1181  +      VdbeCoverage(v);
  1178   1182   #ifdef SQLITE_ENABLE_STAT3
  1179   1183         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, 
  1180   1184                                         pIdx->aiColumn[0], regSample);
  1181   1185   #else
  1182   1186         for(i=0; i<nCol; i++){
  1183   1187           i16 iCol = pIdx->aiColumn[i];
  1184   1188           sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
  1185   1189         }
  1186   1190         sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
  1187   1191   #endif
  1188         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
         1192  +      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
  1189   1193         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
  1190   1194         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
  1191   1195         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
  1192   1196         sqlite3VdbeJumpHere(v, addrIsNull);
  1193   1197       }
  1194   1198   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1195   1199   
................................................................................
  1201   1205   
  1202   1206     /* Create a single sqlite_stat1 entry containing NULL as the index
  1203   1207     ** name and the row count as the content.
  1204   1208     */
  1205   1209     if( pOnlyIdx==0 && needTableCnt ){
  1206   1210       VdbeComment((v, "%s", pTab->zName));
  1207   1211       sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
  1208         -    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
         1212  +    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
  1209   1213       sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
  1210   1214       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
  1211   1215       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1212   1216       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1213   1217       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1214   1218       sqlite3VdbeJumpHere(v, jZeroRows);
  1215   1219     }

Changes to src/build.c.

   944    944       ** set them now.
   945    945       */
   946    946       reg1 = pParse->regRowid = ++pParse->nMem;
   947    947       reg2 = pParse->regRoot = ++pParse->nMem;
   948    948       reg3 = ++pParse->nMem;
   949    949       sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
   950    950       sqlite3VdbeUsesBtree(v, iDb);
   951         -    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
          951  +    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
   952    952       fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
   953    953                     1 : SQLITE_MAX_FILE_FORMAT;
   954    954       sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
   955    955       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
   956    956       sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
   957    957       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
   958    958       sqlite3VdbeJumpHere(v, j1);
................................................................................
  2671   2671     iSorter = pParse->nTab++;
  2672   2672     sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)
  2673   2673                       sqlite3KeyInfoRef(pKey), P4_KEYINFO);
  2674   2674   
  2675   2675     /* Open the table. Loop through all rows of the table, inserting index
  2676   2676     ** records into the sorter. */
  2677   2677     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2678         -  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
         2678  +  addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
  2679   2679     regRecord = sqlite3GetTempReg(pParse);
  2680   2680   
  2681   2681     sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
  2682   2682     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
  2683   2683     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
  2684         -  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
         2684  +  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
  2685   2685     sqlite3VdbeJumpHere(v, addr1);
  2686   2686     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
  2687   2687     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2688   2688                       (char *)pKey, P4_KEYINFO);
  2689   2689     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
  2690   2690   
  2691         -  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
         2691  +  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
  2692   2692     assert( pKey!=0 || db->mallocFailed || pParse->nErr );
  2693   2693     if( pIndex->onError!=OE_None && pKey!=0 ){
  2694   2694       int j2 = sqlite3VdbeCurrentAddr(v) + 3;
  2695   2695       sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
  2696   2696       addr2 = sqlite3VdbeCurrentAddr(v);
  2697   2697       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
  2698         -                         pKey->nField - pIndex->nKeyCol);
         2698  +                         pKey->nField - pIndex->nKeyCol); VdbeCoverage(v);
  2699   2699       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  2700   2700     }else{
  2701   2701       addr2 = sqlite3VdbeCurrentAddr(v);
  2702   2702     }
  2703   2703     sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
  2704   2704     sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
  2705   2705     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  2706   2706     sqlite3ReleaseTempReg(pParse, regRecord);
  2707         -  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
         2707  +  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
  2708   2708     sqlite3VdbeJumpHere(v, addr1);
  2709   2709   
  2710   2710     sqlite3VdbeAddOp1(v, OP_Close, iTab);
  2711   2711     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
  2712   2712     sqlite3VdbeAddOp1(v, OP_Close, iSorter);
  2713   2713   }
  2714   2714   

Changes to src/delete.c.

   439    439         if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
   440    440         addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
   441    441       }else if( pPk ){
   442    442         /* Construct a composite key for the row to be deleted and remember it */
   443    443         iKey = ++pParse->nMem;
   444    444         nKey = 0;   /* Zero tells OP_Found to use a composite key */
   445    445         sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
   446         -                        sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
          446  +                        sqlite3IndexAffinityStr(v, pPk), nPk);
   447    447         sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
   448    448       }else{
   449    449         /* Get the rowid of the row to be deleted and remember it in the RowSet */
   450    450         nKey = 1;  /* OP_Seek always uses a single rowid */
   451    451         sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
   452    452       }
   453    453     
................................................................................
   477    477       */
   478    478       if( okOnePass ){
   479    479         /* Just one row.  Hence the top-of-loop is a no-op */
   480    480         assert( nKey==nPk ); /* OP_Found will use an unpacked key */
   481    481         if( aToOpen[iDataCur-iTabCur] ){
   482    482           assert( pPk!=0 );
   483    483           sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
          484  +        VdbeCoverage(v);
   484    485         }
   485    486       }else if( pPk ){
   486         -      addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur);
          487  +      addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
   487    488         sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
   488    489         assert( nKey==0 );  /* OP_Found will use a composite key */
   489    490       }else{
   490    491         addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
          492  +      VdbeCoverage(v);
   491    493         assert( nKey==1 );
   492    494       }  
   493    495     
   494    496       /* Delete the row */
   495    497   #ifndef SQLITE_OMIT_VIRTUALTABLE
   496    498       if( IsVirtual(pTab) ){
   497    499         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
................................................................................
   507    509                                  iKey, nKey, count, OE_Default, okOnePass);
   508    510       }
   509    511     
   510    512       /* End of the loop over all rowids/primary-keys. */
   511    513       if( okOnePass ){
   512    514         sqlite3VdbeResolveLabel(v, addrBypass);
   513    515       }else if( pPk ){
   514         -      sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1);
          516  +      sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
   515    517         sqlite3VdbeJumpHere(v, addrLoop);
   516    518       }else{
   517    519         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
   518    520         sqlite3VdbeJumpHere(v, addrLoop);
   519    521       }     
   520    522     
   521    523       /* Close the cursors open on the table and its indexes. */
................................................................................
   605    607                            iDataCur, iIdxCur, iPk, (int)nPk));
   606    608   
   607    609     /* Seek cursor iCur to the row to delete. If this row no longer exists 
   608    610     ** (this can happen if a trigger program has already deleted it), do
   609    611     ** not attempt to delete it or fire any DELETE triggers.  */
   610    612     iLabel = sqlite3VdbeMakeLabel(v);
   611    613     opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
   612         -  if( !bNoSeek ) sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
          614  +  if( !bNoSeek ){
          615  +    sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
          616  +    VdbeCoverage(v);
          617  +  }
   613    618    
   614    619     /* If there are any triggers to fire, allocate a range of registers to
   615    620     ** use for the old.* references in the triggers.  */
   616    621     if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
   617    622       u32 mask;                     /* Mask of OLD.* columns in use */
   618    623       int iCol;                     /* Iterator used while populating OLD.* */
   619    624       int addrStart;                /* Start of BEFORE trigger programs */
................................................................................
   647    652       /* If any BEFORE triggers were coded, then seek the cursor to the 
   648    653       ** row to be deleted again. It may be that the BEFORE triggers moved
   649    654       ** the cursor or of already deleted the row that the cursor was
   650    655       ** pointing to.
   651    656       */
   652    657       if( addrStart<sqlite3VdbeCurrentAddr(v) ){
   653    658         sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
          659  +      VdbeCoverage(v);
   654    660       }
   655    661   
   656    662       /* Do FK processing. This call checks that any FK constraints that
   657    663       ** refer to this table (i.e. constraints attached to other tables) 
   658    664       ** are not violated by deleting this row.  */
   659    665       sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
   660    666     }

Changes to src/expr.c.

  1379   1379   void sqlite3ExprCodeIsNullJump(
  1380   1380     Vdbe *v,            /* The VDBE under construction */
  1381   1381     const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
  1382   1382     int iReg,           /* Test the value in this register for NULL */
  1383   1383     int iDest           /* Jump here if the value is null */
  1384   1384   ){
  1385   1385     if( sqlite3ExprCanBeNull(pExpr) ){
  1386         -    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
         1386  +    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest); VdbeCoverage(v);
  1387   1387     }
  1388   1388   }
  1389   1389   
  1390   1390   /*
  1391   1391   ** Return TRUE if the given expression is a constant which would be
  1392   1392   ** unchanged by OP_Affinity with the affinity given in the second
  1393   1393   ** argument.
................................................................................
  1482   1482   
  1483   1483   /*
  1484   1484   ** Code an OP_Once instruction and allocate space for its flag. Return the 
  1485   1485   ** address of the new instruction.
  1486   1486   */
  1487   1487   int sqlite3CodeOnce(Parse *pParse){
  1488   1488     Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
  1489         -  return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
         1489  +  int addr = sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
         1490  +  VdbeCoverage(v);
         1491  +  return addr;
  1490   1492   }
  1491   1493   
  1492   1494   /*
  1493   1495   ** This function is used by the implementation of the IN (...) operator.
  1494   1496   ** The pX parameter is the expression on the RHS of the IN operator, which
  1495   1497   ** might be either a list of expressions or a subquery.
  1496   1498   **
................................................................................
  1833   1835             if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
  1834   1836               sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
  1835   1837             }else{
  1836   1838               r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
  1837   1839               if( isRowid ){
  1838   1840                 sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
  1839   1841                                   sqlite3VdbeCurrentAddr(v)+2);
         1842  +              VdbeCoverage(v);
  1840   1843                 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
  1841   1844               }else{
  1842   1845                 sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
  1843   1846                 sqlite3ExprCacheAffinityChange(pParse, r3, 1);
  1844   1847                 sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
  1845   1848               }
  1846   1849             }
................................................................................
  1956   1959   
  1957   1960     /* If the LHS is NULL, then the result is either false or NULL depending
  1958   1961     ** on whether the RHS is empty or not, respectively.
  1959   1962     */
  1960   1963     if( destIfNull==destIfFalse ){
  1961   1964       /* Shortcut for the common case where the false and NULL outcomes are
  1962   1965       ** the same. */
  1963         -    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
         1966  +    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
  1964   1967     }else{
  1965         -    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
         1968  +    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
  1966   1969       sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
         1970  +    VdbeCoverage(v);
  1967   1971       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
  1968   1972       sqlite3VdbeJumpHere(v, addr1);
  1969   1973     }
  1970   1974   
  1971   1975     if( eType==IN_INDEX_ROWID ){
  1972   1976       /* In this case, the RHS is the ROWID of table b-tree
  1973   1977       */
  1974         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
         1978  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
  1975   1979       sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
         1980  +    VdbeCoverage(v);
  1976   1981     }else{
  1977   1982       /* In this case, the RHS is an index b-tree.
  1978   1983       */
  1979   1984       sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
  1980   1985   
  1981   1986       /* If the set membership test fails, then the result of the 
  1982   1987       ** "x IN (...)" expression must be either 0 or NULL. If the set
................................................................................
  1989   1994         ** cannot contain NULL values. This happens as the result
  1990   1995         ** of a "NOT NULL" constraint in the database schema.
  1991   1996         **
  1992   1997         ** Also run this branch if NULL is equivalent to FALSE
  1993   1998         ** for this particular IN operator.
  1994   1999         */
  1995   2000         sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
  1996         -
         2001  +      VdbeCoverage(v);
  1997   2002       }else{
  1998   2003         /* In this branch, the RHS of the IN might contain a NULL and
  1999   2004         ** the presence of a NULL on the RHS makes a difference in the
  2000   2005         ** outcome.
  2001   2006         */
  2002   2007         int j1, j2, j3;
  2003   2008   
  2004   2009         /* First check to see if the LHS is contained in the RHS.  If so,
  2005   2010         ** then the presence of NULLs in the RHS does not matter, so jump
  2006   2011         ** over all of the code that follows.
  2007   2012         */
  2008   2013         j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
         2014  +      VdbeCoverage(v);
  2009   2015   
  2010   2016         /* Here we begin generating code that runs if the LHS is not
  2011   2017         ** contained within the RHS.  Generate additional code that
  2012   2018         ** tests the RHS for NULLs.  If the RHS contains a NULL then
  2013   2019         ** jump to destIfNull.  If there are no NULLs in the RHS then
  2014   2020         ** jump to destIfFalse.
  2015   2021         */
  2016         -      j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
         2022  +      j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull); VdbeCoverage(v);
  2017   2023         j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
         2024  +      VdbeCoverage(v);
  2018   2025         sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
  2019   2026         sqlite3VdbeJumpHere(v, j3);
  2020   2027         sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
  2021   2028         sqlite3VdbeJumpHere(v, j2);
  2022   2029   
  2023   2030         /* Jump to the appropriate target depending on whether or not
  2024   2031         ** the RHS contains a NULL
  2025   2032         */
  2026         -      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
         2033  +      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
  2027   2034         sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
  2028   2035   
  2029   2036         /* The OP_Found at the top of this branch jumps here when true, 
  2030   2037         ** causing the overall IN expression evaluation to fall through.
  2031   2038         */
  2032   2039         sqlite3VdbeJumpHere(v, j1);
  2033   2040       }
................................................................................
  2556   2563         testcase( op==TK_GT );
  2557   2564         testcase( op==TK_GE );
  2558   2565         testcase( op==TK_EQ );
  2559   2566         testcase( op==TK_NE );
  2560   2567         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  2561   2568         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  2562   2569         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
  2563         -                  r1, r2, inReg, SQLITE_STOREP2);
         2570  +                  r1, r2, inReg, SQLITE_STOREP2); VdbeCoverage(v);
  2564   2571         testcase( regFree1==0 );
  2565   2572         testcase( regFree2==0 );
  2566   2573         break;
  2567   2574       }
  2568   2575       case TK_IS:
  2569   2576       case TK_ISNOT: {
  2570   2577         testcase( op==TK_IS );
  2571   2578         testcase( op==TK_ISNOT );
  2572   2579         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  2573   2580         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  2574   2581         op = (op==TK_IS) ? TK_EQ : TK_NE;
  2575   2582         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
  2576   2583                     r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
         2584  +      VdbeCoverage(v);
  2577   2585         testcase( regFree1==0 );
  2578   2586         testcase( regFree2==0 );
  2579   2587         break;
  2580   2588       }
  2581   2589       case TK_AND:
  2582   2590       case TK_OR:
  2583   2591       case TK_PLUS:
................................................................................
  2659   2667         assert( TK_ISNULL==OP_IsNull );
  2660   2668         assert( TK_NOTNULL==OP_NotNull );
  2661   2669         testcase( op==TK_ISNULL );
  2662   2670         testcase( op==TK_NOTNULL );
  2663   2671         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
  2664   2672         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  2665   2673         testcase( regFree1==0 );
  2666         -      addr = sqlite3VdbeAddOp1(v, op, r1);
         2674  +      addr = sqlite3VdbeAddOp1(v, op, r1); VdbeCoverage(v);
  2667   2675         sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
  2668   2676         sqlite3VdbeJumpHere(v, addr);
  2669   2677         break;
  2670   2678       }
  2671   2679       case TK_AGG_FUNCTION: {
  2672   2680         AggInfo *pInfo = pExpr->pAggInfo;
  2673   2681         if( pInfo==0 ){
................................................................................
  2711   2719         */
  2712   2720         if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
  2713   2721           int endCoalesce = sqlite3VdbeMakeLabel(v);
  2714   2722           assert( nFarg>=2 );
  2715   2723           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
  2716   2724           for(i=1; i<nFarg; i++){
  2717   2725             sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
         2726  +          VdbeCoverage(v);
  2718   2727             sqlite3ExprCacheRemove(pParse, target, 1);
  2719   2728             sqlite3ExprCachePush(pParse);
  2720   2729             sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
  2721   2730             sqlite3ExprCachePop(pParse, 1);
  2722   2731           }
  2723   2732           sqlite3VdbeResolveLabel(v, endCoalesce);
  2724   2733           break;
................................................................................
  2849   2858         r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
  2850   2859         testcase( regFree1==0 );
  2851   2860         testcase( regFree2==0 );
  2852   2861         r3 = sqlite3GetTempReg(pParse);
  2853   2862         r4 = sqlite3GetTempReg(pParse);
  2854   2863         codeCompare(pParse, pLeft, pRight, OP_Ge,
  2855   2864                     r1, r2, r3, SQLITE_STOREP2);
         2865  +      VdbeCoverage(v);
  2856   2866         pLItem++;
  2857   2867         pRight = pLItem->pExpr;
  2858   2868         sqlite3ReleaseTempReg(pParse, regFree2);
  2859   2869         r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
  2860   2870         testcase( regFree2==0 );
  2861   2871         codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
         2872  +      VdbeCoverage(v);
  2862   2873         sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
  2863   2874         sqlite3ReleaseTempReg(pParse, r3);
  2864   2875         sqlite3ReleaseTempReg(pParse, r4);
  2865   2876         break;
  2866   2877       }
  2867   2878       case TK_COLLATE: 
  2868   2879       case TK_UPLUS: {
................................................................................
  3021   3032         if( pExpr->affinity==OE_Abort ){
  3022   3033           sqlite3MayAbort(pParse);
  3023   3034         }
  3024   3035         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  3025   3036         if( pExpr->affinity==OE_Ignore ){
  3026   3037           sqlite3VdbeAddOp4(
  3027   3038               v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
         3039  +        VdbeCoverage(v);
  3028   3040         }else{
  3029   3041           sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
  3030   3042                                 pExpr->affinity, pExpr->u.zToken, 0, 0);
  3031   3043         }
  3032   3044   
  3033   3045         break;
  3034   3046       }
................................................................................
  3108   3120   }
  3109   3121   
  3110   3122   /*
  3111   3123   ** Generate code that will evaluate expression pExpr and store the
  3112   3124   ** results in register target.  The results are guaranteed to appear
  3113   3125   ** in register target.
  3114   3126   */
  3115         -int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
         3127  +void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
  3116   3128     int inReg;
  3117   3129   
  3118   3130     assert( target>0 && target<=pParse->nMem );
  3119   3131     if( pExpr && pExpr->op==TK_REGISTER ){
  3120   3132       sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
  3121   3133     }else{
  3122   3134       inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
  3123   3135       assert( pParse->pVdbe || pParse->db->mallocFailed );
  3124   3136       if( inReg!=target && pParse->pVdbe ){
  3125   3137         sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
  3126   3138       }
  3127   3139     }
  3128         -  return target;
         3140  +}
         3141  +
         3142  +/*
         3143  +** Generate code that will evaluate expression pExpr and store the
         3144  +** results in register target.  The results are guaranteed to appear
         3145  +** in register target.  If the expression is constant, then this routine
         3146  +** might choose to code the expression at initialization time.
         3147  +*/
         3148  +void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
         3149  +  if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
         3150  +    sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
         3151  +  }else{
         3152  +    sqlite3ExprCode(pParse, pExpr, target);
         3153  +  }
  3129   3154   }
  3130   3155   
  3131   3156   /*
  3132   3157   ** Generate code that evalutes the given expression and puts the result
  3133   3158   ** in register target.
  3134   3159   **
  3135   3160   ** Also make a copy of the expression results into another "cache" register
................................................................................
  3136   3161   ** and modify the expression so that the next time it is evaluated,
  3137   3162   ** the result is a copy of the cache register.
  3138   3163   **
  3139   3164   ** This routine is used for expressions that are used multiple 
  3140   3165   ** times.  They are evaluated once and the results of the expression
  3141   3166   ** are reused.
  3142   3167   */
  3143         -int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
         3168  +void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
  3144   3169     Vdbe *v = pParse->pVdbe;
  3145         -  int inReg;
  3146         -  inReg = sqlite3ExprCode(pParse, pExpr, target);
         3170  +  int iMem;
         3171  +
  3147   3172     assert( target>0 );
  3148         -  /* The only place, other than this routine, where expressions can be
  3149         -  ** converted to TK_REGISTER is internal subexpressions in BETWEEN and
  3150         -  ** CASE operators.  Neither ever calls this routine.  And this routine
  3151         -  ** is never called twice on the same expression.  Hence it is impossible
  3152         -  ** for the input to this routine to already be a register.  Nevertheless,
  3153         -  ** it seems prudent to keep the ALWAYS() in case the conditions above
  3154         -  ** change with future modifications or enhancements. */
  3155         -  if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
  3156         -    int iMem;
  3157         -    iMem = ++pParse->nMem;
  3158         -    sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
  3159         -    exprToRegister(pExpr, iMem);
  3160         -  }
  3161         -  return inReg;
         3173  +  assert( pExpr->op!=TK_REGISTER );
         3174  +  sqlite3ExprCode(pParse, pExpr, target);
         3175  +  iMem = ++pParse->nMem;
         3176  +  sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
         3177  +  exprToRegister(pExpr, iMem);
  3162   3178   }
  3163   3179   
  3164   3180   #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
  3165   3181   /*
  3166   3182   ** Generate a human-readable explanation of an expression tree.
  3167   3183   */
  3168   3184   void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
................................................................................
  3605   3621         testcase( op==TK_GE );
  3606   3622         testcase( op==TK_EQ );
  3607   3623         testcase( op==TK_NE );
  3608   3624         testcase( jumpIfNull==0 );
  3609   3625         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3610   3626         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  3611   3627         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
  3612         -                  r1, r2, dest, jumpIfNull);
         3628  +                  r1, r2, dest, jumpIfNull);  VdbeCoverage(v);
  3613   3629         testcase( regFree1==0 );
  3614   3630         testcase( regFree2==0 );
  3615   3631         break;
  3616   3632       }
  3617   3633       case TK_IS:
  3618   3634       case TK_ISNOT: {
  3619   3635         testcase( op==TK_IS );
  3620   3636         testcase( op==TK_ISNOT );
  3621   3637         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3622   3638         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  3623   3639         op = (op==TK_IS) ? TK_EQ : TK_NE;
  3624   3640         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
  3625         -                  r1, r2, dest, SQLITE_NULLEQ);
         3641  +                  r1, r2, dest, SQLITE_NULLEQ);  VdbeCoverage(v);
  3626   3642         testcase( regFree1==0 );
  3627   3643         testcase( regFree2==0 );
  3628   3644         break;
  3629   3645       }
  3630   3646       case TK_ISNULL:
  3631   3647       case TK_NOTNULL: {
  3632   3648         assert( TK_ISNULL==OP_IsNull );
  3633   3649         assert( TK_NOTNULL==OP_NotNull );
  3634   3650         testcase( op==TK_ISNULL );
  3635   3651         testcase( op==TK_NOTNULL );
  3636   3652         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3637         -      sqlite3VdbeAddOp2(v, op, r1, dest);
         3653  +      sqlite3VdbeAddOp2(v, op, r1, dest); VdbeCoverage(v);
  3638   3654         testcase( regFree1==0 );
  3639   3655         break;
  3640   3656       }
  3641   3657       case TK_BETWEEN: {
  3642   3658         testcase( jumpIfNull==0 );
  3643   3659         exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
  3644   3660         break;
................................................................................
  3657   3673         if( exprAlwaysTrue(pExpr) ){
  3658   3674           sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
  3659   3675         }else if( exprAlwaysFalse(pExpr) ){
  3660   3676           /* No-op */
  3661   3677         }else{
  3662   3678           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  3663   3679           sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
         3680  +        VdbeCoverage(v);
  3664   3681           testcase( regFree1==0 );
  3665   3682           testcase( jumpIfNull==0 );
  3666   3683         }
  3667   3684         break;
  3668   3685       }
  3669   3686     }
  3670   3687     sqlite3ReleaseTempReg(pParse, regFree1);
................................................................................
  3758   3775         testcase( op==TK_GE );
  3759   3776         testcase( op==TK_EQ );
  3760   3777         testcase( op==TK_NE );
  3761   3778         testcase( jumpIfNull==0 );
  3762   3779         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3763   3780         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  3764   3781         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
  3765         -                  r1, r2, dest, jumpIfNull);
         3782  +                  r1, r2, dest, jumpIfNull);  VdbeCoverage(v);
  3766   3783         testcase( regFree1==0 );
  3767   3784         testcase( regFree2==0 );
  3768   3785         break;
  3769   3786       }
  3770   3787       case TK_IS:
  3771   3788       case TK_ISNOT: {
  3772   3789         testcase( pExpr->op==TK_IS );
  3773   3790         testcase( pExpr->op==TK_ISNOT );
  3774   3791         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3775   3792         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  3776   3793         op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
  3777   3794         codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
  3778         -                  r1, r2, dest, SQLITE_NULLEQ);
         3795  +                  r1, r2, dest, SQLITE_NULLEQ);  VdbeCoverage(v);
  3779   3796         testcase( regFree1==0 );
  3780   3797         testcase( regFree2==0 );
  3781   3798         break;
  3782   3799       }
  3783   3800       case TK_ISNULL:
  3784   3801       case TK_NOTNULL: {
  3785   3802         testcase( op==TK_ISNULL );
  3786   3803         testcase( op==TK_NOTNULL );
  3787   3804         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3788         -      sqlite3VdbeAddOp2(v, op, r1, dest);
         3805  +      sqlite3VdbeAddOp2(v, op, r1, dest); VdbeCoverage(v);
  3789   3806         testcase( regFree1==0 );
  3790   3807         break;
  3791   3808       }
  3792   3809       case TK_BETWEEN: {
  3793   3810         testcase( jumpIfNull==0 );
  3794   3811         exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
  3795   3812         break;
................................................................................
  3810   3827         if( exprAlwaysFalse(pExpr) ){
  3811   3828           sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
  3812   3829         }else if( exprAlwaysTrue(pExpr) ){
  3813   3830           /* no-op */
  3814   3831         }else{
  3815   3832           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  3816   3833           sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
         3834  +        VdbeCoverage(v);
  3817   3835           testcase( regFree1==0 );
  3818   3836           testcase( jumpIfNull==0 );
  3819   3837         }
  3820   3838         break;
  3821   3839       }
  3822   3840     }
  3823   3841     sqlite3ReleaseTempReg(pParse, regFree1);

Changes to src/fkey.c.

   336    336     ** to check if deleting this row resolves any outstanding violations.
   337    337     **
   338    338     ** Check if any of the key columns in the child table row are NULL. If 
   339    339     ** any are, then the constraint is considered satisfied. No need to 
   340    340     ** search for a matching row in the parent table.  */
   341    341     if( nIncr<0 ){
   342    342       sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
          343  +    VdbeCoverage(v);
   343    344     }
   344    345     for(i=0; i<pFKey->nCol; i++){
   345    346       int iReg = aiCol[i] + regData + 1;
   346         -    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
          347  +    sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
   347    348     }
   348    349   
   349    350     if( isIgnore==0 ){
   350    351       if( pIdx==0 ){
   351    352         /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
   352    353         ** column of the parent table (table pTab).  */
   353    354         int iMustBeInt;               /* Address of MustBeInt instruction */
................................................................................
   356    357         /* Invoke MustBeInt to coerce the child key value to an integer (i.e. 
   357    358         ** apply the affinity of the parent key). If this fails, then there
   358    359         ** is no matching parent key. Before using MustBeInt, make a copy of
   359    360         ** the value. Otherwise, the value inserted into the child key column
   360    361         ** will have INTEGER affinity applied to it, which may not be correct.  */
   361    362         sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
   362    363         iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
          364  +      VdbeCoverage(v);
   363    365     
   364    366         /* If the parent table is the same as the child table, and we are about
   365    367         ** to increment the constraint-counter (i.e. this is an INSERT operation),
   366    368         ** then check if the row being inserted matches itself. If so, do not
   367    369         ** increment the constraint-counter.  */
   368    370         if( pTab==pFKey->pFrom && nIncr==1 ){
   369         -        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
          371  +        sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
   370    372         }
   371    373     
   372    374         sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
   373         -      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
          375  +      sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
   374    376         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
   375    377         sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
   376    378         sqlite3VdbeJumpHere(v, iMustBeInt);
   377    379         sqlite3ReleaseTempReg(pParse, regTemp);
   378    380       }else{
   379    381         int nCol = pFKey->nCol;
   380    382         int regTemp = sqlite3GetTempRange(pParse, nCol);
................................................................................
   402    404             int iChild = aiCol[i]+1+regData;
   403    405             int iParent = pIdx->aiColumn[i]+1+regData;
   404    406             assert( aiCol[i]!=pTab->iPKey );
   405    407             if( pIdx->aiColumn[i]==pTab->iPKey ){
   406    408               /* The parent key is a composite key that includes the IPK column */
   407    409               iParent = regData;
   408    410             }
   409         -          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
          411  +          sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
   410    412             sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
   411    413           }
   412    414           sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
   413    415         }
   414    416     
   415         -      sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
   416         -      sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
   417         -      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
          417  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
          418  +                        sqlite3IndexAffinityStr(v,pIdx), nCol);
          419  +      sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
   418    420     
   419    421         sqlite3ReleaseTempReg(pParse, regRec);
   420    422         sqlite3ReleaseTempRange(pParse, regTemp, nCol);
   421    423       }
   422    424     }
   423    425   
   424    426     if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
................................................................................
   548    550     assert( pIdx==0 || pIdx->pTable==pTab );
   549    551     assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
   550    552     assert( pIdx!=0 || pFKey->nCol==1 );
   551    553     assert( pIdx!=0 || HasRowid(pTab) );
   552    554   
   553    555     if( nIncr<0 ){
   554    556       iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
          557  +    VdbeCoverage(v);
   555    558     }
   556    559   
   557    560     /* Create an Expr object representing an SQL expression like:
   558    561     **
   559    562     **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
   560    563     **
   561    564     ** The collation sequence used for the comparison should be that of
................................................................................
   710    713         ** when this statement is run.  */
   711    714         FKey *p;
   712    715         for(p=pTab->pFKey; p; p=p->pNextFrom){
   713    716           if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
   714    717         }
   715    718         if( !p ) return;
   716    719         iSkip = sqlite3VdbeMakeLabel(v);
   717         -      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
          720  +      sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
   718    721       }
   719    722   
   720    723       pParse->disableTriggers = 1;
   721    724       sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
   722    725       pParse->disableTriggers = 0;
   723    726   
   724    727       /* If the DELETE has generated immediate foreign key constraint 
................................................................................
   728    731       **
   729    732       ** If the SQLITE_DeferFKs flag is set, then this is not required, as
   730    733       ** the statement transaction will not be rolled back even if FK
   731    734       ** constraints are violated.
   732    735       */
   733    736       if( (db->flags & SQLITE_DeferFKs)==0 ){
   734    737         sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
          738  +      VdbeCoverage(v);
   735    739         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
   736    740             OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
   737    741       }
   738    742   
   739    743       if( iSkip ){
   740    744         sqlite3VdbeResolveLabel(v, iSkip);
   741    745       }
................................................................................
   887    891           ** missing, behave as if it is empty. i.e. decrement the relevant
   888    892           ** FK counter for each row of the current table with non-NULL keys.
   889    893           */
   890    894           Vdbe *v = sqlite3GetVdbe(pParse);
   891    895           int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
   892    896           for(i=0; i<pFKey->nCol; i++){
   893    897             int iReg = pFKey->aCol[i].iFrom + regOld + 1;
   894         -          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
          898  +          sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
   895    899           }
   896    900           sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
   897    901         }
   898    902         continue;
   899    903       }
   900    904       assert( pFKey->nCol==1 || (aiFree && pIdx) );
   901    905   

Changes to src/insert.c.

    94     94       pIdx->zColAff[n] = 0;
    95     95     }
    96     96    
    97     97     return pIdx->zColAff;
    98     98   }
    99     99   
   100    100   /*
   101         -** Set P4 of the most recently inserted opcode to a column affinity
   102         -** string for table pTab. A column affinity string has one character
   103         -** for each column indexed by the index, according to the affinity of the
   104         -** column:
          101  +** Compute the affinity string for table pTab, if it has not already been
          102  +** computed.  As an optimization, omit trailing SQLITE_AFF_NONE affinities.
          103  +**
          104  +** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values and
          105  +** if iReg>0 then code an OP_Affinity opcode that will set the affinities
          106  +** for register iReg and following.  Or if affinities exists and iReg==0,
          107  +** then just set the P4 operand of the previous opcode (which should  be
          108  +** an OP_MakeRecord) to the affinity string.
          109  +**
          110  +** A column affinity string has one character column:
   105    111   **
   106    112   **  Character      Column affinity
   107    113   **  ------------------------------
   108    114   **  'a'            TEXT
   109    115   **  'b'            NONE
   110    116   **  'c'            NUMERIC
   111    117   **  'd'            INTEGER
   112    118   **  'e'            REAL
   113    119   */
   114         -void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
   115         -  /* The first time a column affinity string for a particular table
   116         -  ** is required, it is allocated and populated here. It is then 
   117         -  ** stored as a member of the Table structure for subsequent use.
   118         -  **
   119         -  ** The column affinity string will eventually be deleted by
   120         -  ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
   121         -  */
   122         -  if( !pTab->zColAff ){
   123         -    char *zColAff;
   124         -    int i;
          120  +void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
          121  +  int i;
          122  +  char *zColAff = pTab->zColAff;
          123  +  if( zColAff==0 ){
   125    124       sqlite3 *db = sqlite3VdbeDb(v);
   126         -
   127    125       zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
   128    126       if( !zColAff ){
   129    127         db->mallocFailed = 1;
   130    128         return;
   131    129       }
   132    130   
   133    131       for(i=0; i<pTab->nCol; i++){
   134    132         zColAff[i] = pTab->aCol[i].affinity;
   135    133       }
   136         -    zColAff[pTab->nCol] = '\0';
   137         -
          134  +    do{
          135  +      zColAff[i--] = 0;
          136  +    }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
   138    137       pTab->zColAff = zColAff;
   139    138     }
   140         -
   141         -  sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
          139  +  i = sqlite3Strlen30(zColAff);
          140  +  if( i ){
          141  +    if( iReg ){
          142  +      sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
          143  +    }else{
          144  +      sqlite3VdbeChangeP4(v, -1, zColAff, i);
          145  +    }
          146  +  }
   142    147   }
   143    148   
   144    149   /*
   145    150   ** Return non-zero if the table pTab in database iDb or any of its indices
   146    151   ** have been opened at any point in the VDBE program beginning at location
   147    152   ** iStartAddr throught the end of the program.  This is used to see if 
   148    153   ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can 
   149    154   ** run without using temporary table for the results of the SELECT. 
   150    155   */
   151         -static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
          156  +static int readsTable(Parse *p, int iDb, Table *pTab){
   152    157     Vdbe *v = sqlite3GetVdbe(p);
   153    158     int i;
   154    159     int iEnd = sqlite3VdbeCurrentAddr(v);
   155    160   #ifndef SQLITE_OMIT_VIRTUALTABLE
   156    161     VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
   157    162   #endif
   158    163   
   159         -  for(i=iStartAddr; i<iEnd; i++){
          164  +  for(i=1; i<iEnd; i++){
   160    165       VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
   161    166       assert( pOp!=0 );
   162    167       if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
   163    168         Index *pIndex;
   164    169         int tnum = pOp->p2;
   165    170         if( tnum==pTab->tnum ){
   166    171           return 1;
................................................................................
   253    258       pDb = &db->aDb[p->iDb];
   254    259       memId = p->regCtr;
   255    260       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
   256    261       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
   257    262       sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
   258    263       addr = sqlite3VdbeCurrentAddr(v);
   259    264       sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
   260         -    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
          265  +    sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
   261    266       sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
   262         -    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
          267  +    sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
   263    268       sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
   264    269       sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
   265    270       sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
   266    271       sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
   267         -    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
          272  +    sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
   268    273       sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
   269    274       sqlite3VdbeAddOp0(v, OP_Close);
   270    275     }
   271    276   }
   272    277   
   273    278   /*
   274    279   ** Update the maximum rowid for an autoincrement calculation.
................................................................................
   302    307       int j1, j2, j3, j4, j5;
   303    308       int iRec;
   304    309       int memId = p->regCtr;
   305    310   
   306    311       iRec = sqlite3GetTempReg(pParse);
   307    312       assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
   308    313       sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
   309         -    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
   310         -    j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
          314  +    j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
          315  +    j2 = sqlite3VdbeAddOp0(v, OP_Rewind); VdbeCoverage(v);
   311    316       j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
   312         -    j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
   313         -    sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
          317  +    j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec); VdbeCoverage(v);
          318  +    sqlite3VdbeAddOp2(v, OP_Next, 0, j3); VdbeCoverage(v);
   314    319       sqlite3VdbeJumpHere(v, j2);
   315    320       sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
   316    321       j5 = sqlite3VdbeAddOp0(v, OP_Goto);
   317    322       sqlite3VdbeJumpHere(v, j4);
   318    323       sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
   319    324       sqlite3VdbeJumpHere(v, j1);
   320    325       sqlite3VdbeJumpHere(v, j5);
................................................................................
   329    334   /*
   330    335   ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
   331    336   ** above are all no-ops
   332    337   */
   333    338   # define autoIncBegin(A,B,C) (0)
   334    339   # define autoIncStep(A,B,C)
   335    340   #endif /* SQLITE_OMIT_AUTOINCREMENT */
   336         -
   337         -
   338         -/*
   339         -** Generate code for a co-routine that will evaluate a subquery one
   340         -** row at a time.
   341         -**
   342         -** The pSelect parameter is the subquery that the co-routine will evaluation.
   343         -** Information about the location of co-routine and the registers it will use
   344         -** is returned by filling in the pDest object.
   345         -**
   346         -** Registers are allocated as follows:
   347         -**
   348         -**   pDest->iSDParm      The register holding the next entry-point of the
   349         -**                       co-routine.  Run the co-routine to its next breakpoint
   350         -**                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
   351         -**
   352         -**   pDest->iSdst        First result register.
   353         -**
   354         -**   pDest->nSdst        Number of result registers.
   355         -**
   356         -** At EOF the first result register will be marked as "undefined" so that
   357         -** the caller can know when to stop reading results.
   358         -**
   359         -** This routine handles all of the register allocation and fills in the
   360         -** pDest structure appropriately.
   361         -**
   362         -** Here is a schematic of the generated code assuming that X is the 
   363         -** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
   364         -** completed flag reg[pDest->iSDParm+1], and R and S are the range of
   365         -** registers that hold the result set, reg[pDest->iSdst] through
   366         -** reg[pDest->iSdst+pDest->nSdst-1]:
   367         -**
   368         -**         X <- A
   369         -**         goto B
   370         -**      A: setup for the SELECT
   371         -**         loop rows in the SELECT
   372         -**           load results into registers R..S
   373         -**           yield X
   374         -**         end loop
   375         -**         cleanup after the SELECT
   376         -**         end co-routine R
   377         -**      B:
   378         -**
   379         -** To use this subroutine, the caller generates code as follows:
   380         -**
   381         -**         [ Co-routine generated by this subroutine, shown above ]
   382         -**      S: yield X, at EOF goto E
   383         -**         if skip this row, goto C
   384         -**         if terminate loop, goto E
   385         -**         deal with this row
   386         -**      C: goto S
   387         -**      E:
   388         -*/
   389         -int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
   390         -  int regYield;       /* Register holding co-routine entry-point */
   391         -  int addrTop;        /* Top of the co-routine */
   392         -  int rc;             /* Result code */
   393         -  Vdbe *v;            /* VDBE under construction */
   394         -
   395         -  regYield = ++pParse->nMem;
   396         -  v = sqlite3GetVdbe(pParse);
   397         -  addrTop = sqlite3VdbeCurrentAddr(v) + 1;
   398         -  sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
   399         -  sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
   400         -  rc = sqlite3Select(pParse, pSelect, pDest);
   401         -  assert( pParse->nErr==0 || rc );
   402         -  if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
   403         -  if( rc ) return rc;
   404         -  sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
   405         -  sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
   406         -  return rc;
   407         -}
   408         -
   409    341   
   410    342   
   411    343   /* Forward declaration */
   412    344   static int xferOptimization(
   413    345     Parse *pParse,        /* Parser context */
   414    346     Table *pDest,         /* The table we are inserting into */
   415    347     Select *pSelect,      /* A SELECT statement to use as the data source */
................................................................................
   527    459     Index *pIdx;          /* For looping over indices of the table */
   528    460     int nColumn;          /* Number of columns in the data */
   529    461     int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
   530    462     int iDataCur = 0;     /* VDBE cursor that is the main data repository */
   531    463     int iIdxCur = 0;      /* First index cursor */
   532    464     int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
   533    465     int endOfLoop;        /* Label for the end of the insertion loop */
   534         -  int useTempTable = 0; /* Store SELECT results in intermediate table */
   535    466     int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
   536    467     int addrInsTop = 0;   /* Jump to label "D" */
   537    468     int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
   538         -  int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
   539    469     SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
   540    470     int iDb;              /* Index of database holding TABLE */
   541    471     Db *pDb;              /* The database containing table being inserted into */
   542         -  int appendFlag = 0;   /* True if the insert is likely to be an append */
   543         -  int withoutRowid;     /* 0 for normal table.  1 for WITHOUT ROWID table */
          472  +  u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
          473  +  u8 appendFlag = 0;    /* True if the insert is likely to be an append */
          474  +  u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
          475  +  u8 bIdListInOrder = 1; /* True if IDLIST is in table order */
   544    476     ExprList *pList = 0;  /* List of VALUES() to be inserted  */
   545    477   
   546    478     /* Register allocations */
   547    479     int regFromSelect = 0;/* Base register for data coming from SELECT */
   548    480     int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
   549    481     int regRowCount = 0;  /* Memory cell used for the row counter */
   550    482     int regIns;           /* Block of regs holding rowid+data being inserted */
................................................................................
   647    579     }
   648    580   #endif /* SQLITE_OMIT_XFER_OPT */
   649    581   
   650    582     /* If this is an AUTOINCREMENT table, look up the sequence number in the
   651    583     ** sqlite_sequence table and store it in memory cell regAutoinc.
   652    584     */
   653    585     regAutoinc = autoIncBegin(pParse, iDb, pTab);
          586  +
          587  +  /* Allocate registers for holding the rowid of the new row,
          588  +  ** the content of the new row, and the assemblied row record.
          589  +  */
          590  +  regRowid = regIns = pParse->nMem+1;
          591  +  pParse->nMem += pTab->nCol + 1;
          592  +  if( IsVirtual(pTab) ){
          593  +    regRowid++;
          594  +    pParse->nMem++;
          595  +  }
          596  +  regData = regRowid+1;
          597  +
          598  +  /* If the INSERT statement included an IDLIST term, then make sure
          599  +  ** all elements of the IDLIST really are columns of the table and 
          600  +  ** remember the column indices.
          601  +  **
          602  +  ** If the table has an INTEGER PRIMARY KEY column and that column
          603  +  ** is named in the IDLIST, then record in the ipkColumn variable
          604  +  ** the index into IDLIST of the primary key column.  ipkColumn is
          605  +  ** the index of the primary key as it appears in IDLIST, not as
          606  +  ** is appears in the original table.  (The index of the INTEGER
          607  +  ** PRIMARY KEY in the original table is pTab->iPKey.)
          608  +  */
          609  +  if( pColumn ){
          610  +    for(i=0; i<pColumn->nId; i++){
          611  +      pColumn->a[i].idx = -1;
          612  +    }
          613  +    for(i=0; i<pColumn->nId; i++){
          614  +      for(j=0; j<pTab->nCol; j++){
          615  +        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
          616  +          pColumn->a[i].idx = j;
          617  +          if( i!=j ) bIdListInOrder = 0;
          618  +          if( j==pTab->iPKey ){
          619  +            ipkColumn = i;  assert( !withoutRowid );
          620  +          }
          621  +          break;
          622  +        }
          623  +      }
          624  +      if( j>=pTab->nCol ){
          625  +        if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
          626  +          ipkColumn = i;
          627  +        }else{
          628  +          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
          629  +              pTabList, 0, pColumn->a[i].zName);
          630  +          pParse->checkSchema = 1;
          631  +          goto insert_cleanup;
          632  +        }
          633  +      }
          634  +    }
          635  +  }
   654    636   
   655    637     /* Figure out how many columns of data are supplied.  If the data
   656    638     ** is coming from a SELECT statement, then generate a co-routine that
   657    639     ** produces a single row of the SELECT on each invocation.  The
   658    640     ** co-routine is the common header to the 3rd and 4th templates.
   659    641     */
   660    642     if( pSelect ){
   661    643       /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
   662         -    int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
   663         -    if( rc ) goto insert_cleanup;
          644  +    int regYield;       /* Register holding co-routine entry-point */
          645  +    int addrTop;        /* Top of the co-routine */
          646  +    int rc;             /* Result code */
   664    647   
          648  +    regYield = ++pParse->nMem;
          649  +    addrTop = sqlite3VdbeCurrentAddr(v) + 1;
          650  +    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
          651  +    sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
          652  +    dest.iSdst = bIdListInOrder ? regData : 0;
          653  +    dest.nSdst = pTab->nCol;
          654  +    rc = sqlite3Select(pParse, pSelect, &dest);
   665    655       regFromSelect = dest.iSdst;
          656  +    assert( pParse->nErr==0 || rc );
          657  +    if( rc || db->mallocFailed ) goto insert_cleanup;
          658  +    sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
          659  +    sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
   666    660       assert( pSelect->pEList );
   667    661       nColumn = pSelect->pEList->nExpr;
   668         -    assert( dest.nSdst==nColumn );
   669    662   
   670    663       /* Set useTempTable to TRUE if the result of the SELECT statement
   671    664       ** should be written into a temporary table (template 4).  Set to
   672    665       ** FALSE if each output row of the SELECT can be written directly into
   673    666       ** the destination table (template 3).
   674    667       **
   675    668       ** A temp table must be used if the table being updated is also one
   676    669       ** of the tables being read by the SELECT statement.  Also use a 
   677    670       ** temp table in the case of row triggers.
   678    671       */
   679         -    if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
          672  +    if( pTrigger || readsTable(pParse, iDb, pTab) ){
   680    673         useTempTable = 1;
   681    674       }
   682    675   
   683    676       if( useTempTable ){
   684    677         /* Invoke the coroutine to extract information from the SELECT
   685    678         ** and add it to a transient table srcTab.  The code generated
   686    679         ** here is from the 4th template:
................................................................................
   695    688         int regTempRowid;    /* Register to hold temp table ROWID */
   696    689         int addrTop;         /* Label "L" */
   697    690   
   698    691         srcTab = pParse->nTab++;
   699    692         regRec = sqlite3GetTempReg(pParse);
   700    693         regTempRowid = sqlite3GetTempReg(pParse);
   701    694         sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
   702         -      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
          695  +      addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
   703    696         sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
   704    697         sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
   705    698         sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
   706    699         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
   707    700         sqlite3VdbeJumpHere(v, addrTop);
   708    701         sqlite3ReleaseTempReg(pParse, regRec);
   709    702         sqlite3ReleaseTempReg(pParse, regTempRowid);
................................................................................
   720    713       nColumn = pList ? pList->nExpr : 0;
   721    714       for(i=0; i<nColumn; i++){
   722    715         if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
   723    716           goto insert_cleanup;
   724    717         }
   725    718       }
   726    719     }
          720  +
          721  +  /* If there is no IDLIST term but the table has an integer primary
          722  +  ** key, the set the ipkColumn variable to the integer primary key 
          723  +  ** column index in the original table definition.
          724  +  */
          725  +  if( pColumn==0 && nColumn>0 ){
          726  +    ipkColumn = pTab->iPKey;
          727  +  }
   727    728   
   728    729     /* Make sure the number of columns in the source data matches the number
   729    730     ** of columns to be inserted into the table.
   730    731     */
   731    732     if( IsVirtual(pTab) ){
   732    733       for(i=0; i<pTab->nCol; i++){
   733    734         nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
................................................................................
   739    740          pTabList, 0, pTab->nCol-nHidden, nColumn);
   740    741       goto insert_cleanup;
   741    742     }
   742    743     if( pColumn!=0 && nColumn!=pColumn->nId ){
   743    744       sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
   744    745       goto insert_cleanup;
   745    746     }
   746         -
   747         -  /* If the INSERT statement included an IDLIST term, then make sure
   748         -  ** all elements of the IDLIST really are columns of the table and 
   749         -  ** remember the column indices.
   750         -  **
   751         -  ** If the table has an INTEGER PRIMARY KEY column and that column
   752         -  ** is named in the IDLIST, then record in the ipkColumn variable
   753         -  ** the index into IDLIST of the primary key column.  ipkColumn is
   754         -  ** the index of the primary key as it appears in IDLIST, not as
   755         -  ** is appears in the original table.  (The index of the INTEGER
   756         -  ** PRIMARY KEY in the original table is pTab->iPKey.)
   757         -  */
   758         -  if( pColumn ){
   759         -    for(i=0; i<pColumn->nId; i++){
   760         -      pColumn->a[i].idx = -1;
   761         -    }
   762         -    for(i=0; i<pColumn->nId; i++){
   763         -      for(j=0; j<pTab->nCol; j++){
   764         -        if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
   765         -          pColumn->a[i].idx = j;
   766         -          if( j==pTab->iPKey ){
   767         -            ipkColumn = i;  assert( !withoutRowid );
   768         -          }
   769         -          break;
   770         -        }
   771         -      }
   772         -      if( j>=pTab->nCol ){
   773         -        if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
   774         -          ipkColumn = i;
   775         -        }else{
   776         -          sqlite3ErrorMsg(pParse, "table %S has no column named %s",
   777         -              pTabList, 0, pColumn->a[i].zName);
   778         -          pParse->checkSchema = 1;
   779         -          goto insert_cleanup;
   780         -        }
   781         -      }
   782         -    }
   783         -  }
   784         -
   785         -  /* If there is no IDLIST term but the table has an integer primary
   786         -  ** key, the set the ipkColumn variable to the integer primary key 
   787         -  ** column index in the original table definition.
   788         -  */
   789         -  if( pColumn==0 && nColumn>0 ){
   790         -    ipkColumn = pTab->iPKey;
   791         -  }
   792    747       
   793    748     /* Initialize the count of rows to be inserted
   794    749     */
   795    750     if( db->flags & SQLITE_CountRows ){
   796    751       regRowCount = ++pParse->nMem;
   797    752       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
   798    753     }
................................................................................
   818    773       **
   819    774       **         rewind temp table, if empty goto D
   820    775       **      C: loop over rows of intermediate table
   821    776       **           transfer values form intermediate table into <table>
   822    777       **         end loop
   823    778       **      D: ...
   824    779       */
   825         -    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
          780  +    addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
   826    781       addrCont = sqlite3VdbeCurrentAddr(v);
   827    782     }else if( pSelect ){
   828    783       /* This block codes the top of loop only.  The complete loop is the
   829    784       ** following pseudocode (template 3):
   830    785       **
   831    786       **      C: yield X, at EOF goto D
   832    787       **         insert the select result into <table> from R..R+n
   833    788       **         goto C
   834    789       **      D: ...
   835    790       */
   836    791       addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
          792  +    VdbeCoverage(v);
   837    793     }
   838    794   
   839         -  /* Allocate registers for holding the rowid of the new row,
   840         -  ** the content of the new row, and the assemblied row record.
   841         -  */
   842         -  regRowid = regIns = pParse->nMem+1;
   843         -  pParse->nMem += pTab->nCol + 1;
   844         -  if( IsVirtual(pTab) ){
   845         -    regRowid++;
   846         -    pParse->nMem++;
   847         -  }
   848         -  regData = regRowid+1;
   849         -
   850    795     /* Run the BEFORE and INSTEAD OF triggers, if there are any
   851    796     */
   852    797     endOfLoop = sqlite3VdbeMakeLabel(v);
   853    798     if( tmask & TRIGGER_BEFORE ){
   854    799       int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
   855    800   
   856    801       /* build the NEW.* reference row.  Note that if there is an INTEGER
................................................................................
   866    811         assert( !withoutRowid );
   867    812         if( useTempTable ){
   868    813           sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
   869    814         }else{
   870    815           assert( pSelect==0 );  /* Otherwise useTempTable is true */
   871    816           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
   872    817         }
   873         -      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
          818  +      j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
   874    819         sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
   875    820         sqlite3VdbeJumpHere(v, j1);
   876         -      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
          821  +      sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
   877    822       }
   878    823   
   879    824       /* Cannot have triggers on a virtual table. If it were possible,
   880    825       ** this block would have to account for hidden column.
   881    826       */
   882    827       assert( !IsVirtual(pTab) );
   883    828   
................................................................................
   903    848   
   904    849       /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
   905    850       ** do not attempt any conversions before assembling the record.
   906    851       ** If this is a real table, attempt conversions as required by the
   907    852       ** table column affinities.
   908    853       */
   909    854       if( !isView ){
   910         -      sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
   911         -      sqlite3TableAffinityStr(v, pTab);
          855  +      sqlite3TableAffinity(v, pTab, regCols+1);
   912    856       }
   913    857   
   914    858       /* Fire BEFORE or INSTEAD OF triggers */
   915    859       sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, 
   916    860           pTab, regCols-pTab->nCol-1, onError, endOfLoop);
   917    861   
   918    862       sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
................................................................................
   926    870         /* The row that the VUpdate opcode will delete: none */
   927    871         sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
   928    872       }
   929    873       if( ipkColumn>=0 ){
   930    874         if( useTempTable ){
   931    875           sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
   932    876         }else if( pSelect ){
   933         -        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+ipkColumn, regRowid);
          877  +        sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
   934    878         }else{
   935    879           VdbeOp *pOp;
   936    880           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
   937    881           pOp = sqlite3VdbeGetOp(v, -1);
   938    882           if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
   939    883             appendFlag = 1;
   940    884             pOp->opcode = OP_NewRowid;
................................................................................
   945    889         }
   946    890         /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
   947    891         ** to generate a unique primary key value.
   948    892         */
   949    893         if( !appendFlag ){
   950    894           int j1;
   951    895           if( !IsVirtual(pTab) ){
   952         -          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
          896  +          j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
   953    897             sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
   954    898             sqlite3VdbeJumpHere(v, j1);
   955    899           }else{
   956    900             j1 = sqlite3VdbeCurrentAddr(v);
   957         -          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
          901  +          sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); VdbeCoverage(v);
   958    902           }
   959         -        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
          903  +        sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
   960    904         }
   961    905       }else if( IsVirtual(pTab) || withoutRowid ){
   962    906         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
   963    907       }else{
   964    908         sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
   965    909         appendFlag = 1;
   966    910       }
................................................................................
   972    916       nHidden = 0;
   973    917       for(i=0; i<pTab->nCol; i++){
   974    918         int iRegStore = regRowid+1+i;
   975    919         if( i==pTab->iPKey ){
   976    920           /* The value of the INTEGER PRIMARY KEY column is always a NULL.
   977    921           ** Whenever this column is read, the rowid will be substituted
   978    922           ** in its place.  Hence, fill this column with a NULL to avoid
   979         -        ** taking up data space with information that will never be used. */
   980         -        sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
          923  +        ** taking up data space with information that will never be used.
          924  +        ** As there may be shallow copies of this value, make it a soft-NULL */
          925  +        sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
   981    926           continue;
   982    927         }
   983    928         if( pColumn==0 ){
   984    929           if( IsHiddenColumn(&pTab->aCol[i]) ){
   985    930             assert( IsVirtual(pTab) );
   986    931             j = -1;
   987    932             nHidden++;
................................................................................
   990    935           }
   991    936         }else{
   992    937           for(j=0; j<pColumn->nId; j++){
   993    938             if( pColumn->a[j].idx==i ) break;
   994    939           }
   995    940         }
   996    941         if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
   997         -        sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
          942  +        sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
   998    943         }else if( useTempTable ){
   999    944           sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); 
  1000    945         }else if( pSelect ){
  1001         -        sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
          946  +        if( regFromSelect!=regData ){
          947  +          sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
          948  +        }
  1002    949         }else{
  1003    950           sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
  1004    951         }
  1005    952       }
  1006    953   
  1007    954       /* Generate code to check constraints and generate index keys and
  1008    955       ** do the insertion.
................................................................................
  1040    987     }
  1041    988   
  1042    989     /* The bottom of the main insertion loop, if the data source
  1043    990     ** is a SELECT statement.
  1044    991     */
  1045    992     sqlite3VdbeResolveLabel(v, endOfLoop);
  1046    993     if( useTempTable ){
  1047         -    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
          994  +    sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
  1048    995       sqlite3VdbeJumpHere(v, addrInsTop);
  1049    996       sqlite3VdbeAddOp1(v, OP_Close, srcTab);
  1050    997     }else if( pSelect ){
  1051    998       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
  1052    999       sqlite3VdbeJumpHere(v, addrInsTop);
  1053   1000     }
  1054   1001   
................................................................................
  1207   1154     int onError;         /* Conflict resolution strategy */
  1208   1155     int j1;              /* Addresss of jump instruction */
  1209   1156     int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
  1210   1157     int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
  1211   1158     int ipkTop = 0;      /* Top of the rowid change constraint check */
  1212   1159     int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
  1213   1160     u8 isUpdate;         /* True if this is an UPDATE operation */
         1161  +  u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
  1214   1162     int regRowid = -1;   /* Register holding ROWID value */
  1215   1163   
  1216   1164     isUpdate = regOldData!=0;
  1217   1165     db = pParse->db;
  1218   1166     v = sqlite3GetVdbe(pParse);
  1219   1167     assert( v!=0 );
  1220   1168     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
................................................................................
  1261   1209         case OE_Rollback:
  1262   1210         case OE_Fail: {
  1263   1211           char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
  1264   1212                                       pTab->aCol[i].zName);
  1265   1213           sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
  1266   1214                             regNewData+1+i, zMsg, P4_DYNAMIC);
  1267   1215           sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
         1216  +        VdbeCoverage(v);
  1268   1217           break;
  1269   1218         }
  1270   1219         case OE_Ignore: {
  1271   1220           sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
         1221  +        VdbeCoverage(v);
  1272   1222           break;
  1273   1223         }
  1274   1224         default: {
  1275   1225           assert( onError==OE_Replace );
  1276         -        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
         1226  +        j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); VdbeCoverage(v);
  1277   1227           sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
  1278   1228           sqlite3VdbeJumpHere(v, j1);
  1279   1229           break;
  1280   1230         }
  1281   1231       }
  1282   1232     }
  1283   1233   
................................................................................
  1321   1271       }
  1322   1272   
  1323   1273       if( isUpdate ){
  1324   1274         /* pkChng!=0 does not mean that the rowid has change, only that
  1325   1275         ** it might have changed.  Skip the conflict logic below if the rowid
  1326   1276         ** is unchanged. */
  1327   1277         sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
         1278  +      VdbeCoverage(v);
  1328   1279       }
  1329   1280   
  1330   1281       /* If the response to a rowid conflict is REPLACE but the response
  1331   1282       ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
  1332   1283       ** to defer the running of the rowid conflict checking until after
  1333   1284       ** the UNIQUE constraints have run.
  1334   1285       */
................................................................................
  1340   1291           }
  1341   1292         }
  1342   1293       }
  1343   1294   
  1344   1295       /* Check to see if the new rowid already exists in the table.  Skip
  1345   1296       ** the following conflict logic if it does not. */
  1346   1297       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
         1298  +    VdbeCoverage(v);
  1347   1299   
  1348   1300       /* Generate code that deals with a rowid collision */
  1349   1301       switch( onError ){
  1350   1302         default: {
  1351   1303           onError = OE_Abort;
  1352   1304           /* Fall thru into the next case */
  1353   1305         }
................................................................................
  1418   1370     for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
  1419   1371       int regIdx;          /* Range of registers hold conent for pIdx */
  1420   1372       int regR;            /* Range of registers holding conflicting PK */
  1421   1373       int iThisCur;        /* Cursor for this UNIQUE index */
  1422   1374       int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
  1423   1375   
  1424   1376       if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
         1377  +    if( bAffinityDone==0 ){
         1378  +      sqlite3TableAffinity(v, pTab, regNewData+1);
         1379  +      bAffinityDone = 1;
         1380  +    }
  1425   1381       iThisCur = iIdxCur+ix;
  1426   1382       addrUniqueOk = sqlite3VdbeMakeLabel(v);
  1427   1383   
  1428   1384       /* Skip partial indices for which the WHERE clause is not true */
  1429   1385       if( pIdx->pPartIdxWhere ){
  1430   1386         sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
  1431   1387         pParse->ckBase = regNewData+1;
................................................................................
  1448   1404         }else{
  1449   1405           x = iField + regNewData + 1;
  1450   1406         }
  1451   1407         sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
  1452   1408         VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
  1453   1409       }
  1454   1410       sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
  1455         -    sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
  1456   1411       VdbeComment((v, "for %s", pIdx->zName));
  1457   1412       sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
  1458   1413   
  1459   1414       /* In an UPDATE operation, if this index is the PRIMARY KEY index 
  1460   1415       ** of a WITHOUT ROWID table and there has been no change the
  1461   1416       ** primary key, then no collision is possible.  The collision detection
  1462   1417       ** logic below can all be skipped. */
................................................................................
  1476   1431         onError = overrideError;
  1477   1432       }else if( onError==OE_Default ){
  1478   1433         onError = OE_Abort;
  1479   1434       }
  1480   1435       
  1481   1436       /* Check to see if the new index entry will be unique */
  1482   1437       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
  1483         -                         regIdx, pIdx->nKeyCol);
         1438  +                         regIdx, pIdx->nKeyCol); VdbeCoverage(v);
  1484   1439   
  1485   1440       /* Generate code to handle collisions */
  1486   1441       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
  1487   1442       if( isUpdate || onError==OE_Replace ){
  1488   1443         if( HasRowid(pTab) ){
  1489   1444           sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
  1490   1445           /* Conflict only if the rowid of the existing index entry
  1491   1446           ** is different from old-rowid */
  1492   1447           if( isUpdate ){
  1493   1448             sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
         1449  +          VdbeCoverage(v);
  1494   1450           }
  1495   1451         }else{
  1496   1452           int x;
  1497   1453           /* Extract the PRIMARY KEY from the end of the index entry and
  1498   1454           ** store it in registers regR..regR+nPk-1 */
  1499   1455           if( pIdx!=pPk ){
  1500   1456             for(i=0; i<pPk->nKeyCol; i++){
................................................................................
  1521   1477               x = pPk->aiColumn[i];
  1522   1478               if( i==(pPk->nKeyCol-1) ){
  1523   1479                 addrJump = addrUniqueOk;
  1524   1480                 op = OP_Eq;
  1525   1481               }
  1526   1482               sqlite3VdbeAddOp4(v, op, 
  1527   1483                   regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
  1528         -            );
         1484  +            );  VdbeCoverage(v);
  1529   1485             }
  1530   1486           }
  1531   1487         }
  1532   1488       }
  1533   1489   
  1534   1490       /* Generate code that executes if the new index entry is not unique */
  1535   1491       assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
................................................................................
  1593   1549   ){
  1594   1550     Vdbe *v;            /* Prepared statements under construction */
  1595   1551     Index *pIdx;        /* An index being inserted or updated */
  1596   1552     u8 pik_flags;       /* flag values passed to the btree insert */
  1597   1553     int regData;        /* Content registers (after the rowid) */
  1598   1554     int regRec;         /* Register holding assemblied record for the table */
  1599   1555     int i;              /* Loop counter */
         1556  +  u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
  1600   1557   
  1601   1558     v = sqlite3GetVdbe(pParse);
  1602   1559     assert( v!=0 );
  1603   1560     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1604   1561     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1605   1562       if( aRegIdx[i]==0 ) continue;
         1563  +    bAffinityDone = 1;
  1606   1564       if( pIdx->pPartIdxWhere ){
  1607   1565         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
         1566  +      VdbeCoverage(v);
  1608   1567       }
  1609   1568       sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
  1610   1569       pik_flags = 0;
  1611   1570       if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
  1612   1571       if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
  1613   1572         assert( pParse->nested==0 );
  1614   1573         pik_flags |= OPFLAG_NCHANGE;
................................................................................
  1615   1574       }
  1616   1575       if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
  1617   1576     }
  1618   1577     if( !HasRowid(pTab) ) return;
  1619   1578     regData = regNewData + 1;
  1620   1579     regRec = sqlite3GetTempReg(pParse);
  1621   1580     sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
  1622         -  sqlite3TableAffinityStr(v, pTab);
         1581  +  if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
  1623   1582     sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
  1624   1583     if( pParse->nested ){
  1625   1584       pik_flags = 0;
  1626   1585     }else{
  1627   1586       pik_flags = OPFLAG_NCHANGE;
  1628   1587       pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
  1629   1588     }
................................................................................
  1984   1943       **     of index entries might need to change.)
  1985   1944       **
  1986   1945       ** (2) The destination has a unique index.  (The xfer optimization 
  1987   1946       **     is unable to test uniqueness.)
  1988   1947       **
  1989   1948       ** (3) onError is something other than OE_Abort and OE_Rollback.
  1990   1949       */
  1991         -    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
         1950  +    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
  1992   1951       emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
  1993   1952       sqlite3VdbeJumpHere(v, addr1);
  1994   1953     }
  1995   1954     if( HasRowid(pSrc) ){
  1996   1955       sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
  1997         -    emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
         1956  +    emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
  1998   1957       if( pDest->iPKey>=0 ){
  1999   1958         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
  2000   1959         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
         1960  +      VdbeCoverage(v);
  2001   1961         sqlite3RowidConstraint(pParse, onError, pDest);
  2002   1962         sqlite3VdbeJumpHere(v, addr2);
  2003   1963         autoIncStep(pParse, regAutoinc, regRowid);
  2004   1964       }else if( pDest->pIndex==0 ){
  2005   1965         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
  2006   1966       }else{
  2007   1967         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
  2008   1968         assert( (pDest->tabFlags & TF_Autoincrement)==0 );
  2009   1969       }
  2010   1970       sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
  2011   1971       sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
  2012   1972       sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
  2013   1973       sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
  2014         -    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
         1974  +    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
  2015   1975       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  2016   1976       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  2017   1977     }else{
  2018   1978       sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
  2019   1979       sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
  2020   1980     }
  2021   1981     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
................................................................................
  2026   1986       sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
  2027   1987       sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
  2028   1988       VdbeComment((v, "%s", pSrcIdx->zName));
  2029   1989       sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
  2030   1990       sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
  2031   1991       sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
  2032   1992       VdbeComment((v, "%s", pDestIdx->zName));
  2033         -    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
         1993  +    addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
  2034   1994       sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
  2035   1995       sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
  2036         -    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
         1996  +    sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
  2037   1997       sqlite3VdbeJumpHere(v, addr1);
  2038   1998       sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
  2039   1999       sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
  2040   2000     }
  2041   2001     if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
  2042   2002     sqlite3ReleaseTempReg(pParse, regRowid);
  2043   2003     sqlite3ReleaseTempReg(pParse, regData);

Changes to src/main.c.

  3303   3303       ** that demonstrat invariants on well-formed database files.
  3304   3304       */
  3305   3305       case SQLITE_TESTCTRL_NEVER_CORRUPT: {
  3306   3306         sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
  3307   3307         break;
  3308   3308       }
  3309   3309   
         3310  +
         3311  +    /*   sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
         3312  +    **
         3313  +    ** Set the VDBE coverage callback function to xCallback with context 
         3314  +    ** pointer ptr.
         3315  +    */
         3316  +    case SQLITE_TESTCTRL_VDBE_COVERAGE: {
         3317  +#ifdef SQLITE_VDBE_COVERAGE
         3318  +      typedef void (*branch_callback)(void*,int,u8,u8);
         3319  +      sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
         3320  +      sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
         3321  +#endif
         3322  +      break;
         3323  +    }
         3324  +
  3310   3325     }
  3311   3326     va_end(ap);
  3312   3327   #endif /* SQLITE_OMIT_BUILTIN_TEST */
  3313   3328     return rc;
  3314   3329   }
  3315   3330   
  3316   3331   /*

Changes to src/pragma.c.

   820    820     ** Older versions of SQLite would set the default cache size to a
   821    821     ** negative number to indicate synchronous=OFF.  These days, synchronous
   822    822     ** is always on by default regardless of the sign of the default cache
   823    823     ** size.  But continue to take the absolute value of the default cache
   824    824     ** size of historical compatibility.
   825    825     */
   826    826     case PragTyp_DEFAULT_CACHE_SIZE: {
          827  +    static const int iLn = __LINE__+2;
   827    828       static const VdbeOpList getCacheSize[] = {
   828    829         { OP_Transaction, 0, 0,        0},                         /* 0 */
   829    830         { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
   830    831         { OP_IfPos,       1, 8,        0},
   831    832         { OP_Integer,     0, 2,        0},
   832    833         { OP_Subtract,    1, 2,        1},
   833    834         { OP_IfPos,       1, 8,        0},
................................................................................
   837    838       };
   838    839       int addr;
   839    840       sqlite3VdbeUsesBtree(v, iDb);
   840    841       if( !zRight ){
   841    842         sqlite3VdbeSetNumCols(v, 1);
   842    843         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
   843    844         pParse->nMem += 2;
   844         -      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
          845  +      addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
   845    846         sqlite3VdbeChangeP1(v, addr, iDb);
   846    847         sqlite3VdbeChangeP1(v, addr+1, iDb);
   847    848         sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
   848    849       }else{
   849    850         int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
   850    851         sqlite3BeginWriteOperation(pParse, 0, iDb);
   851    852         sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
................................................................................
  1082   1083         rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
  1083   1084         if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
  1084   1085           /* When setting the auto_vacuum mode to either "full" or 
  1085   1086           ** "incremental", write the value of meta[6] in the database
  1086   1087           ** file. Before writing to meta[6], check that meta[3] indicates
  1087   1088           ** that this really is an auto-vacuum capable database.
  1088   1089           */
         1090  +        static const int iLn = __LINE__+2;
  1089   1091           static const VdbeOpList setMeta6[] = {
  1090   1092             { OP_Transaction,    0,         1,                 0},    /* 0 */
  1091   1093             { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
  1092   1094             { OP_If,             1,         0,                 0},    /* 2 */
  1093   1095             { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
  1094   1096             { OP_Integer,        0,         1,                 0},    /* 4 */
  1095   1097             { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
  1096   1098           };
  1097   1099           int iAddr;
  1098         -        iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
         1100  +        iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
  1099   1101           sqlite3VdbeChangeP1(v, iAddr, iDb);
  1100   1102           sqlite3VdbeChangeP1(v, iAddr+1, iDb);
  1101   1103           sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
  1102   1104           sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
  1103   1105           sqlite3VdbeChangeP1(v, iAddr+5, iDb);
  1104   1106           sqlite3VdbeUsesBtree(v, iDb);
  1105   1107         }
................................................................................
  1117   1119     case PragTyp_INCREMENTAL_VACUUM: {
  1118   1120       int iLimit, addr;
  1119   1121       if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
  1120   1122         iLimit = 0x7fffffff;
  1121   1123       }
  1122   1124       sqlite3BeginWriteOperation(pParse, 0, iDb);
  1123   1125       sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
  1124         -    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
         1126  +    addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
  1125   1127       sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
  1126   1128       sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
  1127         -    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
         1129  +    sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
  1128   1130       sqlite3VdbeJumpHere(v, addr);
  1129   1131       break;
  1130   1132     }
  1131   1133   #endif
  1132   1134   
  1133   1135   #ifndef SQLITE_OMIT_PAGER_PRAGMAS
  1134   1136     /*
................................................................................
  1691   1693             k = 0;
  1692   1694             break;
  1693   1695           }
  1694   1696         }
  1695   1697         assert( pParse->nErr>0 || pFK==0 );
  1696   1698         if( pFK ) break;
  1697   1699         if( pParse->nTab<i ) pParse->nTab = i;
  1698         -      addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
         1700  +      addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
  1699   1701         for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
  1700   1702           pParent = sqlite3FindTable(db, pFK->zTo, zDb);
  1701   1703           pIdx = 0;
  1702   1704           aiCols = 0;
  1703   1705           if( pParent ){
  1704   1706             x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
  1705   1707             assert( x==0 );
................................................................................
  1707   1709           addrOk = sqlite3VdbeMakeLabel(v);
  1708   1710           if( pParent && pIdx==0 ){
  1709   1711             int iKey = pFK->aCol[0].iFrom;
  1710   1712             assert( iKey>=0 && iKey<pTab->nCol );
  1711   1713             if( iKey!=pTab->iPKey ){
  1712   1714               sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
  1713   1715               sqlite3ColumnDefault(v, pTab, iKey, regRow);
  1714         -            sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
  1715         -            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
  1716         -               sqlite3VdbeCurrentAddr(v)+3);
         1716  +            sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
         1717  +            sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow, 
         1718  +               sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
  1717   1719             }else{
  1718   1720               sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
  1719   1721             }
  1720         -          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
         1722  +          sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
  1721   1723             sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
  1722   1724             sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
  1723   1725           }else{
  1724   1726             for(j=0; j<pFK->nCol; j++){
  1725   1727               sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
  1726   1728                               aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
  1727         -            sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
         1729  +            sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
  1728   1730             }
  1729   1731             if( pParent ){
  1730         -            sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
  1731         -            sqlite3VdbeChangeP4(v, -1,
  1732         -                     sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
         1732  +            sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
         1733  +                              sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
  1733   1734               sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
         1735  +            VdbeCoverage(v);
  1734   1736             }
  1735   1737           }
  1736   1738           sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
  1737   1739           sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0, 
  1738   1740                             pFK->zTo, P4_TRANSIENT);
  1739   1741           sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
  1740   1742           sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
  1741   1743           sqlite3VdbeResolveLabel(v, addrOk);
  1742   1744           sqlite3DbFree(db, aiCols);
  1743   1745         }
  1744         -      sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
         1746  +      sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
  1745   1747         sqlite3VdbeJumpHere(v, addrTop);
  1746   1748       }
  1747   1749     }
  1748   1750     break;
  1749   1751   #endif /* !defined(SQLITE_OMIT_TRIGGER) */
  1750   1752   #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
  1751   1753   
................................................................................
  1784   1786     case PragTyp_INTEGRITY_CHECK: {
  1785   1787       int i, j, addr, mxErr;
  1786   1788   
  1787   1789       /* Code that appears at the end of the integrity check.  If no error
  1788   1790       ** messages have been generated, output OK.  Otherwise output the
  1789   1791       ** error message
  1790   1792       */
         1793  +    static const int iLn = __LINE__+2;
  1791   1794       static const VdbeOpList endCode[] = {
  1792   1795         { OP_AddImm,      1, 0,        0},    /* 0 */
  1793   1796         { OP_IfNeg,       1, 0,        0},    /* 1 */
  1794   1797         { OP_String8,     0, 3,        0},    /* 2 */
  1795   1798         { OP_ResultRow,   3, 1,        0},
  1796   1799       };
  1797   1800   
................................................................................
  1832   1835         int cnt = 0;
  1833   1836   
  1834   1837         if( OMIT_TEMPDB && i==1 ) continue;
  1835   1838         if( iDb>=0 && i!=iDb ) continue;
  1836   1839   
  1837   1840         sqlite3CodeVerifySchema(pParse, i);
  1838   1841         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
         1842  +      VdbeCoverage(v);
  1839   1843         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1840   1844         sqlite3VdbeJumpHere(v, addr);
  1841   1845   
  1842   1846         /* Do an integrity check of the B-Tree
  1843   1847         **
  1844   1848         ** Begin by filling registers 2, 3, ... with the root pages numbers
  1845   1849         ** for all tables and indices in the database.
................................................................................
  1863   1867   
  1864   1868         /* Make sure sufficient number of registers have been allocated */
  1865   1869         pParse->nMem = MAX( pParse->nMem, cnt+8 );
  1866   1870   
  1867   1871         /* Do the b-tree integrity checks */
  1868   1872         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
  1869   1873         sqlite3VdbeChangeP5(v, (u8)i);
  1870         -      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
         1874  +      addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
  1871   1875         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1872   1876            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
  1873   1877            P4_DYNAMIC);
  1874   1878         sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
  1875   1879         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
  1876   1880         sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
  1877   1881         sqlite3VdbeJumpHere(v, addr);
................................................................................
  1885   1889           int loopTop;
  1886   1890           int iDataCur, iIdxCur;
  1887   1891           int r1 = -1;
  1888   1892   
  1889   1893           if( pTab->pIndex==0 ) continue;
  1890   1894           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
  1891   1895           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
         1896  +        VdbeCoverage(v);
  1892   1897           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1893   1898           sqlite3VdbeJumpHere(v, addr);
  1894   1899           sqlite3ExprCacheClear(pParse);
  1895   1900           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
  1896   1901                                      1, 0, &iDataCur, &iIdxCur);
  1897   1902           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1898   1903           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1899   1904             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1900   1905           }
  1901   1906           pParse->nMem = MAX(pParse->nMem, 8+j);
  1902         -        sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
         1907  +        sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
  1903   1908           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
  1904   1909           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1905   1910             int jmp2, jmp3, jmp4;
  1906   1911             if( pPk==pIdx ) continue;
  1907   1912             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
  1908   1913                                          pPrior, r1);
  1909   1914             pPrior = pIdx;
  1910   1915             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
  1911   1916             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
  1912         -                                      pIdx->nColumn);
         1917  +                                      pIdx->nColumn); VdbeCoverage(v);
  1913   1918             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
  1914   1919             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
  1915   1920             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
  1916   1921             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
  1917   1922                               P4_STATIC);
  1918   1923             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1919   1924             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
  1920   1925             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1921   1926             sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
  1922         -          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
         1927  +          jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
  1923   1928             sqlite3VdbeAddOp0(v, OP_Halt);
  1924   1929             sqlite3VdbeJumpHere(v, jmp4);
  1925   1930             sqlite3VdbeJumpHere(v, jmp2);
  1926   1931             sqlite3VdbeResolveLabel(v, jmp3);
  1927   1932           }
  1928         -        sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop);
         1933  +        sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
  1929   1934           sqlite3VdbeJumpHere(v, loopTop-1);
  1930   1935   #ifndef SQLITE_OMIT_BTREECOUNT
  1931   1936           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
  1932   1937                        "wrong # of entries in index ", P4_STATIC);
  1933   1938           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1934   1939             if( pPk==pIdx ) continue;
  1935   1940             addr = sqlite3VdbeCurrentAddr(v);
  1936         -          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
         1941  +          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
  1937   1942             sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1938   1943             sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
  1939         -          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3);
         1944  +          sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
  1940   1945             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
  1941   1946             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
  1942   1947             sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
  1943   1948             sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
  1944   1949           }
  1945   1950   #endif /* SQLITE_OMIT_BTREECOUNT */
  1946   1951         } 
  1947   1952       }
  1948         -    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
         1953  +    addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
  1949   1954       sqlite3VdbeChangeP2(v, addr, -mxErr);
  1950   1955       sqlite3VdbeJumpHere(v, addr+1);
  1951   1956       sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
  1952   1957     }
  1953   1958     break;
  1954   1959   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  1955   1960   
................................................................................
  2079   2084       if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
  2080   2085         /* Write the specified cookie value */
  2081   2086         static const VdbeOpList setCookie[] = {
  2082   2087           { OP_Transaction,    0,  1,  0},    /* 0 */
  2083   2088           { OP_Integer,        0,  1,  0},    /* 1 */
  2084   2089           { OP_SetCookie,      0,  0,  1},    /* 2 */
  2085   2090         };
  2086         -      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
         2091  +      int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
  2087   2092         sqlite3VdbeChangeP1(v, addr, iDb);
  2088   2093         sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
  2089   2094         sqlite3VdbeChangeP1(v, addr+2, iDb);
  2090   2095         sqlite3VdbeChangeP2(v, addr+2, iCookie);
  2091   2096       }else{
  2092   2097         /* Read the specified cookie value */
  2093   2098         static const VdbeOpList readCookie[] = {
  2094   2099           { OP_Transaction,     0,  0,  0},    /* 0 */
  2095   2100           { OP_ReadCookie,      0,  1,  0},    /* 1 */
  2096   2101           { OP_ResultRow,       1,  1,  0}
  2097   2102         };
  2098         -      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
         2103  +      int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
  2099   2104         sqlite3VdbeChangeP1(v, addr, iDb);
  2100   2105         sqlite3VdbeChangeP1(v, addr+1, iDb);
  2101   2106         sqlite3VdbeChangeP3(v, addr+1, iCookie);
  2102   2107         sqlite3VdbeSetNumCols(v, 1);
  2103   2108         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
  2104   2109       }
  2105   2110     }

Changes to src/select.c.

   451    451       int addr1, addr2;
   452    452       int iLimit;
   453    453       if( pSelect->iOffset ){
   454    454         iLimit = pSelect->iOffset+1;
   455    455       }else{
   456    456         iLimit = pSelect->iLimit;
   457    457       }
   458         -    addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
          458  +    addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); VdbeCoverage(v);
   459    459       sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
   460    460       addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
   461    461       sqlite3VdbeJumpHere(v, addr1);
   462    462       sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
   463    463       sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
   464    464       sqlite3VdbeJumpHere(v, addr2);
   465    465     }
................................................................................
   472    472     Vdbe *v,          /* Generate code into this VM */
   473    473     int iOffset,      /* Register holding the offset counter */
   474    474     int iContinue     /* Jump here to skip the current record */
   475    475   ){
   476    476     if( iOffset>0 && iContinue!=0 ){
   477    477       int addr;
   478    478       sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
   479         -    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset);
          479  +    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
   480    480       sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
   481    481       VdbeComment((v, "skip OFFSET records"));
   482    482       sqlite3VdbeJumpHere(v, addr);
   483    483     }
   484    484   }
   485    485   
   486    486   /*
................................................................................
   500    500     int iMem           /* First element */
   501    501   ){
   502    502     Vdbe *v;
   503    503     int r1;
   504    504   
   505    505     v = pParse->pVdbe;
   506    506     r1 = sqlite3GetTempReg(pParse);
   507         -  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
          507  +  sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
   508    508     sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
   509    509     sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
   510    510     sqlite3ReleaseTempReg(pParse, r1);
   511    511   }
   512    512   
   513    513   #ifndef SQLITE_OMIT_SUBQUERY
   514    514   /*
................................................................................
   581    581     if( pOrderBy==0 && !hasDistinct ){
   582    582       codeOffset(v, p->iOffset, iContinue);
   583    583     }
   584    584   
   585    585     /* Pull the requested columns.
   586    586     */
   587    587     nResultCol = pEList->nExpr;
          588  +
   588    589     if( pDest->iSdst==0 ){
   589    590       pDest->iSdst = pParse->nMem+1;
   590         -    pDest->nSdst = nResultCol;
          591  +    pParse->nMem += nResultCol;
          592  +  }else if( pDest->iSdst+nResultCol > pParse->nMem ){
          593  +    /* This is an error condition that can result, for example, when a SELECT
          594  +    ** on the right-hand side of an INSERT contains more result columns than
          595  +    ** there are columns in the table on the left.  The error will be caught
          596  +    ** and reported later.  But we need to make sure enough memory is allocated
          597  +    ** to avoid other spurious errors in the meantime. */
   591    598       pParse->nMem += nResultCol;
   592         -  }else{ 
   593         -    assert( pDest->nSdst==nResultCol );
   594    599     }
          600  +  pDest->nSdst = nResultCol;
   595    601     regResult = pDest->iSdst;
   596    602     if( srcTab>=0 ){
   597    603       for(i=0; i<nResultCol; i++){
   598    604         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
   599    605         VdbeComment((v, "%s", pEList->a[i].zName));
   600    606       }
   601    607     }else if( eDest!=SRT_Exists ){
................................................................................
   634    640           pOp->p2 = regPrev;
   635    641   
   636    642           iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
   637    643           for(i=0; i<nResultCol; i++){
   638    644             CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
   639    645             if( i<nResultCol-1 ){
   640    646               sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
          647  +            VdbeCoverage(v);
   641    648             }else{
   642    649               sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
   643         -          }
          650  +            VdbeCoverage(v);
          651  +           }
   644    652             sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
   645    653             sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
   646    654           }
   647    655           assert( sqlite3VdbeCurrentAddr(v)==iJump );
   648    656           sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
   649    657           break;
   650    658         }
................................................................................
   702    710         if( eDest==SRT_DistTable ){
   703    711           /* If the destination is DistTable, then cursor (iParm+1) is open
   704    712           ** on an ephemeral index. If the current row is already present
   705    713           ** in the index, do not write it to the output. If not, add the
   706    714           ** current row to the index and proceed with writing it to the
   707    715           ** output table as well.  */
   708    716           int addr = sqlite3VdbeCurrentAddr(v) + 4;
   709         -        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
          717  +        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
   710    718           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
   711    719           assert( pOrderBy==0 );
   712    720         }
   713    721   #endif
   714    722         if( pOrderBy ){
   715    723           pushOntoSorter(pParse, pOrderBy, p, r1);
   716    724         }else{
................................................................................
   813    821         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
   814    822         if( eDest==SRT_DistQueue ){
   815    823           /* If the destination is DistQueue, then cursor (iParm+1) is open
   816    824           ** on a second ephemeral index that holds all values every previously
   817    825           ** added to the queue.  Only add this new value if it has never before
   818    826           ** been added */
   819    827           addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, r3, 0);
          828  +        VdbeCoverage(v);
   820    829           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
   821    830           sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
   822    831         }
   823    832         for(i=0; i<nKey; i++){
   824    833           sqlite3VdbeAddOp2(v, OP_SCopy,
   825    834                             regResult + pSO->a[i].u.x.iOrderByCol - 1,
   826    835                             r2+i);
................................................................................
   851    860     }
   852    861   
   853    862     /* Jump to the end of the loop if the LIMIT is reached.  Except, if
   854    863     ** there is a sorter, in which case the sorter has already limited
   855    864     ** the output for us.
   856    865     */
   857    866     if( pOrderBy==0 && p->iLimit ){
   858         -    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
          867  +    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
   859    868     }
   860    869   }
   861    870   
   862    871   /*
   863    872   ** Allocate a KeyInfo object sufficient for an index of N key columns and
   864    873   ** X extra columns.
   865    874   */
................................................................................
  1070   1079       regRowid = sqlite3GetTempReg(pParse);
  1071   1080     }
  1072   1081     if( p->selFlags & SF_UseSorter ){
  1073   1082       int regSortOut = ++pParse->nMem;
  1074   1083       int ptab2 = pParse->nTab++;
  1075   1084       sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
  1076   1085       addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
         1086  +    VdbeCoverage(v);
  1077   1087       codeOffset(v, p->iOffset, addrContinue);
  1078   1088       sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
  1079   1089       sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
  1080   1090       sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
  1081   1091     }else{
  1082         -    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
         1092  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
  1083   1093       codeOffset(v, p->iOffset, addrContinue);
  1084   1094       sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
  1085   1095     }
  1086   1096     switch( eDest ){
  1087   1097       case SRT_Table:
  1088   1098       case SRT_EphemTab: {
  1089   1099         testcase( eDest==SRT_Table );
................................................................................
  1133   1143     sqlite3ReleaseTempReg(pParse, regRow);
  1134   1144     sqlite3ReleaseTempReg(pParse, regRowid);
  1135   1145   
  1136   1146     /* The bottom of the loop
  1137   1147     */
  1138   1148     sqlite3VdbeResolveLabel(v, addrContinue);
  1139   1149     if( p->selFlags & SF_UseSorter ){
  1140         -    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
         1150  +    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
  1141   1151     }else{
  1142         -    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
         1152  +    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
  1143   1153     }
  1144   1154     sqlite3VdbeResolveLabel(v, addrBreak);
  1145   1155     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
  1146   1156       sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
  1147   1157     }
  1148   1158   }
  1149   1159   
................................................................................
  1683   1693         if( n==0 ){
  1684   1694           sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
  1685   1695         }else if( n>=0 && p->nSelectRow>(u64)n ){
  1686   1696           p->nSelectRow = n;
  1687   1697         }
  1688   1698       }else{
  1689   1699         sqlite3ExprCode(pParse, p->pLimit, iLimit);
  1690         -      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
         1700  +      sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
  1691   1701         VdbeComment((v, "LIMIT counter"));
  1692         -      sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
         1702  +      sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak); VdbeCoverage(v);
  1693   1703       }
  1694   1704       if( p->pOffset ){
  1695   1705         p->iOffset = iOffset = ++pParse->nMem;
  1696   1706         pParse->nMem++;   /* Allocate an extra register for limit+offset */
  1697   1707         sqlite3ExprCode(pParse, p->pOffset, iOffset);
  1698         -      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
         1708  +      sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
  1699   1709         VdbeComment((v, "OFFSET counter"));
  1700         -      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
         1710  +      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); VdbeCoverage(v);
  1701   1711         sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
  1702   1712         sqlite3VdbeJumpHere(v, addr1);
  1703   1713         sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
  1704   1714         VdbeComment((v, "LIMIT+OFFSET"));
  1705         -      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
         1715  +      addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); VdbeCoverage(v);
  1706   1716         sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
  1707   1717         sqlite3VdbeJumpHere(v, addr1);
  1708   1718       }
  1709   1719     }
  1710   1720   }
  1711   1721   
  1712   1722   #ifndef SQLITE_OMIT_COMPOUND_SELECT
................................................................................
  1887   1897     /* Store the results of the setup-query in Queue. */
  1888   1898     pSetup->pNext = 0;
  1889   1899     rc = sqlite3Select(pParse, pSetup, &destQueue);
  1890   1900     pSetup->pNext = p;
  1891   1901     if( rc ) goto end_of_recursive_query;
  1892   1902   
  1893   1903     /* Find the next row in the Queue and output that row */
  1894         -  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak);
         1904  +  addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
  1895   1905   
  1896   1906     /* Transfer the next row in Queue over to Current */
  1897   1907     sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
  1898   1908     if( pOrderBy ){
  1899   1909       sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
  1900   1910     }else{
  1901   1911       sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
................................................................................
  1903   1913     sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
  1904   1914   
  1905   1915     /* Output the single row in Current */
  1906   1916     addrCont = sqlite3VdbeMakeLabel(v);
  1907   1917     codeOffset(v, regOffset, addrCont);
  1908   1918     selectInnerLoop(pParse, p, p->pEList, iCurrent,
  1909   1919         0, 0, pDest, addrCont, addrBreak);
  1910         -  if( regLimit ) sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
         1920  +  if( regLimit ){
         1921  +    sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
         1922  +    VdbeCoverage(v);
         1923  +  }
  1911   1924     sqlite3VdbeResolveLabel(v, addrCont);
  1912   1925   
  1913   1926     /* Execute the recursive SELECT taking the single row in Current as
  1914   1927     ** the value for the recursive-table. Store the results in the Queue.
  1915   1928     */
  1916   1929     p->pPrior = 0;
  1917   1930     sqlite3Select(pParse, p, &destQueue);
................................................................................
  2063   2076         if( rc ){
  2064   2077           goto multi_select_end;
  2065   2078         }
  2066   2079         p->pPrior = 0;
  2067   2080         p->iLimit = pPrior->iLimit;
  2068   2081         p->iOffset = pPrior->iOffset;
  2069   2082         if( p->iLimit ){
  2070         -        addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
         2083  +        addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit); VdbeCoverage(v);
  2071   2084           VdbeComment((v, "Jump ahead if LIMIT reached"));
  2072   2085         }
  2073   2086         explainSetInteger(iSub2, pParse->iNextSelectId);
  2074   2087         rc = sqlite3Select(pParse, p, &dest);
  2075   2088         testcase( rc!=SQLITE_OK );
  2076   2089         pDelete = p->pPrior;
  2077   2090         p->pPrior = pPrior;
................................................................................
  2170   2183             Select *pFirst = p;
  2171   2184             while( pFirst->pPrior ) pFirst = pFirst->pPrior;
  2172   2185             generateColumnNames(pParse, 0, pFirst->pEList);
  2173   2186           }
  2174   2187           iBreak = sqlite3VdbeMakeLabel(v);
  2175   2188           iCont = sqlite3VdbeMakeLabel(v);
  2176   2189           computeLimitRegisters(pParse, p, iBreak);
  2177         -        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
         2190  +        sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
  2178   2191           iStart = sqlite3VdbeCurrentAddr(v);
  2179   2192           selectInnerLoop(pParse, p, p->pEList, unionTab,
  2180   2193                           0, 0, &dest, iCont, iBreak);
  2181   2194           sqlite3VdbeResolveLabel(v, iCont);
  2182         -        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
         2195  +        sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
  2183   2196           sqlite3VdbeResolveLabel(v, iBreak);
  2184   2197           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
  2185   2198         }
  2186   2199         break;
  2187   2200       }
  2188   2201       default: assert( p->op==TK_INTERSECT ); {
  2189   2202         int tab1, tab2;
................................................................................
  2245   2258           Select *pFirst = p;
  2246   2259           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
  2247   2260           generateColumnNames(pParse, 0, pFirst->pEList);
  2248   2261         }
  2249   2262         iBreak = sqlite3VdbeMakeLabel(v);
  2250   2263         iCont = sqlite3VdbeMakeLabel(v);
  2251   2264         computeLimitRegisters(pParse, p, iBreak);
  2252         -      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
         2265  +      sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
  2253   2266         r1 = sqlite3GetTempReg(pParse);
  2254   2267         iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
  2255         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
         2268  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
  2256   2269         sqlite3ReleaseTempReg(pParse, r1);
  2257   2270         selectInnerLoop(pParse, p, p->pEList, tab1,
  2258   2271                         0, 0, &dest, iCont, iBreak);
  2259   2272         sqlite3VdbeResolveLabel(v, iCont);
  2260         -      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
         2273  +      sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
  2261   2274         sqlite3VdbeResolveLabel(v, iBreak);
  2262   2275         sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
  2263   2276         sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
  2264   2277         break;
  2265   2278       }
  2266   2279     }
  2267   2280   
................................................................................
  2360   2373     addr = sqlite3VdbeCurrentAddr(v);
  2361   2374     iContinue = sqlite3VdbeMakeLabel(v);
  2362   2375   
  2363   2376     /* Suppress duplicates for UNION, EXCEPT, and INTERSECT 
  2364   2377     */
  2365   2378     if( regPrev ){
  2366   2379       int j1, j2;
  2367         -    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
         2380  +    j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
  2368   2381       j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
  2369   2382                                 (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
  2370         -    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
         2383  +    sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2); VdbeCoverage(v);
  2371   2384       sqlite3VdbeJumpHere(v, j1);
  2372   2385       sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
  2373   2386       sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
  2374   2387     }
  2375   2388     if( pParse->db->mallocFailed ) return 0;
  2376   2389   
  2377   2390     /* Suppress the first OFFSET entries if there is an OFFSET clause
................................................................................
  2464   2477         break;
  2465   2478       }
  2466   2479     }
  2467   2480   
  2468   2481     /* Jump to the end of the loop if the LIMIT is reached.
  2469   2482     */
  2470   2483     if( p->iLimit ){
  2471         -    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
         2484  +    sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
  2472   2485     }
  2473   2486   
  2474   2487     /* Generate the subroutine return
  2475   2488     */
  2476   2489     sqlite3VdbeResolveLabel(v, iContinue);
  2477   2490     sqlite3VdbeAddOp1(v, OP_Return, regReturn);
  2478   2491   
................................................................................
  2780   2793     */
  2781   2794     if( op==TK_EXCEPT || op==TK_INTERSECT ){
  2782   2795       addrEofA_noB = addrEofA = labelEnd;
  2783   2796     }else{  
  2784   2797       VdbeNoopComment((v, "eof-A subroutine"));
  2785   2798       addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  2786   2799       addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
         2800  +                                     VdbeCoverage(v);
  2787   2801       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
  2788   2802       p->nSelectRow += pPrior->nSelectRow;
  2789   2803     }
  2790   2804   
  2791   2805     /* Generate a subroutine to run when the results from select B
  2792   2806     ** are exhausted and only data in select A remains.
  2793   2807     */
  2794   2808     if( op==TK_INTERSECT ){
  2795   2809       addrEofB = addrEofA;
  2796   2810       if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
  2797   2811     }else{  
  2798   2812       VdbeNoopComment((v, "eof-B subroutine"));
  2799   2813       addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  2800         -    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd);
         2814  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
  2801   2815       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
  2802   2816     }
  2803   2817   
  2804   2818     /* Generate code to handle the case of A<B
  2805   2819     */
  2806   2820     VdbeNoopComment((v, "A-lt-B subroutine"));
  2807   2821     addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
  2808         -  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA);
         2822  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
  2809   2823     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2810   2824   
  2811   2825     /* Generate code to handle the case of A==B
  2812   2826     */
  2813   2827     if( op==TK_ALL ){
  2814   2828       addrAeqB = addrAltB;
  2815   2829     }else if( op==TK_INTERSECT ){
  2816   2830       addrAeqB = addrAltB;
  2817   2831       addrAltB++;
  2818   2832     }else{
  2819   2833       VdbeNoopComment((v, "A-eq-B subroutine"));
  2820   2834       addrAeqB =
  2821         -    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA);
         2835  +    sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
  2822   2836       sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2823   2837     }
  2824   2838   
  2825   2839     /* Generate code to handle the case of A>B
  2826   2840     */
  2827   2841     VdbeNoopComment((v, "A-gt-B subroutine"));
  2828   2842     addrAgtB = sqlite3VdbeCurrentAddr(v);
  2829   2843     if( op==TK_ALL || op==TK_UNION ){
  2830   2844       sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
  2831   2845     }
  2832         -  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB);
         2846  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
  2833   2847     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
  2834   2848   
  2835   2849     /* This code runs once to initialize everything.
  2836   2850     */
  2837   2851     sqlite3VdbeJumpHere(v, j1);
  2838         -  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB);
  2839         -  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB);
         2852  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
         2853  +  sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
  2840   2854   
  2841   2855     /* Implement the main merge loop
  2842   2856     */
  2843   2857     sqlite3VdbeResolveLabel(v, labelCmpr);
  2844   2858     sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
  2845   2859     sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
  2846   2860                            (char*)pKeyMerge, P4_KEYINFO);
  2847   2861     sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
  2848         -  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
         2862  +  sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
  2849   2863   
  2850   2864     /* Jump to the this point in order to terminate the query.
  2851   2865     */
  2852   2866     sqlite3VdbeResolveLabel(v, labelEnd);
  2853   2867   
  2854   2868     /* Set the number of output columns
  2855   2869     */
................................................................................
  4381   4395     ** may have been used, invalidating the underlying buffer holding the
  4382   4396     ** text or blob value. See ticket [883034dcb5].
  4383   4397     **
  4384   4398     ** Another solution would be to change the OP_SCopy used to copy cached
  4385   4399     ** values to an OP_Copy.
  4386   4400     */
  4387   4401     if( regHit ){
  4388         -    addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit);
         4402  +    addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
  4389   4403     }
  4390   4404     sqlite3ExprCacheClear(pParse);
  4391   4405     for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
  4392   4406       sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
  4393   4407     }
  4394   4408     pAggInfo->directMode = 0;
  4395   4409     sqlite3ExprCacheClear(pParse);
................................................................................
  4915   4929           sqlite3ReleaseTempReg(pParse, regRecord);
  4916   4930           sqlite3ReleaseTempRange(pParse, regBase, nCol);
  4917   4931           sqlite3WhereEnd(pWInfo);
  4918   4932           sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
  4919   4933           sortOut = sqlite3GetTempReg(pParse);
  4920   4934           sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
  4921   4935           sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
  4922         -        VdbeComment((v, "GROUP BY sort"));
         4936  +        VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
  4923   4937           sAggInfo.useSortingIdx = 1;
  4924   4938           sqlite3ExprCacheClear(pParse);
  4925   4939         }
  4926   4940   
  4927   4941         /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
  4928   4942         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
  4929   4943         ** Then compare the current GROUP BY terms against the GROUP BY terms
................................................................................
  4942   4956             sAggInfo.directMode = 1;
  4943   4957             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
  4944   4958           }
  4945   4959         }
  4946   4960         sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
  4947   4961                             (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
  4948   4962         j1 = sqlite3VdbeCurrentAddr(v);
  4949         -      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
         4963  +      sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1); VdbeCoverage(v);
  4950   4964   
  4951   4965         /* Generate code that runs whenever the GROUP BY changes.
  4952   4966         ** Changes in the GROUP BY are detected by the previous code
  4953   4967         ** block.  If there were no changes, this block is skipped.
  4954   4968         **
  4955   4969         ** This code copies current group by terms in b0,b1,b2,...
  4956   4970         ** over to a0,a1,a2.  It then calls the output subroutine
  4957   4971         ** and resets the aggregate accumulator registers in preparation
  4958   4972         ** for the next GROUP BY batch.
  4959   4973         */
  4960   4974         sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
  4961   4975         sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
  4962   4976         VdbeComment((v, "output one row"));
  4963         -      sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
         4977  +      sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
  4964   4978         VdbeComment((v, "check abort flag"));
  4965   4979         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
  4966   4980         VdbeComment((v, "reset accumulator"));
  4967   4981   
  4968   4982         /* Update the aggregate accumulators based on the content of
  4969   4983         ** the current row
  4970   4984         */
................................................................................
  4973   4987         sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
  4974   4988         VdbeComment((v, "indicate data in accumulator"));
  4975   4989   
  4976   4990         /* End of the loop
  4977   4991         */
  4978   4992         if( groupBySort ){
  4979   4993           sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
         4994  +        VdbeCoverage(v);
  4980   4995         }else{
  4981   4996           sqlite3WhereEnd(pWInfo);
  4982   4997           sqlite3VdbeChangeToNoop(v, addrSortingIdx);
  4983   4998         }
  4984   4999   
  4985   5000         /* Output the final row of result
  4986   5001         */
................................................................................
  5000   5015         */
  5001   5016         addrSetAbort = sqlite3VdbeCurrentAddr(v);
  5002   5017         sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
  5003   5018         VdbeComment((v, "set abort flag"));
  5004   5019         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5005   5020         sqlite3VdbeResolveLabel(v, addrOutputRow);
  5006   5021         addrOutputRow = sqlite3VdbeCurrentAddr(v);
  5007         -      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
         5022  +      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
  5008   5023         VdbeComment((v, "Groupby result generator entry point"));
  5009   5024         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5010   5025         finalizeAggFunctions(pParse, &sAggInfo);
  5011   5026         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
  5012   5027         selectInnerLoop(pParse, p, p->pEList, -1, pOrderBy,
  5013   5028                         &sDistinct, pDest,
  5014   5029                         addrOutputRow+1, addrSetAbort);

Changes to src/sqlite.h.in.

  6113   6113   #define SQLITE_TESTCTRL_RESERVE                 14
  6114   6114   #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
  6115   6115   #define SQLITE_TESTCTRL_ISKEYWORD               16
  6116   6116   #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
  6117   6117   #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
  6118   6118   #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
  6119   6119   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  6120         -#define SQLITE_TESTCTRL_LAST                    20
         6120  +#define SQLITE_TESTCTRL_VDBE_COVERAGE           21
         6121  +#define SQLITE_TESTCTRL_LAST                    21
  6121   6122   
  6122   6123   /*
  6123   6124   ** CAPI3REF: SQLite Runtime Status
  6124   6125   **
  6125   6126   ** ^This interface is used to retrieve runtime status information
  6126   6127   ** about the performance of SQLite, and optionally to reset various
  6127   6128   ** highwater marks.  ^The first argument is an integer code for

Changes to src/sqliteInt.h.

  2653   2653     void (*xLog)(void*,int,const char*); /* Function for logging */
  2654   2654     void *pLogArg;                       /* First argument to xLog() */
  2655   2655     int bLocaltimeFault;              /* True to fail localtime() calls */
  2656   2656   #ifdef SQLITE_ENABLE_SQLLOG
  2657   2657     void(*xSqllog)(void*,sqlite3*,const char*, int);
  2658   2658     void *pSqllogArg;
  2659   2659   #endif
         2660  +#ifdef SQLITE_VDBE_COVERAGE
         2661  +  /* The following callback (if not NULL) is invoked on every VDBE branch
         2662  +  ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
         2663  +  */
         2664  +  void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
         2665  +  void *pVdbeBranchArg;                                     /* 1st argument */
         2666  +#endif
  2660   2667   };
  2661   2668   
  2662   2669   /*
  2663   2670   ** This macro is used inside of assert() statements to indicate that
  2664   2671   ** the assert is only valid on a well-formed database.  Instead of:
  2665   2672   **
  2666   2673   **     assert( X );
................................................................................
  2986   2993   #ifndef SQLITE_OMIT_AUTOINCREMENT
  2987   2994     void sqlite3AutoincrementBegin(Parse *pParse);
  2988   2995     void sqlite3AutoincrementEnd(Parse *pParse);
  2989   2996   #else
  2990   2997   # define sqlite3AutoincrementBegin(X)
  2991   2998   # define sqlite3AutoincrementEnd(X)
  2992   2999   #endif
  2993         -int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
  2994   3000   void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
  2995   3001   void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  2996   3002   IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  2997   3003   int sqlite3IdListIndex(IdList*,const char*);
  2998   3004   SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
  2999   3005   SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
  3000   3006   SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
................................................................................
  3034   3040   void sqlite3ExprCodeMove(Parse*, int, int, int);
  3035   3041   void sqlite3ExprCacheStore(Parse*, int, int, int);
  3036   3042   void sqlite3ExprCachePush(Parse*);
  3037   3043   void sqlite3ExprCachePop(Parse*, int);
  3038   3044   void sqlite3ExprCacheRemove(Parse*, int, int);
  3039   3045   void sqlite3ExprCacheClear(Parse*);
  3040   3046   void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  3041         -int sqlite3ExprCode(Parse*, Expr*, int);
         3047  +void sqlite3ExprCode(Parse*, Expr*, int);
         3048  +void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
  3042   3049   void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
  3043   3050   int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
  3044   3051   int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  3045         -int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
         3052  +void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  3046   3053   int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
  3047   3054   #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
  3048   3055   #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
  3049   3056   void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  3050   3057   void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  3051   3058   Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  3052   3059   Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
................................................................................
  3220   3227     (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
  3221   3228     sqlite3PutVarint32((A),(B)))
  3222   3229   #define getVarint    sqlite3GetVarint
  3223   3230   #define putVarint    sqlite3PutVarint
  3224   3231   
  3225   3232   
  3226   3233   const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
  3227         -void sqlite3TableAffinityStr(Vdbe *, Table *);
         3234  +void sqlite3TableAffinity(Vdbe*, Table*, int);
  3228   3235   char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  3229   3236   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  3230   3237   char sqlite3ExprAffinity(Expr *pExpr);
  3231   3238   int sqlite3Atoi64(const char*, i64*, int, u8);
  3232   3239   void sqlite3Error(sqlite3*, int, const char*,...);
  3233   3240   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3234   3241   u8 sqlite3HexToInt(int h);

Changes to src/trigger.c.

   562    562   #endif
   563    563   
   564    564     /* Generate code to destroy the database record of the trigger.
   565    565     */
   566    566     assert( pTable!=0 );
   567    567     if( (v = sqlite3GetVdbe(pParse))!=0 ){
   568    568       int base;
          569  +    static const int iLn = __LINE__+2;
   569    570       static const VdbeOpList dropTrigger[] = {
   570    571         { OP_Rewind,     0, ADDR(9),  0},
   571    572         { OP_String8,    0, 1,        0}, /* 1 */
   572    573         { OP_Column,     0, 1,        2},
   573    574         { OP_Ne,         2, ADDR(8),  1},
   574    575         { OP_String8,    0, 1,        0}, /* 4: "trigger" */
   575    576         { OP_Column,     0, 0,        2},
................................................................................
   576    577         { OP_Ne,         2, ADDR(8),  1},
   577    578         { OP_Delete,     0, 0,        0},
   578    579         { OP_Next,       0, ADDR(1),  0}, /* 8 */
   579    580       };
   580    581   
   581    582       sqlite3BeginWriteOperation(pParse, 0, iDb);
   582    583       sqlite3OpenMasterTable(pParse, iDb);
   583         -    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
          584  +    base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger, iLn);
   584    585       sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
   585    586       sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
   586    587       sqlite3ChangeCookie(pParse, iDb);
   587    588       sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
   588    589       sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
   589    590       if( pParse->nMem<3 ){
   590    591         pParse->nMem = 3;

Changes to src/update.c.

   386    386       }
   387    387       if( okOnePass ){
   388    388         sqlite3VdbeChangeToNoop(v, addrOpen);
   389    389         nKey = nPk;
   390    390         regKey = iPk;
   391    391       }else{
   392    392         sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
   393         -                        sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
          393  +                        sqlite3IndexAffinityStr(v, pPk), nPk);
   394    394         sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
   395    395       }
   396    396       sqlite3WhereEnd(pWInfo);
   397    397     }
   398    398   
   399    399     /* Initialize the count of updated rows
   400    400     */
................................................................................
   430    430     }
   431    431   
   432    432     /* Top of the update loop */
   433    433     if( okOnePass ){
   434    434       if( aToOpen[iDataCur-iBaseCur] ){
   435    435         assert( pPk!=0 );
   436    436         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
          437  +      VdbeCoverage(v);
   437    438       }
   438    439       labelContinue = labelBreak;
   439    440       sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
          441  +    VdbeCoverage(v);
   440    442     }else if( pPk ){
   441    443       labelContinue = sqlite3VdbeMakeLabel(v);
   442         -    sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak);
          444  +    sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
   443    445       addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
   444    446       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
          447  +    VdbeCoverage(v);
   445    448     }else{
   446    449       labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
   447    450                                regOldRowid);
          451  +    VdbeCoverage(v);
   448    452       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
          453  +    VdbeCoverage(v);
   449    454     }
   450    455   
   451    456     /* If the record number will change, set register regNewRowid to
   452    457     ** contain the new value. If the record number is not being modified,
   453    458     ** then regNewRowid is the same register as regOldRowid, which is
   454    459     ** already populated.  */
   455    460     assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
   456    461     if( chngRowid ){
   457    462       sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
   458         -    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
          463  +    sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
   459    464     }
   460    465   
   461    466     /* Compute the old pre-UPDATE content of the row being changed, if that
   462    467     ** information is needed */
   463    468     if( chngPk || hasFK || pTrigger ){
   464    469       u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
   465    470       oldmask |= sqlite3TriggerColmask(pParse, 
................................................................................
   520    525       }
   521    526     }
   522    527   
   523    528     /* Fire any BEFORE UPDATE triggers. This happens before constraints are
   524    529     ** verified. One could argue that this is wrong.
   525    530     */
   526    531     if( tmask&TRIGGER_BEFORE ){
   527         -    sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
   528         -    sqlite3TableAffinityStr(v, pTab);
          532  +    sqlite3TableAffinity(v, pTab, regNew);
   529    533       sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, 
   530    534           TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
   531    535   
   532    536       /* The row-trigger may have deleted the row being updated. In this
   533    537       ** case, jump to the next row. No updates or AFTER triggers are 
   534    538       ** required. This behavior - what happens when the row being updated
   535    539       ** is deleted or renamed by a BEFORE trigger - is left undefined in the
   536    540       ** documentation.
   537    541       */
   538    542       if( pPk ){
   539    543         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
          544  +      VdbeCoverage(v);
   540    545       }else{
   541    546         sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
          547  +      VdbeCoverage(v);
   542    548       }
   543    549   
   544    550       /* If it did not delete it, the row-trigger may still have modified 
   545    551       ** some of the columns of the row being updated. Load the values for 
   546    552       ** all columns not modified by the update statement into their 
   547    553       ** registers in case this has happened.
   548    554       */
................................................................................
   567    573         sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
   568    574       }
   569    575   
   570    576       /* Delete the index entries associated with the current record.  */
   571    577       if( bReplace || chngKey ){
   572    578         if( pPk ){
   573    579           j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
          580  +        VdbeCoverage(v);
   574    581         }else{
   575    582           j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
          583  +        VdbeCoverage(v);
   576    584         }
   577    585       }
   578    586       sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx);
   579    587     
   580    588       /* If changing the record number, delete the old record.  */
   581    589       if( hasFK || chngKey || pPk!=0 ){
   582    590         sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
................................................................................
   613    621     /* Repeat the above with the next record to be updated, until
   614    622     ** all record selected by the WHERE clause have been updated.
   615    623     */
   616    624     if( okOnePass ){
   617    625       /* Nothing to do at end-of-loop for a single-pass */
   618    626     }else if( pPk ){
   619    627       sqlite3VdbeResolveLabel(v, labelContinue);
   620         -    sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop);
          628  +    sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
   621    629     }else{
   622    630       sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
   623    631     }
   624    632     sqlite3VdbeResolveLabel(v, labelBreak);
   625    633   
   626    634     /* Close all tables */
   627    635     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
................................................................................
   742    750     */
   743    751     sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
   744    752     sqlite3Select(pParse, pSelect, &dest);
   745    753   
   746    754     /* Generate code to scan the ephemeral table and call VUpdate. */
   747    755     iReg = ++pParse->nMem;
   748    756     pParse->nMem += pTab->nCol+1;
   749         -  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
          757  +  addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v);
   750    758     sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
   751    759     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
   752    760     for(i=0; i<pTab->nCol; i++){
   753    761       sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
   754    762     }
   755    763     sqlite3VtabMakeWritable(pParse, pTab);
   756    764     sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
   757    765     sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
   758    766     sqlite3MayAbort(pParse);
   759         -  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
          767  +  sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
   760    768     sqlite3VdbeJumpHere(v, addr);
   761    769     sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
   762    770   
   763    771     /* Cleanup */
   764    772     sqlite3SelectDelete(db, pSelect);  
   765    773   }
   766    774   #endif /* SQLITE_OMIT_VIRTUALTABLE */

Changes to src/vdbe.c.

   103    103   */
   104    104   #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
   105    105   # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
   106    106   #else
   107    107   # define UPDATE_MAX_BLOBSIZE(P)
   108    108   #endif
   109    109   
          110  +/*
          111  +** Invoke the VDBE coverage callback, if defined
          112  +*/
          113  +#if !defined(SQLITE_VDBE_COVERAGE)
          114  +# define VdbeBranchTaken(I,M)
          115  +#else
          116  +# define VdbeBranchTaken(I,M) \
          117  +    if( sqlite3GlobalConfig.xVdbeBranch!=0 ){ \
          118  +      sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg, \
          119  +                                      pOp->iSrcLine,(I),(M)); }
          120  +#endif
          121  +
   110    122   /*
   111    123   ** Convert the given register into a string if it isn't one
   112    124   ** already. Return non-zero if a malloc() fails.
   113    125   */
   114    126   #define Stringify(P, enc) \
   115    127      if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
   116    128        { goto no_mem; }
................................................................................
   990   1002       VdbeMemRelease(pOut);
   991   1003       pOut->flags = nullFlag;
   992   1004       cnt--;
   993   1005     }
   994   1006     break;
   995   1007   }
   996   1008   
         1009  +/* Opcode: SoftNull P1 * * * *
         1010  +** Synopsis:  r[P1]=NULL
         1011  +**
         1012  +** Set register P1 to have the value NULL as seen by the OP_MakeRecord
         1013  +** instruction, but do not free any string or blob memory associated with
         1014  +** the register, so that if the value was a string or blob that was
         1015  +** previously copied using OP_SCopy, the copies will continue to be valid.
         1016  +*/
         1017  +case OP_SoftNull: {
         1018  +  assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
         1019  +  pOut = &aMem[pOp->p1];
         1020  +  pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
         1021  +  break;
         1022  +}
   997   1023   
   998   1024   /* Opcode: Blob P1 P2 * P4 *
   999   1025   ** Synopsis: r[P2]=P4 (len=P1)
  1000   1026   **
  1001   1027   ** P4 points to a blob of data P1 bytes long.  Store this
  1002   1028   ** blob in register P2.
  1003   1029   */
................................................................................
  1620   1646   ** without data loss, then jump immediately to P2, or if P2==0
  1621   1647   ** raise an SQLITE_MISMATCH exception.
  1622   1648   */
  1623   1649   case OP_MustBeInt: {            /* jump, in1 */
  1624   1650     pIn1 = &aMem[pOp->p1];
  1625   1651     if( (pIn1->flags & MEM_Int)==0 ){
  1626   1652       applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
         1653  +    VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
  1627   1654       if( (pIn1->flags & MEM_Int)==0 ){
  1628   1655         if( pOp->p2==0 ){
  1629   1656           rc = SQLITE_MISMATCH;
  1630   1657           goto abort_due_to_error;
  1631   1658         }else{
  1632   1659           pc = pOp->p2 - 1;
  1633   1660           break;
................................................................................
  1873   1900           res = 1;  /* Results are not equal */
  1874   1901         }
  1875   1902       }else{
  1876   1903         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
  1877   1904         ** then the result is always NULL.
  1878   1905         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
  1879   1906         */
  1880         -      if( pOp->p5 & SQLITE_JUMPIFNULL ){
  1881         -        pc = pOp->p2-1;
  1882         -      }else if( pOp->p5 & SQLITE_STOREP2 ){
         1907  +      if( pOp->p5 & SQLITE_STOREP2 ){
  1883   1908           pOut = &aMem[pOp->p2];
  1884   1909           MemSetTypeFlag(pOut, MEM_Null);
  1885   1910           REGISTER_TRACE(pOp->p2, pOut);
         1911  +      }else{
         1912  +        VdbeBranchTaken((pOp->p5 & SQLITE_JUMPIFNULL)?2:3,4);
         1913  +        if( pOp->p5 & SQLITE_JUMPIFNULL ){
         1914  +          pc = pOp->p2-1;
         1915  +        }
  1886   1916         }
  1887   1917         break;
  1888   1918       }
  1889   1919     }else{
  1890   1920       /* Neither operand is NULL.  Do a comparison. */
  1891   1921       affinity = pOp->p5 & SQLITE_AFF_MASK;
  1892   1922       if( affinity ){
................................................................................
  1911   1941   
  1912   1942     if( pOp->p5 & SQLITE_STOREP2 ){
  1913   1943       pOut = &aMem[pOp->p2];
  1914   1944       memAboutToChange(p, pOut);
  1915   1945       MemSetTypeFlag(pOut, MEM_Int);
  1916   1946       pOut->u.i = res;
  1917   1947       REGISTER_TRACE(pOp->p2, pOut);
  1918         -  }else if( res ){
  1919         -    pc = pOp->p2-1;
         1948  +  }else{
         1949  +    VdbeBranchTaken(res!=0, 4);
         1950  +    if( res ){
         1951  +      pc = pOp->p2-1;
         1952  +    }
  1920   1953     }
  1921         -
  1922   1954     /* Undo any changes made by applyAffinity() to the input registers. */
  1923   1955     pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
  1924   1956     pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
  1925   1957     break;
  1926   1958   }
  1927   1959   
  1928   1960   /* Opcode: Permutation * * * P4 *
................................................................................
  2011   2043   **
  2012   2044   ** Jump to the instruction at address P1, P2, or P3 depending on whether
  2013   2045   ** in the most recent OP_Compare instruction the P1 vector was less than
  2014   2046   ** equal to, or greater than the P2 vector, respectively.
  2015   2047   */
  2016   2048   case OP_Jump: {             /* jump */
  2017   2049     if( iCompare<0 ){
  2018         -    pc = pOp->p1 - 1;
         2050  +    pc = pOp->p1 - 1;  VdbeBranchTaken(0,3);
  2019   2051     }else if( iCompare==0 ){
  2020         -    pc = pOp->p2 - 1;
         2052  +    pc = pOp->p2 - 1;  VdbeBranchTaken(1,3);
  2021   2053     }else{
  2022         -    pc = pOp->p3 - 1;
         2054  +    pc = pOp->p3 - 1;  VdbeBranchTaken(2,3);
  2023   2055     }
  2024   2056     break;
  2025   2057   }
  2026   2058   
  2027   2059   /* Opcode: And P1 P2 P3 * *
  2028   2060   ** Synopsis: r[P3]=(r[P1] && r[P2])
  2029   2061   **
................................................................................
  2119   2151   ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
  2120   2152   ** set the flag and fall through to the next instruction.  In other words,
  2121   2153   ** this opcode causes all following up codes up through P2 (but not including
  2122   2154   ** P2) to run just once and skipped on subsequent times through the loop.
  2123   2155   */
  2124   2156   case OP_Once: {             /* jump */
  2125   2157     assert( pOp->p1<p->nOnceFlag );
         2158  +  VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
  2126   2159     if( p->aOnceFlag[pOp->p1] ){
  2127   2160       pc = pOp->p2-1;
  2128   2161     }else{
  2129   2162       p->aOnceFlag[pOp->p1] = 1;
  2130   2163     }
  2131   2164     break;
  2132   2165   }
................................................................................
  2153   2186   #ifdef SQLITE_OMIT_FLOATING_POINT
  2154   2187       c = sqlite3VdbeIntValue(pIn1)!=0;
  2155   2188   #else
  2156   2189       c = sqlite3VdbeRealValue(pIn1)!=0.0;
  2157   2190   #endif
  2158   2191       if( pOp->opcode==OP_IfNot ) c = !c;
  2159   2192     }
         2193  +  VdbeBranchTaken(c!=0, 2);
  2160   2194     if( c ){
  2161   2195       pc = pOp->p2-1;
  2162   2196     }
  2163   2197     break;
  2164   2198   }
  2165   2199   
  2166   2200   /* Opcode: IsNull P1 P2 * * *
  2167   2201   ** Synopsis:  if r[P1]==NULL goto P2
  2168   2202   **
  2169   2203   ** Jump to P2 if the value in register P1 is NULL.
  2170   2204   */
  2171   2205   case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
  2172   2206     pIn1 = &aMem[pOp->p1];
         2207  +  VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
  2173   2208     if( (pIn1->flags & MEM_Null)!=0 ){
  2174   2209       pc = pOp->p2 - 1;
  2175   2210     }
  2176   2211     break;
  2177   2212   }
  2178   2213   
  2179   2214   /* Opcode: NotNull P1 P2 * * *
  2180   2215   ** Synopsis: if r[P1]!=NULL goto P2
  2181   2216   **
  2182   2217   ** Jump to P2 if the value in register P1 is not NULL.  
  2183   2218   */
  2184   2219   case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
  2185   2220     pIn1 = &aMem[pOp->p1];
         2221  +  VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
  2186   2222     if( (pIn1->flags & MEM_Null)==0 ){
  2187   2223       pc = pOp->p2 - 1;
  2188   2224     }
  2189   2225     break;
  2190   2226   }
  2191   2227   
  2192   2228   /* Opcode: Column P1 P2 P3 P4 P5
................................................................................
  2480   2516     zAffinity = pOp->p4.z;
  2481   2517     assert( zAffinity!=0 );
  2482   2518     assert( zAffinity[pOp->p2]==0 );
  2483   2519     pIn1 = &aMem[pOp->p1];
  2484   2520     while( (cAff = *(zAffinity++))!=0 ){
  2485   2521       assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
  2486   2522       assert( memIsValid(pIn1) );
  2487         -    ExpandBlob(pIn1);
  2488   2523       applyAffinity(pIn1, cAff, encoding);
  2489   2524       pIn1++;
  2490   2525     }
  2491   2526     break;
  2492   2527   }
  2493   2528   
  2494   2529   /* Opcode: MakeRecord P1 P2 P3 P4 *
................................................................................
  2558   2593   
  2559   2594     /* Apply the requested affinity to all inputs
  2560   2595     */
  2561   2596     assert( pData0<=pLast );
  2562   2597     if( zAffinity ){
  2563   2598       pRec = pData0;
  2564   2599       do{
  2565         -      applyAffinity(pRec, *(zAffinity++), encoding);
  2566         -    }while( (++pRec)<=pLast );
         2600  +      applyAffinity(pRec++, *(zAffinity++), encoding);
         2601  +      assert( zAffinity[0]==0 || pRec<=pLast );
         2602  +    }while( zAffinity[0] );
  2567   2603     }
  2568   2604   
  2569   2605     /* Loop through the elements that will make up the record to figure
  2570   2606     ** out how much space is required for the new record.
  2571   2607     */
  2572   2608     pRec = pLast;
  2573   2609     do{
................................................................................
  2905   2941       rc = SQLITE_ERROR;
  2906   2942     }
  2907   2943     break;
  2908   2944   }
  2909   2945   
  2910   2946   /* Opcode: Transaction P1 P2 P3 P4 P5
  2911   2947   **
  2912         -** Begin a transaction.  The transaction ends when a Commit or Rollback
  2913         -** opcode is encountered.  Depending on the ON CONFLICT setting, the
  2914         -** transaction might also be rolled back if an error is encountered.
         2948  +** Begin a transaction on database P1 if a transaction is not already
         2949  +** active.
         2950  +** If P2 is non-zero, then a write-transaction is started, or if a 
         2951  +** read-transaction is already active, it is upgraded to a write-transaction.
         2952  +** If P2 is zero, then a read-transaction is started.
  2915   2953   **
  2916   2954   ** P1 is the index of the database file on which the transaction is
  2917   2955   ** started.  Index 0 is the main database file and index 1 is the
  2918   2956   ** file used for temporary tables.  Indices of 2 or more are used for
  2919   2957   ** attached databases.
  2920   2958   **
  2921         -** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
  2922         -** obtained on the database file when a write-transaction is started.  No
  2923         -** other process can start another write transaction while this transaction is
  2924         -** underway.  Starting a write transaction also creates a rollback journal. A
  2925         -** write transaction must be started before any changes can be made to the
  2926         -** database.  If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
  2927         -** also obtained on the file.
  2928         -**
  2929   2959   ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
  2930   2960   ** true (this flag is set if the Vdbe may modify more than one row and may
  2931   2961   ** throw an ABORT exception), a statement transaction may also be opened.
  2932   2962   ** More specifically, a statement transaction is opened iff the database
  2933   2963   ** connection is currently not in autocommit mode, or if there are other
  2934   2964   ** active statements. A statement transaction allows the changes made by this
  2935   2965   ** VDBE to be rolled back after an error without having to roll back the
  2936   2966   ** entire transaction. If no error is encountered, the statement transaction
  2937   2967   ** will automatically commit when the VDBE halts.
  2938   2968   **
  2939         -** If P2 is zero, then a read-lock is obtained on the database file.
  2940         -**
  2941   2969   ** If P5!=0 then this opcode also checks the schema cookie against P3
  2942   2970   ** and the schema generation counter against P4.
  2943   2971   ** The cookie changes its value whenever the database schema changes.
  2944   2972   ** This operation is used to detect when that the cookie has changed
  2945         -** and that the current process needs to reread the schema.
         2973  +** and that the current process needs to reread the schema.  If the schema
         2974  +** cookie in P3 differs from the schema cookie in the database header or
         2975  +** if the schema generation counter in P4 differs from the current
         2976  +** generation counter, then an SQLITE_SCHEMA error is raised and execution
         2977  +** halts.  The sqlite3_step() wrapper function might then reprepare the
         2978  +** statement and rerun it from the beginning.
  2946   2979   */
  2947   2980   case OP_Transaction: {
  2948   2981     Btree *pBt;
  2949   2982     int iMeta;
  2950   2983     int iGen;
  2951   2984   
  2952   2985     assert( p->bIsReader );
................................................................................
  3459   3492   
  3460   3493       /* If the P3 value could not be converted into an integer without
  3461   3494       ** loss of information, then special processing is required... */
  3462   3495       if( (pIn3->flags & MEM_Int)==0 ){
  3463   3496         if( (pIn3->flags & MEM_Real)==0 ){
  3464   3497           /* If the P3 value cannot be converted into any kind of a number,
  3465   3498           ** then the seek is not possible, so jump to P2 */
  3466         -        pc = pOp->p2 - 1;
         3499  +        pc = pOp->p2 - 1;  VdbeBranchTaken(1,2);
  3467   3500           break;
  3468   3501         }
  3469   3502   
  3470   3503         /* If the approximation iKey is larger than the actual real search
  3471   3504         ** term, substitute >= for > and < for <=. e.g. if the search term
  3472   3505         ** is 4.9 and the integer approximation 5:
  3473   3506         **
................................................................................
  3554   3587         /* res might be negative because the table is empty.  Check to
  3555   3588         ** see if this is the case.
  3556   3589         */
  3557   3590         res = sqlite3BtreeEof(pC->pCursor);
  3558   3591       }
  3559   3592     }
  3560   3593     assert( pOp->p2>0 );
         3594  +  VdbeBranchTaken(res!=0,2);
  3561   3595     if( res ){
  3562   3596       pc = pOp->p2 - 1;
  3563   3597     }
  3564   3598     break;
  3565   3599   }
  3566   3600   
  3567   3601   /* Opcode: Seek P1 P2 * * *
................................................................................
  3687   3721     }
  3688   3722     if( pOp->opcode==OP_NoConflict ){
  3689   3723       /* For the OP_NoConflict opcode, take the jump if any of the
  3690   3724       ** input fields are NULL, since any key with a NULL will not
  3691   3725       ** conflict */
  3692   3726       for(ii=0; ii<r.nField; ii++){
  3693   3727         if( r.aMem[ii].flags & MEM_Null ){
  3694         -        pc = pOp->p2 - 1;
         3728  +        pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
  3695   3729           break;
  3696   3730         }
  3697   3731       }
  3698   3732     }
  3699   3733     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
  3700   3734     if( pOp->p4.i==0 ){
  3701   3735       sqlite3DbFree(db, pFree);
................................................................................
  3705   3739     }
  3706   3740     pC->seekResult = res;
  3707   3741     alreadyExists = (res==0);
  3708   3742     pC->nullRow = 1-alreadyExists;
  3709   3743     pC->deferredMoveto = 0;
  3710   3744     pC->cacheStatus = CACHE_STALE;
  3711   3745     if( pOp->opcode==OP_Found ){
         3746  +    VdbeBranchTaken(alreadyExists!=0,2);
  3712   3747       if( alreadyExists ) pc = pOp->p2 - 1;
  3713   3748     }else{
         3749  +    VdbeBranchTaken(alreadyExists==0,2);
  3714   3750       if( !alreadyExists ) pc = pOp->p2 - 1;
  3715   3751     }
  3716   3752     break;
  3717   3753   }
  3718   3754   
  3719   3755   /* Opcode: NotExists P1 P2 P3 * *
  3720   3756   ** Synopsis: intkey=r[P3]
................................................................................
  3749   3785     iKey = pIn3->u.i;
  3750   3786     rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  3751   3787     pC->lastRowid = pIn3->u.i;
  3752   3788     pC->rowidIsValid = res==0 ?1:0;
  3753   3789     pC->nullRow = 0;
  3754   3790     pC->cacheStatus = CACHE_STALE;
  3755   3791     pC->deferredMoveto = 0;
         3792  +  VdbeBranchTaken(res!=0,2);
  3756   3793     if( res!=0 ){
  3757   3794       pc = pOp->p2 - 1;
  3758   3795       assert( pC->rowidIsValid==0 );
  3759   3796     }
  3760   3797     pC->seekResult = res;
  3761   3798     break;
  3762   3799   }
................................................................................
  4123   4160   
  4124   4161     pC = p->apCsr[pOp->p1];
  4125   4162     assert( isSorter(pC) );
  4126   4163     assert( pOp->p4type==P4_INT32 );
  4127   4164     pIn3 = &aMem[pOp->p3];
  4128   4165     nIgnore = pOp->p4.i;
  4129   4166     rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
         4167  +  VdbeBranchTaken(res!=0,2);
  4130   4168     if( res ){
  4131   4169       pc = pOp->p2-1;
  4132   4170     }
  4133   4171     break;
  4134   4172   };
  4135   4173   
  4136   4174   /* Opcode: SorterData P1 P2 * * *
................................................................................
  4322   4360     res = 0;
  4323   4361     assert( pCrsr!=0 );
  4324   4362     rc = sqlite3BtreeLast(pCrsr, &res);
  4325   4363     pC->nullRow = (u8)res;
  4326   4364     pC->deferredMoveto = 0;
  4327   4365     pC->rowidIsValid = 0;
  4328   4366     pC->cacheStatus = CACHE_STALE;
  4329         -  if( pOp->p2>0 && res ){
  4330         -    pc = pOp->p2 - 1;
         4367  +  if( pOp->p2>0 ){
         4368  +    VdbeBranchTaken(res!=0,2);
         4369  +    if( res ) pc = pOp->p2 - 1;
  4331   4370     }
  4332   4371     break;
  4333   4372   }
  4334   4373   
  4335   4374   
  4336   4375   /* Opcode: Sort P1 P2 * * *
  4337   4376   **
................................................................................
  4380   4419       rc = sqlite3BtreeFirst(pCrsr, &res);
  4381   4420       pC->deferredMoveto = 0;
  4382   4421       pC->cacheStatus = CACHE_STALE;
  4383   4422       pC->rowidIsValid = 0;
  4384   4423     }
  4385   4424     pC->nullRow = (u8)res;
  4386   4425     assert( pOp->p2>0 && pOp->p2<p->nOp );
         4426  +  VdbeBranchTaken(res!=0,2);
  4387   4427     if( res ){
  4388   4428       pc = pOp->p2 - 1;
  4389   4429     }
  4390   4430     break;
  4391   4431   }
  4392   4432   
  4393   4433   /* Opcode: Next P1 P2 P3 P4 P5
................................................................................
  4470   4510     assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
  4471   4511     assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
  4472   4512     assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
  4473   4513     assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
  4474   4514     rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4475   4515   next_tail:
  4476   4516     pC->cacheStatus = CACHE_STALE;
         4517  +  VdbeBranchTaken(res==0,2);
  4477   4518     if( res==0 ){
  4478   4519       pC->nullRow = 0;
  4479   4520       pc = pOp->p2 - 1;
  4480   4521       p->aCounter[pOp->p5]++;
  4481   4522   #ifdef SQLITE_TEST
  4482   4523       sqlite3_search_count++;
  4483   4524   #endif
................................................................................
  4695   4736     if( (pOp->opcode&1)==(OP_IdxLT&1) ){
  4696   4737       assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
  4697   4738       res = -res;
  4698   4739     }else{
  4699   4740       assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
  4700   4741       res++;
  4701   4742     }
         4743  +  VdbeBranchTaken(res>0,2);
  4702   4744     if( res>0 ){
  4703   4745       pc = pOp->p2 - 1 ;
  4704   4746     }
  4705   4747     break;
  4706   4748   }
  4707   4749   
  4708   4750   /* Opcode: Destroy P1 P2 P3 * *
................................................................................
  5056   5098     pIn1 = &aMem[pOp->p1];
  5057   5099     if( (pIn1->flags & MEM_RowSet)==0 
  5058   5100      || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
  5059   5101     ){
  5060   5102       /* The boolean index is empty */
  5061   5103       sqlite3VdbeMemSetNull(pIn1);
  5062   5104       pc = pOp->p2 - 1;
         5105  +    VdbeBranchTaken(1,2);
  5063   5106     }else{
  5064   5107       /* A value was pulled from the index */
  5065   5108       sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
         5109  +    VdbeBranchTaken(0,2);
  5066   5110     }
  5067   5111     goto check_for_interrupt;
  5068   5112   }
  5069   5113   
  5070   5114   /* Opcode: RowSetTest P1 P2 P3 P4
  5071   5115   ** Synopsis: if r[P3] in rowset(P1) goto P2
  5072   5116   **
................................................................................
  5110   5154   
  5111   5155     assert( pOp->p4type==P4_INT32 );
  5112   5156     assert( iSet==-1 || iSet>=0 );
  5113   5157     if( iSet ){
  5114   5158       exists = sqlite3RowSetTest(pIn1->u.pRowSet, 
  5115   5159                                  (u8)(iSet>=0 ? iSet & 0xf : 0xff),
  5116   5160                                  pIn3->u.i);
         5161  +    VdbeBranchTaken(exists!=0,2);
  5117   5162       if( exists ){
  5118   5163         pc = pOp->p2 - 1;
  5119   5164         break;
  5120   5165       }
  5121   5166     }
  5122   5167     if( iSet>=0 ){
  5123   5168       sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
................................................................................
  5302   5347   ** If P1 is non-zero, then the jump is taken if the database constraint-counter
  5303   5348   ** is zero (the one that counts deferred constraint violations). If P1 is
  5304   5349   ** zero, the jump is taken if the statement constraint-counter is zero
  5305   5350   ** (immediate foreign key constraint violations).
  5306   5351   */
  5307   5352   case OP_FkIfZero: {         /* jump */
  5308   5353     if( pOp->p1 ){
         5354  +    VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
  5309   5355       if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
  5310   5356     }else{
         5357  +    VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
  5311   5358       if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
  5312   5359     }
  5313   5360     break;
  5314   5361   }
  5315   5362   #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
  5316   5363   
  5317   5364   #ifndef SQLITE_OMIT_AUTOINCREMENT
................................................................................
  5352   5399   **
  5353   5400   ** It is illegal to use this instruction on a register that does
  5354   5401   ** not contain an integer.  An assertion fault will result if you try.
  5355   5402   */
  5356   5403   case OP_IfPos: {        /* jump, in1 */
  5357   5404     pIn1 = &aMem[pOp->p1];
  5358   5405     assert( pIn1->flags&MEM_Int );
         5406  +  VdbeBranchTaken( pIn1->u.i>0, 2);
  5359   5407     if( pIn1->u.i>0 ){
  5360   5408        pc = pOp->p2 - 1;
  5361   5409     }
  5362   5410     break;
  5363   5411   }
  5364   5412   
  5365   5413   /* Opcode: IfNeg P1 P2 * * *
................................................................................
  5369   5417   **
  5370   5418   ** It is illegal to use this instruction on a register that does
  5371   5419   ** not contain an integer.  An assertion fault will result if you try.
  5372   5420   */
  5373   5421   case OP_IfNeg: {        /* jump, in1 */
  5374   5422     pIn1 = &aMem[pOp->p1];
  5375   5423     assert( pIn1->flags&MEM_Int );
         5424  +  VdbeBranchTaken(pIn1->u.i<0, 2);
  5376   5425     if( pIn1->u.i<0 ){
  5377   5426        pc = pOp->p2 - 1;
  5378   5427     }
  5379   5428     break;
  5380   5429   }
  5381   5430   
  5382   5431   /* Opcode: IfZero P1 P2 P3 * *
................................................................................
  5388   5437   ** It is illegal to use this instruction on a register that does
  5389   5438   ** not contain an integer.  An assertion fault will result if you try.
  5390   5439   */
  5391   5440   case OP_IfZero: {        /* jump, in1 */
  5392   5441     pIn1 = &aMem[pOp->p1];
  5393   5442     assert( pIn1->flags&MEM_Int );
  5394   5443     pIn1->u.i += pOp->p3;
         5444  +  VdbeBranchTaken(pIn1->u.i==0, 2);
  5395   5445     if( pIn1->u.i==0 ){
  5396   5446        pc = pOp->p2 - 1;
  5397   5447     }
  5398   5448     break;
  5399   5449   }
  5400   5450   
  5401   5451   /* Opcode: AggStep * P2 P3 P4 P5
................................................................................
  5659   5709     Btree *pBt;
  5660   5710   
  5661   5711     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5662   5712     assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  5663   5713     assert( p->readOnly==0 );
  5664   5714     pBt = db->aDb[pOp->p1].pBt;
  5665   5715     rc = sqlite3BtreeIncrVacuum(pBt);
         5716  +  VdbeBranchTaken(rc==SQLITE_DONE,2);
  5666   5717     if( rc==SQLITE_DONE ){
  5667   5718       pc = pOp->p2 - 1;
  5668   5719       rc = SQLITE_OK;
  5669   5720     }
  5670   5721     break;
  5671   5722   }
  5672   5723   #endif
................................................................................
  5865   5916       p->inVtabMethod = 1;
  5866   5917       rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
  5867   5918       p->inVtabMethod = 0;
  5868   5919       sqlite3VtabImportErrmsg(p, pVtab);
  5869   5920       if( rc==SQLITE_OK ){
  5870   5921         res = pModule->xEof(pVtabCursor);
  5871   5922       }
  5872         -
         5923  +    VdbeBranchTaken(res!=0,2);
  5873   5924       if( res ){
  5874   5925         pc = pOp->p2 - 1;
  5875   5926       }
  5876   5927     }
  5877   5928     pCur->nullRow = 0;
  5878   5929   
  5879   5930     break;
................................................................................
  5970   6021     p->inVtabMethod = 1;
  5971   6022     rc = pModule->xNext(pCur->pVtabCursor);
  5972   6023     p->inVtabMethod = 0;
  5973   6024     sqlite3VtabImportErrmsg(p, pVtab);
  5974   6025     if( rc==SQLITE_OK ){
  5975   6026       res = pModule->xEof(pCur->pVtabCursor);
  5976   6027     }
  5977         -
         6028  +  VdbeBranchTaken(!res,2);
  5978   6029     if( !res ){
  5979   6030       /* If there is data, jump to P2 */
  5980   6031       pc = pOp->p2 - 1;
  5981   6032     }
  5982   6033     goto check_for_interrupt;
  5983   6034   }
  5984   6035   #endif /* SQLITE_OMIT_VIRTUALTABLE */

Changes to src/vdbe.h.

    64     64   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
    65     65     char *zComment;          /* Comment to improve readability */
    66     66   #endif
    67     67   #ifdef VDBE_PROFILE
    68     68     int cnt;                 /* Number of times this instruction was executed */
    69     69     u64 cycles;              /* Total time spent executing this instruction */
    70     70   #endif
           71  +#ifdef SQLITE_VDBE_COVERAGE
           72  +  int iSrcLine;            /* Source-code line that generated this opcode */
           73  +#endif
    71     74   };
    72     75   typedef struct VdbeOp VdbeOp;
    73     76   
    74     77   
    75     78   /*
    76     79   ** A sub-routine used to implement a trigger program.
    77     80   */
................................................................................
   163    166   Vdbe *sqlite3VdbeCreate(Parse*);
   164    167   int sqlite3VdbeAddOp0(Vdbe*,int);
   165    168   int sqlite3VdbeAddOp1(Vdbe*,int,int);
   166    169   int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
   167    170   int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
   168    171   int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
   169    172   int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
   170         -int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
          173  +int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
   171    174   void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
   172    175   void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
   173    176   void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
   174    177   void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
   175    178   void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
   176    179   void sqlite3VdbeJumpHere(Vdbe*, int addr);
   177    180   void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
................................................................................
   233    236   #   define VdbeModuleComment(X)
   234    237   # endif
   235    238   #else
   236    239   # define VdbeComment(X)
   237    240   # define VdbeNoopComment(X)
   238    241   # define VdbeModuleComment(X)
   239    242   #endif
          243  +
          244  +/* Set the Opcode.iSrcline field of the previous opcode */
          245  +#ifdef SQLITE_VDBE_COVERAGE
          246  +  void sqlite3VdbeSetLineNumber(Vdbe*,int);
          247  +# define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
          248  +# define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
          249  +#else
          250  +# define VdbeCoverage(v)
          251  +# define VdbeCoverageIf(v,x)
          252  +#endif
   240    253   
   241    254   #endif

Changes to src/vdbeaux.c.

   170    170       test_addop_breakpoint();
   171    171     }
   172    172   #endif
   173    173   #ifdef VDBE_PROFILE
   174    174     pOp->cycles = 0;
   175    175     pOp->cnt = 0;
   176    176   #endif
          177  +#ifdef SQLITE_VDBE_COVERAGE
          178  +  pOp->iSrcLine = 0;
          179  +#endif
   177    180     return i;
   178    181   }
   179    182   int sqlite3VdbeAddOp0(Vdbe *p, int op){
   180    183     return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
   181    184   }
   182    185   int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
   183    186     return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
................................................................................
   531    534     return aOp;
   532    535   }
   533    536   
   534    537   /*
   535    538   ** Add a whole list of operations to the operation stack.  Return the
   536    539   ** address of the first operation added.
   537    540   */
   538         -int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
          541  +int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
   539    542     int addr;
   540    543     assert( p->magic==VDBE_MAGIC_INIT );
   541    544     if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
   542    545       return 0;
   543    546     }
   544    547     addr = p->nOp;
   545    548     if( ALWAYS(nOp>0) ){
................................................................................
   558    561         }
   559    562         pOut->p3 = pIn->p3;
   560    563         pOut->p4type = P4_NOTUSED;
   561    564         pOut->p4.p = 0;
   562    565         pOut->p5 = 0;
   563    566   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   564    567         pOut->zComment = 0;
          568  +#endif
          569  +#ifdef SQLITE_VDBE_COVERAGE
          570  +      pOut->iSrcLine = iLineno+i;
          571  +#else
          572  +      (void)iLineno;
   565    573   #endif
   566    574   #ifdef SQLITE_DEBUG
   567    575         if( p->db->flags & SQLITE_VdbeAddopTrace ){
   568    576           sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
   569    577         }
   570    578   #endif
   571    579       }
................................................................................
   847    855       va_start(ap, zFormat);
   848    856       vdbeVComment(p, zFormat, ap);
   849    857       va_end(ap);
   850    858     }
   851    859   }
   852    860   #endif  /* NDEBUG */
   853    861   
          862  +#ifdef SQLITE_VDBE_COVERAGE
          863  +/*
          864  +** Set the value if the iSrcLine field for the previously coded instruction.
          865  +*/
          866  +void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
          867  +  sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
          868  +}
          869  +#endif /* SQLITE_VDBE_COVERAGE */
          870  +
   854    871   /*
   855    872   ** Return the opcode for a given address.  If the address is -1, then
   856    873   ** return the most recently inserted opcode.
   857    874   **
   858    875   ** If a memory allocation error has occurred prior to the calling of this
   859    876   ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
   860    877   ** is readable but not writable, though it is cast to a writable value.

Changes to src/vdbeblob.c.

   129    129     ** uses it to implement the blob_read(), blob_write() and 
   130    130     ** blob_bytes() functions.
   131    131     **
   132    132     ** The sqlite3_blob_close() function finalizes the vdbe program,
   133    133     ** which closes the b-tree cursor and (possibly) commits the 
   134    134     ** transaction.
   135    135     */
          136  +  static const int iLn = __LINE__+4;
   136    137     static const VdbeOpList openBlob[] = {
   137    138       /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
   138    139       {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
   139         -
   140    140       /* One of the following two instructions is replaced by an OP_Noop. */
   141    141       {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
   142    142       {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
   143         -
   144    143       {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
   145    144       {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
   146    145       {OP_Column, 0, 0, 1},          /* 6  */
   147    146       {OP_ResultRow, 1, 0, 0},       /* 7  */
   148    147       {OP_Goto, 0, 4, 0},            /* 8  */
   149    148       {OP_Close, 0, 0, 0},           /* 9  */
   150    149       {OP_Halt, 0, 0, 0},            /* 10 */
................................................................................
   261    260         int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
   262    261   
   263    262   
   264    263         sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags, 
   265    264                              pTab->pSchema->schema_cookie,
   266    265                              pTab->pSchema->iGeneration);
   267    266         sqlite3VdbeChangeP5(v, 1);     
   268         -      sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
          267  +      sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
   269    268   
   270    269         /* Make sure a mutex is held on the table to be accessed */
   271    270         sqlite3VdbeUsesBtree(v, iDb); 
   272    271   
   273    272         /* Configure the OP_TableLock instruction */
   274    273   #ifdef SQLITE_OMIT_SHARED_CACHE
   275    274         sqlite3VdbeChangeToNoop(v, 1);

Changes to src/where.c.

  1704   1704     assert( pLevel->iIdxCur>=0 );
  1705   1705     pLevel->iIdxCur = pParse->nTab++;
  1706   1706     sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
  1707   1707     sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1708   1708     VdbeComment((v, "for %s", pTable->zName));
  1709   1709   
  1710   1710     /* Fill the automatic index with content */
  1711         -  addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
         1711  +  addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
  1712   1712     regRecord = sqlite3GetTempReg(pParse);
  1713   1713     sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
  1714   1714     sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
  1715   1715     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  1716         -  sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
         1716  +  sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
  1717   1717     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
  1718   1718     sqlite3VdbeJumpHere(v, addrTop);
  1719   1719     sqlite3ReleaseTempReg(pParse, regRecord);
  1720   1720     
  1721   1721     /* Jump here when skipping the initialization */
  1722   1722     sqlite3VdbeJumpHere(v, addrInit);
  1723   1723   }
................................................................................
  2384   2384       iReg = iTarget;
  2385   2385       eType = sqlite3FindInIndex(pParse, pX, 0);
  2386   2386       if( eType==IN_INDEX_INDEX_DESC ){
  2387   2387         testcase( bRev );
  2388   2388         bRev = !bRev;
  2389   2389       }
  2390   2390       iTab = pX->iTable;
  2391         -    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
         2391  +    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); VdbeCoverage(v);
  2392   2392       assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
  2393   2393       pLoop->wsFlags |= WHERE_IN_ABLE;
  2394   2394       if( pLevel->u.in.nIn==0 ){
  2395   2395         pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  2396   2396       }
  2397   2397       pLevel->u.in.nIn++;
  2398   2398       pLevel->u.in.aInLoop =
................................................................................
  2404   2404         pIn->iCur = iTab;
  2405   2405         if( eType==IN_INDEX_ROWID ){
  2406   2406           pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
  2407   2407         }else{
  2408   2408           pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
  2409   2409         }
  2410   2410         pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
  2411         -      sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
         2411  +      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
  2412   2412       }else{
  2413   2413         pLevel->u.in.nIn = 0;
  2414   2414       }
  2415   2415   #endif
  2416   2416     }
  2417   2417     disableTerm(pLevel, pTerm);
  2418   2418     return iReg;
................................................................................
  2498   2498     zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
  2499   2499     if( !zAff ){
  2500   2500       pParse->db->mallocFailed = 1;
  2501   2501     }
  2502   2502   
  2503   2503     if( nSkip ){
  2504   2504       int iIdxCur = pLevel->iIdxCur;
  2505         -    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
         2505  +    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); VdbeCoverage(v);
  2506   2506       VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
  2507   2507       j = sqlite3VdbeAddOp0(v, OP_Goto);
  2508   2508       pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
  2509         -                            iIdxCur, 0, regBase, nSkip);
         2509  +                            iIdxCur, 0, regBase, nSkip); VdbeCoverage(v);
  2510   2510       sqlite3VdbeJumpHere(v, j);
  2511   2511       for(j=0; j<nSkip; j++){
  2512   2512         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
  2513   2513         assert( pIdx->aiColumn[j]>=0 );
  2514   2514         VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
  2515   2515       }
  2516   2516     }    
................................................................................
  2783   2783     }
  2784   2784   
  2785   2785     /* Special case of a FROM clause subquery implemented as a co-routine */
  2786   2786     if( pTabItem->viaCoroutine ){
  2787   2787       int regYield = pTabItem->regReturn;
  2788   2788       sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
  2789   2789       pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
         2790  +    VdbeCoverage(v);
  2790   2791       VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
  2791   2792       pLevel->op = OP_Goto;
  2792   2793     }else
  2793   2794   
  2794   2795   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2795   2796     if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  2796   2797       /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
................................................................................
  2815   2816         }
  2816   2817       }
  2817   2818       sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
  2818   2819       sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
  2819   2820       sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
  2820   2821                         pLoop->u.vtab.idxStr,
  2821   2822                         pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
         2823  +    VdbeCoverage(v);
  2822   2824       pLoop->u.vtab.needFree = 0;
  2823   2825       for(j=0; j<nConstraint && j<16; j++){
  2824   2826         if( (pLoop->u.vtab.omitMask>>j)&1 ){
  2825   2827           disableTerm(pLevel, pLoop->aLTerm[j]);
  2826   2828         }
  2827   2829       }
  2828   2830       pLevel->op = OP_VNext;
................................................................................
  2846   2848       pTerm = pLoop->aLTerm[0];
  2847   2849       assert( pTerm!=0 );
  2848   2850       assert( pTerm->pExpr!=0 );
  2849   2851       assert( omitTable==0 );
  2850   2852       testcase( pTerm->wtFlags & TERM_VIRTUAL );
  2851   2853       iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
  2852   2854       addrNxt = pLevel->addrNxt;
  2853         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
         2855  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
  2854   2856       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
         2857  +    VdbeCoverage(v);
  2855   2858       sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
  2856   2859       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  2857   2860       VdbeComment((v, "pk"));
  2858   2861       pLevel->op = OP_Noop;
  2859   2862     }else if( (pLoop->wsFlags & WHERE_IPK)!=0
  2860   2863            && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
  2861   2864     ){
................................................................................
  2897   2900         assert( (pStart->wtFlags & TERM_VNULL)==0 );
  2898   2901         testcase( pStart->wtFlags & TERM_VIRTUAL );
  2899   2902         pX = pStart->pExpr;
  2900   2903         assert( pX!=0 );
  2901   2904         testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
  2902   2905         r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
  2903   2906         sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
  2904         -      VdbeComment((v, "pk"));
         2907  +      VdbeComment((v, "pk")); VdbeCoverage(v);
  2905   2908         sqlite3ExprCacheAffinityChange(pParse, r1, 1);
  2906   2909         sqlite3ReleaseTempReg(pParse, rTemp);
  2907   2910         disableTerm(pLevel, pStart);
  2908   2911       }else{
  2909   2912         sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
         2913  +      VdbeCoverage(v);
  2910   2914       }
  2911   2915       if( pEnd ){
  2912   2916         Expr *pX;
  2913   2917         pX = pEnd->pExpr;
  2914   2918         assert( pX!=0 );
  2915   2919         assert( (pEnd->wtFlags & TERM_VNULL)==0 );
  2916   2920         testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
................................................................................
  2930   2934       pLevel->p2 = start;
  2931   2935       assert( pLevel->p5==0 );
  2932   2936       if( testOp!=OP_Noop ){
  2933   2937         iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
  2934   2938         sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
  2935   2939         sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  2936   2940         sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
         2941  +      VdbeCoverage(v);
  2937   2942         sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
  2938   2943       }
  2939   2944     }else if( pLoop->wsFlags & WHERE_INDEXED ){
  2940   2945       /* Case 4: A scan using an index.
  2941   2946       **
  2942   2947       **         The WHERE clause may contain zero or more equality 
  2943   2948       **         terms ("==" or "IN" operators) that refer to the N
................................................................................
  3103   3108       testcase( op==OP_Rewind );
  3104   3109       testcase( op==OP_Last );
  3105   3110       testcase( op==OP_SeekGT );
  3106   3111       testcase( op==OP_SeekGE );
  3107   3112       testcase( op==OP_SeekLE );
  3108   3113       testcase( op==OP_SeekLT );
  3109   3114       sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
         3115  +    VdbeCoverage(v);
         3116  +    VdbeCoverageIf(v, op==OP_Rewind);
         3117  +    VdbeCoverageIf(v, op==OP_Last);
         3118  +    VdbeCoverageIf(v, op==OP_SeekGT);
         3119  +    VdbeCoverageIf(v, op==OP_SeekGE);
         3120  +    VdbeCoverageIf(v, op==OP_SeekLE);
         3121  +    VdbeCoverageIf(v, op==OP_SeekLT);
  3110   3122   
  3111   3123       /* Load the value for the inequality constraint at the end of the
  3112   3124       ** range (if any).
  3113   3125       */
  3114   3126       nConstraint = nEq;
  3115   3127       if( pRangeEnd ){
  3116   3128         Expr *pRight = pRangeEnd->pExpr->pRight;
................................................................................
  3140   3152       if( nConstraint ){
  3141   3153         op = aEndOp[bRev*2 + endEq];
  3142   3154         testcase( op==OP_IdxGT );
  3143   3155         testcase( op==OP_IdxGE );
  3144   3156         testcase( op==OP_IdxLT );
  3145   3157         testcase( op==OP_IdxLE );
  3146   3158         sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
         3159  +      VdbeCoverage(v);
  3147   3160       }
  3148   3161   
  3149   3162       /* Seek the table cursor, if required */
  3150   3163       disableTerm(pLevel, pRangeStart);
  3151   3164       disableTerm(pLevel, pRangeEnd);
  3152   3165       if( omitTable ){
  3153   3166         /* pIdx is a covering index.  No need to access the main table. */
................................................................................
  3160   3173         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  3161   3174         iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
  3162   3175         for(j=0; j<pPk->nKeyCol; j++){
  3163   3176           k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
  3164   3177           sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
  3165   3178         }
  3166   3179         sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
  3167         -                           iRowidReg, pPk->nKeyCol);
         3180  +                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
  3168   3181       }
  3169   3182   
  3170   3183       /* Record the instruction used to terminate the loop. Disable 
  3171   3184       ** WHERE clause terms made redundant by the index range scan.
  3172   3185       */
  3173   3186       if( pLoop->wsFlags & WHERE_ONEROW ){
  3174   3187         pLevel->op = OP_Noop;
................................................................................
  3344   3357             if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  3345   3358               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  3346   3359               int r;
  3347   3360               r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
  3348   3361                                            regRowid, 0);
  3349   3362               sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
  3350   3363                                    sqlite3VdbeCurrentAddr(v)+2, r, iSet);
         3364  +            VdbeCoverage(v);
  3351   3365             }
  3352   3366             sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
  3353   3367   
  3354   3368             /* The pSubWInfo->untestedTerms flag means that this OR term
  3355   3369             ** contained one or more AND term from a notReady table.  The
  3356   3370             ** terms from the notReady table could not be tested and will
  3357   3371             ** need to be tested later.
................................................................................
  3412   3426         /* Tables marked isRecursive have only a single row that is stored in
  3413   3427         ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
  3414   3428         pLevel->op = OP_Noop;
  3415   3429       }else{
  3416   3430         pLevel->op = aStep[bRev];
  3417   3431         pLevel->p1 = iCur;
  3418   3432         pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
         3433  +      VdbeCoverageIf(v, bRev);
         3434  +      VdbeCoverageIf(v, !bRev);
  3419   3435         pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  3420   3436       }
  3421   3437     }
  3422   3438   
  3423   3439     /* Insert code to test every subexpression that can be completely
  3424   3440     ** computed using the current set of tables.
  3425   3441     */
................................................................................
  5781   5797       int addr;
  5782   5798       pLevel = &pWInfo->a[i];
  5783   5799       pLoop = pLevel->pWLoop;
  5784   5800       sqlite3VdbeResolveLabel(v, pLevel->addrCont);
  5785   5801       if( pLevel->op!=OP_Noop ){
  5786   5802         sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
  5787   5803         sqlite3VdbeChangeP5(v, pLevel->p5);
         5804  +      VdbeCoverage(v);
  5788   5805       }
  5789   5806       if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
  5790   5807         struct InLoop *pIn;
  5791   5808         int j;
  5792   5809         sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
  5793   5810         for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
  5794   5811           sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
  5795   5812           sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
         5813  +        VdbeCoverage(v);
  5796   5814           sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
  5797   5815         }
  5798   5816         sqlite3DbFree(db, pLevel->u.in.aInLoop);
  5799   5817       }
  5800   5818       sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
  5801   5819       if( pLevel->addrSkip ){
  5802   5820         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
  5803   5821         VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
  5804   5822         sqlite3VdbeJumpHere(v, pLevel->addrSkip);
  5805   5823         sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
  5806   5824       }
  5807   5825       if( pLevel->iLeftJoin ){
  5808         -      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
         5826  +      addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
  5809   5827         assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
  5810   5828              || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
  5811   5829         if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
  5812   5830           sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
  5813   5831         }
  5814   5832         if( pLoop->wsFlags & WHERE_INDEXED ){
  5815   5833           sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);

Changes to test/insert.test.

   394    394         CREATE TABLE t10(a,b,c);
   395    395         INSERT INTO t10 VALUES(1,2,3), (4,5,6), (7,8,9);
   396    396         SELECT * FROM t10;
   397    397       }
   398    398     } {1 2 3 4 5 6 7 8 9}
   399    399     do_test insert-10.2 {
   400    400       catchsql {
   401         -      INSERT INTO t10 VALUES(11,12,13), (14,15);
          401  +      INSERT INTO t10 VALUES(11,12,13), (14,15), (16,17,28);
   402    402       }
   403    403     } {1 {all VALUES must have the same number of terms}}
   404    404   }
          405  +
          406  +# Need for the OP_SoftNull opcode
          407  +#
          408  +do_execsql_test insert-11.1 {
          409  +  CREATE TABLE t11a AS SELECT '123456789' AS x;
          410  +  CREATE TABLE t11b (a INTEGER PRIMARY KEY, b, c);
          411  +  INSERT INTO t11b SELECT x, x, x FROM t11a;
          412  +  SELECT quote(a), quote(b), quote(c) FROM t11b;
          413  +} {123456789 '123456789' '123456789'}
          414  +
   405    415   
   406    416   integrity_check insert-99.0
   407    417   
   408    418   finish_test

Changes to test/insert4.test.

   249    249     } {}
   250    250   }
   251    251   
   252    252   # Check some error conditions:
   253    253   #
   254    254   do_test insert4-5.1 {
   255    255     # Table does not exist.
   256         -  catchsql { INSERT INTO t2 SELECT * FROM nosuchtable }
          256  +  catchsql { INSERT INTO t2 SELECT a, b FROM nosuchtable }
   257    257   } {1 {no such table: nosuchtable}}
   258    258   do_test insert4-5.2 {
   259    259     # Number of columns does not match.
   260    260     catchsql { 
   261    261       CREATE TABLE t5(a, b, c);
   262    262       INSERT INTO t4 SELECT * FROM t5;
   263    263     }