/ Check-in [68a6d5e2]
Login

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

Overview
Comment:Merge the latest 3.8.6 beta changes from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 68a6d5e2f43702c78057ae2f2a7345c981d24e17
User & Date: drh 2014-08-06 01:25:47
Context
2014-08-06
03:16
Merge in the fix for the InterlockedCompareExchange() build issue. check-in: 091aa5f1 user: drh tags: sessions
01:25
Merge the latest 3.8.6 beta changes from trunk. check-in: 68a6d5e2 user: drh tags: sessions
01:08
Fix typos in the opcode documentation. Comment changes only. No changes to code. check-in: 717245d4 user: drh tags: trunk
2014-07-30
14:29
Merge the fix for the CREATE UNIQUE INDEX problem into the sessions branch. check-in: 43401ee6 user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/analyze.c.

  1125   1125         **   if( idx(1) != regPrev(1) ) goto chng_addr_1
  1126   1126         **   ...
  1127   1127         **   regChng = N
  1128   1128         **   goto endDistinctTest
  1129   1129         */
  1130   1130         sqlite3VdbeAddOp0(v, OP_Goto);
  1131   1131         addrNextRow = sqlite3VdbeCurrentAddr(v);
  1132         -      if( nColTest==1 && pIdx->nKeyCol==1 && pIdx->onError!=OE_None ){
         1132  +      if( nColTest==1 && pIdx->nKeyCol==1 && IsUniqueIndex(pIdx) ){
  1133   1133           /* For a single-column UNIQUE index, once we have found a non-NULL
  1134   1134           ** row, we know that all the rest will be distinct, so skip 
  1135   1135           ** subsequent distinctness tests. */
  1136   1136           sqlite3VdbeAddOp2(v, OP_NotNull, regPrev, endDistinctTest);
  1137   1137           VdbeCoverage(v);
  1138   1138         }
  1139   1139         for(i=0; i<nColTest; i++){

Changes to src/btree.c.

   158    158     }
   159    159   
   160    160     /* If the client is reading  or writing an index and the schema is
   161    161     ** not loaded, then it is too difficult to actually check to see if
   162    162     ** the correct locks are held.  So do not bother - just return true.
   163    163     ** This case does not come up very often anyhow.
   164    164     */
   165         -  if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
          165  +  if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){
   166    166       return 1;
   167    167     }
   168    168   
   169    169     /* Figure out the root-page that the lock should be held on. For table
   170    170     ** b-trees, this is just the root page of the b-tree being read or
   171    171     ** written. For index b-trees, it is the root page of the associated
   172    172     ** table.  */

Changes to src/build.c.

  2126   2126         assert( pTable->aCol==0 );
  2127   2127         pTable->nCol = pSelTab->nCol;
  2128   2128         pTable->aCol = pSelTab->aCol;
  2129   2129         pSelTab->nCol = 0;
  2130   2130         pSelTab->aCol = 0;
  2131   2131         sqlite3DeleteTable(db, pSelTab);
  2132   2132         assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
  2133         -      pTable->pSchema->flags |= DB_UnresetViews;
         2133  +      pTable->pSchema->schemaFlags |= DB_UnresetViews;
  2134   2134       }else{
  2135   2135         pTable->nCol = 0;
  2136   2136         nErr++;
  2137   2137       }
  2138   2138       sqlite3SelectDelete(db, pSel);
  2139   2139     } else {
  2140   2140       nErr++;
................................................................................
  2703   2703     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
  2704   2704     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2705   2705                       (char *)pKey, P4_KEYINFO);
  2706   2706     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
  2707   2707   
  2708   2708     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
  2709   2709     assert( pKey!=0 || db->mallocFailed || pParse->nErr );
  2710         -  if( pIndex->onError!=OE_None && pKey!=0 ){
         2710  +  if( IsUniqueIndex(pIndex) && pKey!=0 ){
  2711   2711       int j2 = sqlite3VdbeCurrentAddr(v) + 3;
  2712   2712       sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
  2713   2713       addr2 = sqlite3VdbeCurrentAddr(v);
  2714   2714       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
  2715   2715                            pIndex->nKeyCol); VdbeCoverage(v);
  2716   2716       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  2717   2717     }else{
................................................................................
  3100   3100       ** If there are different collating sequences or if the columns of
  3101   3101       ** the constraint occur in different orders, then the constraints are
  3102   3102       ** considered distinct and both result in separate indices.
  3103   3103       */
  3104   3104       Index *pIdx;
  3105   3105       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  3106   3106         int k;
  3107         -      assert( pIdx->onError!=OE_None );
         3107  +      assert( IsUniqueIndex(pIdx) );
  3108   3108         assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
  3109         -      assert( pIndex->onError!=OE_None );
         3109  +      assert( IsUniqueIndex(pIndex) );
  3110   3110   
  3111   3111         if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
  3112   3112         for(k=0; k<pIdx->nKeyCol; k++){
  3113   3113           const char *z1;
  3114   3114           const char *z2;
  3115   3115           if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
  3116   3116           z1 = pIdx->azColl[k];
................................................................................
  3293   3293     ** 6 and each subsequent value (if any) is 5.  */
  3294   3294     memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
  3295   3295     for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
  3296   3296       a[i] = 23;                    assert( 23==sqlite3LogEst(5) );
  3297   3297     }
  3298   3298   
  3299   3299     assert( 0==sqlite3LogEst(1) );
  3300         -  if( pIdx->onError!=OE_None ) a[pIdx->nKeyCol] = 0;
         3300  +  if( IsUniqueIndex(pIdx) ) a[pIdx->nKeyCol] = 0;
  3301   3301   }
  3302   3302   
  3303   3303   /*
  3304   3304   ** This routine will drop an existing named index.  This routine
  3305   3305   ** implements the DROP INDEX statement.
  3306   3306   */
  3307   3307   void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){

Changes to src/callback.c.

   443    443     for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
   444    444       Table *pTab = sqliteHashData(pElem);
   445    445       sqlite3DeleteTable(0, pTab);
   446    446     }
   447    447     sqlite3HashClear(&temp1);
   448    448     sqlite3HashClear(&pSchema->fkeyHash);
   449    449     pSchema->pSeqTab = 0;
   450         -  if( pSchema->flags & DB_SchemaLoaded ){
          450  +  if( pSchema->schemaFlags & DB_SchemaLoaded ){
   451    451       pSchema->iGeneration++;
   452         -    pSchema->flags &= ~DB_SchemaLoaded;
          452  +    pSchema->schemaFlags &= ~DB_SchemaLoaded;
   453    453     }
   454    454   }
   455    455   
   456    456   /*
   457    457   ** Find and return the schema associated with a BTree.  Create
   458    458   ** a new one if necessary.
   459    459   */

Changes to src/expr.c.

  1364   1364     if( op==TK_REGISTER ) op = p->op2;
  1365   1365     switch( op ){
  1366   1366       case TK_INTEGER:
  1367   1367       case TK_STRING:
  1368   1368       case TK_FLOAT:
  1369   1369       case TK_BLOB:
  1370   1370         return 0;
         1371  +    case TK_COLUMN:
         1372  +      assert( p->pTab!=0 );
         1373  +      return p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0;
  1371   1374       default:
  1372   1375         return 1;
  1373   1376     }
  1374   1377   }
  1375   1378   
  1376   1379   /*
  1377   1380   ** Return TRUE if the given expression is a constant which would be
................................................................................
  1471   1474   ** address of the new instruction.
  1472   1475   */
  1473   1476   int sqlite3CodeOnce(Parse *pParse){
  1474   1477     Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
  1475   1478     return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
  1476   1479   }
  1477   1480   
         1481  +/*
         1482  +** Generate code that checks the left-most column of index table iCur to see if
         1483  +** it contains any NULL entries.  Cause the register at regHasNull to be set
         1484  +** to a non-NULL value if iCur contains no NULLs.  Cause register regHasNull
         1485  +** to be set to NULL if iCur contains one or more NULL values.
         1486  +*/
         1487  +static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){
         1488  +  int j1;
         1489  +  sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull);
         1490  +  j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v);
         1491  +  sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull);
         1492  +  sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
         1493  +  VdbeComment((v, "first_entry_in(%d)", iCur));
         1494  +  sqlite3VdbeJumpHere(v, j1);
         1495  +}
         1496  +
         1497  +
         1498  +#ifndef SQLITE_OMIT_SUBQUERY
         1499  +/*
         1500  +** The argument is an IN operator with a list (not a subquery) on the 
         1501  +** right-hand side.  Return TRUE if that list is constant.
         1502  +*/
         1503  +static int sqlite3InRhsIsConstant(Expr *pIn){
         1504  +  Expr *pLHS;
         1505  +  int res;
         1506  +  assert( !ExprHasProperty(pIn, EP_xIsSelect) );
         1507  +  pLHS = pIn->pLeft;
         1508  +  pIn->pLeft = 0;
         1509  +  res = sqlite3ExprIsConstant(pIn);
         1510  +  pIn->pLeft = pLHS;
         1511  +  return res;
         1512  +}
         1513  +#endif
         1514  +
  1478   1515   /*
  1479   1516   ** This function is used by the implementation of the IN (...) operator.
  1480   1517   ** The pX parameter is the expression on the RHS of the IN operator, which
  1481   1518   ** might be either a list of expressions or a subquery.
  1482   1519   **
  1483   1520   ** The job of this routine is to find or create a b-tree object that can
  1484   1521   ** be used either to test for membership in the RHS set or to iterate through
  1485   1522   ** all members of the RHS set, skipping duplicates.
  1486   1523   **
  1487         -** A cursor is opened on the b-tree object that the RHS of the IN operator
         1524  +** A cursor is opened on the b-tree object that is the RHS of the IN operator
  1488   1525   ** and pX->iTable is set to the index of that cursor.
  1489   1526   **
  1490   1527   ** The returned value of this function indicates the b-tree type, as follows:
  1491   1528   **
  1492   1529   **   IN_INDEX_ROWID      - The cursor was opened on a database table.
  1493   1530   **   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
  1494   1531   **   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
  1495   1532   **   IN_INDEX_EPH        - The cursor was opened on a specially created and
  1496   1533   **                         populated epheremal table.
         1534  +**   IN_INDEX_NOOP       - No cursor was allocated.  The IN operator must be
         1535  +**                         implemented as a sequence of comparisons.
  1497   1536   **
  1498   1537   ** An existing b-tree might be used if the RHS expression pX is a simple
  1499   1538   ** subquery such as:
  1500   1539   **
  1501   1540   **     SELECT <column> FROM <table>
  1502   1541   **
  1503   1542   ** If the RHS of the IN operator is a list or a more complex subquery, then
  1504   1543   ** an ephemeral table might need to be generated from the RHS and then
  1505   1544   ** pX->iTable made to point to the ephermeral table instead of an
  1506         -** existing table.  
         1545  +** existing table.
  1507   1546   **
  1508         -** If the prNotFound parameter is 0, then the b-tree will be used to iterate
  1509         -** through the set members, skipping any duplicates. In this case an
  1510         -** epheremal table must be used unless the selected <column> is guaranteed
         1547  +** The inFlags parameter must contain exactly one of the bits
         1548  +** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP.  If inFlags contains
         1549  +** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
         1550  +** fast membership test.  When the IN_INDEX_LOOP bit is set, the
         1551  +** IN index will be used to loop over all values of the RHS of the
         1552  +** IN operator.
         1553  +**
         1554  +** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
         1555  +** through the set members) then the b-tree must not contain duplicates.
         1556  +** An epheremal table must be used unless the selected <column> is guaranteed
  1511   1557   ** to be unique - either because it is an INTEGER PRIMARY KEY or it
  1512   1558   ** has a UNIQUE constraint or UNIQUE index.
  1513   1559   **
  1514         -** If the prNotFound parameter is not 0, then the b-tree will be used 
  1515         -** for fast set membership tests. In this case an epheremal table must 
         1560  +** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 
         1561  +** for fast set membership tests) then an epheremal table must 
  1516   1562   ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 
  1517   1563   ** be found with <column> as its left-most column.
         1564  +**
         1565  +** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and
         1566  +** if the RHS of the IN operator is a list (not a subquery) then this
         1567  +** routine might decide that creating an ephemeral b-tree for membership
         1568  +** testing is too expensive and return IN_INDEX_NOOP.  In that case, the
         1569  +** calling routine should implement the IN operator using a sequence
         1570  +** of Eq or Ne comparison operations.
  1518   1571   **
  1519   1572   ** When the b-tree is being used for membership tests, the calling function
  1520         -** needs to know whether or not the structure contains an SQL NULL 
  1521         -** value in order to correctly evaluate expressions like "X IN (Y, Z)".
  1522         -** If there is any chance that the (...) might contain a NULL value at
         1573  +** might need to know whether or not the RHS side of the IN operator
         1574  +** contains a NULL.  If prRhsHasNull is not a NULL pointer and 
         1575  +** if there is any chance that the (...) might contain a NULL value at
  1523   1576   ** runtime, then a register is allocated and the register number written
  1524         -** to *prNotFound. If there is no chance that the (...) contains a
  1525         -** NULL value, then *prNotFound is left unchanged.
         1577  +** to *prRhsHasNull. If there is no chance that the (...) contains a
         1578  +** NULL value, then *prRhsHasNull is left unchanged.
  1526   1579   **
  1527         -** If a register is allocated and its location stored in *prNotFound, then
  1528         -** its initial value is NULL.  If the (...) does not remain constant
  1529         -** for the duration of the query (i.e. the SELECT within the (...)
  1530         -** is a correlated subquery) then the value of the allocated register is
  1531         -** reset to NULL each time the subquery is rerun. This allows the
  1532         -** caller to use vdbe code equivalent to the following:
  1533         -**
  1534         -**   if( register==NULL ){
  1535         -**     has_null = <test if data structure contains null>
  1536         -**     register = 1
  1537         -**   }
  1538         -**
  1539         -** in order to avoid running the <test if data structure contains null>
  1540         -** test more often than is necessary.
         1580  +** If a register is allocated and its location stored in *prRhsHasNull, then
         1581  +** the value in that register will be NULL if the b-tree contains one or more
         1582  +** NULL values, and it will be some non-NULL value if the b-tree contains no
         1583  +** NULL values.
  1541   1584   */
  1542   1585   #ifndef SQLITE_OMIT_SUBQUERY
  1543         -int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
         1586  +int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){
  1544   1587     Select *p;                            /* SELECT to the right of IN operator */
  1545   1588     int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
  1546   1589     int iTab = pParse->nTab++;            /* Cursor of the RHS table */
  1547         -  int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
         1590  +  int mustBeUnique;                     /* True if RHS must be unique */
  1548   1591     Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
  1549   1592   
  1550   1593     assert( pX->op==TK_IN );
         1594  +  mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
  1551   1595   
  1552   1596     /* Check to see if an existing table or index can be used to
  1553   1597     ** satisfy the query.  This is preferable to generating a new 
  1554   1598     ** ephemeral table.
  1555   1599     */
  1556   1600     p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
  1557   1601     if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
................................................................................
  1600   1644         ** it is not, it is not possible to use any index.
  1601   1645         */
  1602   1646         int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
  1603   1647   
  1604   1648         for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
  1605   1649           if( (pIdx->aiColumn[0]==iCol)
  1606   1650            && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
  1607         -         && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
         1651  +         && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx)))
  1608   1652           ){
  1609   1653             int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
  1610   1654             sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
  1611   1655             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1612   1656             VdbeComment((v, "%s", pIdx->zName));
  1613   1657             assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
  1614   1658             eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
  1615   1659   
  1616         -          if( prNotFound && !pTab->aCol[iCol].notNull ){
  1617         -            *prNotFound = ++pParse->nMem;
  1618         -            sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
         1660  +          if( prRhsHasNull && !pTab->aCol[iCol].notNull ){
         1661  +            *prRhsHasNull = ++pParse->nMem;
         1662  +            sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull);
  1619   1663             }
  1620   1664             sqlite3VdbeJumpHere(v, iAddr);
  1621   1665           }
  1622   1666         }
  1623   1667       }
  1624   1668     }
         1669  +
         1670  +  /* If no preexisting index is available for the IN clause
         1671  +  ** and IN_INDEX_NOOP is an allowed reply
         1672  +  ** and the RHS of the IN operator is a list, not a subquery
         1673  +  ** and the RHS is not contant or has two or fewer terms,
         1674  +  ** then it is not worth creating an ephermeral table to evaluate
         1675  +  ** the IN operator so return IN_INDEX_NOOP.
         1676  +  */
         1677  +  if( eType==0
         1678  +   && (inFlags & IN_INDEX_NOOP_OK)
         1679  +   && !ExprHasProperty(pX, EP_xIsSelect)
         1680  +   && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
         1681  +  ){
         1682  +    eType = IN_INDEX_NOOP;
         1683  +  }
         1684  +     
  1625   1685   
  1626   1686     if( eType==0 ){
  1627         -    /* Could not found an existing table or index to use as the RHS b-tree.
         1687  +    /* Could not find an existing table or index to use as the RHS b-tree.
  1628   1688       ** We will have to generate an ephemeral table to do the job.
  1629   1689       */
  1630   1690       u32 savedNQueryLoop = pParse->nQueryLoop;
  1631   1691       int rMayHaveNull = 0;
  1632   1692       eType = IN_INDEX_EPH;
  1633         -    if( prNotFound ){
  1634         -      *prNotFound = rMayHaveNull = ++pParse->nMem;
  1635         -      sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
  1636         -    }else{
         1693  +    if( inFlags & IN_INDEX_LOOP ){
  1637   1694         pParse->nQueryLoop = 0;
  1638   1695         if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
  1639   1696           eType = IN_INDEX_ROWID;
  1640   1697         }
         1698  +    }else if( prRhsHasNull ){
         1699  +      *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
  1641   1700       }
  1642   1701       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
  1643   1702       pParse->nQueryLoop = savedNQueryLoop;
  1644   1703     }else{
  1645   1704       pX->iTable = iTab;
  1646   1705     }
  1647   1706     return eType;
................................................................................
  1664   1723   ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
  1665   1724   ** to some integer key column of a table B-Tree. In this case, use an
  1666   1725   ** intkey B-Tree to store the set of IN(...) values instead of the usual
  1667   1726   ** (slower) variable length keys B-Tree.
  1668   1727   **
  1669   1728   ** If rMayHaveNull is non-zero, that means that the operation is an IN
  1670   1729   ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
  1671         -** Furthermore, the IN is in a WHERE clause and that we really want
  1672         -** to iterate over the RHS of the IN operator in order to quickly locate
  1673         -** all corresponding LHS elements.  All this routine does is initialize
  1674         -** the register given by rMayHaveNull to NULL.  Calling routines will take
  1675         -** care of changing this register value to non-NULL if the RHS is NULL-free.
  1676         -**
  1677         -** If rMayHaveNull is zero, that means that the subquery is being used
  1678         -** for membership testing only.  There is no need to initialize any
  1679         -** registers to indicate the presence or absence of NULLs on the RHS.
         1730  +** All this routine does is initialize the register given by rMayHaveNull
         1731  +** to NULL.  Calling routines will take care of changing this register
         1732  +** value to non-NULL if the RHS is NULL-free.
  1680   1733   **
  1681   1734   ** For a SELECT or EXISTS operator, return the register that holds the
  1682   1735   ** result.  For IN operators or if an error occurs, the return value is 0.
  1683   1736   */
  1684   1737   #ifndef SQLITE_OMIT_SUBQUERY
  1685   1738   int sqlite3CodeSubselect(
  1686   1739     Parse *pParse,          /* Parsing context */
  1687   1740     Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
  1688         -  int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
         1741  +  int rHasNullFlag,       /* Register that records whether NULLs exist in RHS */
  1689   1742     int isRowid             /* If true, LHS of IN operator is a rowid */
  1690   1743   ){
  1691         -  int testAddr = -1;                      /* One-time test address */
         1744  +  int jmpIfDynamic = -1;                      /* One-time test address */
  1692   1745     int rReg = 0;                           /* Register storing resulting */
  1693   1746     Vdbe *v = sqlite3GetVdbe(pParse);
  1694   1747     if( NEVER(v==0) ) return 0;
  1695   1748     sqlite3ExprCachePush(pParse);
  1696   1749   
  1697   1750     /* This code must be run in its entirety every time it is encountered
  1698   1751     ** if any of the following is true:
................................................................................
  1701   1754     **    *  The right-hand side is an expression list containing variables
  1702   1755     **    *  We are inside a trigger
  1703   1756     **
  1704   1757     ** If all of the above are false, then we can run this code just once
  1705   1758     ** save the results, and reuse the same result on subsequent invocations.
  1706   1759     */
  1707   1760     if( !ExprHasProperty(pExpr, EP_VarSelect) ){
  1708         -    testAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
         1761  +    jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v);
  1709   1762     }
  1710   1763   
  1711   1764   #ifndef SQLITE_OMIT_EXPLAIN
  1712   1765     if( pParse->explain==2 ){
  1713   1766       char *zMsg = sqlite3MPrintf(
  1714         -        pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
         1767  +        pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ",
  1715   1768           pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
  1716   1769       );
  1717   1770       sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
  1718   1771     }
  1719   1772   #endif
  1720   1773   
  1721   1774     switch( pExpr->op ){
  1722   1775       case TK_IN: {
  1723   1776         char affinity;              /* Affinity of the LHS of the IN */
  1724   1777         int addr;                   /* Address of OP_OpenEphemeral instruction */
  1725   1778         Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
  1726   1779         KeyInfo *pKeyInfo = 0;      /* Key information */
  1727   1780   
  1728         -      if( rMayHaveNull ){
  1729         -        sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
  1730         -      }
  1731         -
  1732   1781         affinity = sqlite3ExprAffinity(pLeft);
  1733   1782   
  1734   1783         /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
  1735   1784         ** expression it is handled the same way.  An ephemeral table is 
  1736   1785         ** filled with single-field index keys representing the results
  1737   1786         ** from the SELECT or the <exprlist>.
  1738   1787         **
................................................................................
  1750   1799   
  1751   1800         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1752   1801           /* Case 1:     expr IN (SELECT ...)
  1753   1802           **
  1754   1803           ** Generate code to write the results of the select into the temporary
  1755   1804           ** table allocated and opened above.
  1756   1805           */
         1806  +        Select *pSelect = pExpr->x.pSelect;
  1757   1807           SelectDest dest;
  1758   1808           ExprList *pEList;
  1759   1809   
  1760   1810           assert( !isRowid );
  1761   1811           sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
  1762   1812           dest.affSdst = (u8)affinity;
  1763   1813           assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
  1764         -        pExpr->x.pSelect->iLimit = 0;
         1814  +        pSelect->iLimit = 0;
         1815  +        testcase( pSelect->selFlags & SF_Distinct );
         1816  +        pSelect->selFlags &= ~SF_Distinct;
  1765   1817           testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
  1766         -        if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
         1818  +        if( sqlite3Select(pParse, pSelect, &dest) ){
  1767   1819             sqlite3KeyInfoUnref(pKeyInfo);
  1768   1820             return 0;
  1769   1821           }
  1770         -        pEList = pExpr->x.pSelect->pEList;
         1822  +        pEList = pSelect->pEList;
  1771   1823           assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
  1772   1824           assert( pEList!=0 );
  1773   1825           assert( pEList->nExpr>0 );
  1774   1826           assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  1775   1827           pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
  1776   1828                                                            pEList->a[0].pExpr);
  1777   1829         }else if( ALWAYS(pExpr->x.pList!=0) ){
................................................................................
  1794   1846             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  1795   1847             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  1796   1848           }
  1797   1849   
  1798   1850           /* Loop through each expression in <exprlist>. */
  1799   1851           r1 = sqlite3GetTempReg(pParse);
  1800   1852           r2 = sqlite3GetTempReg(pParse);
  1801         -        sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
         1853  +        if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
  1802   1854           for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
  1803   1855             Expr *pE2 = pItem->pExpr;
  1804   1856             int iValToIns;
  1805   1857   
  1806   1858             /* If the expression is not constant then we will need to
  1807   1859             ** disable the test that was generated above that makes sure
  1808   1860             ** this code only executes once.  Because for a non-constant
  1809   1861             ** expression we need to rerun this code each time.
  1810   1862             */
  1811         -          if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
  1812         -            sqlite3VdbeChangeToNoop(v, testAddr);
  1813         -            testAddr = -1;
         1863  +          if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){
         1864  +            sqlite3VdbeChangeToNoop(v, jmpIfDynamic);
         1865  +            jmpIfDynamic = -1;
  1814   1866             }
  1815   1867   
  1816   1868             /* Evaluate the expression and insert it into the temp table */
  1817   1869             if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
  1818   1870               sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
  1819   1871             }else{
  1820   1872               r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
................................................................................
  1876   1928         }
  1877   1929         rReg = dest.iSDParm;
  1878   1930         ExprSetVVAProperty(pExpr, EP_NoReduce);
  1879   1931         break;
  1880   1932       }
  1881   1933     }
  1882   1934   
  1883         -  if( testAddr>=0 ){
  1884         -    sqlite3VdbeJumpHere(v, testAddr);
         1935  +  if( rHasNullFlag ){
         1936  +    sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag);
         1937  +  }
         1938  +
         1939  +  if( jmpIfDynamic>=0 ){
         1940  +    sqlite3VdbeJumpHere(v, jmpIfDynamic);
  1885   1941     }
  1886   1942     sqlite3ExprCachePop(pParse);
  1887   1943   
  1888   1944     return rReg;
  1889   1945   }
  1890   1946   #endif /* SQLITE_OMIT_SUBQUERY */
  1891   1947   
................................................................................
  1898   1954   **
  1899   1955   ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
  1900   1956   ** is an array of zero or more values.  The expression is true if the LHS is
  1901   1957   ** contained within the RHS.  The value of the expression is unknown (NULL)
  1902   1958   ** if the LHS is NULL or if the LHS is not contained within the RHS and the
  1903   1959   ** RHS contains one or more NULL values.
  1904   1960   **
  1905         -** This routine generates code will jump to destIfFalse if the LHS is not 
         1961  +** This routine generates code that jumps to destIfFalse if the LHS is not 
  1906   1962   ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
  1907   1963   ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
  1908   1964   ** within the RHS then fall through.
  1909   1965   */
  1910   1966   static void sqlite3ExprCodeIN(
  1911   1967     Parse *pParse,        /* Parsing and code generating context */
  1912   1968     Expr *pExpr,          /* The IN expression */
................................................................................
  1921   1977   
  1922   1978     /* Compute the RHS.   After this step, the table with cursor
  1923   1979     ** pExpr->iTable will contains the values that make up the RHS.
  1924   1980     */
  1925   1981     v = pParse->pVdbe;
  1926   1982     assert( v!=0 );       /* OOM detected prior to this routine */
  1927   1983     VdbeNoopComment((v, "begin IN expr"));
  1928         -  eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
         1984  +  eType = sqlite3FindInIndex(pParse, pExpr,
         1985  +                             IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK,
         1986  +                             destIfFalse==destIfNull ? 0 : &rRhsHasNull);
  1929   1987   
  1930   1988     /* Figure out the affinity to use to create a key from the results
  1931   1989     ** of the expression. affinityStr stores a static string suitable for
  1932   1990     ** P4 of OP_MakeRecord.
  1933   1991     */
  1934   1992     affinity = comparisonAffinity(pExpr);
  1935   1993   
  1936   1994     /* Code the LHS, the <expr> from "<expr> IN (...)".
  1937   1995     */
  1938   1996     sqlite3ExprCachePush(pParse);
  1939   1997     r1 = sqlite3GetTempReg(pParse);
  1940   1998     sqlite3ExprCode(pParse, pExpr->pLeft, r1);
  1941   1999   
  1942         -  /* If the LHS is NULL, then the result is either false or NULL depending
  1943         -  ** on whether the RHS is empty or not, respectively.
         2000  +  /* If sqlite3FindInIndex() did not find or create an index that is
         2001  +  ** suitable for evaluating the IN operator, then evaluate using a
         2002  +  ** sequence of comparisons.
  1944   2003     */
  1945         -  if( destIfNull==destIfFalse ){
  1946         -    /* Shortcut for the common case where the false and NULL outcomes are
  1947         -    ** the same. */
  1948         -    sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
  1949         -  }else{
  1950         -    int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
  1951         -    sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
  1952         -    VdbeCoverage(v);
  1953         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
  1954         -    sqlite3VdbeJumpHere(v, addr1);
  1955         -  }
  1956         -
  1957         -  if( eType==IN_INDEX_ROWID ){
  1958         -    /* In this case, the RHS is the ROWID of table b-tree
  1959         -    */
  1960         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
  1961         -    sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
  1962         -    VdbeCoverage(v);
         2004  +  if( eType==IN_INDEX_NOOP ){
         2005  +    ExprList *pList = pExpr->x.pList;
         2006  +    CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
         2007  +    int labelOk = sqlite3VdbeMakeLabel(v);
         2008  +    int r2, regToFree;
         2009  +    int regCkNull = 0;
         2010  +    int ii;
         2011  +    assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
         2012  +    if( destIfNull!=destIfFalse ){
         2013  +      regCkNull = sqlite3GetTempReg(pParse);
         2014  +      sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull);
         2015  +    }
         2016  +    for(ii=0; ii<pList->nExpr; ii++){
         2017  +      r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
         2018  +      if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
         2019  +        sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
         2020  +      }
         2021  +      if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
         2022  +        sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2,
         2023  +                          (void*)pColl, P4_COLLSEQ);
         2024  +        VdbeCoverageIf(v, ii<pList->nExpr-1);
         2025  +        VdbeCoverageIf(v, ii==pList->nExpr-1);
         2026  +        sqlite3VdbeChangeP5(v, affinity);
         2027  +      }else{
         2028  +        assert( destIfNull==destIfFalse );
         2029  +        sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2,
         2030  +                          (void*)pColl, P4_COLLSEQ); VdbeCoverage(v);
         2031  +        sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL);
         2032  +      }
         2033  +      sqlite3ReleaseTempReg(pParse, regToFree);
         2034  +    }
         2035  +    if( regCkNull ){
         2036  +      sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v);
         2037  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
         2038  +    }
         2039  +    sqlite3VdbeResolveLabel(v, labelOk);
         2040  +    sqlite3ReleaseTempReg(pParse, regCkNull);
  1963   2041     }else{
  1964         -    /* In this case, the RHS is an index b-tree.
  1965         -    */
  1966         -    sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
  1967         -
  1968         -    /* If the set membership test fails, then the result of the 
  1969         -    ** "x IN (...)" expression must be either 0 or NULL. If the set
  1970         -    ** contains no NULL values, then the result is 0. If the set 
  1971         -    ** contains one or more NULL values, then the result of the
  1972         -    ** expression is also NULL.
         2042  +  
         2043  +    /* If the LHS is NULL, then the result is either false or NULL depending
         2044  +    ** on whether the RHS is empty or not, respectively.
  1973   2045       */
  1974         -    if( rRhsHasNull==0 || destIfFalse==destIfNull ){
  1975         -      /* This branch runs if it is known at compile time that the RHS
  1976         -      ** cannot contain NULL values. This happens as the result
  1977         -      ** of a "NOT NULL" constraint in the database schema.
  1978         -      **
  1979         -      ** Also run this branch if NULL is equivalent to FALSE
  1980         -      ** for this particular IN operator.
         2046  +    if( sqlite3ExprCanBeNull(pExpr->pLeft) ){
         2047  +      if( destIfNull==destIfFalse ){
         2048  +        /* Shortcut for the common case where the false and NULL outcomes are
         2049  +        ** the same. */
         2050  +        sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
         2051  +      }else{
         2052  +        int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
         2053  +        sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
         2054  +        VdbeCoverage(v);
         2055  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
         2056  +        sqlite3VdbeJumpHere(v, addr1);
         2057  +      }
         2058  +    }
         2059  +  
         2060  +    if( eType==IN_INDEX_ROWID ){
         2061  +      /* In this case, the RHS is the ROWID of table b-tree
  1981   2062         */
  1982         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
         2063  +      sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
         2064  +      sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
  1983   2065         VdbeCoverage(v);
  1984   2066       }else{
  1985         -      /* In this branch, the RHS of the IN might contain a NULL and
  1986         -      ** the presence of a NULL on the RHS makes a difference in the
  1987         -      ** outcome.
         2067  +      /* In this case, the RHS is an index b-tree.
  1988   2068         */
  1989         -      int j1, j2;
  1990         -
  1991         -      /* First check to see if the LHS is contained in the RHS.  If so,
  1992         -      ** then the presence of NULLs in the RHS does not matter, so jump
  1993         -      ** over all of the code that follows.
         2069  +      sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
         2070  +  
         2071  +      /* If the set membership test fails, then the result of the 
         2072  +      ** "x IN (...)" expression must be either 0 or NULL. If the set
         2073  +      ** contains no NULL values, then the result is 0. If the set 
         2074  +      ** contains one or more NULL values, then the result of the
         2075  +      ** expression is also NULL.
  1994   2076         */
  1995         -      j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
  1996         -      VdbeCoverage(v);
  1997         -
  1998         -      /* Here we begin generating code that runs if the LHS is not
  1999         -      ** contained within the RHS.  Generate additional code that
  2000         -      ** tests the RHS for NULLs.  If the RHS contains a NULL then
  2001         -      ** jump to destIfNull.  If there are no NULLs in the RHS then
  2002         -      ** jump to destIfFalse.
  2003         -      */
  2004         -      sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
  2005         -      sqlite3VdbeAddOp2(v, OP_IfNot, rRhsHasNull, destIfFalse); VdbeCoverage(v);
  2006         -      j2 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
  2007         -      VdbeCoverage(v);
  2008         -      sqlite3VdbeAddOp2(v, OP_Integer, 0, rRhsHasNull);
  2009         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
  2010         -      sqlite3VdbeJumpHere(v, j2);
  2011         -      sqlite3VdbeAddOp2(v, OP_Integer, 1, rRhsHasNull);
  2012         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
  2013         -
  2014         -      /* The OP_Found at the top of this branch jumps here when true, 
  2015         -      ** causing the overall IN expression evaluation to fall through.
  2016         -      */
  2017         -      sqlite3VdbeJumpHere(v, j1);
         2077  +      assert( destIfFalse!=destIfNull || rRhsHasNull==0 );
         2078  +      if( rRhsHasNull==0 ){
         2079  +        /* This branch runs if it is known at compile time that the RHS
         2080  +        ** cannot contain NULL values. This happens as the result
         2081  +        ** of a "NOT NULL" constraint in the database schema.
         2082  +        **
         2083  +        ** Also run this branch if NULL is equivalent to FALSE
         2084  +        ** for this particular IN operator.
         2085  +        */
         2086  +        sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
         2087  +        VdbeCoverage(v);
         2088  +      }else{
         2089  +        /* In this branch, the RHS of the IN might contain a NULL and
         2090  +        ** the presence of a NULL on the RHS makes a difference in the
         2091  +        ** outcome.
         2092  +        */
         2093  +        int j1;
         2094  +  
         2095  +        /* First check to see if the LHS is contained in the RHS.  If so,
         2096  +        ** then the answer is TRUE the presence of NULLs in the RHS does
         2097  +        ** not matter.  If the LHS is not contained in the RHS, then the
         2098  +        ** answer is NULL if the RHS contains NULLs and the answer is
         2099  +        ** FALSE if the RHS is NULL-free.
         2100  +        */
         2101  +        j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
         2102  +        VdbeCoverage(v);
         2103  +        sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull);
         2104  +        VdbeCoverage(v);
         2105  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
         2106  +        sqlite3VdbeJumpHere(v, j1);
         2107  +      }
  2018   2108       }
  2019   2109     }
  2020   2110     sqlite3ReleaseTempReg(pParse, r1);
  2021   2111     sqlite3ExprCachePop(pParse);
  2022   2112     VdbeComment((v, "end IN expr"));
  2023   2113   }
  2024   2114   #endif /* SQLITE_OMIT_SUBQUERY */
................................................................................
  2634   2724         assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
  2635   2725         sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
  2636   2726         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  2637   2727         testcase( regFree1==0 );
  2638   2728         addr = sqlite3VdbeAddOp1(v, op, r1);
  2639   2729         VdbeCoverageIf(v, op==TK_ISNULL);
  2640   2730         VdbeCoverageIf(v, op==TK_NOTNULL);
  2641         -      sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
         2731  +      sqlite3VdbeAddOp2(v, OP_Integer, 0, target);
  2642   2732         sqlite3VdbeJumpHere(v, addr);
  2643   2733         break;
  2644   2734       }
  2645   2735       case TK_AGG_FUNCTION: {
  2646   2736         AggInfo *pInfo = pExpr->pAggInfo;
  2647   2737         if( pInfo==0 ){
  2648   2738           assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
  2670   2760           pFarg = pExpr->x.pList;
  2671   2761         }
  2672   2762         nFarg = pFarg ? pFarg->nExpr : 0;
  2673   2763         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2674   2764         zId = pExpr->u.zToken;
  2675   2765         nId = sqlite3Strlen30(zId);
  2676   2766         pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
  2677         -      if( pDef==0 ){
         2767  +      if( pDef==0 || pDef->xFunc==0 ){
  2678   2768           sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
  2679   2769           break;
  2680   2770         }
  2681   2771   
  2682   2772         /* Attempt a direct implementation of the built-in COALESCE() and
  2683   2773         ** IFNULL() functions.  This avoids unnecessary evalation of
  2684   2774         ** arguments past the first non-NULL argument.

Changes to src/fkey.c.

   221    221       assert( nCol>1 );
   222    222       aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
   223    223       if( !aiCol ) return 1;
   224    224       *paiCol = aiCol;
   225    225     }
   226    226   
   227    227     for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
   228         -    if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){ 
          228  +    if( pIdx->nKeyCol==nCol && IsUniqueIndex(pIdx) ){ 
   229    229         /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
   230    230         ** of columns. If each indexed column corresponds to a foreign key
   231    231         ** column of pFKey, then this index is a winner.  */
   232    232   
   233    233         if( zKey==0 ){
   234    234           /* If zKey is NULL, then this foreign key is implicitly mapped to 
   235    235           ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be 

Changes to src/insert.c.

  1894   1894        && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0) 
  1895   1895            || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
  1896   1896       ){
  1897   1897         return 0;    /* Default values must be the same for all columns */
  1898   1898       }
  1899   1899     }
  1900   1900     for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
  1901         -    if( pDestIdx->onError!=OE_None ){
         1901  +    if( IsUniqueIndex(pDestIdx) ){
  1902   1902         destHasUniqueIdx = 1;
  1903   1903       }
  1904   1904       for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
  1905   1905         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
  1906   1906       }
  1907   1907       if( pSrcIdx==0 ){
  1908   1908         return 0;    /* pDestIdx has no corresponding index in pSrc */

Changes to src/mutex.c.

    77     77   }
    78     78   
    79     79   /*
    80     80   ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
    81     81   */
    82     82   sqlite3_mutex *sqlite3_mutex_alloc(int id){
    83     83   #ifndef SQLITE_OMIT_AUTOINIT
    84         -  if( sqlite3_initialize() ) return 0;
           84  +  if( id<=SQLITE_MUTEX_RECURSIVE && sqlite3_initialize() ) return 0;
    85     85   #endif
    86     86     return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
    87     87   }
    88     88   
    89     89   sqlite3_mutex *sqlite3MutexAlloc(int id){
    90     90     if( !sqlite3GlobalConfig.bCoreMutex ){
    91     91       return 0;

Changes to src/mutex_noop.c.

   103    103   
   104    104   /*
   105    105   ** The sqlite3_mutex_alloc() routine allocates a new
   106    106   ** mutex and returns a pointer to it.  If it returns NULL
   107    107   ** that means that a mutex could not be allocated. 
   108    108   */
   109    109   static sqlite3_mutex *debugMutexAlloc(int id){
   110         -  static sqlite3_debug_mutex aStatic[6];
          110  +  static sqlite3_debug_mutex aStatic[SQLITE_MUTEX_STATIC_APP3 - 1];
   111    111     sqlite3_debug_mutex *pNew = 0;
   112    112     switch( id ){
   113    113       case SQLITE_MUTEX_FAST:
   114    114       case SQLITE_MUTEX_RECURSIVE: {
   115    115         pNew = sqlite3Malloc(sizeof(*pNew));
   116    116         if( pNew ){
   117    117           pNew->id = id;

Changes to src/mutex_unix.c.

    92     92   ** to sqlite3_mutex_alloc() is one of these integer constants:
    93     93   **
    94     94   ** <ul>
    95     95   ** <li>  SQLITE_MUTEX_FAST
    96     96   ** <li>  SQLITE_MUTEX_RECURSIVE
    97     97   ** <li>  SQLITE_MUTEX_STATIC_MASTER
    98     98   ** <li>  SQLITE_MUTEX_STATIC_MEM
    99         -** <li>  SQLITE_MUTEX_STATIC_MEM2
           99  +** <li>  SQLITE_MUTEX_STATIC_OPEN
   100    100   ** <li>  SQLITE_MUTEX_STATIC_PRNG
   101    101   ** <li>  SQLITE_MUTEX_STATIC_LRU
   102    102   ** <li>  SQLITE_MUTEX_STATIC_PMEM
          103  +** <li>  SQLITE_MUTEX_STATIC_APP1
          104  +** <li>  SQLITE_MUTEX_STATIC_APP2
          105  +** <li>  SQLITE_MUTEX_STATIC_APP3
   103    106   ** </ul>
   104    107   **
   105    108   ** The first two constants cause sqlite3_mutex_alloc() to create
   106    109   ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   107    110   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   108    111   ** The mutex implementation does not need to make a distinction
   109    112   ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
................................................................................
   124    127   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
   125    128   ** returns a different mutex on every call.  But for the static 
   126    129   ** mutex types, the same mutex is returned on every call that has
   127    130   ** the same type number.
   128    131   */
   129    132   static sqlite3_mutex *pthreadMutexAlloc(int iType){
   130    133     static sqlite3_mutex staticMutexes[] = {
          134  +    SQLITE3_MUTEX_INITIALIZER,
          135  +    SQLITE3_MUTEX_INITIALIZER,
          136  +    SQLITE3_MUTEX_INITIALIZER,
   131    137       SQLITE3_MUTEX_INITIALIZER,
   132    138       SQLITE3_MUTEX_INITIALIZER,
   133    139       SQLITE3_MUTEX_INITIALIZER,
   134    140       SQLITE3_MUTEX_INITIALIZER,
   135    141       SQLITE3_MUTEX_INITIALIZER,
   136    142       SQLITE3_MUTEX_INITIALIZER
   137    143     };

Changes to src/mutex_w32.c.

    76     76     return winMutexNotheld2(p, tid);
    77     77   }
    78     78   #endif
    79     79   
    80     80   /*
    81     81   ** Initialize and deinitialize the mutex subsystem.
    82     82   */
    83         -static sqlite3_mutex winMutex_staticMutexes[6] = {
           83  +static sqlite3_mutex winMutex_staticMutexes[] = {
           84  +  SQLITE3_MUTEX_INITIALIZER,
           85  +  SQLITE3_MUTEX_INITIALIZER,
           86  +  SQLITE3_MUTEX_INITIALIZER,
    84     87     SQLITE3_MUTEX_INITIALIZER,
    85     88     SQLITE3_MUTEX_INITIALIZER,
    86     89     SQLITE3_MUTEX_INITIALIZER,
    87     90     SQLITE3_MUTEX_INITIALIZER,
    88     91     SQLITE3_MUTEX_INITIALIZER,
    89     92     SQLITE3_MUTEX_INITIALIZER
    90     93   };
    91     94   
    92     95   static int winMutex_isInit = 0;
           96  +static int winMutex_isNt = -1; /* <0 means "need to query" */
    93     97   
    94         -/* As winMutexInit() and winMutexEnd() are called as part of the
    95         -** sqlite3_initialize() and sqlite3_shutdown() processing, the
    96         -** "interlocked" magic used in this section may not strictly
    97         -** necessary.
           98  +/* As the winMutexInit() and winMutexEnd() functions are called as part
           99  +** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
          100  +** "interlocked" magic used here is probably not strictly necessary.
    98    101   */
    99         -static LONG winMutex_lock = 0;
          102  +static LONG volatile winMutex_lock = 0;
   100    103   
   101    104   int sqlite3_win32_is_nt(void); /* os_win.c */
   102    105   void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
   103    106   
   104    107   static int winMutexInit(void){
   105    108     /* The first to increment to 1 does actual initialization */
   106    109     if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
................................................................................
   146    149   ** to sqlite3_mutex_alloc() is one of these integer constants:
   147    150   **
   148    151   ** <ul>
   149    152   ** <li>  SQLITE_MUTEX_FAST
   150    153   ** <li>  SQLITE_MUTEX_RECURSIVE
   151    154   ** <li>  SQLITE_MUTEX_STATIC_MASTER
   152    155   ** <li>  SQLITE_MUTEX_STATIC_MEM
   153         -** <li>  SQLITE_MUTEX_STATIC_MEM2
          156  +** <li>  SQLITE_MUTEX_STATIC_OPEN
   154    157   ** <li>  SQLITE_MUTEX_STATIC_PRNG
   155    158   ** <li>  SQLITE_MUTEX_STATIC_LRU
   156    159   ** <li>  SQLITE_MUTEX_STATIC_PMEM
          160  +** <li>  SQLITE_MUTEX_STATIC_APP1
          161  +** <li>  SQLITE_MUTEX_STATIC_APP2
          162  +** <li>  SQLITE_MUTEX_STATIC_APP3
   157    163   ** </ul>
   158    164   **
   159    165   ** The first two constants cause sqlite3_mutex_alloc() to create
   160    166   ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
   161    167   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
   162    168   ** The mutex implementation does not need to make a distinction
   163    169   ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
................................................................................
   227    233   */
   228    234   static void winMutexFree(sqlite3_mutex *p){
   229    235     assert( p );
   230    236   #ifdef SQLITE_DEBUG
   231    237     assert( p->nRef==0 && p->owner==0 );
   232    238     assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
   233    239   #endif
          240  +  assert( winMutex_isInit==1 );
   234    241     DeleteCriticalSection(&p->mutex);
   235    242     sqlite3_free(p);
   236    243   }
   237    244   
   238    245   /*
   239    246   ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
   240    247   ** to enter a mutex.  If another thread is already within the mutex,
................................................................................
   252    259   #endif
   253    260   #ifdef SQLITE_DEBUG
   254    261     assert( p );
   255    262     assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
   256    263   #else
   257    264     assert( p );
   258    265   #endif
          266  +  assert( winMutex_isInit==1 );
   259    267     EnterCriticalSection(&p->mutex);
   260    268   #ifdef SQLITE_DEBUG
   261    269     assert( p->nRef>0 || p->owner==0 );
   262    270     p->owner = tid;
   263    271     p->nRef++;
   264    272     if( p->trace ){
   265    273       OSTRACE(("ENTER-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
................................................................................
   283    291     ** The TryEnterCriticalSection() interface is only available on WinNT.
   284    292     ** And some windows compilers complain if you try to use it without
   285    293     ** first doing some #defines that prevent SQLite from building on Win98.
   286    294     ** For that reason, we will omit this optimization for now.  See
   287    295     ** ticket #2685.
   288    296     */
   289    297   #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0400
   290         -  if( sqlite3_win32_is_nt() && TryEnterCriticalSection(&p->mutex) ){
          298  +  assert( winMutex_isInit==1 );
          299  +  assert( winMutex_isNt>=-1 && winMutex_isNt<=1 );
          300  +  if( winMutex_isNt<0 ){
          301  +    winMutex_isNt = sqlite3_win32_is_nt();
          302  +  }
          303  +  assert( winMutex_isNt==0 || winMutex_isNt==1 );
          304  +  if( winMutex_isNt && TryEnterCriticalSection(&p->mutex) ){
   291    305   #ifdef SQLITE_DEBUG
   292    306       p->owner = tid;
   293    307       p->nRef++;
   294    308   #endif
   295    309       rc = SQLITE_OK;
   296    310     }
   297    311   #else
................................................................................
   320    334   #ifdef SQLITE_DEBUG
   321    335     assert( p->nRef>0 );
   322    336     assert( p->owner==tid );
   323    337     p->nRef--;
   324    338     if( p->nRef==0 ) p->owner = 0;
   325    339     assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
   326    340   #endif
          341  +  assert( winMutex_isInit==1 );
   327    342     LeaveCriticalSection(&p->mutex);
   328    343   #ifdef SQLITE_DEBUG
   329    344     if( p->trace ){
   330    345       OSTRACE(("LEAVE-MUTEX tid=%lu, mutex=%p (%d), nRef=%d\n",
   331    346                tid, p, p->trace, p->nRef));
   332    347     }
   333    348   #endif

Changes to src/os_win.c.

   410    410   ** 1:   Operating system is Win9x.
   411    411   ** 2:   Operating system is WinNT.
   412    412   **
   413    413   ** In order to facilitate testing on a WinNT system, the test fixture
   414    414   ** can manually set this value to 1 to emulate Win98 behavior.
   415    415   */
   416    416   #ifdef SQLITE_TEST
   417         -int sqlite3_os_type = 0;
   418         -#elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
   419         -      defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
   420         -static int sqlite3_os_type = 0;
          417  +LONG volatile sqlite3_os_type = 0;
          418  +#else
          419  +static LONG volatile sqlite3_os_type = 0;
   421    420   #endif
   422    421   
   423    422   #ifndef SYSCALL
   424    423   #  define SYSCALL sqlite3_syscall_ptr
   425    424   #endif
   426    425   
   427    426   /*
................................................................................
  1043   1042     { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
  1044   1043   #else
  1045   1044     { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
  1046   1045   #endif
  1047   1046   
  1048   1047   #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
  1049   1048           LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
         1049  +
         1050  +  { "InterlockedCompareExchange", (SYSCALL)InterlockedCompareExchange, 0 },
         1051  +
         1052  +#define osInterlockedCompareExchange ((LONG(WINAPI*)(LONG volatile*, \
         1053  +        LONG,LONG))aSyscall[76].pCurrent)
  1050   1054   
  1051   1055   }; /* End of the overrideable system calls */
  1052   1056   
  1053   1057   /*
  1054   1058   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
  1055   1059   ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
  1056   1060   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
................................................................................
  1294   1298   #if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
  1295   1299   # define osIsNT()  (1)
  1296   1300   #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
  1297   1301   # define osIsNT()  (1)
  1298   1302   #elif !defined(SQLITE_WIN32_HAS_WIDE)
  1299   1303   # define osIsNT()  (0)
  1300   1304   #else
  1301         -# define osIsNT()  (sqlite3_win32_is_nt())
         1305  +# define osIsNT()  ((sqlite3_os_type==2) || sqlite3_win32_is_nt())
  1302   1306   #endif
  1303   1307   
  1304   1308   /*
  1305   1309   ** This function determines if the machine is running a version of Windows
  1306   1310   ** based on the NT kernel.
  1307   1311   */
  1308   1312   int sqlite3_win32_is_nt(void){
  1309         -  if( sqlite3_os_type==0 ){
         1313  +  if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
  1310   1314   #if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
  1311   1315       OSVERSIONINFOW sInfo;
  1312   1316       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1313   1317       osGetVersionExW(&sInfo);
  1314   1318   #else
  1315   1319       OSVERSIONINFOA sInfo;
  1316   1320       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
  1317   1321       osGetVersionExA(&sInfo);
  1318   1322   #endif
  1319         -    sqlite3_os_type = (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1;
         1323  +    osInterlockedCompareExchange(&sqlite3_os_type,
         1324  +        (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
  1320   1325     }
  1321         -  return (sqlite3_os_type == 2);
         1326  +  return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
  1322   1327   }
  1323   1328   
  1324   1329   #ifdef SQLITE_WIN32_MALLOC
  1325   1330   /*
  1326   1331   ** Allocate nBytes of memory.
  1327   1332   */
  1328   1333   static void *winMemMalloc(int nBytes){
................................................................................
  5471   5476       winGetSystemCall,    /* xGetSystemCall */
  5472   5477       winNextSystemCall,   /* xNextSystemCall */
  5473   5478     };
  5474   5479   #endif
  5475   5480   
  5476   5481     /* Double-check that the aSyscall[] array has been constructed
  5477   5482     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  5478         -  assert( ArraySize(aSyscall)==76 );
         5483  +  assert( ArraySize(aSyscall)==77 );
  5479   5484   
  5480   5485     /* get memory map allocation granularity */
  5481   5486     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  5482   5487   #if SQLITE_OS_WINRT
  5483   5488     osGetNativeSystemInfo(&winSysInfo);
  5484   5489   #else
  5485   5490     osGetSystemInfo(&winSysInfo);

Changes to src/pragma.c.

  1540   1540         sqlite3CodeVerifySchema(pParse, iDb);
  1541   1541         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
  1542   1542         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
  1543   1543         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
  1544   1544         for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
  1545   1545           sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
  1546   1546           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
  1547         -        sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
         1547  +        sqlite3VdbeAddOp2(v, OP_Integer, IsUniqueIndex(pIdx), 3);
  1548   1548           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
  1549   1549         }
  1550   1550       }
  1551   1551     }
  1552   1552     break;
  1553   1553   
  1554   1554     case PragTyp_DATABASE_LIST: {
................................................................................
  1790   1790   
  1791   1791       /* Code that appears at the end of the integrity check.  If no error
  1792   1792       ** messages have been generated, output OK.  Otherwise output the
  1793   1793       ** error message
  1794   1794       */
  1795   1795       static const int iLn = VDBE_OFFSET_LINENO(2);
  1796   1796       static const VdbeOpList endCode[] = {
  1797         -      { OP_AddImm,      1, 0,        0},    /* 0 */
  1798         -      { OP_IfNeg,       1, 0,        0},    /* 1 */
  1799         -      { OP_String8,     0, 3,        0},    /* 2 */
         1797  +      { OP_IfNeg,       1, 0,        0},    /* 0 */
         1798  +      { OP_String8,     0, 3,        0},    /* 1 */
  1800   1799         { OP_ResultRow,   3, 1,        0},
  1801   1800       };
  1802   1801   
  1803   1802       int isQuick = (sqlite3Tolower(zLeft[0])=='q');
  1804   1803   
  1805   1804       /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
  1806   1805       ** then iDb is set to the index of the database identified by <db>.
................................................................................
  1904   1903           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1905   1904           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1906   1905             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1907   1906           }
  1908   1907           pParse->nMem = MAX(pParse->nMem, 8+j);
  1909   1908           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
  1910   1909           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
         1910  +        /* Verify that all NOT NULL columns really are NOT NULL */
         1911  +        for(j=0; j<pTab->nCol; j++){
         1912  +          char *zErr;
         1913  +          int jmp2, jmp3;
         1914  +          if( j==pTab->iPKey ) continue;
         1915  +          if( pTab->aCol[j].notNull==0 ) continue;
         1916  +          sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, j, 3);
         1917  +          sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG);
         1918  +          jmp2 = sqlite3VdbeAddOp1(v, OP_NotNull, 3); VdbeCoverage(v);
         1919  +          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
         1920  +          zErr = sqlite3MPrintf(db, "NULL value in %s.%s", pTab->zName,
         1921  +                              pTab->aCol[j].zName);
         1922  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, zErr, P4_DYNAMIC);
         1923  +          sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
         1924  +          jmp3 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
         1925  +          sqlite3VdbeAddOp0(v, OP_Halt);
         1926  +          sqlite3VdbeJumpHere(v, jmp2);
         1927  +          sqlite3VdbeJumpHere(v, jmp3);
         1928  +        }
         1929  +        /* Validate index entries for the current row */
  1911   1930           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1912         -          int jmp2, jmp3, jmp4;
         1931  +          int jmp2, jmp3, jmp4, jmp5;
         1932  +          int ckUniq = sqlite3VdbeMakeLabel(v);
  1913   1933             if( pPk==pIdx ) continue;
  1914   1934             r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
  1915   1935                                          pPrior, r1);
  1916   1936             pPrior = pIdx;
  1917   1937             sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
  1918         -          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
         1938  +          /* Verify that an index entry exists for the current table row */
         1939  +          jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, ckUniq, r1,
  1919   1940                                         pIdx->nColumn); VdbeCoverage(v);
  1920   1941             sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
  1921   1942             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
  1922   1943             sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
  1923         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
  1924         -                            P4_STATIC);
         1944  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, 
         1945  +                            " missing from index ", P4_STATIC);
  1925   1946             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1926         -          sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
         1947  +          jmp5 = sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
         1948  +                                   pIdx->zName, P4_TRANSIENT);
  1927   1949             sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
  1928   1950             sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
  1929   1951             jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
  1930   1952             sqlite3VdbeAddOp0(v, OP_Halt);
  1931         -          sqlite3VdbeJumpHere(v, jmp4);
  1932   1953             sqlite3VdbeJumpHere(v, jmp2);
         1954  +          /* For UNIQUE indexes, verify that only one entry exists with the
         1955  +          ** current key.  The entry is unique if (1) any column is NULL
         1956  +          ** or (2) the next entry has a different key */
         1957  +          if( IsUniqueIndex(pIdx) ){
         1958  +            int uniqOk = sqlite3VdbeMakeLabel(v);
         1959  +            int jmp6;
         1960  +            int kk;
         1961  +            for(kk=0; kk<pIdx->nKeyCol; kk++){
         1962  +              int iCol = pIdx->aiColumn[kk];
         1963  +              assert( iCol>=0 && iCol<pTab->nCol );
         1964  +              if( pTab->aCol[iCol].notNull ) continue;
         1965  +              sqlite3VdbeAddOp2(v, OP_IsNull, r1+kk, uniqOk);
         1966  +              VdbeCoverage(v);
         1967  +            }
         1968  +            jmp6 = sqlite3VdbeAddOp1(v, OP_Next, iIdxCur+j); VdbeCoverage(v);
         1969  +            sqlite3VdbeAddOp2(v, OP_Goto, 0, uniqOk);
         1970  +            sqlite3VdbeJumpHere(v, jmp6);
         1971  +            sqlite3VdbeAddOp4Int(v, OP_IdxGT, iIdxCur+j, uniqOk, r1,
         1972  +                                 pIdx->nKeyCol); VdbeCoverage(v);
         1973  +            sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
         1974  +            sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
         1975  +                              "non-unique entry in index ", P4_STATIC);
         1976  +            sqlite3VdbeAddOp2(v, OP_Goto, 0, jmp5);
         1977  +            sqlite3VdbeResolveLabel(v, uniqOk);
         1978  +          }
         1979  +          sqlite3VdbeJumpHere(v, jmp4);
  1933   1980             sqlite3ResolvePartIdxLabel(pParse, jmp3);
  1934   1981           }
  1935   1982           sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
  1936   1983           sqlite3VdbeJumpHere(v, loopTop-1);
  1937   1984   #ifndef SQLITE_OMIT_BTREECOUNT
  1938   1985           sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
  1939   1986                        "wrong # of entries in index ", P4_STATIC);
................................................................................
  1950   1997             sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
  1951   1998             sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
  1952   1999           }
  1953   2000   #endif /* SQLITE_OMIT_BTREECOUNT */
  1954   2001         } 
  1955   2002       }
  1956   2003       addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
  1957         -    sqlite3VdbeChangeP2(v, addr, -mxErr);
  1958         -    sqlite3VdbeJumpHere(v, addr+1);
  1959         -    sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
         2004  +    sqlite3VdbeChangeP3(v, addr, -mxErr);
         2005  +    sqlite3VdbeJumpHere(v, addr);
         2006  +    sqlite3VdbeChangeP4(v, addr+1, "ok", P4_STATIC);
  1960   2007     }
  1961   2008     break;
  1962   2009   #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
  1963   2010   
  1964   2011   #ifndef SQLITE_OMIT_UTF16
  1965   2012     /*
  1966   2013     **   PRAGMA encoding

Changes to src/select.c.

   537    537   static void codeOffset(
   538    538     Vdbe *v,          /* Generate code into this VM */
   539    539     int iOffset,      /* Register holding the offset counter */
   540    540     int iContinue     /* Jump here to skip the current record */
   541    541   ){
   542    542     if( iOffset>0 ){
   543    543       int addr;
   544         -    sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
   545         -    addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
          544  +    addr = sqlite3VdbeAddOp3(v, OP_IfNeg, iOffset, 0, -1); VdbeCoverage(v);
   546    545       sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
   547    546       VdbeComment((v, "skip OFFSET records"));
   548    547       sqlite3VdbeJumpHere(v, addr);
   549    548     }
   550    549   }
   551    550   
   552    551   /*

Changes to src/sqlite.h.in.

  5871   5871   ** to sqlite3_mutex_alloc() is one of these integer constants:
  5872   5872   **
  5873   5873   ** <ul>
  5874   5874   ** <li>  SQLITE_MUTEX_FAST
  5875   5875   ** <li>  SQLITE_MUTEX_RECURSIVE
  5876   5876   ** <li>  SQLITE_MUTEX_STATIC_MASTER
  5877   5877   ** <li>  SQLITE_MUTEX_STATIC_MEM
  5878         -** <li>  SQLITE_MUTEX_STATIC_MEM2
         5878  +** <li>  SQLITE_MUTEX_STATIC_OPEN
  5879   5879   ** <li>  SQLITE_MUTEX_STATIC_PRNG
  5880   5880   ** <li>  SQLITE_MUTEX_STATIC_LRU
  5881         -** <li>  SQLITE_MUTEX_STATIC_LRU2
         5881  +** <li>  SQLITE_MUTEX_STATIC_PMEM
         5882  +** <li>  SQLITE_MUTEX_STATIC_APP1
         5883  +** <li>  SQLITE_MUTEX_STATIC_APP2
  5882   5884   ** </ul>)^
  5883   5885   **
  5884   5886   ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
  5885   5887   ** cause sqlite3_mutex_alloc() to create
  5886   5888   ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
  5887   5889   ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
  5888   5890   ** The mutex implementation does not need to make a distinction
................................................................................
  6078   6080   #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
  6079   6081   #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
  6080   6082   #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
  6081   6083   #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
  6082   6084   #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
  6083   6085   #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
  6084   6086   #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
         6087  +#define SQLITE_MUTEX_STATIC_APP1      8  /* For use by application */
         6088  +#define SQLITE_MUTEX_STATIC_APP2      9  /* For use by application */
         6089  +#define SQLITE_MUTEX_STATIC_APP3     10  /* For use by application */
  6085   6090   
  6086   6091   /*
  6087   6092   ** CAPI3REF: Retrieve the mutex for a database connection
  6088   6093   **
  6089   6094   ** ^This interface returns a pointer the [sqlite3_mutex] object that 
  6090   6095   ** serializes access to the [database connection] given in the argument
  6091   6096   ** when the [threading mode] is Serialized.

Changes to src/sqliteInt.h.

   873    873     Hash tblHash;        /* All tables indexed by name */
   874    874     Hash idxHash;        /* All (named) indices indexed by name */
   875    875     Hash trigHash;       /* All triggers indexed by name */
   876    876     Hash fkeyHash;       /* All foreign keys by referenced table name */
   877    877     Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
   878    878     u8 file_format;      /* Schema format version for this file */
   879    879     u8 enc;              /* Text encoding used by this database */
   880         -  u16 flags;           /* Flags associated with this schema */
          880  +  u16 schemaFlags;     /* Flags associated with this schema */
   881    881     int cache_size;      /* Number of pages to use in the cache */
   882    882   };
   883    883   
   884    884   /*
   885    885   ** These macros can be used to test, set, or clear bits in the 
   886    886   ** Db.pSchema->flags field.
   887    887   */
   888         -#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
   889         -#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
   890         -#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
   891         -#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
          888  +#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->schemaFlags&(P))==(P))
          889  +#define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->schemaFlags&(P))!=0)
          890  +#define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->schemaFlags|=(P)
          891  +#define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->schemaFlags&=~(P)
   892    892   
   893    893   /*
   894    894   ** Allowed values for the DB.pSchema->flags field.
   895    895   **
   896    896   ** The DB_SchemaLoaded flag is set after the database schema has been
   897    897   ** read into internal hash tables.
   898    898   **
................................................................................
  1724   1724   #define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
  1725   1725   #define SQLITE_IDXTYPE_UNIQUE      1   /* Implements a UNIQUE constraint */
  1726   1726   #define SQLITE_IDXTYPE_PRIMARYKEY  2   /* Is the PRIMARY KEY for the table */
  1727   1727   
  1728   1728   /* Return true if index X is a PRIMARY KEY index */
  1729   1729   #define IsPrimaryKeyIndex(X)  ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
  1730   1730   
         1731  +/* Return true if index X is a UNIQUE index */
         1732  +#define IsUniqueIndex(X)      ((X)->onError!=OE_None)
         1733  +
  1731   1734   /*
  1732   1735   ** Each sample stored in the sqlite_stat3 table is represented in memory 
  1733   1736   ** using a structure of this type.  See documentation at the top of the
  1734   1737   ** analyze.c source file for additional information.
  1735   1738   */
  1736   1739   struct IndexSample {
  1737   1740     void *p;          /* Pointer to sampled record */
................................................................................
  3596   3599     void sqlite3BeginBenignMalloc(void);
  3597   3600     void sqlite3EndBenignMalloc(void);
  3598   3601   #else
  3599   3602     #define sqlite3BeginBenignMalloc()
  3600   3603     #define sqlite3EndBenignMalloc()
  3601   3604   #endif
  3602   3605   
  3603         -#define IN_INDEX_ROWID           1
  3604         -#define IN_INDEX_EPH             2
  3605         -#define IN_INDEX_INDEX_ASC       3
  3606         -#define IN_INDEX_INDEX_DESC      4
  3607         -int sqlite3FindInIndex(Parse *, Expr *, int*);
         3606  +/*
         3607  +** Allowed return values from sqlite3FindInIndex()
         3608  +*/
         3609  +#define IN_INDEX_ROWID        1   /* Search the rowid of the table */
         3610  +#define IN_INDEX_EPH          2   /* Search an ephemeral b-tree */
         3611  +#define IN_INDEX_INDEX_ASC    3   /* Existing index ASCENDING */
         3612  +#define IN_INDEX_INDEX_DESC   4   /* Existing index DESCENDING */
         3613  +#define IN_INDEX_NOOP         5   /* No table available. Use comparisons */
         3614  +/*
         3615  +** Allowed flags for the 3rd parameter to sqlite3FindInIndex().
         3616  +*/
         3617  +#define IN_INDEX_NOOP_OK     0x0001  /* OK to return IN_INDEX_NOOP */
         3618  +#define IN_INDEX_MEMBERSHIP  0x0002  /* IN operator used for membership test */
         3619  +#define IN_INDEX_LOOP        0x0004  /* IN operator used as a loop */
         3620  +int sqlite3FindInIndex(Parse *, Expr *, u32, int*);
  3608   3621   
  3609   3622   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
  3610   3623     int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
  3611   3624     int sqlite3JournalSize(sqlite3_vfs *);
  3612   3625     int sqlite3JournalCreate(sqlite3_file *);
  3613   3626     int sqlite3JournalExists(sqlite3_file *p);
  3614   3627   #else

Changes to src/test1.c.

  6577   6577     extern int sqlite3_opentemp_count;
  6578   6578     extern int sqlite3_like_count;
  6579   6579     extern int sqlite3_xferopt_count;
  6580   6580     extern int sqlite3_pager_readdb_count;
  6581   6581     extern int sqlite3_pager_writedb_count;
  6582   6582     extern int sqlite3_pager_writej_count;
  6583   6583   #if SQLITE_OS_WIN
  6584         -  extern int sqlite3_os_type;
         6584  +  extern LONG volatile sqlite3_os_type;
  6585   6585   #endif
  6586   6586   #ifdef SQLITE_DEBUG
  6587   6587     extern int sqlite3WhereTrace;
  6588   6588     extern int sqlite3OSTrace;
  6589   6589     extern int sqlite3WalTrace;
  6590   6590   #endif
  6591   6591   #ifdef SQLITE_TEST
................................................................................
  6635   6635   #endif
  6636   6636   #ifndef SQLITE_OMIT_UTF16
  6637   6637     Tcl_LinkVar(interp, "sqlite_last_needed_collation",
  6638   6638         (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  6639   6639   #endif
  6640   6640   #if SQLITE_OS_WIN
  6641   6641     Tcl_LinkVar(interp, "sqlite_os_type",
  6642         -      (char*)&sqlite3_os_type, TCL_LINK_INT);
         6642  +      (char*)&sqlite3_os_type, TCL_LINK_LONG);
  6643   6643   #endif
  6644   6644   #ifdef SQLITE_TEST
  6645   6645     {
  6646   6646       static const char *query_plan = "*** OBSOLETE VARIABLE ***";
  6647   6647       Tcl_LinkVar(interp, "sqlite_query_plan",
  6648   6648          (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  6649   6649     }

Changes to src/test_multiplex.c.

  1172   1172   **
  1173   1173   ** All SQLite database connections must be closed before calling this
  1174   1174   ** routine.
  1175   1175   **
  1176   1176   ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
  1177   1177   ** shutting down in order to free all remaining multiplex groups.
  1178   1178   */
  1179         -int sqlite3_multiplex_shutdown(void){
         1179  +int sqlite3_multiplex_shutdown(int eForce){
         1180  +  int rc = SQLITE_OK;
  1180   1181     if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE;
  1181         -  if( gMultiplex.pGroups ) return SQLITE_MISUSE;
         1182  +  if( gMultiplex.pGroups ){
         1183  +    sqlite3_log(SQLITE_MISUSE, "sqlite3_multiplex_shutdown() called "
         1184  +                "while database connections are still open");
         1185  +    if( !eForce ) return SQLITE_MISUSE;
         1186  +    rc = SQLITE_MISUSE;
         1187  +  }
  1182   1188     gMultiplex.isInitialized = 0;
  1183   1189     sqlite3_mutex_free(gMultiplex.pMutex);
  1184   1190     sqlite3_vfs_unregister(&gMultiplex.sThisVfs);
  1185   1191     memset(&gMultiplex, 0, sizeof(gMultiplex));
  1186         -  return SQLITE_OK;
         1192  +  return rc;
  1187   1193   }
  1188   1194   
  1189   1195   /***************************** Test Code ***********************************/
  1190   1196   #ifdef SQLITE_TEST
  1191   1197   #include <tcl.h>
  1192   1198   extern const char *sqlite3ErrName(int);
  1193   1199   
................................................................................
  1232   1238     int objc,
  1233   1239     Tcl_Obj *CONST objv[]
  1234   1240   ){
  1235   1241     int rc;                         /* Value returned by multiplex_shutdown() */
  1236   1242   
  1237   1243     UNUSED_PARAMETER(clientData);
  1238   1244   
  1239         -  if( objc!=1 ){
  1240         -    Tcl_WrongNumArgs(interp, 1, objv, "");
         1245  +  if( objc==2 && strcmp(Tcl_GetString(objv[1]),"-force")!=0 ){
         1246  +    objc = 3;
         1247  +  }
         1248  +  if( (objc!=1 && objc!=2) ){
         1249  +    Tcl_WrongNumArgs(interp, 1, objv, "?-force?");
  1241   1250       return TCL_ERROR;
  1242   1251     }
  1243   1252   
  1244   1253     /* Call sqlite3_multiplex_shutdown() */
  1245         -  rc = sqlite3_multiplex_shutdown();
         1254  +  rc = sqlite3_multiplex_shutdown(objc==2);
  1246   1255     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_STATIC);
  1247   1256   
  1248   1257     return TCL_OK;
  1249   1258   }
  1250   1259   
  1251   1260   /*
  1252   1261   ** tclcmd:  sqlite3_multiplex_dump

Changes to src/test_multiplex.h.

    86     86   **
    87     87   ** All SQLite database connections must be closed before calling this
    88     88   ** routine.
    89     89   **
    90     90   ** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
    91     91   ** shutting down in order to free all remaining multiplex groups.
    92     92   */
    93         -extern int sqlite3_multiplex_shutdown(void);
           93  +extern int sqlite3_multiplex_shutdown(int eForce);
    94     94   
    95     95   #ifdef __cplusplus
    96     96   }  /* End of the 'extern "C"' block */
    97     97   #endif
    98     98   
    99     99   #endif /* _TEST_MULTIPLEX_H */

Changes to src/update.c.

   434    434       if( aToOpen[iDataCur-iBaseCur] ){
   435    435         assert( pPk!=0 );
   436    436         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
   437    437         VdbeCoverageNeverTaken(v);
   438    438       }
   439    439       labelContinue = labelBreak;
   440    440       sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
   441         -    VdbeCoverage(v);
          441  +    VdbeCoverageIf(v, pPk==0);
          442  +    VdbeCoverageIf(v, pPk!=0);
   442    443     }else if( pPk ){
   443    444       labelContinue = sqlite3VdbeMakeLabel(v);
   444    445       sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
   445    446       addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
   446    447       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
   447    448       VdbeCoverage(v);
   448    449     }else{

Changes to src/vdbe.c.

   122    122   ** feature is used for test suite validation only and does not appear an
   123    123   ** production builds.
   124    124   **
   125    125   ** M is an integer, 2 or 3, that indices how many different ways the
   126    126   ** branch can go.  It is usually 2.  "I" is the direction the branch
   127    127   ** goes.  0 means falls through.  1 means branch is taken.  2 means the
   128    128   ** second alternative branch is taken.
          129  +**
          130  +** iSrcLine is the source code line (from the __LINE__ macro) that
          131  +** generated the VDBE instruction.  This instrumentation assumes that all
          132  +** source code is in a single file (the amalgamation).  Special values 1
          133  +** and 2 for the iSrcLine parameter mean that this particular branch is
          134  +** always taken or never taken, respectively.
   129    135   */
   130    136   #if !defined(SQLITE_VDBE_COVERAGE)
   131    137   # define VdbeBranchTaken(I,M)
   132    138   #else
   133    139   # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
   134    140     static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
   135    141       if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
................................................................................
   796    802     pOut->flags = MEM_Int;
   797    803     if( pOp->p2 ) pc = pOp->p2 - 1;
   798    804     break;
   799    805   }
   800    806   
   801    807   /* Opcode:  EndCoroutine P1 * * * *
   802    808   **
   803         -** The instruction at the address in register P1 is an Yield.
          809  +** The instruction at the address in register P1 is a Yield.
   804    810   ** Jump to the P2 parameter of that Yield.
   805    811   ** After the jump, register P1 becomes undefined.
   806    812   **
   807    813   ** See also: InitCoroutine
   808    814   */
   809    815   case OP_EndCoroutine: {           /* in1 */
   810    816     VdbeOp *pCaller;
................................................................................
   989    995   }
   990    996   #endif
   991    997   
   992    998   /* Opcode: String8 * P2 * P4 *
   993    999   ** Synopsis: r[P2]='P4'
   994   1000   **
   995   1001   ** P4 points to a nul terminated UTF-8 string. This opcode is transformed 
   996         -** into an OP_String before it is executed for the first time.  During
         1002  +** into a String before it is executed for the first time.  During
   997   1003   ** this transformation, the length of string P4 is computed and stored
   998   1004   ** as the P1 parameter.
   999   1005   */
  1000   1006   case OP_String8: {         /* same as TK_STRING, out2-prerelease */
  1001   1007     assert( pOp->p4.z!=0 );
  1002   1008     pOp->opcode = OP_String;
  1003   1009     pOp->p1 = sqlite3Strlen30(pOp->p4.z);
................................................................................
  2235   2241     break;
  2236   2242   }
  2237   2243   
  2238   2244   /* Opcode: If P1 P2 P3 * *
  2239   2245   **
  2240   2246   ** Jump to P2 if the value in register P1 is true.  The value
  2241   2247   ** is considered true if it is numeric and non-zero.  If the value
  2242         -** in P1 is NULL then take the jump if P3 is non-zero.
         2248  +** in P1 is NULL then take the jump if and only if P3 is non-zero.
  2243   2249   */
  2244   2250   /* Opcode: IfNot P1 P2 P3 * *
  2245   2251   **
  2246   2252   ** Jump to P2 if the value in register P1 is False.  The value
  2247   2253   ** is considered false if it has a numeric value of zero.  If the value
  2248         -** in P1 is NULL then take the jump if P3 is zero.
         2254  +** in P1 is NULL then take the jump if and only if P3 is non-zero.
  2249   2255   */
  2250   2256   case OP_If:                 /* jump, in1 */
  2251   2257   case OP_IfNot: {            /* jump, in1 */
  2252   2258     int c;
  2253   2259     pIn1 = &aMem[pOp->p1];
  2254   2260     if( pIn1->flags & MEM_Null ){
  2255   2261       c = pOp->p3;
................................................................................
  3513   3519   ** that are used as an unpacked index key. 
  3514   3520   **
  3515   3521   ** Reposition cursor P1 so that  it points to the smallest entry that 
  3516   3522   ** is greater than or equal to the key value. If there are no records 
  3517   3523   ** greater than or equal to the key and P2 is not zero, then jump to P2.
  3518   3524   **
  3519   3525   ** This opcode leaves the cursor configured to move in forward order,
  3520         -** from the begining toward the end.  In other words, the cursor is
         3526  +** from the beginning toward the end.  In other words, the cursor is
  3521   3527   ** configured to use Next, not Prev.
  3522   3528   **
  3523   3529   ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
  3524   3530   */
  3525   3531   /* Opcode: SeekGT P1 P2 P3 P4 *
  3526   3532   ** Synopsis: key=r[P3@P4]
  3527   3533   **
................................................................................
  3753   3759   ** P4>0 then register P3 is the first of P4 registers that form an unpacked
  3754   3760   ** record.
  3755   3761   **
  3756   3762   ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
  3757   3763   ** is a prefix of any entry in P1 then a jump is made to P2 and
  3758   3764   ** P1 is left pointing at the matching entry.
  3759   3765   **
  3760         -** This operation leaves the cursor in a state where it cannot be
  3761         -** advanced in either direction.  In other words, the Next and Prev
  3762         -** opcodes do not work after this operation.
         3766  +** This operation leaves the cursor in a state where it can be
         3767  +** advanced in the forward direction.  The Next instruction will work,
         3768  +** but not the Prev instruction.
  3763   3769   **
  3764   3770   ** See also: NotFound, NoConflict, NotExists. SeekGe
  3765   3771   */
  3766   3772   /* Opcode: NotFound P1 P2 P3 P4 *
  3767   3773   ** Synopsis: key=r[P3@P4]
  3768   3774   **
  3769   3775   ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
................................................................................
  3822   3828   #endif
  3823   3829   
  3824   3830     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3825   3831     assert( pOp->p4type==P4_INT32 );
  3826   3832     pC = p->apCsr[pOp->p1];
  3827   3833     assert( pC!=0 );
  3828   3834   #ifdef SQLITE_DEBUG
  3829         -  pC->seekOp = 0;
         3835  +  pC->seekOp = pOp->opcode;
  3830   3836   #endif
  3831   3837     pIn3 = &aMem[pOp->p3];
  3832   3838     assert( pC->pCursor!=0 );
  3833   3839     assert( pC->isTable==0 );
  3834   3840     pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
  3835   3841     if( pOp->p4.i>0 ){
  3836   3842       r.pKeyInfo = pC->pKeyInfo;
................................................................................
  4232   4238   /* Opcode: Delete P1 P2 P3 P4 *
  4233   4239   **
  4234   4240   ** Delete the record at which the P1 cursor is currently pointing.
  4235   4241   **
  4236   4242   ** The cursor will be left pointing at either the next or the previous
  4237   4243   ** record in the table. If it is left pointing at the next record, then
  4238   4244   ** the next Next instruction will be a no-op.  Hence it is OK to delete
  4239         -** a record from within an Next loop.
         4245  +** a record from within a Next loop.
  4240   4246   **
  4241   4247   ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
  4242   4248   ** incremented (otherwise not).
  4243   4249   **
  4244   4250   ** P1 must not be pseudo-table.  It has to be a real table with
  4245   4251   ** multiple rows.
  4246   4252   **
................................................................................
  4331   4337     break;
  4332   4338   }
  4333   4339   
  4334   4340   /* Opcode: SorterCompare P1 P2 P3 P4
  4335   4341   ** Synopsis:  if key(P1)!=trim(r[P3],P4) goto P2
  4336   4342   **
  4337   4343   ** P1 is a sorter cursor. This instruction compares a prefix of the
  4338         -** the record blob in register P3 against a prefix of the entry that 
         4344  +** record blob in register P3 against a prefix of the entry that 
  4339   4345   ** the sorter cursor currently points to.  Only the first P4 fields
  4340   4346   ** of r[P3] and the sorter record are compared.
  4341   4347   **
  4342   4348   ** If either P3 or the sorter contains a NULL in one of their significant
  4343   4349   ** fields (not counting the P4 fields at the end which are ignored) then
  4344   4350   ** the comparison is assumed to be equal.
  4345   4351   **
................................................................................
  4730   4736     assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
  4731   4737     assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
  4732   4738   
  4733   4739     /* The Next opcode is only used after SeekGT, SeekGE, and Rewind.
  4734   4740     ** The Prev opcode is only used after SeekLT, SeekLE, and Last. */
  4735   4741     assert( pOp->opcode!=OP_Next || pOp->opcode!=OP_NextIfOpen
  4736   4742          || pC->seekOp==OP_SeekGT || pC->seekOp==OP_SeekGE
  4737         -       || pC->seekOp==OP_Rewind );
         4743  +       || pC->seekOp==OP_Rewind || pC->seekOp==OP_Found);
  4738   4744     assert( pOp->opcode!=OP_Prev || pOp->opcode!=OP_PrevIfOpen
  4739   4745          || pC->seekOp==OP_SeekLT || pC->seekOp==OP_SeekLE
  4740   4746          || pC->seekOp==OP_Last );
  4741   4747   
  4742   4748     rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4743   4749   next_tail:
  4744   4750     pC->cacheStatus = CACHE_STALE;
................................................................................
  5657   5663     VdbeBranchTaken( pIn1->u.i>0, 2);
  5658   5664     if( pIn1->u.i>0 ){
  5659   5665        pc = pOp->p2 - 1;
  5660   5666     }
  5661   5667     break;
  5662   5668   }
  5663   5669   
  5664         -/* Opcode: IfNeg P1 P2 * * *
  5665         -** Synopsis: if r[P1]<0 goto P2
         5670  +/* Opcode: IfNeg P1 P2 P3 * *
         5671  +** Synopsis: r[P1]+=P3, if r[P1]<0 goto P2
  5666   5672   **
  5667         -** If the value of register P1 is less than zero, jump to P2. 
  5668         -**
  5669         -** It is illegal to use this instruction on a register that does
  5670         -** not contain an integer.  An assertion fault will result if you try.
         5673  +** Register P1 must contain an integer.  Add literal P3 to the value in
         5674  +** register P1 then if the value of register P1 is less than zero, jump to P2. 
  5671   5675   */
  5672   5676   case OP_IfNeg: {        /* jump, in1 */
  5673   5677     pIn1 = &aMem[pOp->p1];
  5674   5678     assert( pIn1->flags&MEM_Int );
         5679  +  pIn1->u.i += pOp->p3;
  5675   5680     VdbeBranchTaken(pIn1->u.i<0, 2);
  5676   5681     if( pIn1->u.i<0 ){
  5677   5682        pc = pOp->p2 - 1;
  5678   5683     }
  5679   5684     break;
  5680   5685   }
  5681   5686   
  5682   5687   /* Opcode: IfZero P1 P2 P3 * *
  5683   5688   ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
  5684   5689   **
  5685   5690   ** The register P1 must contain an integer.  Add literal P3 to the
  5686   5691   ** value in register P1.  If the result is exactly 0, jump to P2. 
  5687         -**
  5688         -** It is illegal to use this instruction on a register that does
  5689         -** not contain an integer.  An assertion fault will result if you try.
  5690   5692   */
  5691   5693   case OP_IfZero: {        /* jump, in1 */
  5692   5694     pIn1 = &aMem[pOp->p1];
  5693   5695     assert( pIn1->flags&MEM_Int );
  5694   5696     pIn1->u.i += pOp->p3;
  5695   5697     VdbeBranchTaken(pIn1->u.i==0, 2);
  5696   5698     if( pIn1->u.i==0 ){

Changes to src/where.c.

  1466   1466     **      where X is a constant value. The collation sequences of the
  1467   1467     **      comparison and select-list expressions must match those of the index.
  1468   1468     **
  1469   1469     **   3. All of those index columns for which the WHERE clause does not
  1470   1470     **      contain a "col=X" term are subject to a NOT NULL constraint.
  1471   1471     */
  1472   1472     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1473         -    if( pIdx->onError==OE_None ) continue;
         1473  +    if( !IsUniqueIndex(pIdx) ) continue;
  1474   1474       for(i=0; i<pIdx->nKeyCol; i++){
  1475   1475         i16 iCol = pIdx->aiColumn[i];
  1476   1476         if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
  1477   1477           int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
  1478   1478           if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
  1479   1479             break;
  1480   1480           }
................................................................................
  2518   2518       ){
  2519   2519         testcase( iEq==0 );
  2520   2520         testcase( bRev );
  2521   2521         bRev = !bRev;
  2522   2522       }
  2523   2523       assert( pX->op==TK_IN );
  2524   2524       iReg = iTarget;
  2525         -    eType = sqlite3FindInIndex(pParse, pX, 0);
         2525  +    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
  2526   2526       if( eType==IN_INDEX_INDEX_DESC ){
  2527   2527         testcase( bRev );
  2528   2528         bRev = !bRev;
  2529   2529       }
  2530   2530       iTab = pX->iTable;
  2531   2531       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
  2532   2532       VdbeCoverageIf(v, bRev);
................................................................................
  4372   4372         }
  4373   4373         assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
  4374   4374                           ** changes "x IN (?)" into "x=?". */
  4375   4375   
  4376   4376       }else if( eOp & (WO_EQ) ){
  4377   4377         pNew->wsFlags |= WHERE_COLUMN_EQ;
  4378   4378         if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
  4379         -        if( iCol>=0 && pProbe->onError==OE_None ){
         4379  +        if( iCol>=0 && !IsUniqueIndex(pProbe) ){
  4380   4380             pNew->wsFlags |= WHERE_UNQ_WANTED;
  4381   4381           }else{
  4382   4382             pNew->wsFlags |= WHERE_ONEROW;
  4383   4383           }
  4384   4384         }
  4385   4385       }else if( eOp & WO_ISNULL ){
  4386   4386         pNew->wsFlags |= WHERE_COLUMN_NULL;
................................................................................
  5227   5227         }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
  5228   5228           return 0;
  5229   5229         }else{
  5230   5230           nKeyCol = pIndex->nKeyCol;
  5231   5231           nColumn = pIndex->nColumn;
  5232   5232           assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
  5233   5233           assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
  5234         -        isOrderDistinct = pIndex->onError!=OE_None;
         5234  +        isOrderDistinct = IsUniqueIndex(pIndex);
  5235   5235         }
  5236   5236   
  5237   5237         /* Loop through all columns of the index and deal with the ones
  5238   5238         ** that are not constrained by == or IN.
  5239   5239         */
  5240   5240         rev = revSet = 0;
  5241   5241         distinctColumns = 0;
................................................................................
  5742   5742       pLoop->u.btree.nEq = 1;
  5743   5743       /* TUNING: Cost of a rowid lookup is 10 */
  5744   5744       pLoop->rRun = 33;  /* 33==sqlite3LogEst(10) */
  5745   5745     }else{
  5746   5746       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  5747   5747         assert( pLoop->aLTermSpace==pLoop->aLTerm );
  5748   5748         assert( ArraySize(pLoop->aLTermSpace)==4 );
  5749         -      if( pIdx->onError==OE_None 
         5749  +      if( !IsUniqueIndex(pIdx)
  5750   5750          || pIdx->pPartIdxWhere!=0 
  5751   5751          || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) 
  5752   5752         ) continue;
  5753   5753         for(j=0; j<pIdx->nKeyCol; j++){
  5754   5754           pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
  5755   5755           if( pTerm==0 ) break;
  5756   5756           pLoop->aLTerm[j] = pTerm;

Changes to test/in4.test.

   227    227     SELECT * FROM t3 WHERE x IN (10);
   228    228   } {10 10 10}
   229    229   do_execsql_test in4-3.44 {
   230    230     EXPLAIN
   231    231     SELECT * FROM t3 WHERE x IN (10);
   232    232   } {~/OpenEphemeral/}
   233    233   do_execsql_test in4-3.45 {
   234         -  SELECT * FROM t3 WHERE x NOT IN (10,11);
          234  +  SELECT * FROM t3 WHERE x NOT IN (10,11,99999);
   235    235   } {1 1 1}
   236    236   do_execsql_test in4-3.46 {
   237    237     EXPLAIN
   238         -  SELECT * FROM t3 WHERE x NOT IN (10,11);
          238  +  SELECT * FROM t3 WHERE x NOT IN (10,11,99999);
   239    239   } {/OpenEphemeral/}
   240    240   do_execsql_test in4-3.47 {
   241    241     SELECT * FROM t3 WHERE x NOT IN (10);
   242    242   } {1 1 1}
   243    243   do_execsql_test in4-3.48 {
   244    244     EXPLAIN
   245    245     SELECT * FROM t3 WHERE x NOT IN (10);

Changes to test/multiplex.test.

    64     64       forcedelete [multiplex_name $name $i]
    65     65       forcedelete [multiplex_name $name-journal $i]
    66     66       forcedelete [multiplex_name $name-wal $i]
    67     67     }
    68     68   }
    69     69   
    70     70   db close
           71  +sqlite3_shutdown
           72  +test_sqlite3_log xLog
           73  +proc xLog {error_code msg} {
           74  +  lappend ::log $error_code $msg 
           75  +}
           76  +unset -nocomplain log
    71     77   
    72     78   multiplex_delete test.db
    73     79   multiplex_delete test2.db
    74     80   
    75     81   #-------------------------------------------------------------------------
    76     82   #   multiplex-1.1.*: Test initialize and shutdown.
    77     83   
................................................................................
   184    190   
   185    191   do_test multiplex-2.3.1 {
   186    192     sqlite3 db2 test2.x
   187    193     db2 close
   188    194   } {}
   189    195   
   190    196   
          197  +unset -nocomplain ::log
   191    198   do_test multiplex-2.4.1 {
   192    199     sqlite3_multiplex_shutdown
   193    200   } {SQLITE_MISUSE}
   194    201   do_test multiplex-2.4.2 {
   195    202     execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
   196    203   } {}
          204  +do_test multiplex-2.4.3 {
          205  +  set ::log
          206  +} {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}}
   197    207   do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
   198    208   do_test multiplex-2.4.5 {
   199    209     db close
   200    210     sqlite3 db test.x
   201    211     db eval vacuum
   202    212     db close
   203    213     glob test.x*
................................................................................
   579    589     multiplex_delete test.x
   580    590     sqlite3_multiplex_shutdown
   581    591   } {SQLITE_OK}
   582    592   
   583    593   }
   584    594   
   585    595   
          596  +catch { db close }
   586    597   catch { sqlite3_multiplex_shutdown }
          598  +sqlite3_shutdown
          599  +test_sqlite3_log 
          600  +sqlite3_initialize
   587    601   finish_test

Changes to test/pragma.test.

   427    427     do_test pragma-3.19 {
   428    428       catch {db close}
   429    429       forcedelete test.db test.db-journal
   430    430       sqlite3 db test.db
   431    431       db eval {PRAGMA integrity_check}
   432    432     } {ok}
   433    433   }
   434         -#exit
          434  +
          435  +# Verify that PRAGMA integrity_check catches UNIQUE and NOT NULL
          436  +# constraint violations.
          437  +#
          438  +do_execsql_test pragma-3.20 {
          439  +  CREATE TABLE t1(a,b);
          440  +  CREATE INDEX t1a ON t1(a);
          441  +  INSERT INTO t1 VALUES(1,1),(2,2),(3,3),(2,4),(NULL,5),(NULL,6);
          442  +  PRAGMA writable_schema=ON;
          443  +  UPDATE sqlite_master SET sql='CREATE UNIQUE INDEX t1a ON t1(a)'
          444  +   WHERE name='t1a';
          445  +  UPDATE sqlite_master SET sql='CREATE TABLE t1(a NOT NULL,b)'
          446  +   WHERE name='t1';
          447  +  PRAGMA writable_schema=OFF;
          448  +  ALTER TABLE t1 RENAME TO t1x;
          449  +  PRAGMA integrity_check;
          450  +} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a} {NULL value in t1x.a}}
          451  +do_execsql_test pragma-3.21 {
          452  +  PRAGMA integrity_check(3);
          453  +} {{non-unique entry in index t1a} {NULL value in t1x.a} {non-unique entry in index t1a}}
          454  +do_execsql_test pragma-3.22 {
          455  +  PRAGMA integrity_check(2);
          456  +} {{non-unique entry in index t1a} {NULL value in t1x.a}}
          457  +do_execsql_test pragma-3.21 {
          458  +  PRAGMA integrity_check(1);
          459  +} {{non-unique entry in index t1a}}
   435    460   
   436    461   # Test modifying the cache_size of an attached database.
   437    462   ifcapable pager_pragmas&&attach {
   438    463   do_test pragma-4.1 {
   439    464     execsql {
   440    465       ATTACH 'test2.db' AS aux;
   441    466       pragma aux.cache_size;

Changes to test/table.test.

   721    721   do_test table-15.2 {
   722    722     execsql {BEGIN}
   723    723     for {set i 0} {$i<2000} {incr i} {
   724    724       execsql "DROP TABLE tbl$i"
   725    725     }
   726    726     execsql {COMMIT}
   727    727   } {}
          728  +
          729  +# Ticket 3a88d85f36704eebe134f7f48aebf00cd6438c1a (2014-08-05)
          730  +# The following SQL script segfaults while running the INSERT statement:
          731  +#
          732  +#    CREATE TABLE t1(x DEFAULT(max(1)));
          733  +#    INSERT INTO t1(rowid) VALUES(1);
          734  +#
          735  +# The problem appears to be the use of an aggregate function as part of
          736  +# the default value for a column. This problem has been in the code since
          737  +# at least 2006-01-01 and probably before that. This problem was detected
          738  +# and reported on the sqlite-users@sqlite.org mailing list by Zsb√°n Ambrus. 
          739  +#
          740  +do_execsql_test table-16.1 {
          741  +  CREATE TABLE t16(x DEFAULT(max(1)));
          742  +  INSERT INTO t16(x) VALUES(123);
          743  +  SELECT rowid, x FROM t16;
          744  +} {1 123}
          745  +do_catchsql_test table-16.2 {
          746  +  INSERT INTO t16(rowid) VALUES(4);
          747  +} {1 {unknown function: max()}}
          748  +do_execsql_test table-16.3 {
          749  +  DROP TABLE t16;
          750  +  CREATE TABLE t16(x DEFAULT(abs(1)));
          751  +  INSERT INTO t16(rowid) VALUES(4);
          752  +  SELECT rowid, x FROM t16;
          753  +} {4 1}
          754  +do_catchsql_test table-16.4 {
          755  +  DROP TABLE t16;
          756  +  CREATE TABLE t16(x DEFAULT(avg(1)));
          757  +  INSERT INTO t16(rowid) VALUES(123);
          758  +  SELECT rowid, x FROM t16;
          759  +} {1 {unknown function: avg()}}
          760  +do_catchsql_test table-16.5 {
          761  +  DROP TABLE t16;
          762  +  CREATE TABLE t16(x DEFAULT(count()));
          763  +  INSERT INTO t16(rowid) VALUES(123);
          764  +  SELECT rowid, x FROM t16;
          765  +} {1 {unknown function: count()}}
          766  +do_catchsql_test table-16.6 {
          767  +  DROP TABLE t16;
          768  +  CREATE TABLE t16(x DEFAULT(group_concat('x',',')));
          769  +  INSERT INTO t16(rowid) VALUES(123);
          770  +  SELECT rowid, x FROM t16;
          771  +} {1 {unknown function: group_concat()}}
          772  +do_catchsql_test table-16.7 {
          773  +  INSERT INTO t16 DEFAULT VALUES;
          774  +} {1 {unknown function: group_concat()}}
   728    775   
   729    776   finish_test

Changes to test/tester.tcl.

   865    865     }
   866    866   }
   867    867   
   868    868   # Run this routine last
   869    869   #
   870    870   proc finish_test {} {
   871    871     catch {db close}
          872  +  catch {db1 close}
   872    873     catch {db2 close}
   873    874     catch {db3 close}
   874    875     if {0==[info exists ::SLAVE]} { finalize_testing }
   875    876   }
   876    877   proc finalize_testing {} {
   877    878     global sqlite_open_file_count
   878    879   

Changes to test/tkt-80e031a00f.test.

   156    156   do_execsql_test tkt-80e031a00f.319 {SELECT 'c' NOT IN t7} 0
   157    157   do_execsql_test tkt-80e031a00f.320 {SELECT 'c' IN t7n} 1
   158    158   do_execsql_test tkt-80e031a00f.321 {SELECT 'd' NOT IN t7n} 0
   159    159   do_execsql_test tkt-80e031a00f.322 {SELECT 'b' IN t8} 1
   160    160   do_execsql_test tkt-80e031a00f.323 {SELECT 'c' NOT IN t8} 0
   161    161   do_execsql_test tkt-80e031a00f.324 {SELECT 'c' IN t8n} 1
   162    162   do_execsql_test tkt-80e031a00f.325 {SELECT 'd' NOT IN t8n} 0
          163  +do_execsql_test tkt-80e031a00f.326 {SELECT 'a' IN (NULL,'a')} 1
          164  +do_execsql_test tkt-80e031a00f.327 {SELECT 'a' IN (NULL,'b')} {{}}
          165  +do_execsql_test tkt-80e031a00f.328 {SELECT 'a' NOT IN (NULL,'a')} 0
          166  +do_execsql_test tkt-80e031a00f.329 {SELECT 'a' NOT IN (NULL,'b')} {{}}
   163    167   #
   164    168   # Row 4:
   165    169   do_execsql_test tkt-80e031a00f.400 {SELECT 1 IN (2,3,4,null)} {{}}
   166    170   do_execsql_test tkt-80e031a00f.401 {SELECT 1 NOT IN (2,3,4,null)} {{}}
   167    171   do_execsql_test tkt-80e031a00f.402 {SELECT 'a' IN ('b','c',null,'d')} {{}}
   168    172   do_execsql_test tkt-80e031a00f.403 {SELECT 'a' NOT IN (null,'b','c','d')} {{}}
   169    173   do_execsql_test tkt-80e031a00f.404 {SELECT 1 IN t4n} {{}}