/ Check-in [11ca4ed8]
Login

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

Overview
Comment:Merge the latest trunk changes into the stat3-trunk branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | stat3-trunk
Files: files | file ages | folders
SHA1: 11ca4ed8bf850dae1a24b7182f70039f32bd8dd1
User & Date: drh 2011-09-13 19:09:28
Context
2011-09-16
19:29
Merge all the latest trunk changes into the experimental STAT3 branch. check-in: 51908c8f user: drh tags: stat3-trunk
2011-09-13
19:09
Merge the latest trunk changes into the stat3-trunk branch. check-in: 11ca4ed8 user: drh tags: stat3-trunk
2011-09-11
10:14
Cleanup pdb/ilk files generated by the MSVC makefile. check-in: a9db247b user: mistachkin tags: trunk
2011-08-26
13:16
Merge the stat3-enhancement branch with trunk, but keep the resulting merge in a separate branch for now. check-in: 63f2c785 user: drh tags: stat3-trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   771    771   tclsqlite3$(TEXE):	tclsqlite-shell.lo libsqlite3.la
   772    772   	$(LTLINK) -o $@ tclsqlite-shell.lo \
   773    773   		 libsqlite3.la $(LIBTCL)
   774    774   
   775    775   # Rules to build opcodes.c and opcodes.h
   776    776   #
   777    777   opcodes.c:	opcodes.h $(TOP)/mkopcodec.awk
   778         -	sort -n -b -k 3 opcodes.h | $(NAWK) -f $(TOP)/mkopcodec.awk >opcodes.c
          778  +	$(NAWK) -f $(TOP)/mkopcodec.awk opcodes.h >opcodes.c
   779    779   
   780    780   opcodes.h:	parse.h $(TOP)/src/vdbe.c $(TOP)/mkopcodeh.awk
   781    781   	cat parse.h $(TOP)/src/vdbe.c | $(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   782    782   
   783    783   # Rules to build parse.c and parse.h - the outputs of lemon.
   784    784   #
   785    785   parse.h:	parse.c

Changes to Makefile.msc.

   805    805   tclsqlite3.exe:	tclsqlite-shell.lo libsqlite3.lib
   806    806   	$(LTLINK) tclsqlite-shell.lo \
   807    807   		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) libsqlite3.lib $(LIBTCL)
   808    808   
   809    809   # Rules to build opcodes.c and opcodes.h
   810    810   #
   811    811   opcodes.c:	opcodes.h $(TOP)\mkopcodec.awk
   812         -	$(NAWK) "/#define OP_/ { print }" opcodes.h | sort /+45 | $(NAWK) -f $(TOP)\mkopcodec.awk > opcodes.c
          812  +	$(NAWK) -f $(TOP)\mkopcodec.awk opcodes.h > opcodes.c
   813    813   
   814    814   opcodes.h:	parse.h $(TOP)\src\vdbe.c $(TOP)\mkopcodeh.awk
   815    815   	type parse.h $(TOP)\src\vdbe.c | $(NAWK) -f $(TOP)\mkopcodeh.awk > opcodes.h
   816    816   
   817    817   # Rules to build parse.c and parse.h - the outputs of lemon.
   818    818   #
   819    819   parse.h:	parse.c
................................................................................
   934    934   	$(LTLINK) -DTCLSH=2 -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1 \
   935    935   		-DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE \
   936    936   		-DBUILD_sqlite -I$(TCLINCDIR) \
   937    937   		$(TESTFIXTURE_SRC) \
   938    938   		/link $(LTLINKOPTS) /LIBPATH:$(TCLLIBDIR) $(LIBTCL) $(TLIBS)
   939    939   
   940    940   clean:
   941         -	del /Q *.lo *.lib *.obj sqlite3.exe libsqlite3.lib
          941  +	del /Q *.lo *.ilk *.lib *.obj *.pdb sqlite3.exe libsqlite3.lib
   942    942   	del /Q sqlite3.h opcodes.c opcodes.h
   943    943   	del /Q lemon.exe lempar.c parse.*
   944    944   	del /Q mkkeywordhash.exe keywordhash.h
   945    945   	-rmdir /Q/S tsrc
   946    946   	del /Q .target_source
   947    947   	del /Q testfixture.exe testfixture.exp test.db
   948    948   	del /Q sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def

Changes to Makefile.vxworks.

   513    513   	$(TCCX_SHARED) $(TCL_FLAGS) -c $(TOP)/src/tclsqlite.c
   514    514   
   515    515   
   516    516   
   517    517   # Rules to build opcodes.c and opcodes.h
   518    518   #
   519    519   opcodes.c:	opcodes.h $(TOP)/mkopcodec.awk
   520         -	sort -n -b -k 3 opcodes.h | $(NAWK) -f $(TOP)/mkopcodec.awk >opcodes.c
          520  +	$(NAWK) -f $(TOP)/mkopcodec.awk opcodes.h >opcodes.c
   521    521   
   522    522   opcodes.h:	parse.h $(TOP)/src/vdbe.c $(TOP)/mkopcodeh.awk
   523    523   	cat parse.h $(TOP)/src/vdbe.c | \
   524    524   		$(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   525    525   
   526    526   # Rules to build parse.c and parse.h - the outputs of lemon.
   527    527   #

Changes to main.mk.

   425    425   	$(TCCX) $(TCL_FLAGS) -c $(TOP)/src/tclsqlite.c
   426    426   
   427    427   
   428    428   
   429    429   # Rules to build opcodes.c and opcodes.h
   430    430   #
   431    431   opcodes.c:	opcodes.h $(TOP)/mkopcodec.awk
   432         -	sort -n -b -k 3 opcodes.h | $(NAWK) -f $(TOP)/mkopcodec.awk >opcodes.c
          432  +	$(NAWK) -f $(TOP)/mkopcodec.awk opcodes.h >opcodes.c
   433    433   
   434    434   opcodes.h:	parse.h $(TOP)/src/vdbe.c $(TOP)/mkopcodeh.awk
   435    435   	cat parse.h $(TOP)/src/vdbe.c | \
   436    436   		$(NAWK) -f $(TOP)/mkopcodeh.awk >opcodes.h
   437    437   
   438    438   # Rules to build parse.c and parse.h - the outputs of lemon.
   439    439   #

Changes to mkopcodec.awk.

    13     13     print "/* See the mkopcodec.awk script for details. */"
    14     14     printf "#if !defined(SQLITE_OMIT_EXPLAIN)"
    15     15     printf    " || !defined(NDEBUG)"
    16     16     printf    " || defined(VDBE_PROFILE)"
    17     17     print     " || defined(SQLITE_DEBUG)"
    18     18     print "const char *sqlite3OpcodeName(int i){"
    19     19     print " static const char *const azName[] = { \"?\","
           20  +  mx = 0
    20     21   }
    21     22   /define OP_/ {
    22     23     sub("OP_","",$2)
    23         -  i++
    24         -  printf "     /* %3d */ \"%s\",\n", $3, $2
           24  +  i = $3+0
           25  +  label[i] = $2
           26  +  if( mx<i ) mx = i
    25     27   }
    26     28   END {
           29  +  for(i=1; i<=mx; i++){
           30  +    printf "     /* %3d */ \"%s\",\n", i, label[i]
           31  +  }
    27     32     print "  };"
    28     33     print "  return azName[i];"
    29     34     print "}"
    30     35     print "#endif"
    31     36   }

Changes to src/btree.c.

   652    652     i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
   653    653     int bias,           /* Bias search to the high end */
   654    654     int *pRes           /* Write search results here */
   655    655   ){
   656    656     int rc;                    /* Status code */
   657    657     UnpackedRecord *pIdxKey;   /* Unpacked index key */
   658    658     char aSpace[150];          /* Temp space for pIdxKey - to avoid a malloc */
          659  +  char *pFree = 0;
   659    660   
   660    661     if( pKey ){
   661    662       assert( nKey==(i64)(int)nKey );
   662         -    pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
   663         -                                      aSpace, sizeof(aSpace));
          663  +    pIdxKey = sqlite3VdbeAllocUnpackedRecord(
          664  +        pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
          665  +    );
   664    666       if( pIdxKey==0 ) return SQLITE_NOMEM;
          667  +    sqlite3VdbeRecordUnpack(pCur->pKeyInfo, nKey, pKey, pIdxKey);
   665    668     }else{
   666    669       pIdxKey = 0;
   667    670     }
   668    671     rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
   669         -  if( pKey ){
   670         -    sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
          672  +  if( pFree ){
          673  +    sqlite3DbFree(pCur->pKeyInfo->db, pFree);
   671    674     }
   672    675     return rc;
   673    676   }
   674    677   
   675    678   /*
   676    679   ** Restore the cursor to the position it was in (or as close to as possible)
   677    680   ** when saveCursorPosition() was called. Note that this call deletes the 
................................................................................
  1730   1733   
  1731   1734     /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
  1732   1735     assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
  1733   1736   
  1734   1737     /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
  1735   1738     assert( (flags & BTREE_SINGLE)==0 || isTempDb );
  1736   1739   
  1737         -  /* The BTREE_SORTER flag is only used if SQLITE_OMIT_MERGE_SORT is undef */
  1738         -#ifdef SQLITE_OMIT_MERGE_SORT
  1739         -  assert( (flags & BTREE_SORTER)==0 );
  1740         -#endif
  1741         -
  1742         -  /* BTREE_SORTER is always on a BTREE_SINGLE, BTREE_OMIT_JOURNAL */
  1743         -  assert( (flags & BTREE_SORTER)==0 ||
  1744         -          (flags & (BTREE_SINGLE|BTREE_OMIT_JOURNAL))
  1745         -                                        ==(BTREE_SINGLE|BTREE_OMIT_JOURNAL) );
  1746         -
  1747   1740     if( db->flags & SQLITE_NoReadlock ){
  1748   1741       flags |= BTREE_NO_READLOCK;
  1749   1742     }
  1750   1743     if( isMemdb ){
  1751   1744       flags |= BTREE_MEMORY;
  1752         -    flags &= ~BTREE_SORTER;
  1753   1745     }
  1754   1746     if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
  1755   1747       vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
  1756   1748     }
  1757   1749     p = sqlite3MallocZero(sizeof(Btree));
  1758   1750     if( !p ){
  1759   1751       return SQLITE_NOMEM;
................................................................................
  2750   2742       nCell = pPage->nCell;
  2751   2743   
  2752   2744       for(i=0; i<nCell; i++){
  2753   2745         u8 *pCell = findCell(pPage, i);
  2754   2746         if( eType==PTRMAP_OVERFLOW1 ){
  2755   2747           CellInfo info;
  2756   2748           btreeParseCellPtr(pPage, pCell, &info);
  2757         -        if( info.iOverflow ){
  2758         -          if( iFrom==get4byte(&pCell[info.iOverflow]) ){
  2759         -            put4byte(&pCell[info.iOverflow], iTo);
  2760         -            break;
  2761         -          }
         2749  +        if( info.iOverflow
         2750  +         && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
         2751  +         && iFrom==get4byte(&pCell[info.iOverflow])
         2752  +        ){
         2753  +          put4byte(&pCell[info.iOverflow], iTo);
         2754  +          break;
  2762   2755           }
  2763   2756         }else{
  2764   2757           if( get4byte(pCell)==iFrom ){
  2765   2758             put4byte(pCell, iTo);
  2766   2759             break;
  2767   2760           }
  2768   2761         }
................................................................................
  3475   3468     assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
  3476   3469     assert( pBt->pPage1 && pBt->pPage1->aData );
  3477   3470   
  3478   3471     if( NEVER(wrFlag && pBt->readOnly) ){
  3479   3472       return SQLITE_READONLY;
  3480   3473     }
  3481   3474     if( iTable==1 && btreePagecount(pBt)==0 ){
  3482         -    return SQLITE_EMPTY;
         3475  +    assert( wrFlag==0 );
         3476  +    iTable = 0;
  3483   3477     }
  3484   3478   
  3485   3479     /* Now that no other errors can occur, finish filling in the BtCursor
  3486   3480     ** variables and link the cursor into the BtShared list.  */
  3487   3481     pCur->pgnoRoot = (Pgno)iTable;
  3488   3482     pCur->iPage = -1;
  3489   3483     pCur->pKeyInfo = pKeyInfo;
................................................................................
  4229   4223   
  4230   4224     if( pCur->iPage>=0 ){
  4231   4225       int i;
  4232   4226       for(i=1; i<=pCur->iPage; i++){
  4233   4227         releasePage(pCur->apPage[i]);
  4234   4228       }
  4235   4229       pCur->iPage = 0;
         4230  +  }else if( pCur->pgnoRoot==0 ){
         4231  +    pCur->eState = CURSOR_INVALID;
         4232  +    return SQLITE_OK;
  4236   4233     }else{
  4237   4234       rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
  4238   4235       if( rc!=SQLITE_OK ){
  4239   4236         pCur->eState = CURSOR_INVALID;
  4240   4237         return rc;
  4241   4238       }
  4242   4239       pCur->iPage = 0;
................................................................................
  4338   4335     int rc;
  4339   4336   
  4340   4337     assert( cursorHoldsMutex(pCur) );
  4341   4338     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4342   4339     rc = moveToRoot(pCur);
  4343   4340     if( rc==SQLITE_OK ){
  4344   4341       if( pCur->eState==CURSOR_INVALID ){
  4345         -      assert( pCur->apPage[pCur->iPage]->nCell==0 );
         4342  +      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
  4346   4343         *pRes = 1;
  4347   4344       }else{
  4348   4345         assert( pCur->apPage[pCur->iPage]->nCell>0 );
  4349   4346         *pRes = 0;
  4350   4347         rc = moveToLeftmost(pCur);
  4351   4348       }
  4352   4349     }
................................................................................
  4377   4374   #endif
  4378   4375       return SQLITE_OK;
  4379   4376     }
  4380   4377   
  4381   4378     rc = moveToRoot(pCur);
  4382   4379     if( rc==SQLITE_OK ){
  4383   4380       if( CURSOR_INVALID==pCur->eState ){
  4384         -      assert( pCur->apPage[pCur->iPage]->nCell==0 );
         4381  +      assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
  4385   4382         *pRes = 1;
  4386   4383       }else{
  4387   4384         assert( pCur->eState==CURSOR_VALID );
  4388   4385         *pRes = 0;
  4389   4386         rc = moveToRightmost(pCur);
  4390   4387         pCur->atLast = rc==SQLITE_OK ?1:0;
  4391   4388       }
................................................................................
  4450   4447       }
  4451   4448     }
  4452   4449   
  4453   4450     rc = moveToRoot(pCur);
  4454   4451     if( rc ){
  4455   4452       return rc;
  4456   4453     }
  4457         -  assert( pCur->apPage[pCur->iPage] );
  4458         -  assert( pCur->apPage[pCur->iPage]->isInit );
  4459         -  assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
         4454  +  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
         4455  +  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
         4456  +  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
  4460   4457     if( pCur->eState==CURSOR_INVALID ){
  4461   4458       *pRes = -1;
  4462         -    assert( pCur->apPage[pCur->iPage]->nCell==0 );
         4459  +    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
  4463   4460       return SQLITE_OK;
  4464   4461     }
  4465   4462     assert( pCur->apPage[0]->intKey || pIdxKey );
  4466   4463     for(;;){
  4467   4464       int lwr, upr, idx;
  4468   4465       Pgno chldPg;
  4469   4466       MemPage *pPage = pCur->apPage[pCur->iPage];
................................................................................
  5182   5179     u32 ovflPageSize;
  5183   5180   
  5184   5181     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  5185   5182     btreeParseCellPtr(pPage, pCell, &info);
  5186   5183     if( info.iOverflow==0 ){
  5187   5184       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
  5188   5185     }
         5186  +  if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
         5187  +    return SQLITE_CORRUPT;  /* Cell extends past end of page */
         5188  +  }
  5189   5189     ovflPgno = get4byte(&pCell[info.iOverflow]);
  5190   5190     assert( pBt->usableSize > 4 );
  5191   5191     ovflPageSize = pBt->usableSize - 4;
  5192   5192     nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
  5193   5193     assert( ovflPgno==0 || nOvfl>0 );
  5194   5194     while( nOvfl-- ){
  5195   5195       Pgno iNext = 0;
................................................................................
  7284   7284       */
  7285   7285       zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
  7286   7286       releasePage(pPage);
  7287   7287     }
  7288   7288     return rc;  
  7289   7289   }
  7290   7290   int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
  7291         -  BtShared *pBt = p->pBt;
  7292   7291     int rc;
  7293   7292     sqlite3BtreeEnter(p);
  7294         -  if( (pBt->openFlags&BTREE_SINGLE) ){
  7295         -    pBt->nPage = 0;
  7296         -    sqlite3PagerTruncateImage(pBt->pPager, 1);
  7297         -    rc = newDatabase(pBt);
  7298         -  }else{
  7299         -    rc = btreeDropTable(p, iTable, piMoved);
  7300         -  }
         7293  +  rc = btreeDropTable(p, iTable, piMoved);
  7301   7294     sqlite3BtreeLeave(p);
  7302   7295     return rc;
  7303   7296   }
  7304   7297   
  7305   7298   
  7306   7299   /*
  7307   7300   ** This function may only be called if the b-tree connection already
................................................................................
  7372   7365   ** SQLITE_OK is returned if the operation is successfully executed. 
  7373   7366   ** Otherwise, if an error is encountered (i.e. an IO error or database
  7374   7367   ** corruption) an SQLite error code is returned.
  7375   7368   */
  7376   7369   int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
  7377   7370     i64 nEntry = 0;                      /* Value to return in *pnEntry */
  7378   7371     int rc;                              /* Return code */
         7372  +
         7373  +  if( pCur->pgnoRoot==0 ){
         7374  +    *pnEntry = 0;
         7375  +    return SQLITE_OK;
         7376  +  }
  7379   7377     rc = moveToRoot(pCur);
  7380   7378   
  7381   7379     /* Unless an error occurs, the following loop runs one iteration for each
  7382   7380     ** page in the B-Tree structure (not including overflow pages). 
  7383   7381     */
  7384   7382     while( rc==SQLITE_OK ){
  7385   7383       int iIdx;                          /* Index of child node in parent */

Changes to src/btree.h.

    57     57   ** pager.h.
    58     58   */
    59     59   #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
    60     60   #define BTREE_NO_READLOCK   2  /* Omit readlocks on readonly files */
    61     61   #define BTREE_MEMORY        4  /* This is an in-memory DB */
    62     62   #define BTREE_SINGLE        8  /* The file contains at most 1 b-tree */
    63     63   #define BTREE_UNORDERED    16  /* Use of a hash implementation is OK */
    64         -#define BTREE_SORTER       32  /* Used as accumulator in external merge sort */
    65     64   
    66     65   int sqlite3BtreeClose(Btree*);
    67     66   int sqlite3BtreeSetCacheSize(Btree*,int);
    68     67   int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
    69     68   int sqlite3BtreeSyncDisabled(Btree*);
    70     69   int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
    71     70   int sqlite3BtreeGetPageSize(Btree*);

Changes to src/build.c.

  2339   2339   */
  2340   2340   static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
  2341   2341     Table *pTab = pIndex->pTable;  /* The table that is indexed */
  2342   2342     int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
  2343   2343     int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
  2344   2344     int iSorter = iTab;            /* Cursor opened by OpenSorter (if in use) */
  2345   2345     int addr1;                     /* Address of top of loop */
         2346  +  int addr2;                     /* Address to jump to for next iteration */
  2346   2347     int tnum;                      /* Root page of index */
  2347   2348     Vdbe *v;                       /* Generate code into this virtual machine */
  2348   2349     KeyInfo *pKey;                 /* KeyInfo for index */
  2349   2350     int regIdxKey;                 /* Registers containing the index key */
  2350   2351     int regRecord;                 /* Register holding assemblied index record */
  2351   2352     sqlite3 *db = pParse->db;      /* The database connection */
  2352   2353     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
  2353   2354   
  2354         -  /* Set bUseSorter to use OP_OpenSorter, or clear it to insert directly 
  2355         -  ** into the index. The sorter is used unless either OMIT_MERGE_SORT is
  2356         -  ** defined or the system is configured to store temp files in-memory. */
  2357         -#ifdef SQLITE_OMIT_MERGE_SORT
  2358         -  static const int bUseSorter = 0;
  2359         -#else
  2360         -  const int bUseSorter = !sqlite3TempInMemory(pParse->db);
  2361         -#endif
  2362         -
  2363   2355   #ifndef SQLITE_OMIT_AUTHORIZATION
  2364   2356     if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
  2365   2357         db->aDb[iDb].zName ) ){
  2366   2358       return;
  2367   2359     }
  2368   2360   #endif
  2369   2361   
................................................................................
  2381   2373     pKey = sqlite3IndexKeyinfo(pParse, pIndex);
  2382   2374     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2383   2375                       (char *)pKey, P4_KEYINFO_HANDOFF);
  2384   2376     if( memRootPage>=0 ){
  2385   2377       sqlite3VdbeChangeP5(v, 1);
  2386   2378     }
  2387   2379   
         2380  +#ifndef SQLITE_OMIT_MERGE_SORT
  2388   2381     /* Open the sorter cursor if we are to use one. */
  2389         -  if( bUseSorter ){
  2390         -    iSorter = pParse->nTab++;
  2391         -    sqlite3VdbeAddOp4(v, OP_OpenSorter, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
  2392         -    sqlite3VdbeChangeP5(v, BTREE_SORTER);
  2393         -  }
         2382  +  iSorter = pParse->nTab++;
         2383  +  sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
         2384  +#endif
  2394   2385   
  2395   2386     /* Open the table. Loop through all rows of the table, inserting index
  2396   2387     ** records into the sorter. */
  2397   2388     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2398   2389     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
         2390  +  addr2 = addr1 + 1;
  2399   2391     regRecord = sqlite3GetTempReg(pParse);
  2400   2392     regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
  2401   2393   
  2402         -  if( bUseSorter ){
  2403         -    sqlite3VdbeAddOp2(v, OP_IdxInsert, iSorter, regRecord);
  2404         -    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
  2405         -    sqlite3VdbeJumpHere(v, addr1);
  2406         -    addr1 = sqlite3VdbeAddOp2(v, OP_Sort, iSorter, 0);
  2407         -    sqlite3VdbeAddOp2(v, OP_RowKey, iSorter, regRecord);
         2394  +#ifndef SQLITE_OMIT_MERGE_SORT
         2395  +  sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
         2396  +  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
         2397  +  sqlite3VdbeJumpHere(v, addr1);
         2398  +  addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
         2399  +  if( pIndex->onError!=OE_None ){
         2400  +    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
         2401  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
         2402  +    addr2 = sqlite3VdbeCurrentAddr(v);
         2403  +    sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
         2404  +    sqlite3HaltConstraint(
         2405  +        pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
         2406  +    );
         2407  +  }else{
         2408  +    addr2 = sqlite3VdbeCurrentAddr(v);
  2408   2409     }
  2409         -
         2410  +  sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
         2411  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
         2412  +  sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
         2413  +#else
  2410   2414     if( pIndex->onError!=OE_None ){
  2411   2415       const int regRowid = regIdxKey + pIndex->nColumn;
  2412   2416       const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
  2413   2417       void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
  2414   2418   
  2415   2419       /* The registers accessed by the OP_IsUnique opcode were allocated
  2416   2420       ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey()
................................................................................
  2421   2425       ** we can be sure that no other temp registers have been allocated
  2422   2426       ** since sqlite3ReleaseTempRange() was called, it is safe to do so.
  2423   2427       */
  2424   2428       sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
  2425   2429       sqlite3HaltConstraint(
  2426   2430           pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
  2427   2431     }
  2428         -  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, bUseSorter);
         2432  +  sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
  2429   2433     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
         2434  +#endif
  2430   2435     sqlite3ReleaseTempReg(pParse, regRecord);
  2431         -  sqlite3VdbeAddOp2(v, OP_Next, iSorter, addr1+1);
         2436  +  sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
  2432   2437     sqlite3VdbeJumpHere(v, addr1);
  2433   2438   
  2434   2439     sqlite3VdbeAddOp1(v, OP_Close, iTab);
  2435   2440     sqlite3VdbeAddOp1(v, OP_Close, iIdx);
  2436   2441     sqlite3VdbeAddOp1(v, OP_Close, iSorter);
  2437   2442   }
  2438   2443   

Changes to src/ctime.c.

   143    143   #endif
   144    144   #ifdef SQLITE_INT64_TYPE
   145    145     "INT64_TYPE",
   146    146   #endif
   147    147   #ifdef SQLITE_LOCK_TRACE
   148    148     "LOCK_TRACE",
   149    149   #endif
          150  +#ifdef SQLITE_MAX_SCHEMA_RETRY
          151  +  "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
          152  +#endif
   150    153   #ifdef SQLITE_MEMDEBUG
   151    154     "MEMDEBUG",
   152    155   #endif
   153    156   #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
   154    157     "MIXED_ENDIAN_64BIT_FLOAT",
   155    158   #endif
   156    159   #ifdef SQLITE_NO_SYNC

Changes to src/expr.c.

  2283   2283         AggInfo *pAggInfo = pExpr->pAggInfo;
  2284   2284         struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
  2285   2285         if( !pAggInfo->directMode ){
  2286   2286           assert( pCol->iMem>0 );
  2287   2287           inReg = pCol->iMem;
  2288   2288           break;
  2289   2289         }else if( pAggInfo->useSortingIdx ){
  2290         -        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
         2290  +        sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
  2291   2291                                 pCol->iSorterColumn, target);
  2292   2292           break;
  2293   2293         }
  2294   2294         /* Otherwise, fall thru into the TK_COLUMN case */
  2295   2295       }
  2296   2296       case TK_COLUMN: {
  2297   2297         if( pExpr->iTable<0 ){

Changes to src/os_common.h.

    25     25   ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
    26     26   ** switch.  The following code should catch this problem at compile-time.
    27     27   */
    28     28   #ifdef MEMORY_DEBUG
    29     29   # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
    30     30   #endif
    31     31   
    32         -#ifdef SQLITE_DEBUG
           32  +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
    33     33   # ifndef SQLITE_DEBUG_OS_TRACE
    34     34   #   define SQLITE_DEBUG_OS_TRACE 0
    35     35   # endif
    36     36     int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
    37     37   # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
    38     38   #else
    39     39   # define OSTRACE(X)

Changes to src/os_unix.c.

  2521   2521   ** to a non-zero value otherwise *pResOut is set to zero.  The return value
  2522   2522   ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
  2523   2523   */
  2524   2524   static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
  2525   2525     int rc = SQLITE_OK;
  2526   2526     int reserved = 0;
  2527   2527     unixFile *pFile = (unixFile*)id;
         2528  +  afpLockingContext *context;
  2528   2529     
  2529   2530     SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
  2530   2531     
  2531   2532     assert( pFile );
  2532         -  afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
         2533  +  context = (afpLockingContext *) pFile->lockingContext;
  2533   2534     if( context->reserved ){
  2534   2535       *pResOut = 1;
  2535   2536       return SQLITE_OK;
  2536   2537     }
  2537   2538     unixEnterMutex(); /* Because pFile->pInode is shared across threads */
  2538   2539     
  2539   2540     /* Check if a thread in this process holds such a lock */
................................................................................
  2665   2666       }
  2666   2667     }
  2667   2668     
  2668   2669     /* If control gets to this point, then actually go ahead and make
  2669   2670     ** operating system calls for the specified lock.
  2670   2671     */
  2671   2672     if( eFileLock==SHARED_LOCK ){
  2672         -    int lrc1, lrc2, lrc1Errno;
         2673  +    int lrc1, lrc2, lrc1Errno = 0;
  2673   2674       long lk, mask;
  2674   2675       
  2675   2676       assert( pInode->nShared==0 );
  2676   2677       assert( pInode->eFileLock==0 );
  2677   2678           
  2678   2679       mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
  2679   2680       /* Now get the read-lock SHARED_LOCK */
................................................................................
  3141   3142   */
  3142   3143   int sqlite3_sync_count = 0;
  3143   3144   int sqlite3_fullsync_count = 0;
  3144   3145   #endif
  3145   3146   
  3146   3147   /*
  3147   3148   ** We do not trust systems to provide a working fdatasync().  Some do.
  3148         -** Others do no.  To be safe, we will stick with the (slower) fsync().
  3149         -** If you know that your system does support fdatasync() correctly,
         3149  +** Others do no.  To be safe, we will stick with the (slightly slower)
         3150  +** fsync(). If you know that your system does support fdatasync() correctly,
  3150   3151   ** then simply compile with -Dfdatasync=fdatasync
  3151   3152   */
  3152         -#if !defined(fdatasync) && !defined(__linux__)
         3153  +#if !defined(fdatasync)
  3153   3154   # define fdatasync fsync
  3154   3155   #endif
  3155   3156   
  3156   3157   /*
  3157   3158   ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
  3158   3159   ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
  3159   3160   ** only available on Mac OS X.  But that could change.
................................................................................
  3435   3436   ** proxying locking division.
  3436   3437   */
  3437   3438   static int proxyFileControl(sqlite3_file*,int,void*);
  3438   3439   #endif
  3439   3440   
  3440   3441   /* 
  3441   3442   ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT 
  3442         -** file-control operation.
  3443         -**
  3444         -** If the user has configured a chunk-size for this file, it could be
  3445         -** that the file needs to be extended at this point. Otherwise, the
  3446         -** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
         3443  +** file-control operation.  Enlarge the database to nBytes in size
         3444  +** (rounded up to the next chunk-size).  If the database is already
         3445  +** nBytes or larger, this routine is a no-op.
  3447   3446   */
  3448   3447   static int fcntlSizeHint(unixFile *pFile, i64 nByte){
  3449         -  { /* preserve indentation of removed "if" */
         3448  +  if( pFile->szChunk>0 ){
  3450   3449       i64 nSize;                    /* Required file size */
  3451         -    i64 szChunk;                  /* Chunk size */
  3452   3450       struct stat buf;              /* Used to hold return values of fstat() */
  3453   3451      
  3454   3452       if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
  3455   3453   
  3456         -    szChunk = pFile->szChunk;
  3457         -    if( szChunk==0 ){
  3458         -      nSize = nByte;
  3459         -    }else{
  3460         -      nSize = ((nByte+szChunk-1) / szChunk) * szChunk;
  3461         -    }
         3454  +    nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
  3462   3455       if( nSize>(i64)buf.st_size ){
  3463   3456   
  3464   3457   #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
  3465   3458         /* The code below is handling the return value of osFallocate() 
  3466   3459         ** correctly. posix_fallocate() is defined to "returns zero on success, 
  3467   3460         ** or an error number on  failure". See the manpage for details. */
  3468   3461         int err;
................................................................................
  3643   3636   ** All other fields are read/write.  The unixShm.pFile->mutex must be held
  3644   3637   ** while accessing any read/write fields.
  3645   3638   */
  3646   3639   struct unixShm {
  3647   3640     unixShmNode *pShmNode;     /* The underlying unixShmNode object */
  3648   3641     unixShm *pNext;            /* Next unixShm with the same unixShmNode */
  3649   3642     u8 hasMutex;               /* True if holding the unixShmNode mutex */
         3643  +  u8 id;                     /* Id of this connection within its unixShmNode */
  3650   3644     u16 sharedMask;            /* Mask of shared locks held */
  3651   3645     u16 exclMask;              /* Mask of exclusive locks held */
  3652         -#ifdef SQLITE_DEBUG
  3653         -  u8 id;                     /* Id of this connection within its unixShmNode */
  3654         -#endif
  3655   3646   };
  3656   3647   
  3657   3648   /*
  3658   3649   ** Constants used for locking
  3659   3650   */
  3660   3651   #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
  3661   3652   #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
................................................................................
  4953   4944     int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
  4954   4945     int isCreate     = (flags & SQLITE_OPEN_CREATE);
  4955   4946     int isReadonly   = (flags & SQLITE_OPEN_READONLY);
  4956   4947     int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
  4957   4948   #if SQLITE_ENABLE_LOCKING_STYLE
  4958   4949     int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
  4959   4950   #endif
         4951  +#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
         4952  +  struct statfs fsInfo;
         4953  +#endif
  4960   4954   
  4961   4955     /* If creating a master or main-file journal, this function will open
  4962   4956     ** a file-descriptor on the directory too. The first time unixSync()
  4963   4957     ** is called the directory file descriptor will be fsync()ed and close()d.
  4964   4958     */
  4965   4959     int syncDir = (isCreate && (
  4966   4960           eType==SQLITE_OPEN_MASTER_JOURNAL 
................................................................................
  5085   5079     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
  5086   5080   #endif
  5087   5081   
  5088   5082     noLock = eType!=SQLITE_OPEN_MAIN_DB;
  5089   5083   
  5090   5084     
  5091   5085   #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
  5092         -  struct statfs fsInfo;
  5093   5086     if( fstatfs(fd, &fsInfo) == -1 ){
  5094   5087       ((unixFile*)pFile)->lastErrno = errno;
  5095   5088       robust_close(p, fd, __LINE__);
  5096   5089       return SQLITE_IOERR_ACCESS;
  5097   5090     }
  5098   5091     if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
  5099   5092       ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
................................................................................
  5109   5102       int useProxy = 0;
  5110   5103   
  5111   5104       /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means 
  5112   5105       ** never use proxy, NULL means use proxy for non-local files only.  */
  5113   5106       if( envforce!=NULL ){
  5114   5107         useProxy = atoi(envforce)>0;
  5115   5108       }else{
  5116         -      struct statfs fsInfo;
  5117   5109         if( statfs(zPath, &fsInfo) == -1 ){
  5118   5110           /* In theory, the close(fd) call is sub-optimal. If the file opened
  5119   5111           ** with fd is a database file, and there are other connections open
  5120   5112           ** on that file that are currently holding advisory locks on it,
  5121   5113           ** then the call to close() will cancel those locks. In practice,
  5122   5114           ** we're assuming that statfs() doesn't fail very often. At least
  5123   5115           ** not while other file descriptors opened by the same process on
................................................................................
  5850   5842         int err = errno;
  5851   5843         if( pError ){
  5852   5844           *pError = err;
  5853   5845         }
  5854   5846         return SQLITE_IOERR;
  5855   5847       }
  5856   5848     }
         5849  +#else
         5850  +  UNUSED_PARAMETER(pError);
  5857   5851   #endif
  5858   5852   #ifdef SQLITE_TEST
  5859   5853     /* simulate multiple hosts by creating unique hostid file paths */
  5860   5854     if( sqlite3_hostid_num != 0){
  5861   5855       pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
  5862   5856     }
  5863   5857   #endif
................................................................................
  5942   5936   static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
  5943   5937     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; 
  5944   5938     unixFile *conchFile = pCtx->conchFile;
  5945   5939     int rc = SQLITE_OK;
  5946   5940     int nTries = 0;
  5947   5941     struct timespec conchModTime;
  5948   5942     
         5943  +  memset(&conchModTime, 0, sizeof(conchModTime));
  5949   5944     do {
  5950   5945       rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
  5951   5946       nTries ++;
  5952   5947       if( rc==SQLITE_BUSY ){
  5953   5948         /* If the lock failed (busy):
  5954   5949          * 1st try: get the mod time of the conch, wait 0.5s and try again. 
  5955   5950          * 2nd try: fail if the mod time changed or host id is different, wait 
................................................................................
  6173   6168           }
  6174   6169         }
  6175   6170         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
  6176   6171         
  6177   6172       end_takeconch:
  6178   6173         OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
  6179   6174         if( rc==SQLITE_OK && pFile->openFlags ){
         6175  +        int fd;
  6180   6176           if( pFile->h>=0 ){
  6181   6177             robust_close(pFile, pFile->h, __LINE__);
  6182   6178           }
  6183   6179           pFile->h = -1;
  6184         -        int fd = robust_open(pCtx->dbPath, pFile->openFlags,
         6180  +        fd = robust_open(pCtx->dbPath, pFile->openFlags,
  6185   6181                         SQLITE_DEFAULT_FILE_PERMISSIONS);
  6186   6182           OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
  6187   6183           if( fd>=0 ){
  6188   6184             pFile->h = fd;
  6189   6185           }else{
  6190   6186             rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
  6191   6187              during locking */

Changes to src/os_win.c.

  1212   1212     SimulateIOError(return SQLITE_IOERR_TRUNCATE);
  1213   1213   
  1214   1214     /* If the user has configured a chunk-size for this file, truncate the
  1215   1215     ** file so that it consists of an integer number of chunks (i.e. the
  1216   1216     ** actual file size after the operation may be larger than the requested
  1217   1217     ** size).
  1218   1218     */
  1219         -  if( pFile->szChunk ){
         1219  +  if( pFile->szChunk>0 ){
  1220   1220       nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
  1221   1221     }
  1222   1222   
  1223   1223     /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
  1224   1224     if( seekWinFile(pFile, nByte) ){
  1225   1225       rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate1", pFile->zPath);
  1226   1226     }else if( 0==SetEndOfFile(pFile->h) ){
................................................................................
  1599   1599         return SQLITE_OK;
  1600   1600       }
  1601   1601       case SQLITE_FCNTL_CHUNK_SIZE: {
  1602   1602         pFile->szChunk = *(int *)pArg;
  1603   1603         return SQLITE_OK;
  1604   1604       }
  1605   1605       case SQLITE_FCNTL_SIZE_HINT: {
  1606         -      winFile *pFile = (winFile*)id;
  1607         -      sqlite3_int64 oldSz;
  1608         -      int rc = winFileSize(id, &oldSz);
  1609         -      if( rc==SQLITE_OK ){
  1610         -        sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
  1611         -        if( newSz>oldSz ){
  1612         -          SimulateIOErrorBenign(1);
  1613         -          rc = winTruncate(id, newSz);
  1614         -          SimulateIOErrorBenign(0);
         1606  +      if( pFile->szChunk>0 ){
         1607  +        sqlite3_int64 oldSz;
         1608  +        int rc = winFileSize(id, &oldSz);
         1609  +        if( rc==SQLITE_OK ){
         1610  +          sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
         1611  +          if( newSz>oldSz ){
         1612  +            SimulateIOErrorBenign(1);
         1613  +            rc = winTruncate(id, newSz);
         1614  +            SimulateIOErrorBenign(0);
         1615  +          }
  1615   1616           }
         1617  +        return rc;
  1616   1618         }
  1617         -      return rc;
         1619  +      return SQLITE_OK;
  1618   1620       }
  1619   1621       case SQLITE_FCNTL_PERSIST_WAL: {
  1620   1622         int bPersist = *(int*)pArg;
  1621   1623         if( bPersist<0 ){
  1622   1624           *(int*)pArg = pFile->bPersistWal;
  1623   1625         }else{
  1624   1626           pFile->bPersistWal = bPersist!=0;

Changes to src/pager.c.

   617    617     u8 fullSync;                /* Do extra syncs of the journal for robustness */
   618    618     u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
   619    619     u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
   620    620     u8 tempFile;                /* zFilename is a temporary file */
   621    621     u8 readOnly;                /* True for a read-only database */
   622    622     u8 memDb;                   /* True to inhibit all file I/O */
   623    623     u8 hasSeenStress;           /* pagerStress() called one or more times */
   624         -  u8 isSorter;                /* True for a PAGER_SORTER */
   625    624   
   626    625     /**************************************************************************
   627    626     ** The following block contains those class members that change during
   628    627     ** routine opertion.  Class members not in this block are either fixed
   629    628     ** when the pager is first created or else only change when there is a
   630    629     ** significant mode change (such as changing the page_size, locking_mode,
   631    630     ** or the journal_mode).  From another view, these class members describe
................................................................................
   841    840       assert( p->journalMode==PAGER_JOURNALMODE_OFF 
   842    841            || p->journalMode==PAGER_JOURNALMODE_MEMORY 
   843    842       );
   844    843       assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
   845    844       assert( pagerUseWal(p)==0 );
   846    845     }
   847    846   
   848         -  /* A sorter is a temp file that never spills to disk and always has
   849         -  ** the doNotSpill flag set
   850         -  */
   851         -  if( p->isSorter ){
   852         -    assert( p->tempFile );
   853         -    assert( p->doNotSpill );
   854         -    assert( p->fd->pMethods==0 );
   855         -  }
   856         -
   857    847     /* If changeCountDone is set, a RESERVED lock or greater must be held
   858    848     ** on the file.
   859    849     */
   860    850     assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
   861    851     assert( p->eLock!=PENDING_LOCK );
   862    852   
   863    853     switch( p->eState ){
................................................................................
  4553   4543     }else if( memDb ){
  4554   4544       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
  4555   4545     }
  4556   4546     /* pPager->xBusyHandler = 0; */
  4557   4547     /* pPager->pBusyHandlerArg = 0; */
  4558   4548     pPager->xReiniter = xReinit;
  4559   4549     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
  4560         -#ifndef SQLITE_OMIT_MERGE_SORT
  4561         -  if( flags & PAGER_SORTER ){
  4562         -    pPager->doNotSpill = 1;
  4563         -    pPager->isSorter = 1;
  4564         -  }
  4565         -#endif
  4566   4550   
  4567   4551     *ppPager = pPager;
  4568   4552     return SQLITE_OK;
  4569   4553   }
  4570   4554   
  4571   4555   
  4572   4556   
................................................................................
  6103   6087   /*
  6104   6088   ** Return true if this is an in-memory pager.
  6105   6089   */
  6106   6090   int sqlite3PagerIsMemdb(Pager *pPager){
  6107   6091     return MEMDB;
  6108   6092   }
  6109   6093   
  6110         -#ifndef SQLITE_OMIT_MERGE_SORT
  6111         -/*
  6112         -** Return true if the pager has seen a pagerStress callback.
  6113         -*/
  6114         -int sqlite3PagerUnderStress(Pager *pPager){
  6115         -  assert( pPager->isSorter );
  6116         -  assert( pPager->doNotSpill );
  6117         -  return pPager->hasSeenStress;
  6118         -}
  6119         -#endif
  6120         -
  6121   6094   /*
  6122   6095   ** Check that there are at least nSavepoint savepoints open. If there are
  6123   6096   ** currently less than nSavepoints open, then open one or more savepoints
  6124   6097   ** to make up the difference. If the number of savepoints is already
  6125   6098   ** equal to nSavepoint, then this function is a no-op.
  6126   6099   **
  6127   6100   ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error 

Changes to src/pager.h.

   152    152   const char *sqlite3PagerFilename(Pager*);
   153    153   const sqlite3_vfs *sqlite3PagerVfs(Pager*);
   154    154   sqlite3_file *sqlite3PagerFile(Pager*);
   155    155   const char *sqlite3PagerJournalname(Pager*);
   156    156   int sqlite3PagerNosync(Pager*);
   157    157   void *sqlite3PagerTempSpace(Pager*);
   158    158   int sqlite3PagerIsMemdb(Pager*);
   159         -#ifndef SQLITE_OMIT_MERGE_SORT
   160         -int sqlite3PagerUnderStress(Pager*);
   161         -#endif
   162    159   
   163    160   /* Functions used to truncate the database file. */
   164    161   void sqlite3PagerTruncateImage(Pager*,Pgno);
   165    162   
   166    163   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
   167    164   void *sqlite3PagerCodec(DbPage *);
   168    165   #endif

Changes to src/select.c.

   415    415     Select *pSelect,       /* The whole SELECT statement */
   416    416     int regData            /* Register holding data to be sorted */
   417    417   ){
   418    418     Vdbe *v = pParse->pVdbe;
   419    419     int nExpr = pOrderBy->nExpr;
   420    420     int regBase = sqlite3GetTempRange(pParse, nExpr+2);
   421    421     int regRecord = sqlite3GetTempReg(pParse);
          422  +  int op;
   422    423     sqlite3ExprCacheClear(pParse);
   423    424     sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
   424    425     sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
   425    426     sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
   426    427     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
   427         -  sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
          428  +  if( pSelect->selFlags & SF_UseSorter ){
          429  +    op = OP_SorterInsert;
          430  +  }else{
          431  +    op = OP_IdxInsert;
          432  +  }
          433  +  sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
   428    434     sqlite3ReleaseTempReg(pParse, regRecord);
   429    435     sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
   430    436     if( pSelect->iLimit ){
   431    437       int addr1, addr2;
   432    438       int iLimit;
   433    439       if( pSelect->iOffset ){
   434    440         iLimit = pSelect->iOffset+1;
................................................................................
   889    895     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
   890    896       pseudoTab = pParse->nTab++;
   891    897       sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
   892    898       regRowid = 0;
   893    899     }else{
   894    900       regRowid = sqlite3GetTempReg(pParse);
   895    901     }
   896         -  addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
   897         -  codeOffset(v, p, addrContinue);
   898         -  sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
          902  +  if( p->selFlags & SF_UseSorter ){
          903  +    int regSortOut = ++pParse->nMem;
          904  +    int ptab2 = pParse->nTab++;
          905  +    sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
          906  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
          907  +    codeOffset(v, p, addrContinue);
          908  +    sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
          909  +    sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
          910  +    sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
          911  +  }else{
          912  +    addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
          913  +    codeOffset(v, p, addrContinue);
          914  +    sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
          915  +  }
   899    916     switch( eDest ){
   900    917       case SRT_Table:
   901    918       case SRT_EphemTab: {
   902    919         testcase( eDest==SRT_Table );
   903    920         testcase( eDest==SRT_EphemTab );
   904    921         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
   905    922         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
................................................................................
   944    961     }
   945    962     sqlite3ReleaseTempReg(pParse, regRow);
   946    963     sqlite3ReleaseTempReg(pParse, regRowid);
   947    964   
   948    965     /* The bottom of the loop
   949    966     */
   950    967     sqlite3VdbeResolveLabel(v, addrContinue);
   951         -  sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
          968  +  if( p->selFlags & SF_UseSorter ){
          969  +    sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
          970  +  }else{
          971  +    sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
          972  +  }
   952    973     sqlite3VdbeResolveLabel(v, addrBreak);
   953    974     if( eDest==SRT_Output || eDest==SRT_Coroutine ){
   954    975       sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
   955    976     }
   956    977   }
   957    978   
   958    979   /*
................................................................................
  3910   3931     }
  3911   3932   
  3912   3933     /* Set the limiter.
  3913   3934     */
  3914   3935     iEnd = sqlite3VdbeMakeLabel(v);
  3915   3936     p->nSelectRow = (double)LARGEST_INT64;
  3916   3937     computeLimitRegisters(pParse, p, iEnd);
         3938  +  if( p->iLimit==0 && addrSortIndex>=0 ){
         3939  +    sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
         3940  +    p->selFlags |= SF_UseSorter;
         3941  +  }
  3917   3942   
  3918   3943     /* Open a virtual index to use for the distinct set.
  3919   3944     */
  3920   3945     if( p->selFlags & SF_Distinct ){
  3921   3946       KeyInfo *pKeyInfo;
  3922   3947       distinct = pParse->nTab++;
  3923   3948       pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
................................................................................
  3945   3970         sqlite3VdbeChangeToNoop(v, addrSortIndex, 1);
  3946   3971         p->addrOpenEphm[2] = -1;
  3947   3972       }
  3948   3973   
  3949   3974       if( pWInfo->eDistinct ){
  3950   3975         VdbeOp *pOp;                /* No longer required OpenEphemeral instr. */
  3951   3976        
  3952         -      assert( addrDistinctIndex>0 );
         3977  +      assert( addrDistinctIndex>=0 );
  3953   3978         pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
  3954   3979   
  3955   3980         assert( isDistinct );
  3956   3981         assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED 
  3957   3982              || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE 
  3958   3983         );
  3959   3984         distinct = -1;
................................................................................
  4004   4029       int iBMem;          /* First Mem address for previous GROUP BY */
  4005   4030       int iUseFlag;       /* Mem address holding flag indicating that at least
  4006   4031                           ** one row of the input to the aggregator has been
  4007   4032                           ** processed */
  4008   4033       int iAbortFlag;     /* Mem address which causes query abort if positive */
  4009   4034       int groupBySort;    /* Rows come from source in GROUP BY order */
  4010   4035       int addrEnd;        /* End of processing for this SELECT */
         4036  +    int sortPTab = 0;   /* Pseudotable used to decode sorting results */
         4037  +    int sortOut = 0;    /* Output register from the sorter */
  4011   4038   
  4012   4039       /* Remove any and all aliases between the result set and the
  4013   4040       ** GROUP BY clause.
  4014   4041       */
  4015   4042       if( pGroupBy ){
  4016   4043         int k;                        /* Loop counter */
  4017   4044         struct ExprList_item *pItem;  /* For looping over expression in a list */
................................................................................
  4065   4092         int addrTopOfLoop;  /* Top of the input loop */
  4066   4093         int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
  4067   4094         int addrReset;      /* Subroutine for resetting the accumulator */
  4068   4095         int regReset;       /* Return address register for reset subroutine */
  4069   4096   
  4070   4097         /* If there is a GROUP BY clause we might need a sorting index to
  4071   4098         ** implement it.  Allocate that sorting index now.  If it turns out
  4072         -      ** that we do not need it after all, the OpenEphemeral instruction
         4099  +      ** that we do not need it after all, the OP_SorterOpen instruction
  4073   4100         ** will be converted into a Noop.  
  4074   4101         */
  4075   4102         sAggInfo.sortingIdx = pParse->nTab++;
  4076   4103         pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
  4077         -      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, 
         4104  +      addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen, 
  4078   4105             sAggInfo.sortingIdx, sAggInfo.nSortingColumn, 
  4079   4106             0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
  4080   4107   
  4081   4108         /* Initialize memory locations used by GROUP BY aggregate processing
  4082   4109         */
  4083   4110         iUseFlag = ++pParse->nMem;
  4084   4111         iAbortFlag = ++pParse->nMem;
................................................................................
  4151   4178                 sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
  4152   4179               }
  4153   4180               j++;
  4154   4181             }
  4155   4182           }
  4156   4183           regRecord = sqlite3GetTempReg(pParse);
  4157   4184           sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
  4158         -        sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
         4185  +        sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
  4159   4186           sqlite3ReleaseTempReg(pParse, regRecord);
  4160   4187           sqlite3ReleaseTempRange(pParse, regBase, nCol);
  4161   4188           sqlite3WhereEnd(pWInfo);
  4162         -        sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
         4189  +        sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
         4190  +        sortOut = sqlite3GetTempReg(pParse);
         4191  +        sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
         4192  +        sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
  4163   4193           VdbeComment((v, "GROUP BY sort"));
  4164   4194           sAggInfo.useSortingIdx = 1;
  4165   4195           sqlite3ExprCacheClear(pParse);
  4166   4196         }
  4167   4197   
  4168   4198         /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
  4169   4199         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
  4170   4200         ** Then compare the current GROUP BY terms against the GROUP BY terms
  4171   4201         ** from the previous row currently stored in a0, a1, a2...
  4172   4202         */
  4173   4203         addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
  4174   4204         sqlite3ExprCacheClear(pParse);
         4205  +      if( groupBySort ){
         4206  +        sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
         4207  +      }
  4175   4208         for(j=0; j<pGroupBy->nExpr; j++){
  4176   4209           if( groupBySort ){
  4177         -          sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
         4210  +          sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
         4211  +          if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
  4178   4212           }else{
  4179   4213             sAggInfo.directMode = 1;
  4180   4214             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
  4181   4215           }
  4182   4216         }
  4183   4217         sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
  4184   4218                             (char*)pKeyInfo, P4_KEYINFO);
................................................................................
  4209   4243         updateAccumulator(pParse, &sAggInfo);
  4210   4244         sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
  4211   4245         VdbeComment((v, "indicate data in accumulator"));
  4212   4246   
  4213   4247         /* End of the loop
  4214   4248         */
  4215   4249         if( groupBySort ){
  4216         -        sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
         4250  +        sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
  4217   4251         }else{
  4218   4252           sqlite3WhereEnd(pWInfo);
  4219   4253           sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
  4220   4254         }
  4221   4255   
  4222   4256         /* Output the final row of result
  4223   4257         */

Changes to src/sqliteInt.h.

   368    368   ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
   369    369   ** on the command-line
   370    370   */
   371    371   #ifndef SQLITE_TEMP_STORE
   372    372   # define SQLITE_TEMP_STORE 1
   373    373   #endif
   374    374   
   375         -/*
   376         -** If all temporary storage is in-memory, then omit the external merge-sort
   377         -** logic since it is superfluous.
   378         -*/
   379         -#if SQLITE_TEMP_STORE==3 && !defined(SQLITE_OMIT_MERGE_SORT)
   380         -# define SQLITE_OMIT_MERGE_SORT
   381         -#endif
   382         -
   383    375   /*
   384    376   ** GCC does not define the offsetof() macro so we'll have to do it
   385    377   ** ourselves.
   386    378   */
   387    379   #ifndef offsetof
   388    380   #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
   389    381   #endif
................................................................................
  1566   1558   */
  1567   1559   struct AggInfo {
  1568   1560     u8 directMode;          /* Direct rendering mode means take data directly
  1569   1561                             ** from source tables rather than from accumulators */
  1570   1562     u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
  1571   1563                             ** than the source table */
  1572   1564     int sortingIdx;         /* Cursor number of the sorting index */
         1565  +  int sortingIdxPTab;     /* Cursor number of pseudo-table */
  1573   1566     ExprList *pGroupBy;     /* The group by clause */
  1574   1567     int nSortingColumn;     /* Number of columns in the sorting index */
  1575   1568     struct AggInfo_col {    /* For each column used in source tables */
  1576   1569       Table *pTab;             /* Source table */
  1577   1570       int iTable;              /* Cursor number of the source table */
  1578   1571       int iColumn;             /* Column number within the source table */
  1579   1572       int iSorterColumn;       /* Column number in the sorting index */
................................................................................
  2098   2091   */
  2099   2092   #define SF_Distinct        0x0001  /* Output should be DISTINCT */
  2100   2093   #define SF_Resolved        0x0002  /* Identifiers have been resolved */
  2101   2094   #define SF_Aggregate       0x0004  /* Contains aggregate functions */
  2102   2095   #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
  2103   2096   #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
  2104   2097   #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
         2098  +#define SF_UseSorter       0x0040  /* Sort using a sorter */
  2105   2099   
  2106   2100   
  2107   2101   /*
  2108   2102   ** The results of a select can be distributed in several ways.  The
  2109   2103   ** "SRT" prefix means "SELECT Result Type".
  2110   2104   */
  2111   2105   #define SRT_Union        1  /* Store result as keys in an index */

Changes to src/test1.c.

  4391   4391   
  4392   4392   /*
  4393   4393   ** Fill the stack with a known bitpattern.
  4394   4394   */
  4395   4395   static void prepStack(void){
  4396   4396     int i;
  4397   4397     u32 bigBuf[65536];
  4398         -  for(i=0; i<sizeof(bigBuf); i++) bigBuf[i] = 0xdeadbeef;
         4398  +  for(i=0; i<sizeof(bigBuf)/sizeof(bigBuf[0]); i++) bigBuf[i] = 0xdeadbeef;
  4399   4399     sqlite3_stack_baseline = (u8*)&bigBuf[65536];
  4400   4400   }
  4401   4401   
  4402   4402   /*
  4403   4403   ** Get the current stack depth.  Used for debugging only.
  4404   4404   */
  4405   4405   u64 sqlite3StackDepth(void){

Changes to src/test_thread.c.

   278    278     extern void Md5_Register(sqlite3*);
   279    279   
   280    280     UNUSED_PARAMETER(clientData);
   281    281     UNUSED_PARAMETER(objc);
   282    282   
   283    283     zFilename = Tcl_GetString(objv[2]);
   284    284     rc = sqlite3_open(zFilename, &db);
          285  +#ifdef SQLITE_HAS_CODEC
          286  +  if( db && objc>=4 ){
          287  +    const char *zKey;
          288  +    int nKey;
          289  +    zKey = Tcl_GetStringFromObj(objv[3], &nKey);
          290  +    rc = sqlite3_key(db, zKey, nKey);
          291  +    if( rc!=SQLITE_OK ){
          292  +      char *zErrMsg = sqlite3_mprintf("error %d: %s", rc, sqlite3_errmsg(db));
          293  +      sqlite3_close(db);
          294  +      Tcl_AppendResult(interp, zErrMsg, (char*)0);
          295  +      sqlite3_free(zErrMsg);
          296  +      return TCL_ERROR;
          297  +    }
          298  +  }
          299  +#endif
   285    300     Md5_Register(db);
   286    301     sqlite3_busy_handler(db, xBusy, 0);
   287    302     
   288    303     if( sqlite3TestMakePointerStr(interp, zBuf, db) ) return TCL_ERROR;
   289    304     Tcl_AppendResult(interp, zBuf, 0);
   290    305   
   291    306     return TCL_OK;
................................................................................
   345    360   
   346    361     rc = Tcl_GetIndexFromObjStruct(
   347    362         interp, objv[1], aSub, sizeof(aSub[0]), "sub-command", 0, &iIndex
   348    363     );
   349    364     if( rc!=TCL_OK ) return rc;
   350    365     pSub = &aSub[iIndex];
   351    366   
   352         -  if( objc!=(pSub->nArg+2) ){
          367  +  if( objc<(pSub->nArg+2) ){
   353    368       Tcl_WrongNumArgs(interp, 2, objv, pSub->zUsage);
   354    369       return TCL_ERROR;
   355    370     }
   356    371   
   357    372     return pSub->xProc(clientData, interp, objc, objv);
   358    373   }
   359    374   

Changes to src/vdbe.c.

   669    669       */
   670    670       assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
   671    671       if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
   672    672         assert( pOp->p2>0 );
   673    673         assert( pOp->p2<=p->nMem );
   674    674         pOut = &aMem[pOp->p2];
   675    675         memAboutToChange(p, pOut);
   676         -      sqlite3VdbeMemReleaseExternal(pOut);
          676  +      MemReleaseExt(pOut);
   677    677         pOut->flags = MEM_Int;
   678    678       }
   679    679   
   680    680       /* Sanity checking on other operands */
   681    681   #ifdef SQLITE_DEBUG
   682    682       if( (pOp->opflags & OPFLG_IN1)!=0 ){
   683    683         assert( pOp->p1>0 );
................................................................................
  2114   2114     Mem sMem;          /* For storing the record being decoded */
  2115   2115     u8 *zIdx;          /* Index into header */
  2116   2116     u8 *zEndHdr;       /* Pointer to first byte after the header */
  2117   2117     u32 offset;        /* Offset into the data */
  2118   2118     u32 szField;       /* Number of bytes in the content of a field */
  2119   2119     int szHdr;         /* Size of the header size field at start of record */
  2120   2120     int avail;         /* Number of bytes of available data */
         2121  +  u32 t;             /* A type code from the record header */
  2121   2122     Mem *pReg;         /* PseudoTable input register */
  2122   2123   
  2123   2124   
  2124   2125     p1 = pOp->p1;
  2125   2126     p2 = pOp->p2;
  2126   2127     pC = 0;
  2127   2128     memset(&sMem, 0, sizeof(sMem));
  2128   2129     assert( p1<p->nCursor );
  2129   2130     assert( pOp->p3>0 && pOp->p3<=p->nMem );
  2130   2131     pDest = &aMem[pOp->p3];
  2131   2132     memAboutToChange(p, pDest);
  2132         -  MemSetTypeFlag(pDest, MEM_Null);
  2133   2133     zRec = 0;
  2134   2134   
  2135   2135     /* This block sets the variable payloadSize to be the total number of
  2136   2136     ** bytes in the record.
  2137   2137     **
  2138   2138     ** zRec is set to be the complete text of the record if it is available.
  2139   2139     ** The complete record text is always available for pseudo-tables
................................................................................
  2169   2169         assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
  2170   2170         payloadSize = (u32)payloadSize64;
  2171   2171       }else{
  2172   2172         assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2173   2173         rc = sqlite3BtreeDataSize(pCrsr, &payloadSize);
  2174   2174         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  2175   2175       }
  2176         -  }else if( pC->pseudoTableReg>0 ){
         2176  +  }else if( ALWAYS(pC->pseudoTableReg>0) ){
  2177   2177       pReg = &aMem[pC->pseudoTableReg];
  2178   2178       assert( pReg->flags & MEM_Blob );
  2179   2179       assert( memIsValid(pReg) );
  2180   2180       payloadSize = pReg->n;
  2181   2181       zRec = pReg->z;
  2182   2182       pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  2183   2183       assert( payloadSize==0 || zRec!=0 );
  2184   2184     }else{
  2185   2185       /* Consider the row to be NULL */
  2186   2186       payloadSize = 0;
  2187   2187     }
  2188   2188   
  2189         -  /* If payloadSize is 0, then just store a NULL */
         2189  +  /* If payloadSize is 0, then just store a NULL.  This can happen because of
         2190  +  ** nullRow or because of a corrupt database. */
  2190   2191     if( payloadSize==0 ){
  2191         -    assert( pDest->flags&MEM_Null );
         2192  +    MemSetTypeFlag(pDest, MEM_Null);
  2192   2193       goto op_column_out;
  2193   2194     }
  2194   2195     assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
  2195   2196     if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2196   2197       goto too_big;
  2197   2198     }
  2198   2199   
................................................................................
  2291   2292       ** arrays.  aType[i] will contain the type integer for the i-th
  2292   2293       ** column and aOffset[i] will contain the offset from the beginning
  2293   2294       ** of the record to the start of the data for the i-th column
  2294   2295       */
  2295   2296       for(i=0; i<nField; i++){
  2296   2297         if( zIdx<zEndHdr ){
  2297   2298           aOffset[i] = offset;
  2298         -        zIdx += getVarint32(zIdx, aType[i]);
  2299         -        szField = sqlite3VdbeSerialTypeLen(aType[i]);
         2299  +        if( zIdx[0]<0x80 ){
         2300  +          t = zIdx[0];
         2301  +          zIdx++;
         2302  +        }else{
         2303  +          zIdx += sqlite3GetVarint32(zIdx, &t);
         2304  +        }
         2305  +        aType[i] = t;
         2306  +        szField = sqlite3VdbeSerialTypeLen(t);
  2300   2307           offset += szField;
  2301   2308           if( offset<szField ){  /* True if offset overflows */
  2302   2309             zIdx = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
  2303   2310             break;
  2304   2311           }
  2305   2312         }else{
  2306   2313           /* If i is less that nField, then there are less fields in this
................................................................................
  2333   2340     ** then there are not enough fields in the record to satisfy the
  2334   2341     ** request.  In this case, set the value NULL or to P4 if P4 is
  2335   2342     ** a pointer to a Mem object.
  2336   2343     */
  2337   2344     if( aOffset[p2] ){
  2338   2345       assert( rc==SQLITE_OK );
  2339   2346       if( zRec ){
  2340         -      sqlite3VdbeMemReleaseExternal(pDest);
         2347  +      MemReleaseExt(pDest);
  2341   2348         sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
  2342   2349       }else{
  2343   2350         len = sqlite3VdbeSerialTypeLen(aType[p2]);
  2344   2351         sqlite3VdbeMemMove(&sMem, pDest);
  2345   2352         rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, pC->isIndex, &sMem);
  2346   2353         if( rc!=SQLITE_OK ){
  2347   2354           goto op_column_out;
................................................................................
  2350   2357         sqlite3VdbeSerialGet((u8*)zData, aType[p2], pDest);
  2351   2358       }
  2352   2359       pDest->enc = encoding;
  2353   2360     }else{
  2354   2361       if( pOp->p4type==P4_MEM ){
  2355   2362         sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
  2356   2363       }else{
  2357         -      assert( pDest->flags&MEM_Null );
         2364  +      MemSetTypeFlag(pDest, MEM_Null);
  2358   2365       }
  2359   2366     }
  2360   2367   
  2361   2368     /* If we dynamically allocated space to hold the data (in the
  2362   2369     ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  2363   2370     ** dynamically allocated space over to the pDest structure.
  2364   2371     ** This prevents a memory copy.
................................................................................
  2546   2553   */
  2547   2554   #ifndef SQLITE_OMIT_BTREECOUNT
  2548   2555   case OP_Count: {         /* out2-prerelease */
  2549   2556     i64 nEntry;
  2550   2557     BtCursor *pCrsr;
  2551   2558   
  2552   2559     pCrsr = p->apCsr[pOp->p1]->pCursor;
  2553         -  if( pCrsr ){
         2560  +  if( ALWAYS(pCrsr) ){
  2554   2561       rc = sqlite3BtreeCount(pCrsr, &nEntry);
  2555   2562     }else{
  2556   2563       nEntry = 0;
  2557   2564     }
  2558   2565     pOut->u.i = nEntry;
  2559   2566     break;
  2560   2567   }
................................................................................
  3108   3115     pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
  3109   3116     if( pCur==0 ) goto no_mem;
  3110   3117     pCur->nullRow = 1;
  3111   3118     pCur->isOrdered = 1;
  3112   3119     rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
  3113   3120     pCur->pKeyInfo = pKeyInfo;
  3114   3121   
  3115         -  /* Since it performs no memory allocation or IO, the only values that
  3116         -  ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK. 
  3117         -  ** SQLITE_EMPTY is only returned when attempting to open the table
  3118         -  ** rooted at page 1 of a zero-byte database.  */
  3119         -  assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
  3120         -  if( rc==SQLITE_EMPTY ){
  3121         -    pCur->pCursor = 0;
  3122         -    rc = SQLITE_OK;
  3123         -  }
         3122  +  /* Since it performs no memory allocation or IO, the only value that
         3123  +  ** sqlite3BtreeCursor() may return is SQLITE_OK. */
         3124  +  assert( rc==SQLITE_OK );
  3124   3125   
  3125   3126     /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
  3126   3127     ** SQLite used to check if the root-page flags were sane at this point
  3127   3128     ** and report database corruption if they were not, but this check has
  3128   3129     ** since moved into the btree layer.  */  
  3129   3130     pCur->isTable = pOp->p4type!=P4_KEYINFO;
  3130   3131     pCur->isIndex = !pCur->isTable;
................................................................................
  3157   3158   /* Opcode: OpenAutoindex P1 P2 * P4 *
  3158   3159   **
  3159   3160   ** This opcode works the same as OP_OpenEphemeral.  It has a
  3160   3161   ** different name to distinguish its use.  Tables created using
  3161   3162   ** by this opcode will be used for automatically created transient
  3162   3163   ** indices in joins.
  3163   3164   */
  3164         -/* Opcode: OpenSorter P1 P2 * P4 *
  3165         -**
  3166         -** This opcode works like OP_OpenEphemeral except that it opens
  3167         -** a transient index that is specifically designed to sort large
  3168         -** tables using an external merge-sort algorithm.
  3169         -*/
  3170         -case OP_OpenSorter: 
  3171   3165   case OP_OpenAutoindex: 
  3172   3166   case OP_OpenEphemeral: {
  3173   3167     VdbeCursor *pCx;
  3174   3168     static const int vfsFlags = 
  3175   3169         SQLITE_OPEN_READWRITE |
  3176   3170         SQLITE_OPEN_CREATE |
  3177   3171         SQLITE_OPEN_EXCLUSIVE |
  3178   3172         SQLITE_OPEN_DELETEONCLOSE |
  3179   3173         SQLITE_OPEN_TRANSIENT_DB;
  3180   3174   
  3181   3175     assert( pOp->p1>=0 );
  3182         -  assert( (pOp->opcode==OP_OpenSorter)==((pOp->p5 & BTREE_SORTER)!=0) );
  3183   3176     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3184   3177     if( pCx==0 ) goto no_mem;
  3185   3178     pCx->nullRow = 1;
  3186   3179     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
  3187   3180                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  3188   3181     if( rc==SQLITE_OK ){
  3189   3182       rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
................................................................................
  3209   3202       }else{
  3210   3203         rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
  3211   3204         pCx->isTable = 1;
  3212   3205       }
  3213   3206     }
  3214   3207     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3215   3208     pCx->isIndex = !pCx->isTable;
         3209  +  break;
         3210  +}
         3211  +
         3212  +/* Opcode: OpenSorter P1 P2 * P4 *
         3213  +**
         3214  +** This opcode works like OP_OpenEphemeral except that it opens
         3215  +** a transient index that is specifically designed to sort large
         3216  +** tables using an external merge-sort algorithm.
         3217  +*/
         3218  +case OP_SorterOpen: {
         3219  +  VdbeCursor *pCx;
  3216   3220   #ifndef SQLITE_OMIT_MERGE_SORT
  3217         -  if( rc==SQLITE_OK && pOp->opcode==OP_OpenSorter ){
  3218         -    rc = sqlite3VdbeSorterInit(db, pCx);
  3219         -  }
         3221  +  pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
         3222  +  if( pCx==0 ) goto no_mem;
         3223  +  pCx->pKeyInfo = pOp->p4.pKeyInfo;
         3224  +  pCx->pKeyInfo->enc = ENC(p->db);
         3225  +  pCx->isSorter = 1;
         3226  +  rc = sqlite3VdbeSorterInit(db, pCx);
         3227  +#else
         3228  +  pOp->opcode = OP_OpenEphemeral;
         3229  +  pc--;
  3220   3230   #endif
  3221   3231     break;
  3222   3232   }
  3223   3233   
  3224   3234   /* Opcode: OpenPseudo P1 P2 P3 * *
  3225   3235   **
  3226   3236   ** Open a new cursor that points to a fake table that contains a single
................................................................................
  3329   3339     pC = p->apCsr[pOp->p1];
  3330   3340     assert( pC!=0 );
  3331   3341     assert( pC->pseudoTableReg==0 );
  3332   3342     assert( OP_SeekLe == OP_SeekLt+1 );
  3333   3343     assert( OP_SeekGe == OP_SeekLt+2 );
  3334   3344     assert( OP_SeekGt == OP_SeekLt+3 );
  3335   3345     assert( pC->isOrdered );
  3336         -  if( pC->pCursor!=0 ){
         3346  +  if( ALWAYS(pC->pCursor!=0) ){
  3337   3347       oc = pOp->opcode;
  3338   3348       pC->nullRow = 0;
  3339   3349       if( pC->isTable ){
  3340   3350         /* The input value in P3 might be of any type: integer, real, string,
  3341   3351         ** blob, or NULL.  But it needs to be an integer before we can do
  3342   3352         ** the seek, so covert it. */
  3343   3353         pIn3 = &aMem[pOp->p3];
................................................................................
  3517   3527   ** See also: Found, NotExists, IsUnique
  3518   3528   */
  3519   3529   case OP_NotFound:       /* jump, in3 */
  3520   3530   case OP_Found: {        /* jump, in3 */
  3521   3531     int alreadyExists;
  3522   3532     VdbeCursor *pC;
  3523   3533     int res;
         3534  +  char *pFree;
  3524   3535     UnpackedRecord *pIdxKey;
  3525   3536     UnpackedRecord r;
  3526   3537     char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
  3527   3538   
  3528   3539   #ifdef SQLITE_TEST
  3529   3540     sqlite3_found_count++;
  3530   3541   #endif
................................................................................
  3544   3555         r.aMem = pIn3;
  3545   3556   #ifdef SQLITE_DEBUG
  3546   3557         { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
  3547   3558   #endif
  3548   3559         r.flags = UNPACKED_PREFIX_MATCH;
  3549   3560         pIdxKey = &r;
  3550   3561       }else{
         3562  +      pIdxKey = sqlite3VdbeAllocUnpackedRecord(
         3563  +          pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
         3564  +      ); 
         3565  +      if( pIdxKey==0 ) goto no_mem;
  3551   3566         assert( pIn3->flags & MEM_Blob );
  3552   3567         assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
  3553         -      pIdxKey = sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z,
  3554         -                                        aTempRec, sizeof(aTempRec));
  3555         -      if( pIdxKey==0 ){
  3556         -        goto no_mem;
  3557         -      }
         3568  +      sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
  3558   3569         pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
  3559   3570       }
  3560   3571       rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
  3561   3572       if( pOp->p4.i==0 ){
  3562         -      sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
         3573  +      sqlite3DbFree(db, pFree);
  3563   3574       }
  3564   3575       if( rc!=SQLITE_OK ){
  3565   3576         break;
  3566   3577       }
  3567   3578       alreadyExists = (res==0);
  3568   3579       pC->deferredMoveto = 0;
  3569   3580       pC->cacheStatus = CACHE_STALE;
................................................................................
  3687   3698     assert( pIn3->flags & MEM_Int );
  3688   3699     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  3689   3700     pC = p->apCsr[pOp->p1];
  3690   3701     assert( pC!=0 );
  3691   3702     assert( pC->isTable );
  3692   3703     assert( pC->pseudoTableReg==0 );
  3693   3704     pCrsr = pC->pCursor;
  3694         -  if( pCrsr!=0 ){
         3705  +  if( ALWAYS(pCrsr!=0) ){
  3695   3706       res = 0;
  3696   3707       iKey = pIn3->u.i;
  3697   3708       rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
  3698   3709       pC->lastRowid = pIn3->u.i;
  3699   3710       pC->rowidIsValid = res==0 ?1:0;
  3700   3711       pC->nullRow = 0;
  3701   3712       pC->cacheStatus = CACHE_STALE;
................................................................................
  4064   4075   ** This is used by trigger programs.
  4065   4076   */
  4066   4077   case OP_ResetCount: {
  4067   4078     sqlite3VdbeSetChanges(db, p->nChange);
  4068   4079     p->nChange = 0;
  4069   4080     break;
  4070   4081   }
         4082  +
         4083  +/* Opcode: SorterCompare P1 P2 P3
         4084  +**
         4085  +** P1 is a sorter cursor. This instruction compares the record blob in 
         4086  +** register P3 with the entry that the sorter cursor currently points to.
         4087  +** If, excluding the rowid fields at the end, the two records are a match,
         4088  +** fall through to the next instruction. Otherwise, jump to instruction P2.
         4089  +*/
         4090  +case OP_SorterCompare: {
         4091  +  VdbeCursor *pC;
         4092  +  int res;
         4093  +
         4094  +  pC = p->apCsr[pOp->p1];
         4095  +  assert( isSorter(pC) );
         4096  +  pIn3 = &aMem[pOp->p3];
         4097  +  rc = sqlite3VdbeSorterCompare(pC, pIn3, &res);
         4098  +  if( res ){
         4099  +    pc = pOp->p2-1;
         4100  +  }
         4101  +  break;
         4102  +};
         4103  +
         4104  +/* Opcode: SorterData P1 P2 * * *
         4105  +**
         4106  +** Write into register P2 the current sorter data for sorter cursor P1.
         4107  +*/
         4108  +case OP_SorterData: {
         4109  +  VdbeCursor *pC;
         4110  +#ifndef SQLITE_OMIT_MERGE_SORT
         4111  +  pOut = &aMem[pOp->p2];
         4112  +  pC = p->apCsr[pOp->p1];
         4113  +  assert( pC->isSorter );
         4114  +  rc = sqlite3VdbeSorterRowkey(pC, pOut);
         4115  +#else
         4116  +  pOp->opcode = OP_RowKey;
         4117  +  pc--;
         4118  +#endif
         4119  +  break;
         4120  +}
  4071   4121   
  4072   4122   /* Opcode: RowData P1 P2 * * *
  4073   4123   **
  4074   4124   ** Write into register P2 the complete row data for cursor P1.
  4075   4125   ** There is no interpretation of the data.  
  4076   4126   ** It is just copied onto the P2 register exactly as 
  4077   4127   ** it is found in the database file.
................................................................................
  4098   4148   
  4099   4149     pOut = &aMem[pOp->p2];
  4100   4150     memAboutToChange(p, pOut);
  4101   4151   
  4102   4152     /* Note that RowKey and RowData are really exactly the same instruction */
  4103   4153     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4104   4154     pC = p->apCsr[pOp->p1];
  4105         -  assert( pC->isTable || pOp->opcode==OP_RowKey );
         4155  +  assert( pC->isSorter==0 );
         4156  +  assert( pC->isTable || pOp->opcode!=OP_RowData );
  4106   4157     assert( pC->isIndex || pOp->opcode==OP_RowData );
  4107   4158     assert( pC!=0 );
  4108   4159     assert( pC->nullRow==0 );
  4109   4160     assert( pC->pseudoTableReg==0 );
  4110         -
  4111         -  if( isSorter(pC) ){
  4112         -    assert( pOp->opcode==OP_RowKey );
  4113         -    rc = sqlite3VdbeSorterRowkey(pC, pOut);
  4114         -    break;
  4115         -  }
  4116         -
         4161  +  assert( !pC->isSorter );
  4117   4162     assert( pC->pCursor!=0 );
  4118   4163     pCrsr = pC->pCursor;
  4119   4164     assert( sqlite3BtreeCursorIsValid(pCrsr) );
  4120   4165   
  4121   4166     /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
  4122   4167     ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
  4123   4168     ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
................................................................................
  4214   4259     VdbeCursor *pC;
  4215   4260   
  4216   4261     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4217   4262     pC = p->apCsr[pOp->p1];
  4218   4263     assert( pC!=0 );
  4219   4264     pC->nullRow = 1;
  4220   4265     pC->rowidIsValid = 0;
         4266  +  assert( pC->pCursor || pC->pVtabCursor );
  4221   4267     if( pC->pCursor ){
  4222   4268       sqlite3BtreeClearCursor(pC->pCursor);
  4223   4269     }
  4224   4270     break;
  4225   4271   }
  4226   4272   
  4227   4273   /* Opcode: Last P1 P2 * * *
................................................................................
  4237   4283     BtCursor *pCrsr;
  4238   4284     int res;
  4239   4285   
  4240   4286     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4241   4287     pC = p->apCsr[pOp->p1];
  4242   4288     assert( pC!=0 );
  4243   4289     pCrsr = pC->pCursor;
  4244         -  if( pCrsr==0 ){
         4290  +  if( NEVER(pCrsr==0) ){
  4245   4291       res = 1;
  4246   4292     }else{
  4247   4293       rc = sqlite3BtreeLast(pCrsr, &res);
  4248   4294     }
  4249   4295     pC->nullRow = (u8)res;
  4250   4296     pC->deferredMoveto = 0;
  4251   4297     pC->rowidIsValid = 0;
................................................................................
  4265   4311   ** Sorting is accomplished by writing records into a sorting index,
  4266   4312   ** then rewinding that index and playing it back from beginning to
  4267   4313   ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
  4268   4314   ** rewinding so that the global variable will be incremented and
  4269   4315   ** regression tests can determine whether or not the optimizer is
  4270   4316   ** correctly optimizing out sorts.
  4271   4317   */
         4318  +case OP_SorterSort:    /* jump */
         4319  +#ifdef SQLITE_OMIT_MERGE_SORT
         4320  +  pOp->opcode = OP_Sort;
         4321  +#endif
  4272   4322   case OP_Sort: {        /* jump */
  4273   4323   #ifdef SQLITE_TEST
  4274   4324     sqlite3_sort_count++;
  4275   4325     sqlite3_search_count--;
  4276   4326   #endif
  4277   4327     p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
  4278   4328     /* Fall through into OP_Rewind */
................................................................................
  4289   4339     VdbeCursor *pC;
  4290   4340     BtCursor *pCrsr;
  4291   4341     int res;
  4292   4342   
  4293   4343     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4294   4344     pC = p->apCsr[pOp->p1];
  4295   4345     assert( pC!=0 );
         4346  +  assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
  4296   4347     res = 1;
  4297   4348     if( isSorter(pC) ){
  4298   4349       rc = sqlite3VdbeSorterRewind(db, pC, &res);
  4299         -  }else if( (pCrsr = pC->pCursor)!=0 ){
         4350  +  }else{
         4351  +    pCrsr = pC->pCursor;
         4352  +    assert( pCrsr );
  4300   4353       rc = sqlite3BtreeFirst(pCrsr, &res);
  4301   4354       pC->atFirst = res==0 ?1:0;
  4302   4355       pC->deferredMoveto = 0;
  4303   4356       pC->cacheStatus = CACHE_STALE;
  4304   4357       pC->rowidIsValid = 0;
  4305   4358     }
  4306   4359     pC->nullRow = (u8)res;
................................................................................
  4307   4360     assert( pOp->p2>0 && pOp->p2<p->nOp );
  4308   4361     if( res ){
  4309   4362       pc = pOp->p2 - 1;
  4310   4363     }
  4311   4364     break;
  4312   4365   }
  4313   4366   
  4314         -/* Opcode: Next P1 P2 * * P5
         4367  +/* Opcode: Next P1 P2 * P4 P5
  4315   4368   **
  4316   4369   ** Advance cursor P1 so that it points to the next key/data pair in its
  4317   4370   ** table or index.  If there are no more key/value pairs then fall through
  4318   4371   ** to the following instruction.  But if the cursor advance was successful,
  4319   4372   ** jump immediately to P2.
  4320   4373   **
  4321   4374   ** The P1 cursor must be for a real table, not a pseudo-table.
         4375  +**
         4376  +** P4 is always of type P4_ADVANCE. The function pointer points to
         4377  +** sqlite3BtreeNext().
  4322   4378   **
  4323   4379   ** If P5 is positive and the jump is taken, then event counter
  4324   4380   ** number P5-1 in the prepared statement is incremented.
  4325   4381   **
  4326   4382   ** See also: Prev
  4327   4383   */
  4328   4384   /* Opcode: Prev P1 P2 * * P5
................................................................................
  4329   4385   **
  4330   4386   ** Back up cursor P1 so that it points to the previous key/data pair in its
  4331   4387   ** table or index.  If there is no previous key/value pairs then fall through
  4332   4388   ** to the following instruction.  But if the cursor backup was successful,
  4333   4389   ** jump immediately to P2.
  4334   4390   **
  4335   4391   ** The P1 cursor must be for a real table, not a pseudo-table.
         4392  +**
         4393  +** P4 is always of type P4_ADVANCE. The function pointer points to
         4394  +** sqlite3BtreePrevious().
  4336   4395   **
  4337   4396   ** If P5 is positive and the jump is taken, then event counter
  4338   4397   ** number P5-1 in the prepared statement is incremented.
  4339   4398   */
         4399  +case OP_SorterNext:    /* jump */
         4400  +#ifdef SQLITE_OMIT_MERGE_SORT
         4401  +  pOp->opcode = OP_Next;
         4402  +#endif
  4340   4403   case OP_Prev:          /* jump */
  4341   4404   case OP_Next: {        /* jump */
  4342   4405     VdbeCursor *pC;
  4343         -  BtCursor *pCrsr;
  4344   4406     int res;
  4345   4407   
  4346   4408     CHECK_FOR_INTERRUPT;
  4347   4409     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4348   4410     assert( pOp->p5<=ArraySize(p->aCounter) );
  4349   4411     pC = p->apCsr[pOp->p1];
  4350   4412     if( pC==0 ){
  4351   4413       break;  /* See ticket #2273 */
  4352   4414     }
         4415  +  assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
  4353   4416     if( isSorter(pC) ){
  4354         -    assert( pOp->opcode==OP_Next );
         4417  +    assert( pOp->opcode==OP_SorterNext );
  4355   4418       rc = sqlite3VdbeSorterNext(db, pC, &res);
  4356   4419     }else{
  4357         -    pCrsr = pC->pCursor;
  4358         -    if( pCrsr==0 ){
  4359         -      pC->nullRow = 1;
  4360         -      break;
  4361         -    }
  4362   4420       res = 1;
  4363   4421       assert( pC->deferredMoveto==0 );
  4364         -    rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(pCrsr, &res) :
  4365         -                                sqlite3BtreePrevious(pCrsr, &res);
         4422  +    assert( pC->pCursor );
         4423  +    assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
         4424  +    assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
         4425  +    rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4366   4426     }
  4367   4427     pC->nullRow = (u8)res;
  4368   4428     pC->cacheStatus = CACHE_STALE;
  4369   4429     if( res==0 ){
  4370   4430       pc = pOp->p2 - 1;
  4371   4431       if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
  4372   4432   #ifdef SQLITE_TEST
................................................................................
  4385   4445   **
  4386   4446   ** P3 is a flag that provides a hint to the b-tree layer that this
  4387   4447   ** insert is likely to be an append.
  4388   4448   **
  4389   4449   ** This instruction only works for indices.  The equivalent instruction
  4390   4450   ** for tables is OP_Insert.
  4391   4451   */
         4452  +case OP_SorterInsert:       /* in2 */
         4453  +#ifdef SQLITE_OMIT_MERGE_SORT
         4454  +  pOp->opcode = OP_IdxInsert;
         4455  +#endif
  4392   4456   case OP_IdxInsert: {        /* in2 */
  4393   4457     VdbeCursor *pC;
  4394   4458     BtCursor *pCrsr;
  4395   4459     int nKey;
  4396   4460     const char *zKey;
  4397   4461   
  4398   4462     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4399   4463     pC = p->apCsr[pOp->p1];
  4400   4464     assert( pC!=0 );
         4465  +  assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
  4401   4466     pIn2 = &aMem[pOp->p2];
  4402   4467     assert( pIn2->flags & MEM_Blob );
  4403   4468     pCrsr = pC->pCursor;
  4404   4469     if( ALWAYS(pCrsr!=0) ){
  4405   4470       assert( pC->isTable==0 );
  4406   4471       rc = ExpandBlob(pIn2);
  4407   4472       if( rc==SQLITE_OK ){
  4408         -      nKey = pIn2->n;
  4409         -      zKey = pIn2->z;
  4410         -      rc = sqlite3VdbeSorterWrite(db, pC, nKey);
  4411         -      if( rc==SQLITE_OK ){
         4473  +      if( isSorter(pC) ){
         4474  +        rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
         4475  +      }else{
         4476  +        nKey = pIn2->n;
         4477  +        zKey = pIn2->z;
  4412   4478           rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3, 
  4413   4479               ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
  4414         -        );
         4480  +            );
  4415   4481           assert( pC->deferredMoveto==0 );
         4482  +        pC->cacheStatus = CACHE_STALE;
  4416   4483         }
  4417         -      pC->cacheStatus = CACHE_STALE;
  4418   4484       }
  4419   4485     }
  4420   4486     break;
  4421   4487   }
  4422   4488   
  4423   4489   /* Opcode: IdxDelete P1 P2 P3 * *
  4424   4490   **

Changes to src/vdbe.h.

    57     57       VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
    58     58       CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
    59     59       Mem *pMem;             /* Used when p4type is P4_MEM */
    60     60       VTable *pVtab;         /* Used when p4type is P4_VTAB */
    61     61       KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
    62     62       int *ai;               /* Used when p4type is P4_INTARRAY */
    63     63       SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
           64  +    int (*xAdvance)(BtCursor *, int *);
    64     65     } p4;
    65     66   #ifdef SQLITE_DEBUG
    66     67     char *zComment;          /* Comment to improve readability */
    67     68   #endif
    68     69   #ifdef VDBE_PROFILE
    69     70     int cnt;                 /* Number of times this instruction was executed */
    70     71     u64 cycles;              /* Total time spent executing this instruction */
................................................................................
   112    113   #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
   113    114   #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
   114    115   #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
   115    116   #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
   116    117   #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
   117    118   #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
   118    119   #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
          120  +#define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
   119    121   
   120    122   /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
   121    123   ** is made.  That copy is freed when the Vdbe is finalized.  But if the
   122    124   ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
   123    125   ** gets freed when the Vdbe is finalized so it still should be obtained
   124    126   ** from a single sqliteMalloc().  But no copy is made and the calling
   125    127   ** function should *not* try to free the KeyInfo.
................................................................................
   206    208   VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
   207    209   sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
   208    210   void sqlite3VdbeSetVarmask(Vdbe*, int);
   209    211   #ifndef SQLITE_OMIT_TRACE
   210    212     char *sqlite3VdbeExpandSql(Vdbe*, const char*);
   211    213   #endif
   212    214   
   213         -UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int);
   214         -void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
          215  +void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
   215    216   int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
          217  +UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
   216    218   
   217    219   #ifndef SQLITE_OMIT_TRIGGER
   218    220   void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
   219    221   #endif
   220    222   
   221    223   
   222    224   #ifndef NDEBUG

Changes to src/vdbeInt.h.

    55     55     Bool atFirst;         /* True if pointing to first entry */
    56     56     Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
    57     57     Bool nullRow;         /* True if pointing to a row with no data */
    58     58     Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
    59     59     Bool isTable;         /* True if a table requiring integer keys */
    60     60     Bool isIndex;         /* True if an index containing keys only - no data */
    61     61     Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
           62  +  Bool isSorter;        /* True if a new-style sorter */
    62     63     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    63     64     const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
    64     65     i64 seqCount;         /* Sequence counter */
    65     66     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    66     67     i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
    67         -  VdbeSorter *pSorter;  /* Sorter object for OP_OpenSorter cursors */
           68  +  VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    68     69   
    69     70     /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or 
    70     71     ** OP_IsUnique opcode on this cursor. */
    71     72     int seekResult;
    72     73   
    73     74     /* Cached information about the header for the data record that the
    74     75     ** cursor is currently pointing to.  Only valid if cacheStatus matches
................................................................................
   380    381   double sqlite3VdbeRealValue(Mem*);
   381    382   void sqlite3VdbeIntegerAffinity(Mem*);
   382    383   int sqlite3VdbeMemRealify(Mem*);
   383    384   int sqlite3VdbeMemNumerify(Mem*);
   384    385   int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
   385    386   void sqlite3VdbeMemRelease(Mem *p);
   386    387   void sqlite3VdbeMemReleaseExternal(Mem *p);
          388  +#define MemReleaseExt(X)  \
          389  +  if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
          390  +    sqlite3VdbeMemReleaseExternal(X);
   387    391   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   388    392   const char *sqlite3OpcodeName(int);
   389    393   int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
   390    394   int sqlite3VdbeCloseStatement(Vdbe *, int);
   391    395   void sqlite3VdbeFrameDelete(VdbeFrame*);
   392    396   int sqlite3VdbeFrameRestore(VdbeFrame *);
   393    397   void sqlite3VdbeMemStoreType(Mem *pMem);
................................................................................
   395    399   #ifdef SQLITE_OMIT_MERGE_SORT
   396    400   # define sqlite3VdbeSorterInit(Y,Z)      SQLITE_OK
   397    401   # define sqlite3VdbeSorterWrite(X,Y,Z)   SQLITE_OK
   398    402   # define sqlite3VdbeSorterClose(Y,Z)
   399    403   # define sqlite3VdbeSorterRowkey(Y,Z)    SQLITE_OK
   400    404   # define sqlite3VdbeSorterRewind(X,Y,Z)  SQLITE_OK
   401    405   # define sqlite3VdbeSorterNext(X,Y,Z)    SQLITE_OK
          406  +# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
   402    407   #else
   403    408   int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
   404         -int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, int);
   405    409   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   406    410   int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
   407         -int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
   408    411   int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
          412  +int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
          413  +int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
          414  +int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
   409    415   #endif
   410    416   
   411    417   #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
   412    418     void sqlite3VdbeEnter(Vdbe*);
   413    419     void sqlite3VdbeLeave(Vdbe*);
   414    420   #else
   415    421   # define sqlite3VdbeEnter(X)

Changes to src/vdbeaux.c.

   429    429       }else if( opcode==OP_VFilter ){
   430    430         int n;
   431    431         assert( p->nOp - i >= 3 );
   432    432         assert( pOp[-1].opcode==OP_Integer );
   433    433         n = pOp[-1].p1;
   434    434         if( n>nMaxArgs ) nMaxArgs = n;
   435    435   #endif
          436  +    }else if( opcode==OP_Next || opcode==OP_SorterNext ){
          437  +      pOp->p4.xAdvance = sqlite3BtreeNext;
          438  +      pOp->p4type = P4_ADVANCE;
          439  +    }else if( opcode==OP_Prev ){
          440  +      pOp->p4.xAdvance = sqlite3BtreePrevious;
          441  +      pOp->p4type = P4_ADVANCE;
   436    442       }
   437    443   
   438    444       if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
   439    445         assert( -1-pOp->p2<p->nLabel );
   440    446         pOp->p2 = aLabel[-1-pOp->p2];
   441    447       }
   442    448     }
................................................................................
   934    940       case P4_INTARRAY: {
   935    941         sqlite3_snprintf(nTemp, zTemp, "intarray");
   936    942         break;
   937    943       }
   938    944       case P4_SUBPROGRAM: {
   939    945         sqlite3_snprintf(nTemp, zTemp, "program");
   940    946         break;
          947  +    }
          948  +    case P4_ADVANCE: {
          949  +      zTemp[0] = 0;
          950  +      break;
   941    951       }
   942    952       default: {
   943    953         zP4 = pOp->p4.z;
   944    954         if( zP4==0 ){
   945    955           zP4 = zTemp;
   946    956           zTemp[0] = 0;
   947    957         }
................................................................................
  2816   2826         }
  2817   2827         return len;
  2818   2828       }
  2819   2829     }
  2820   2830     return 0;
  2821   2831   }
  2822   2832   
  2823         -
  2824   2833   /*
  2825         -** Given the nKey-byte encoding of a record in pKey[], parse the
  2826         -** record into a UnpackedRecord structure.  Return a pointer to
  2827         -** that structure.
         2834  +** This routine is used to allocate sufficient space for an UnpackedRecord
         2835  +** structure large enough to be used with sqlite3VdbeRecordUnpack() if
         2836  +** the first argument is a pointer to KeyInfo structure pKeyInfo.
  2828   2837   **
  2829         -** The calling function might provide szSpace bytes of memory
  2830         -** space at pSpace.  This space can be used to hold the returned
  2831         -** VDbeParsedRecord structure if it is large enough.  If it is
  2832         -** not big enough, space is obtained from sqlite3_malloc().
         2838  +** The space is either allocated using sqlite3DbMallocRaw() or from within
         2839  +** the unaligned buffer passed via the second and third arguments (presumably
         2840  +** stack space). If the former, then *ppFree is set to a pointer that should
         2841  +** be eventually freed by the caller using sqlite3DbFree(). Or, if the 
         2842  +** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
         2843  +** before returning.
  2833   2844   **
  2834         -** The returned structure should be closed by a call to
  2835         -** sqlite3VdbeDeleteUnpackedRecord().
  2836         -*/ 
  2837         -UnpackedRecord *sqlite3VdbeRecordUnpack(
  2838         -  KeyInfo *pKeyInfo,     /* Information about the record format */
  2839         -  int nKey,              /* Size of the binary record */
  2840         -  const void *pKey,      /* The binary record */
  2841         -  char *pSpace,          /* Unaligned space available to hold the object */
  2842         -  int szSpace            /* Size of pSpace[] in bytes */
         2845  +** If an OOM error occurs, NULL is returned.
         2846  +*/
         2847  +UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
         2848  +  KeyInfo *pKeyInfo,              /* Description of the record */
         2849  +  char *pSpace,                   /* Unaligned space available */
         2850  +  int szSpace,                    /* Size of pSpace[] in bytes */
         2851  +  char **ppFree                   /* OUT: Caller should free this pointer */
  2843   2852   ){
  2844         -  const unsigned char *aKey = (const unsigned char *)pKey;
  2845         -  UnpackedRecord *p;  /* The unpacked record that we will return */
  2846         -  int nByte;          /* Memory space needed to hold p, in bytes */
  2847         -  int d;
  2848         -  u32 idx;
  2849         -  u16 u;              /* Unsigned loop counter */
  2850         -  u32 szHdr;
  2851         -  Mem *pMem;
  2852         -  int nOff;           /* Increase pSpace by this much to 8-byte align it */
  2853         -  
  2854         -  /*
  2855         -  ** We want to shift the pointer pSpace up such that it is 8-byte aligned.
         2853  +  UnpackedRecord *p;              /* Unpacked record to return */
         2854  +  int nOff;                       /* Increment pSpace by nOff to align it */
         2855  +  int nByte;                      /* Number of bytes required for *p */
         2856  +
         2857  +  /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
  2856   2858     ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift 
  2857   2859     ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
  2858   2860     */
  2859   2861     nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
  2860         -  pSpace += nOff;
  2861         -  szSpace -= nOff;
  2862   2862     nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
  2863         -  if( nByte>szSpace ){
  2864         -    p = sqlite3DbMallocRaw(pKeyInfo->db, nByte);
  2865         -    if( p==0 ) return 0;
  2866         -    p->flags = UNPACKED_NEED_FREE | UNPACKED_NEED_DESTROY;
         2863  +  if( nByte>szSpace+nOff ){
         2864  +    p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
         2865  +    *ppFree = (char *)p;
         2866  +    if( !p ) return 0;
  2867   2867     }else{
  2868         -    p = (UnpackedRecord*)pSpace;
  2869         -    p->flags = UNPACKED_NEED_DESTROY;
         2868  +    p = (UnpackedRecord*)&pSpace[nOff];
         2869  +    *ppFree = 0;
  2870   2870     }
         2871  +
         2872  +  p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
  2871   2873     p->pKeyInfo = pKeyInfo;
  2872   2874     p->nField = pKeyInfo->nField + 1;
  2873         -  p->aMem = pMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
         2875  +  return p;
         2876  +}
         2877  +
         2878  +/*
         2879  +** Given the nKey-byte encoding of a record in pKey[], populate the 
         2880  +** UnpackedRecord structure indicated by the fourth argument with the
         2881  +** contents of the decoded record.
         2882  +*/ 
         2883  +void sqlite3VdbeRecordUnpack(
         2884  +  KeyInfo *pKeyInfo,     /* Information about the record format */
         2885  +  int nKey,              /* Size of the binary record */
         2886  +  const void *pKey,      /* The binary record */
         2887  +  UnpackedRecord *p      /* Populate this structure before returning. */
         2888  +){
         2889  +  const unsigned char *aKey = (const unsigned char *)pKey;
         2890  +  int d; 
         2891  +  u32 idx;                        /* Offset in aKey[] to read from */
         2892  +  u16 u;                          /* Unsigned loop counter */
         2893  +  u32 szHdr;
         2894  +  Mem *pMem = p->aMem;
         2895  +
         2896  +  p->flags = 0;
  2874   2897     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
  2875   2898     idx = getVarint32(aKey, szHdr);
  2876   2899     d = szHdr;
  2877   2900     u = 0;
  2878   2901     while( idx<szHdr && u<p->nField && d<=nKey ){
  2879   2902       u32 serial_type;
  2880   2903   
................................................................................
  2885   2908       pMem->zMalloc = 0;
  2886   2909       d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
  2887   2910       pMem++;
  2888   2911       u++;
  2889   2912     }
  2890   2913     assert( u<=pKeyInfo->nField + 1 );
  2891   2914     p->nField = u;
  2892         -  return (void*)p;
  2893         -}
  2894         -
  2895         -/*
  2896         -** This routine destroys a UnpackedRecord object.
  2897         -*/
  2898         -void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord *p){
  2899         -#ifdef SQLITE_DEBUG
  2900         -  int i;
  2901         -  Mem *pMem;
  2902         -
  2903         -  assert( p!=0 );
  2904         -  assert( p->flags & UNPACKED_NEED_DESTROY );
  2905         -  for(i=0, pMem=p->aMem; i<p->nField; i++, pMem++){
  2906         -    /* The unpacked record is always constructed by the
  2907         -    ** sqlite3VdbeUnpackRecord() function above, which makes all
  2908         -    ** strings and blobs static.  And none of the elements are
  2909         -    ** ever transformed, so there is never anything to delete.
  2910         -    */
  2911         -    if( NEVER(pMem->zMalloc) ) sqlite3VdbeMemRelease(pMem);
  2912         -  }
  2913         -#endif
  2914         -  if( p->flags & UNPACKED_NEED_FREE ){
  2915         -    sqlite3DbFree(p->pKeyInfo->db, p);
  2916         -  }
  2917   2915   }
  2918   2916   
  2919   2917   /*
  2920   2918   ** This function compares the two table rows or index records
  2921   2919   ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
  2922   2920   ** or positive integer if key1 is less than, equal to or 
  2923   2921   ** greater than key2.  The {nKey1, pKey1} key must be a blob

Changes to src/vdbemem.c.

   267    267   /*
   268    268   ** If the memory cell contains a string value that must be freed by
   269    269   ** invoking an external callback, free it now. Calling this function
   270    270   ** does not free any Mem.zMalloc buffer.
   271    271   */
   272    272   void sqlite3VdbeMemReleaseExternal(Mem *p){
   273    273     assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
   274         -  testcase( p->flags & MEM_Agg );
   275         -  testcase( p->flags & MEM_Dyn );
   276         -  testcase( p->flags & MEM_RowSet );
   277         -  testcase( p->flags & MEM_Frame );
   278         -  if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
   279         -    if( p->flags&MEM_Agg ){
   280         -      sqlite3VdbeMemFinalize(p, p->u.pDef);
   281         -      assert( (p->flags & MEM_Agg)==0 );
   282         -      sqlite3VdbeMemRelease(p);
   283         -    }else if( p->flags&MEM_Dyn && p->xDel ){
   284         -      assert( (p->flags&MEM_RowSet)==0 );
   285         -      p->xDel((void *)p->z);
   286         -      p->xDel = 0;
   287         -    }else if( p->flags&MEM_RowSet ){
   288         -      sqlite3RowSetClear(p->u.pRowSet);
   289         -    }else if( p->flags&MEM_Frame ){
   290         -      sqlite3VdbeMemSetNull(p);
   291         -    }
          274  +  if( p->flags&MEM_Agg ){
          275  +    sqlite3VdbeMemFinalize(p, p->u.pDef);
          276  +    assert( (p->flags & MEM_Agg)==0 );
          277  +    sqlite3VdbeMemRelease(p);
          278  +  }else if( p->flags&MEM_Dyn && p->xDel ){
          279  +    assert( (p->flags&MEM_RowSet)==0 );
          280  +    p->xDel((void *)p->z);
          281  +    p->xDel = 0;
          282  +  }else if( p->flags&MEM_RowSet ){
          283  +    sqlite3RowSetClear(p->u.pRowSet);
          284  +  }else if( p->flags&MEM_Frame ){
          285  +    sqlite3VdbeMemSetNull(p);
   292    286     }
   293    287   }
   294    288   
   295    289   /*
   296    290   ** Release any memory held by the Mem. This may leave the Mem in an
   297    291   ** inconsistent state, for example with (Mem.z==0) and
   298    292   ** (Mem.type==SQLITE_TEXT).
   299    293   */
   300    294   void sqlite3VdbeMemRelease(Mem *p){
   301         -  sqlite3VdbeMemReleaseExternal(p);
          295  +  MemReleaseExt(p);
   302    296     sqlite3DbFree(p->db, p->zMalloc);
   303    297     p->z = 0;
   304    298     p->zMalloc = 0;
   305    299     p->xDel = 0;
   306    300   }
   307    301   
   308    302   /*
................................................................................
   616    610   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   617    611   ** pTo are freed.  The pFrom->z field is not duplicated.  If
   618    612   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   619    613   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   620    614   */
   621    615   void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
   622    616     assert( (pFrom->flags & MEM_RowSet)==0 );
   623         -  sqlite3VdbeMemReleaseExternal(pTo);
          617  +  MemReleaseExt(pTo);
   624    618     memcpy(pTo, pFrom, MEMCELLSIZE);
   625    619     pTo->xDel = 0;
   626    620     if( (pFrom->flags&MEM_Static)==0 ){
   627    621       pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
   628    622       assert( srcType==MEM_Ephem || srcType==MEM_Static );
   629    623       pTo->flags |= srcType;
   630    624     }
................................................................................
   634    628   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   635    629   ** freed before the copy is made.
   636    630   */
   637    631   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   638    632     int rc = SQLITE_OK;
   639    633   
   640    634     assert( (pFrom->flags & MEM_RowSet)==0 );
   641         -  sqlite3VdbeMemReleaseExternal(pTo);
          635  +  MemReleaseExt(pTo);
   642    636     memcpy(pTo, pFrom, MEMCELLSIZE);
   643    637     pTo->flags &= ~MEM_Dyn;
   644    638   
   645    639     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   646    640       if( 0==(pFrom->flags&MEM_Static) ){
   647    641         pTo->flags |= MEM_Ephem;
   648    642         rc = sqlite3VdbeMemMakeWriteable(pTo);

Changes to src/vdbesort.c.

    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "vdbeInt.h"
    20     20   
    21     21   #ifndef SQLITE_OMIT_MERGE_SORT
    22     22   
    23     23   typedef struct VdbeSorterIter VdbeSorterIter;
           24  +typedef struct SorterRecord SorterRecord;
    24     25   
    25     26   /*
    26     27   ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
    27     28   **
    28     29   ** As keys are added to the sorter, they are written to disk in a series
    29     30   ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
    30     31   ** the same as the cache-size allowed for temporary databases. In order
................................................................................
    88     89   **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
    89     90   **
    90     91   ** In other words, each time we advance to the next sorter element, log2(N)
    91     92   ** key comparison operations are required, where N is the number of segments
    92     93   ** being merged (rounded up to the next power of 2).
    93     94   */
    94     95   struct VdbeSorter {
    95         -  int nWorking;                   /* Start a new b-tree after this many pages */
    96         -  int nBtree;                     /* Current size of b-tree contents as PMA */
           96  +  int nInMemory;                  /* Current size of pRecord list as PMA */
    97     97     int nTree;                      /* Used size of aTree/aIter (power of 2) */
    98     98     VdbeSorterIter *aIter;          /* Array of iterators to merge */
    99     99     int *aTree;                     /* Current state of incremental merge */
   100    100     i64 iWriteOff;                  /* Current write offset within file pTemp1 */
   101    101     i64 iReadOff;                   /* Current read offset within file pTemp1 */
   102    102     sqlite3_file *pTemp1;           /* PMA file 1 */
   103    103     int nPMA;                       /* Number of PMAs stored in pTemp1 */
          104  +  SorterRecord *pRecord;          /* Head of in-memory record list */
          105  +  int mnPmaSize;                  /* Minimum PMA size, in bytes */
          106  +  int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
          107  +  UnpackedRecord *pUnpacked;      /* Used to unpack keys */
   104    108   };
   105    109   
   106    110   /*
   107    111   ** The following type is an iterator for a PMA. It caches the current key in 
   108    112   ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
   109    113   */
   110    114   struct VdbeSorterIter {
................................................................................
   112    116     i64 iEof;                       /* 1 byte past EOF for this iterator */
   113    117     sqlite3_file *pFile;            /* File iterator is reading from */
   114    118     int nAlloc;                     /* Bytes of space at aAlloc */
   115    119     u8 *aAlloc;                     /* Allocated space */
   116    120     int nKey;                       /* Number of bytes in key */
   117    121     u8 *aKey;                       /* Pointer to current key */
   118    122   };
          123  +
          124  +/*
          125  +** A structure to store a single record. All in-memory records are connected
          126  +** together into a linked list headed at VdbeSorter.pRecord using the 
          127  +** SorterRecord.pNext pointer.
          128  +*/
          129  +struct SorterRecord {
          130  +  void *pVal;
          131  +  int nVal;
          132  +  SorterRecord *pNext;
          133  +};
   119    134   
   120    135   /* Minimum allowable value for the VdbeSorter.nWorking variable */
   121    136   #define SORTER_MIN_WORKING 10
   122    137   
   123    138   /* Maximum number of segments to merge in a single pass. */
   124    139   #define SORTER_MAX_MERGE_COUNT 16
   125    140   
................................................................................
   138    153   */
   139    154   static int vdbeSorterIterNext(
   140    155     sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
   141    156     VdbeSorterIter *pIter           /* Iterator to advance */
   142    157   ){
   143    158     int rc;                         /* Return Code */
   144    159     int nRead;                      /* Number of bytes read */
   145         -  int nRec;                       /* Size of record in bytes */
   146         -  int iOff;                       /* Size of serialized size varint in bytes */
          160  +  int nRec = 0;                   /* Size of record in bytes */
          161  +  int iOff = 0;                   /* Size of serialized size varint in bytes */
   147    162   
   148    163     nRead = pIter->iEof - pIter->iReadOff;
   149    164     if( nRead>5 ) nRead = 5;
   150    165     if( nRead<=0 ){
   151    166       /* This is an EOF condition */
   152    167       vdbeSorterIterZero(db, pIter);
   153    168       return SQLITE_OK;
   154    169     }
   155    170   
   156    171     rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
   157         -  iOff = getVarint32(pIter->aAlloc, nRec);
   158         -
   159         -  if( rc==SQLITE_OK && (iOff+nRec)>nRead ){
   160         -    int nRead2;                   /* Number of extra bytes to read */
   161         -    if( (iOff+nRec)>pIter->nAlloc ){
   162         -      int nNew = pIter->nAlloc*2;
   163         -      while( (iOff+nRec)>nNew ) nNew = nNew*2;
   164         -      pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
   165         -      if( !pIter->aAlloc ) return SQLITE_NOMEM;
   166         -      pIter->nAlloc = nNew;
          172  +  if( rc==SQLITE_OK ){
          173  +    iOff = getVarint32(pIter->aAlloc, nRec);
          174  +    if( (iOff+nRec)>nRead ){
          175  +      int nRead2;                   /* Number of extra bytes to read */
          176  +      if( (iOff+nRec)>pIter->nAlloc ){
          177  +        int nNew = pIter->nAlloc*2;
          178  +        while( (iOff+nRec)>nNew ) nNew = nNew*2;
          179  +        pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
          180  +        if( !pIter->aAlloc ) return SQLITE_NOMEM;
          181  +        pIter->nAlloc = nNew;
          182  +      }
          183  +  
          184  +      nRead2 = iOff + nRec - nRead;
          185  +      rc = sqlite3OsRead(
          186  +          pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
          187  +      );
   167    188       }
   168         -
   169         -    nRead2 = iOff + nRec - nRead;
   170         -    rc = sqlite3OsRead(
   171         -        pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
   172         -    );
   173    189     }
   174    190   
   175         -  assert( nRec>0 || rc!=SQLITE_OK );
          191  +  assert( rc!=SQLITE_OK || nRec>0 );
   176    192     pIter->iReadOff += iOff+nRec;
   177    193     pIter->nKey = nRec;
   178    194     pIter->aKey = &pIter->aAlloc[iOff];
   179    195     return rc;
   180    196   }
   181    197   
   182    198   /*
................................................................................
   212    228   ** (i.e. if no IO error occurs), then *piOffset is set to the offset of
   213    229   ** the first byte past the end of the varint before returning. *piVal is
   214    230   ** set to the integer value read. If an error occurs, the final values of
   215    231   ** both *piOffset and *piVal are undefined.
   216    232   */
   217    233   static int vdbeSorterReadVarint(
   218    234     sqlite3_file *pFile,            /* File to read from */
   219         -  i64 iEof,                       /* Total number of bytes in file */
   220    235     i64 *piOffset,                  /* IN/OUT: Read offset in pFile */
   221    236     i64 *piVal                      /* OUT: Value read from file */
   222    237   ){
   223    238     u8 aVarint[9];                  /* Buffer large enough for a varint */
   224    239     i64 iOff = *piOffset;           /* Offset in file to read from */
   225         -  int nRead = 9;                  /* Number of bytes to read from file */
   226    240     int rc;                         /* Return code */
   227    241   
   228         -  assert( iEof>iOff );
   229         -  if( (iEof-iOff)<nRead ){
   230         -    nRead = iEof-iOff;
   231         -  }
   232         -
   233         -  rc = sqlite3OsRead(pFile, aVarint, nRead, iOff);
          242  +  rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
   234    243     if( rc==SQLITE_OK ){
   235    244       *piOffset += getVarint(aVarint, (u64 *)piVal);
   236    245     }
   237    246   
   238    247     return rc;
   239    248   }
   240    249   
................................................................................
   258    267     pIter->pFile = pSorter->pTemp1;
   259    268     pIter->iReadOff = iStart;
   260    269     pIter->nAlloc = 128;
   261    270     pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
   262    271     if( !pIter->aAlloc ){
   263    272       rc = SQLITE_NOMEM;
   264    273     }else{
   265         -    i64 iEof = pSorter->iWriteOff;     /* EOF of file pSorter->pTemp1 */
   266    274       i64 nByte;                         /* Total size of PMA in bytes */
   267         -    rc = vdbeSorterReadVarint(pSorter->pTemp1, iEof, &pIter->iReadOff, &nByte);
          275  +    rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
   268    276       *pnByte += nByte;
   269    277       pIter->iEof = pIter->iReadOff + nByte;
   270    278     }
   271    279     if( rc==SQLITE_OK ){
   272    280       rc = vdbeSorterIterNext(db, pIter);
   273    281     }
   274    282     return rc;
   275    283   }
   276    284   
          285  +
          286  +/*
          287  +** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2, 
          288  +** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
          289  +** used by the comparison. If an error occurs, return an SQLite error code.
          290  +** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
          291  +** value, depending on whether key1 is smaller, equal to or larger than key2.
          292  +**
          293  +** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
          294  +** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
          295  +** is true and key1 contains even a single NULL value, it is considered to
          296  +** be less than key2. Even if key2 also contains NULL values.
          297  +**
          298  +** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
          299  +** has been allocated and contains an unpacked record that is used as key2.
          300  +*/
          301  +static int vdbeSorterCompare(
          302  +  VdbeCursor *pCsr,               /* Cursor object (for pKeyInfo) */
          303  +  int bOmitRowid,                 /* Ignore rowid field at end of keys */
          304  +  void *pKey1, int nKey1,         /* Left side of comparison */
          305  +  void *pKey2, int nKey2,         /* Right side of comparison */
          306  +  int *pRes                       /* OUT: Result of comparison */
          307  +){
          308  +  KeyInfo *pKeyInfo = pCsr->pKeyInfo;
          309  +  VdbeSorter *pSorter = pCsr->pSorter;
          310  +  UnpackedRecord *r2 = pSorter->pUnpacked;
          311  +  int i;
          312  +
          313  +  if( pKey2 ){
          314  +    sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
          315  +  }
          316  +
          317  +  if( bOmitRowid ){
          318  +    r2->nField = pKeyInfo->nField;
          319  +    assert( r2->nField>0 );
          320  +    for(i=0; i<r2->nField; i++){
          321  +      if( r2->aMem[i].flags & MEM_Null ){
          322  +        *pRes = -1;
          323  +        return SQLITE_OK;
          324  +      }
          325  +    }
          326  +    r2->flags |= UNPACKED_PREFIX_MATCH;
          327  +  }
          328  +
          329  +  *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
          330  +  return SQLITE_OK;
          331  +}
          332  +
   277    333   /*
   278    334   ** This function is called to compare two iterator keys when merging 
   279    335   ** multiple b-tree segments. Parameter iOut is the index of the aTree[] 
   280    336   ** value to recalculate.
   281    337   */
   282    338   static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
   283    339     VdbeSorter *pSorter = pCsr->pSorter;
................................................................................
   301    357     p2 = &pSorter->aIter[i2];
   302    358   
   303    359     if( p1->pFile==0 ){
   304    360       iRes = i2;
   305    361     }else if( p2->pFile==0 ){
   306    362       iRes = i1;
   307    363     }else{
   308         -    char aSpace[150];
   309         -    UnpackedRecord *r1;
   310         -
   311         -    r1 = sqlite3VdbeRecordUnpack(
   312         -        pCsr->pKeyInfo, p1->nKey, p1->aKey, aSpace, sizeof(aSpace)
          364  +    int res;
          365  +    int rc;
          366  +    assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
          367  +    rc = vdbeSorterCompare(
          368  +        pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
   313    369       );
   314         -    if( r1==0 ) return SQLITE_NOMEM;
          370  +    /* The vdbeSorterCompare() call cannot fail since pCsr->pSorter->pUnpacked
          371  +    ** has already been allocated. */
          372  +    assert( rc==SQLITE_OK );
   315    373   
   316         -    if( sqlite3VdbeRecordCompare(p2->nKey, p2->aKey, r1)>=0 ){
          374  +    if( res<=0 ){
   317    375         iRes = i1;
   318    376       }else{
   319    377         iRes = i2;
   320    378       }
   321         -    sqlite3VdbeDeleteUnpackedRecord(r1);
   322    379     }
   323    380   
   324    381     pSorter->aTree[iOut] = iRes;
   325    382     return SQLITE_OK;
   326    383   }
   327    384   
   328    385   /*
   329    386   ** Initialize the temporary index cursor just opened as a sorter cursor.
   330    387   */
   331    388   int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
   332         -  assert( pCsr->pKeyInfo && pCsr->pBt );
   333         -  pCsr->pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
   334         -  return (pCsr->pSorter ? SQLITE_OK : SQLITE_NOMEM);
          389  +  int pgsz;                       /* Page size of main database */
          390  +  int mxCache;                    /* Cache size */
          391  +  VdbeSorter *pSorter;            /* The new sorter */
          392  +  char *d;                        /* Dummy */
          393  +
          394  +  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
          395  +  pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
          396  +  if( pSorter==0 ){
          397  +    return SQLITE_NOMEM;
          398  +  }
          399  +  
          400  +  pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
          401  +  if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
          402  +  assert( pSorter->pUnpacked==(UnpackedRecord *)d );
          403  +
          404  +  if( !sqlite3TempInMemory(db) ){
          405  +    pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
          406  +    pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
          407  +    mxCache = db->aDb[0].pSchema->cache_size;
          408  +    if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
          409  +    pSorter->mxPmaSize = mxCache * pgsz;
          410  +  }
          411  +
          412  +  return SQLITE_OK;
          413  +}
          414  +
          415  +/*
          416  +** Free the list of sorted records starting at pRecord.
          417  +*/
          418  +static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
          419  +  SorterRecord *p;
          420  +  SorterRecord *pNext;
          421  +  for(p=pRecord; p; p=pNext){
          422  +    pNext = p->pNext;
          423  +    sqlite3DbFree(db, p);
          424  +  }
   335    425   }
   336    426   
   337    427   /*
   338    428   ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
   339    429   */
   340    430   void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
   341    431     VdbeSorter *pSorter = pCsr->pSorter;
................................................................................
   346    436           vdbeSorterIterZero(db, &pSorter->aIter[i]);
   347    437         }
   348    438         sqlite3DbFree(db, pSorter->aIter);
   349    439       }
   350    440       if( pSorter->pTemp1 ){
   351    441         sqlite3OsCloseFree(pSorter->pTemp1);
   352    442       }
          443  +    vdbeSorterRecordFree(db, pSorter->pRecord);
          444  +    sqlite3DbFree(db, pSorter->pUnpacked);
   353    445       sqlite3DbFree(db, pSorter);
   354    446       pCsr->pSorter = 0;
   355    447     }
   356    448   }
   357    449   
   358    450   /*
   359    451   ** Allocate space for a file-handle and open a temporary file. If successful,
................................................................................
   365    457     return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
   366    458         SQLITE_OPEN_TEMP_JOURNAL |
   367    459         SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
   368    460         SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
   369    461     );
   370    462   }
   371    463   
          464  +/*
          465  +** Attemp to merge the two sorted lists p1 and p2 into a single list. If no
          466  +** error occurs set *ppOut to the head of the new list and return SQLITE_OK.
          467  +*/
          468  +static int vdbeSorterMerge(
          469  +  sqlite3 *db,                    /* Database handle */
          470  +  VdbeCursor *pCsr,               /* For pKeyInfo */
          471  +  SorterRecord *p1,               /* First list to merge */
          472  +  SorterRecord *p2,               /* Second list to merge */
          473  +  SorterRecord **ppOut            /* OUT: Head of merged list */
          474  +){
          475  +  int rc = SQLITE_OK;
          476  +  SorterRecord *pFinal = 0;
          477  +  SorterRecord **pp = &pFinal;
          478  +  void *pVal2 = p2 ? p2->pVal : 0;
          479  +
          480  +  while( p1 && p2 ){
          481  +    int res;
          482  +    rc = vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
          483  +    if( rc!=SQLITE_OK ){
          484  +      *pp = 0;
          485  +      vdbeSorterRecordFree(db, p1);
          486  +      vdbeSorterRecordFree(db, p2);
          487  +      vdbeSorterRecordFree(db, pFinal);
          488  +      *ppOut = 0;
          489  +      return rc;
          490  +    }
          491  +    if( res<=0 ){
          492  +      *pp = p1;
          493  +      pp = &p1->pNext;
          494  +      p1 = p1->pNext;
          495  +      pVal2 = 0;
          496  +    }else{
          497  +      *pp = p2;
          498  +       pp = &p2->pNext;
          499  +      p2 = p2->pNext;
          500  +      if( p2==0 ) break;
          501  +      pVal2 = p2->pVal;
          502  +    }
          503  +  }
          504  +  *pp = p1 ? p1 : p2;
          505  +
          506  +  *ppOut = pFinal;
          507  +  return SQLITE_OK;
          508  +}
          509  +
          510  +/*
          511  +** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
          512  +** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
          513  +** occurs.
          514  +*/
          515  +static int vdbeSorterSort(sqlite3 *db, VdbeCursor *pCsr){
          516  +  int rc = SQLITE_OK;
          517  +  int i;
          518  +  SorterRecord **aSlot;
          519  +  SorterRecord *p;
          520  +  VdbeSorter *pSorter = pCsr->pSorter;
          521  +
          522  +  aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
          523  +  if( !aSlot ){
          524  +    return SQLITE_NOMEM;
          525  +  }
          526  +
          527  +  p = pSorter->pRecord;
          528  +  while( p ){
          529  +    SorterRecord *pNext = p->pNext;
          530  +    p->pNext = 0;
          531  +    for(i=0; rc==SQLITE_OK && aSlot[i]; i++){
          532  +      rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p);
          533  +      aSlot[i] = 0;
          534  +    }
          535  +    if( rc!=SQLITE_OK ){
          536  +      vdbeSorterRecordFree(db, pNext);
          537  +      break;
          538  +    }
          539  +    aSlot[i] = p;
          540  +    p = pNext;
          541  +  }
          542  +
          543  +  p = 0;
          544  +  for(i=0; i<64; i++){
          545  +    if( rc==SQLITE_OK ){
          546  +      rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p);
          547  +    }else{
          548  +      vdbeSorterRecordFree(db, aSlot[i]);
          549  +    }
          550  +  }
          551  +  pSorter->pRecord = p;
          552  +
          553  +  sqlite3_free(aSlot);
          554  +  return rc;
          555  +}
          556  +
   372    557   
   373    558   /*
   374         -** Write the current contents of the b-tree to a PMA. Return SQLITE_OK
   375         -** if successful, or an SQLite error code otherwise.
          559  +** Write the current contents of the in-memory linked-list to a PMA. Return
          560  +** SQLITE_OK if successful, or an SQLite error code otherwise.
   376    561   **
   377    562   ** The format of a PMA is:
   378    563   **
   379    564   **     * A varint. This varint contains the total number of bytes of content
   380    565   **       in the PMA (not including the varint itself).
   381    566   **
   382    567   **     * One or more records packed end-to-end in order of ascending keys. 
   383    568   **       Each record consists of a varint followed by a blob of data (the 
   384    569   **       key). The varint is the number of bytes in the blob of data.
   385    570   */
   386         -static int vdbeSorterBtreeToPMA(sqlite3 *db, VdbeCursor *pCsr){
          571  +static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
   387    572     int rc = SQLITE_OK;             /* Return code */
   388    573     VdbeSorter *pSorter = pCsr->pSorter;
   389         -  int res = 0;
   390    574   
   391         -  /* sqlite3BtreeFirst() cannot fail because sorter btrees are always held
   392         -  ** in memory and so an I/O error is not possible. */
   393         -  rc = sqlite3BtreeFirst(pCsr->pCursor, &res);
   394         -  if( NEVER(rc!=SQLITE_OK) || res ) return rc;
   395         -  assert( pSorter->nBtree>0 );
          575  +  if( pSorter->nInMemory==0 ){
          576  +    assert( pSorter->pRecord==0 );
          577  +    return rc;
          578  +  }
          579  +
          580  +  rc = vdbeSorterSort(db, pCsr);
   396    581   
   397    582     /* If the first temporary PMA file has not been opened, open it now. */
   398         -  if( pSorter->pTemp1==0 ){
          583  +  if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
   399    584       rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
   400    585       assert( rc!=SQLITE_OK || pSorter->pTemp1 );
   401    586       assert( pSorter->iWriteOff==0 );
   402    587       assert( pSorter->nPMA==0 );
   403    588     }
   404    589   
   405    590     if( rc==SQLITE_OK ){
   406         -    i64 iWriteOff = pSorter->iWriteOff;
   407         -    void *aMalloc = 0;            /* Array used to hold a single record */
   408         -    int nMalloc = 0;              /* Allocated size of aMalloc[] in bytes */
          591  +    i64 iOff = pSorter->iWriteOff;
          592  +    SorterRecord *p;
          593  +    SorterRecord *pNext = 0;
          594  +    static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
   409    595   
   410    596       pSorter->nPMA++;
   411         -    for(
   412         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nBtree, &iWriteOff);
   413         -      rc==SQLITE_OK && res==0;
   414         -      rc = sqlite3BtreeNext(pCsr->pCursor, &res)
   415         -    ){
   416         -      i64 nKey;                   /* Size of this key in bytes */
          597  +    rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
          598  +    for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
          599  +      pNext = p->pNext;
          600  +      rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
   417    601   
   418         -      /* Write the size of the record in bytes to the output file */
   419         -      (void)sqlite3BtreeKeySize(pCsr->pCursor, &nKey);
   420         -      rc = vdbeSorterWriteVarint(pSorter->pTemp1, nKey, &iWriteOff);
   421         -
   422         -      /* Make sure the aMalloc[] buffer is large enough for the record */
   423         -      if( rc==SQLITE_OK && nKey>nMalloc ){
   424         -        aMalloc = sqlite3DbReallocOrFree(db, aMalloc, nKey);
   425         -        if( !aMalloc ){ 
   426         -          rc = SQLITE_NOMEM; 
   427         -        }else{
   428         -          nMalloc = nKey;
   429         -        }
          602  +      if( rc==SQLITE_OK ){
          603  +        rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
          604  +        iOff += p->nVal;
   430    605         }
   431    606   
   432         -      /* Write the record itself to the output file */
   433         -      if( rc==SQLITE_OK ){
   434         -        /* sqlite3BtreeKey() cannot fail because sorter btrees held in memory */
   435         -        rc = sqlite3BtreeKey(pCsr->pCursor, 0, nKey, aMalloc);
   436         -        if( ALWAYS(rc==SQLITE_OK) ){
   437         -          rc = sqlite3OsWrite(pSorter->pTemp1, aMalloc, nKey, iWriteOff);
   438         -          iWriteOff += nKey;
   439         -        }
   440         -      }
   441         -
   442         -      if( rc!=SQLITE_OK ) break;
          607  +      sqlite3DbFree(db, p);
   443    608       }
   444    609   
   445    610       /* This assert verifies that unless an error has occurred, the size of 
   446    611       ** the PMA on disk is the same as the expected size stored in
   447         -    ** pSorter->nBtree. */ 
   448         -    assert( rc!=SQLITE_OK || pSorter->nBtree==(
   449         -          iWriteOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nBtree)
          612  +    ** pSorter->nInMemory. */ 
          613  +    assert( rc!=SQLITE_OK || pSorter->nInMemory==(
          614  +          iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
   450    615       ));
   451    616   
   452         -    pSorter->iWriteOff = iWriteOff;
   453         -    sqlite3DbFree(db, aMalloc);
          617  +    pSorter->iWriteOff = iOff;
          618  +    if( rc==SQLITE_OK ){
          619  +      /* Terminate each file with 8 extra bytes so that from any offset
          620  +      ** in the file we can always read 9 bytes without a SHORT_READ error */
          621  +      rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
          622  +    }
          623  +    pSorter->pRecord = p;
   454    624     }
   455    625   
   456         -  pSorter->nBtree = 0;
   457    626     return rc;
   458    627   }
   459    628   
   460    629   /*
   461         -** This function is called on a sorter cursor by the VDBE before each row 
   462         -** is inserted into VdbeCursor.pCsr. Argument nKey is the size of the key, in
   463         -** bytes, about to be inserted.
   464         -**
   465         -** If it is determined that the temporary b-tree accessed via VdbeCursor.pCsr
   466         -** is large enough, its contents are written to a sorted PMA on disk and the
   467         -** tree emptied. This prevents the b-tree (which must be small enough to
   468         -** fit entirely in the cache in order to support efficient inserts) from
   469         -** growing too large.
   470         -**
   471         -** An SQLite error code is returned if an error occurs. Otherwise, SQLITE_OK.
          630  +** Add a record to the sorter.
   472    631   */
   473         -int sqlite3VdbeSorterWrite(sqlite3 *db, VdbeCursor *pCsr, int nKey){
   474         -  int rc = SQLITE_OK;             /* Return code */
          632  +int sqlite3VdbeSorterWrite(
          633  +  sqlite3 *db,                    /* Database handle */
          634  +  VdbeCursor *pCsr,               /* Sorter cursor */
          635  +  Mem *pVal                       /* Memory cell containing record */
          636  +){
   475    637     VdbeSorter *pSorter = pCsr->pSorter;
   476         -  if( pSorter ){
   477         -    Pager *pPager = sqlite3BtreePager(pCsr->pBt);
   478         -    int nPage;                    /* Current size of temporary file in pages */
   479         -
   480         -    /* Sorters never spill to disk */
   481         -    assert( sqlite3PagerFile(pPager)->pMethods==0 );
   482         -
   483         -    /* Determine how many pages the temporary b-tree has grown to */
   484         -    sqlite3PagerPagecount(pPager, &nPage);
   485         -
   486         -    /* If pSorter->nWorking is still zero, but the temporary file has been
   487         -    ** created in the file-system, then the most recent insert into the
   488         -    ** current b-tree segment probably caused the cache to overflow (it is
   489         -    ** also possible that sqlite3_release_memory() was called). So set the
   490         -    ** size of the working set to a little less than the current size of the 
   491         -    ** file in pages.  */
   492         -    if( pSorter->nWorking==0 && sqlite3PagerUnderStress(pPager) ){
   493         -      pSorter->nWorking = nPage-5;
   494         -      if( pSorter->nWorking<SORTER_MIN_WORKING ){
   495         -        pSorter->nWorking = SORTER_MIN_WORKING;
   496         -      }
   497         -    }
   498         -
   499         -    /* If the number of pages used by the current b-tree segment is greater
   500         -    ** than the size of the working set (VdbeSorter.nWorking), start a new
   501         -    ** segment b-tree.  */
   502         -    if( pSorter->nWorking && nPage>=pSorter->nWorking ){
   503         -      BtCursor *p = pCsr->pCursor;/* Cursor structure to close and reopen */
   504         -      int iRoot;                  /* Root page of new tree */
   505         -
   506         -      /* Copy the current contents of the b-tree into a PMA in sorted order.
   507         -      ** Close the currently open b-tree cursor. */
   508         -      rc = vdbeSorterBtreeToPMA(db, pCsr);
   509         -      sqlite3BtreeCloseCursor(p);
   510         -
   511         -      if( rc==SQLITE_OK ){
   512         -        rc = sqlite3BtreeDropTable(pCsr->pBt, 2, 0);
   513         -#ifdef SQLITE_DEBUG
   514         -        sqlite3PagerPagecount(pPager, &nPage);
   515         -        assert( rc!=SQLITE_OK || nPage==1 );
   516         -#endif
   517         -      }
   518         -      if( rc==SQLITE_OK ){
   519         -        rc = sqlite3BtreeCreateTable(pCsr->pBt, &iRoot, BTREE_BLOBKEY);
   520         -      }
   521         -      if( rc==SQLITE_OK ){
   522         -        assert( iRoot==2 );
   523         -        rc = sqlite3BtreeCursor(pCsr->pBt, iRoot, 1, pCsr->pKeyInfo, p);
   524         -      }
   525         -    }
   526         -
   527         -    pSorter->nBtree += sqlite3VarintLen(nKey) + nKey;
          638  +  int rc = SQLITE_OK;             /* Return Code */
          639  +  SorterRecord *pNew;             /* New list element */
          640  +
          641  +  assert( pSorter );
          642  +  pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
          643  +
          644  +  pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
          645  +  if( pNew==0 ){
          646  +    rc = SQLITE_NOMEM;
          647  +  }else{
          648  +    pNew->pVal = (void *)&pNew[1];
          649  +    memcpy(pNew->pVal, pVal->z, pVal->n);
          650  +    pNew->nVal = pVal->n;
          651  +    pNew->pNext = pSorter->pRecord;
          652  +    pSorter->pRecord = pNew;
          653  +  }
          654  +
          655  +  /* See if the contents of the sorter should now be written out. They
          656  +  ** are written out when either of the following are true:
          657  +  **
          658  +  **   * The total memory allocated for the in-memory list is greater 
          659  +  **     than (page-size * cache-size), or
          660  +  **
          661  +  **   * The total memory allocated for the in-memory list is greater 
          662  +  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
          663  +  */
          664  +  if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
          665  +        (pSorter->nInMemory>pSorter->mxPmaSize)
          666  +     || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
          667  +  )){
          668  +    rc = vdbeSorterListToPMA(db, pCsr);
          669  +    pSorter->nInMemory = 0;
   528    670     }
          671  +
   529    672     return rc;
   530    673   }
   531    674   
   532    675   /*
   533    676   ** Helper function for sqlite3VdbeSorterRewind(). 
   534    677   */
   535    678   static int vdbeSorterInitMerge(
................................................................................
   539    682   ){
   540    683     VdbeSorter *pSorter = pCsr->pSorter;
   541    684     int rc = SQLITE_OK;             /* Return code */
   542    685     int i;                          /* Used to iterator through aIter[] */
   543    686     i64 nByte = 0;                  /* Total bytes in all opened PMAs */
   544    687   
   545    688     /* Initialize the iterators. */
   546         -  for(i=0; rc==SQLITE_OK && i<SORTER_MAX_MERGE_COUNT; i++){
          689  +  for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
   547    690       VdbeSorterIter *pIter = &pSorter->aIter[i];
   548    691       rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
   549    692       pSorter->iReadOff = pIter->iEof;
   550         -    assert( pSorter->iReadOff<=pSorter->iWriteOff || rc!=SQLITE_OK );
   551         -    if( pSorter->iReadOff>=pSorter->iWriteOff ) break;
          693  +    assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
          694  +    if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
   552    695     }
   553    696   
   554    697     /* Initialize the aTree[] array. */
   555    698     for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
   556    699       rc = vdbeSorterDoCompare(pCsr, i);
   557    700     }
   558    701   
................................................................................
   571    714     i64 iWrite2 = 0;                /* Write offset for pTemp2 */
   572    715     int nIter;                      /* Number of iterators used */
   573    716     int nByte;                      /* Bytes of space required for aIter/aTree */
   574    717     int N = 2;                      /* Power of 2 >= nIter */
   575    718   
   576    719     assert( pSorter );
   577    720   
   578         -  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
   579         -  rc = vdbeSorterBtreeToPMA(db, pCsr);
   580         -  sqlite3BtreeCloseCursor(pCsr->pCursor);
   581         -  if( rc!=SQLITE_OK ) return rc;
          721  +  /* If no data has been written to disk, then do not do so now. Instead,
          722  +  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
          723  +  ** from the in-memory list.  */
   582    724     if( pSorter->nPMA==0 ){
   583         -    *pbEof = 1;
   584         -    return SQLITE_OK;
          725  +    *pbEof = !pSorter->pRecord;
          726  +    assert( pSorter->aTree==0 );
          727  +    return vdbeSorterSort(db, pCsr);
   585    728     }
          729  +
          730  +  /* Write the current b-tree to a PMA. Close the b-tree cursor. */
          731  +  rc = vdbeSorterListToPMA(db, pCsr);
          732  +  if( rc!=SQLITE_OK ) return rc;
   586    733   
   587    734     /* Allocate space for aIter[] and aTree[]. */
   588    735     nIter = pSorter->nPMA;
   589    736     if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
   590    737     assert( nIter>0 );
   591    738     while( N<nIter ) N += N;
   592    739     nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
................................................................................
   666    813   }
   667    814   
   668    815   /*
   669    816   ** Advance to the next element in the sorter.
   670    817   */
   671    818   int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
   672    819     VdbeSorter *pSorter = pCsr->pSorter;
   673         -  int iPrev = pSorter->aTree[1];  /* Index of iterator to advance */
   674         -  int i;                          /* Index of aTree[] to recalculate */
   675    820     int rc;                         /* Return code */
   676    821   
   677         -  rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
   678         -  for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
   679         -    rc = vdbeSorterDoCompare(pCsr, i);
   680         -  }
          822  +  if( pSorter->aTree ){
          823  +    int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
          824  +    int i;                        /* Index of aTree[] to recalculate */
          825  +
          826  +    rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
          827  +    for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
          828  +      rc = vdbeSorterDoCompare(pCsr, i);
          829  +    }
   681    830   
   682         -  *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
          831  +    *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
          832  +  }else{
          833  +    SorterRecord *pFree = pSorter->pRecord;
          834  +    pSorter->pRecord = pFree->pNext;
          835  +    pFree->pNext = 0;
          836  +    vdbeSorterRecordFree(db, pFree);
          837  +    *pbEof = !pSorter->pRecord;
          838  +    rc = SQLITE_OK;
          839  +  }
   683    840     return rc;
   684    841   }
          842  +
          843  +/*
          844  +** Return a pointer to a buffer owned by the sorter that contains the 
          845  +** current key.
          846  +*/
          847  +static void *vdbeSorterRowkey(
          848  +  VdbeSorter *pSorter,            /* Sorter object */
          849  +  int *pnKey                      /* OUT: Size of current key in bytes */
          850  +){
          851  +  void *pKey;
          852  +  if( pSorter->aTree ){
          853  +    VdbeSorterIter *pIter;
          854  +    pIter = &pSorter->aIter[ pSorter->aTree[1] ];
          855  +    *pnKey = pIter->nKey;
          856  +    pKey = pIter->aKey;
          857  +  }else{
          858  +    *pnKey = pSorter->pRecord->nVal;
          859  +    pKey = pSorter->pRecord->pVal;
          860  +  }
          861  +  return pKey;
          862  +}
   685    863   
   686    864   /*
   687    865   ** Copy the current sorter key into the memory cell pOut.
   688    866   */
   689    867   int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
   690    868     VdbeSorter *pSorter = pCsr->pSorter;
   691         -  VdbeSorterIter *pIter;
          869  +  void *pKey; int nKey;           /* Sorter key to copy into pOut */
   692    870   
   693         -  pIter = &pSorter->aIter[ pSorter->aTree[1] ];
   694         -
   695         -  /* Coverage testing note: As things are currently, this call will always
   696         -  ** succeed. This is because the memory cell passed by the VDBE layer 
   697         -  ** happens to be the same one as was used to assemble the keys before they
   698         -  ** were passed to the sorter - meaning it is always large enough for the
   699         -  ** largest key. But this could change very easily, so we leave the call
   700         -  ** to sqlite3VdbeMemGrow() in. */
   701         -  if( NEVER(sqlite3VdbeMemGrow(pOut, pIter->nKey, 0)) ){
          871  +  pKey = vdbeSorterRowkey(pSorter, &nKey);
          872  +  if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
   702    873       return SQLITE_NOMEM;
   703    874     }
   704         -  pOut->n = pIter->nKey;
          875  +  pOut->n = nKey;
   705    876     MemSetTypeFlag(pOut, MEM_Blob);
   706         -  memcpy(pOut->z, pIter->aKey, pIter->nKey);
          877  +  memcpy(pOut->z, pKey, nKey);
   707    878   
   708    879     return SQLITE_OK;
   709    880   }
          881  +
          882  +/*
          883  +** Compare the key in memory cell pVal with the key that the sorter cursor
          884  +** passed as the first argument currently points to. For the purposes of
          885  +** the comparison, ignore the rowid field at the end of each record.
          886  +**
          887  +** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
          888  +** Otherwise, set *pRes to a negative, zero or positive value if the
          889  +** key in pVal is smaller than, equal to or larger than the current sorter
          890  +** key.
          891  +*/
          892  +int sqlite3VdbeSorterCompare(
          893  +  VdbeCursor *pCsr,               /* Sorter cursor */
          894  +  Mem *pVal,                      /* Value to compare to current sorter key */
          895  +  int *pRes                       /* OUT: Result of comparison */
          896  +){
          897  +  int rc;
          898  +  VdbeSorter *pSorter = pCsr->pSorter;
          899  +  void *pKey; int nKey;           /* Sorter key to compare pVal with */
          900  +
          901  +  pKey = vdbeSorterRowkey(pSorter, &nKey);
          902  +  rc = vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
          903  +  assert( rc!=SQLITE_OK || pVal->db->mallocFailed || (*pRes)<=0 );
          904  +  return rc;
          905  +}
   710    906   
   711    907   #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */

Changes to test/distinct.test.

    41     41     uplevel [list do_test $tn [list is_distinct_noop $sql] 0]
    42     42   }
    43     43   
    44     44   proc do_temptables_test {tn sql temptables} {
    45     45     uplevel [list do_test $tn [subst -novar {
    46     46       set ret ""
    47     47       db eval "EXPLAIN [set sql]" {
    48         -      if {$opcode == "OpenEphemeral"} { 
           48  +      if {$opcode == "OpenEphemeral" || $opcode == "SorterOpen"} { 
    49     49           if {$p5 != "10" && $p5!="00"} { error "p5 = $p5" }
    50     50           if {$p5 == "10"} {
    51     51             lappend ret hash
    52     52           } else {
    53     53             lappend ret btree
    54     54           }
    55     55         }

Changes to test/index4.test.

   104    104       DROP TABLE t1;
   105    105       CREATE TABLE t1(x);
   106    106     COMMIT;
   107    107     CREATE INDEX i1 ON t1(x); 
   108    108     PRAGMA integrity_check
   109    109   } {ok}
   110    110   
          111  +do_execsql_test 2.1 {
          112  +  BEGIN;
          113  +    CREATE TABLE t2(x);
          114  +    INSERT INTO t2 VALUES(14);
          115  +    INSERT INTO t2 VALUES(35);
          116  +    INSERT INTO t2 VALUES(15);
          117  +    INSERT INTO t2 VALUES(35);
          118  +    INSERT INTO t2 VALUES(16);
          119  +  COMMIT;
          120  +}
          121  +do_catchsql_test 2.2 {
          122  +  CREATE UNIQUE INDEX i3 ON t2(x);
          123  +} {1 {indexed columns are not unique}}
          124  +
   111    125   
   112    126   finish_test

Changes to test/misc3.test.

   266    266           a INTEGER DEFAULT 54321,
   267    267           b TEXT DEFAULT "hello",
   268    268           c REAL DEFAULT 3.1415926
   269    269         );
   270    270         CREATE UNIQUE INDEX ex1i1 ON ex1(a);
   271    271         EXPLAIN REINDEX;
   272    272       }]
   273         -    regexp { IsUnique \d+ \d+ \d+ \d+ } $x
          273  +    regexp { SorterCompare \d+ \d+ \d+ } $x
   274    274     } {1}
   275    275     if {[regexp {16} [db one {PRAGMA encoding}]]} {
   276    276       do_test misc3-6.11-utf16 {
   277    277         set x [execsql {
   278    278           EXPLAIN SELECT a+123456789012, b*4.5678, c FROM ex1 ORDER BY +a, b DESC
   279    279         }]
   280    280         set y [regexp { 123456789012 } $x]

Changes to test/pager1.test.

  2433   2433     }
  2434   2434     db close
  2435   2435     sqlite3 db test.db
  2436   2436     execsql {
  2437   2437       BEGIN;
  2438   2438       INSERT INTO t1 VALUES(1, randomblob(10000));
  2439   2439     }
         2440  +  file_control_chunksize_test db main 1024
  2440   2441     file_control_sizehint_test db main 20971520; # 20MB
  2441   2442     execsql {
  2442   2443       PRAGMA cache_size = 10;
  2443   2444       INSERT INTO t1 VALUES(1, randomblob(10000));
  2444   2445       INSERT INTO t1 VALUES(2, randomblob(10000));
  2445   2446       INSERT INTO t1 SELECT x+2, randomblob(10000) from t1;
  2446   2447       INSERT INTO t1 SELECT x+4, randomblob(10000) from t1;

Changes to test/server1.test.

    25     25     finish_test
    26     26     return
    27     27   }
    28     28   
    29     29   # The sample server implementation does not work right when memory
    30     30   # management is enabled.
    31     31   #
    32         -ifcapable memorymanage {
           32  +ifcapable (memorymanage||mutex_noop) {
    33     33     finish_test
    34     34     return
    35     35   }
    36     36   
    37     37   # Create some data to work with
    38     38   #
    39     39   do_test server1-1.1 {

Changes to test/thread001.test.

    38     38     catchsql { DROP TABLE ab; }
    39     39   
    40     40     do_test thread001.$tn.0 {
    41     41       db close
    42     42       sqlite3_enable_shared_cache $shared_cache
    43     43       sqlite3_enable_shared_cache $shared_cache
    44     44     } $shared_cache
    45         -  sqlite3 db test.db -fullmutex 1
           45  +  sqlite3 db test.db -fullmutex 1 -key xyzzy
    46     46   
    47     47     set dbconfig ""
    48     48     if {$same_db} {
    49     49       set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
    50     50     }
    51     51   
    52     52     # Set up a database and a schema. The database contains a single
................................................................................
    73     73       execsql { PRAGMA integrity_check }
    74     74     } {ok}
    75     75   
    76     76     set thread_program {
    77     77       #sqlthread parent {puts STARTING..}
    78     78       set needToClose 0
    79     79       if {![info exists ::DB]} {
    80         -      set ::DB [sqlthread open test.db]
           80  +      set ::DB [sqlthread open test.db xyzzy]
    81     81         #sqlthread parent "puts \"OPEN $::DB\""
    82     82         set needToClose 1
    83     83       }
    84     84     
    85     85       for {set i 0} {$i < 100} {incr i} {
    86     86         # Test that the invariant is true.
    87     87         do_test t1 {

Changes to test/thread002.test.

    12     12   #   This test attempts to deadlock SQLite in shared-cache mode.
    13     13   #     
    14     14   #
    15     15   # $Id: thread002.test,v 1.9 2009/03/26 14:48:07 danielk1977 Exp $
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   
           19  +set do_not_use_codec 1
    19     20   source $testdir/tester.tcl
    20     21   if {[run_thread_tests]==0} { finish_test ; return }
           22  +
    21     23   
    22     24   db close
    23     25   set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
    24     26   
    25     27   set ::NTHREAD 10
    26     28   
    27     29   do_test thread002.1 {

Changes to test/thread003.test.

    76     76   #
    77     77   set nSecond 30
    78     78   puts "Starting thread003.2 (should run for ~$nSecond seconds)"
    79     79   do_test thread003.2 {
    80     80     foreach zFile {test.db test2.db} {
    81     81       set SCRIPT [format {
    82     82         set iEnd [expr {[clock_seconds] + %d}]
    83         -      set ::DB [sqlthread open %s]
           83  +      set ::DB [sqlthread open %s xyzzy]
    84     84     
    85     85         # Set the cache size to 15 pages per cache. 30 available globally.
    86     86         execsql { PRAGMA cache_size = 15 }
    87     87     
    88     88         while {[clock_seconds] < $iEnd} {
    89     89           set iQuery [expr {int(rand()*5000)}]
    90     90           execsql " SELECT * FROM t1 WHERE a = $iQuery "
................................................................................
   113    113   set nSecond 30
   114    114   puts "Starting thread003.3 (should run for ~$nSecond seconds)"
   115    115   do_test thread003.3 {
   116    116     foreach zFile {test.db test2.db} {
   117    117       set SCRIPT [format {
   118    118         set iStart [clock_seconds]
   119    119         set iEnd [expr {[clock_seconds] + %d}]
   120         -      set ::DB [sqlthread open %s]
          120  +      set ::DB [sqlthread open %s xyzzy]
   121    121     
   122    122         # Set the cache size to 15 pages per cache. 30 available globally.
   123    123         execsql { PRAGMA cache_size = 15 }
   124    124     
   125    125         while {[clock_seconds] < $iEnd} {
   126    126           set iQuery [expr {int(rand()*5000)}]
   127    127           execsql "SELECT * FROM t1 WHERE a = $iQuery"
................................................................................
   152    152   set nSecond 30
   153    153   puts "Starting thread003.4 (should run for ~$nSecond seconds)"
   154    154   unset -nocomplain finished(1)
   155    155   unset -nocomplain finished(2)
   156    156   do_test thread003.4 {
   157    157     thread_spawn finished(1) $thread_procs [format {
   158    158       set iEnd [expr {[clock_seconds] + %d}]
   159         -    set ::DB [sqlthread open test.db]
          159  +    set ::DB [sqlthread open test.db xyzzy]
   160    160   
   161    161       # Set the cache size to 15 pages per cache. 30 available globally.
   162    162       execsql { PRAGMA cache_size = 15 }
   163    163   
   164    164       while {[clock_seconds] < $iEnd} {
   165    165         set iQuery [expr {int(rand()*5000)}]
   166    166         execsql "SELECT * FROM t1 WHERE a = $iQuery"

Changes to test/wal5.test.

   231    231       } {}
   232    232       do_test 2.3.$tn.2 { file_page_counts } {1 5 1 5}
   233    233       do_test 2.3.$tn.3 { sql2 { BEGIN; SELECT * FROM t1 } } {1 2}
   234    234       do_test 2.3.$tn.4 { sql1 { INSERT INTO t1 VALUES(3, 4) } } {}
   235    235       do_test 2.3.$tn.5 { sql1 { INSERT INTO t2 VALUES(3, 4) } } {}
   236    236       do_test 2.3.$tn.6 { file_page_counts } {1 7 1 7}
   237    237       do_test 2.3.$tn.7 { code1 { do_wal_checkpoint db -mode full } } {1 7 5}
   238         -    do_test 2.3.$tn.8 { file_page_counts } {2 7 2 7}
          238  +    do_test 2.3.$tn.8 { file_page_counts } {1 7 2 7}
   239    239     }
   240    240   
   241    241     # Check that checkpoints block on the correct locks. And respond correctly
   242    242     # if they cannot obtain those locks. There are three locks that a checkpoint
   243    243     # may block on (in the following order):
   244    244     #
   245    245     #   1. The writer lock: FULL and RESTART checkpoints block until any writer
................................................................................
   339    339   
   340    340       do_test 3.$tn.6 { code3 { do_wal_checkpoint db3 } } {0 0 0}
   341    341     }
   342    342   }
   343    343   
   344    344   
   345    345   finish_test
          346  +

Changes to tool/lemon.c.

  2518   2518     filesize = ftell(fp);
  2519   2519     rewind(fp);
  2520   2520     filebuf = (char *)malloc( filesize+1 );
  2521   2521     if( filebuf==0 ){
  2522   2522       ErrorMsg(ps.filename,0,"Can't allocate %d of memory to hold this file.",
  2523   2523         filesize+1);
  2524   2524       gp->errorcnt++;
         2525  +    fclose(fp);
  2525   2526       return;
  2526   2527     }
  2527   2528     if( fread(filebuf,1,filesize,fp)!=filesize ){
  2528   2529       ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
  2529   2530         filesize);
  2530   2531       free(filebuf);
  2531   2532       gp->errorcnt++;
         2533  +    fclose(fp);
  2532   2534       return;
  2533   2535     }
  2534   2536     fclose(fp);
  2535   2537     filebuf[filesize] = 0;
  2536   2538   
  2537   2539     /* Make an initial pass through the file to handle %ifdef and %ifndef */
  2538   2540     preprocess_input(filebuf);

Changes to tool/spaceanal.tcl.

    37     37   
    38     38   # In-memory database for collecting statistics. This script loops through
    39     39   # the tables and indices in the database being analyzed, adding a row for each
    40     40   # to an in-memory database (for which the schema is shown below). It then
    41     41   # queries the in-memory db to produce the space-analysis report.
    42     42   #
    43     43   sqlite3 mem :memory:
    44         -set tabledef\
    45         -{CREATE TABLE space_used(
           44  +set tabledef {CREATE TABLE space_used(
    46     45      name clob,        -- Name of a table or index in the database file
    47     46      tblname clob,     -- Name of associated table
    48     47      is_index boolean, -- TRUE if it is an index, false for a table
    49     48      nentry int,       -- Number of entries in the BTree
    50     49      leaf_entries int, -- Number of leaf entries
    51     50      payload int,      -- Total amount of data stored in this table or index
    52     51      ovfl_payload int, -- Total amount of data stored on overflow pages
................................................................................
   289    288     if {$int_pages>0} {
   290    289       statline {Index pages used} $int_pages
   291    290     }
   292    291     statline {Primary pages used} $leaf_pages
   293    292     statline {Overflow pages used} $ovfl_pages
   294    293     statline {Total pages used} $total_pages
   295    294     if {$int_unused>0} {
   296         -    set int_unused_percent \
   297         -         [percent $int_unused [expr {$int_pages*$pageSize}] {of index space}]
          295  +    set int_unused_percent [
          296  +         percent $int_unused [expr {$int_pages*$pageSize}] {of index space}]
   298    297       statline "Unused bytes on index pages" $int_unused $int_unused_percent
   299    298     }
   300         -  statline "Unused bytes on primary pages" $leaf_unused \
   301         -     [percent $leaf_unused [expr {$leaf_pages*$pageSize}] {of primary space}]
   302         -  statline "Unused bytes on overflow pages" $ovfl_unused \
   303         -     [percent $ovfl_unused [expr {$ovfl_pages*$pageSize}] {of overflow space}]
   304         -  statline "Unused bytes on all pages" $total_unused \
   305         -               [percent $total_unused $storage {of all space}]
          299  +  statline "Unused bytes on primary pages" $leaf_unused [
          300  +     percent $leaf_unused [expr {$leaf_pages*$pageSize}] {of primary space}]
          301  +  statline "Unused bytes on overflow pages" $ovfl_unused [
          302  +     percent $ovfl_unused [expr {$ovfl_pages*$pageSize}] {of overflow space}]
          303  +  statline "Unused bytes on all pages" $total_unused [
          304  +               percent $total_unused $storage {of all space}]
   306    305     return 1
   307    306   }
   308    307   
   309    308   # Calculate the overhead in pages caused by auto-vacuum. 
   310    309   #
   311    310   # This procedure calculates and returns the number of pages used by the 
   312    311   # auto-vacuum 'pointer-map'. If the database does not support auto-vacuum,
................................................................................
   448    447   Page size in bytes
   449    448   
   450    449       The number of bytes in a single page of the database file.  
   451    450       Usually 1024.
   452    451   
   453    452   Number of pages in the whole file
   454    453   }
   455         -puts \
   456         -"    The number of $pageSize-byte pages that go into forming the complete
          454  +puts "    The number of $pageSize-byte pages that go into forming the complete
   457    455       database"
   458         -puts \
   459         -{
          456  +puts {
   460    457   Pages that store data
   461    458   
   462    459       The number of pages that store data, either as primary B*Tree pages or
   463    460       as overflow pages.  The number at the right is the data pages divided by
   464    461       the total number of pages in the file.
   465    462   
   466    463   Pages on the freelist

Changes to tool/tostr.awk.

     1      1   #!/usr/bin/awk
     2      2   #
     3      3   # Convert input text into a C string
     4      4   #
     5      5   {
     6         -  gsub(/\\/,"\\\\");
     7      6     gsub(/\"/,"\\\"");
     8      7     print "\"" $0 "\\n\"";
     9      8   }