/ Check-in [6fc72077]
Login

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

Overview
Comment:Merge the latest trunk enhancements into the apple-osx branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 6fc7207727d50c3039c7a0c6c9151cad6b9d25de
User & Date: drh 2014-10-09 11:27:38
Context
2014-10-09
11:40
Record the errno on fstat() failures. check-in: 2a6a0820 user: drh tags: apple-osx
11:27
Merge the latest trunk enhancements into the apple-osx branch. check-in: 6fc72077 user: drh tags: apple-osx
2014-10-08
19:33
Remove an always-true branch in whereRangeScanEst(). Replace it with an assert(). check-in: 42e48fd3 user: drh tags: trunk
2014-10-01
01:46
Merge the latest enhancements from trunk. check-in: 2078454a user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/fts3/fts3.c.

  4422   4422         for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
  4423   4423           rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
  4424   4424           if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
  4425   4425             iMax = a[i].iDocid;
  4426   4426             bMaxSet = 1;
  4427   4427           }
  4428   4428         }
  4429         -      assert( rc!=SQLITE_OK || a[p->nToken-1].bIgnore==0 );
         4429  +      assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
  4430   4430         assert( rc!=SQLITE_OK || bMaxSet );
  4431   4431   
  4432   4432         /* Keep advancing iterators until they all point to the same document */
  4433   4433         for(i=0; i<p->nToken; i++){
  4434   4434           while( rc==SQLITE_OK && bEof==0 
  4435   4435               && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0 
  4436   4436           ){

Changes to src/analyze.c.

  1433   1433     int c;
  1434   1434     int i;
  1435   1435     tRowcnt v;
  1436   1436   
  1437   1437   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1438   1438     if( z==0 ) z = "";
  1439   1439   #else
  1440         -  if( NEVER(z==0) ) z = "";
         1440  +  assert( z!=0 );
  1441   1441   #endif
  1442   1442     for(i=0; *z && i<nOut; i++){
  1443   1443       v = 0;
  1444   1444       while( (c=z[0])>='0' && c<='9' ){
  1445   1445         v = v*10 + c - '0';
  1446   1446         z++;
  1447   1447       }
  1448   1448   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1449         -    if( aOut ){
  1450         -      aOut[i] = v;
  1451         -    }else
         1449  +    if( aOut ) aOut[i] = v;
         1450  +    if( aLog ) aLog[i] = sqlite3LogEst(v);
  1452   1451   #else
  1453   1452       assert( aOut==0 );
  1454   1453       UNUSED_PARAMETER(aOut);
         1454  +    assert( aLog!=0 );
         1455  +    aLog[i] = sqlite3LogEst(v);
  1455   1456   #endif
  1456         -    {
  1457         -      aLog[i] = sqlite3LogEst(v);
  1458         -    }
  1459   1457       if( *z==' ' ) z++;
  1460   1458     }
  1461   1459   #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
  1462   1460     assert( pIndex!=0 );
  1463   1461   #else
  1464   1462     if( pIndex )
  1465   1463   #endif
................................................................................
  1512   1510       pIndex = sqlite3PrimaryKeyIndex(pTable);
  1513   1511     }else{
  1514   1512       pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
  1515   1513     }
  1516   1514     z = argv[2];
  1517   1515   
  1518   1516     if( pIndex ){
         1517  +    int nCol = pIndex->nKeyCol+1;
         1518  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1519  +    tRowcnt * const aiRowEst = pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(
         1520  +        sizeof(tRowcnt) * nCol
         1521  +    );
         1522  +    if( aiRowEst==0 ) pInfo->db->mallocFailed = 1;
         1523  +#else
         1524  +    tRowcnt * const aiRowEst = 0;
         1525  +#endif
  1519   1526       pIndex->bUnordered = 0;
  1520         -    decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
         1527  +    decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
  1521   1528       if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
  1522   1529     }else{
  1523   1530       Index fakeIdx;
  1524   1531       fakeIdx.szIdxRow = pTable->szTabRow;
  1525   1532   #ifdef SQLITE_ENABLE_COSTMULT
  1526   1533       fakeIdx.pTable = pTable;
  1527   1534   #endif
................................................................................
  1572   1579         ** sample columns except the last. The last is always set to 1, as
  1573   1580         ** once the trailing PK fields are considered all index keys are
  1574   1581         ** unique.  */
  1575   1582         nCol = pIdx->nSampleCol-1;
  1576   1583         pIdx->aAvgEq[nCol] = 1;
  1577   1584       }
  1578   1585       for(iCol=0; iCol<nCol; iCol++){
         1586  +      int nSample = pIdx->nSample;
  1579   1587         int i;                    /* Used to iterate through samples */
  1580   1588         tRowcnt sumEq = 0;        /* Sum of the nEq values */
  1581         -      tRowcnt nSum = 0;         /* Number of terms contributing to sumEq */
  1582   1589         tRowcnt avgEq = 0;
  1583         -      tRowcnt nDLt = pFinal->anDLt[iCol];
         1590  +      tRowcnt nRow;             /* Number of rows in index */
         1591  +      i64 nSum100 = 0;          /* Number of terms contributing to sumEq */
         1592  +      i64 nDist100;             /* Number of distinct values in index */
         1593  +
         1594  +      if( pIdx->aiRowEst==0 || pIdx->aiRowEst[iCol+1]==0 ){
         1595  +        nRow = pFinal->anLt[iCol];
         1596  +        nDist100 = (i64)100 * pFinal->anDLt[iCol];
         1597  +        nSample--;
         1598  +      }else{
         1599  +        nRow = pIdx->aiRowEst[0];
         1600  +        nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
         1601  +      }
  1584   1602   
  1585   1603         /* Set nSum to the number of distinct (iCol+1) field prefixes that
  1586         -      ** occur in the stat4 table for this index before pFinal. Set
  1587         -      ** sumEq to the sum of the nEq values for column iCol for the same
  1588         -      ** set (adding the value only once where there exist duplicate 
  1589         -      ** prefixes).  */
  1590         -      for(i=0; i<(pIdx->nSample-1); i++){
  1591         -        if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
         1604  +      ** occur in the stat4 table for this index. Set sumEq to the sum of 
         1605  +      ** the nEq values for column iCol for the same set (adding the value 
         1606  +      ** only once where there exist duplicate prefixes).  */
         1607  +      for(i=0; i<nSample; i++){
         1608  +        if( i==(pIdx->nSample-1)
         1609  +         || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] 
         1610  +        ){
  1592   1611             sumEq += aSample[i].anEq[iCol];
  1593         -          nSum++;
         1612  +          nSum100 += 100;
  1594   1613           }
  1595   1614         }
  1596         -      if( nDLt>nSum ){
  1597         -        avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
         1615  +
         1616  +      if( nDist100>nSum100 ){
         1617  +        avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
  1598   1618         }
  1599   1619         if( avgEq==0 ) avgEq = 1;
  1600   1620         pIdx->aAvgEq[iCol] = avgEq;
  1601   1621       }
  1602   1622     }
  1603   1623   }
  1604   1624   
................................................................................
  1841   1861     /* Load the statistics from the sqlite_stat4 table. */
  1842   1862   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1843   1863     if( rc==SQLITE_OK ){
  1844   1864       int lookasideEnabled = db->lookaside.bEnabled;
  1845   1865       db->lookaside.bEnabled = 0;
  1846   1866       rc = loadStat4(db, sInfo.zDatabase);
  1847   1867       db->lookaside.bEnabled = lookasideEnabled;
         1868  +  }
         1869  +  for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
         1870  +    Index *pIdx = sqliteHashData(i);
         1871  +    sqlite3_free(pIdx->aiRowEst);
         1872  +    pIdx->aiRowEst = 0;
  1848   1873     }
  1849   1874   #endif
  1850   1875   
  1851   1876     if( rc==SQLITE_NOMEM ){
  1852   1877       db->mallocFailed = 1;
  1853   1878     }
  1854   1879     return rc;
  1855   1880   }
  1856   1881   
  1857   1882   
  1858   1883   #endif /* SQLITE_OMIT_ANALYZE */

Changes to src/btree.c.

  4045   4045   ){
  4046   4046     unsigned char *aPayload;
  4047   4047     int rc = SQLITE_OK;
  4048   4048     int iIdx = 0;
  4049   4049     MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
  4050   4050     BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
  4051   4051   #ifdef SQLITE_DIRECT_OVERFLOW_READ
         4052  +  unsigned char * const pBufStart = pBuf;
  4052   4053     int bEnd;                                 /* True if reading to end of data */
  4053   4054   #endif
  4054   4055   
  4055   4056     assert( pPage );
  4056   4057     assert( pCur->eState==CURSOR_VALID );
  4057   4058     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  4058   4059     assert( cursorHoldsMutex(pCur) );
................................................................................
  4172   4173           **
  4173   4174           **   1) this is a read operation, and 
  4174   4175           **   2) data is required from the start of this overflow page, and
  4175   4176           **   3) the database is file-backed, and
  4176   4177           **   4) there is no open write-transaction, and
  4177   4178           **   5) the database is not a WAL database,
  4178   4179           **   6) all data from the page is being read.
         4180  +        **   7) at least 4 bytes have already been read into the output buffer 
  4179   4181           **
  4180   4182           ** then data can be read directly from the database file into the
  4181   4183           ** output buffer, bypassing the page-cache altogether. This speeds
  4182   4184           ** up loading large records that span many overflow pages.
  4183   4185           */
  4184   4186           if( (eOp&0x01)==0                                      /* (1) */
  4185   4187            && offset==0                                          /* (2) */
  4186   4188            && (bEnd || a==ovflSize)                              /* (6) */
  4187   4189            && pBt->inTransaction==TRANS_READ                     /* (4) */
  4188   4190            && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
  4189   4191            && pBt->pPage1->aData[19]==0x01                       /* (5) */
         4192  +         && &pBuf[-4]>=pBufStart                               /* (7) */
  4190   4193           ){
  4191   4194             u8 aSave[4];
  4192   4195             u8 *aWrite = &pBuf[-4];
         4196  +          assert( aWrite>=pBufStart );                         /* hence (7) */
  4193   4197             memcpy(aSave, aWrite, 4);
  4194   4198             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
  4195   4199             nextPage = get4byte(aWrite);
  4196   4200             memcpy(aWrite, aSave, 4);
  4197   4201           }else
  4198   4202   #endif
  4199   4203   

Changes to src/build.c.

   431    431   #ifndef SQLITE_OMIT_ANALYZE
   432    432     sqlite3DeleteIndexSamples(db, p);
   433    433   #endif
   434    434     if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo);
   435    435     sqlite3ExprDelete(db, p->pPartIdxWhere);
   436    436     sqlite3DbFree(db, p->zColAff);
   437    437     if( p->isResized ) sqlite3DbFree(db, p->azColl);
          438  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          439  +  sqlite3_free(p->aiRowEst);
          440  +#endif
   438    441     sqlite3DbFree(db, p);
   439    442   }
   440    443   
   441    444   /*
   442    445   ** For the index called zIdxName which is found in the database iDb,
   443    446   ** unlike that index from its Table then remove the index from
   444    447   ** the index hash table and free all memory structures associated

Changes to src/expr.c.

  3335   3335       case TK_REM:     zBinOp = "REM";    break;
  3336   3336       case TK_BITAND:  zBinOp = "BITAND"; break;
  3337   3337       case TK_BITOR:   zBinOp = "BITOR";  break;
  3338   3338       case TK_SLASH:   zBinOp = "DIV";    break;
  3339   3339       case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
  3340   3340       case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
  3341   3341       case TK_CONCAT:  zBinOp = "CONCAT"; break;
         3342  +    case TK_DOT:     zBinOp = "DOT";    break;
  3342   3343   
  3343   3344       case TK_UMINUS:  zUniOp = "UMINUS"; break;
  3344   3345       case TK_UPLUS:   zUniOp = "UPLUS";  break;
  3345   3346       case TK_BITNOT:  zUniOp = "BITNOT"; break;
  3346   3347       case TK_NOT:     zUniOp = "NOT";    break;
  3347   3348       case TK_ISNULL:  zUniOp = "ISNULL"; break;
  3348   3349       case TK_NOTNULL: zUniOp = "NOTNULL"; break;

Changes to src/global.c.

   125    125     0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
   126    126     0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
   127    127     0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
   128    128     0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
   129    129   };
   130    130   #endif
   131    131   
          132  +/* EVIDENCE-OF: R-02982-34736 In order to maintain full backwards
          133  +** compatibility for legacy applications, the URI filename capability is
          134  +** disabled by default.
          135  +**
          136  +** EVIDENCE-OF: R-38799-08373 URI filenames can be enabled or disabled
          137  +** using the SQLITE_USE_URI=1 or SQLITE_USE_URI=0 compile-time options.
          138  +*/
   132    139   #ifndef SQLITE_USE_URI
   133    140   # define  SQLITE_USE_URI 0
   134    141   #endif
   135    142   
   136    143   #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
   137    144   # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
   138    145   #endif

Changes to src/main.c.

   475    475         */
   476    476         typedef void(*LOGFUNC_t)(void*,int,const char*);
   477    477         sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
   478    478         sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
   479    479         break;
   480    480       }
   481    481   
          482  +    /* EVIDENCE-OF: R-55548-33817 The compile-time setting for URI filenames
          483  +    ** can be changed at start-time using the
          484  +    ** sqlite3_config(SQLITE_CONFIG_URI,1) or
          485  +    ** sqlite3_config(SQLITE_CONFIG_URI,0) configuration calls.
          486  +    */
   482    487       case SQLITE_CONFIG_URI: {
   483    488         sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
   484    489         break;
   485    490       }
   486    491   
   487    492       case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
   488    493         sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
................................................................................
  2254   2259     char *zFile;
  2255   2260     char c;
  2256   2261     int nUri = sqlite3Strlen30(zUri);
  2257   2262   
  2258   2263     assert( *pzErrMsg==0 );
  2259   2264   
  2260   2265     if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri) 
  2261         -   && nUri>=5 && memcmp(zUri, "file:", 5)==0 
         2266  +   && nUri>=5 && memcmp(zUri, "file:", 5)==0 /* IMP: R-57884-37496 */
  2262   2267     ){
  2263   2268       char *zOpt;
  2264   2269       int eState;                   /* Parser state when parsing URI */
  2265   2270       int iIn;                      /* Input character index */
  2266   2271       int iOut = 0;                 /* Output character index */
  2267   2272       int nByte = nUri+2;           /* Bytes of space to allocate */
  2268   2273   
................................................................................
  2535   2540     */
  2536   2541     assert( SQLITE_OPEN_READONLY  == 0x01 );
  2537   2542     assert( SQLITE_OPEN_READWRITE == 0x02 );
  2538   2543     assert( SQLITE_OPEN_CREATE    == 0x04 );
  2539   2544     testcase( (1<<(flags&7))==0x02 ); /* READONLY */
  2540   2545     testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
  2541   2546     testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
  2542         -  if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
         2547  +  if( ((1<<(flags&7)) & 0x46)==0 ){
         2548  +    return SQLITE_MISUSE_BKPT;  /* IMP: R-65497-44594 */
         2549  +  }
  2543   2550   
  2544   2551     if( sqlite3GlobalConfig.bCoreMutex==0 ){
  2545   2552       isThreadsafe = 0;
  2546   2553     }else if( flags & SQLITE_OPEN_NOMUTEX ){
  2547   2554       isThreadsafe = 0;
  2548   2555     }else if( flags & SQLITE_OPEN_FULLMUTEX ){
  2549   2556       isThreadsafe = 1;

Changes to src/malloc.c.

   306    306     }else if( sqlite3GlobalConfig.bMemstat ){
   307    307       sqlite3_mutex_enter(mem0.mutex);
   308    308       mallocWithAlarm((int)n, &p);
   309    309       sqlite3_mutex_leave(mem0.mutex);
   310    310     }else{
   311    311       p = sqlite3GlobalConfig.m.xMalloc((int)n);
   312    312     }
   313         -  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
          313  +  assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-11148-40995 */
   314    314     return p;
   315    315   }
   316    316   
   317    317   /*
   318    318   ** This version of the memory allocation is for use by the application.
   319    319   ** First make sure the memory subsystem is initialized, then do the
   320    320   ** allocation.
................................................................................
   443    443   
   444    444   /*
   445    445   ** Return the size of a memory allocation previously obtained from
   446    446   ** sqlite3Malloc() or sqlite3_malloc().
   447    447   */
   448    448   int sqlite3MallocSize(void *p){
   449    449     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   450         -  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   451    450     return sqlite3GlobalConfig.m.xSize(p);
   452    451   }
   453    452   int sqlite3DbMallocSize(sqlite3 *db, void *p){
   454    453     if( db==0 ){
          454  +    assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
          455  +    assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   455    456       return sqlite3MallocSize(p);
   456    457     }else{
   457    458       assert( sqlite3_mutex_held(db->mutex) );
   458    459       if( isLookaside(db, p) ){
   459    460         return db->lookaside.sz;
   460    461       }else{
   461         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   462         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
   463         -      assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
          462  +      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          463  +      assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   464    464         return sqlite3GlobalConfig.m.xSize(p);
   465    465       }
   466    466     }
   467    467   }
   468    468   sqlite3_uint64 sqlite3_msize(void *p){
          469  +  assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
          470  +  assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   469    471     return (sqlite3_uint64)sqlite3GlobalConfig.m.xSize(p);
   470    472   }
   471    473   
   472    474   /*
   473    475   ** Free memory previously obtained from sqlite3Malloc().
   474    476   */
   475    477   void sqlite3_free(void *p){
   476    478     if( p==0 ) return;  /* IMP: R-49053-54554 */
   477         -  assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
   478    479     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
          480  +  assert( sqlite3MemdebugNoType(p, ~MEMTYPE_HEAP) );
   479    481     if( sqlite3GlobalConfig.bMemstat ){
   480    482       sqlite3_mutex_enter(mem0.mutex);
   481    483       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
   482    484       sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
   483    485       sqlite3GlobalConfig.m.xFree(p);
   484    486       sqlite3_mutex_leave(mem0.mutex);
   485    487     }else{
................................................................................
   515    517   #endif
   516    518         pBuf->pNext = db->lookaside.pFree;
   517    519         db->lookaside.pFree = pBuf;
   518    520         db->lookaside.nOut--;
   519    521         return;
   520    522       }
   521    523     }
   522         -  assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   523         -  assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
          524  +  assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          525  +  assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   524    526     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   525    527     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   526    528     sqlite3_free(p);
   527    529   }
   528    530   
   529    531   /*
   530    532   ** Change the size of an existing memory allocation
   531    533   */
   532    534   void *sqlite3Realloc(void *pOld, u64 nBytes){
   533    535     int nOld, nNew, nDiff;
   534    536     void *pNew;
          537  +  assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
          538  +  assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   535    539     if( pOld==0 ){
   536         -    return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
          540  +    return sqlite3Malloc(nBytes); /* IMP: R-04300-56712 */
   537    541     }
   538    542     if( nBytes==0 ){
   539         -    sqlite3_free(pOld); /* IMP: R-31593-10574 */
          543  +    sqlite3_free(pOld); /* IMP: R-26507-47431 */
   540    544       return 0;
   541    545     }
   542    546     if( nBytes>=0x7fffff00 ){
   543    547       /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
   544    548       return 0;
   545    549     }
   546    550     nOld = sqlite3MallocSize(pOld);
................................................................................
   554    558       sqlite3_mutex_enter(mem0.mutex);
   555    559       sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, (int)nBytes);
   556    560       nDiff = nNew - nOld;
   557    561       if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >= 
   558    562             mem0.alarmThreshold-nDiff ){
   559    563         sqlite3MallocAlarm(nDiff);
   560    564       }
   561         -    assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
   562         -    assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
   563    565       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   564    566       if( pNew==0 && mem0.alarmCallback ){
   565    567         sqlite3MallocAlarm((int)nBytes);
   566    568         pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   567    569       }
   568    570       if( pNew ){
   569    571         nNew = sqlite3MallocSize(pNew);
   570    572         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
   571    573       }
   572    574       sqlite3_mutex_leave(mem0.mutex);
   573    575     }else{
   574    576       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
   575    577     }
   576         -  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
          578  +  assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-11148-40995 */
   577    579     return pNew;
   578    580   }
   579    581   
   580    582   /*
   581    583   ** The public interface to sqlite3Realloc.  Make sure that the memory
   582    584   ** subsystem is initialized prior to invoking sqliteRealloc.
   583    585   */
   584    586   void *sqlite3_realloc(void *pOld, int n){
   585    587   #ifndef SQLITE_OMIT_AUTOINIT
   586    588     if( sqlite3_initialize() ) return 0;
   587    589   #endif
   588         -  if( n<0 ) n = 0;
          590  +  if( n<0 ) n = 0;  /* IMP: R-26507-47431 */
   589    591     return sqlite3Realloc(pOld, n);
   590    592   }
   591    593   void *sqlite3_realloc64(void *pOld, sqlite3_uint64 n){
   592    594   #ifndef SQLITE_OMIT_AUTOINIT
   593    595     if( sqlite3_initialize() ) return 0;
   594    596   #endif
   595    597     return sqlite3Realloc(pOld, n);
................................................................................
   668    670       return 0;
   669    671     }
   670    672   #endif
   671    673     p = sqlite3Malloc(n);
   672    674     if( !p && db ){
   673    675       db->mallocFailed = 1;
   674    676     }
   675         -  sqlite3MemdebugSetType(p, MEMTYPE_DB |
   676         -         ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
          677  +  sqlite3MemdebugSetType(p, 
          678  +         (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
   677    679     return p;
   678    680   }
   679    681   
   680    682   /*
   681    683   ** Resize the block of memory pointed to by p to n bytes. If the
   682    684   ** resize fails, set the mallocFailed flag in the connection object.
   683    685   */
................................................................................
   695    697         }
   696    698         pNew = sqlite3DbMallocRaw(db, n);
   697    699         if( pNew ){
   698    700           memcpy(pNew, p, db->lookaside.sz);
   699    701           sqlite3DbFree(db, p);
   700    702         }
   701    703       }else{
   702         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
   703         -      assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
          704  +      assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
          705  +      assert( sqlite3MemdebugNoType(p, ~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
   704    706         sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
   705    707         pNew = sqlite3_realloc64(p, n);
   706    708         if( !pNew ){
   707         -        sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
   708    709           db->mallocFailed = 1;
   709    710         }
   710         -      sqlite3MemdebugSetType(pNew, MEMTYPE_DB | 
          711  +      sqlite3MemdebugSetType(pNew,
   711    712               (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
   712    713       }
   713    714     }
   714    715     return pNew;
   715    716   }
   716    717   
   717    718   /*

Changes to src/mem2.c.

   390    390   /*
   391    391   ** Return TRUE if the mask of type in eType matches the type of the
   392    392   ** allocation p.  Also return true if p==NULL.
   393    393   **
   394    394   ** This routine is designed for use within an assert() statement, to
   395    395   ** verify the type of an allocation.  For example:
   396    396   **
   397         -**     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
          397  +**     assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
   398    398   */
   399    399   int sqlite3MemdebugHasType(void *p, u8 eType){
   400    400     int rc = 1;
   401    401     if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   402    402       struct MemBlockHdr *pHdr;
   403    403       pHdr = sqlite3MemsysGetHeader(p);
   404    404       assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
................................................................................
   412    412   /*
   413    413   ** Return TRUE if the mask of type in eType matches no bits of the type of the
   414    414   ** allocation p.  Also return true if p==NULL.
   415    415   **
   416    416   ** This routine is designed for use within an assert() statement, to
   417    417   ** verify the type of an allocation.  For example:
   418    418   **
   419         -**     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
          419  +**     assert( sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
   420    420   */
   421    421   int sqlite3MemdebugNoType(void *p, u8 eType){
   422    422     int rc = 1;
   423    423     if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
   424    424       struct MemBlockHdr *pHdr;
   425    425       pHdr = sqlite3MemsysGetHeader(p);
   426    426       assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */

Changes to src/pager.c.

  3626   3626       if( rc==SQLITE_OK ){
  3627   3627         pNew = (char *)sqlite3PageMalloc(pageSize);
  3628   3628         if( !pNew ) rc = SQLITE_NOMEM;
  3629   3629       }
  3630   3630   
  3631   3631       if( rc==SQLITE_OK ){
  3632   3632         pager_reset(pPager);
  3633         -      sqlite3PageFree(pPager->pTmpSpace);
  3634         -      pPager->pTmpSpace = pNew;
  3635   3633         rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
  3636   3634       }
  3637   3635       if( rc==SQLITE_OK ){
         3636  +      sqlite3PageFree(pPager->pTmpSpace);
         3637  +      pPager->pTmpSpace = pNew;
  3638   3638         pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
  3639   3639         pPager->pageSize = pageSize;
         3640  +    }else{
         3641  +      sqlite3PageFree(pNew);
  3640   3642       }
  3641   3643     }
  3642   3644   
  3643   3645     *pPageSize = pPager->pageSize;
  3644   3646     if( rc==SQLITE_OK ){
  3645   3647       if( nReserve<0 ) nReserve = pPager->nReserve;
  3646   3648       assert( nReserve>=0 && nReserve<1000 );

Changes to src/parse.y.

   957    957   expr(A) ::= expr(X) NOT NULL(E). {spanUnaryPostfix(&A,pParse,TK_NOTNULL,&X,&E);}
   958    958   
   959    959   %include {
   960    960     /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
   961    961     ** unary TK_ISNULL or TK_NOTNULL expression. */
   962    962     static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
   963    963       sqlite3 *db = pParse->db;
   964         -    if( db->mallocFailed==0 && pY->op==TK_NULL ){
          964  +    if( pY && pA && pY->op==TK_NULL ){
   965    965         pA->op = (u8)op;
   966    966         sqlite3ExprDelete(db, pA->pRight);
   967    967         pA->pRight = 0;
   968    968       }
   969    969     }
   970    970   }
   971    971   

Changes to src/sqlite.h.in.

  2665   2665   ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
  2666   2666   ** object.)^ ^(If the database is opened (and/or created) successfully, then
  2667   2667   ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
  2668   2668   ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
  2669   2669   ** an English language description of the error following a failure of any
  2670   2670   ** of the sqlite3_open() routines.
  2671   2671   **
  2672         -** ^The default encoding for the database will be UTF-8 if
  2673         -** sqlite3_open() or sqlite3_open_v2() is called and
  2674         -** UTF-16 in the native byte order if sqlite3_open16() is used.
         2672  +** ^The default encoding will be UTF-8 for databases created using
         2673  +** sqlite3_open() or sqlite3_open_v2().  ^The default encoding for databases
         2674  +** created using sqlite3_open16() will be UTF-16 in the native byte order.
  2675   2675   **
  2676   2676   ** Whether or not an error occurs when it is opened, resources
  2677   2677   ** associated with the [database connection] handle should be released by
  2678   2678   ** passing it to [sqlite3_close()] when it is no longer required.
  2679   2679   **
  2680   2680   ** The sqlite3_open_v2() interface works like sqlite3_open()
  2681   2681   ** except that it accepts two additional parameters for additional control
................................................................................
  2755   2755   ** present, is ignored.
  2756   2756   **
  2757   2757   ** ^SQLite uses the path component of the URI as the name of the disk file
  2758   2758   ** which contains the database. ^If the path begins with a '/' character, 
  2759   2759   ** then it is interpreted as an absolute path. ^If the path does not begin 
  2760   2760   ** with a '/' (meaning that the authority section is omitted from the URI)
  2761   2761   ** then the path is interpreted as a relative path. 
  2762         -** ^On windows, the first component of an absolute path 
  2763         -** is a drive specification (e.g. "C:").
         2762  +** ^(On windows, the first component of an absolute path 
         2763  +** is a drive specification (e.g. "C:").)^
  2764   2764   **
  2765   2765   ** [[core URI query parameters]]
  2766   2766   ** The query component of a URI may contain parameters that are interpreted
  2767   2767   ** either by SQLite itself, or by a [VFS | custom VFS implementation].
  2768         -** SQLite interprets the following three query parameters:
         2768  +** SQLite and its built-in [VFSes] interpret the
         2769  +** following query parameters:
  2769   2770   **
  2770   2771   ** <ul>
  2771   2772   **   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
  2772   2773   **     a VFS object that provides the operating system interface that should
  2773   2774   **     be used to access the database file on disk. ^If this option is set to
  2774   2775   **     an empty string the default VFS object is used. ^Specifying an unknown
  2775   2776   **     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
................................................................................
  2796   2797   **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
  2797   2798   **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is 
  2798   2799   **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
  2799   2800   **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
  2800   2801   **     a URI filename, its value overrides any behavior requested by setting
  2801   2802   **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
  2802   2803   **
  2803         -**  <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
  2804         -**     "1") or "false" (or "off" or "no" or "0") to indicate that the
         2804  +**  <li> <b>psow</b>: ^The psow parameter indicates whether or not the
  2805   2805   **     [powersafe overwrite] property does or does not apply to the
  2806         -**     storage media on which the database file resides.  ^The psow query
  2807         -**     parameter only works for the built-in unix and Windows VFSes.
         2806  +**     storage media on which the database file resides.
  2808   2807   **
  2809   2808   **  <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
  2810   2809   **     which if set disables file locking in rollback journal modes.  This
  2811   2810   **     is useful for accessing a database on a filesystem that does not
  2812   2811   **     support locking.  Caution:  Database corruption might result if two
  2813   2812   **     or more processes write to the same database and any one of those
  2814   2813   **     processes uses nolock=1.
................................................................................
  3395   3394   ** that parameter must be the byte offset
  3396   3395   ** where the NUL terminator would occur assuming the string were NUL
  3397   3396   ** terminated.  If any NUL characters occur at byte offsets less than 
  3398   3397   ** the value of the fourth parameter then the resulting string value will
  3399   3398   ** contain embedded NULs.  The result of expressions involving strings
  3400   3399   ** with embedded NULs is undefined.
  3401   3400   **
  3402         -** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
  3403         -** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
         3401  +** ^The fifth argument to the BLOB and string binding interfaces
         3402  +** is a destructor used to dispose of the BLOB or
  3404   3403   ** string after SQLite has finished with it.  ^The destructor is called
  3405         -** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
  3406         -** sqlite3_bind_text(), or sqlite3_bind_text16() fails.  
         3404  +** to dispose of the BLOB or string even if the call to bind API fails.
  3407   3405   ** ^If the fifth argument is
  3408   3406   ** the special value [SQLITE_STATIC], then SQLite assumes that the
  3409   3407   ** information is in static, unmanaged space and does not need to be freed.
  3410   3408   ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
  3411   3409   ** SQLite makes its own private copy of the data immediately, before
  3412   3410   ** the sqlite3_bind_*() routine returns.
  3413   3411   **
  3414   3412   ** ^The sixth argument to sqlite3_bind_text64() must be one of
  3415   3413   ** [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE]
  3416   3414   ** to specify the encoding of the text in the third parameter.  If
  3417         -** the sixth argument to sqlite3_bind_text64() is not how of the
         3415  +** the sixth argument to sqlite3_bind_text64() is not one of the
  3418   3416   ** allowed values shown above, or if the text encoding is different
  3419   3417   ** from the encoding specified by the sixth parameter, then the behavior
  3420   3418   ** is undefined.
  3421   3419   **
  3422   3420   ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
  3423   3421   ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
  3424   3422   ** (just an integer to hold its size) while it is being processed.
................................................................................
  4446   4444   ** of the application-defined function to be the 64-bit signed integer
  4447   4445   ** value given in the 2nd argument.
  4448   4446   **
  4449   4447   ** ^The sqlite3_result_null() interface sets the return value
  4450   4448   ** of the application-defined function to be NULL.
  4451   4449   **
  4452   4450   ** ^The sqlite3_result_text(), sqlite3_result_text16(),
  4453         -** sqlite3_result_text16le(), and sqlite3_result_text16be()
         4451  +** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
  4454   4452   ** set the return value of the application-defined function to be
  4455   4453   ** a text string which is represented as UTF-8, UTF-16 native byte order,
  4456   4454   ** UTF-16 little endian, or UTF-16 big endian, respectively.
  4457   4455   ** ^The sqlite3_result_text64() interface sets the return value of an
  4458   4456   ** application-defined function to be a text string in an encoding
  4459   4457   ** specified by the fifth (and last) parameter, which must be one
  4460   4458   ** of [SQLITE_UTF8], [SQLITE_UTF16], [SQLITE_UTF16BE], or [SQLITE_UTF16LE].

Changes to src/sqliteInt.h.

  1807   1807     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
  1808   1808     unsigned isCovering:1;   /* True if this is a covering index */
  1809   1809   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1810   1810     int nSample;             /* Number of elements in aSample[] */
  1811   1811     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
  1812   1812     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
  1813   1813     IndexSample *aSample;    /* Samples of the left-most key */
         1814  +  tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this table */
  1814   1815   #endif
  1815   1816   };
  1816   1817   
  1817   1818   /*
  1818   1819   ** Allowed values for Index.idxType
  1819   1820   */
  1820   1821   #define SQLITE_IDXTYPE_APPDEF      0   /* Created using CREATE INDEX */
................................................................................
  3805   3806     int sqlite3MemdebugNoType(void*,u8);
  3806   3807   #else
  3807   3808   # define sqlite3MemdebugSetType(X,Y)  /* no-op */
  3808   3809   # define sqlite3MemdebugHasType(X,Y)  1
  3809   3810   # define sqlite3MemdebugNoType(X,Y)   1
  3810   3811   #endif
  3811   3812   #define MEMTYPE_HEAP       0x01  /* General heap allocations */
  3812         -#define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
         3813  +#define MEMTYPE_LOOKASIDE  0x02  /* Heap that might have been lookaside */
  3813   3814   #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
  3814   3815   #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
  3815         -#define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
  3816   3816   
  3817   3817   #if (SQLITE_ENABLE_APPLE_SPI>0) && defined(__APPLE__)
  3818   3818   
  3819   3819   /*
  3820   3820   ** An instance of the following structure is used to hold the process ID
  3821   3821   ** and return-by-reference lockstate value.  The SQLITE_FCNTL_LOCKSTATE_PID
  3822   3822   ** requires the 4th argument to sqlite3_file_control to be a pointer to an

Changes to src/status.c.

   209    209         db->pnBytesFreed = &nByte;
   210    210         for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
   211    211           sqlite3VdbeClearObject(db, pVdbe);
   212    212           sqlite3DbFree(db, pVdbe);
   213    213         }
   214    214         db->pnBytesFreed = 0;
   215    215   
   216         -      *pHighwater = 0;
          216  +      *pHighwater = 0;  /* IMP: R-64479-57858 */
   217    217         *pCurrent = nByte;
   218    218   
   219    219         break;
   220    220       }
   221    221   
   222    222       /*
   223    223       ** Set *pCurrent to the total cache hits or misses encountered by all
................................................................................
   234    234   
   235    235         for(i=0; i<db->nDb; i++){
   236    236           if( db->aDb[i].pBt ){
   237    237             Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
   238    238             sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
   239    239           }
   240    240         }
   241         -      *pHighwater = 0;
          241  +      *pHighwater = 0; /* IMP: R-42420-56072 */
          242  +                       /* IMP: R-54100-20147 */
          243  +                       /* IMP: R-29431-39229 */
   242    244         *pCurrent = nRet;
   243    245         break;
   244    246       }
   245    247   
   246    248       /* Set *pCurrent to non-zero if there are unresolved deferred foreign
   247    249       ** key constraints.  Set *pCurrent to zero if all foreign key constraints
   248    250       ** have been satisfied.  The *pHighwater is always set to zero.
   249    251       */
   250    252       case SQLITE_DBSTATUS_DEFERRED_FKS: {
   251         -      *pHighwater = 0;
          253  +      *pHighwater = 0;  /* IMP: R-11967-56545 */
   252    254         *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
   253    255         break;
   254    256       }
   255    257   
   256    258       default: {
   257    259         rc = SQLITE_ERROR;
   258    260       }
   259    261     }
   260    262     sqlite3_mutex_leave(db->mutex);
   261    263     return rc;
   262    264   }

Changes to src/test1.c.

  5701   5701       { "SQLITE_LIMIT_COMPOUND_SELECT",     SQLITE_LIMIT_COMPOUND_SELECT      },
  5702   5702       { "SQLITE_LIMIT_VDBE_OP",             SQLITE_LIMIT_VDBE_OP              },
  5703   5703       { "SQLITE_LIMIT_FUNCTION_ARG",        SQLITE_LIMIT_FUNCTION_ARG         },
  5704   5704       { "SQLITE_LIMIT_ATTACHED",            SQLITE_LIMIT_ATTACHED             },
  5705   5705       { "SQLITE_LIMIT_LIKE_PATTERN_LENGTH", SQLITE_LIMIT_LIKE_PATTERN_LENGTH  },
  5706   5706       { "SQLITE_LIMIT_VARIABLE_NUMBER",     SQLITE_LIMIT_VARIABLE_NUMBER      },
  5707   5707       { "SQLITE_LIMIT_TRIGGER_DEPTH",       SQLITE_LIMIT_TRIGGER_DEPTH        },
         5708  +    { "SQLITE_LIMIT_WORKER_THREADS",      SQLITE_LIMIT_WORKER_THREADS       },
  5708   5709       
  5709   5710       /* Out of range test cases */
  5710   5711       { "SQLITE_LIMIT_TOOSMALL",            -1,                               },
  5711         -    { "SQLITE_LIMIT_TOOBIG",              SQLITE_LIMIT_TRIGGER_DEPTH+1      },
         5712  +    { "SQLITE_LIMIT_TOOBIG",              SQLITE_LIMIT_WORKER_THREADS+1     },
  5712   5713     };
  5713   5714     int i, id;
  5714   5715     int val;
  5715   5716     const char *zId;
  5716   5717   
  5717   5718     if( objc!=4 ){
  5718   5719       Tcl_AppendResult(interp, "wrong # args: should be \"",

Changes to src/test_config.c.

   676    676     LINKVAR( MAX_TRIGGER_DEPTH );
   677    677     LINKVAR( DEFAULT_TEMP_CACHE_SIZE );
   678    678     LINKVAR( DEFAULT_CACHE_SIZE );
   679    679     LINKVAR( DEFAULT_PAGE_SIZE );
   680    680     LINKVAR( DEFAULT_FILE_FORMAT );
   681    681     LINKVAR( MAX_ATTACHED );
   682    682     LINKVAR( MAX_DEFAULT_PAGE_SIZE );
          683  +  LINKVAR( MAX_WORKER_THREADS );
   683    684   
   684    685     {
   685    686       static const int cv_TEMP_STORE = SQLITE_TEMP_STORE;
   686    687       Tcl_LinkVar(interp, "TEMP_STORE", (char *)&(cv_TEMP_STORE),
   687    688                   TCL_LINK_INT | TCL_LINK_READ_ONLY);
   688    689     }
   689    690   

Changes to src/vdbe.c.

  1554   1554   
  1555   1555     assert( pOp->p4type==P4_FUNCDEF );
  1556   1556     ctx.pFunc = pOp->p4.pFunc;
  1557   1557     ctx.iOp = pc;
  1558   1558     ctx.pVdbe = p;
  1559   1559     MemSetTypeFlag(ctx.pOut, MEM_Null);
  1560   1560     ctx.fErrorOrAux = 0;
  1561         -  assert( db->lastRowid==lastRowid );
         1561  +  db->lastRowid = lastRowid;
  1562   1562     (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  1563   1563     lastRowid = db->lastRowid;  /* Remember rowid changes made by xFunc */
  1564   1564   
  1565   1565     /* If the function returned an error, throw an exception */
  1566   1566     if( ctx.fErrorOrAux ){
  1567   1567       if( ctx.isError ){
  1568   1568         sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
................................................................................
  4329   4329     }else{
  4330   4330       VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
  4331   4331       assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
  4332   4332       if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  4333   4333         goto too_big;
  4334   4334       }
  4335   4335     }
  4336         -  if( sqlite3VdbeMemClearAndResize(pOut, n) ){
         4336  +  testcase( n==0 );
         4337  +  if( sqlite3VdbeMemClearAndResize(pOut, MAX(n,32)) ){
  4337   4338       goto no_mem;
  4338   4339     }
  4339   4340     pOut->n = n;
  4340   4341     MemSetTypeFlag(pOut, MEM_Blob);
  4341   4342     if( pC->isTable==0 ){
  4342   4343       rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
  4343   4344     }else{

Changes to src/vdbeapi.c.

   330    330     const char *z, 
   331    331     sqlite3_uint64 n,
   332    332     void (*xDel)(void *),
   333    333     unsigned char enc
   334    334   ){
   335    335     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   336    336     assert( xDel!=SQLITE_DYNAMIC );
          337  +  if( enc==SQLITE_UTF16 ) enc = SQLITE_UTF16NATIVE;
   337    338     if( n>0x7fffffff ){
   338    339       (void)invokeValueDestructor(z, xDel, pCtx);
   339    340     }else{
   340    341       setResultStrOrError(pCtx, z, (int)n, enc, xDel);
   341    342     }
   342    343   }
   343    344   #ifndef SQLITE_OMIT_UTF16

Changes to src/vdbemem.c.

    27     27   */
    28     28   int sqlite3VdbeCheckMemInvariants(Mem *p){
    29     29     /* If MEM_Dyn is set then Mem.xDel!=0.  
    30     30     ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
    31     31     */
    32     32     assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
    33     33   
    34         -  /* MEM_Dyn may only be set if Mem.szMalloc==0 */
           34  +  /* MEM_Dyn may only be set if Mem.szMalloc==0.  In this way we
           35  +  ** ensure that if Mem.szMalloc>0 then it is safe to do
           36  +  ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
           37  +  ** That saves a few cycles in inner loops. */
    35     38     assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
    36     39   
    37     40     /* Cannot be both MEM_Int and MEM_Real at the same time */
    38     41     assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
    39     42   
    40     43     /* The szMalloc field holds the correct memory allocation size */
    41     44     assert( p->szMalloc==0
................................................................................
   163    166   ** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
   164    167   ** values are preserved.
   165    168   **
   166    169   ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
   167    170   ** if unable to complete the resizing.
   168    171   */
   169    172   int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
   170         -  assert( szNew>=0 );
          173  +  assert( szNew>0 );
          174  +  assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
   171    175     if( pMem->szMalloc<szNew ){
   172    176       return sqlite3VdbeMemGrow(pMem, szNew, 0);
   173    177     }
   174    178     assert( (pMem->flags & MEM_Dyn)==0 );
   175    179     pMem->z = pMem->zMalloc;
   176    180     pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
   177    181     return SQLITE_OK;
................................................................................
   887    891       int nAlloc = nByte;
   888    892       if( flags&MEM_Term ){
   889    893         nAlloc += (enc==SQLITE_UTF8?1:2);
   890    894       }
   891    895       if( nByte>iLimit ){
   892    896         return SQLITE_TOOBIG;
   893    897       }
   894         -    if( sqlite3VdbeMemClearAndResize(pMem, nAlloc) ){
          898  +    testcase( nAlloc==0 );
          899  +    testcase( nAlloc==31 );
          900  +    testcase( nAlloc==32 );
          901  +    if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
   895    902         return SQLITE_NOMEM;
   896    903       }
   897    904       memcpy(pMem->z, z, nAlloc);
   898    905     }else if( xDel==SQLITE_DYNAMIC ){
   899    906       sqlite3VdbeMemRelease(pMem);
   900    907       pMem->zMalloc = pMem->z = (char *)z;
   901    908       pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
................................................................................
   990    997   }
   991    998   
   992    999   /*
   993   1000   ** The pVal argument is known to be a value other than NULL.
   994   1001   ** Convert it into a string with encoding enc and return a pointer
   995   1002   ** to a zero-terminated version of that string.
   996   1003   */
   997         -SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
         1004  +static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
   998   1005     assert( pVal!=0 );
   999   1006     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
  1000   1007     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
  1001   1008     assert( (pVal->flags & MEM_RowSet)==0 );
  1002   1009     assert( (pVal->flags & (MEM_Null))==0 );
  1003   1010     if( pVal->flags & (MEM_Blob|MEM_Str) ){
  1004   1011       pVal->flags |= MEM_Str;

Changes to src/where.c.

  2202   2202         }else{
  2203   2203           /* Note: this call could be optimized away - since the same values must 
  2204   2204           ** have been requested when testing key $P in whereEqualScanEst().  */
  2205   2205           whereKeyStats(pParse, p, pRec, 0, a);
  2206   2206           iLower = a[0];
  2207   2207           iUpper = a[0] + a[1];
  2208   2208         }
         2209  +
         2210  +      assert( pLower==0 || (pLower->eOperator & (WO_GT|WO_GE))!=0 );
         2211  +      assert( pUpper==0 || (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
         2212  +      assert( p->pKeyInfo!=0 && p->pKeyInfo->aSortOrder!=0 );
         2213  +      if( p->pKeyInfo->aSortOrder[nEq] ){
         2214  +        /* The roles of pLower and pUpper are swapped for a DESC index */
         2215  +        SWAP(WhereTerm*, pLower, pUpper);
         2216  +      }
  2209   2217   
  2210   2218         /* If possible, improve on the iLower estimate using ($P:$L). */
  2211   2219         if( pLower ){
  2212   2220           int bOk;                    /* True if value is extracted from pExpr */
  2213   2221           Expr *pExpr = pLower->pExpr->pRight;
  2214         -        assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
  2215   2222           rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
  2216   2223           if( rc==SQLITE_OK && bOk ){
  2217   2224             tRowcnt iNew;
  2218   2225             whereKeyStats(pParse, p, pRec, 0, a);
  2219         -          iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
         2226  +          iNew = a[0] + ((pLower->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
  2220   2227             if( iNew>iLower ) iLower = iNew;
  2221   2228             nOut--;
  2222   2229             pLower = 0;
  2223   2230           }
  2224   2231         }
  2225   2232   
  2226   2233         /* If possible, improve on the iUpper estimate using ($P:$U). */
  2227   2234         if( pUpper ){
  2228   2235           int bOk;                    /* True if value is extracted from pExpr */
  2229   2236           Expr *pExpr = pUpper->pExpr->pRight;
  2230         -        assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
  2231   2237           rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
  2232   2238           if( rc==SQLITE_OK && bOk ){
  2233   2239             tRowcnt iNew;
  2234   2240             whereKeyStats(pParse, p, pRec, 1, a);
  2235         -          iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
         2241  +          iNew = a[0] + ((pUpper->eOperator & (WO_GT|WO_LE)) ? a[1] : 0);
  2236   2242             if( iNew<iUpper ) iUpper = iNew;
  2237   2243             nOut--;
  2238   2244             pUpper = 0;
  2239   2245           }
  2240   2246         }
  2241   2247   
  2242   2248         pBuilder->pRec = pRec;
................................................................................
  4135   4141     ppPrev = whereLoopFindLesser(&pWInfo->pLoops, pTemplate);
  4136   4142   
  4137   4143     if( ppPrev==0 ){
  4138   4144       /* There already exists a WhereLoop on the list that is better
  4139   4145       ** than pTemplate, so just ignore pTemplate */
  4140   4146   #if WHERETRACE_ENABLED /* 0x8 */
  4141   4147       if( sqlite3WhereTrace & 0x8 ){
  4142         -      sqlite3DebugPrintf("ins-noop: ");
         4148  +      sqlite3DebugPrintf("   skip: ");
  4143   4149         whereLoopPrint(pTemplate, pBuilder->pWC);
  4144   4150       }
  4145   4151   #endif
  4146   4152       return SQLITE_OK;  
  4147   4153     }else{
  4148   4154       p = *ppPrev;
  4149   4155     }
................................................................................
  4151   4157     /* If we reach this point it means that either p[] should be overwritten
  4152   4158     ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
  4153   4159     ** WhereLoop and insert it.
  4154   4160     */
  4155   4161   #if WHERETRACE_ENABLED /* 0x8 */
  4156   4162     if( sqlite3WhereTrace & 0x8 ){
  4157   4163       if( p!=0 ){
  4158         -      sqlite3DebugPrintf("ins-del:  ");
         4164  +      sqlite3DebugPrintf("replace: ");
  4159   4165         whereLoopPrint(p, pBuilder->pWC);
  4160   4166       }
  4161         -    sqlite3DebugPrintf("ins-new:  ");
         4167  +    sqlite3DebugPrintf("    add: ");
  4162   4168       whereLoopPrint(pTemplate, pBuilder->pWC);
  4163   4169     }
  4164   4170   #endif
  4165   4171     if( p==0 ){
  4166   4172       /* Allocate a new WhereLoop to add to the end of the list */
  4167   4173       *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
  4168   4174       if( p==0 ) return SQLITE_NOMEM;
................................................................................
  4178   4184         ppTail = whereLoopFindLesser(ppTail, pTemplate);
  4179   4185         if( ppTail==0 ) break;
  4180   4186         pToDel = *ppTail;
  4181   4187         if( pToDel==0 ) break;
  4182   4188         *ppTail = pToDel->pNextLoop;
  4183   4189   #if WHERETRACE_ENABLED /* 0x8 */
  4184   4190         if( sqlite3WhereTrace & 0x8 ){
  4185         -        sqlite3DebugPrintf("ins-del:  ");
         4191  +        sqlite3DebugPrintf(" delete: ");
  4186   4192           whereLoopPrint(pToDel, pBuilder->pWC);
  4187   4193         }
  4188   4194   #endif
  4189   4195         whereLoopDelete(db, pToDel);
  4190   4196       }
  4191   4197     }
  4192   4198     whereLoopXfer(db, p, pTemplate);

Added test/analyzeD.test.

            1  +# 2005 July 22
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.
           12  +# This file implements tests for the ANALYZE command.
           13  +#
           14  +# $Id: analyze.test,v 1.9 2008/08/11 18:44:58 drh Exp $
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +set ::testprefix analyzeD
           19  +
           20  +ifcapable {!stat4} {
           21  +  finish_test
           22  +  return
           23  +}
           24  +
           25  +
           26  +# Set up a table with the following properties:
           27  +#
           28  +#    * Contains 1000 rows.
           29  +#    * Column a contains even integers between 0 and 18, inclusive (so that
           30  +#      a=? for any such integer matches 100 rows).
           31  +#    * Column b contains integers between 0 and 9, inclusive.
           32  +#    * Column c contains integers between 0 and 199, inclusive (so that
           33  +#      for any such integer, c=? matches 5 rows).
           34  +#    * Then add 7 rows with a new value for "a" - 3001. The stat4 table will
           35  +#      not contain any samples with a=3001.
           36  +#
           37  +do_execsql_test 1.0 {
           38  +  CREATE TABLE t1(a, b, c);
           39  +}
           40  +do_test 1.1 {
           41  +  for {set i 1} {$i < 1000} {incr i} {
           42  +    set c [expr $i % 200]
           43  +    execsql { INSERT INTO t1(a, b, c) VALUES( 2*($i/100), $i%10, $c ) }
           44  +  }
           45  +
           46  +  execsql {
           47  +    INSERT INTO t1 VALUES(3001, 3001, 3001);
           48  +    INSERT INTO t1 VALUES(3001, 3001, 3002);
           49  +    INSERT INTO t1 VALUES(3001, 3001, 3003);
           50  +    INSERT INTO t1 VALUES(3001, 3001, 3004);
           51  +    INSERT INTO t1 VALUES(3001, 3001, 3005);
           52  +    INSERT INTO t1 VALUES(3001, 3001, 3006);
           53  +    INSERT INTO t1 VALUES(3001, 3001, 3007);
           54  +
           55  +    CREATE INDEX t1_ab ON t1(a, b);
           56  +    CREATE INDEX t1_c ON t1(c);
           57  +
           58  +    ANALYZE;
           59  +  }
           60  +} {}
           61  +
           62  +# With full ANALYZE data, SQLite sees that c=150 (5 rows) is better than
           63  +# a=3001 (7 rows).
           64  +#
           65  +do_eqp_test 1.2 {
           66  +  SELECT * FROM t1 WHERE a=3001 AND c=150;
           67  +} {
           68  +  0 0 0 {SEARCH TABLE t1 USING INDEX t1_c (c=?)}
           69  +}
           70  +
           71  +do_test 1.3 {
           72  +  execsql { DELETE FROM sqlite_stat1 }
           73  +  db close
           74  +  sqlite3 db test.db
           75  +} {}
           76  +
           77  +# Without stat1, because 3001 is larger than all samples in the stat4
           78  +# table, SQLite things that a=3001 matches just 1 row. So it (incorrectly)
           79  +# chooses it over the c=150 index (5 rows). Even with stat1 data, things
           80  +# worked this way before commit [e6f7f97dbc].
           81  +#
           82  +do_eqp_test 1.4 {
           83  +  SELECT * FROM t1 WHERE a=3001 AND c=150;
           84  +} {
           85  +  0 0 0 {SEARCH TABLE t1 USING INDEX t1_ab (a=?)}
           86  +}
           87  +
           88  +do_test 1.5 {
           89  +  execsql { 
           90  +    UPDATE t1 SET a=13 WHERE a = 3001;
           91  +    ANALYZE;
           92  +  }
           93  +} {}
           94  +
           95  +do_eqp_test 1.6 {
           96  +  SELECT * FROM t1 WHERE a=13 AND c=150;
           97  +} {
           98  +  0 0 0 {SEARCH TABLE t1 USING INDEX t1_c (c=?)}
           99  +}
          100  +
          101  +do_test 1.7 {
          102  +  execsql { DELETE FROM sqlite_stat1 }
          103  +  db close
          104  +  sqlite3 db test.db
          105  +} {}
          106  +
          107  +# Same test as 1.4, except this time the 7 rows that match the a=? condition 
          108  +# do not feature larger values than all rows in the stat4 table. So SQLite
          109  +# gets this right, even without stat1 data.
          110  +do_eqp_test 1.8 {
          111  +  SELECT * FROM t1 WHERE a=13 AND c=150;
          112  +} {
          113  +  0 0 0 {SEARCH TABLE t1 USING INDEX t1_c (c=?)}
          114  +}
          115  +
          116  +finish_test
          117  +

Added test/analyzeE.test.

            1  +# 2014-10-08
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements tests for using STAT4 information
           12  +# on a descending index in a range query.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set ::testprefix analyzeE
           18  +
           19  +ifcapable {!stat4} {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +# Verify that range queries on an ASCENDING index will use the
           25  +# index only if the range covers only a small fraction of the
           26  +# entries.
           27  +#
           28  +do_execsql_test analyzeE-1.0 {
           29  +  CREATE TABLE t1(a,b);
           30  +  WITH RECURSIVE
           31  +    cnt(x) AS (VALUES(1000) UNION ALL SELECT x+1 FROM cnt WHERE x<2000)
           32  +  INSERT INTO t1(a,b) SELECT x, x FROM cnt;
           33  +  CREATE INDEX t1a ON t1(a);
           34  +  ANALYZE;
           35  +} {}
           36  +do_execsql_test analyzeE-1.1 {
           37  +  EXPLAIN QUERY PLAN
           38  +  SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500;
           39  +} {/SCAN TABLE t1/}
           40  +do_execsql_test analyzeE-1.2 {
           41  +  EXPLAIN QUERY PLAN
           42  +  SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000;
           43  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           44  +do_execsql_test analyzeE-1.3 {
           45  +  EXPLAIN QUERY PLAN
           46  +  SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750;
           47  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           48  +do_execsql_test analyzeE-1.4 {
           49  +  EXPLAIN QUERY PLAN
           50  +  SELECT * FROM t1 WHERE a BETWEEN 1 AND 500
           51  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           52  +do_execsql_test analyzeE-1.5 {
           53  +  EXPLAIN QUERY PLAN
           54  +  SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000
           55  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           56  +do_execsql_test analyzeE-1.6 {
           57  +  EXPLAIN QUERY PLAN
           58  +  SELECT * FROM t1 WHERE a<500
           59  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           60  +do_execsql_test analyzeE-1.7 {
           61  +  EXPLAIN QUERY PLAN
           62  +  SELECT * FROM t1 WHERE a>2500
           63  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           64  +do_execsql_test analyzeE-1.8 {
           65  +  EXPLAIN QUERY PLAN
           66  +  SELECT * FROM t1 WHERE a>1900
           67  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           68  +do_execsql_test analyzeE-1.9 {
           69  +  EXPLAIN QUERY PLAN
           70  +  SELECT * FROM t1 WHERE a>1100
           71  +} {/SCAN TABLE t1/}
           72  +do_execsql_test analyzeE-1.10 {
           73  +  EXPLAIN QUERY PLAN
           74  +  SELECT * FROM t1 WHERE a<1100
           75  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           76  +do_execsql_test analyzeE-1.11 {
           77  +  EXPLAIN QUERY PLAN
           78  +  SELECT * FROM t1 WHERE a<1900
           79  +} {/SCAN TABLE t1/}
           80  +
           81  +# Verify that everything works the same on a DESCENDING index.
           82  +#
           83  +do_execsql_test analyzeE-2.0 {
           84  +  DROP INDEX t1a;
           85  +  CREATE INDEX t1a ON t1(a DESC);
           86  +  ANALYZE;
           87  +} {}
           88  +do_execsql_test analyzeE-2.1 {
           89  +  EXPLAIN QUERY PLAN
           90  +  SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500;
           91  +} {/SCAN TABLE t1/}
           92  +do_execsql_test analyzeE-2.2 {
           93  +  EXPLAIN QUERY PLAN
           94  +  SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000;
           95  +} {/SEARCH TABLE t1 USING INDEX t1a/}
           96  +do_execsql_test analyzeE-2.3 {
           97  +  EXPLAIN QUERY PLAN
           98  +  SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750;
           99  +} {/SEARCH TABLE t1 USING INDEX t1a/}
          100  +do_execsql_test analyzeE-2.4 {
          101  +  EXPLAIN QUERY PLAN
          102  +  SELECT * FROM t1 WHERE a BETWEEN 1 AND 500
          103  +} {/SEARCH TABLE t1 USING INDEX t1a/}
          104  +do_execsql_test analyzeE-2.5 {
          105  +  EXPLAIN QUERY PLAN
          106  +  SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000
          107  +} {/SEARCH TABLE t1 USING INDEX t1a/}
          108  +do_execsql_test analyzeE-2.6 {
          109  +  EXPLAIN QUERY PLAN
          110  +  SELECT * FROM t1 WHERE a<500
          111  +} {/SEARCH TABLE t1 USING INDEX t1a/}
          112  +do_execsql_test analyzeE-2.7 {
          113  +  EXPLAIN QUERY PLAN
          114  +  SELECT * FROM t1 WHERE a>2500
          115  +} {/SEARCH TABLE t1 USING INDEX t1a/}
          116  +do_execsql_test analyzeE-2.8 {
          117  +  EXPLAIN QUERY PLAN
          118  +  SELECT * FROM t1 WHERE a>1900
          119  +} {/SEARCH TABLE t1 USING INDEX t1a/}
          120  +do_execsql_test analyzeE-2.9 {
          121  +  EXPLAIN QUERY PLAN
          122  +  SELECT * FROM t1 WHERE a>1100
          123  +} {/SCAN TABLE t1/}
          124  +do_execsql_test analyzeE-2.10 {
          125  +  EXPLAIN QUERY PLAN
          126  +  SELECT * FROM t1 WHERE a<1100
          127  +} {/SEARCH TABLE t1 USING INDEX t1a/}
          128  +do_execsql_test analyzeE-2.11 {
          129  +  EXPLAIN QUERY PLAN
          130  +  SELECT * FROM t1 WHERE a<1900
          131  +} {/SCAN TABLE t1/}
          132  +
          133  +# Now do a range query on the second term of an ASCENDING index
          134  +# where the first term is constrained by equality.
          135  +#
          136  +do_execsql_test analyzeE-3.0 {
          137  +  DROP TABLE t1;
          138  +  CREATE TABLE t1(a,b,c);
          139  +  WITH RECURSIVE
          140  +    cnt(x) AS (VALUES(1000) UNION ALL SELECT x+1 FROM cnt WHERE x<2000)
          141  +  INSERT INTO t1(a,b,c) SELECT x, x, 123 FROM cnt;
          142  +  CREATE INDEX t1ca ON t1(c,a);
          143  +  ANALYZE;
          144  +} {}
          145  +do_execsql_test analyzeE-3.1 {
          146  +  EXPLAIN QUERY PLAN
          147  +  SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500 AND c=123;
          148  +} {/SCAN TABLE t1/}
          149  +do_execsql_test analyzeE-3.2 {
          150  +  EXPLAIN QUERY PLAN
          151  +  SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000 AND c=123;
          152  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          153  +do_execsql_test analyzeE-3.3 {
          154  +  EXPLAIN QUERY PLAN
          155  +  SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750 AND c=123;
          156  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          157  +do_execsql_test analyzeE-3.4 {
          158  +  EXPLAIN QUERY PLAN
          159  +  SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 AND c=123
          160  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          161  +do_execsql_test analyzeE-3.5 {
          162  +  EXPLAIN QUERY PLAN
          163  +  SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 AND c=123
          164  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          165  +do_execsql_test analyzeE-3.6 {
          166  +  EXPLAIN QUERY PLAN
          167  +  SELECT * FROM t1 WHERE a<500 AND c=123
          168  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          169  +do_execsql_test analyzeE-3.7 {
          170  +  EXPLAIN QUERY PLAN
          171  +  SELECT * FROM t1 WHERE a>2500 AND c=123
          172  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          173  +do_execsql_test analyzeE-3.8 {
          174  +  EXPLAIN QUERY PLAN
          175  +  SELECT * FROM t1 WHERE a>1900 AND c=123
          176  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          177  +do_execsql_test analyzeE-3.9 {
          178  +  EXPLAIN QUERY PLAN
          179  +  SELECT * FROM t1 WHERE a>1100 AND c=123
          180  +} {/SCAN TABLE t1/}
          181  +do_execsql_test analyzeE-3.10 {
          182  +  EXPLAIN QUERY PLAN
          183  +  SELECT * FROM t1 WHERE a<1100 AND c=123
          184  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          185  +do_execsql_test analyzeE-3.11 {
          186  +  EXPLAIN QUERY PLAN
          187  +  SELECT * FROM t1 WHERE a<1900 AND c=123
          188  +} {/SCAN TABLE t1/}
          189  +
          190  +# Repeat the 3.x tests using a DESCENDING index
          191  +#
          192  +do_execsql_test analyzeE-4.0 {
          193  +  DROP INDEX t1ca;
          194  +  CREATE INDEX t1ca ON t1(c ASC,a DESC);
          195  +  ANALYZE;
          196  +} {}
          197  +do_execsql_test analyzeE-4.1 {
          198  +  EXPLAIN QUERY PLAN
          199  +  SELECT * FROM t1 WHERE a BETWEEN 500 AND 2500 AND c=123;
          200  +} {/SCAN TABLE t1/}
          201  +do_execsql_test analyzeE-4.2 {
          202  +  EXPLAIN QUERY PLAN
          203  +  SELECT * FROM t1 WHERE a BETWEEN 2900 AND 3000 AND c=123;
          204  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          205  +do_execsql_test analyzeE-4.3 {
          206  +  EXPLAIN QUERY PLAN
          207  +  SELECT * FROM t1 WHERE a BETWEEN 1700 AND 1750 AND c=123;
          208  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          209  +do_execsql_test analyzeE-4.4 {
          210  +  EXPLAIN QUERY PLAN
          211  +  SELECT * FROM t1 WHERE a BETWEEN 1 AND 500 AND c=123
          212  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          213  +do_execsql_test analyzeE-4.5 {
          214  +  EXPLAIN QUERY PLAN
          215  +  SELECT * FROM t1 WHERE a BETWEEN 3000 AND 3000000 AND c=123
          216  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          217  +do_execsql_test analyzeE-4.6 {
          218  +  EXPLAIN QUERY PLAN
          219  +  SELECT * FROM t1 WHERE a<500 AND c=123
          220  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          221  +do_execsql_test analyzeE-4.7 {
          222  +  EXPLAIN QUERY PLAN
          223  +  SELECT * FROM t1 WHERE a>2500 AND c=123
          224  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          225  +do_execsql_test analyzeE-4.8 {
          226  +  EXPLAIN QUERY PLAN
          227  +  SELECT * FROM t1 WHERE a>1900 AND c=123
          228  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          229  +do_execsql_test analyzeE-4.9 {
          230  +  EXPLAIN QUERY PLAN
          231  +  SELECT * FROM t1 WHERE a>1100 AND c=123
          232  +} {/SCAN TABLE t1/}
          233  +do_execsql_test analyzeE-4.10 {
          234  +  EXPLAIN QUERY PLAN
          235  +  SELECT * FROM t1 WHERE a<1100 AND c=123
          236  +} {/SEARCH TABLE t1 USING INDEX t1ca/}
          237  +do_execsql_test analyzeE-4.11 {
          238  +  EXPLAIN QUERY PLAN
          239  +  SELECT * FROM t1 WHERE a<1900 AND c=123
          240  +} {/SCAN TABLE t1/}
          241  +
          242  +finish_test

Changes to test/corruptI.test.

    71     71     catchsql { SELECT * FROM r WHERE x >= 10.0 }
    72     72   } {1 {database disk image is malformed}}
    73     73   
    74     74   do_test 2.2 {
    75     75     catchsql { SELECT * FROM r WHERE x >= 10 }
    76     76   } {1 {database disk image is malformed}}
    77     77   
    78         -reset_db
    79         -
    80         -do_execsql_test 3.1 {
    81         -  PRAGMA page_size = 512;
    82         -  CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
    83         -  WITH s(a, b) AS (
    84         -    SELECT 2, 'abcdefghij'
    85         -    UNION ALL
    86         -    SELECT a+2, b FROM s WHERe a < 40
    87         -  )
    88         -  INSERT INTO t1 SELECT * FROM s;
    89         -} {}
    90         -
    91         -do_test 3.2 {
    92         -  hexio_write test.db [expr 512+3] 0054
    93         -  db close
    94         -  sqlite3 db test.db
    95         -  execsql { INSERT INTO t1 VALUES(5, 'klmnopqrst') }
    96         -  execsql { INSERT INTO t1 VALUES(7, 'klmnopqrst') }
    97         -} {}
    98         -
    99         -db close
   100         -sqlite3 db test.db
   101         -do_catchsql_test 3.2 {
   102         -  INSERT INTO t1 VALUES(9, 'klmnopqrst');
   103         -} {1 {database disk image is malformed}}
   104         -
           78  +if {[db one {SELECT sqlite_compileoption_used('ENABLE_OVERSIZE_CELL_CHECK')}]} {
           79  +  # The following tests only work if OVERSIZE_CELL_CHECK is disabled
           80  +} else {
           81  +  reset_db
           82  +  do_execsql_test 3.1 {
           83  +     PRAGMA auto_vacuum=0;
           84  +     PRAGMA page_size = 512;
           85  +     CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
           86  +     WITH s(a, b) AS (
           87  +       SELECT 2, 'abcdefghij'
           88  +       UNION ALL
           89  +       SELECT a+2, b FROM s WHERe a < 40
           90  +     )
           91  +     INSERT INTO t1 SELECT * FROM s;
           92  +   } {}
           93  +   
           94  +   do_test 3.2 {
           95  +     hexio_write test.db [expr 512+3] 0054
           96  +     db close
           97  +     sqlite3 db test.db
           98  +     execsql { INSERT INTO t1 VALUES(5, 'klmnopqrst') }
           99  +     execsql { INSERT INTO t1 VALUES(7, 'klmnopqrst') }
          100  +   } {}
          101  +   
          102  +   db close
          103  +   sqlite3 db test.db
          104  +   do_catchsql_test 3.3 {
          105  +     INSERT INTO t1 VALUES(9, 'klmnopqrst');
          106  +   } {1 {database disk image is malformed}}
          107  +} ;# end-if !defined(ENABLE_OVERSIZE_CELL_CHECK)
   105    108   finish_test

Changes to test/e_createtable.test.

   858    858     execsql { DELETE FROM t1 }
   859    859   } {
   860    860     1   "INSERT INTO t1(x, y) VALUES('abc', 'xyz')"   {'abc' 'xyz' NULL}
   861    861     2   "INSERT INTO t1(x, z) VALUES('abc', 'xyz')"   {'abc' NULL 'xyz'}
   862    862     3   "INSERT INTO t1 DEFAULT VALUES"               {NULL NULL NULL}
   863    863   }
   864    864   
   865         -# EVIDENCE-OF: R-62940-43005 An explicit DEFAULT clause may specify that
          865  +# EVIDENCE-OF: R-07343-35026 An explicit DEFAULT clause may specify that
   866    866   # the default value is NULL, a string constant, a blob constant, a
   867         -# signed-number, or any constant expression enclosed in parentheses. An
   868         -# explicit default value may also be one of the special case-independent
   869         -# keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.
          867  +# signed-number, or any constant expression enclosed in parentheses. A
          868  +# default value may also be one of the special case-independent keywords
          869  +# CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP.
   870    870   #
   871    871   do_execsql_test e_createtable-3.3.1 {
   872    872     CREATE TABLE t4(
   873    873       a DEFAULT NULL,
   874    874       b DEFAULT 'string constant',
   875    875       c DEFAULT X'424C4F42',
   876    876       d DEFAULT 1,
................................................................................
   880    880       h DEFAULT ( substr('abcd', 0, 2) || 'cd' ),
   881    881       i DEFAULT CURRENT_TIME,
   882    882       j DEFAULT CURRENT_DATE,
   883    883       k DEFAULT CURRENT_TIMESTAMP
   884    884     );
   885    885   } {}
   886    886   
   887         -# EVIDENCE-OF: R-36381-62919 For the purposes of the DEFAULT clause, an
   888         -# expression is considered constant provided that it does not contain
   889         -# any sub-queries, column or table references, or string literals
          887  +# EVIDENCE-OF: R-18415-27776 For the purposes of the DEFAULT clause, an
          888  +# expression is considered constant if it does contains no sub-queries,
          889  +# column or table references, bound parameters, or string literals
   890    890   # enclosed in double-quotes instead of single-quotes.
   891    891   #
   892    892   do_createtable_tests 3.4.1 -error {
   893    893     default value of column [x] is not constant
   894    894   } {
   895    895     1   {CREATE TABLE t5(x DEFAULT ( (SELECT 1) ))}  {}
   896    896     2   {CREATE TABLE t5(x DEFAULT ( "abc" ))}  {}
   897    897     3   {CREATE TABLE t5(x DEFAULT ( 1 IN (SELECT 1) ))}  {}
   898    898     4   {CREATE TABLE t5(x DEFAULT ( EXISTS (SELECT 1) ))}  {}
          899  +  5   {CREATE TABLE t5(x DEFAULT ( x!=?1 ))}  {}
   899    900   }
   900    901   do_createtable_tests 3.4.2 -repair {
   901    902     catchsql { DROP TABLE t5 }
   902    903   } {
   903    904     1   {CREATE TABLE t5(x DEFAULT ( 'abc' ))}  {}
   904    905     2   {CREATE TABLE t5(x DEFAULT ( 1 IN (1, 2, 3) ))}  {}
   905    906   }

Changes to test/e_uri.test.

   121    121     sqlite3_close $DB
   122    122   }
   123    123   
   124    124   # ensure uri processing enabled for the rest of the tests
   125    125   sqlite3_shutdown
   126    126   sqlite3_config_uri 1
   127    127   
          128  +# EVIDENCE-OF: R-06842-00595 If the URI contains an authority, then it
          129  +# must be either an empty string or the string "localhost".
          130  +#
   128    131   # EVIDENCE-OF: R-17482-00398 If the authority is not an empty string or
   129    132   # "localhost", an error is returned to the caller.
   130    133   #
   131    134   if {$tcl_platform(platform) == "unix"} {
   132    135     set flags [list SQLITE_OPEN_READWRITE SQLITE_OPEN_CREATE SQLITE_OPEN_URI]
   133    136     foreach {tn uri error} "
   134    137       1  {file://localhost[test_pwd /]test.db}   {not an error}

Changes to test/expr.test.

   201    201   test_expr expr-1.124 {i1=NULL, i2=NULL} \
   202    202     {CASE WHEN i1 IS NOT i2 THEN 'yes' ELSE 'no' END} no
   203    203   test_expr expr-1.125 {i1=6, i2=NULL} \
   204    204     {CASE WHEN i1 IS NOT i2 THEN 'yes' ELSE 'no' END} yes
   205    205   test_expr expr-1.126 {i1=8, i2=8} \
   206    206     {CASE WHEN i1 IS NOT i2 THEN 'yes' ELSE 'no' END} no
   207    207   
          208  +do_catchsql_test expr-1.127 {
          209  +  SELECT 1 IS #1;
          210  +} {1 {near "#1": syntax error}}
          211  +
   208    212   ifcapable floatingpoint {if {[working_64bit_int]} {
   209    213     test_expr expr-1.200\
   210    214         {i1=9223372036854775806, i2=1} {i1+i2}      9223372036854775807
   211    215     test_realnum_expr expr-1.201\
   212    216         {i1=9223372036854775806, i2=2} {i1+i2}      9.22337203685478e+18
   213    217     test_realnum_expr expr-1.202\
   214    218         {i1=9223372036854775806, i2=100000} {i1+i2} 9.22337203685488e+18

Changes to test/mallocA.test.

   115    115     }
   116    116   }
   117    117   
   118    118   do_execsql_test 7.0 {
   119    119     PRAGMA cache_size = 5;
   120    120   }
   121    121   do_faultsim_test 7 -faults oom-trans* -prep {
   122         -  if {$iFail < 500} { set iFail 2000 }
   123         -  if {$iFail > 1215} { set iFail 2000 }
   124    122   } -body {
   125    123     execsql {
   126    124       WITH r(x,y) AS (
   127    125         SELECT 1, randomblob(100)
   128    126         UNION ALL
   129    127         SELECT x+1, randomblob(100) FROM r
   130    128         LIMIT 1000

Added test/ovfl.test.

            1  +# 2014 October 01
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this file is testing the SQLITE_DIRECT_OVERFLOW_READ logic.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set testprefix ovfl
           18  +
           19  +# Populate table t2:
           20  +#
           21  +#   CREATE TABLE t1(c1 TEXT, c2 TEXT);
           22  +#
           23  +# with 2000 rows. In each row, c2 spans multiple overflow pages. The text
           24  +# value of c1 ranges in size from 1 to 2000 bytes. The idea is to create
           25  +# at least one row where the first byte of c2 is also the first byte of
           26  +# an overflow page. This was at one point exposing an obscure bug in the
           27  +# SQLITE_DIRECT_OVERFLOW_READ logic.
           28  +#
           29  +do_test 1.1 {
           30  +  set c2 [string repeat abcdefghij 200]
           31  +  execsql {
           32  +    PRAGMA cache_size = 10;
           33  +    CREATE TABLE t1(c1 TEXT, c2 TEXT);
           34  +    BEGIN;
           35  +  }
           36  +  for {set i 1} {$i <= 2000} {incr i} {
           37  +    set c1 [string repeat . $i]
           38  +    execsql { INSERT INTO t1 VALUES($c1, $c2) }
           39  +  }
           40  +  execsql COMMIT
           41  +} {}
           42  +
           43  +do_execsql_test 1.2 {
           44  +  SELECT sum(length(c2)) FROM t1;
           45  +} [expr 2000 * 2000]
           46  +
           47  +finish_test
           48  +
           49  +

Changes to test/sqllimits1.test.

    47     47   } $SQLITE_MAX_ATTACHED
    48     48   do_test sqllimits1-1.9 {
    49     49     sqlite3_limit db SQLITE_LIMIT_LIKE_PATTERN_LENGTH -1
    50     50   } $SQLITE_MAX_LIKE_PATTERN_LENGTH
    51     51   do_test sqllimits1-1.10 {
    52     52     sqlite3_limit db SQLITE_LIMIT_VARIABLE_NUMBER -1
    53     53   } $SQLITE_MAX_VARIABLE_NUMBER
           54  +do_test sqllimits1-1.11 {
           55  +  sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH -1
           56  +} $SQLITE_MAX_TRIGGER_DEPTH
           57  +do_test sqllimits1-1.12 {
           58  +  sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS 99999
           59  +  sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS -1
           60  +} $SQLITE_MAX_WORKER_THREADS
    54     61   
    55     62   # Limit parameters out of range.
    56     63   #
    57     64   do_test sqllimits1-1.20 {
    58     65     sqlite3_limit db SQLITE_LIMIT_TOOSMALL 123
    59     66   } {-1}
    60     67   do_test sqllimits1-1.21 {