/ Check-in [35c44a3c]
Login

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

Overview
Comment:Merge recent performance enhancements from trunk onto the threads branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | threads
Files: files | file ages | folders
SHA1:35c44a3c73e2e8b14ff194c41986f4bdb9dfe737
User & Date: drh 2014-08-29 14:40:07
Context
2014-08-29
16:20
Add SQLITE_LIMIT_WORKER_THREADS for controlling the maximum number of worker threads. check-in: 1b598c68 user: drh tags: threads
14:40
Merge recent performance enhancements from trunk onto the threads branch. check-in: 35c44a3c user: drh tags: threads
14:20
Not a fault after all, rather a testing mistake. Move this change back to trunk. Was: Avoid an unnecessary OP_Move operation for expression subqueries. check-in: 2a74129a user: drh tags: trunk
2014-08-25
23:44
Remove the SQLITE_CONFIG_WORKER_THREADS configuration parameter. The number of worker threads in the sorter is now determined only by the PRAGMA threads=N setting. check-in: e3305d4b user: drh tags: threads
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/expr.c.

  1908   1908         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
  1909   1909   
  1910   1910         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
  1911   1911         pSel = pExpr->x.pSelect;
  1912   1912         sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
  1913   1913         if( pExpr->op==TK_SELECT ){
  1914   1914           dest.eDest = SRT_Mem;
         1915  +        dest.iSdst = dest.iSDParm;
  1915   1916           sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
  1916   1917           VdbeComment((v, "Init subquery result"));
  1917   1918         }else{
  1918   1919           dest.eDest = SRT_Exists;
  1919   1920           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
  1920   1921           VdbeComment((v, "Init EXISTS result"));
  1921   1922         }

Changes to src/pager.c.

  3618   3618   
  3619   3619       if( rc==SQLITE_OK ){
  3620   3620         pager_reset(pPager);
  3621   3621         pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
  3622   3622         pPager->pageSize = pageSize;
  3623   3623         sqlite3PageFree(pPager->pTmpSpace);
  3624   3624         pPager->pTmpSpace = pNew;
  3625         -      sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
         3625  +      rc = sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
  3626   3626       }
  3627   3627     }
  3628   3628   
  3629   3629     *pPageSize = pPager->pageSize;
  3630   3630     if( rc==SQLITE_OK ){
  3631   3631       if( nReserve<0 ) nReserve = pPager->nReserve;
  3632   3632       assert( nReserve>=0 && nReserve<1000 );
................................................................................
  4381   4381     **
  4382   4382     ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
  4383   4383     ** regardless of whether or not a sync is required.  This is set during
  4384   4384     ** a rollback or by user request, respectively.
  4385   4385     **
  4386   4386     ** Spilling is also prohibited when in an error state since that could
  4387   4387     ** lead to database corruption.   In the current implementaton it 
  4388         -  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
         4388  +  ** is impossible for sqlite3PcacheFetch() to be called with createFlag==3
  4389   4389     ** while in the error state, hence it is impossible for this routine to
  4390   4390     ** be called in the error state.  Nevertheless, we include a NEVER()
  4391   4391     ** test for the error state as a safeguard against future changes.
  4392   4392     */
  4393   4393     if( NEVER(pPager->errCode) ) return SQLITE_OK;
  4394   4394     testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
  4395   4395     testcase( pPager->doNotSpill & SPILLFLAG_OFF );
................................................................................
  4717   4717     */
  4718   4718     if( rc==SQLITE_OK ){
  4719   4719       assert( pPager->memDb==0 );
  4720   4720       rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
  4721   4721       testcase( rc!=SQLITE_OK );
  4722   4722     }
  4723   4723   
  4724         -  /* If an error occurred in either of the blocks above, free the 
  4725         -  ** Pager structure and close the file.
         4724  +  /* Initialize the PCache object. */
         4725  +  if( rc==SQLITE_OK ){
         4726  +    assert( nExtra<1000 );
         4727  +    nExtra = ROUND8(nExtra);
         4728  +    rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
         4729  +                           !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
         4730  +  }
         4731  +
         4732  +  /* If an error occurred above, free the  Pager structure and close the file.
  4726   4733     */
  4727   4734     if( rc!=SQLITE_OK ){
  4728         -    assert( !pPager->pTmpSpace );
  4729   4735       sqlite3OsClose(pPager->fd);
         4736  +    sqlite3PageFree(pPager->pTmpSpace);
  4730   4737       sqlite3_free(pPager);
  4731   4738       return rc;
  4732   4739     }
  4733   4740   
  4734         -  /* Initialize the PCache object. */
  4735         -  assert( nExtra<1000 );
  4736         -  nExtra = ROUND8(nExtra);
  4737         -  sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
  4738         -                    !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
  4739         -
  4740   4741     PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
  4741   4742     IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
  4742   4743   
  4743   4744     pPager->useJournal = (u8)useJournal;
  4744   4745     /* pPager->stmtOpen = 0; */
  4745   4746     /* pPager->stmtInUse = 0; */
  4746   4747     /* pPager->nRef = 0; */
................................................................................
  5281   5282     }
  5282   5283   
  5283   5284     /* If the pager is in the error state, return an error immediately. 
  5284   5285     ** Otherwise, request the page from the PCache layer. */
  5285   5286     if( pPager->errCode!=SQLITE_OK ){
  5286   5287       rc = pPager->errCode;
  5287   5288     }else{
  5288         -
  5289   5289       if( bMmapOk && pagerUseWal(pPager) ){
  5290   5290         rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
  5291   5291         if( rc!=SQLITE_OK ) goto pager_acquire_err;
  5292   5292       }
  5293   5293   
  5294   5294       if( bMmapOk && iFrame==0 ){
  5295   5295         void *pData = 0;
................................................................................
  5296   5296   
  5297   5297         rc = sqlite3OsFetch(pPager->fd, 
  5298   5298             (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
  5299   5299         );
  5300   5300   
  5301   5301         if( rc==SQLITE_OK && pData ){
  5302   5302           if( pPager->eState>PAGER_READER ){
  5303         -          (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
         5303  +          pPg = sqlite3PagerLookup(pPager, pgno);
  5304   5304           }
  5305   5305           if( pPg==0 ){
  5306   5306             rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
  5307   5307           }else{
  5308   5308             sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
  5309   5309           }
  5310   5310           if( pPg ){
................................................................................
  5314   5314           }
  5315   5315         }
  5316   5316         if( rc!=SQLITE_OK ){
  5317   5317           goto pager_acquire_err;
  5318   5318         }
  5319   5319       }
  5320   5320   
  5321         -    rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
         5321  +    {
         5322  +      sqlite3_pcache_page *pBase;
         5323  +      pBase = sqlite3PcacheFetch(pPager->pPCache, pgno, 3);
         5324  +      if( pBase==0 ){
         5325  +        rc = sqlite3PcacheFetchStress(pPager->pPCache, pgno, &pBase);
         5326  +        if( rc!=SQLITE_OK ) goto pager_acquire_err;
         5327  +      }
         5328  +      pPg = *ppPage = sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pBase);
         5329  +      if( pPg==0 ) rc = SQLITE_NOMEM;
         5330  +    }
  5322   5331     }
  5323   5332   
  5324   5333     if( rc!=SQLITE_OK ){
  5325   5334       /* Either the call to sqlite3PcacheFetch() returned an error or the
  5326   5335       ** pager was already in the error-state when this function was called.
  5327   5336       ** Set pPg to 0 and jump to the exception handler.  */
  5328   5337       pPg = 0;
................................................................................
  5411   5420   ** See also sqlite3PagerGet().  The difference between this routine
  5412   5421   ** and sqlite3PagerGet() is that _get() will go to the disk and read
  5413   5422   ** in the page if the page is not already in cache.  This routine
  5414   5423   ** returns NULL if the page is not in cache or if a disk I/O error 
  5415   5424   ** has ever happened.
  5416   5425   */
  5417   5426   DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
  5418         -  PgHdr *pPg = 0;
         5427  +  sqlite3_pcache_page *pPage;
  5419   5428     assert( pPager!=0 );
  5420   5429     assert( pgno!=0 );
  5421   5430     assert( pPager->pPCache!=0 );
  5422         -  sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
  5423         -  return pPg;
         5431  +  pPage = sqlite3PcacheFetch(pPager->pPCache, pgno, 0);
         5432  +  return sqlite3PcacheFetchFinish(pPager->pPCache, pgno, pPage);
  5424   5433   }
  5425   5434   
  5426   5435   /*
  5427   5436   ** Release a page reference.
  5428   5437   **
  5429   5438   ** If the number of references to the page drop to zero, then the
  5430   5439   ** page is added to the LRU list.  When all references to all pages

Changes to src/pcache.c.

   139    139     if( p->pCache->bPurgeable ){
   140    140       if( p->pgno==1 ){
   141    141         p->pCache->pPage1 = 0;
   142    142       }
   143    143       sqlite3GlobalConfig.pcache2.xUnpin(p->pCache->pCache, p->pPage, 0);
   144    144     }
   145    145   }
          146  +
          147  +/*
          148  +** Compute the number of pages of cache requested.
          149  +*/
          150  +static int numberOfCachePages(PCache *p){
          151  +  if( p->szCache>=0 ){
          152  +    return p->szCache;
          153  +  }else{
          154  +    return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
          155  +  }
          156  +}
   146    157   
   147    158   /*************************************************** General Interfaces ******
   148    159   **
   149    160   ** Initialize and shutdown the page cache subsystem. Neither of these 
   150    161   ** functions are threadsafe.
   151    162   */
   152    163   int sqlite3PcacheInitialize(void){
................................................................................
   172    183   
   173    184   /*
   174    185   ** Create a new PCache object. Storage space to hold the object
   175    186   ** has already been allocated and is passed in as the p pointer. 
   176    187   ** The caller discovers how much space needs to be allocated by 
   177    188   ** calling sqlite3PcacheSize().
   178    189   */
   179         -void sqlite3PcacheOpen(
          190  +int sqlite3PcacheOpen(
   180    191     int szPage,                  /* Size of every page */
   181    192     int szExtra,                 /* Extra space associated with each page */
   182    193     int bPurgeable,              /* True if pages are on backing store */
   183    194     int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
   184    195     void *pStress,               /* Argument to xStress */
   185    196     PCache *p                    /* Preallocated space for the PCache */
   186    197   ){
   187    198     memset(p, 0, sizeof(PCache));
   188         -  p->szPage = szPage;
          199  +  p->szPage = 1;
   189    200     p->szExtra = szExtra;
   190    201     p->bPurgeable = bPurgeable;
   191    202     p->eCreate = 2;
   192    203     p->xStress = xStress;
   193    204     p->pStress = pStress;
   194    205     p->szCache = 100;
          206  +  return sqlite3PcacheSetPageSize(p, szPage);
   195    207   }
   196    208   
   197    209   /*
   198    210   ** Change the page size for PCache object. The caller must ensure that there
   199    211   ** are no outstanding page references when this function is called.
   200    212   */
   201         -void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
          213  +int sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
   202    214     assert( pCache->nRef==0 && pCache->pDirty==0 );
   203         -  if( pCache->pCache ){
   204         -    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   205         -    pCache->pCache = 0;
          215  +  if( pCache->szPage ){
          216  +    sqlite3_pcache *pNew;
          217  +    pNew = sqlite3GlobalConfig.pcache2.xCreate(
          218  +                szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
          219  +    );
          220  +    if( pNew==0 ) return SQLITE_NOMEM;
          221  +    sqlite3GlobalConfig.pcache2.xCachesize(pNew, numberOfCachePages(pCache));
          222  +    if( pCache->pCache ){
          223  +      sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
          224  +    }
          225  +    pCache->pCache = pNew;
   206    226       pCache->pPage1 = 0;
          227  +    pCache->szPage = szPage;
   207    228     }
   208         -  pCache->szPage = szPage;
   209         -}
   210         -
   211         -/*
   212         -** Compute the number of pages of cache requested.
   213         -*/
   214         -static int numberOfCachePages(PCache *p){
   215         -  if( p->szCache>=0 ){
   216         -    return p->szCache;
   217         -  }else{
   218         -    return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
   219         -  }
          229  +  return SQLITE_OK;
   220    230   }
   221    231   
   222    232   /*
   223    233   ** Try to obtain a page from the cache.
          234  +**
          235  +** This routine returns a pointer to an sqlite3_pcache_page object if
          236  +** such an object is already in cache, or if a new one is created.
          237  +** This routine returns a NULL pointer if the object was not in cache
          238  +** and could not be created.
          239  +**
          240  +** The createFlags should be 0 to check for existing pages and should
          241  +** be 3 (not 1, but 3) to try to create a new page.
          242  +**
          243  +** If the createFlag is 0, then NULL is always returned if the page
          244  +** is not already in the cache.  If createFlag is 1, then a new page
          245  +** is created only if that can be done without spilling dirty pages
          246  +** and without exceeding the cache size limit.
          247  +**
          248  +** The caller needs to invoke sqlite3PcacheFetchFinish() to properly
          249  +** initialize the sqlite3_pcache_page object and convert it into a
          250  +** PgHdr object.  The sqlite3PcacheFetch() and sqlite3PcacheFetchFinish()
          251  +** routines are split this way for performance reasons. When separated
          252  +** they can both (usually) operate without having to push values to
          253  +** the stack on entry and pop them back off on exit, which saves a
          254  +** lot of pushing and popping.
   224    255   */
   225         -int sqlite3PcacheFetch(
          256  +sqlite3_pcache_page *sqlite3PcacheFetch(
   226    257     PCache *pCache,       /* Obtain the page from this cache */
   227    258     Pgno pgno,            /* Page number to obtain */
   228         -  int createFlag,       /* If true, create page if it does not exist already */
   229         -  PgHdr **ppPage        /* Write the page here */
          259  +  int createFlag        /* If true, create page if it does not exist already */
   230    260   ){
   231         -  sqlite3_pcache_page *pPage;
   232         -  PgHdr *pPgHdr = 0;
   233    261     int eCreate;
   234    262   
   235    263     assert( pCache!=0 );
   236         -  assert( createFlag==1 || createFlag==0 );
          264  +  assert( pCache->pCache!=0 );
          265  +  assert( createFlag==3 || createFlag==0 );
   237    266     assert( pgno>0 );
   238    267   
   239         -  /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
   240         -  ** allocate it now.
   241         -  */
   242         -  if( !pCache->pCache ){
   243         -    sqlite3_pcache *p;
   244         -    if( !createFlag ){
   245         -      *ppPage = 0;
   246         -      return SQLITE_OK;
   247         -    }
   248         -    p = sqlite3GlobalConfig.pcache2.xCreate(
   249         -        pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
   250         -    );
   251         -    if( !p ){
   252         -      return SQLITE_NOMEM;
   253         -    }
   254         -    sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
   255         -    pCache->pCache = p;
   256         -  }
   257         -
   258    268     /* eCreate defines what to do if the page does not exist.
   259    269     **    0     Do not allocate a new page.  (createFlag==0)
   260    270     **    1     Allocate a new page if doing so is inexpensive.
   261    271     **          (createFlag==1 AND bPurgeable AND pDirty)
   262    272     **    2     Allocate a new page even it doing so is difficult.
   263    273     **          (createFlag==1 AND !(bPurgeable AND pDirty)
   264    274     */
   265         -  eCreate = createFlag==0 ? 0 : pCache->eCreate;
   266         -  assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
   267         -  pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
   268         -  if( !pPage && eCreate==1 ){
   269         -    PgHdr *pPg;
   270         -
   271         -    /* Find a dirty page to write-out and recycle. First try to find a 
   272         -    ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
   273         -    ** cleared), but if that is not possible settle for any other 
   274         -    ** unreferenced dirty page.
   275         -    */
   276         -    expensive_assert( pcacheCheckSynced(pCache) );
   277         -    for(pPg=pCache->pSynced; 
   278         -        pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
   279         -        pPg=pPg->pDirtyPrev
   280         -    );
   281         -    pCache->pSynced = pPg;
   282         -    if( !pPg ){
   283         -      for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
   284         -    }
   285         -    if( pPg ){
   286         -      int rc;
          275  +  eCreate = createFlag & pCache->eCreate;
          276  +  assert( eCreate==0 || eCreate==1 || eCreate==2 );
          277  +  assert( createFlag==0 || pCache->eCreate==eCreate );
          278  +  assert( createFlag==0 || eCreate==1+(!pCache->bPurgeable||!pCache->pDirty) );
          279  +  return sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
          280  +}
          281  +
          282  +/*
          283  +** If the sqlite3PcacheFetch() routine is unable to allocate a new
          284  +** page because new clean pages are available for reuse and the cache
          285  +** size limit has been reached, then this routine can be invoked to 
          286  +** try harder to allocate a page.  This routine might invoke the stress
          287  +** callback to spill dirty pages to the journal.  It will then try to
          288  +** allocate the new page and will only fail to allocate a new page on
          289  +** an OOM error.
          290  +**
          291  +** This routine should be invoked only after sqlite3PcacheFetch() fails.
          292  +*/
          293  +int sqlite3PcacheFetchStress(
          294  +  PCache *pCache,                 /* Obtain the page from this cache */
          295  +  Pgno pgno,                      /* Page number to obtain */
          296  +  sqlite3_pcache_page **ppPage    /* Write result here */
          297  +){
          298  +  PgHdr *pPg;
          299  +  if( pCache->eCreate==2 ) return 0;
          300  +
          301  +
          302  +  /* Find a dirty page to write-out and recycle. First try to find a 
          303  +  ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
          304  +  ** cleared), but if that is not possible settle for any other 
          305  +  ** unreferenced dirty page.
          306  +  */
          307  +  expensive_assert( pcacheCheckSynced(pCache) );
          308  +  for(pPg=pCache->pSynced; 
          309  +      pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC)); 
          310  +      pPg=pPg->pDirtyPrev
          311  +  );
          312  +  pCache->pSynced = pPg;
          313  +  if( !pPg ){
          314  +    for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
          315  +  }
          316  +  if( pPg ){
          317  +    int rc;
   287    318   #ifdef SQLITE_LOG_CACHE_SPILL
   288         -      sqlite3_log(SQLITE_FULL, 
   289         -                  "spill page %d making room for %d - cache used: %d/%d",
   290         -                  pPg->pgno, pgno,
   291         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
   292         -                  numberOfCachePages(pCache));
          319  +    sqlite3_log(SQLITE_FULL, 
          320  +                "spill page %d making room for %d - cache used: %d/%d",
          321  +                pPg->pgno, pgno,
          322  +                sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
          323  +                numberOfCachePages(pCache));
   293    324   #endif
   294         -      rc = pCache->xStress(pCache->pStress, pPg);
   295         -      if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
   296         -        return rc;
   297         -      }
   298         -    }
   299         -
   300         -    pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
   301         -  }
   302         -
   303         -  if( pPage ){
   304         -    pPgHdr = (PgHdr *)pPage->pExtra;
   305         -
   306         -    if( !pPgHdr->pPage ){
   307         -      memset(pPgHdr, 0, sizeof(PgHdr));
   308         -      pPgHdr->pPage = pPage;
   309         -      pPgHdr->pData = pPage->pBuf;
   310         -      pPgHdr->pExtra = (void *)&pPgHdr[1];
   311         -      memset(pPgHdr->pExtra, 0, pCache->szExtra);
   312         -      pPgHdr->pCache = pCache;
   313         -      pPgHdr->pgno = pgno;
   314         -    }
   315         -    assert( pPgHdr->pCache==pCache );
   316         -    assert( pPgHdr->pgno==pgno );
   317         -    assert( pPgHdr->pData==pPage->pBuf );
   318         -    assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
   319         -
   320         -    if( 0==pPgHdr->nRef ){
   321         -      pCache->nRef++;
   322         -    }
   323         -    pPgHdr->nRef++;
   324         -    if( pgno==1 ){
   325         -      pCache->pPage1 = pPgHdr;
          325  +    rc = pCache->xStress(pCache->pStress, pPg);
          326  +    if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
          327  +      return rc;
   326    328       }
   327    329     }
   328         -  *ppPage = pPgHdr;
   329         -  return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
          330  +  *ppPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
          331  +  return *ppPage==0 ? SQLITE_NOMEM : SQLITE_OK;
          332  +}
          333  +
          334  +/*
          335  +** This is a helper routine for sqlite3PcacheFetchFinish()
          336  +**
          337  +** In the uncommon case where the page being fetched has not been
          338  +** initialized, this routine is invoked to do the initialization.
          339  +** This routine is broken out into a separate function since it
          340  +** requires extra stack manipulation that can be avoided in the common
          341  +** case.
          342  +*/
          343  +static SQLITE_NOINLINE PgHdr *pcacheFetchFinishWithInit(
          344  +  PCache *pCache,             /* Obtain the page from this cache */
          345  +  Pgno pgno,                  /* Page number obtained */
          346  +  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
          347  +){
          348  +  PgHdr *pPgHdr;
          349  +  assert( pPage!=0 );
          350  +  pPgHdr = (PgHdr*)pPage->pExtra;
          351  +  assert( pPgHdr->pPage==0 );
          352  + memset(pPgHdr, 0, sizeof(PgHdr));
          353  +  pPgHdr->pPage = pPage;
          354  +  pPgHdr->pData = pPage->pBuf;
          355  +  pPgHdr->pExtra = (void *)&pPgHdr[1];
          356  +  memset(pPgHdr->pExtra, 0, pCache->szExtra);
          357  +  pPgHdr->pCache = pCache;
          358  +  pPgHdr->pgno = pgno;
          359  +  return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
          360  +}
          361  +
          362  +/*
          363  +** This routine converts the sqlite3_pcache_page object returned by
          364  +** sqlite3PcacheFetch() into an initialized PgHdr object.  This routine
          365  +** must be called after sqlite3PcacheFetch() in order to get a usable
          366  +** result.
          367  +*/
          368  +PgHdr *sqlite3PcacheFetchFinish(
          369  +  PCache *pCache,             /* Obtain the page from this cache */
          370  +  Pgno pgno,                  /* Page number obtained */
          371  +  sqlite3_pcache_page *pPage  /* Page obtained by prior PcacheFetch() call */
          372  +){
          373  +  PgHdr *pPgHdr;
          374  +
          375  +  if( pPage==0 ) return 0;
          376  +  pPgHdr = (PgHdr *)pPage->pExtra;
          377  +
          378  +  if( !pPgHdr->pPage ){
          379  +    return pcacheFetchFinishWithInit(pCache, pgno, pPage);
          380  +  }
          381  +  if( 0==pPgHdr->nRef ){
          382  +    pCache->nRef++;
          383  +  }
          384  +  pPgHdr->nRef++;
          385  +  if( pgno==1 ){
          386  +    pCache->pPage1 = pPgHdr;
          387  +  }
          388  +  return pPgHdr;
   330    389   }
   331    390   
   332    391   /*
   333    392   ** Decrement the reference count on a page. If the page is clean and the
   334    393   ** reference count drops to 0, then it is made elible for recycling.
   335    394   */
   336    395   void SQLITE_NOINLINE sqlite3PcacheRelease(PgHdr *p){
................................................................................
   467    526     }
   468    527   }
   469    528   
   470    529   /*
   471    530   ** Close a cache.
   472    531   */
   473    532   void sqlite3PcacheClose(PCache *pCache){
   474         -  if( pCache->pCache ){
   475         -    sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   476         -  }
          533  +  assert( pCache->pCache!=0 );
          534  +  sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
   477    535   }
   478    536   
   479    537   /* 
   480    538   ** Discard the contents of the cache.
   481    539   */
   482    540   void sqlite3PcacheClear(PCache *pCache){
   483    541     sqlite3PcacheTruncate(pCache, 0);
................................................................................
   578    636     return p->nRef;
   579    637   }
   580    638   
   581    639   /* 
   582    640   ** Return the total number of pages in the cache.
   583    641   */
   584    642   int sqlite3PcachePagecount(PCache *pCache){
   585         -  int nPage = 0;
   586         -  if( pCache->pCache ){
   587         -    nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
   588         -  }
   589         -  return nPage;
          643  +  assert( pCache->pCache!=0 );
          644  +  return sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
   590    645   }
   591    646   
   592    647   #ifdef SQLITE_TEST
   593    648   /*
   594    649   ** Get the suggested cache-size value.
   595    650   */
   596    651   int sqlite3PcacheGetCachesize(PCache *pCache){
................................................................................
   598    653   }
   599    654   #endif
   600    655   
   601    656   /*
   602    657   ** Set the suggested cache-size value.
   603    658   */
   604    659   void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
          660  +  assert( pCache->pCache!=0 );
   605    661     pCache->szCache = mxPage;
   606         -  if( pCache->pCache ){
   607         -    sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
   608         -                                           numberOfCachePages(pCache));
   609         -  }
          662  +  sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
          663  +                                         numberOfCachePages(pCache));
   610    664   }
   611    665   
   612    666   /*
   613    667   ** Free up as much memory as possible from the page cache.
   614    668   */
   615    669   void sqlite3PcacheShrink(PCache *pCache){
   616         -  if( pCache->pCache ){
   617         -    sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
   618         -  }
          670  +  assert( pCache->pCache!=0 );
          671  +  sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
   619    672   }
   620    673   
   621    674   #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
   622    675   /*
   623    676   ** For all dirty pages currently in the cache, invoke the specified
   624    677   ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
   625    678   ** defined.

Changes to src/pcache.h.

    64     64   */
    65     65   void sqlite3PCacheBufferSetup(void *, int sz, int n);
    66     66   
    67     67   /* Create a new pager cache.
    68     68   ** Under memory stress, invoke xStress to try to make pages clean.
    69     69   ** Only clean and unpinned pages can be reclaimed.
    70     70   */
    71         -void sqlite3PcacheOpen(
           71  +int sqlite3PcacheOpen(
    72     72     int szPage,                    /* Size of every page */
    73     73     int szExtra,                   /* Extra space associated with each page */
    74     74     int bPurgeable,                /* True if pages are on backing store */
    75     75     int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
    76     76     void *pStress,                 /* Argument to xStress */
    77     77     PCache *pToInit                /* Preallocated space for the PCache */
    78     78   );
    79     79   
    80     80   /* Modify the page-size after the cache has been created. */
    81         -void sqlite3PcacheSetPageSize(PCache *, int);
           81  +int sqlite3PcacheSetPageSize(PCache *, int);
    82     82   
    83     83   /* Return the size in bytes of a PCache object.  Used to preallocate
    84     84   ** storage space.
    85     85   */
    86     86   int sqlite3PcacheSize(void);
    87     87   
    88     88   /* One release per successful fetch.  Page is pinned until released.
    89     89   ** Reference counted. 
    90     90   */
    91         -int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
           91  +sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
           92  +int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
           93  +PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
    92     94   void sqlite3PcacheRelease(PgHdr*);
    93     95   
    94     96   void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
    95     97   void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
    96     98   void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
    97     99   void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
    98    100   

Changes to src/select.c.

   851    851       ** of the scan loop.
   852    852       */
   853    853       case SRT_Mem: {
   854    854         assert( nResultCol==1 );
   855    855         if( pSort ){
   856    856           pushOntoSorter(pParse, pSort, p, regResult, 1, nPrefixReg);
   857    857         }else{
   858         -        sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
          858  +        assert( regResult==iParm );
   859    859           /* The LIMIT clause will jump out of the loop for us */
   860    860         }
   861    861         break;
   862    862       }
   863    863   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
   864    864   
   865    865       case SRT_Coroutine:       /* Send data to a co-routine */

Changes to src/vdbe.c.

  1541   1541     sqlite3_value **apVal;
  1542   1542     int n;
  1543   1543   
  1544   1544     n = pOp->p5;
  1545   1545     apVal = p->apArg;
  1546   1546     assert( apVal || n==0 );
  1547   1547     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  1548         -  pOut = &aMem[pOp->p3];
  1549         -  memAboutToChange(p, pOut);
         1548  +  ctx.pOut = &aMem[pOp->p3];
         1549  +  memAboutToChange(p, ctx.pOut);
  1550   1550   
  1551   1551     assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
  1552   1552     assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
  1553   1553     pArg = &aMem[pOp->p2];
  1554   1554     for(i=0; i<n; i++, pArg++){
  1555   1555       assert( memIsValid(pArg) );
  1556   1556       apVal[i] = pArg;
................................................................................
  1558   1558       REGISTER_TRACE(pOp->p2+i, pArg);
  1559   1559     }
  1560   1560   
  1561   1561     assert( pOp->p4type==P4_FUNCDEF );
  1562   1562     ctx.pFunc = pOp->p4.pFunc;
  1563   1563     ctx.iOp = pc;
  1564   1564     ctx.pVdbe = p;
  1565         -
  1566         -  /* The output cell may already have a buffer allocated. Move
  1567         -  ** the pointer to ctx.s so in case the user-function can use
  1568         -  ** the already allocated buffer instead of allocating a new one.
  1569         -  */
  1570         -  memcpy(&ctx.s, pOut, sizeof(Mem));
  1571         -  pOut->flags = MEM_Null;
  1572         -  pOut->xDel = 0;
  1573         -  pOut->zMalloc = 0;
  1574         -  MemSetTypeFlag(&ctx.s, MEM_Null);
         1565  +  MemSetTypeFlag(ctx.pOut, MEM_Null);
  1575   1566   
  1576   1567     ctx.fErrorOrAux = 0;
  1577   1568     if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  1578   1569       assert( pOp>aOp );
  1579   1570       assert( pOp[-1].p4type==P4_COLLSEQ );
  1580   1571       assert( pOp[-1].opcode==OP_CollSeq );
  1581   1572       ctx.pColl = pOp[-1].p4.pColl;
  1582   1573     }
  1583   1574     db->lastRowid = lastRowid;
  1584   1575     (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  1585   1576     lastRowid = db->lastRowid;
  1586   1577   
  1587         -  if( db->mallocFailed ){
  1588         -    /* Even though a malloc() has failed, the implementation of the
  1589         -    ** user function may have called an sqlite3_result_XXX() function
  1590         -    ** to return a value. The following call releases any resources
  1591         -    ** associated with such a value.
  1592         -    */
  1593         -    sqlite3VdbeMemRelease(&ctx.s);
  1594         -    goto no_mem;
  1595         -  }
  1596         -
  1597   1578     /* If the function returned an error, throw an exception */
  1598   1579     if( ctx.fErrorOrAux ){
  1599   1580       if( ctx.isError ){
  1600         -      sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
         1581  +      sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(ctx.pOut));
  1601   1582         rc = ctx.isError;
  1602   1583       }
  1603   1584       sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
  1604   1585     }
  1605   1586   
  1606   1587     /* Copy the result of the function into register P3 */
  1607         -  sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  1608         -  assert( pOut->flags==MEM_Null );
  1609         -  memcpy(pOut, &ctx.s, sizeof(Mem));
  1610         -  if( sqlite3VdbeMemTooBig(pOut) ){
         1588  +  sqlite3VdbeChangeEncoding(ctx.pOut, encoding);
         1589  +  if( sqlite3VdbeMemTooBig(ctx.pOut) ){
  1611   1590       goto too_big;
  1612   1591     }
  1613   1592   
  1614         -#if 0
  1615         -  /* The app-defined function has done something that as caused this
  1616         -  ** statement to expire.  (Perhaps the function called sqlite3_exec()
  1617         -  ** with a CREATE TABLE statement.)
  1618         -  */
  1619         -  if( p->expired ) rc = SQLITE_ABORT;
  1620         -#endif
  1621         -
  1622         -  REGISTER_TRACE(pOp->p3, pOut);
  1623         -  UPDATE_MAX_BLOBSIZE(pOut);
         1593  +  REGISTER_TRACE(pOp->p3, ctx.pOut);
         1594  +  UPDATE_MAX_BLOBSIZE(ctx.pOut);
  1624   1595     break;
  1625   1596   }
  1626   1597   
  1627   1598   /* Opcode: BitAnd P1 P2 P3 * *
  1628   1599   ** Synopsis:  r[P3]=r[P1]&r[P2]
  1629   1600   **
  1630   1601   ** Take the bit-wise AND of the values in register P1 and P2 and
................................................................................
  1765   1736     }
  1766   1737     break;
  1767   1738   }
  1768   1739   #endif
  1769   1740   
  1770   1741   #ifndef SQLITE_OMIT_CAST
  1771   1742   /* Opcode: Cast P1 P2 * * *
         1743  +** Synopsis: affinity(r[P1])
  1772   1744   **
  1773   1745   ** Force the value in register P1 to be the type defined by P2.
  1774   1746   ** 
  1775   1747   ** <ul>
  1776   1748   ** <li value="97"> TEXT
  1777   1749   ** <li value="98"> BLOB
  1778   1750   ** <li value="99"> NUMERIC
................................................................................
  5629   5601   ** successors.
  5630   5602   */
  5631   5603   case OP_AggStep: {
  5632   5604     int n;
  5633   5605     int i;
  5634   5606     Mem *pMem;
  5635   5607     Mem *pRec;
         5608  +  Mem t;
  5636   5609     sqlite3_context ctx;
  5637   5610     sqlite3_value **apVal;
  5638   5611   
  5639   5612     n = pOp->p5;
  5640   5613     assert( n>=0 );
  5641   5614     pRec = &aMem[pOp->p2];
  5642   5615     apVal = p->apArg;
................................................................................
  5646   5619       apVal[i] = pRec;
  5647   5620       memAboutToChange(p, pRec);
  5648   5621     }
  5649   5622     ctx.pFunc = pOp->p4.pFunc;
  5650   5623     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  5651   5624     ctx.pMem = pMem = &aMem[pOp->p3];
  5652   5625     pMem->n++;
  5653         -  ctx.s.flags = MEM_Null;
  5654         -  ctx.s.z = 0;
  5655         -  ctx.s.zMalloc = 0;
  5656         -  ctx.s.xDel = 0;
  5657         -  ctx.s.db = db;
         5626  +  t.flags = MEM_Null;
         5627  +  t.z = 0;
         5628  +  t.zMalloc = 0;
         5629  +  t.xDel = 0;
         5630  +  t.db = db;
         5631  +  ctx.pOut = &t;
  5658   5632     ctx.isError = 0;
  5659   5633     ctx.pColl = 0;
  5660   5634     ctx.skipFlag = 0;
  5661   5635     if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5662   5636       assert( pOp>p->aOp );
  5663   5637       assert( pOp[-1].p4type==P4_COLLSEQ );
  5664   5638       assert( pOp[-1].opcode==OP_CollSeq );
  5665   5639       ctx.pColl = pOp[-1].p4.pColl;
  5666   5640     }
  5667   5641     (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  5668   5642     if( ctx.isError ){
  5669         -    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
         5643  +    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&t));
  5670   5644       rc = ctx.isError;
  5671   5645     }
  5672   5646     if( ctx.skipFlag ){
  5673   5647       assert( pOp[-1].opcode==OP_CollSeq );
  5674   5648       i = pOp[-1].p1;
  5675   5649       if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
  5676   5650     }
  5677         -
  5678         -  sqlite3VdbeMemRelease(&ctx.s);
  5679         -
         5651  +  sqlite3VdbeMemRelease(&t);
  5680   5652     break;
  5681   5653   }
  5682   5654   
  5683   5655   /* Opcode: AggFinal P1 P2 * P4 *
  5684   5656   ** Synopsis: accum=r[P1] N=P2
  5685   5657   **
  5686   5658   ** Execute the finalizer function for an aggregate.  P1 is
................................................................................
  6122   6094       sqlite3VdbeMemSetNull(pDest);
  6123   6095       break;
  6124   6096     }
  6125   6097     pVtab = pCur->pVtabCursor->pVtab;
  6126   6098     pModule = pVtab->pModule;
  6127   6099     assert( pModule->xColumn );
  6128   6100     memset(&sContext, 0, sizeof(sContext));
  6129         -
  6130         -  /* The output cell may already have a buffer allocated. Move
  6131         -  ** the current contents to sContext.s so in case the user-function 
  6132         -  ** can use the already allocated buffer instead of allocating a 
  6133         -  ** new one.
  6134         -  */
  6135         -  sqlite3VdbeMemMove(&sContext.s, pDest);
  6136         -  MemSetTypeFlag(&sContext.s, MEM_Null);
  6137         -
         6101  +  sContext.pOut = pDest;
         6102  +  MemSetTypeFlag(pDest, MEM_Null);
  6138   6103     rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
  6139   6104     sqlite3VtabImportErrmsg(p, pVtab);
  6140   6105     if( sContext.isError ){
  6141   6106       rc = sContext.isError;
  6142   6107     }
  6143         -
  6144         -  /* Copy the result of the function to the P3 register. We
  6145         -  ** do this regardless of whether or not an error occurred to ensure any
  6146         -  ** dynamic allocation in sContext.s (a Mem struct) is  released.
  6147         -  */
  6148         -  sqlite3VdbeChangeEncoding(&sContext.s, encoding);
  6149         -  sqlite3VdbeMemMove(pDest, &sContext.s);
         6108  +  sqlite3VdbeChangeEncoding(pDest, encoding);
  6150   6109     REGISTER_TRACE(pOp->p3, pDest);
  6151   6110     UPDATE_MAX_BLOBSIZE(pDest);
  6152   6111   
  6153   6112     if( sqlite3VdbeMemTooBig(pDest) ){
  6154   6113       goto too_big;
  6155   6114     }
  6156   6115     break;

Changes to src/vdbeInt.h.

   262    262   ** But this file is the only place where the internal details of this
   263    263   ** structure are known.
   264    264   **
   265    265   ** This structure is defined inside of vdbeInt.h because it uses substructures
   266    266   ** (Mem) which are only defined there.
   267    267   */
   268    268   struct sqlite3_context {
          269  +  Mem *pOut;            /* The return value is stored here */
   269    270     FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
   270         -  Mem s;                /* The return value is stored here */
   271    271     Mem *pMem;            /* Memory cell used to store aggregate context */
   272    272     CollSeq *pColl;       /* Collating sequence */
   273    273     Vdbe *pVdbe;          /* The VM that owns this context */
   274    274     int iOp;              /* Instruction number of OP_Function */
   275    275     int isError;          /* Error code returned by the function. */
   276    276     u8 skipFlag;          /* Skip skip accumulator loading if true */
   277    277     u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */

Changes to src/vdbeapi.c.

   219    219   static void setResultStrOrError(
   220    220     sqlite3_context *pCtx,  /* Function context */
   221    221     const char *z,          /* String pointer */
   222    222     int n,                  /* Bytes in string, or negative */
   223    223     u8 enc,                 /* Encoding of z.  0 for BLOBs */
   224    224     void (*xDel)(void*)     /* Destructor function */
   225    225   ){
   226         -  if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
          226  +  if( sqlite3VdbeMemSetStr(pCtx->pOut, z, n, enc, xDel)==SQLITE_TOOBIG ){
   227    227       sqlite3_result_error_toobig(pCtx);
   228    228     }
   229    229   }
   230    230   void sqlite3_result_blob(
   231    231     sqlite3_context *pCtx, 
   232    232     const void *z, 
   233    233     int n, 
   234    234     void (*xDel)(void *)
   235    235   ){
   236    236     assert( n>=0 );
   237         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          237  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   238    238     setResultStrOrError(pCtx, z, n, 0, xDel);
   239    239   }
   240    240   void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
   241         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   242         -  sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
          241  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          242  +  sqlite3VdbeMemSetDouble(pCtx->pOut, rVal);
   243    243   }
   244    244   void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
   245         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          245  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   246    246     pCtx->isError = SQLITE_ERROR;
   247    247     pCtx->fErrorOrAux = 1;
   248         -  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
          248  +  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
   249    249   }
   250    250   #ifndef SQLITE_OMIT_UTF16
   251    251   void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
   252         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          252  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   253    253     pCtx->isError = SQLITE_ERROR;
   254    254     pCtx->fErrorOrAux = 1;
   255         -  sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
          255  +  sqlite3VdbeMemSetStr(pCtx->pOut, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
   256    256   }
   257    257   #endif
   258    258   void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
   259         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   260         -  sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
          259  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          260  +  sqlite3VdbeMemSetInt64(pCtx->pOut, (i64)iVal);
   261    261   }
   262    262   void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
   263         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   264         -  sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
          263  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          264  +  sqlite3VdbeMemSetInt64(pCtx->pOut, iVal);
   265    265   }
   266    266   void sqlite3_result_null(sqlite3_context *pCtx){
   267         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   268         -  sqlite3VdbeMemSetNull(&pCtx->s);
          267  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          268  +  sqlite3VdbeMemSetNull(pCtx->pOut);
   269    269   }
   270    270   void sqlite3_result_text(
   271    271     sqlite3_context *pCtx, 
   272    272     const char *z, 
   273    273     int n,
   274    274     void (*xDel)(void *)
   275    275   ){
   276         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          276  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   277    277     setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
   278    278   }
   279    279   #ifndef SQLITE_OMIT_UTF16
   280    280   void sqlite3_result_text16(
   281    281     sqlite3_context *pCtx, 
   282    282     const void *z, 
   283    283     int n, 
   284    284     void (*xDel)(void *)
   285    285   ){
   286         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          286  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   287    287     setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
   288    288   }
   289    289   void sqlite3_result_text16be(
   290    290     sqlite3_context *pCtx, 
   291    291     const void *z, 
   292    292     int n, 
   293    293     void (*xDel)(void *)
   294    294   ){
   295         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          295  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   296    296     setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
   297    297   }
   298    298   void sqlite3_result_text16le(
   299    299     sqlite3_context *pCtx, 
   300    300     const void *z, 
   301    301     int n, 
   302    302     void (*xDel)(void *)
   303    303   ){
   304         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          304  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   305    305     setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
   306    306   }
   307    307   #endif /* SQLITE_OMIT_UTF16 */
   308    308   void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
   309         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   310         -  sqlite3VdbeMemCopy(&pCtx->s, pValue);
          309  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          310  +  sqlite3VdbeMemCopy(pCtx->pOut, pValue);
   311    311   }
   312    312   void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
   313         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   314         -  sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
          313  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          314  +  sqlite3VdbeMemSetZeroBlob(pCtx->pOut, n);
   315    315   }
   316    316   void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
   317    317     pCtx->isError = errCode;
   318    318     pCtx->fErrorOrAux = 1;
   319         -  if( pCtx->s.flags & MEM_Null ){
   320         -    sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1, 
          319  +  if( pCtx->pOut->flags & MEM_Null ){
          320  +    sqlite3VdbeMemSetStr(pCtx->pOut, sqlite3ErrStr(errCode), -1, 
   321    321                            SQLITE_UTF8, SQLITE_STATIC);
   322    322     }
   323    323   }
   324    324   
   325    325   /* Force an SQLITE_TOOBIG error. */
   326    326   void sqlite3_result_error_toobig(sqlite3_context *pCtx){
   327         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          327  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   328    328     pCtx->isError = SQLITE_TOOBIG;
   329    329     pCtx->fErrorOrAux = 1;
   330         -  sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1, 
          330  +  sqlite3VdbeMemSetStr(pCtx->pOut, "string or blob too big", -1, 
   331    331                          SQLITE_UTF8, SQLITE_STATIC);
   332    332   }
   333    333   
   334    334   /* An SQLITE_NOMEM error. */
   335    335   void sqlite3_result_error_nomem(sqlite3_context *pCtx){
   336         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   337         -  sqlite3VdbeMemSetNull(&pCtx->s);
          336  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          337  +  sqlite3VdbeMemSetNull(pCtx->pOut);
   338    338     pCtx->isError = SQLITE_NOMEM;
   339    339     pCtx->fErrorOrAux = 1;
   340         -  pCtx->s.db->mallocFailed = 1;
          340  +  pCtx->pOut->db->mallocFailed = 1;
   341    341   }
   342    342   
   343    343   /*
   344    344   ** This function is called after a transaction has been committed. It 
   345    345   ** invokes callbacks registered with sqlite3_wal_hook() as required.
   346    346   */
   347    347   static int doWalCallbacks(sqlite3 *db){
................................................................................
   564    564   ** returns a copy of the pointer to the database connection (the 1st
   565    565   ** parameter) of the sqlite3_create_function() and
   566    566   ** sqlite3_create_function16() routines that originally registered the
   567    567   ** application defined function.
   568    568   */
   569    569   sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
   570    570     assert( p && p->pFunc );
   571         -  return p->s.db;
          571  +  return p->pOut->db;
   572    572   }
   573    573   
   574    574   /*
   575    575   ** Return the current time for a statement
   576    576   */
   577    577   sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
   578    578     Vdbe *v = p->pVdbe;
   579    579     int rc;
   580    580     if( v->iCurrentTime==0 ){
   581         -    rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime);
          581  +    rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, &v->iCurrentTime);
   582    582       if( rc ) v->iCurrentTime = 0;
   583    583     }
   584    584     return v->iCurrentTime;
   585    585   }
   586    586   
   587    587   /*
   588    588   ** The following is the implementation of an SQL function that always
................................................................................
   631    631   /*
   632    632   ** Allocate or return the aggregate context for a user function.  A new
   633    633   ** context is allocated on the first call.  Subsequent calls return the
   634    634   ** same context that was returned on prior calls.
   635    635   */
   636    636   void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
   637    637     assert( p && p->pFunc && p->pFunc->xStep );
   638         -  assert( sqlite3_mutex_held(p->s.db->mutex) );
          638  +  assert( sqlite3_mutex_held(p->pOut->db->mutex) );
   639    639     testcase( nByte<0 );
   640    640     if( (p->pMem->flags & MEM_Agg)==0 ){
   641    641       return createAggContext(p, nByte);
   642    642     }else{
   643    643       return (void*)p->pMem->z;
   644    644     }
   645    645   }
................................................................................
   647    647   /*
   648    648   ** Return the auxilary data pointer, if any, for the iArg'th argument to
   649    649   ** the user-function defined by pCtx.
   650    650   */
   651    651   void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   652    652     AuxData *pAuxData;
   653    653   
   654         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          654  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   655    655     for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
   656    656       if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
   657    657     }
   658    658   
   659    659     return (pAuxData ? pAuxData->pAux : 0);
   660    660   }
   661    661   
................................................................................
   669    669     int iArg, 
   670    670     void *pAux, 
   671    671     void (*xDelete)(void*)
   672    672   ){
   673    673     AuxData *pAuxData;
   674    674     Vdbe *pVdbe = pCtx->pVdbe;
   675    675   
   676         -  assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
          676  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   677    677     if( iArg<0 ) goto failed;
   678    678   
   679    679     for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
   680    680       if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
   681    681     }
   682    682     if( pAuxData==0 ){
   683    683       pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));

Changes to src/vdbemem.c.

   196    196       pMem->n += pMem->u.nZero;
   197    197       pMem->flags &= ~(MEM_Zero|MEM_Term);
   198    198     }
   199    199     return SQLITE_OK;
   200    200   }
   201    201   #endif
   202    202   
   203         -
   204    203   /*
   205         -** Make sure the given Mem is \u0000 terminated.
          204  +** It is already known that pMem contains an unterminated string.
          205  +** Add the zero terminator.
   206    206   */
   207         -int sqlite3VdbeMemNulTerminate(Mem *pMem){
   208         -  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   209         -  if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
   210         -    return SQLITE_OK;   /* Nothing to do */
   211         -  }
          207  +static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
   212    208     if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
   213    209       return SQLITE_NOMEM;
   214    210     }
   215    211     pMem->z[pMem->n] = 0;
   216    212     pMem->z[pMem->n+1] = 0;
   217    213     pMem->flags |= MEM_Term;
   218    214     return SQLITE_OK;
   219    215   }
          216  +
          217  +/*
          218  +** Make sure the given Mem is \u0000 terminated.
          219  +*/
          220  +int sqlite3VdbeMemNulTerminate(Mem *pMem){
          221  +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
          222  +  testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
          223  +  testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
          224  +  if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
          225  +    return SQLITE_OK;   /* Nothing to do */
          226  +  }else{
          227  +    return vdbeMemAddTerminator(pMem);
          228  +  }
          229  +}
   220    230   
   221    231   /*
   222    232   ** Add MEM_Str to the set of representations for the given Mem.  Numbers
   223    233   ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
   224    234   ** is a no-op.
   225    235   **
   226    236   ** Existing representations MEM_Int and MEM_Real are invalidated if
................................................................................
   276    286   ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
   277    287   ** otherwise.
   278    288   */
   279    289   int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
   280    290     int rc = SQLITE_OK;
   281    291     if( ALWAYS(pFunc && pFunc->xFinalize) ){
   282    292       sqlite3_context ctx;
          293  +    Mem t;
   283    294       assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
   284    295       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   285    296       memset(&ctx, 0, sizeof(ctx));
   286         -    ctx.s.flags = MEM_Null;
   287         -    ctx.s.db = pMem->db;
          297  +    memset(&t, 0, sizeof(t));
          298  +    t.flags = MEM_Null;
          299  +    t.db = pMem->db;
          300  +    ctx.pOut = &t;
   288    301       ctx.pMem = pMem;
   289    302       ctx.pFunc = pFunc;
   290    303       pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
   291    304       assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
   292    305       sqlite3DbFree(pMem->db, pMem->zMalloc);
   293         -    memcpy(pMem, &ctx.s, sizeof(ctx.s));
          306  +    memcpy(pMem, &t, sizeof(t));
   294    307       rc = ctx.isError;
   295    308     }
   296    309     return rc;
   297    310   }
   298    311   
   299    312   /*
   300    313   ** If the memory cell contains a string value that must be freed by
................................................................................
   600    613     sqlite3VdbeMemGrow(pMem, n, 0);
   601    614     if( pMem->z ){
   602    615       pMem->n = n;
   603    616       memset(pMem->z, 0, n);
   604    617     }
   605    618   #endif
   606    619   }
          620  +
          621  +/*
          622  +** The pMem is known to contain content that needs to be destroyed prior
          623  +** to a value change.  So invoke the destructor, then set the value to
          624  +** a 64-bit integer.
          625  +*/
          626  +static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
          627  +  sqlite3VdbeMemReleaseExternal(pMem);
          628  +  pMem->u.i = val;
          629  +  pMem->flags = MEM_Int;
          630  +}
   607    631   
   608    632   /*
   609    633   ** Delete any previous value and set the value stored in *pMem to val,
   610    634   ** manifest type INTEGER.
   611    635   */
   612    636   void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
   613         -  sqlite3VdbeMemRelease(pMem);
   614         -  pMem->u.i = val;
   615         -  pMem->flags = MEM_Int;
          637  +  if( VdbeMemDynamic(pMem) ){
          638  +    vdbeReleaseAndSetInt64(pMem, val);
          639  +  }else{
          640  +    pMem->u.i = val;
          641  +    pMem->flags = MEM_Int;
          642  +  }
   616    643   }
   617    644   
   618    645   #ifndef SQLITE_OMIT_FLOATING_POINT
   619    646   /*
   620    647   ** Delete any previous value and set the value stored in *pMem to val,
   621    648   ** manifest type REAL.
   622    649   */
................................................................................
   904    931       }else{
   905    932         sqlite3VdbeMemRelease(pMem);
   906    933       }
   907    934     }
   908    935   
   909    936     return rc;
   910    937   }
          938  +
          939  +/*
          940  +** The pVal argument is known to be a value other than NULL.
          941  +** Convert it into a string with encoding enc and return a pointer
          942  +** to a zero-terminated version of that string.
          943  +*/
          944  +SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
          945  +  assert( pVal!=0 );
          946  +  assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
          947  +  assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
          948  +  assert( (pVal->flags & MEM_RowSet)==0 );
          949  +  assert( (pVal->flags & (MEM_Null))==0 );
          950  +  if( pVal->flags & (MEM_Blob|MEM_Str) ){
          951  +    pVal->flags |= MEM_Str;
          952  +    if( pVal->flags & MEM_Zero ){
          953  +      sqlite3VdbeMemExpandBlob(pVal);
          954  +    }
          955  +    if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
          956  +      sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
          957  +    }
          958  +    if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
          959  +      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
          960  +      if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
          961  +        return 0;
          962  +      }
          963  +    }
          964  +    sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
          965  +  }else{
          966  +    sqlite3VdbeMemStringify(pVal, enc, 0);
          967  +    assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
          968  +  }
          969  +  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
          970  +              || pVal->db->mallocFailed );
          971  +  if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
          972  +    return pVal->z;
          973  +  }else{
          974  +    return 0;
          975  +  }
          976  +}
   911    977   
   912    978   /* This function is only available internally, it is not part of the
   913    979   ** external API. It works in a similar way to sqlite3_value_text(),
   914    980   ** except the data returned is in the encoding specified by the second
   915    981   ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
   916    982   ** SQLITE_UTF8.
   917    983   **
   918    984   ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
   919    985   ** If that is the case, then the result must be aligned on an even byte
   920    986   ** boundary.
   921    987   */
   922    988   const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
   923    989     if( !pVal ) return 0;
   924         -
   925    990     assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
   926    991     assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
   927    992     assert( (pVal->flags & MEM_RowSet)==0 );
   928         -
          993  +  if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
          994  +    return pVal->z;
          995  +  }
   929    996     if( pVal->flags&MEM_Null ){
   930    997       return 0;
   931    998     }
   932         -  assert( (MEM_Blob>>3) == MEM_Str );
   933         -  pVal->flags |= (pVal->flags & MEM_Blob)>>3;
   934         -  ExpandBlob(pVal);
   935         -  if( pVal->flags&MEM_Str ){
   936         -    sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
   937         -    if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
   938         -      assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
   939         -      if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
   940         -        return 0;
   941         -      }
   942         -    }
   943         -    sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
   944         -  }else{
   945         -    assert( (pVal->flags&MEM_Blob)==0 );
   946         -    sqlite3VdbeMemStringify(pVal, enc, 0);
   947         -    assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
   948         -  }
   949         -  assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
   950         -              || pVal->db->mallocFailed );
   951         -  if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
   952         -    return pVal->z;
   953         -  }else{
   954         -    return 0;
   955         -  }
          999  +  return valueToText(pVal, enc);
   956   1000   }
   957   1001   
   958   1002   /*
   959   1003   ** Create a new sqlite3_value object.
   960   1004   */
   961   1005   sqlite3_value *sqlite3ValueNew(sqlite3 *db){
   962   1006     Mem *p = sqlite3DbMallocZero(db, sizeof(*p));

Changes to src/where.c.

  2187   2187         ** less than the upper bound of the range query. Where the upper bound
  2188   2188         ** is either ($P) or ($P:$U). Again, even if $U is available, both values
  2189   2189         ** of iUpper are requested of whereKeyStats() and the smaller used.
  2190   2190         */
  2191   2191         tRowcnt iLower;
  2192   2192         tRowcnt iUpper;
  2193   2193   
         2194  +      if( pRec ){
         2195  +        testcase( pRec->nField!=pBuilder->nRecValid );
         2196  +        pRec->nField = pBuilder->nRecValid;
         2197  +      }
  2194   2198         if( nEq==p->nKeyCol ){
  2195   2199           aff = SQLITE_AFF_INTEGER;
  2196   2200         }else{
  2197   2201           aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
  2198   2202         }
  2199   2203         /* Determine iLower and iUpper using ($P) only. */
  2200   2204         if( nEq==0 ){
................................................................................
  2246   2250             nNew = sqlite3LogEst(iUpper - iLower);
  2247   2251           }else{
  2248   2252             nNew = 10;        assert( 10==sqlite3LogEst(2) );
  2249   2253           }
  2250   2254           if( nNew<nOut ){
  2251   2255             nOut = nNew;
  2252   2256           }
  2253         -        WHERETRACE(0x10, ("range scan regions: %u..%u  est=%d\n",
         2257  +        WHERETRACE(0x10, ("STAT4 range scan: %u..%u  est=%d\n",
  2254   2258                              (u32)iLower, (u32)iUpper, nOut));
  2255   2259         }
  2256   2260       }else{
  2257   2261         int bDone = 0;
  2258   2262         rc = whereRangeSkipScanEst(pParse, pLower, pUpper, pLoop, &bDone);
  2259   2263         if( bDone ) return rc;
  2260   2264       }
................................................................................
  2274   2278     ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
  2275   2279     ** match 1/64 of the index. */ 
  2276   2280     if( pLower && pUpper ) nNew -= 20;
  2277   2281   
  2278   2282     nOut -= (pLower!=0) + (pUpper!=0);
  2279   2283     if( nNew<10 ) nNew = 10;
  2280   2284     if( nNew<nOut ) nOut = nNew;
         2285  +#if defined(WHERETRACE_ENABLED)
         2286  +  if( pLoop->nOut>nOut ){
         2287  +    WHERETRACE(0x10,("Range scan lowers nOut from %d to %d\n",
         2288  +                    pLoop->nOut, nOut));
         2289  +  }
         2290  +#endif
  2281   2291     pLoop->nOut = (LogEst)nOut;
  2282   2292     return rc;
  2283   2293   }
  2284   2294   
  2285   2295   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2286   2296   /*
  2287   2297   ** Estimate the number of rows that will be returned based on
................................................................................
  2386   2396       nRowEst += nEst;
  2387   2397       pBuilder->nRecValid = nRecValid;
  2388   2398     }
  2389   2399   
  2390   2400     if( rc==SQLITE_OK ){
  2391   2401       if( nRowEst > nRow0 ) nRowEst = nRow0;
  2392   2402       *pnRow = nRowEst;
  2393         -    WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
         2403  +    WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
  2394   2404     }
  2395   2405     assert( pBuilder->nRecValid==nRecValid );
  2396   2406     return rc;
  2397   2407   }
  2398   2408   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  2399   2409   
  2400   2410   /*
................................................................................
  4709   4719     }
  4710   4720   #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
  4711   4721   
  4712   4722     /* Loop over all indices
  4713   4723     */
  4714   4724     for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
  4715   4725       if( pProbe->pPartIdxWhere!=0
  4716         -     && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
         4726  +     && !whereUsablePartialIndex(pSrc->iCursor, pWC, pProbe->pPartIdxWhere) ){
         4727  +      testcase( pNew->iTab!=pSrc->iCursor );  /* See ticket [98d973b8f5] */
  4717   4728         continue;  /* Partial index inappropriate for this query */
  4718   4729       }
  4719   4730       rSize = pProbe->aiRowLogEst[0];
  4720   4731       pNew->u.btree.nEq = 0;
  4721   4732       pNew->u.btree.nSkip = 0;
  4722   4733       pNew->nLTerm = 0;
  4723   4734       pNew->iSortIdx = 0;

Changes to test/index7.test.

   243    243   do_execsql_test index7-5.0 {
   244    244     CREATE INDEX t3b ON t3(b) WHERE xyzzy.t3.b BETWEEN 5 AND 10;
   245    245                                  /* ^^^^^-- ignored */
   246    246     ANALYZE;
   247    247     SELECT count(*) FROM t3 WHERE t3.b BETWEEN 5 AND 10;
   248    248     SELECT stat+0 FROM sqlite_stat1 WHERE idx='t3b';
   249    249   } {6 6}
          250  +
          251  +# Verify that the problem identified by ticket [98d973b8f5] has been fixed.
          252  +#
          253  +do_execsql_test index7-6.1 {
          254  +  CREATE TABLE t5(a, b);
          255  +  CREATE TABLE t4(c, d);
          256  +  INSERT INTO t5 VALUES(1, 'xyz');
          257  +  INSERT INTO t4 VALUES('abc', 'not xyz');
          258  +  SELECT * FROM (SELECT * FROM t5 WHERE a=1 AND b='xyz'), t4 WHERE c='abc';
          259  +} {
          260  +  1 xyz abc {not xyz}
          261  +}
          262  +do_execsql_test index7-6.2 {
          263  +  CREATE INDEX i4 ON t4(c) WHERE d='xyz';
          264  +  SELECT * FROM (SELECT * FROM t5 WHERE a=1 AND b='xyz'), t4 WHERE c='abc';
          265  +} {
          266  +  1 xyz abc {not xyz}
          267  +}
          268  +do_execsql_test index7-6.3 {
          269  +  CREATE VIEW v4 AS SELECT * FROM t4;
          270  +  INSERT INTO t4 VALUES('def', 'xyz');
          271  +  SELECT * FROM v4 WHERE d='xyz' AND c='def'
          272  +} {
          273  +  def xyz
          274  +}
          275  +do_eqp_test index7-6.4 {
          276  +  SELECT * FROM v4 WHERE d='xyz' AND c='def'
          277  +} {
          278  +  0 0 0 {SEARCH TABLE t4 USING INDEX i4 (c=?)}
          279  +}
   250    280   
   251    281   finish_test

Changes to test/whereJ.test.

   366    366        AND t0b.id=2
   367    367        AND t1b.id BETWEEN t0b.minChild AND t0b.maxChild
   368    368        AND t2b.id BETWEEN t1b.minChild AND t1b.maxChild
   369    369        AND t3b.id BETWEEN t2b.minChild AND t2b.maxChild
   370    370        AND t4.id BETWEEN t3a.minChild AND t3b.maxChild
   371    371     ORDER BY t4.x;
   372    372   } {~/SCAN/}
          373  +
          374  +############################################################################
          375  +
          376  +ifcapable stat4 {
          377  +  # Create and populate table.
          378  +  do_execsql_test 3.1 { CREATE TABLE t1(a, b, c) }
          379  +  for {set i 0} {$i < 32} {incr i 2} {
          380  +    for {set x 0} {$x < 100} {incr x} {
          381  +      execsql { INSERT INTO t1 VALUES($i, $x, $c) }
          382  +      incr c
          383  +    }
          384  +    execsql { INSERT INTO t1 VALUES($i+1, 5, $c) }
          385  +    incr c
          386  +  }
          387  +  
          388  +  do_execsql_test 3.2 {
          389  +    SELECT a, count(*) FROM t1 GROUP BY a HAVING a < 8;
          390  +  } {
          391  +    0 100 1 1 2 100 3 1 4 100 5 1 6 100 7 1
          392  +  }
          393  +  
          394  +  do_execsql_test 3.3 {
          395  +    CREATE INDEX idx_ab ON t1(a, b);
          396  +    CREATE INDEX idx_c ON t1(c);
          397  +    ANALYZE;
          398  +  } {}
          399  +  
          400  +  # This one should use index "idx_c".
          401  +  do_eqp_test 3.4 {
          402  +    SELECT * FROM t1 WHERE 
          403  +      a = 4 AND b BETWEEN 20 AND 80           -- Matches 80 rows
          404  +        AND
          405  +      c BETWEEN 150 AND 160                   -- Matches 10 rows
          406  +  } {
          407  +    0 0 0 {SEARCH TABLE t1 USING INDEX idx_c (c>? AND c<?)}
          408  +  }
          409  +  
          410  +  # This one should use index "idx_ab".
          411  +  do_eqp_test 3.5 {
          412  +    SELECT * FROM t1 WHERE 
          413  +      a = 5 AND b BETWEEN 20 AND 80           -- Matches 1 row
          414  +        AND
          415  +      c BETWEEN 150 AND 160                   -- Matches 10 rows
          416  +  } {
          417  +    0 0 0 {SEARCH TABLE t1 USING INDEX idx_ab (a=? AND b>? AND b<?)}
          418  +  }
          419  +}
   373    420   
   374    421   
   375    422   finish_test