/ Check-in [fc9ae839]
Login

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

Overview
Comment:Merge in performance enhancements from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1:fc9ae839569eb28eb734c52d95676c59b2e27494
User & Date: drh 2013-11-26 18:00:29
Context
2013-11-27
21:53
Merge all recent trunk changes. check-in: 3a2a1bd4 user: drh tags: sessions
2013-11-26
18:00
Merge in performance enhancements from trunk. check-in: fc9ae839 user: drh tags: sessions
16:51
Fix a possible NULL pointer deference in the wordcount test program. check-in: 6f91dca0 user: drh tags: trunk
2013-11-14
19:18
Merge the skip-scan optimization into the sessions branch. check-in: 7596d1bf user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   949    949   
   950    950   showdb$(TEXE):	$(TOP)/tool/showdb.c sqlite3.c
   951    951   	$(LTLINK) -o $@ $(TOP)/tool/showdb.c sqlite3.c $(TLIBS)
   952    952   
   953    953   wordcount$(TEXE):	$(TOP)/test/wordcount.c sqlite3.c
   954    954   	$(LTLINK) -o $@ $(TOP)/test/wordcount.c sqlite3.c $(TLIBS)
   955    955   
          956  +speedtest1$(TEXE):	$(TOP)/test/wordcount.c sqlite3.lo
          957  +	$(LTLINK) -o $@ $(TOP)/test/speedtest1.c sqlite3.lo $(TLIBS)
          958  +
   956    959   # Standard install and cleanup targets
   957    960   #
   958    961   lib_install:	libsqlite3.la
   959    962   	$(INSTALL) -d $(DESTDIR)$(libdir)
   960    963   	$(LTINSTALL) libsqlite3.la $(DESTDIR)$(libdir)
   961    964   	
   962    965   install:	sqlite3$(BEXE) lib_install sqlite3.h sqlite3.pc ${HAVE_TCL:1=tcl_install}

Changes to Makefile.msc.

  1353   1353   	$(NAWK) -f $(TOP)\tool\tostr.awk $(TOP)\tool\spaceanal.tcl >> $@
  1354   1354   	echo ; return zMainloop; } >> $@
  1355   1355   
  1356   1356   sqlite3_analyzer.exe:	sqlite3_analyzer.c $(LIBRESOBJS)
  1357   1357   	$(LTLINK) -DBUILD_sqlite -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
  1358   1358   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1359   1359   
  1360         -showdb.exe:	$(TOP)/tool/showdb.c sqlite3.c
         1360  +showdb.exe:	$(TOP)\tool\showdb.c sqlite3.c
  1361   1361   	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o $@ \
  1362         -		$(TOP)/tool/showdb.c sqlite3.c
         1362  +		$(TOP)\tool\showdb.c sqlite3.c
  1363   1363   
  1364         -wordcount.exe:	$(TOP)/test/wordcount.c sqlite3.c
         1364  +wordcount.exe:	$(TOP)\test\wordcount.c sqlite3.c
  1365   1365   	$(LTLINK) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o $@ \
  1366         -		$(TOP)/test/wordcount.c sqlite3.c
         1366  +		$(TOP)\test\wordcount.c sqlite3.c
         1367  +
         1368  +speedtest1.exe:	$(TOP)\test\speedtest1.c sqlite3.c
         1369  +	$(LTLINK) -DSQLITE_OMIT_LOAD_EXTENSION -o $@ \
         1370  +		$(TOP)\test\speedtest1.c sqlite3.c
  1367   1371   
  1368   1372   clean:
  1369   1373   	del /Q *.lo *.ilk *.lib *.obj *.pdb sqlite3.exe libsqlite3.lib
  1370   1374   	del /Q *.cod *.da *.bb *.bbg gmon.out
  1371   1375   	del /Q sqlite3.h opcodes.c opcodes.h
  1372   1376   	del /Q lemon.exe lempar.c parse.*
  1373   1377   	del /Q mkkeywordhash.exe keywordhash.h

Changes to addopcodes.awk.

    24     24     printf "#define TK_%-29s %4d\n", "ILLEGAL",         ++max
    25     25     printf "#define TK_%-29s %4d\n", "SPACE",           ++max
    26     26     printf "#define TK_%-29s %4d\n", "UNCLOSED_STRING", ++max
    27     27     printf "#define TK_%-29s %4d\n", "FUNCTION",        ++max
    28     28     printf "#define TK_%-29s %4d\n", "COLUMN",          ++max
    29     29     printf "#define TK_%-29s %4d\n", "AGG_FUNCTION",    ++max
    30     30     printf "#define TK_%-29s %4d\n", "AGG_COLUMN",      ++max
    31         -  printf "#define TK_%-29s %4d\n", "CONST_FUNC",      ++max
    32     31     printf "#define TK_%-29s %4d\n", "UMINUS",          ++max
    33     32     printf "#define TK_%-29s %4d\n", "UPLUS",           ++max
    34     33   }

Changes to ext/fts3/fts3.c.

   329    329     assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
   330    330     return (int) (q - (unsigned char *)p);
   331    331   }
   332    332   
   333    333   #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
   334    334     v = (v & mask1) | ( (*ptr++) << shift );                    \
   335    335     if( (v & mask2)==0 ){ var = v; return ret; }
          336  +#define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
          337  +  v = (*ptr++);                                               \
          338  +  if( (v & mask2)==0 ){ var = v; return ret; }
   336    339   
   337    340   /* 
   338    341   ** Read a 64-bit variable-length integer from memory starting at p[0].
   339    342   ** Return the number of bytes read, or 0 on error.
   340    343   ** The value is stored in *v.
   341    344   */
   342    345   int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
   343    346     const char *pStart = p;
   344    347     u32 a;
   345    348     u64 b;
   346    349     int shift;
   347    350   
   348         -  GETVARINT_STEP(a, p, 0,  0x00,     0x80, *v, 1);
          351  +  GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
   349    352     GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
   350    353     GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
   351    354     GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
   352    355     b = (a & 0x0FFFFFFF );
   353    356   
   354    357     for(shift=28; shift<=63; shift+=7){
   355    358       u64 c = *p++;
   356    359       b += (c&0x7F) << shift;
   357    360       if( (c & 0x80)==0 ) break;
   358    361     }
   359    362     *v = b;
   360         -  return p - pStart;
          363  +  return (int)(p - pStart);
   361    364   }
   362    365   
   363    366   /*
   364    367   ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
   365    368   ** 32-bit integer before it is returned.
   366    369   */
   367    370   int sqlite3Fts3GetVarint32(const char *p, int *pi){
   368    371     u32 a;
   369    372   
   370    373   #ifndef fts3GetVarint32
   371         -  GETVARINT_STEP(a, p, 0,  0x00,     0x80, *pi, 1);
          374  +  GETVARINT_INIT(a, p, 0,  0x00,     0x80, *pi, 1);
   372    375   #else
   373    376     a = (*p++);
   374    377     assert( a & 0x80 );
   375    378   #endif
   376    379   
   377    380     GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *pi, 2);
   378    381     GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *pi, 3);

Changes to main.mk.

   637    637   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showdb$(EXE) \
   638    638   		$(TOP)/tool/showdb.c sqlite3.c
   639    639   
   640    640   wordcount$(EXE):	$(TOP)/test/wordcount.c sqlite3.c
   641    641   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o wordcount$(EXE) \
   642    642   		$(TOP)/test/wordcount.c sqlite3.c
   643    643   
          644  +speedtest1$(EXE):	$(TOP)/test/speedtest1.c sqlite3.o
          645  +	$(TCC) -o speedtest1$(EXE) $(TOP)/test/speedtest1.c sqlite3.o $(THREADLIB)
          646  +
   644    647   # This target will fail if the SQLite amalgamation contains any exported
   645    648   # symbols that do not begin with "sqlite3_". It is run as part of the
   646    649   # releasetest.tcl script.
   647    650   #
   648    651   checksymbols: sqlite3.o
   649    652   	nm -g --defined-only sqlite3.o | grep -v " sqlite3_" ; test $$? -ne 0
   650    653   

Changes to mkopcodeh.awk.

   132    132        || name=="OP_Savepoint"     \
   133    133        || name=="OP_Checkpoint"    \
   134    134        || name=="OP_Vacuum"        \
   135    135        || name=="OP_JournalMode"   \
   136    136        || name=="OP_VUpdate"       \
   137    137        || name=="OP_VFilter"       \
   138    138        || name=="OP_Next"          \
          139  +     || name=="OP_NextIfOpen"    \
   139    140        || name=="OP_SorterNext"    \
   140    141        || name=="OP_Prev"          \
          142  +     || name=="OP_PrevIfOpen"    \
   141    143       ){
   142    144         cnt++
   143    145         while( used[cnt] ) cnt++
   144    146         op[name] = cnt
   145    147         used[cnt] = 1
   146    148         def[cnt] = name
   147    149       }

Changes to src/backup.c.

    92     92       }else{
    93     93         pParse->db = pDb;
    94     94         if( sqlite3OpenTempDatabase(pParse) ){
    95     95           sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
    96     96           rc = SQLITE_ERROR;
    97     97         }
    98     98         sqlite3DbFree(pErrorDb, pParse->zErrMsg);
           99  +      sqlite3ParserReset(pParse);
    99    100         sqlite3StackFree(pErrorDb, pParse);
   100    101       }
   101    102       if( rc ){
   102    103         return 0;
   103    104       }
   104    105     }
   105    106   

Changes to src/btree.c.

  4202   4202   **
  4203   4203   ** The pointer returned by this routine looks directly into the cached
  4204   4204   ** page of the database.  The data might change or move the next time
  4205   4205   ** any btree routine is called.
  4206   4206   */
  4207   4207   static const unsigned char *fetchPayload(
  4208   4208     BtCursor *pCur,      /* Cursor pointing to entry to read from */
  4209         -  int *pAmt,           /* Write the number of available bytes here */
         4209  +  u32 *pAmt,           /* Write the number of available bytes here */
  4210   4210     int skipKey          /* read beginning at data if this is true */
  4211   4211   ){
  4212   4212     unsigned char *aPayload;
  4213   4213     MemPage *pPage;
  4214   4214     u32 nKey;
  4215   4215     u32 nLocal;
  4216   4216   
  4217   4217     assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
  4218   4218     assert( pCur->eState==CURSOR_VALID );
  4219   4219     assert( cursorHoldsMutex(pCur) );
  4220   4220     pPage = pCur->apPage[pCur->iPage];
  4221   4221     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
  4222         -  if( NEVER(pCur->info.nSize==0) ){
         4222  +  if( pCur->info.nSize==0 ){
  4223   4223       btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
  4224   4224                      &pCur->info);
  4225   4225     }
  4226   4226     aPayload = pCur->info.pCell;
  4227   4227     aPayload += pCur->info.nHeader;
  4228   4228     if( pPage->intKey ){
  4229   4229       nKey = 0;
................................................................................
  4252   4252   ** including calls from other threads against the same cache.
  4253   4253   ** Hence, a mutex on the BtShared should be held prior to calling
  4254   4254   ** this routine.
  4255   4255   **
  4256   4256   ** These routines is used to get quick access to key and data
  4257   4257   ** in the common case where no overflow pages are used.
  4258   4258   */
  4259         -const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
         4259  +const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
  4260   4260     const void *p = 0;
  4261   4261     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4262   4262     assert( cursorHoldsMutex(pCur) );
  4263   4263     if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  4264   4264       p = (const void*)fetchPayload(pCur, pAmt, 0);
  4265   4265     }
  4266   4266     return p;
  4267   4267   }
  4268         -const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
         4268  +const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
  4269   4269     const void *p = 0;
  4270   4270     assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  4271   4271     assert( cursorHoldsMutex(pCur) );
  4272   4272     if( ALWAYS(pCur->eState==CURSOR_VALID) ){
  4273   4273       p = (const void*)fetchPayload(pCur, pAmt, 1);
  4274   4274     }
  4275   4275     return p;
................................................................................
  4643   4643     if( pCur->eState==CURSOR_INVALID ){
  4644   4644       *pRes = -1;
  4645   4645       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
  4646   4646       return SQLITE_OK;
  4647   4647     }
  4648   4648     assert( pCur->apPage[0]->intKey || pIdxKey );
  4649   4649     for(;;){
  4650         -    int lwr, upr, idx;
         4650  +    int lwr, upr, idx, c;
  4651   4651       Pgno chldPg;
  4652   4652       MemPage *pPage = pCur->apPage[pCur->iPage];
  4653         -    int c;
         4653  +    u8 *pCell;                          /* Pointer to current cell in pPage */
  4654   4654   
  4655   4655       /* pPage->nCell must be greater than zero. If this is the root-page
  4656   4656       ** the cursor would have been INVALID above and this for(;;) loop
  4657   4657       ** not run. If this is not the root-page, then the moveToChild() routine
  4658   4658       ** would have already detected db corruption. Similarly, pPage must
  4659   4659       ** be the right kind (index or table) of b-tree page. Otherwise
  4660   4660       ** a moveToChild() or moveToRoot() call would have detected corruption.  */
  4661   4661       assert( pPage->nCell>0 );
  4662   4662       assert( pPage->intKey==(pIdxKey==0) );
  4663   4663       lwr = 0;
  4664   4664       upr = pPage->nCell-1;
  4665         -    if( biasRight ){
  4666         -      pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
  4667         -    }else{
  4668         -      pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
  4669         -    }
  4670         -    for(;;){
  4671         -      u8 *pCell;                          /* Pointer to current cell in pPage */
  4672         -
  4673         -      assert( idx==pCur->aiIdx[pCur->iPage] );
  4674         -      pCur->info.nSize = 0;
  4675         -      pCell = findCell(pPage, idx) + pPage->childPtrSize;
  4676         -      if( pPage->intKey ){
         4665  +    assert( biasRight==0 || biasRight==1 );
         4666  +    idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
         4667  +    pCur->aiIdx[pCur->iPage] = (u16)idx;
         4668  +    if( pPage->intKey ){
         4669  +      for(;;){
  4677   4670           i64 nCellKey;
         4671  +        pCell = findCell(pPage, idx) + pPage->childPtrSize;
  4678   4672           if( pPage->hasData ){
  4679         -          u32 dummy;
  4680         -          pCell += getVarint32(pCell, dummy);
         4673  +          while( 0x80 <= *(pCell++) ){
         4674  +            if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
         4675  +          }
  4681   4676           }
  4682   4677           getVarint(pCell, (u64*)&nCellKey);
  4683         -        if( nCellKey==intKey ){
  4684         -          c = 0;
  4685         -        }else if( nCellKey<intKey ){
  4686         -          c = -1;
         4678  +        if( nCellKey<intKey ){
         4679  +          lwr = idx+1;
         4680  +          if( lwr>upr ){ c = -1; break; }
         4681  +        }else if( nCellKey>intKey ){
         4682  +          upr = idx-1;
         4683  +          if( lwr>upr ){ c = +1; break; }
  4687   4684           }else{
  4688         -          assert( nCellKey>intKey );
  4689         -          c = +1;
         4685  +          assert( nCellKey==intKey );
         4686  +          pCur->validNKey = 1;
         4687  +          pCur->info.nKey = nCellKey;
         4688  +          pCur->aiIdx[pCur->iPage] = (u16)idx;
         4689  +          if( !pPage->leaf ){
         4690  +            lwr = idx;
         4691  +            goto moveto_next_layer;
         4692  +          }else{
         4693  +            *pRes = 0;
         4694  +            rc = SQLITE_OK;
         4695  +            goto moveto_finish;
         4696  +          }
  4690   4697           }
  4691         -        pCur->validNKey = 1;
  4692         -        pCur->info.nKey = nCellKey;
  4693         -      }else{
         4698  +        assert( lwr+upr>=0 );
         4699  +        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2; */
         4700  +      }
         4701  +    }else{
         4702  +      for(;;){
         4703  +        int nCell;
         4704  +        pCell = findCell(pPage, idx) + pPage->childPtrSize;
         4705  +
  4694   4706           /* The maximum supported page-size is 65536 bytes. This means that
  4695   4707           ** the maximum number of record bytes stored on an index B-Tree
  4696   4708           ** page is less than 16384 bytes and may be stored as a 2-byte
  4697   4709           ** varint. This information is used to attempt to avoid parsing 
  4698   4710           ** the entire cell by checking for the cases where the record is 
  4699   4711           ** stored entirely within the b-tree page by inspecting the first 
  4700   4712           ** 2 bytes of the cell.
  4701   4713           */
  4702         -        int nCell = pCell[0];
         4714  +        nCell = pCell[0];
  4703   4715           if( nCell<=pPage->max1bytePayload
  4704   4716            /* && (pCell+nCell)<pPage->aDataEnd */
  4705   4717           ){
  4706   4718             /* This branch runs if the record-size field of the cell is a
  4707   4719             ** single byte varint and the record fits entirely on the main
  4708   4720             ** b-tree page.  */
  4709   4721             testcase( pCell+nCell+1==pPage->aDataEnd );
................................................................................
  4726   4738             btreeParseCellPtr(pPage, pCellBody, &pCur->info);
  4727   4739             nCell = (int)pCur->info.nKey;
  4728   4740             pCellKey = sqlite3Malloc( nCell );
  4729   4741             if( pCellKey==0 ){
  4730   4742               rc = SQLITE_NOMEM;
  4731   4743               goto moveto_finish;
  4732   4744             }
         4745  +          pCur->aiIdx[pCur->iPage] = (u16)idx;
  4733   4746             rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
  4734   4747             if( rc ){
  4735   4748               sqlite3_free(pCellKey);
  4736   4749               goto moveto_finish;
  4737   4750             }
  4738   4751             c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
  4739   4752             sqlite3_free(pCellKey);
  4740   4753           }
  4741         -      }
  4742         -      if( c==0 ){
  4743         -        if( pPage->intKey && !pPage->leaf ){
  4744         -          lwr = idx;
  4745         -          break;
         4754  +        if( c<0 ){
         4755  +          lwr = idx+1;
         4756  +        }else if( c>0 ){
         4757  +          upr = idx-1;
  4746   4758           }else{
         4759  +          assert( c==0 );
  4747   4760             *pRes = 0;
  4748   4761             rc = SQLITE_OK;
         4762  +          pCur->aiIdx[pCur->iPage] = (u16)idx;
  4749   4763             goto moveto_finish;
  4750   4764           }
         4765  +        if( lwr>upr ) break;
         4766  +        assert( lwr+upr>=0 );
         4767  +        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
  4751   4768         }
  4752         -      if( c<0 ){
  4753         -        lwr = idx+1;
  4754         -      }else{
  4755         -        upr = idx-1;
  4756         -      }
  4757         -      if( lwr>upr ){
  4758         -        break;
  4759         -      }
  4760         -      pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
  4761   4769       }
  4762   4770       assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
  4763   4771       assert( pPage->isInit );
  4764   4772       if( pPage->leaf ){
  4765         -      chldPg = 0;
  4766         -    }else if( lwr>=pPage->nCell ){
  4767         -      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
  4768         -    }else{
  4769         -      chldPg = get4byte(findCell(pPage, lwr));
  4770         -    }
  4771         -    if( chldPg==0 ){
  4772   4773         assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
         4774  +      pCur->aiIdx[pCur->iPage] = (u16)idx;
  4773   4775         *pRes = c;
  4774   4776         rc = SQLITE_OK;
  4775   4777         goto moveto_finish;
  4776   4778       }
         4779  +moveto_next_layer:
         4780  +    if( lwr>=pPage->nCell ){
         4781  +      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
         4782  +    }else{
         4783  +      chldPg = get4byte(findCell(pPage, lwr));
         4784  +    }
  4777   4785       pCur->aiIdx[pCur->iPage] = (u16)lwr;
  4778         -    pCur->info.nSize = 0;
  4779         -    pCur->validNKey = 0;
  4780   4786       rc = moveToChild(pCur, chldPg);
  4781         -    if( rc ) goto moveto_finish;
         4787  +    if( rc ) break;
  4782   4788     }
  4783   4789   moveto_finish:
         4790  +  pCur->info.nSize = 0;
         4791  +  pCur->validNKey = 0;
  4784   4792     return rc;
  4785   4793   }
  4786   4794   
  4787   4795   
  4788   4796   /*
  4789   4797   ** Return TRUE if the cursor is not pointing at an entry of the table.
  4790   4798   **

Changes to src/btree.h.

   174    174   int sqlite3BtreeFirst(BtCursor*, int *pRes);
   175    175   int sqlite3BtreeLast(BtCursor*, int *pRes);
   176    176   int sqlite3BtreeNext(BtCursor*, int *pRes);
   177    177   int sqlite3BtreeEof(BtCursor*);
   178    178   int sqlite3BtreePrevious(BtCursor*, int *pRes);
   179    179   int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
   180    180   int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
   181         -const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt);
   182         -const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt);
          181  +const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
          182  +const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
   183    183   int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
   184    184   int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
   185    185   void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
   186    186   sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
   187    187   
   188    188   char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
   189    189   struct Pager *sqlite3BtreePager(Btree*);

Changes to src/build.c.

   146    146       ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
   147    147       ** set for each database that is used.  Generate code to start a
   148    148       ** transaction on each used database and to verify the schema cookie
   149    149       ** on each used database.
   150    150       */
   151    151       if( pParse->cookieGoto>0 ){
   152    152         yDbMask mask;
   153         -      int iDb;
          153  +      int iDb, i, addr;
   154    154         sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
   155    155         for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
   156    156           if( (mask & pParse->cookieMask)==0 ) continue;
   157    157           sqlite3VdbeUsesBtree(v, iDb);
   158    158           sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
   159    159           if( db->init.busy==0 ){
   160    160             assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   161    161             sqlite3VdbeAddOp3(v, OP_VerifyCookie,
   162    162                               iDb, pParse->cookieValue[iDb],
   163    163                               db->aDb[iDb].pSchema->iGeneration);
   164    164           }
   165    165         }
   166    166   #ifndef SQLITE_OMIT_VIRTUALTABLE
   167         -      {
   168         -        int i;
   169         -        for(i=0; i<pParse->nVtabLock; i++){
   170         -          char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
   171         -          sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
   172         -        }
   173         -        pParse->nVtabLock = 0;
          167  +      for(i=0; i<pParse->nVtabLock; i++){
          168  +        char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
          169  +        sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
   174    170         }
          171  +      pParse->nVtabLock = 0;
   175    172   #endif
   176    173   
   177    174         /* Once all the cookies have been verified and transactions opened, 
   178    175         ** obtain the required table-locks. This is a no-op unless the 
   179    176         ** shared-cache feature is enabled.
   180    177         */
   181    178         codeTableLocks(pParse);
   182    179   
   183    180         /* Initialize any AUTOINCREMENT data structures required.
   184    181         */
   185    182         sqlite3AutoincrementBegin(pParse);
          183  +
          184  +      /* Code constant expressions that where factored out of inner loops */
          185  +      addr = pParse->cookieGoto;
          186  +      if( pParse->pConstExpr ){
          187  +        ExprList *pEL = pParse->pConstExpr;
          188  +        pParse->cookieGoto = 0;
          189  +        for(i=0; i<pEL->nExpr; i++){
          190  +          sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
          191  +        }
          192  +      }
   186    193   
   187    194         /* Finally, jump back to the beginning of the executable code. */
   188         -      sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto);
          195  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
   189    196       }
   190    197     }
   191    198   
   192    199   
   193    200     /* Get the VDBE program ready for execution
   194    201     */
   195    202     if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){

Changes to src/delete.c.

   223    223     Parse *pParse,         /* The parser context */
   224    224     SrcList *pTabList,     /* The table from which we should delete things */
   225    225     Expr *pWhere           /* The WHERE clause.  May be null */
   226    226   ){
   227    227     Vdbe *v;               /* The virtual database engine */
   228    228     Table *pTab;           /* The table from which records will be deleted */
   229    229     const char *zDb;       /* Name of database holding pTab */
   230         -  int end, addr = 0;     /* A couple addresses of generated code */
   231    230     int i;                 /* Loop counter */
   232    231     WhereInfo *pWInfo;     /* Information about the WHERE clause */
   233    232     Index *pIdx;           /* For looping over indices of the table */
   234    233     int iTabCur;           /* Cursor number for the table */
   235    234     int iDataCur;          /* VDBE cursor for the canonical data source */
   236    235     int iIdxCur;           /* Cursor number of the first index */
          236  +  int nIdx;              /* Number of indices */
   237    237     sqlite3 *db;           /* Main database structure */
   238    238     AuthContext sContext;  /* Authorization context */
   239    239     NameContext sNC;       /* Name context to resolve expressions in */
   240    240     int iDb;               /* Database number */
   241    241     int memCnt = -1;       /* Memory cell used for change counting */
   242    242     int rcauth;            /* Value returned by authorization callback */
   243         -
          243  +  int okOnePass;         /* True for one-pass algorithm without the FIFO */
          244  +  int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
          245  +  u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
          246  +  Index *pPk;            /* The PRIMARY KEY index on the table */
          247  +  int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
          248  +  i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
          249  +  int iKey;              /* Memory cell holding key of row to be deleted */
          250  +  i16 nKey;              /* Number of memory cells in the row key */
          251  +  int iEphCur = 0;       /* Ephemeral table holding all primary key values */
          252  +  int iRowSet = 0;       /* Register for rowset of rows to delete */
          253  +  int addrBypass = 0;    /* Address of jump over the delete logic */
          254  +  int addrLoop = 0;      /* Top of the delete loop */
          255  +  int addrDelete = 0;    /* Jump directly to the delete logic */
          256  +  int addrEphOpen = 0;   /* Instruction to open the Ephermeral table */
          257  + 
   244    258   #ifndef SQLITE_OMIT_TRIGGER
   245    259     int isView;                  /* True if attempting to delete from a view */
   246    260     Trigger *pTrigger;           /* List of table triggers, if required */
   247    261   #endif
   248    262   
   249    263     memset(&sContext, 0, sizeof(sContext));
   250    264     db = pParse->db;
................................................................................
   291    305     rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
   292    306     assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
   293    307     if( rcauth==SQLITE_DENY ){
   294    308       goto delete_from_cleanup;
   295    309     }
   296    310     assert(!isView || pTrigger);
   297    311   
   298         -  /* Assign  cursor number to the table and all its indices.
          312  +  /* Assign cursor numbers to the table and all its indices.
   299    313     */
   300    314     assert( pTabList->nSrc==1 );
   301    315     iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
   302         -  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
          316  +  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
   303    317       pParse->nTab++;
   304    318     }
   305    319   
   306    320     /* Start the view context
   307    321     */
   308    322     if( isView ){
   309    323       sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
................................................................................
   368    382       }
   369    383       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   370    384         assert( pIdx->pSchema==pTab->pSchema );
   371    385         sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
   372    386       }
   373    387     }else
   374    388   #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
   375         -  if( !HasRowid(pTab) ){
   376         -    /* There is a WHERE clause on a WITHOUT ROWID table.  
          389  +  {
          390  +    if( HasRowid(pTab) ){
          391  +      /* For a rowid table, initialize the RowSet to an empty set */
          392  +      pPk = 0;
          393  +      nPk = 1;
          394  +      iRowSet = ++pParse->nMem;
          395  +      sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
          396  +    }else{
          397  +      /* For a WITHOUT ROWID table, create an ephermeral table used to
          398  +      ** hold all primary keys for rows to be deleted. */
          399  +      pPk = sqlite3PrimaryKeyIndex(pTab);
          400  +      assert( pPk!=0 );
          401  +      nPk = pPk->nKeyCol;
          402  +      iPk = pParse->nMem+1;
          403  +      pParse->nMem += nPk;
          404  +      iEphCur = pParse->nTab++;
          405  +      addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
          406  +      sqlite3VdbeSetP4KeyInfo(pParse, pPk);
          407  +    }
          408  +  
          409  +    /* Construct a query to find the rowid or primary key for every row
          410  +    ** to be deleted, based on the WHERE clause.
   377    411       */
   378         -    Index *pPk;      /* The PRIMARY KEY index on the table */
   379         -    int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
   380         -    int iEph;        /* Ephemeral table holding all primary key values */
   381         -    int iKey;        /* Key value inserting into iEph */
   382         -    i16 nPk;         /* Number of components of the PRIMARY KEY */
   383         -
   384         -    pPk = sqlite3PrimaryKeyIndex(pTab);
   385         -    assert( pPk!=0 );
   386         -    nPk = pPk->nKeyCol;
   387         -    iPk = pParse->nMem+1;
   388         -    pParse->nMem += nPk;
   389         -    iKey = ++pParse->nMem;
   390         -    iEph = pParse->nTab++;
   391         -
   392         -    sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
   393         -    sqlite3VdbeSetP4KeyInfo(pParse, pPk);
   394         -    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 0, 0);
          412  +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0, 
          413  +                               WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
          414  +                               iTabCur+1);
   395    415       if( pWInfo==0 ) goto delete_from_cleanup;
   396         -    for(i=0; i<nPk; i++){
   397         -      sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, pPk->aiColumn[i],iPk+i);
   398         -    }
   399         -    sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
   400         -                      sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
   401         -    sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, iKey);
          416  +    okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
          417  +  
          418  +    /* Keep track of the number of rows to be deleted */
   402    419       if( db->flags & SQLITE_CountRows ){
   403    420         sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
   404    421       }
   405         -    sqlite3WhereEnd(pWInfo);
   406         -
   407         -    /* Open cursors for all indices of the table.
   408         -    */
   409         -    sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite,
   410         -                               iTabCur, &iDataCur, &iIdxCur);
   411         -
   412         -    /* Loop over the primary keys to be deleted. */
   413         -    addr = sqlite3VdbeAddOp1(v, OP_Rewind, iEph);
   414         -    sqlite3VdbeAddOp2(v, OP_RowKey, iEph, iPk);
   415         -
   416         -    /* Delete the row */
   417         -    sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
   418         -                             iPk, 0, 1, OE_Default, 0);
   419         -
   420         -    /* End of the delete loop */
   421         -    sqlite3VdbeAddOp2(v, OP_Next, iEph, addr+1);
   422         -    sqlite3VdbeJumpHere(v, addr);
   423         -
   424         -    /* Close the cursors open on the table and its indexes. */
   425         -    assert( iDataCur>=iIdxCur );
   426         -    for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   427         -      sqlite3VdbeAddOp1(v, OP_Close, iIdxCur+i);
          422  +  
          423  +    /* Extract the rowid or primary key for the current row */
          424  +    if( pPk ){
          425  +      for(i=0; i<nPk; i++){
          426  +        sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
          427  +                                        pPk->aiColumn[i], iPk+i);
          428  +      }
          429  +      iKey = iPk;
          430  +    }else{
          431  +      iKey = pParse->nMem + 1;
          432  +      iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
          433  +      if( iKey>pParse->nMem ) pParse->nMem = iKey;
          434  +    }
          435  +  
          436  +    if( okOnePass ){
          437  +      /* For ONEPASS, no need to store the rowid/primary-key.  There is only
          438  +      ** one, so just keep it in its register(s) and fall through to the
          439  +      ** delete code.
          440  +      */
          441  +      nKey = nPk; /* OP_Found will use an unpacked key */
          442  +      aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
          443  +      if( aToOpen==0 ){
          444  +        sqlite3WhereEnd(pWInfo);
          445  +        goto delete_from_cleanup;
          446  +      }
          447  +      memset(aToOpen, 1, nIdx+1);
          448  +      aToOpen[nIdx+1] = 0;
          449  +      if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
          450  +      if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
          451  +      if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
          452  +      addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
          453  +    }else if( pPk ){
          454  +      /* Construct a composite key for the row to be deleted and remember it */
          455  +      iKey = ++pParse->nMem;
          456  +      nKey = 0;   /* Zero tells OP_Found to use a composite key */
          457  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
          458  +                        sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
          459  +      sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
          460  +    }else{
          461  +      /* Get the rowid of the row to be deleted and remember it in the RowSet */
          462  +      nKey = 1;  /* OP_Seek always uses a single rowid */
          463  +      sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
   428    464       }
   429         -  }else{
   430         -    /* There is a WHERE clause on a rowid table.  Run a loop that extracts
   431         -    ** all rowids to be deleted into a RowSet.
   432         -    */
   433         -    int iRowSet = ++pParse->nMem;   /* Register for rowset of rows to delete */
   434         -    int iRowid = ++pParse->nMem;    /* Used for storing rowid values. */
   435         -    int regRowid;                   /* Actual register containing rowids */
   436         -
   437         -    /* Collect rowids of every row to be deleted.
   438         -    */
   439         -    sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
   440         -    pWInfo = sqlite3WhereBegin(
   441         -        pParse, pTabList, pWhere, 0, 0, WHERE_DUPLICATES_OK, 0
   442         -    );
   443         -    if( pWInfo==0 ) goto delete_from_cleanup;
   444         -    regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iRowid, 0);
   445         -    sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid);
   446         -    if( db->flags & SQLITE_CountRows ){
   447         -      sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
   448         -    }
          465  +  
          466  +    /* End of the WHERE loop */
   449    467       sqlite3WhereEnd(pWInfo);
   450         -
   451         -    /* Delete every item whose key was written to the list during the
   452         -    ** database scan.  We have to delete items after the scan is complete
   453         -    ** because deleting an item can change the scan order.  */
   454         -    end = sqlite3VdbeMakeLabel(v);
   455         -
          468  +    if( okOnePass ){
          469  +      /* Bypass the delete logic below if the WHERE loop found zero rows */
          470  +      addrBypass = sqlite3VdbeMakeLabel(v);
          471  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
          472  +      sqlite3VdbeJumpHere(v, addrDelete);
          473  +    }
          474  +  
   456    475       /* Unless this is a view, open cursors for the table we are 
   457    476       ** deleting from and all its indices. If this is a view, then the
   458    477       ** only effect this statement has is to fire the INSTEAD OF 
   459         -    ** triggers.  */
          478  +    ** triggers.
          479  +    */
   460    480       if( !isView ){
   461         -      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur,
          481  +      sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
   462    482                                    &iDataCur, &iIdxCur);
   463         -      assert( iDataCur==iTabCur );
   464         -      assert( iIdxCur==iDataCur+1 );
          483  +      assert( pPk || iDataCur==iTabCur );
          484  +      assert( pPk || iIdxCur==iDataCur+1 );
   465    485       }
   466         -
   467         -    addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid);
   468         -
          486  +  
          487  +    /* Set up a loop over the rowids/primary-keys that were found in the
          488  +    ** where-clause loop above.
          489  +    */
          490  +    if( okOnePass ){
          491  +      /* Just one row.  Hence the top-of-loop is a no-op */
          492  +      assert( nKey==nPk ); /* OP_Found will use an unpacked key */
          493  +      if( aToOpen[iDataCur-iTabCur] ){
          494  +        assert( pPk!=0 );
          495  +        sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
          496  +      }
          497  +    }else if( pPk ){
          498  +      addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur);
          499  +      sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
          500  +      assert( nKey==0 );  /* OP_Found will use a composite key */
          501  +    }else{
          502  +      addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
          503  +      assert( nKey==1 );
          504  +    }  
          505  +  
   469    506       /* Delete the row */
   470    507   #ifndef SQLITE_OMIT_VIRTUALTABLE
   471    508       if( IsVirtual(pTab) ){
   472    509         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
   473    510         sqlite3VtabMakeWritable(pParse, pTab);
   474         -      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, (char*)pVTab, P4_VTAB);
          511  +      sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
   475    512         sqlite3VdbeChangeP5(v, OE_Abort);
   476    513         sqlite3MayAbort(pParse);
   477    514       }else
   478    515   #endif
   479    516       {
   480    517         int count = (pParse->nested==0);    /* True to count changes */
   481    518         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
   482         -                               iRowid, 1, count, OE_Default, 0);
          519  +                               iKey, nKey, count, OE_Default, okOnePass);
   483    520       }
   484         -
   485         -    /* End of the delete loop */
   486         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
   487         -    sqlite3VdbeResolveLabel(v, end);
   488         -
          521  +  
          522  +    /* End of the loop over all rowids/primary-keys. */
          523  +    if( okOnePass ){
          524  +      sqlite3VdbeResolveLabel(v, addrBypass);
          525  +    }else if( pPk ){
          526  +      sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1);
          527  +      sqlite3VdbeJumpHere(v, addrLoop);
          528  +    }else{
          529  +      sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
          530  +      sqlite3VdbeJumpHere(v, addrLoop);
          531  +    }     
          532  +  
   489    533       /* Close the cursors open on the table and its indexes. */
   490    534       if( !isView && !IsVirtual(pTab) ){
   491         -      sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
          535  +      if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
   492    536         for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   493    537           sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
   494    538         }
   495    539       }
   496         -  }
          540  +  } /* End non-truncate path */
   497    541   
   498    542     /* Update the sqlite_sequence table by storing the content of the
   499    543     ** maximum rowid counter values recorded while inserting into
   500    544     ** autoincrement tables.
   501    545     */
   502    546     if( pParse->nested==0 && pParse->pTriggerTab==0 ){
   503    547       sqlite3AutoincrementEnd(pParse);
................................................................................
   513    557       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
   514    558     }
   515    559   
   516    560   delete_from_cleanup:
   517    561     sqlite3AuthContextPop(&sContext);
   518    562     sqlite3SrcListDelete(db, pTabList);
   519    563     sqlite3ExprDelete(db, pWhere);
          564  +  sqlite3DbFree(db, aToOpen);
   520    565     return;
   521    566   }
   522    567   /* Make sure "isView" and other macros defined above are undefined. Otherwise
   523    568   ** thely may interfere with compilation of other functions in this file
   524    569   ** (or in another file, if this file becomes part of the amalgamation).  */
   525    570   #ifdef isView
   526    571    #undef isView
................................................................................
   579    624     if( !bNoSeek ) sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
   580    625    
   581    626     /* If there are any triggers to fire, allocate a range of registers to
   582    627     ** use for the old.* references in the triggers.  */
   583    628     if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
   584    629       u32 mask;                     /* Mask of OLD.* columns in use */
   585    630       int iCol;                     /* Iterator used while populating OLD.* */
          631  +    int addrStart;                /* Start of BEFORE trigger programs */
   586    632   
   587    633       /* TODO: Could use temporary registers here. Also could attempt to
   588    634       ** avoid copying the contents of the rowid register.  */
   589    635       mask = sqlite3TriggerColmask(
   590    636           pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
   591    637       );
   592    638       mask |= sqlite3FkOldmask(pParse, pTab);
................................................................................
   599    645       for(iCol=0; iCol<pTab->nCol; iCol++){
   600    646         if( mask==0xffffffff || mask&(1<<iCol) ){
   601    647           sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
   602    648         }
   603    649       }
   604    650   
   605    651       /* Invoke BEFORE DELETE trigger programs. */
          652  +    addrStart = sqlite3VdbeCurrentAddr(v);
   606    653       sqlite3CodeRowTrigger(pParse, pTrigger, 
   607    654           TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
   608    655       );
   609    656   
   610         -    /* Seek the cursor to the row to be deleted again. It may be that
   611         -    ** the BEFORE triggers coded above have already removed the row
   612         -    ** being deleted. Do not attempt to delete the row a second time, and 
   613         -    ** do not fire AFTER triggers.  */
   614         -    sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
          657  +    /* If any BEFORE triggers were coded, then seek the cursor to the 
          658  +    ** row to be deleted again. It may be that the BEFORE triggers moved
          659  +    ** the cursor or of already deleted the row that the cursor was
          660  +    ** pointing to.
          661  +    */
          662  +    if( addrStart<sqlite3VdbeCurrentAddr(v) ){
          663  +      sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
          664  +    }
   615    665   
   616    666       /* Do FK processing. This call checks that any FK constraints that
   617    667       ** refer to this table (i.e. constraints attached to other tables) 
   618    668       ** are not violated by deleting this row.  */
   619    669       sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
   620    670     }
   621    671   

Changes to src/expr.c.

   926    926       Expr *pOldExpr = pOldItem->pExpr;
   927    927       pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
   928    928       pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
   929    929       pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
   930    930       pItem->sortOrder = pOldItem->sortOrder;
   931    931       pItem->done = 0;
   932    932       pItem->bSpanIsTab = pOldItem->bSpanIsTab;
   933         -    pItem->iOrderByCol = pOldItem->iOrderByCol;
   934         -    pItem->iAlias = pOldItem->iAlias;
          933  +    pItem->u = pOldItem->u;
   935    934     }
   936    935     return pNew;
   937    936   }
   938    937   
   939    938   /*
   940    939   ** If cursors, triggers, views and subqueries are all omitted from
   941    940   ** the build, then none of the following routines, except for 
................................................................................
  1188   1187     if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){
  1189   1188       pWalker->u.i = 0;
  1190   1189       return WRC_Abort;
  1191   1190     }
  1192   1191   
  1193   1192     switch( pExpr->op ){
  1194   1193       /* Consider functions to be constant if all their arguments are constant
  1195         -    ** and pWalker->u.i==2 */
         1194  +    ** and either pWalker->u.i==2 or the function as the SQLITE_FUNC_CONST
         1195  +    ** flag. */
  1196   1196       case TK_FUNCTION:
  1197         -      if( pWalker->u.i==2 ) return 0;
         1197  +      if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){
         1198  +        return WRC_Continue;
         1199  +      }
  1198   1200         /* Fall through */
  1199   1201       case TK_ID:
  1200   1202       case TK_COLUMN:
  1201   1203       case TK_AGG_FUNCTION:
  1202   1204       case TK_AGG_COLUMN:
  1203   1205         testcase( pExpr->op==TK_ID );
  1204   1206         testcase( pExpr->op==TK_COLUMN );
................................................................................
  2352   2354     Vdbe *v = pParse->pVdbe;  /* The VM under construction */
  2353   2355     int op;                   /* The opcode being coded */
  2354   2356     int inReg = target;       /* Results stored in register inReg */
  2355   2357     int regFree1 = 0;         /* If non-zero free this temporary register */
  2356   2358     int regFree2 = 0;         /* If non-zero free this temporary register */
  2357   2359     int r1, r2, r3, r4;       /* Various register numbers */
  2358   2360     sqlite3 *db = pParse->db; /* The database connection */
         2361  +  Expr tempX;               /* Temporary expression node */
  2359   2362   
  2360   2363     assert( target>0 && target<=pParse->nMem );
  2361   2364     if( v==0 ){
  2362   2365       assert( pParse->db->mallocFailed );
  2363   2366       return 0;
  2364   2367     }
  2365   2368   
................................................................................
  2571   2574           codeInteger(pParse, pLeft, 1, target);
  2572   2575   #ifndef SQLITE_OMIT_FLOATING_POINT
  2573   2576         }else if( pLeft->op==TK_FLOAT ){
  2574   2577           assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2575   2578           codeReal(v, pLeft->u.zToken, 1, target);
  2576   2579   #endif
  2577   2580         }else{
  2578         -        regFree1 = r1 = sqlite3GetTempReg(pParse);
  2579         -        sqlite3VdbeAddOp2(v, OP_Integer, 0, r1);
         2581  +        tempX.op = TK_INTEGER;
         2582  +        tempX.flags = EP_IntValue|EP_TokenOnly;
         2583  +        tempX.u.iValue = 0;
         2584  +        r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
  2580   2585           r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
  2581   2586           sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
  2582   2587           testcase( regFree2==0 );
  2583   2588         }
  2584   2589         inReg = target;
  2585   2590         break;
  2586   2591       }
................................................................................
  2617   2622           assert( !ExprHasProperty(pExpr, EP_IntValue) );
  2618   2623           sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
  2619   2624         }else{
  2620   2625           inReg = pInfo->aFunc[pExpr->iAgg].iMem;
  2621   2626         }
  2622   2627         break;
  2623   2628       }
  2624         -    case TK_CONST_FUNC:
  2625   2629       case TK_FUNCTION: {
  2626   2630         ExprList *pFarg;       /* List of function arguments */
  2627   2631         int nFarg;             /* Number of function arguments */
  2628   2632         FuncDef *pDef;         /* The function definition object */
  2629   2633         int nId;               /* Length of the function name in bytes */
  2630   2634         const char *zId;       /* The function name */
  2631   2635         int constMask = 0;     /* Mask of function arguments that are constant */
  2632   2636         int i;                 /* Loop counter */
  2633   2637         u8 enc = ENC(db);      /* The text encoding used by this database */
  2634   2638         CollSeq *pColl = 0;    /* A collating sequence */
  2635   2639   
  2636   2640         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  2637         -      testcase( op==TK_CONST_FUNC );
  2638         -      testcase( op==TK_FUNCTION );
  2639   2641         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
  2640   2642           pFarg = 0;
  2641   2643         }else{
  2642   2644           pFarg = pExpr->x.pList;
  2643   2645         }
  2644   2646         nFarg = pFarg ? pFarg->nExpr : 0;
  2645   2647         assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
  2675   2677         */
  2676   2678         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
  2677   2679           assert( nFarg>=1 );
  2678   2680           sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
  2679   2681           break;
  2680   2682         }
  2681   2683   
         2684  +      for(i=0; i<nFarg; i++){
         2685  +        if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
         2686  +          constMask |= (1<<i);
         2687  +        }
         2688  +        if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
         2689  +          pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
         2690  +        }
         2691  +      }
  2682   2692         if( pFarg ){
  2683         -        r1 = sqlite3GetTempRange(pParse, nFarg);
         2693  +        if( constMask ){
         2694  +          r1 = pParse->nMem+1;
         2695  +          pParse->nMem += nFarg;
         2696  +        }else{
         2697  +          r1 = sqlite3GetTempRange(pParse, nFarg);
         2698  +        }
  2684   2699   
  2685   2700           /* For length() and typeof() functions with a column argument,
  2686   2701           ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
  2687   2702           ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
  2688   2703           ** loading.
  2689   2704           */
  2690   2705           if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
................................................................................
  2691   2706             u8 exprOp;
  2692   2707             assert( nFarg==1 );
  2693   2708             assert( pFarg->a[0].pExpr!=0 );
  2694   2709             exprOp = pFarg->a[0].pExpr->op;
  2695   2710             if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
  2696   2711               assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
  2697   2712               assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
  2698         -            testcase( (pDef->funcFlags&~SQLITE_FUNC_ENCMASK)
  2699         -                       ==SQLITE_FUNC_LENGTH );
  2700         -            pFarg->a[0].pExpr->op2 = pDef->funcFlags&~SQLITE_FUNC_ENCMASK;
         2713  +            testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
         2714  +            pFarg->a[0].pExpr->op2 = 
         2715  +                  pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
  2701   2716             }
  2702   2717           }
  2703   2718   
  2704   2719           sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
  2705         -        sqlite3ExprCodeExprList(pParse, pFarg, r1, 1);
         2720  +        sqlite3ExprCodeExprList(pParse, pFarg, r1, 
         2721  +                                SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
  2706   2722           sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
  2707   2723         }else{
  2708   2724           r1 = 0;
  2709   2725         }
  2710   2726   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2711   2727         /* Possibly overload the function if the first argument is
  2712   2728         ** a virtual table column.
................................................................................
  2722   2738         */
  2723   2739         if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
  2724   2740           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
  2725   2741         }else if( nFarg>0 ){
  2726   2742           pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
  2727   2743         }
  2728   2744   #endif
  2729         -      for(i=0; i<nFarg; i++){
  2730         -        if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
  2731         -          constMask |= (1<<i);
  2732         -        }
  2733         -        if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
  2734         -          pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
  2735         -        }
  2736         -      }
  2737   2745         if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  2738   2746           if( !pColl ) pColl = db->pDfltColl; 
  2739   2747           sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
  2740   2748         }
  2741   2749         sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
  2742   2750                           (char*)pDef, P4_FUNCDEF);
  2743   2751         sqlite3VdbeChangeP5(v, (u8)nFarg);
  2744         -      if( nFarg ){
         2752  +      if( nFarg && constMask==0 ){
  2745   2753           sqlite3ReleaseTempRange(pParse, r1, nFarg);
  2746   2754         }
  2747   2755         break;
  2748   2756       }
  2749   2757   #ifndef SQLITE_OMIT_SUBQUERY
  2750   2758       case TK_EXISTS:
  2751   2759       case TK_SELECT: {
................................................................................
  2888   2896         int endLabel;                     /* GOTO label for end of CASE stmt */
  2889   2897         int nextCase;                     /* GOTO label for next WHEN clause */
  2890   2898         int nExpr;                        /* 2x number of WHEN terms */
  2891   2899         int i;                            /* Loop counter */
  2892   2900         ExprList *pEList;                 /* List of WHEN terms */
  2893   2901         struct ExprList_item *aListelem;  /* Array of WHEN terms */
  2894   2902         Expr opCompare;                   /* The X==Ei expression */
  2895         -      Expr cacheX;                      /* Cached expression X */
  2896   2903         Expr *pX;                         /* The X expression */
  2897   2904         Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
  2898   2905         VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
  2899   2906   
  2900   2907         assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
  2901   2908         assert(pExpr->x.pList->nExpr > 0);
  2902   2909         pEList = pExpr->x.pList;
  2903   2910         aListelem = pEList->a;
  2904   2911         nExpr = pEList->nExpr;
  2905   2912         endLabel = sqlite3VdbeMakeLabel(v);
  2906   2913         if( (pX = pExpr->pLeft)!=0 ){
  2907         -        cacheX = *pX;
         2914  +        tempX = *pX;
  2908   2915           testcase( pX->op==TK_COLUMN );
  2909         -        testcase( pX->op==TK_REGISTER );
  2910         -        exprToRegister(&cacheX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
         2916  +        exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
  2911   2917           testcase( regFree1==0 );
  2912   2918           opCompare.op = TK_EQ;
  2913         -        opCompare.pLeft = &cacheX;
         2919  +        opCompare.pLeft = &tempX;
  2914   2920           pTest = &opCompare;
  2915   2921           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
  2916   2922           ** The value in regFree1 might get SCopy-ed into the file result.
  2917   2923           ** So make sure that the regFree1 register is not reused for other
  2918   2924           ** purposes and possibly overwritten.  */
  2919   2925           regFree1 = 0;
  2920   2926         }
................................................................................
  2926   2932           }else{
  2927   2933             pTest = aListelem[i].pExpr;
  2928   2934           }
  2929   2935           nextCase = sqlite3VdbeMakeLabel(v);
  2930   2936           testcase( pTest->op==TK_COLUMN );
  2931   2937           sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
  2932   2938           testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
  2933         -        testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
  2934   2939           sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
  2935   2940           sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
  2936   2941           sqlite3ExprCachePop(pParse, 1);
  2937   2942           sqlite3VdbeResolveLabel(v, nextCase);
  2938   2943         }
  2939   2944         if( (nExpr&1)!=0 ){
  2940   2945           sqlite3ExprCachePush(pParse);
................................................................................
  2976   2981       }
  2977   2982   #endif
  2978   2983     }
  2979   2984     sqlite3ReleaseTempReg(pParse, regFree1);
  2980   2985     sqlite3ReleaseTempReg(pParse, regFree2);
  2981   2986     return inReg;
  2982   2987   }
         2988  +
         2989  +/*
         2990  +** Factor out the code of the given expression to initialization time.
         2991  +*/
         2992  +void sqlite3ExprCodeAtInit(
         2993  +  Parse *pParse,    /* Parsing context */
         2994  +  Expr *pExpr,      /* The expression to code when the VDBE initializes */
         2995  +  int regDest,      /* Store the value in this register */
         2996  +  u8 reusable       /* True if this expression is reusable */
         2997  +){
         2998  +  ExprList *p;
         2999  +  assert( ConstFactorOk(pParse) );
         3000  +  p = pParse->pConstExpr;
         3001  +  pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
         3002  +  p = sqlite3ExprListAppend(pParse, p, pExpr);
         3003  +  if( p ){
         3004  +     struct ExprList_item *pItem = &p->a[p->nExpr-1];
         3005  +     pItem->u.iConstExprReg = regDest;
         3006  +     pItem->reusable = reusable;
         3007  +  }
         3008  +  pParse->pConstExpr = p;
         3009  +}
  2983   3010   
  2984   3011   /*
  2985   3012   ** Generate code to evaluate an expression and store the results
  2986   3013   ** into a register.  Return the register number where the results
  2987   3014   ** are stored.
  2988   3015   **
  2989   3016   ** If the register is a temporary register that can be deallocated,
  2990   3017   ** then write its number into *pReg.  If the result register is not
  2991   3018   ** a temporary, then set *pReg to zero.
         3019  +**
         3020  +** If pExpr is a constant, then this routine might generate this
         3021  +** code to fill the register in the initialization section of the
         3022  +** VDBE program, in order to factor it out of the evaluation loop.
  2992   3023   */
  2993   3024   int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
  2994         -  int r1 = sqlite3GetTempReg(pParse);
  2995         -  int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
  2996         -  if( r2==r1 ){
  2997         -    *pReg = r1;
         3025  +  int r2;
         3026  +  pExpr = sqlite3ExprSkipCollate(pExpr);
         3027  +  if( ConstFactorOk(pParse)
         3028  +   && pExpr->op!=TK_REGISTER
         3029  +   && sqlite3ExprIsConstantNotJoin(pExpr)
         3030  +  ){
         3031  +    ExprList *p = pParse->pConstExpr;
         3032  +    int i;
         3033  +    *pReg  = 0;
         3034  +    if( p ){
         3035  +      struct ExprList_item *pItem;
         3036  +      for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
         3037  +        if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
         3038  +          return pItem->u.iConstExprReg;
         3039  +        }
         3040  +      }
         3041  +    }
         3042  +    r2 = ++pParse->nMem;
         3043  +    sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
  2998   3044     }else{
  2999         -    sqlite3ReleaseTempReg(pParse, r1);
  3000         -    *pReg = 0;
         3045  +    int r1 = sqlite3GetTempReg(pParse);
         3046  +    r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
         3047  +    if( r2==r1 ){
         3048  +      *pReg = r1;
         3049  +    }else{
         3050  +      sqlite3ReleaseTempReg(pParse, r1);
         3051  +      *pReg = 0;
         3052  +    }
  3001   3053     }
  3002   3054     return r2;
  3003   3055   }
  3004   3056   
  3005   3057   /*
  3006   3058   ** Generate code that will evaluate expression pExpr and store the
  3007   3059   ** results in register target.  The results are guaranteed to appear
................................................................................
  3036   3088   ** are reused.
  3037   3089   */
  3038   3090   int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
  3039   3091     Vdbe *v = pParse->pVdbe;
  3040   3092     int inReg;
  3041   3093     inReg = sqlite3ExprCode(pParse, pExpr, target);
  3042   3094     assert( target>0 );
  3043         -  /* This routine is called for terms to INSERT or UPDATE.  And the only
  3044         -  ** other place where expressions can be converted into TK_REGISTER is
  3045         -  ** in WHERE clause processing.  So as currently implemented, there is
  3046         -  ** no way for a TK_REGISTER to exist here.  But it seems prudent to
  3047         -  ** keep the ALWAYS() in case the conditions above change with future
  3048         -  ** modifications or enhancements. */
         3095  +  /* The only place, other than this routine, where expressions can be
         3096  +  ** converted to TK_REGISTER is internal subexpressions in BETWEEN and
         3097  +  ** CASE operators.  Neither ever calls this routine.  And this routine
         3098  +  ** is never called twice on the same expression.  Hence it is impossible
         3099  +  ** for the input to this routine to already be a register.  Nevertheless,
         3100  +  ** it seems prudent to keep the ALWAYS() in case the conditions above
         3101  +  ** change with future modifications or enhancements. */
  3049   3102     if( ALWAYS(pExpr->op!=TK_REGISTER) ){  
  3050   3103       int iMem;
  3051   3104       iMem = ++pParse->nMem;
  3052   3105       sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
  3053   3106       exprToRegister(pExpr, iMem);
  3054   3107     }
  3055   3108     return inReg;
................................................................................
  3173   3226       case TK_COLLATE: {
  3174   3227         sqlite3ExplainExpr(pOut, pExpr->pLeft);
  3175   3228         sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
  3176   3229         break;
  3177   3230       }
  3178   3231   
  3179   3232       case TK_AGG_FUNCTION:
  3180         -    case TK_CONST_FUNC:
  3181   3233       case TK_FUNCTION: {
  3182   3234         ExprList *pFarg;       /* List of function arguments */
  3183   3235         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
  3184   3236           pFarg = 0;
  3185   3237         }else{
  3186   3238           pFarg = pExpr->x.pList;
  3187   3239         }
................................................................................
  3323   3375           sqlite3ExplainNL(pOut);
  3324   3376         }
  3325   3377       }
  3326   3378       sqlite3ExplainPop(pOut);
  3327   3379     }
  3328   3380   }
  3329   3381   #endif /* SQLITE_DEBUG */
  3330         -
  3331         -/*
  3332         -** Return TRUE if pExpr is an constant expression that is appropriate
  3333         -** for factoring out of a loop.  Appropriate expressions are:
  3334         -**
  3335         -**    *  Any expression that evaluates to two or more opcodes.
  3336         -**
  3337         -**    *  Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, 
  3338         -**       or OP_Variable that does not need to be placed in a 
  3339         -**       specific register.
  3340         -**
  3341         -** There is no point in factoring out single-instruction constant
  3342         -** expressions that need to be placed in a particular register.  
  3343         -** We could factor them out, but then we would end up adding an
  3344         -** OP_SCopy instruction to move the value into the correct register
  3345         -** later.  We might as well just use the original instruction and
  3346         -** avoid the OP_SCopy.
  3347         -*/
  3348         -static int isAppropriateForFactoring(Expr *p){
  3349         -  if( !sqlite3ExprIsConstantNotJoin(p) ){
  3350         -    return 0;  /* Only constant expressions are appropriate for factoring */
  3351         -  }
  3352         -  if( (p->flags & EP_FixedDest)==0 ){
  3353         -    return 1;  /* Any constant without a fixed destination is appropriate */
  3354         -  }
  3355         -  while( p->op==TK_UPLUS ) p = p->pLeft;
  3356         -  switch( p->op ){
  3357         -#ifndef SQLITE_OMIT_BLOB_LITERAL
  3358         -    case TK_BLOB:
  3359         -#endif
  3360         -    case TK_VARIABLE:
  3361         -    case TK_INTEGER:
  3362         -    case TK_FLOAT:
  3363         -    case TK_NULL:
  3364         -    case TK_STRING: {
  3365         -      testcase( p->op==TK_BLOB );
  3366         -      testcase( p->op==TK_VARIABLE );
  3367         -      testcase( p->op==TK_INTEGER );
  3368         -      testcase( p->op==TK_FLOAT );
  3369         -      testcase( p->op==TK_NULL );
  3370         -      testcase( p->op==TK_STRING );
  3371         -      /* Single-instruction constants with a fixed destination are
  3372         -      ** better done in-line.  If we factor them, they will just end
  3373         -      ** up generating an OP_SCopy to move the value to the destination
  3374         -      ** register. */
  3375         -      return 0;
  3376         -    }
  3377         -    case TK_UMINUS: {
  3378         -      if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){
  3379         -        return 0;
  3380         -      }
  3381         -      break;
  3382         -    }
  3383         -    default: {
  3384         -      break;
  3385         -    }
  3386         -  }
  3387         -  return 1;
  3388         -}
  3389         -
  3390         -/*
  3391         -** If pExpr is a constant expression that is appropriate for
  3392         -** factoring out of a loop, then evaluate the expression
  3393         -** into a register and convert the expression into a TK_REGISTER
  3394         -** expression.
  3395         -*/
  3396         -static int evalConstExpr(Walker *pWalker, Expr *pExpr){
  3397         -  Parse *pParse = pWalker->pParse;
  3398         -  switch( pExpr->op ){
  3399         -    case TK_IN:
  3400         -    case TK_REGISTER: {
  3401         -      return WRC_Prune;
  3402         -    }
  3403         -    case TK_COLLATE: {
  3404         -      return WRC_Continue;
  3405         -    }
  3406         -    case TK_FUNCTION:
  3407         -    case TK_AGG_FUNCTION:
  3408         -    case TK_CONST_FUNC: {
  3409         -      /* The arguments to a function have a fixed destination.
  3410         -      ** Mark them this way to avoid generated unneeded OP_SCopy
  3411         -      ** instructions. 
  3412         -      */
  3413         -      ExprList *pList = pExpr->x.pList;
  3414         -      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  3415         -      if( pList ){
  3416         -        int i = pList->nExpr;
  3417         -        struct ExprList_item *pItem = pList->a;
  3418         -        for(; i>0; i--, pItem++){
  3419         -          if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest;
  3420         -        }
  3421         -      }
  3422         -      break;
  3423         -    }
  3424         -  }
  3425         -  if( isAppropriateForFactoring(pExpr) ){
  3426         -    int r1 = ++pParse->nMem;
  3427         -    int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
  3428         -    /* If r2!=r1, it means that register r1 is never used.  That is harmless
  3429         -    ** but suboptimal, so we want to know about the situation to fix it.
  3430         -    ** Hence the following assert: */
  3431         -    assert( r2==r1 );
  3432         -    exprToRegister(pExpr, r2);
  3433         -    return WRC_Prune;
  3434         -  }
  3435         -  return WRC_Continue;
  3436         -}
  3437         -
  3438         -/*
  3439         -** Preevaluate constant subexpressions within pExpr and store the
  3440         -** results in registers.  Modify pExpr so that the constant subexpresions
  3441         -** are TK_REGISTER opcodes that refer to the precomputed values.
  3442         -**
  3443         -** This routine is a no-op if the jump to the cookie-check code has
  3444         -** already occur.  Since the cookie-check jump is generated prior to
  3445         -** any other serious processing, this check ensures that there is no
  3446         -** way to accidently bypass the constant initializations.
  3447         -**
  3448         -** This routine is also a no-op if the SQLITE_FactorOutConst optimization
  3449         -** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS)
  3450         -** interface.  This allows test logic to verify that the same answer is
  3451         -** obtained for queries regardless of whether or not constants are
  3452         -** precomputed into registers or if they are inserted in-line.
  3453         -*/
  3454         -void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
  3455         -  Walker w;
  3456         -  if( pParse->cookieGoto ) return;
  3457         -  if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
  3458         -  memset(&w, 0, sizeof(w));
  3459         -  w.xExprCallback = evalConstExpr;
  3460         -  w.pParse = pParse;
  3461         -  sqlite3WalkExpr(&w, pExpr);
  3462         -}
  3463         -
  3464   3382   
  3465   3383   /*
  3466   3384   ** Generate code that pushes the value of every element of the given
  3467   3385   ** expression list into a sequence of registers beginning at target.
  3468   3386   **
  3469   3387   ** Return the number of elements evaluated.
         3388  +**
         3389  +** The SQLITE_ECEL_DUP flag prevents the arguments from being
         3390  +** filled using OP_SCopy.  OP_Copy must be used instead.
         3391  +**
         3392  +** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
         3393  +** factored out into initialization code.
  3470   3394   */
  3471   3395   int sqlite3ExprCodeExprList(
  3472   3396     Parse *pParse,     /* Parsing context */
  3473   3397     ExprList *pList,   /* The expression list to be coded */
  3474   3398     int target,        /* Where to write results */
  3475         -  int doHardCopy     /* Make a hard copy of every element */
         3399  +  u8 flags           /* SQLITE_ECEL_* flags */
  3476   3400   ){
  3477   3401     struct ExprList_item *pItem;
  3478   3402     int i, n;
         3403  +  u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
  3479   3404     assert( pList!=0 );
  3480   3405     assert( target>0 );
  3481   3406     assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
  3482   3407     n = pList->nExpr;
         3408  +  if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
  3483   3409     for(pItem=pList->a, i=0; i<n; i++, pItem++){
  3484   3410       Expr *pExpr = pItem->pExpr;
  3485         -    int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
  3486         -    if( inReg!=target+i ){
  3487         -      sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy,
  3488         -                        inReg, target+i);
         3411  +    if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
         3412  +      sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
         3413  +    }else{
         3414  +      int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
         3415  +      if( inReg!=target+i ){
         3416  +        sqlite3VdbeAddOp2(pParse->pVdbe, copyOp, inReg, target+i);
         3417  +      }
  3489   3418       }
  3490   3419     }
  3491   3420     return n;
  3492   3421   }
  3493   3422   
  3494   3423   /*
  3495   3424   ** Generate code for a BETWEEN operator.
................................................................................
  3835   3764   ** expressions are the same.  But if you get a 0 or 1 return, then you
  3836   3765   ** can be sure the expressions are the same.  In the places where
  3837   3766   ** this routine is used, it does not hurt to get an extra 2 - that
  3838   3767   ** just might result in some slightly slower code.  But returning
  3839   3768   ** an incorrect 0 or 1 could lead to a malfunction.
  3840   3769   */
  3841   3770   int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
  3842         -  if( pA==0||pB==0 ){
         3771  +  u32 combinedFlags;
         3772  +  if( pA==0 || pB==0 ){
  3843   3773       return pB==pA ? 0 : 2;
  3844   3774     }
  3845         -  assert( !ExprHasProperty(pA, EP_TokenOnly|EP_Reduced) );
  3846         -  assert( !ExprHasProperty(pB, EP_TokenOnly|EP_Reduced) );
  3847         -  if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
         3775  +  combinedFlags = pA->flags | pB->flags;
         3776  +  if( combinedFlags & EP_IntValue ){
         3777  +    if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
         3778  +      return 0;
         3779  +    }
  3848   3780       return 2;
  3849   3781     }
  3850         -  if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
  3851         -  if( pA->op!=pB->op && (pA->op!=TK_REGISTER || pA->op2!=pB->op) ){
         3782  +  if( pA->op!=pB->op ){
  3852   3783       if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
  3853   3784         return 1;
  3854   3785       }
  3855   3786       if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
  3856   3787         return 1;
  3857   3788       }
  3858   3789       return 2;
  3859   3790     }
  3860         -  if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
  3861         -  if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
  3862         -  if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
  3863         -  if( pA->iColumn!=pB->iColumn ) return 2;
  3864         -  if( pA->iTable!=pB->iTable 
  3865         -   && pA->op!=TK_REGISTER
  3866         -   && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
  3867         -  if( ExprHasProperty(pA, EP_IntValue) ){
  3868         -    if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
  3869         -      return 2;
  3870         -    }
  3871         -  }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
  3872         -    if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
         3791  +  if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
  3873   3792       if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
  3874   3793         return pA->op==TK_COLLATE ? 1 : 2;
  3875   3794       }
         3795  +  }
         3796  +  if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
         3797  +  if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
         3798  +    if( combinedFlags & EP_xIsSelect ) return 2;
         3799  +    if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
         3800  +    if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
         3801  +    if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
         3802  +    if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
         3803  +      if( pA->iColumn!=pB->iColumn ) return 2;
         3804  +      if( pA->iTable!=pB->iTable 
         3805  +       && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
         3806  +    }
  3876   3807     }
  3877   3808     return 0;
  3878   3809   }
  3879   3810   
  3880   3811   /*
  3881   3812   ** Compare two ExprList objects.  Return 0 if they are identical and 
  3882   3813   ** non-zero if they differ in any way.

Changes to src/fkey.c.

   544    544     WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
   545    545     int iFkIfZero = 0;              /* Address of OP_FkIfZero */
   546    546     Vdbe *v = sqlite3GetVdbe(pParse);
   547    547   
   548    548     assert( pIdx==0 || pIdx->pTable==pTab );
   549    549     assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
   550    550     assert( pIdx!=0 || pFKey->nCol==1 );
          551  +  assert( pIdx!=0 || HasRowid(pTab) );
   551    552   
   552    553     if( nIncr<0 ){
   553    554       iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
   554    555     }
   555    556   
   556    557     /* Create an Expr object representing an SQL expression like:
   557    558     **
................................................................................
   596    597       if( HasRowid(pTab) ){
   597    598         pLeft = exprTableRegister(pParse, pTab, regData, -1);
   598    599         pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
   599    600         pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
   600    601       }else{
   601    602         Expr *pEq, *pAll = 0;
   602    603         Index *pPk = sqlite3PrimaryKeyIndex(pTab);
          604  +      assert( pIdx!=0 );
   603    605         for(i=0; i<pPk->nKeyCol; i++){
   604    606           i16 iCol = pIdx->aiColumn[i];
   605    607           pLeft = exprTableRegister(pParse, pTab, regData, iCol);
   606    608           pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
   607    609           pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
   608    610           pAll = sqlite3ExprAnd(db, pAll, pEq);
   609    611         }

Changes to src/func.c.

  1660   1660       FUNCTION(coalesce,           1, 0, 0, 0                ),
  1661   1661       FUNCTION(coalesce,           0, 0, 0, 0                ),
  1662   1662       FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
  1663   1663       FUNCTION(hex,                1, 0, 0, hexFunc          ),
  1664   1664       FUNCTION2(ifnull,            2, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
  1665   1665       FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
  1666   1666       FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
  1667         -    FUNCTION(random,             0, 0, 0, randomFunc       ),
  1668         -    FUNCTION(randomblob,         1, 0, 0, randomBlob       ),
         1667  +    VFUNCTION(random,            0, 0, 0, randomFunc       ),
         1668  +    VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
  1669   1669       FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
  1670   1670       FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
  1671   1671       FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
  1672   1672       FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
  1673   1673   #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
  1674   1674       FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
  1675   1675       FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
  1676   1676   #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
  1677   1677       FUNCTION(quote,              1, 0, 0, quoteFunc        ),
  1678         -    FUNCTION(last_insert_rowid,  0, 0, 0, last_insert_rowid),
  1679         -    FUNCTION(changes,            0, 0, 0, changes          ),
  1680         -    FUNCTION(total_changes,      0, 0, 0, total_changes    ),
         1678  +    VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
         1679  +    VFUNCTION(changes,           0, 0, 0, changes          ),
         1680  +    VFUNCTION(total_changes,     0, 0, 0, total_changes    ),
  1681   1681       FUNCTION(replace,            3, 0, 0, replaceFunc      ),
  1682   1682       FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
  1683   1683     #ifdef SQLITE_SOUNDEX
  1684   1684       FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
  1685   1685     #endif
  1686   1686     #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1687   1687       FUNCTION(load_extension,     1, 0, 0, loadExt          ),

Changes to src/insert.c.

   816    816       regRowCount = ++pParse->nMem;
   817    817       sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
   818    818     }
   819    819   
   820    820     /* If this is not a view, open the table and and all indices */
   821    821     if( !isView ){
   822    822       int nIdx;
   823         -    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1,
          823  +    nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
   824    824                                         &iDataCur, &iIdxCur);
   825    825       aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
   826    826       if( aRegIdx==0 ){
   827    827         goto insert_cleanup;
   828    828       }
   829    829       for(i=0; i<nIdx; i++){
   830    830         aRegIdx[i] = ++pParse->nMem;
................................................................................
  1688   1688   ** pTab->pIndex list.
  1689   1689   */
  1690   1690   int sqlite3OpenTableAndIndices(
  1691   1691     Parse *pParse,   /* Parsing context */
  1692   1692     Table *pTab,     /* Table to be opened */
  1693   1693     int op,          /* OP_OpenRead or OP_OpenWrite */
  1694   1694     int iBase,       /* Use this for the table cursor, if there is one */
         1695  +  u8 *aToOpen,     /* If not NULL: boolean for each table and index */
  1695   1696     int *piDataCur,  /* Write the database source cursor number here */
  1696   1697     int *piIdxCur    /* Write the first index cursor number here */
  1697   1698   ){
  1698   1699     int i;
  1699   1700     int iDb;
         1701  +  int iDataCur;
  1700   1702     Index *pIdx;
  1701   1703     Vdbe *v;
  1702   1704   
  1703   1705     assert( op==OP_OpenRead || op==OP_OpenWrite );
  1704   1706     if( IsVirtual(pTab) ){
         1707  +    assert( aToOpen==0 );
  1705   1708       *piDataCur = 0;
  1706   1709       *piIdxCur = 1;
  1707   1710       return 0;
  1708   1711     }
  1709   1712     iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
  1710   1713     v = sqlite3GetVdbe(pParse);
  1711   1714     assert( v!=0 );
  1712   1715     if( iBase<0 ) iBase = pParse->nTab;
  1713         -  if( HasRowid(pTab) ){
  1714         -    *piDataCur = iBase++;
  1715         -    sqlite3OpenTable(pParse, *piDataCur, iDb, pTab, op);
         1716  +  iDataCur = iBase++;
         1717  +  if( piDataCur ) *piDataCur = iDataCur;
         1718  +  if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
         1719  +    sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
  1716   1720     }else{
  1717   1721       sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
  1718   1722     }
  1719         -  *piIdxCur = iBase;
         1723  +  if( piIdxCur ) *piIdxCur = iBase;
  1720   1724     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1721   1725       int iIdxCur = iBase++;
  1722   1726       assert( pIdx->pSchema==pTab->pSchema );
  1723         -    if( pIdx->autoIndex==2 && !HasRowid(pTab) ) *piDataCur = iIdxCur;
  1724         -    sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
  1725         -    sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
  1726         -    VdbeComment((v, "%s", pIdx->zName));
         1727  +    if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
         1728  +      *piDataCur = iIdxCur;
         1729  +    }
         1730  +    if( aToOpen==0 || aToOpen[i+1] ){
         1731  +      sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
         1732  +      sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
         1733  +      VdbeComment((v, "%s", pIdx->zName));
         1734  +    }
  1727   1735     }
  1728   1736     if( iBase>pParse->nTab ) pParse->nTab = iBase;
  1729   1737     return i;
  1730   1738   }
  1731   1739   
  1732   1740   
  1733   1741   #ifdef SQLITE_TEST

Changes to src/main.c.

   510    510         }
   511    511         sqlite3GlobalConfig.mxMmap = mxMmap;
   512    512         if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
   513    513         if( szMmap>mxMmap) szMmap = mxMmap;
   514    514         sqlite3GlobalConfig.szMmap = szMmap;
   515    515         break;
   516    516       }
          517  +
          518  +#if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC)
          519  +    case SQLITE_CONFIG_WIN32_HEAPSIZE: {
          520  +      sqlite3GlobalConfig.nHeap = va_arg(ap, int);
          521  +      break;
          522  +    }
          523  +#endif
   517    524   
   518    525       default: {
   519    526         rc = SQLITE_ERROR;
   520    527         break;
   521    528       }
   522    529     }
   523    530     va_end(ap);

Changes to src/mem5.c.

   205    205       int i = ((u8 *)p-mem5.zPool)/mem5.szAtom;
   206    206       assert( i>=0 && i<mem5.nBlock );
   207    207       iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
   208    208     }
   209    209     return iSize;
   210    210   }
   211    211   
   212         -/*
   213         -** Find the first entry on the freelist iLogsize.  Unlink that
   214         -** entry and return its index. 
   215         -*/
   216         -static int memsys5UnlinkFirst(int iLogsize){
   217         -  int i;
   218         -  int iFirst;
   219         -
   220         -  assert( iLogsize>=0 && iLogsize<=LOGMAX );
   221         -  i = iFirst = mem5.aiFreelist[iLogsize];
   222         -  assert( iFirst>=0 );
   223         -  while( i>0 ){
   224         -    if( i<iFirst ) iFirst = i;
   225         -    i = MEM5LINK(i)->next;
   226         -  }
   227         -  memsys5Unlink(iFirst, iLogsize);
   228         -  return iFirst;
   229         -}
   230         -
   231    212   /*
   232    213   ** Return a block of memory of at least nBytes in size.
   233    214   ** Return NULL if unable.  Return NULL if nBytes==0.
   234    215   **
   235    216   ** The caller guarantees that nByte is positive.
   236    217   **
   237    218   ** The caller has obtained a mutex prior to invoking this
................................................................................
   269    250     */
   270    251     for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
   271    252     if( iBin>LOGMAX ){
   272    253       testcase( sqlite3GlobalConfig.xLog!=0 );
   273    254       sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
   274    255       return 0;
   275    256     }
   276         -  i = memsys5UnlinkFirst(iBin);
          257  +  i = mem5.aiFreelist[iBin];
          258  +  memsys5Unlink(i, iBin);
   277    259     while( iBin>iLogsize ){
   278    260       int newSize;
   279    261   
   280    262       iBin--;
   281    263       newSize = 1 << iBin;
   282    264       mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
   283    265       memsys5Link(i+newSize, iBin);

Changes to src/os_win.c.

  1397   1397   
  1398   1398     if( !pWinMemData ) return SQLITE_ERROR;
  1399   1399     assert( pWinMemData->magic1==WINMEM_MAGIC1 );
  1400   1400     assert( pWinMemData->magic2==WINMEM_MAGIC2 );
  1401   1401   
  1402   1402   #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
  1403   1403     if( !pWinMemData->hHeap ){
         1404  +    DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
         1405  +    DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
         1406  +    if( dwMaximumSize==0 ){
         1407  +      dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
         1408  +    }else if( dwInitialSize>dwMaximumSize ){
         1409  +      dwInitialSize = dwMaximumSize;
         1410  +    }
  1404   1411       pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
  1405         -                                      SQLITE_WIN32_HEAP_INIT_SIZE,
  1406         -                                      SQLITE_WIN32_HEAP_MAX_SIZE);
         1412  +                                      dwInitialSize, dwMaximumSize);
  1407   1413       if( !pWinMemData->hHeap ){
  1408   1414         sqlite3_log(SQLITE_NOMEM,
  1409         -          "failed to HeapCreate (%lu), flags=%u, initSize=%u, maxSize=%u",
  1410         -          osGetLastError(), SQLITE_WIN32_HEAP_FLAGS,
  1411         -          SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE);
         1415  +          "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
         1416  +          osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
         1417  +          dwMaximumSize);
  1412   1418         return SQLITE_NOMEM;
  1413   1419       }
  1414   1420       pWinMemData->bOwned = TRUE;
  1415   1421       assert( pWinMemData->bOwned );
  1416   1422     }
  1417   1423   #else
  1418   1424     pWinMemData->hHeap = osGetProcessHeap();
................................................................................
  4064   4070   /****************************************************************************
  4065   4071   **************************** sqlite3_vfs methods ****************************
  4066   4072   **
  4067   4073   ** This division contains the implementation of methods on the
  4068   4074   ** sqlite3_vfs object.
  4069   4075   */
  4070   4076   
  4071         -#if 0
         4077  +#if defined(__CYGWIN__)
  4072   4078   /*
  4073   4079   ** Convert a filename from whatever the underlying operating system
  4074   4080   ** supports for filenames into UTF-8.  Space to hold the result is
  4075   4081   ** obtained from malloc and must be freed by the calling function.
  4076   4082   */
  4077   4083   static char *winConvertToUtf8Filename(const void *zFilename){
  4078   4084     char *zConverted = 0;
................................................................................
  4240   4246                                "winGetTempname2", zDir);
  4241   4247           }
  4242   4248           if( winIsDir(zConverted) ){
  4243   4249             /* At this point, we know the candidate directory exists and should
  4244   4250             ** be used.  However, we may need to convert the string containing
  4245   4251             ** its name into UTF-8 (i.e. if it is UTF-16 right now).
  4246   4252             */
  4247         -          if( osIsNT() ){
  4248         -            char *zUtf8 = winUnicodeToUtf8(zConverted);
  4249         -            if( !zUtf8 ){
  4250         -              sqlite3_free(zConverted);
  4251         -              sqlite3_free(zBuf);
  4252         -              OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  4253         -              return SQLITE_IOERR_NOMEM;
  4254         -            }
  4255         -            sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
  4256         -            sqlite3_free(zUtf8);
         4253  +          char *zUtf8 = winConvertToUtf8Filename(zConverted);
         4254  +          if( !zUtf8 ){
  4257   4255               sqlite3_free(zConverted);
  4258         -            break;
  4259         -          }else{
  4260         -            sqlite3_snprintf(nMax, zBuf, "%s", zConverted);
  4261         -            sqlite3_free(zConverted);
  4262         -            break;
         4256  +            sqlite3_free(zBuf);
         4257  +            OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
         4258  +            return SQLITE_IOERR_NOMEM;
  4263   4259             }
         4260  +          sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
         4261  +          sqlite3_free(zUtf8);
         4262  +          sqlite3_free(zConverted);
         4263  +          break;
  4264   4264           }
  4265   4265           sqlite3_free(zConverted);
  4266   4266         }
  4267   4267       }
  4268   4268     }
  4269   4269   #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
  4270   4270     else if( osIsNT() ){
................................................................................
  4941   4941       **       for converting the relative path name to an absolute
  4942   4942       **       one by prepending the data directory and a slash.
  4943   4943       */
  4944   4944       char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
  4945   4945       if( !zOut ){
  4946   4946         return SQLITE_IOERR_NOMEM;
  4947   4947       }
  4948         -    if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
  4949         -                         pVfs->mxPathname+1)<0 ){
         4948  +    if( cygwin_conv_path(
         4949  +            (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
         4950  +            CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
  4950   4951         sqlite3_free(zOut);
  4951   4952         return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
  4952   4953                            "winFullPathname1", zRelative);
         4954  +    }else{
         4955  +      char *zUtf8 = winConvertToUtf8Filename(zOut);
         4956  +      if( !zUtf8 ){
         4957  +        sqlite3_free(zOut);
         4958  +        return SQLITE_IOERR_NOMEM;
         4959  +      }
         4960  +      sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
         4961  +                       sqlite3_data_directory, winGetDirSep(), zUtf8);
         4962  +      sqlite3_free(zUtf8);
         4963  +      sqlite3_free(zOut);
  4953   4964       }
  4954         -    sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
  4955         -                     sqlite3_data_directory, winGetDirSep(), zOut);
  4956         -    sqlite3_free(zOut);
  4957   4965     }else{
  4958         -    if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
         4966  +    char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
         4967  +    if( !zOut ){
         4968  +      return SQLITE_IOERR_NOMEM;
         4969  +    }
         4970  +    if( cygwin_conv_path(
         4971  +            (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
         4972  +            zRelative, zOut, pVfs->mxPathname+1)<0 ){
         4973  +      sqlite3_free(zOut);
  4959   4974         return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
  4960   4975                            "winFullPathname2", zRelative);
         4976  +    }else{
         4977  +      char *zUtf8 = winConvertToUtf8Filename(zOut);
         4978  +      if( !zUtf8 ){
         4979  +        sqlite3_free(zOut);
         4980  +        return SQLITE_IOERR_NOMEM;
         4981  +      }
         4982  +      sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
         4983  +      sqlite3_free(zUtf8);
         4984  +      sqlite3_free(zOut);
  4961   4985       }
  4962   4986     }
  4963   4987     return SQLITE_OK;
  4964   4988   #endif
  4965   4989   
  4966   4990   #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
  4967   4991     SimulateIOError( return SQLITE_ERROR );

Changes to src/parse.y.

   850    850     }
   851    851   }
   852    852   expr(A) ::= ID(X) LP STAR RP(E). {
   853    853     A.pExpr = sqlite3ExprFunction(pParse, 0, &X);
   854    854     spanSet(&A,&X,&E);
   855    855   }
   856    856   term(A) ::= CTIME_KW(OP). {
   857         -  /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are
   858         -  ** treated as functions that return constants */
   859         -  A.pExpr = sqlite3ExprFunction(pParse, 0,&OP);
   860         -  if( A.pExpr ){
   861         -    A.pExpr->op = TK_CONST_FUNC;  
   862         -  }
          857  +  A.pExpr = sqlite3ExprFunction(pParse, 0, &OP);
   863    858     spanSet(&A, &OP, &OP);
   864    859   }
   865    860   
   866    861   %include {
   867    862     /* This routine constructs a binary expression node out of two ExprSpan
   868    863     ** objects and uses the result to populate a new ExprSpan object.
   869    864     */

Changes to src/pragma.c.

  1887   1887           if( pTab->pIndex==0 ) continue;
  1888   1888           pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
  1889   1889           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
  1890   1890           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1891   1891           sqlite3VdbeJumpHere(v, addr);
  1892   1892           sqlite3ExprCacheClear(pParse);
  1893   1893           sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
  1894         -                                   1, &iDataCur, &iIdxCur);
         1894  +                                   1, 0, &iDataCur, &iIdxCur);
  1895   1895           sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
  1896   1896           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1897   1897             sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
  1898   1898           }
  1899   1899           pParse->nMem = MAX(pParse->nMem, 8+j);
  1900   1900           sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
  1901   1901           loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);

Changes to src/prepare.c.

   519    519           break;
   520    520         }
   521    521       }
   522    522       assert( i>=0 && i<db->nDb );
   523    523     }
   524    524     return i;
   525    525   }
          526  +
          527  +/*
          528  +** Free all memory allocations in the pParse object
          529  +*/
          530  +void sqlite3ParserReset(Parse *pParse){
          531  +  if( pParse ) sqlite3ExprListDelete(pParse->db, pParse->pConstExpr);
          532  +}
   526    533   
   527    534   /*
   528    535   ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
   529    536   */
   530    537   static int sqlite3Prepare(
   531    538     sqlite3 *db,              /* Database handle. */
   532    539     const char *zSql,         /* UTF-8 encoded SQL statement. */
................................................................................
   677    684       TriggerPrg *pT = pParse->pTriggerPrg;
   678    685       pParse->pTriggerPrg = pT->pNext;
   679    686       sqlite3DbFree(db, pT);
   680    687     }
   681    688   
   682    689   end_prepare:
   683    690   
          691  +  sqlite3ParserReset(pParse);
   684    692     sqlite3StackFree(db, pParse);
   685    693     rc = sqlite3ApiExit(db, rc);
   686    694     assert( (rc&db->errMask)==rc );
   687    695     return rc;
   688    696   }
   689    697   static int sqlite3LockAndPrepare(
   690    698     sqlite3 *db,              /* Database handle. */

Changes to src/resolve.c.

   104    104     pDup = sqlite3ExprDup(db, pOrig, 0);
   105    105     if( pDup==0 ) return;
   106    106     if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
   107    107       incrAggFunctionDepth(pDup, nSubquery);
   108    108       pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
   109    109       if( pDup==0 ) return;
   110    110       ExprSetProperty(pDup, EP_Skip);
   111         -    if( pEList->a[iCol].iAlias==0 ){
   112         -      pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
          111  +    if( pEList->a[iCol].u.x.iAlias==0 ){
          112  +      pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
   113    113       }
   114         -    pDup->iTable = pEList->a[iCol].iAlias;
          114  +    pDup->iTable = pEList->a[iCol].u.x.iAlias;
   115    115     }
   116    116     if( pExpr->op==TK_COLLATE ){
   117    117       pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
   118    118     }
   119    119   
   120    120     /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
   121    121     ** prevents ExprDelete() from deleting the Expr structure itself,
................................................................................
   663    663           zColumn = pRight->pRight->u.zToken;
   664    664         }
   665    665         return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
   666    666       }
   667    667   
   668    668       /* Resolve function names
   669    669       */
   670         -    case TK_CONST_FUNC:
   671    670       case TK_FUNCTION: {
   672    671         ExprList *pList = pExpr->x.pList;    /* The argument list */
   673    672         int n = pList ? pList->nExpr : 0;    /* Number of arguments */
   674    673         int no_such_func = 0;       /* True if no such function exists */
   675    674         int wrong_num_args = 0;     /* True if wrong number of arguments */
   676    675         int is_agg = 0;             /* True if is an aggregate function */
   677    676         int auth;                   /* Authorization to use the function */
   678    677         int nId;                    /* Number of characters in function name */
   679    678         const char *zId;            /* The function name. */
   680    679         FuncDef *pDef;              /* Information about the function */
   681    680         u8 enc = ENC(pParse->db);   /* The database encoding */
   682    681   
   683         -      testcase( pExpr->op==TK_CONST_FUNC );
   684    682         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   685    683         notValidPartIdxWhere(pParse, pNC, "functions");
   686    684         zId = pExpr->u.zToken;
   687    685         nId = sqlite3Strlen30(zId);
   688    686         pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
   689    687         if( pDef==0 ){
   690    688           pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
................................................................................
   721    719               sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
   722    720                                       pDef->zName);
   723    721               pNC->nErr++;
   724    722             }
   725    723             pExpr->op = TK_NULL;
   726    724             return WRC_Prune;
   727    725           }
          726  +        if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Constant);
   728    727         }
   729    728   #endif
   730    729         if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
   731    730           sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
   732    731           pNC->nErr++;
   733    732           is_agg = 0;
   734    733         }else if( no_such_func && pParse->db->init.busy==0 ){
................................................................................
   972    971             pItem->pExpr = pNew;
   973    972           }else{
   974    973             assert( pItem->pExpr->op==TK_COLLATE );
   975    974             assert( pItem->pExpr->pLeft==pE );
   976    975             pItem->pExpr->pLeft = pNew;
   977    976           }
   978    977           sqlite3ExprDelete(db, pE);
   979         -        pItem->iOrderByCol = (u16)iCol;
          978  +        pItem->u.x.iOrderByCol = (u16)iCol;
   980    979           pItem->done = 1;
   981    980         }else{
   982    981           moreToDo = 1;
   983    982         }
   984    983       }
   985    984       pSelect = pSelect->pNext;
   986    985     }
................................................................................
   993    992     }
   994    993     return 0;
   995    994   }
   996    995   
   997    996   /*
   998    997   ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
   999    998   ** the SELECT statement pSelect.  If any term is reference to a
  1000         -** result set expression (as determined by the ExprList.a.iOrderByCol field)
  1001         -** then convert that term into a copy of the corresponding result set
          999  +** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
         1000  +** field) then convert that term into a copy of the corresponding result set
  1002   1001   ** column.
  1003   1002   **
  1004   1003   ** If any errors are detected, add an error message to pParse and
  1005   1004   ** return non-zero.  Return zero if no errors are seen.
  1006   1005   */
  1007   1006   int sqlite3ResolveOrderGroupBy(
  1008   1007     Parse *pParse,        /* Parsing context.  Leave error messages here */
................................................................................
  1021   1020       sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
  1022   1021       return 1;
  1023   1022     }
  1024   1023   #endif
  1025   1024     pEList = pSelect->pEList;
  1026   1025     assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
  1027   1026     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
  1028         -    if( pItem->iOrderByCol ){
  1029         -      if( pItem->iOrderByCol>pEList->nExpr ){
         1027  +    if( pItem->u.x.iOrderByCol ){
         1028  +      if( pItem->u.x.iOrderByCol>pEList->nExpr ){
  1030   1029           resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
  1031   1030           return 1;
  1032   1031         }
  1033         -      resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
         1032  +      resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
  1034   1033       }
  1035   1034     }
  1036   1035     return 0;
  1037   1036   }
  1038   1037   
  1039   1038   /*
  1040   1039   ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
................................................................................
  1075   1074       if( zType[0]!='G' ){
  1076   1075         iCol = resolveAsName(pParse, pSelect->pEList, pE2);
  1077   1076         if( iCol>0 ){
  1078   1077           /* If an AS-name match is found, mark this ORDER BY column as being
  1079   1078           ** a copy of the iCol-th result-set column.  The subsequent call to
  1080   1079           ** sqlite3ResolveOrderGroupBy() will convert the expression to a
  1081   1080           ** copy of the iCol-th result-set expression. */
  1082         -        pItem->iOrderByCol = (u16)iCol;
         1081  +        pItem->u.x.iOrderByCol = (u16)iCol;
  1083   1082           continue;
  1084   1083         }
  1085   1084       }
  1086   1085       if( sqlite3ExprIsInteger(pE2, &iCol) ){
  1087   1086         /* The ORDER BY term is an integer constant.  Again, set the column
  1088   1087         ** number so that sqlite3ResolveOrderGroupBy() will convert the
  1089   1088         ** order-by term to a copy of the result-set expression */
  1090   1089         if( iCol<1 || iCol>0xffff ){
  1091   1090           resolveOutOfRangeError(pParse, zType, i+1, nResult);
  1092   1091           return 1;
  1093   1092         }
  1094         -      pItem->iOrderByCol = (u16)iCol;
         1093  +      pItem->u.x.iOrderByCol = (u16)iCol;
  1095   1094         continue;
  1096   1095       }
  1097   1096   
  1098   1097       /* Otherwise, treat the ORDER BY term as an ordinary expression */
  1099         -    pItem->iOrderByCol = 0;
         1098  +    pItem->u.x.iOrderByCol = 0;
  1100   1099       if( sqlite3ResolveExprNames(pNC, pE) ){
  1101   1100         return 1;
  1102   1101       }
  1103   1102       for(j=0; j<pSelect->pEList->nExpr; j++){
  1104   1103         if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
  1105         -        pItem->iOrderByCol = j+1;
         1104  +        pItem->u.x.iOrderByCol = j+1;
  1106   1105         }
  1107   1106       }
  1108   1107     }
  1109   1108     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
  1110   1109   }
  1111   1110   
  1112   1111   /*

Changes to src/select.c.

   595    595         sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
   596    596       }
   597    597     }else if( eDest!=SRT_Exists ){
   598    598       /* If the destination is an EXISTS(...) expression, the actual
   599    599       ** values returned by the SELECT are not required.
   600    600       */
   601    601       sqlite3ExprCacheClear(pParse);
   602         -    sqlite3ExprCodeExprList(pParse, pEList, regResult, eDest==SRT_Output);
          602  +    sqlite3ExprCodeExprList(pParse, pEList, regResult,
          603  +                            (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
   603    604     }
   604    605     nColumn = nResultCol;
   605    606   
   606    607     /* If the DISTINCT keyword was present on the SELECT statement
   607    608     ** and this row has been seen before, then do not make this row
   608    609     ** part of the result.
   609    610     */
................................................................................
  2368   2369     ** the ORDER BY clause covers every term of the result set.  Add
  2369   2370     ** terms to the ORDER BY clause as necessary.
  2370   2371     */
  2371   2372     if( op!=TK_ALL ){
  2372   2373       for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
  2373   2374         struct ExprList_item *pItem;
  2374   2375         for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
  2375         -        assert( pItem->iOrderByCol>0 );
  2376         -        if( pItem->iOrderByCol==i ) break;
         2376  +        assert( pItem->u.x.iOrderByCol>0 );
         2377  +        if( pItem->u.x.iOrderByCol==i ) break;
  2377   2378         }
  2378   2379         if( j==nOrderBy ){
  2379   2380           Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
  2380   2381           if( pNew==0 ) return SQLITE_NOMEM;
  2381   2382           pNew->flags |= EP_IntValue;
  2382   2383           pNew->u.iValue = i;
  2383   2384           pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
  2384         -        if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
         2385  +        if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
  2385   2386         }
  2386   2387       }
  2387   2388     }
  2388   2389   
  2389   2390     /* Compute the comparison permutation and keyinfo that is used with
  2390   2391     ** the permutation used to determine if the next
  2391   2392     ** row of results comes from selectA or selectB.  Also add explicit
................................................................................
  2393   2394     ** to the right and the left are evaluated, they use the correct
  2394   2395     ** collation.
  2395   2396     */
  2396   2397     aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
  2397   2398     if( aPermute ){
  2398   2399       struct ExprList_item *pItem;
  2399   2400       for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
  2400         -      assert( pItem->iOrderByCol>0  && pItem->iOrderByCol<=p->pEList->nExpr );
  2401         -      aPermute[i] = pItem->iOrderByCol - 1;
         2401  +      assert( pItem->u.x.iOrderByCol>0
         2402  +          && pItem->u.x.iOrderByCol<=p->pEList->nExpr );
         2403  +      aPermute[i] = pItem->u.x.iOrderByCol - 1;
  2402   2404       }
  2403   2405       pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1);
  2404   2406       if( pKeyMerge ){
  2405   2407         for(i=0; i<nOrderBy; i++){
  2406   2408           CollSeq *pColl;
  2407   2409           Expr *pTerm = pOrderBy->a[i].pExpr;
  2408   2410           if( pTerm->flags & EP_Collate ){
................................................................................
  2974   2976         testcase( pSub1->pSrc->nSrc>1 );
  2975   2977       }
  2976   2978   
  2977   2979       /* Restriction 18. */
  2978   2980       if( p->pOrderBy ){
  2979   2981         int ii;
  2980   2982         for(ii=0; ii<p->pOrderBy->nExpr; ii++){
  2981         -        if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
         2983  +        if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
  2982   2984         }
  2983   2985       }
  2984   2986     }
  2985   2987   
  2986   2988     /***** If we reach this point, flattening is permitted. *****/
  2987   2989   
  2988   2990     /* Authorize the subquery */
................................................................................
  3880   3882       int addrNext = 0;
  3881   3883       int regAgg;
  3882   3884       ExprList *pList = pF->pExpr->x.pList;
  3883   3885       assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
  3884   3886       if( pList ){
  3885   3887         nArg = pList->nExpr;
  3886   3888         regAgg = sqlite3GetTempRange(pParse, nArg);
  3887         -      sqlite3ExprCodeExprList(pParse, pList, regAgg, 1);
         3889  +      sqlite3ExprCodeExprList(pParse, pList, regAgg, SQLITE_ECEL_DUP);
  3888   3890       }else{
  3889   3891         nArg = 0;
  3890   3892         regAgg = 0;
  3891   3893       }
  3892   3894       if( pF->iDistinct>=0 ){
  3893   3895         addrNext = sqlite3VdbeMakeLabel(v);
  3894   3896         assert( nArg==1 );
................................................................................
  4381   4383       ** GROUP BY clause.
  4382   4384       */
  4383   4385       if( pGroupBy ){
  4384   4386         int k;                        /* Loop counter */
  4385   4387         struct ExprList_item *pItem;  /* For looping over expression in a list */
  4386   4388   
  4387   4389         for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
  4388         -        pItem->iAlias = 0;
         4390  +        pItem->u.x.iAlias = 0;
  4389   4391         }
  4390   4392         for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
  4391         -        pItem->iAlias = 0;
         4393  +        pItem->u.x.iAlias = 0;
  4392   4394         }
  4393   4395         if( p->nSelectRow>100 ) p->nSelectRow = 100;
  4394   4396       }else{
  4395   4397         p->nSelectRow = 1;
  4396   4398       }
  4397   4399   
  4398   4400    

Changes to src/shell.c.

   462    462     const char *zDbFilename;    /* name of the database file */
   463    463     char *zFreeOnClose;         /* Filename to free when closing */
   464    464     const char *zVfs;           /* Name of VFS to use */
   465    465     sqlite3_stmt *pStmt;   /* Current statement if any. */
   466    466     FILE *pLog;            /* Write log output here */
   467    467     int *aiIndent;         /* Array of indents used in MODE_Explain */
   468    468     int nIndent;           /* Size of array aiIndent[] */
          469  +  int iIndent;           /* Index of current op in aiIndent[] */
   469    470   };
   470    471   
   471    472   /*
   472    473   ** These are the allowed modes.
   473    474   */
   474    475   #define MODE_Line     0  /* One column per line.  Blank line between records */
   475    476   #define MODE_Column   1  /* One record per line in neat columns */
................................................................................
   767    768           }else{
   768    769              w = 10;
   769    770           }
   770    771           if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
   771    772             w = strlen30(azArg[i]);
   772    773           }
   773    774           if( i==1 && p->aiIndent && p->pStmt ){
   774         -          int iOp = sqlite3_column_int(p->pStmt, 0);
   775         -          if( iOp<p->nIndent ){
   776         -            fprintf(p->out, "%*.s", p->aiIndent[iOp], "");
          775  +          if( p->iIndent<p->nIndent ){
          776  +            fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
   777    777             }
          778  +          p->iIndent++;
   778    779           }
   779    780           if( w<0 ){
   780    781             fprintf(p->out,"%*.*s%s",-w,-w,
   781    782                 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
   782    783           }else{
   783    784             fprintf(p->out,"%-*.*s%s",w,w,
   784    785                 azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
................................................................................
  1169   1170   **
  1170   1171   ** The indenting rules are:
  1171   1172   **
  1172   1173   **     * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
  1173   1174   **       all opcodes that occur between the p2 jump destination and the opcode
  1174   1175   **       itself by 2 spaces.
  1175   1176   **
  1176         -**     * For each "Goto", if the jump destination is a "Yield", "SeekGt",
  1177         -**       or "SeekLt" instruction that occurs earlier in the program than
  1178         -**       the Goto itself, indent all opcodes between the earlier instruction
         1177  +**     * For each "Goto", if the jump destination is earlier in the program
         1178  +**       and ends on one of:
         1179  +**          Yield  SeekGt  SeekLt  RowSetRead
         1180  +**       then indent all opcodes between the earlier instruction
  1179   1181   **       and "Goto" by 2 spaces.
  1180   1182   */
  1181   1183   static void explain_data_prepare(struct callback_data *p, sqlite3_stmt *pSql){
  1182   1184     const char *zSql;               /* The text of the SQL statement */
  1183   1185     const char *z;                  /* Used to check if this is an EXPLAIN */
  1184   1186     int *abYield = 0;               /* True if op is an OP_Yield */
  1185   1187     int nAlloc = 0;                 /* Allocated size of p->aiIndent[], abYield */
  1186         -  int iOp;
         1188  +  int iOp;                        /* Index of operation in p->aiIndent[] */
  1187   1189   
  1188         -  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", 0 };
  1189         -  const char *azYield[] = { "Yield", "SeekLt", "SeekGt", 0 };
         1190  +  const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
         1191  +  const char *azYield[] = { "Yield", "SeekLt", "SeekGt", "RowSetRead", 0 };
  1190   1192     const char *azGoto[] = { "Goto", 0 };
  1191   1193   
  1192   1194     /* Try to figure out if this is really an EXPLAIN statement. If this
  1193   1195     ** cannot be verified, return early.  */
  1194   1196     zSql = sqlite3_sql(pSql);
  1195   1197     if( zSql==0 ) return;
  1196   1198     for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
  1197   1199     if( sqlite3_strnicmp(z, "explain", 7) ) return;
  1198   1200   
  1199   1201     for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
  1200   1202       int i;
         1203  +    int iAddr = sqlite3_column_int(pSql, 0);
  1201   1204       const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
         1205  +
         1206  +    /* Set p2 to the P2 field of the current opcode. Then, assuming that
         1207  +    ** p2 is an instruction address, set variable p2op to the index of that
         1208  +    ** instruction in the aiIndent[] array. p2 and p2op may be different if
         1209  +    ** the current instruction is part of a sub-program generated by an
         1210  +    ** SQL trigger or foreign key.  */
  1202   1211       int p2 = sqlite3_column_int(pSql, 3);
         1212  +    int p2op = (p2 + (iOp-iAddr));
  1203   1213   
  1204   1214       /* Grow the p->aiIndent array as required */
  1205   1215       if( iOp>=nAlloc ){
  1206   1216         nAlloc += 100;
  1207   1217         p->aiIndent = (int*)sqlite3_realloc(p->aiIndent, nAlloc*sizeof(int));
  1208   1218         abYield = (int*)sqlite3_realloc(abYield, nAlloc*sizeof(int));
  1209   1219       }
  1210   1220       abYield[iOp] = str_in_array(zOp, azYield);
  1211   1221       p->aiIndent[iOp] = 0;
  1212   1222       p->nIndent = iOp+1;
  1213   1223   
  1214   1224       if( str_in_array(zOp, azNext) ){
  1215         -      for(i=p2; i<iOp; i++) p->aiIndent[i] += 2;
         1225  +      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
  1216   1226       }
  1217         -    if( str_in_array(zOp, azGoto) && p2<p->nIndent && abYield[p2] ){
  1218         -      for(i=p2+1; i<iOp; i++) p->aiIndent[i] += 2;
         1227  +    if( str_in_array(zOp, azGoto) && p2op<p->nIndent && abYield[p2op] ){
         1228  +      for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
  1219   1229       }
  1220   1230     }
  1221   1231   
         1232  +  p->iIndent = 0;
  1222   1233     sqlite3_free(abYield);
  1223   1234     sqlite3_reset(pSql);
  1224   1235   }
  1225   1236   
  1226   1237   /*
  1227   1238   ** Free the array allocated by explain_data_prepare().
  1228   1239   */
  1229   1240   static void explain_data_delete(struct callback_data *p){
  1230   1241     sqlite3_free(p->aiIndent);
  1231   1242     p->aiIndent = 0;
  1232   1243     p->nIndent = 0;
         1244  +  p->iIndent = 0;
  1233   1245   }
  1234   1246   
  1235   1247   /*
  1236   1248   ** Execute a statement or set of statements.  Print 
  1237   1249   ** any result rows/columns depending on the current mode 
  1238   1250   ** set via the supplied callback.
  1239   1251   **
................................................................................
  1291   1303           if( zExplain && zExplain[0] ){
  1292   1304             fprintf(pArg->out, "%s", zExplain);
  1293   1305           }
  1294   1306         }
  1295   1307   
  1296   1308         /* If the shell is currently in ".explain" mode, gather the extra
  1297   1309         ** data required to add indents to the output.*/
  1298         -      if( pArg->mode==MODE_Explain ){
         1310  +      if( pArg && pArg->mode==MODE_Explain ){
  1299   1311           explain_data_prepare(pArg, pStmt);
  1300   1312         }
  1301   1313   
  1302   1314         /* perform the first step.  this will tell us if we
  1303   1315         ** have a result set or not and how wide it is.
  1304   1316         */
  1305   1317         rc = sqlite3_step(pStmt);

Changes to src/sqlite.h.in.

   361    361   ** is not changed.
   362    362   **
   363    363   ** Restrictions:
   364    364   **
   365    365   ** <ul>
   366    366   ** <li> The application must insure that the 1st parameter to sqlite3_exec()
   367    367   **      is a valid and open [database connection].
   368         -** <li> The application must not close [database connection] specified by
          368  +** <li> The application must not close the [database connection] specified by
   369    369   **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
   370    370   ** <li> The application must not modify the SQL statement text passed into
   371    371   **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
   372    372   ** </ul>
   373    373   */
   374    374   int sqlite3_exec(
   375    375     sqlite3*,                                  /* An open database */
................................................................................
   438    438   ** address this, newer versions of SQLite (version 3.3.8 and later) include
   439    439   ** support for additional result codes that provide more detailed information
   440    440   ** about errors. The extended result codes are enabled or disabled
   441    441   ** on a per database connection basis using the
   442    442   ** [sqlite3_extended_result_codes()] API.
   443    443   **
   444    444   ** Some of the available extended result codes are listed here.
   445         -** One may expect the number of extended result codes will be expand
          445  +** One may expect the number of extended result codes will increase
   446    446   ** over time.  Software that uses extended result codes should expect
   447    447   ** to see new result codes in future releases of SQLite.
   448    448   **
   449    449   ** The SQLITE_OK result code will never be extended.  It will always
   450    450   ** be exactly zero.
   451    451   */
   452    452   #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
................................................................................
  1376   1376   ** a memory allocation given a particular requested size.  Most memory
  1377   1377   ** allocators round up memory allocations at least to the next multiple
  1378   1378   ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
  1379   1379   ** Every memory allocation request coming in through [sqlite3_malloc()]
  1380   1380   ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0, 
  1381   1381   ** that causes the corresponding memory allocation to fail.
  1382   1382   **
  1383         -** The xInit method initializes the memory allocator.  (For example,
         1383  +** The xInit method initializes the memory allocator.  For example,
  1384   1384   ** it might allocate any require mutexes or initialize internal data
  1385   1385   ** structures.  The xShutdown method is invoked (indirectly) by
  1386   1386   ** [sqlite3_shutdown()] and should deallocate any resources acquired
  1387   1387   ** by xInit.  The pAppData pointer is used as the only parameter to
  1388   1388   ** xInit and xShutdown.
  1389   1389   **
  1390   1390   ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
................................................................................
  1678   1678   ** either the [PRAGMA mmap_size] command, or by using the
  1679   1679   ** [SQLITE_FCNTL_MMAP_SIZE] file control.  ^(The maximum allowed mmap size
  1680   1680   ** cannot be changed at run-time.  Nor may the maximum allowed mmap size
  1681   1681   ** exceed the compile-time maximum mmap size set by the
  1682   1682   ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
  1683   1683   ** ^If either argument to this option is negative, then that argument is
  1684   1684   ** changed to its compile-time default.
         1685  +**
         1686  +** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
         1687  +** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
         1688  +** <dd>^This option is only available if SQLite is compiled for Windows
         1689  +** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
         1690  +** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
         1691  +** that specifies the maximum size of the created heap.
  1685   1692   ** </dl>
  1686   1693   */
  1687   1694   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  1688   1695   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  1689   1696   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  1690   1697   #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  1691   1698   #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
................................................................................
  1702   1709   #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1703   1710   #define SQLITE_CONFIG_URI          17  /* int */
  1704   1711   #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1705   1712   #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
  1706   1713   #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
  1707   1714   #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
  1708   1715   #define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
         1716  +#define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
  1709   1717   
  1710   1718   /*
  1711   1719   ** CAPI3REF: Database Connection Configuration Options
  1712   1720   **
  1713   1721   ** These constants are the available integer configuration options that
  1714   1722   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1715   1723   **
................................................................................
  3102   3110   ** then the statement will be automatically recompiled, as if there had been 
  3103   3111   ** a schema change, on the first  [sqlite3_step()] call following any change
  3104   3112   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3105   3113   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3106   3114   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3107   3115   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3108   3116   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  3109         -** the 
  3110   3117   ** </li>
  3111   3118   ** </ol>
  3112   3119   */
  3113   3120   int sqlite3_prepare(
  3114   3121     sqlite3 *db,            /* Database handle */
  3115   3122     const char *zSql,       /* SQL statement, UTF-8 encoded */
  3116   3123     int nByte,              /* Maximum length of zSql in bytes. */
................................................................................
  3832   3839   ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
  3833   3840   ** with calls to sqlite3_column_bytes().
  3834   3841   **
  3835   3842   ** ^The pointers returned are valid until a type conversion occurs as
  3836   3843   ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
  3837   3844   ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
  3838   3845   ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
  3839         -** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
         3846  +** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
  3840   3847   ** [sqlite3_free()].
  3841   3848   **
  3842   3849   ** ^(If a memory allocation error occurs during the evaluation of any
  3843   3850   ** of these routines, a default value is returned.  The default value
  3844   3851   ** is either the integer 0, the floating point number 0.0, or a NULL
  3845   3852   ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
  3846   3853   ** [SQLITE_NOMEM].)^
................................................................................
  4910   4917   int sqlite3_release_memory(int);
  4911   4918   
  4912   4919   /*
  4913   4920   ** CAPI3REF: Free Memory Used By A Database Connection
  4914   4921   **
  4915   4922   ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
  4916   4923   ** memory as possible from database connection D. Unlike the
  4917         -** [sqlite3_release_memory()] interface, this interface is effect even
  4918         -** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
         4924  +** [sqlite3_release_memory()] interface, this interface is in effect even
         4925  +** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
  4919   4926   ** omitted.
  4920   4927   **
  4921   4928   ** See also: [sqlite3_release_memory()]
  4922   4929   */
  4923   4930   int sqlite3_db_release_memory(sqlite3*);
  4924   4931   
  4925   4932   /*

Changes to src/sqliteInt.h.

  1081   1081   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
  1082   1082   #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
  1083   1083   #else
  1084   1084   #define OptimizationDisabled(db, mask)  0
  1085   1085   #define OptimizationEnabled(db, mask)   1
  1086   1086   #endif
  1087   1087   
         1088  +/*
         1089  +** Return true if it OK to factor constant expressions into the initialization
         1090  +** code. The argument is a Parse object for the code generator.
         1091  +*/
         1092  +#define ConstFactorOk(P) \
         1093  +  ((P)->cookieGoto>0 && OptimizationEnabled((P)->db,SQLITE_FactorOutConst))
         1094  +
  1088   1095   /*
  1089   1096   ** Possible values for the sqlite.magic field.
  1090   1097   ** The numbers are obtained at random and have no special meaning, other
  1091   1098   ** than being distinct from one another.
  1092   1099   */
  1093   1100   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
  1094   1101   #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
................................................................................
  1147   1154   #define SQLITE_FUNC_EPHEM    0x010 /* Ephemeral.  Delete with VDBE */
  1148   1155   #define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */
  1149   1156   #define SQLITE_FUNC_LENGTH   0x040 /* Built-in length() function */
  1150   1157   #define SQLITE_FUNC_TYPEOF   0x080 /* Built-in typeof() function */
  1151   1158   #define SQLITE_FUNC_COUNT    0x100 /* Built-in count(*) aggregate */
  1152   1159   #define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */
  1153   1160   #define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */
         1161  +#define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */
  1154   1162   
  1155   1163   /*
  1156   1164   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
  1157   1165   ** used to create the initializers for the FuncDef structures.
  1158   1166   **
  1159   1167   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
  1160   1168   **     Used to create a scalar function definition of a function zName 
  1161   1169   **     implemented by C function xFunc that accepts nArg arguments. The
  1162   1170   **     value passed as iArg is cast to a (void*) and made available
  1163   1171   **     as the user-data (sqlite3_user_data()) for the function. If 
  1164   1172   **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
  1165   1173   **
         1174  +**   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
         1175  +**     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
         1176  +**
  1166   1177   **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
  1167   1178   **     Used to create an aggregate function definition implemented by
  1168   1179   **     the C functions xStep and xFinal. The first four parameters
  1169   1180   **     are interpreted in the same way as the first 4 parameters to
  1170   1181   **     FUNCTION().
  1171   1182   **
  1172   1183   **   LIKEFUNC(zName, nArg, pArg, flags)
................................................................................
  1174   1185   **     that accepts nArg arguments and is implemented by a call to C 
  1175   1186   **     function likeFunc. Argument pArg is cast to a (void *) and made
  1176   1187   **     available as the function user-data (sqlite3_user_data()). The
  1177   1188   **     FuncDef.flags variable is set to the value passed as the flags
  1178   1189   **     parameter.
  1179   1190   */
  1180   1191   #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
         1192  +  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
         1193  +   SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
         1194  +#define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
  1181   1195     {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  1182   1196      SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
  1183   1197   #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
  1184         -  {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags, \
         1198  +  {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
  1185   1199      SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
  1186   1200   #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
  1187         -  {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
         1201  +  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
  1188   1202      pArg, 0, xFunc, 0, 0, #zName, 0, 0}
  1189   1203   #define LIKEFUNC(zName, nArg, arg, flags) \
  1190         -  {nArg, SQLITE_UTF8|flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
         1204  +  {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
         1205  +   (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
  1191   1206   #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
  1192   1207     {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
  1193   1208      SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
  1194   1209   
  1195   1210   /*
  1196   1211   ** All current savepoints are stored in a linked list starting at
  1197   1212   ** sqlite3.pSavepoint. The first element in the list is the most recently
................................................................................
  1833   1848   #define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
  1834   1849   #define EP_Error     0x000008 /* Expression contains one or more errors */
  1835   1850   #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
  1836   1851   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  1837   1852   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  1838   1853   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
  1839   1854   #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
  1840         -#define EP_FixedDest 0x000200 /* Result needed in a specific register */
         1855  +      /* unused      0x000200 */
  1841   1856   #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
  1842   1857   #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
  1843   1858   #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
  1844   1859   #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
  1845   1860   #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
  1846   1861   #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
  1847   1862   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
  1848   1863   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
  1849   1864   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
         1865  +#define EP_Constant  0x080000 /* Node is a constant */
  1850   1866   
  1851   1867   /*
  1852   1868   ** These macros can be used to test, set, or clear bits in the 
  1853   1869   ** Expr.flags field.
  1854   1870   */
  1855   1871   #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
  1856   1872   #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
................................................................................
  1904   1920     struct ExprList_item { /* For each expression in the list */
  1905   1921       Expr *pExpr;            /* The list of expressions */
  1906   1922       char *zName;            /* Token associated with this expression */
  1907   1923       char *zSpan;            /* Original text of the expression */
  1908   1924       u8 sortOrder;           /* 1 for DESC or 0 for ASC */
  1909   1925       unsigned done :1;       /* A flag to indicate when processing is finished */
  1910   1926       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
  1911         -    u16 iOrderByCol;        /* For ORDER BY, column number in result set */
  1912         -    u16 iAlias;             /* Index into Parse.aAlias[] for zName */
         1927  +    unsigned reusable :1;   /* Constant expression is reusable */
         1928  +    union {
         1929  +      struct {
         1930  +        u16 iOrderByCol;      /* For ORDER BY, column number in result set */
         1931  +        u16 iAlias;           /* Index into Parse.aAlias[] for zName */
         1932  +      } x;
         1933  +      int iConstExprReg;      /* Register in which Expr value is cached */
         1934  +    } u;
  1913   1935     } *a;                  /* Alloc a power of two greater or equal to nExpr */
  1914   1936   };
  1915   1937   
  1916   1938   /*
  1917   1939   ** An instance of this structure is used by the parser to record both
  1918   1940   ** the parse tree for an expression and the span of input text for an
  1919   1941   ** expression.
................................................................................
  2282   2304       int iTable;           /* Table cursor number */
  2283   2305       int iColumn;          /* Table column number */
  2284   2306       u8 tempReg;           /* iReg is a temp register that needs to be freed */
  2285   2307       int iLevel;           /* Nesting level */
  2286   2308       int iReg;             /* Reg with value of this column. 0 means none. */
  2287   2309       int lru;              /* Least recently used entry has the smallest value */
  2288   2310     } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
         2311  +  ExprList *pConstExpr;/* Constant expressions */
  2289   2312     yDbMask writeMask;   /* Start a write transaction on these databases */
  2290   2313     yDbMask cookieMask;  /* Bitmask of schema verified databases */
  2291   2314     int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  2292   2315     int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
  2293   2316     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
  2294   2317     int regRoot;         /* Register holding root page number for new objects */
  2295   2318     int nMaxArg;         /* Max args passed to user function by sub-program */
................................................................................
  2896   2919   void sqlite3ExprCacheStore(Parse*, int, int, int);
  2897   2920   void sqlite3ExprCachePush(Parse*);
  2898   2921   void sqlite3ExprCachePop(Parse*, int);
  2899   2922   void sqlite3ExprCacheRemove(Parse*, int, int);
  2900   2923   void sqlite3ExprCacheClear(Parse*);
  2901   2924   void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  2902   2925   int sqlite3ExprCode(Parse*, Expr*, int);
         2926  +void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
  2903   2927   int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
  2904   2928   int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  2905   2929   int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  2906         -void sqlite3ExprCodeConstants(Parse*, Expr*);
  2907         -int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int);
         2930  +int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
         2931  +#define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
         2932  +#define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
  2908   2933   void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  2909   2934   void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  2910   2935   Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  2911   2936   Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
  2912   2937   Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
  2913   2938   Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  2914   2939   void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
................................................................................
  2945   2970   int sqlite3IsRowid(const char*);
  2946   2971   void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
  2947   2972   void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
  2948   2973   int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
  2949   2974   void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
  2950   2975                                        u8,u8,int,int*);
  2951   2976   void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
  2952         -int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, int*, int*);
         2977  +int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
  2953   2978   void sqlite3BeginWriteOperation(Parse*, int, int);
  2954   2979   void sqlite3MultiWrite(Parse*);
  2955   2980   void sqlite3MayAbort(Parse*);
  2956   2981   void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
  2957   2982   void sqlite3UniqueConstraint(Parse*, int, Index*);
  2958   2983   void sqlite3RowidConstraint(Parse*, int, Table*);
  2959   2984   Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
................................................................................
  3267   3292   int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
  3268   3293   int sqlite3VtabBegin(sqlite3 *, VTable *);
  3269   3294   FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
  3270   3295   void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
  3271   3296   sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
  3272   3297   int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
  3273   3298   int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
         3299  +void sqlite3ParserReset(Parse*);
  3274   3300   int sqlite3Reprepare(Vdbe*);
  3275   3301   void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
  3276   3302   CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
  3277   3303   int sqlite3TempInMemory(const sqlite3*);
  3278   3304   const char *sqlite3JournalModename(int);
  3279   3305   #ifndef SQLITE_OMIT_WAL
  3280   3306     int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);

Changes to src/tclsqlite.c.

   421    421   /*
   422    422   ** Find an SqlFunc structure with the given name.  Or create a new
   423    423   ** one if an existing one cannot be found.  Return a pointer to the
   424    424   ** structure.
   425    425   */
   426    426   static SqlFunc *findSqlFunc(SqliteDb *pDb, const char *zName){
   427    427     SqlFunc *p, *pNew;
   428         -  int i;
   429         -  pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + strlen30(zName) + 1 );
          428  +  int nName = strlen30(zName);
          429  +  pNew = (SqlFunc*)Tcl_Alloc( sizeof(*pNew) + nName + 1 );
   430    430     pNew->zName = (char*)&pNew[1];
   431         -  for(i=0; zName[i]; i++){ pNew->zName[i] = tolower(zName[i]); }
   432         -  pNew->zName[i] = 0;
          431  +  memcpy(pNew->zName, zName, nName+1);
   433    432     for(p=pDb->pFunc; p; p=p->pNext){ 
   434         -    if( strcmp(p->zName, pNew->zName)==0 ){
          433  +    if( sqlite3_stricmp(p->zName, pNew->zName)==0 ){
   435    434         Tcl_Free((char*)pNew);
   436    435         return p;
   437    436       }
   438    437     }
   439    438     pNew->interp = pDb->interp;
   440    439     pNew->pDb = pDb;
   441    440     pNew->pScript = 0;
................................................................................
  1123   1122   ){
  1124   1123     const char *zSql = zIn;         /* Pointer to first SQL statement in zIn */
  1125   1124     sqlite3_stmt *pStmt;            /* Prepared statement object */
  1126   1125     SqlPreparedStmt *pPreStmt;      /* Pointer to cached statement */
  1127   1126     int nSql;                       /* Length of zSql in bytes */
  1128   1127     int nVar;                       /* Number of variables in statement */
  1129   1128     int iParm = 0;                  /* Next free entry in apParm */
         1129  +  char c;
  1130   1130     int i;
  1131   1131     Tcl_Interp *interp = pDb->interp;
  1132   1132   
  1133   1133     *ppPreStmt = 0;
  1134   1134   
  1135   1135     /* Trim spaces from the start of zSql and calculate the remaining length. */
  1136         -  while( isspace(zSql[0]) ){ zSql++; }
         1136  +  while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; }
  1137   1137     nSql = strlen30(zSql);
  1138   1138   
  1139   1139     for(pPreStmt = pDb->stmtList; pPreStmt; pPreStmt=pPreStmt->pNext){
  1140   1140       int n = pPreStmt->nSql;
  1141   1141       if( nSql>=n 
  1142   1142           && memcmp(pPreStmt->zSql, zSql, n)==0
  1143   1143           && (zSql[n]==0 || zSql[n-1]==';')

Changes to src/test_config.c.

    58     58   #endif
    59     59   
    60     60   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
    61     61     Tcl_SetVar2(interp, "sqlite_options", "curdir", "1", TCL_GLOBAL_ONLY);
    62     62   #else
    63     63     Tcl_SetVar2(interp, "sqlite_options", "curdir", "0", TCL_GLOBAL_ONLY);
    64     64   #endif
           65  +
           66  +#ifdef SQLITE_WIN32_MALLOC
           67  +  Tcl_SetVar2(interp, "sqlite_options", "win32malloc", "1", TCL_GLOBAL_ONLY);
           68  +#else
           69  +  Tcl_SetVar2(interp, "sqlite_options", "win32malloc", "0", TCL_GLOBAL_ONLY);
           70  +#endif
    65     71   
    66     72   #ifdef SQLITE_DEBUG
    67     73     Tcl_SetVar2(interp, "sqlite_options", "debug", "1", TCL_GLOBAL_ONLY);
    68     74   #else
    69     75     Tcl_SetVar2(interp, "sqlite_options", "debug", "0", TCL_GLOBAL_ONLY);
    70     76   #endif
    71     77   

Changes to src/test_malloc.c.

  1125   1125       zBuf = realloc(zBuf, nByte);
  1126   1126       rc = sqlite3_config(SQLITE_CONFIG_HEAP, zBuf, nByte, nMinAlloc);
  1127   1127     }
  1128   1128   
  1129   1129     Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
  1130   1130     return TCL_OK;
  1131   1131   }
         1132  +
         1133  +/*
         1134  +** Usage:    sqlite3_config_heap_size NBYTE
         1135  +*/
         1136  +static int test_config_heap_size(
         1137  +  void * clientData, 
         1138  +  Tcl_Interp *interp,
         1139  +  int objc,
         1140  +  Tcl_Obj *CONST objv[]
         1141  +){
         1142  +  int nByte;         /* Size to pass to sqlite3_config() */
         1143  +  int rc;            /* Return code of sqlite3_config() */
         1144  +
         1145  +  Tcl_Obj * CONST *aArg = &objv[1];
         1146  +  int nArg = objc-1;
         1147  +
         1148  +  if( nArg!=1 ){
         1149  +    Tcl_WrongNumArgs(interp, 1, objv, "NBYTE");
         1150  +    return TCL_ERROR;
         1151  +  }
         1152  +  if( Tcl_GetIntFromObj(interp, aArg[0], &nByte) ) return TCL_ERROR;
         1153  +
         1154  +  rc = sqlite3_config(SQLITE_CONFIG_WIN32_HEAPSIZE, nByte);
         1155  +
         1156  +  Tcl_SetResult(interp, (char *)sqlite3ErrName(rc), TCL_VOLATILE);
         1157  +  return TCL_OK;
         1158  +}
  1132   1159   
  1133   1160   /*
  1134   1161   ** Usage:    sqlite3_config_error  [DB]
  1135   1162   **
  1136   1163   ** Invoke sqlite3_config() or sqlite3_db_config() with invalid
  1137   1164   ** opcodes and verify that they return errors.
  1138   1165   */
................................................................................
  1469   1496        { "sqlite3_config_scratch",     test_config_scratch           ,0 },
  1470   1497        { "sqlite3_config_pagecache",   test_config_pagecache         ,0 },
  1471   1498        { "sqlite3_config_alt_pcache",  test_alt_pcache               ,0 },
  1472   1499        { "sqlite3_status",             test_status                   ,0 },
  1473   1500        { "sqlite3_db_status",          test_db_status                ,0 },
  1474   1501        { "install_malloc_faultsim",    test_install_malloc_faultsim  ,0 },
  1475   1502        { "sqlite3_config_heap",        test_config_heap              ,0 },
         1503  +     { "sqlite3_config_heap_size",   test_config_heap_size         ,0 },
  1476   1504        { "sqlite3_config_memstatus",   test_config_memstatus         ,0 },
  1477   1505        { "sqlite3_config_lookaside",   test_config_lookaside         ,0 },
  1478   1506        { "sqlite3_config_error",       test_config_error             ,0 },
  1479   1507        { "sqlite3_config_uri",         test_config_uri               ,0 },
  1480   1508        { "sqlite3_config_cis",         test_config_cis               ,0 },
  1481   1509        { "sqlite3_db_config_lookaside",test_db_config_lookaside      ,0 },
  1482   1510        { "sqlite3_dump_memsys3",       test_dump_memsys3             ,3 },

Changes to src/trigger.c.

   920    920       pPrg->aColmask[0] = pSubParse->oldmask;
   921    921       pPrg->aColmask[1] = pSubParse->newmask;
   922    922       sqlite3VdbeDelete(v);
   923    923     }
   924    924   
   925    925     assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
   926    926     assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
          927  +  sqlite3ParserReset(pSubParse);
   927    928     sqlite3StackFree(db, pSubParse);
   928    929   
   929    930     return pPrg;
   930    931   }
   931    932       
   932    933   /*
   933    934   ** Return a pointer to a TriggerPrg object containing the sub-program for

Changes to src/update.c.

    97     97     Table *pTab;           /* The table to be updated */
    98     98     int addrTop = 0;       /* VDBE instruction address of the start of the loop */
    99     99     WhereInfo *pWInfo;     /* Information about the WHERE clause */
   100    100     Vdbe *v;               /* The virtual database engine */
   101    101     Index *pIdx;           /* For looping over indices */
   102    102     Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
   103    103     int nIdx;              /* Number of indices that need updating */
          104  +  int iBaseCur;          /* Base cursor number */
   104    105     int iDataCur;          /* Cursor for the canonical data btree */
   105    106     int iIdxCur;           /* Cursor for the first index */
   106    107     sqlite3 *db;           /* The database structure */
   107    108     int *aRegIdx = 0;      /* One register assigned to each index to be updated */
   108    109     int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
   109    110                            ** an expression for the i-th column of the table.
   110    111                            ** aXRef[i]==-1 if the i-th column is not changed. */
          112  +  u8 *aToOpen;           /* 1 for tables and indices to be opened */
   111    113     u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
   112    114     u8 chngRowid;          /* Rowid changed in a normal table */
   113    115     u8 chngKey;            /* Either chngPk or chngRowid */
   114    116     Expr *pRowidExpr = 0;  /* Expression defining the new record number */
   115         -  int openAll = 0;       /* True if all indices need to be opened */
   116    117     AuthContext sContext;  /* The authorization context */
   117    118     NameContext sNC;       /* The name-context to resolve expressions in */
   118    119     int iDb;               /* Database containing the table being updated */
   119    120     int okOnePass;         /* True for one-pass algorithm without the FIFO */
   120    121     int hasFK;             /* True if foreign key processing is required */
   121    122     int labelBreak;        /* Jump here to break out of UPDATE loop */
   122    123     int labelContinue;     /* Jump here to continue next step of UPDATE loop */
................................................................................
   172    173   
   173    174     if( sqlite3ViewGetColumnNames(pParse, pTab) ){
   174    175       goto update_cleanup;
   175    176     }
   176    177     if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
   177    178       goto update_cleanup;
   178    179     }
   179         -  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol );
   180         -  if( aXRef==0 ) goto update_cleanup;
   181         -  for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
   182    180   
   183    181     /* Allocate a cursors for the main database table and for all indices.
   184    182     ** The index cursors might not be used, but if they are used they
   185    183     ** need to occur right after the database cursor.  So go ahead and
   186    184     ** allocate enough space, just in case.
   187    185     */
   188         -  pTabList->a[0].iCursor = iDataCur = pParse->nTab++;
          186  +  pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
   189    187     iIdxCur = iDataCur+1;
   190    188     pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
   191    189     for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
   192    190       if( pIdx->autoIndex==2 && pPk!=0 ){
   193    191         iDataCur = pParse->nTab;
   194    192         pTabList->a[0].iCursor = iDataCur;
   195    193       }
   196    194       pParse->nTab++;
   197    195     }
          196  +
          197  +  /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
          198  +  ** Initialize aXRef[] and aToOpen[] to their default values.
          199  +  */
          200  +  aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
          201  +  if( aXRef==0 ) goto update_cleanup;
          202  +  aRegIdx = aXRef+pTab->nCol;
          203  +  aToOpen = (u8*)(aRegIdx+nIdx);
          204  +  memset(aToOpen, 1, nIdx+1);
          205  +  aToOpen[nIdx+1] = 0;
          206  +  for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
   198    207   
   199    208     /* Initialize the name-context */
   200    209     memset(&sNC, 0, sizeof(sNC));
   201    210     sNC.pParse = pParse;
   202    211     sNC.pSrcList = pTabList;
   203    212   
   204    213     /* Resolve the column names in all the expressions of the
................................................................................
   249    258       }
   250    259   #endif
   251    260     }
   252    261     assert( (chngRowid & chngPk)==0 );
   253    262     assert( chngRowid==0 || chngRowid==1 );
   254    263     assert( chngPk==0 || chngPk==1 );
   255    264     chngKey = chngRowid + chngPk;
          265  +
          266  +  /* The SET expressions are not actually used inside the WHERE loop.
          267  +  ** So reset the colUsed mask
          268  +  */
          269  +  pTabList->a[0].colUsed = 0;
   256    270   
   257    271     hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
   258    272   
   259         -  /* Allocate memory for the array aRegIdx[].  There is one entry in the
   260         -  ** array for each index associated with table being updated.  Fill in
   261         -  ** the value with a register number for indices that are to be used
   262         -  ** and with zero for unused indices.
          273  +  /* There is one entry in the aRegIdx[] array for each index on the table
          274  +  ** being updated.  Fill in aRegIdx[] with a register number that will hold
          275  +  ** the key for accessing each index.  
   263    276     */
   264         -  if( nIdx>0 ){
   265         -    aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
   266         -    if( aRegIdx==0 ) goto update_cleanup;
   267         -  }
   268    277     for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
   269    278       int reg;
   270    279       if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
   271    280         reg = ++pParse->nMem;
   272    281       }else{
   273    282         reg = 0;
   274    283         for(i=0; i<pIdx->nKeyCol; i++){
   275    284           if( aXRef[pIdx->aiColumn[i]]>=0 ){
   276    285             reg = ++pParse->nMem;
   277    286             break;
   278    287           }
   279    288         }
   280    289       }
          290  +    if( reg==0 ) aToOpen[j+1] = 0;
   281    291       aRegIdx[j] = reg;
   282    292     }
   283    293   
   284    294     /* Begin generating code. */
   285    295     v = sqlite3GetVdbe(pParse);
   286    296     if( v==0 ) goto update_cleanup;
   287    297     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
................................................................................
   397    407     if( !isView ){
   398    408       /* 
   399    409       ** Open every index that needs updating.  Note that if any
   400    410       ** index could potentially invoke a REPLACE conflict resolution 
   401    411       ** action, then we need to open all indices because we might need
   402    412       ** to be deleting some records.
   403    413       */
   404         -    if( !okOnePass && HasRowid(pTab) ){
   405         -      sqlite3OpenTable(pParse, iDataCur, iDb, pTab, OP_OpenWrite); 
   406         -    }
   407         -    sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
   408    414       if( onError==OE_Replace ){
   409         -      openAll = 1;
          415  +      memset(aToOpen, 1, nIdx+1);
   410    416       }else{
   411         -      openAll = 0;
   412    417         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   413    418           if( pIdx->onError==OE_Replace ){
   414         -          openAll = 1;
          419  +          memset(aToOpen, 1, nIdx+1);
   415    420             break;
   416    421           }
   417    422         }
   418    423       }
   419         -    for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
   420         -      int iThisCur = iIdxCur+i;
   421         -      assert( aRegIdx );
   422         -      if( (openAll || aRegIdx[i]>0)
   423         -       && iThisCur!=aiCurOnePass[1]
   424         -      ){
   425         -        assert( iThisCur!=aiCurOnePass[0] );
   426         -        sqlite3VdbeAddOp3(v, OP_OpenWrite, iThisCur, pIdx->tnum, iDb);
   427         -        sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
   428         -        assert( pParse->nTab>iThisCur );
   429         -        VdbeComment((v, "%s", pIdx->zName));
   430         -        if( okOnePass && pPk && iThisCur==iDataCur ){
   431         -          sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak,
   432         -                               regKey, nKey);
   433         -        }
   434         -      }
          424  +    if( okOnePass ){
          425  +      if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
          426  +      if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
   435    427       }
          428  +    sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
          429  +                               0, 0);
   436    430     }
   437    431   
   438    432     /* Top of the update loop */
   439    433     if( okOnePass ){
          434  +    if( aToOpen[iDataCur-iBaseCur] ){
          435  +      assert( pPk!=0 );
          436  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
          437  +    }
   440    438       labelContinue = labelBreak;
   441    439       sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
   442    440     }else if( pPk ){
   443    441       labelContinue = sqlite3VdbeMakeLabel(v);
   444    442       sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak);
   445    443       addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
   446    444       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
................................................................................
   638    636       sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
   639    637     }
   640    638     sqlite3VdbeResolveLabel(v, labelBreak);
   641    639   
   642    640     /* Close all tables */
   643    641     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
   644    642       assert( aRegIdx );
   645         -    if( openAll || aRegIdx[i]>0 ){
          643  +    if( aToOpen[i+1] ){
   646    644         sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
   647    645       }
   648    646     }
   649    647     if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
   650    648   
   651    649     /* Update the sqlite_sequence table by storing the content of the
   652    650     ** maximum rowid counter values recorded while inserting into
................................................................................
   665    663       sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
   666    664       sqlite3VdbeSetNumCols(v, 1);
   667    665       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
   668    666     }
   669    667   
   670    668   update_cleanup:
   671    669     sqlite3AuthContextPop(&sContext);
   672         -  sqlite3DbFree(db, aRegIdx);
   673         -  sqlite3DbFree(db, aXRef);
          670  +  sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
   674    671     sqlite3SrcListDelete(db, pTabList);
   675    672     sqlite3ExprListDelete(db, pChanges);
   676    673     sqlite3ExprDelete(db, pWhere);
   677    674     return;
   678    675   }
   679    676   /* Make sure "isView" and other macros defined above are undefined. Otherwise
   680    677   ** thely may interfere with compilation of other functions in this file

Changes to src/util.c.

   508    508     }
   509    509     zStart = zNum;
   510    510     while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
   511    511     for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
   512    512       u = u*10 + c - '0';
   513    513     }
   514    514     if( u>LARGEST_INT64 ){
   515         -    *pNum = SMALLEST_INT64;
          515  +    *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
   516    516     }else if( neg ){
   517    517       *pNum = -(i64)u;
   518    518     }else{
   519    519       *pNum = (i64)u;
   520    520     }
   521    521     testcase( i==18 );
   522    522     testcase( i==19 );
................................................................................
   539    539       }else if( c>0 ){
   540    540         /* zNum is greater than 9223372036854775808 so it overflows */
   541    541         return 1;
   542    542       }else{
   543    543         /* zNum is exactly 9223372036854775808.  Fits if negative.  The
   544    544         ** special case 2 overflow if positive */
   545    545         assert( u-1==LARGEST_INT64 );
   546         -      assert( (*pNum)==SMALLEST_INT64 );
   547    546         return neg ? 0 : 2;
   548    547       }
   549    548     }
   550    549   }
   551    550   
   552    551   /*
   553    552   ** If zNum represents an integer that will fit in 32-bits, then set

Changes to src/vdbe.c.

   218    218     ** cursor 1 is managed by memory cell (p->nMem-1), etc.
   219    219     */
   220    220     Mem *pMem = &p->aMem[p->nMem-iCur];
   221    221   
   222    222     int nByte;
   223    223     VdbeCursor *pCx = 0;
   224    224     nByte = 
   225         -      ROUND8(sizeof(VdbeCursor)) + 
   226         -      (isBtreeCursor?sqlite3BtreeCursorSize():0) + 
   227         -      2*nField*sizeof(u32);
          225  +      ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField + 
          226  +      (isBtreeCursor?sqlite3BtreeCursorSize():0);
   228    227   
   229    228     assert( iCur<p->nCursor );
   230    229     if( p->apCsr[iCur] ){
   231    230       sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   232    231       p->apCsr[iCur] = 0;
   233    232     }
   234    233     if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
   235    234       p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
   236    235       memset(pCx, 0, sizeof(VdbeCursor));
   237    236       pCx->iDb = iDb;
   238    237       pCx->nField = nField;
   239         -    if( nField ){
   240         -      pCx->aType = (u32 *)&pMem->z[ROUND8(sizeof(VdbeCursor))];
   241         -    }
   242    238       if( isBtreeCursor ){
   243    239         pCx->pCursor = (BtCursor*)
   244         -          &pMem->z[ROUND8(sizeof(VdbeCursor))+2*nField*sizeof(u32)];
          240  +          &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
   245    241         sqlite3BtreeCursorZero(pCx->pCursor);
   246    242       }
   247    243     }
   248    244     return pCx;
   249    245   }
   250    246   
   251    247   /*
................................................................................
  1091   1087   */
  1092   1088   case OP_Move: {
  1093   1089     char *zMalloc;   /* Holding variable for allocated memory */
  1094   1090     int n;           /* Number of registers left to copy */
  1095   1091     int p1;          /* Register to copy from */
  1096   1092     int p2;          /* Register to copy to */
  1097   1093   
  1098         -  n = pOp->p3 + 1;
         1094  +  n = pOp->p3;
  1099   1095     p1 = pOp->p1;
  1100   1096     p2 = pOp->p2;
  1101         -  assert( n>0 && p1>0 && p2>0 );
         1097  +  assert( n>=0 && p1>0 && p2>0 );
  1102   1098     assert( p1+n<=p2 || p2+n<=p1 );
  1103   1099   
  1104   1100     pIn1 = &aMem[p1];
  1105   1101     pOut = &aMem[p2];
  1106         -  while( n-- ){
         1102  +  do{
  1107   1103       assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
  1108   1104       assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
  1109   1105       assert( memIsValid(pIn1) );
  1110   1106       memAboutToChange(p, pOut);
  1111   1107       zMalloc = pOut->zMalloc;
  1112   1108       pOut->zMalloc = 0;
  1113   1109       sqlite3VdbeMemMove(pOut, pIn1);
................................................................................
  1116   1112         pOut->pScopyFrom += p1 - pOp->p2;
  1117   1113       }
  1118   1114   #endif
  1119   1115       pIn1->zMalloc = zMalloc;
  1120   1116       REGISTER_TRACE(p2++, pOut);
  1121   1117       pIn1++;
  1122   1118       pOut++;
  1123         -  }
         1119  +  }while( n-- );
  1124   1120     break;
  1125   1121   }
  1126   1122   
  1127   1123   /* Opcode: Copy P1 P2 P3 * *
  1128   1124   ** Synopsis: r[P2@P3]=r[P1@P3]
  1129   1125   **
  1130   1126   ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
................................................................................
  1328   1324   ** Synopsis:  r[P3]=r[P2]-r[P1]
  1329   1325   **
  1330   1326   ** Subtract the value in register P1 from the value in register P2
  1331   1327   ** and store the result in register P3.
  1332   1328   ** If either input is NULL, the result is NULL.
  1333   1329   */
  1334   1330   /* Opcode: Divide P1 P2 P3 * *
  1335         -** Synopsis:  r[P3]=r[P1]/r[P2]
         1331  +** Synopsis:  r[P3]=r[P2]/r[P1]
  1336   1332   **
  1337   1333   ** Divide the value in register P1 by the value in register P2
  1338   1334   ** and store the result in register P3 (P3=P2/P1). If the value in 
  1339   1335   ** register P1 is zero, then the result is NULL. If either input is 
  1340   1336   ** NULL, the result is NULL.
  1341   1337   */
  1342   1338   /* Opcode: Remainder P1 P2 P3 * *
  1343         -** Synopsis:  r[P3]=r[P1]%r[P2]
         1339  +** Synopsis:  r[P3]=r[P2]%r[P1]
  1344   1340   **
  1345         -** Compute the remainder after integer division of the value in
  1346         -** register P1 by the value in register P2 and store the result in P3. 
  1347         -** If the value in register P2 is zero the result is NULL.
         1341  +** Compute the remainder after integer register P2 is divided by 
         1342  +** register P1 and store the result in register P3. 
         1343  +** If the value in register P1 is zero the result is NULL.
  1348   1344   ** If either operand is NULL, the result is NULL.
  1349   1345   */
  1350   1346   case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
  1351   1347   case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
  1352   1348   case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
  1353   1349   case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
  1354   1350   case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
................................................................................
  1497   1493       Deephemeralize(pArg);
  1498   1494       sqlite3VdbeMemStoreType(pArg);
  1499   1495       REGISTER_TRACE(pOp->p2+i, pArg);
  1500   1496     }
  1501   1497   
  1502   1498     assert( pOp->p4type==P4_FUNCDEF );
  1503   1499     ctx.pFunc = pOp->p4.pFunc;
  1504         -  ctx.s.flags = MEM_Null;
  1505         -  ctx.s.db = db;
  1506         -  ctx.s.xDel = 0;
  1507         -  ctx.s.zMalloc = 0;
  1508   1500     ctx.iOp = pc;
  1509   1501     ctx.pVdbe = p;
  1510   1502   
  1511   1503     /* The output cell may already have a buffer allocated. Move
  1512   1504     ** the pointer to ctx.s so in case the user-function can use
  1513   1505     ** the already allocated buffer instead of allocating a new one.
  1514   1506     */
  1515         -  sqlite3VdbeMemMove(&ctx.s, pOut);
         1507  +  memcpy(&ctx.s, pOut, sizeof(Mem));
         1508  +  pOut->flags = MEM_Null;
         1509  +  pOut->xDel = 0;
         1510  +  pOut->zMalloc = 0;
  1516   1511     MemSetTypeFlag(&ctx.s, MEM_Null);
  1517   1512   
  1518   1513     ctx.fErrorOrAux = 0;
  1519   1514     if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  1520   1515       assert( pOp>aOp );
  1521   1516       assert( pOp[-1].p4type==P4_COLLSEQ );
  1522   1517       assert( pOp[-1].opcode==OP_CollSeq );
................................................................................
  1543   1538         rc = ctx.isError;
  1544   1539       }
  1545   1540       sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
  1546   1541     }
  1547   1542   
  1548   1543     /* Copy the result of the function into register P3 */
  1549   1544     sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  1550         -  sqlite3VdbeMemMove(pOut, &ctx.s);
         1545  +  assert( pOut->flags==MEM_Null );
         1546  +  memcpy(pOut, &ctx.s, sizeof(Mem));
  1551   1547     if( sqlite3VdbeMemTooBig(pOut) ){
  1552   1548       goto too_big;
  1553   1549     }
  1554   1550   
  1555   1551   #if 0
  1556   1552     /* The app-defined function has done something that as caused this
  1557   1553     ** statement to expire.  (Perhaps the function called sqlite3_exec()
................................................................................
  1668   1664   ** Force the value in register P1 to be an integer.  If the value
  1669   1665   ** in P1 is not an integer and cannot be converted into an integer
  1670   1666   ** without data loss, then jump immediately to P2, or if P2==0
  1671   1667   ** raise an SQLITE_MISMATCH exception.
  1672   1668   */
  1673   1669   case OP_MustBeInt: {            /* jump, in1 */
  1674   1670     pIn1 = &aMem[pOp->p1];
  1675         -  applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
  1676   1671     if( (pIn1->flags & MEM_Int)==0 ){
  1677         -    if( pOp->p2==0 ){
  1678         -      rc = SQLITE_MISMATCH;
  1679         -      goto abort_due_to_error;
  1680         -    }else{
  1681         -      pc = pOp->p2 - 1;
         1672  +    applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
         1673  +    if( (pIn1->flags & MEM_Int)==0 ){
         1674  +      if( pOp->p2==0 ){
         1675  +        rc = SQLITE_MISMATCH;
         1676  +        goto abort_due_to_error;
         1677  +      }else{
         1678  +        pc = pOp->p2 - 1;
         1679  +        break;
         1680  +      }
  1682   1681       }
  1683         -  }else{
  1684         -    MemSetTypeFlag(pIn1, MEM_Int);
  1685   1682     }
         1683  +  MemSetTypeFlag(pIn1, MEM_Int);
  1686   1684     break;
  1687   1685   }
  1688   1686   
  1689   1687   #ifndef SQLITE_OMIT_FLOATING_POINT
  1690   1688   /* Opcode: RealAffinity P1 * * * *
  1691   1689   **
  1692   1690   ** If register P1 holds an integer convert it to a real value.
................................................................................
  1803   1801       sqlite3VdbeMemRealify(pIn1);
  1804   1802     }
  1805   1803     break;
  1806   1804   }
  1807   1805   #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
  1808   1806   
  1809   1807   /* Opcode: Lt P1 P2 P3 P4 P5
  1810         -** Synopsis: if r[P1]<r[P3] goto P3
         1808  +** Synopsis: if r[P1]<r[P3] goto P2
  1811   1809   **
  1812   1810   ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
  1813   1811   ** jump to address P2.  
  1814   1812   **
  1815   1813   ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
  1816   1814   ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL 
  1817   1815   ** bit is clear then fall through if either operand is NULL.
................................................................................
  2257   2255   **
  2258   2256   ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
  2259   2257   ** the result is guaranteed to only be used as the argument of a length()
  2260   2258   ** or typeof() function, respectively.  The loading of large blobs can be
  2261   2259   ** skipped for length() and all content loading can be skipped for typeof().
  2262   2260   */
  2263   2261   case OP_Column: {
  2264         -  u32 payloadSize;   /* Number of bytes in the record */
  2265   2262     i64 payloadSize64; /* Number of bytes in the record */
  2266         -  int p1;            /* P1 value of the opcode */
  2267   2263     int p2;            /* column number to retrieve */
  2268   2264     VdbeCursor *pC;    /* The VDBE cursor */
  2269         -  char *zRec;        /* Pointer to complete record-data */
  2270   2265     BtCursor *pCrsr;   /* The BTree cursor */
  2271   2266     u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
  2272   2267     u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
  2273         -  int nField;        /* number of fields in the record */
  2274   2268     int len;           /* The length of the serialized data for the column */
  2275   2269     int i;             /* Loop counter */
  2276         -  char *zData;       /* Part of the record being decoded */
  2277   2270     Mem *pDest;        /* Where to write the extracted value */
  2278   2271     Mem sMem;          /* For storing the record being decoded */
  2279         -  u8 *zIdx;          /* Index into header */
  2280         -  u8 *zEndHdr;       /* Pointer to first byte after the header */
         2272  +  const u8 *zData;   /* Part of the record being decoded */
         2273  +  const u8 *zHdr;    /* Next unparsed byte of the header */
         2274  +  const u8 *zEndHdr; /* Pointer to first byte after the header */
  2281   2275     u32 offset;        /* Offset into the data */
  2282   2276     u32 szField;       /* Number of bytes in the content of a field */
  2283         -  int szHdr;         /* Size of the header size field at start of record */
  2284         -  int avail;         /* Number of bytes of available data */
         2277  +  u32 avail;         /* Number of bytes of available data */
  2285   2278     u32 t;             /* A type code from the record header */
  2286   2279     Mem *pReg;         /* PseudoTable input register */
  2287   2280   
  2288         -
  2289         -  p1 = pOp->p1;
  2290   2281     p2 = pOp->p2;
  2291         -  pC = 0;
  2292         -  memset(&sMem, 0, sizeof(sMem));
  2293         -  assert( p1<p->nCursor );
  2294   2282     assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
  2295   2283     pDest = &aMem[pOp->p3];
  2296   2284     memAboutToChange(p, pDest);
  2297         -  zRec = 0;
  2298         -
  2299         -  /* This block sets the variable payloadSize to be the total number of
  2300         -  ** bytes in the record.
  2301         -  **
  2302         -  ** zRec is set to be the complete text of the record if it is available.
  2303         -  ** The complete record text is always available for pseudo-tables
  2304         -  ** If the record is stored in a cursor, the complete record text
  2305         -  ** might be available in the  pC->aRow cache.  Or it might not be.
  2306         -  ** If the data is unavailable,  zRec is set to NULL.
  2307         -  **
  2308         -  ** We also compute the number of columns in the record.  For cursors,
  2309         -  ** the number of columns is stored in the VdbeCursor.nField element.
  2310         -  */
  2311         -  pC = p->apCsr[p1];
         2285  +  assert( pOp->p1>=0 && pOp->p1<p->nCursor );
         2286  +  pC = p->apCsr[pOp->p1];
  2312   2287     assert( pC!=0 );
         2288  +  assert( p2<pC->nField );
         2289  +  aType = pC->aType;
         2290  +  aOffset = aType + pC->nField;
  2313   2291   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2314         -  assert( pC->pVtabCursor==0 );
         2292  +  assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
  2315   2293   #endif
  2316   2294     pCrsr = pC->pCursor;
  2317         -  if( pCrsr!=0 ){
  2318         -    /* The record is stored in a B-Tree */
  2319         -    rc = sqlite3VdbeCursorMoveto(pC);
  2320         -    if( rc ) goto abort_due_to_error;
         2295  +  assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
         2296  +  assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
         2297  +
         2298  +  /* If the cursor cache is stale, bring it up-to-date */
         2299  +  rc = sqlite3VdbeCursorMoveto(pC);
         2300  +  if( rc ) goto abort_due_to_error;
         2301  +  if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
  2321   2302       if( pC->nullRow ){
  2322         -      payloadSize = 0;
  2323         -    }else if( pC->cacheStatus==p->cacheCtr ){
  2324         -      payloadSize = pC->payloadSize;
  2325         -      zRec = (char*)pC->aRow;
  2326         -    }else if( pC->isIndex ){
  2327         -      assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2328         -      VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
  2329         -      assert( rc==SQLITE_OK );   /* True because of CursorMoveto() call above */
  2330         -      /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
  2331         -      ** payload size, so it is impossible for payloadSize64 to be
  2332         -      ** larger than 32 bits. */
  2333         -      assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
  2334         -      payloadSize = (u32)payloadSize64;
  2335         -    }else{
  2336         -      assert( sqlite3BtreeCursorIsValid(pCrsr) );
  2337         -      VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &payloadSize);
  2338         -      assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
  2339         -    }
  2340         -  }else{
  2341         -    assert( pC->pseudoTableReg>0 );
  2342         -    pReg = &aMem[pC->pseudoTableReg];
  2343         -    if( pC->multiPseudo ){
  2344         -      sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
  2345         -      Deephemeralize(pDest);
  2346         -      goto op_column_out;
  2347         -    }
  2348         -    assert( pReg->flags & MEM_Blob );
  2349         -    assert( memIsValid(pReg) );
  2350         -    payloadSize = pReg->n;
  2351         -    zRec = pReg->z;
  2352         -    pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
  2353         -    assert( payloadSize==0 || zRec!=0 );
  2354         -  }
  2355         -
  2356         -  /* If payloadSize is 0, then just store a NULL.  This can happen because of
  2357         -  ** nullRow or because of a corrupt database. */
  2358         -  if( payloadSize==0 ){
  2359         -    MemSetTypeFlag(pDest, MEM_Null);
  2360         -    goto op_column_out;
  2361         -  }
  2362         -  assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
  2363         -  if( payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2364         -    goto too_big;
  2365         -  }
  2366         -
  2367         -  nField = pC->nField;
  2368         -  assert( p2<nField );
  2369         -
  2370         -  /* Read and parse the table header.  Store the results of the parse
  2371         -  ** into the record header cache fields of the cursor.
  2372         -  */
  2373         -  aType = pC->aType;
  2374         -  if( pC->cacheStatus==p->cacheCtr ){
  2375         -    aOffset = pC->aOffset;
  2376         -  }else{
  2377         -    assert(aType);
  2378         -    avail = 0;
  2379         -    pC->aOffset = aOffset = &aType[nField];
  2380         -    pC->payloadSize = payloadSize;
         2303  +      if( pCrsr==0 ){
         2304  +        assert( pC->pseudoTableReg>0 );
         2305  +        pReg = &aMem[pC->pseudoTableReg];
         2306  +        if( pC->multiPseudo ){
         2307  +          sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
         2308  +          Deephemeralize(pDest);
         2309  +          goto op_column_out;
         2310  +        }
         2311  +        assert( pReg->flags & MEM_Blob );
         2312  +        assert( memIsValid(pReg) );
         2313  +        pC->payloadSize = pC->szRow = avail = pReg->n;
         2314  +        pC->aRow = (u8*)pReg->z;
         2315  +      }else{
         2316  +        MemSetTypeFlag(pDest, MEM_Null);
         2317  +        goto op_column_out;
         2318  +      }
         2319  +    }else{
         2320  +      assert( pCrsr );
         2321  +      if( pC->isTable==0 ){
         2322  +        assert( sqlite3BtreeCursorIsValid(pCrsr) );
         2323  +        VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
         2324  +        assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
         2325  +        /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
         2326  +        ** payload size, so it is impossible for payloadSize64 to be
         2327  +        ** larger than 32 bits. */
         2328  +        assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
         2329  +        pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
         2330  +        pC->payloadSize = (u32)payloadSize64;
         2331  +      }else{
         2332  +        assert( sqlite3BtreeCursorIsValid(pCrsr) );
         2333  +        VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
         2334  +        assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
         2335  +        pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
         2336  +      }
         2337  +      assert( avail<=65536 );  /* Maximum page size is 64KiB */
         2338  +      if( pC->payloadSize <= (u32)avail ){
         2339  +        pC->szRow = pC->payloadSize;
         2340  +      }else{
         2341  +        pC->szRow = avail;
         2342  +      }
         2343  +      if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
         2344  +        goto too_big;
         2345  +      }
         2346  +    }
  2381   2347       pC->cacheStatus = p->cacheCtr;
  2382         -
  2383         -    /* Figure out how many bytes are in the header */
  2384         -    if( zRec ){
  2385         -      zData = zRec;
  2386         -    }else{
  2387         -      if( pC->isIndex ){
  2388         -        zData = (char*)sqlite3BtreeKeyFetch(pCrsr, &avail);
  2389         -      }else{
  2390         -        zData = (char*)sqlite3BtreeDataFetch(pCrsr, &avail);
  2391         -      }
  2392         -      /* If KeyFetch()/DataFetch() managed to get the entire payload,
  2393         -      ** save the payload in the pC->aRow cache.  That will save us from
  2394         -      ** having to make additional calls to fetch the content portion of
  2395         -      ** the record.
  2396         -      */
  2397         -      assert( avail>=0 );
  2398         -      if( payloadSize <= (u32)avail ){
  2399         -        zRec = zData;
  2400         -        pC->aRow = (u8*)zData;
  2401         -      }else{
  2402         -        pC->aRow = 0;
  2403         -      }
         2348  +    pC->iHdrOffset = getVarint32(pC->aRow, offset);
         2349  +    pC->nHdrParsed = 0;
         2350  +    aOffset[0] = offset;
         2351  +    if( avail<offset ){
         2352  +      /* pC->aRow does not have to hold the entire row, but it does at least
         2353  +      ** need to cover the header of the record.  If pC->aRow does not contain
         2354  +      ** the complete header, then set it to zero, forcing the header to be
         2355  +      ** dynamically allocated. */
         2356  +      pC->aRow = 0;
         2357  +      pC->szRow = 0;
  2404   2358       }
  2405         -    /* The following assert is true in all cases except when
  2406         -    ** the database file has been corrupted externally.
  2407         -    **    assert( zRec!=0 || avail>=payloadSize || avail>=9 ); */
  2408         -    szHdr = getVarint32((u8*)zData, offset);
  2409   2359   
  2410   2360       /* Make sure a corrupt database has not given us an oversize header.
  2411   2361       ** Do this now to avoid an oversize memory allocation.
  2412   2362       **
  2413   2363       ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
  2414   2364       ** types use so much data space that there can only be 4096 and 32 of
  2415   2365       ** them, respectively.  So the maximum header length results from a
  2416   2366       ** 3-byte type for each of the maximum of 32768 columns plus three
  2417   2367       ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
  2418   2368       */
  2419         -    if( offset > 98307 ){
         2369  +    if( offset > 98307 || offset > pC->payloadSize ){
  2420   2370         rc = SQLITE_CORRUPT_BKPT;
  2421         -      goto op_column_out;
         2371  +      goto op_column_error;
  2422   2372       }
         2373  +  }
  2423   2374   
  2424         -    /* Compute in len the number of bytes of data we need to read in order
  2425         -    ** to get nField type values.  offset is an upper bound on this.  But
  2426         -    ** nField might be significantly less than the true number of columns
  2427         -    ** in the table, and in that case, 5*nField+3 might be smaller than offset.
  2428         -    ** We want to minimize len in order to limit the size of the memory
  2429         -    ** allocation, especially if a corrupt database file has caused offset
  2430         -    ** to be oversized. Offset is limited to 98307 above.  But 98307 might
  2431         -    ** still exceed Robson memory allocation limits on some configurations.
  2432         -    ** On systems that cannot tolerate large memory allocations, nField*5+3
  2433         -    ** will likely be much smaller since nField will likely be less than
  2434         -    ** 20 or so.  This insures that Robson memory allocation limits are
  2435         -    ** not exceeded even for corrupt database files.
         2375  +  /* Make sure at least the first p2+1 entries of the header have been
         2376  +  ** parsed and valid information is in aOffset[] and aType[].
         2377  +  */
         2378  +  if( pC->nHdrParsed<=p2 ){
         2379  +    /* If there is more header available for parsing in the record, try
         2380  +    ** to extract additional fields up through the p2+1-th field 
  2436   2381       */
  2437         -    len = nField*5 + 3;
  2438         -    if( len > (int)offset ) len = (int)offset;
  2439         -
  2440         -    /* The KeyFetch() or DataFetch() above are fast and will get the entire
  2441         -    ** record header in most cases.  But they will fail to get the complete
  2442         -    ** record header if the record header does not fit on a single page
  2443         -    ** in the B-Tree.  When that happens, use sqlite3VdbeMemFromBtree() to
  2444         -    ** acquire the complete header text.
  2445         -    */
  2446         -    if( !zRec && avail<len ){
  2447         -      sMem.flags = 0;
  2448         -      sMem.db = 0;
  2449         -      rc = sqlite3VdbeMemFromBtree(pCrsr, 0, len, pC->isIndex, &sMem);
  2450         -      if( rc!=SQLITE_OK ){
  2451         -        goto op_column_out;
  2452         -      }
  2453         -      zData = sMem.z;
  2454         -    }
  2455         -    zEndHdr = (u8 *)&zData[len];
  2456         -    zIdx = (u8 *)&zData[szHdr];
  2457         -
  2458         -    /* Scan the header and use it to fill in the aType[] and aOffset[]
  2459         -    ** arrays.  aType[i] will contain the type integer for the i-th
  2460         -    ** column and aOffset[i] will contain the offset from the beginning
  2461         -    ** of the record to the start of the data for the i-th column
  2462         -    */
  2463         -    for(i=0; i<nField; i++){
  2464         -      if( zIdx<zEndHdr ){
  2465         -        aOffset[i] = offset;
  2466         -        if( zIdx[0]<0x80 ){
  2467         -          t = zIdx[0];
  2468         -          zIdx++;
         2382  +    if( pC->iHdrOffset<aOffset[0] ){
         2383  +      /* Make sure zData points to enough of the record to cover the header. */
         2384  +      if( pC->aRow==0 ){
         2385  +        memset(&sMem, 0, sizeof(sMem));
         2386  +        rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0], 
         2387  +                                     !pC->isTable, &sMem);
         2388  +        if( rc!=SQLITE_OK ){
         2389  +          goto op_column_error;
         2390  +        }
         2391  +        zData = (u8*)sMem.z;
         2392  +      }else{
         2393  +        zData = pC->aRow;
         2394  +      }
         2395  +  
         2396  +      /* Fill in aType[i] and aOffset[i] values through the p2-th field. */
         2397  +      i = pC->nHdrParsed;
         2398  +      offset = aOffset[i];
         2399  +      zHdr = zData + pC->iHdrOffset;
         2400  +      zEndHdr = zData + aOffset[0];
         2401  +      assert( i<=p2 && zHdr<zEndHdr );
         2402  +      do{
         2403  +        if( zHdr[0]<0x80 ){
         2404  +          t = zHdr[0];
         2405  +          zHdr++;
  2469   2406           }else{
  2470         -          zIdx += sqlite3GetVarint32(zIdx, &t);
         2407  +          zHdr += sqlite3GetVarint32(zHdr, &t);
  2471   2408           }
  2472   2409           aType[i] = t;
  2473   2410           szField = sqlite3VdbeSerialTypeLen(t);
  2474   2411           offset += szField;
  2475   2412           if( offset<szField ){  /* True if offset overflows */
  2476         -          zIdx = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
         2413  +          zHdr = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
  2477   2414             break;
  2478   2415           }
  2479         -      }else{
  2480         -        /* If i is less that nField, then there are fewer fields in this
  2481         -        ** record than SetNumColumns indicated there are columns in the
  2482         -        ** table. Set the offset for any extra columns not present in
  2483         -        ** the record to 0. This tells code below to store the default value
  2484         -        ** for the column instead of deserializing a value from the record.
  2485         -        */
  2486         -        aOffset[i] = 0;
  2487         -      }
  2488         -    }
  2489         -    sqlite3VdbeMemRelease(&sMem);
  2490         -    sMem.flags = MEM_Null;
  2491         -
  2492         -    /* If we have read more header data than was contained in the header,
  2493         -    ** or if the end of the last field appears to be past the end of the
  2494         -    ** record, or if the end of the last field appears to be before the end
  2495         -    ** of the record (when all fields present), then we must be dealing 
  2496         -    ** with a corrupt database.
         2416  +        i++;
         2417  +        aOffset[i] = offset;
         2418  +      }while( i<=p2 && zHdr<zEndHdr );
         2419  +      pC->nHdrParsed = i;
         2420  +      pC->iHdrOffset = (u32)(zHdr - zData);
         2421  +      if( pC->aRow==0 ){
         2422  +        sqlite3VdbeMemRelease(&sMem);
         2423  +        sMem.flags = MEM_Null;
         2424  +      }
         2425  +  
         2426  +      /* If we have read more header data than was contained in the header,
         2427  +      ** or if the end of the last field appears to be past the end of the
         2428  +      ** record, or if the end of the last field appears to be before the end
         2429  +      ** of the record (when all fields present), then we must be dealing 
         2430  +      ** with a corrupt database.
         2431  +      */
         2432  +      if( (zHdr > zEndHdr)
         2433  +       || (offset > pC->payloadSize)
         2434  +       || (zHdr==zEndHdr && offset!=pC->payloadSize)
         2435  +      ){
         2436  +        rc = SQLITE_CORRUPT_BKPT;
         2437  +        goto op_column_error;
         2438  +      }
         2439  +    }
         2440  +
         2441  +    /* If after trying to extra new entries from the header, nHdrParsed is
         2442  +    ** still not up to p2, that means that the record has fewer than p2
         2443  +    ** columns.  So the result will be either the default value or a NULL.
  2497   2444       */
  2498         -    if( (zIdx > zEndHdr) || (offset > payloadSize)
  2499         -         || (zIdx==zEndHdr && offset!=payloadSize) ){
  2500         -      rc = SQLITE_CORRUPT_BKPT;
         2445  +    if( pC->nHdrParsed<=p2 ){
         2446  +      if( pOp->p4type==P4_MEM ){
         2447  +        sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
         2448  +      }else{
         2449  +        MemSetTypeFlag(pDest, MEM_Null);
         2450  +      }
  2501   2451         goto op_column_out;
  2502   2452       }
  2503   2453     }
  2504   2454   
  2505         -  /* Get the column information. If aOffset[p2] is non-zero, then 
  2506         -  ** deserialize the value from the record. If aOffset[p2] is zero,
  2507         -  ** then there are not enough fields in the record to satisfy the
  2508         -  ** request.  In this case, set the value NULL or to P4 if P4 is
  2509         -  ** a pointer to a Mem object.
  2510         -  */
  2511         -  if( aOffset[p2] ){
  2512         -    assert( rc==SQLITE_OK );
  2513         -    if( zRec ){
  2514         -      /* This is the common case where the whole row fits on a single page */
  2515         -      VdbeMemRelease(pDest);
  2516         -      sqlite3VdbeSerialGet((u8 *)&zRec[aOffset[p2]], aType[p2], pDest);
  2517         -    }else{
  2518         -      /* This branch happens only when the row overflows onto multiple pages */
  2519         -      t = aType[p2];
  2520         -      if( (pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
  2521         -       && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0)
  2522         -      ){
  2523         -        /* Content is irrelevant for the typeof() function and for
  2524         -        ** the length(X) function if X is a blob.  So we might as well use
  2525         -        ** bogus content rather than reading content from disk.  NULL works
  2526         -        ** for text and blob and whatever is in the payloadSize64 variable
  2527         -        ** will work for everything else. */
  2528         -        zData = t<12 ? (char*)&payloadSize64 : 0;
  2529         -      }else{
  2530         -        len = sqlite3VdbeSerialTypeLen(t);
  2531         -        sqlite3VdbeMemMove(&sMem, pDest);
  2532         -        rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len,  pC->isIndex,
  2533         -                                     &sMem);
  2534         -        if( rc!=SQLITE_OK ){
  2535         -          goto op_column_out;
  2536         -        }
  2537         -        zData = sMem.z;
  2538         -      }
  2539         -      sqlite3VdbeSerialGet((u8*)zData, t, pDest);
  2540         -    }
  2541         -    pDest->enc = encoding;
  2542         -  }else{
  2543         -    if( pOp->p4type==P4_MEM ){
  2544         -      sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
  2545         -    }else{
  2546         -      MemSetTypeFlag(pDest, MEM_Null);
  2547         -    }
  2548         -  }
  2549         -
  2550         -  /* If we dynamically allocated space to hold the data (in the
  2551         -  ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
  2552         -  ** dynamically allocated space over to the pDest structure.
  2553         -  ** This prevents a memory copy.
  2554         -  */
  2555         -  if( sMem.zMalloc ){
  2556         -    assert( sMem.z==sMem.zMalloc );
  2557         -    assert( !(pDest->flags & MEM_Dyn) );
  2558         -    assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
  2559         -    pDest->flags &= ~(MEM_Ephem|MEM_Static);
  2560         -    pDest->flags |= MEM_Term;
  2561         -    pDest->z = sMem.z;
  2562         -    pDest->zMalloc = sMem.zMalloc;
  2563         -  }
  2564         -
  2565         -  rc = sqlite3VdbeMemMakeWriteable(pDest);
         2455  +  /* Extract the content for the p2+1-th column.  Control can only
         2456  +  ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
         2457  +  ** all valid.
         2458  +  */
         2459  +  assert( p2<pC->nHdrParsed );
         2460  +  assert( rc==SQLITE_OK );
         2461  +  if( pC->szRow>=aOffset[p2+1] ){
         2462  +    /* This is the common case where the desired content fits on the original
         2463  +    ** page - where the content is not on an overflow page */
         2464  +    VdbeMemRelease(pDest);
         2465  +    sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
         2466  +  }else{
         2467  +    /* This branch happens only when content is on overflow pages */	
         2468  +    t = aType[p2];
         2469  +    if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
         2470  +          && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
         2471  +     || (len = sqlite3VdbeSerialTypeLen(t))==0
         2472  +    ){
         2473  +      /* Content is irrelevant for the typeof() function and for
         2474  +      ** the length(X) function if X is a blob.  So we might as well use
         2475  +      ** bogus content rather than reading content from disk.  NULL works
         2476  +      ** for text and blob and whatever is in the payloadSize64 variable
         2477  +      ** will work for everything else.  Content is also irrelevant if
         2478  +      ** the content length is 0. */
         2479  +      zData = t<=13 ? (u8*)&payloadSize64 : 0;
         2480  +      sMem.zMalloc = 0;
         2481  +    }else{
         2482  +      memset(&sMem, 0, sizeof(sMem));
         2483  +      sqlite3VdbeMemMove(&sMem, pDest);
         2484  +      rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
         2485  +                                   &sMem);
         2486  +      if( rc!=SQLITE_OK ){
         2487  +        goto op_column_error;
         2488  +      }
         2489  +      zData = (u8*)sMem.z;
         2490  +    }
         2491  +    sqlite3VdbeSerialGet(zData, t, pDest);
         2492  +    /* If we dynamically allocated space to hold the data (in the
         2493  +    ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
         2494  +    ** dynamically allocated space over to the pDest structure.
         2495  +    ** This prevents a memory copy. */
         2496  +    if( sMem.zMalloc ){
         2497  +      assert( sMem.z==sMem.zMalloc );
         2498  +      assert( !(pDest->flags & MEM_Dyn) );
         2499  +      assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
         2500  +      pDest->flags &= ~(MEM_Ephem|MEM_Static);
         2501  +      pDest->flags |= MEM_Term;
         2502  +      pDest->z = sMem.z;
         2503  +      pDest->zMalloc = sMem.zMalloc;
         2504  +    }
         2505  +  }
         2506  +  pDest->enc = encoding;
  2566   2507   
  2567   2508   op_column_out:
         2509  +  rc = sqlite3VdbeMemMakeWriteable(pDest);
         2510  +op_column_error:
  2568   2511     UPDATE_MAX_BLOBSIZE(pDest);
  2569   2512     REGISTER_TRACE(pOp->p3, pDest);
  2570   2513     break;
  2571   2514   }
  2572   2515   
  2573   2516   /* Opcode: Affinity P1 P2 * P4 *
  2574   2517   ** Synopsis: affinity(r[P1@P2])
................................................................................
  3316   3259       assert( pKeyInfo->enc==ENC(db) );
  3317   3260       assert( pKeyInfo->db==db );
  3318   3261       nField = pKeyInfo->nField+pKeyInfo->nXField;
  3319   3262     }else if( pOp->p4type==P4_INT32 ){
  3320   3263       nField = pOp->p4.i;
  3321   3264     }
  3322   3265     assert( pOp->p1>=0 );
         3266  +  assert( nField>=0 );
         3267  +  testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
  3323   3268     pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
  3324   3269     if( pCur==0 ) goto no_mem;
  3325   3270     pCur->nullRow = 1;
  3326   3271     pCur->isOrdered = 1;
  3327   3272     rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
  3328   3273     pCur->pKeyInfo = pKeyInfo;
  3329   3274     assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
  3330   3275     sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
  3331   3276   
  3332   3277     /* Since it performs no memory allocation or IO, the only value that
  3333   3278     ** sqlite3BtreeCursor() may return is SQLITE_OK. */
  3334   3279     assert( rc==SQLITE_OK );
  3335   3280   
  3336         -  /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
         3281  +  /* Set the VdbeCursor.isTable variable. Previous versions of
  3337   3282     ** SQLite used to check if the root-page flags were sane at this point
  3338   3283     ** and report database corruption if they were not, but this check has
  3339   3284     ** since moved into the btree layer.  */  
  3340   3285     pCur->isTable = pOp->p4type!=P4_KEYINFO;
  3341         -  pCur->isIndex = !pCur->isTable;
  3342   3286     break;
  3343   3287   }
  3344   3288   
  3345   3289   /* Opcode: OpenEphemeral P1 P2 * P4 P5
  3346   3290   ** Synopsis: nColumn=P2
  3347   3291   **
  3348   3292   ** Open a new cursor P1 to a transient table.
................................................................................
  3376   3320     static const int vfsFlags = 
  3377   3321         SQLITE_OPEN_READWRITE |
  3378   3322         SQLITE_OPEN_CREATE |
  3379   3323         SQLITE_OPEN_EXCLUSIVE |
  3380   3324         SQLITE_OPEN_DELETEONCLOSE |
  3381   3325         SQLITE_OPEN_TRANSIENT_DB;
  3382   3326     assert( pOp->p1>=0 );
         3327  +  assert( pOp->p2>=0 );
  3383   3328     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3384   3329     if( pCx==0 ) goto no_mem;
  3385   3330     pCx->nullRow = 1;
  3386   3331     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
  3387   3332                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  3388   3333     if( rc==SQLITE_OK ){
  3389   3334       rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
................................................................................
  3408   3353         pCx->isTable = 0;
  3409   3354       }else{
  3410   3355         rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
  3411   3356         pCx->isTable = 1;
  3412   3357       }
  3413   3358     }
  3414   3359     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3415         -  pCx->isIndex = !pCx->isTable;
  3416   3360     break;
  3417   3361   }
  3418   3362   
  3419   3363   /* Opcode: SorterOpen P1 * * P4 *
  3420   3364   **
  3421   3365   ** This opcode works like OP_OpenEphemeral except that it opens
  3422   3366   ** a transient index that is specifically designed to sort large
  3423   3367   ** tables using an external merge-sort algorithm.
  3424   3368   */
  3425   3369   case OP_SorterOpen: {
  3426   3370     VdbeCursor *pCx;
  3427   3371   
         3372  +  assert( pOp->p1>=0 );
         3373  +  assert( pOp->p2>=0 );
  3428   3374     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3429   3375     if( pCx==0 ) goto no_mem;
  3430   3376     pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3431   3377     assert( pCx->pKeyInfo->db==db );
  3432   3378     assert( pCx->pKeyInfo->enc==ENC(db) );
  3433         -  pCx->isSorter = 1;
  3434   3379     rc = sqlite3VdbeSorterInit(db, pCx);
  3435   3380     break;
  3436   3381   }
  3437   3382   
  3438   3383   /* Opcode: OpenPseudo P1 P2 P3 * P5
  3439   3384   ** Synopsis: content in r[P2@P3]
  3440   3385   **
................................................................................
  3452   3397   ** P3 is the number of fields in the records that will be stored by
  3453   3398   ** the pseudo-table.
  3454   3399   */
  3455   3400   case OP_OpenPseudo: {
  3456   3401     VdbeCursor *pCx;
  3457   3402   
  3458   3403     assert( pOp->p1>=0 );
         3404  +  assert( pOp->p3>=0 );
  3459   3405     pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
  3460   3406     if( pCx==0 ) goto no_mem;
  3461   3407     pCx->nullRow = 1;
  3462   3408     pCx->pseudoTableReg = pOp->p2;
  3463   3409     pCx->isTable = 1;
  3464         -  pCx->isIndex = 0;
  3465   3410     pCx->multiPseudo = pOp->p5;
  3466   3411     break;
  3467   3412   }
  3468   3413   
  3469   3414   /* Opcode: Close P1 * * * *
  3470   3415   **
  3471   3416   ** Close a cursor previously opened as P1.  If P1 is not
................................................................................
  3575   3520           pc = pOp->p2 - 1;
  3576   3521           break;
  3577   3522         }
  3578   3523         /* If we reach this point, then the P3 value must be a floating
  3579   3524         ** point number. */
  3580   3525         assert( (pIn3->flags & MEM_Real)!=0 );
  3581   3526   
  3582         -      if( iKey==SMALLEST_INT64 && (pIn3->r<(double)iKey || pIn3->r>0) ){
         3527  +      if( (iKey==SMALLEST_INT64 && pIn3->r<(double)iKey)
         3528  +       || (iKey==LARGEST_INT64 && pIn3->r>(double)iKey)
         3529  +      ){
  3583   3530           /* The P3 value is too large in magnitude to be expressed as an
  3584   3531           ** integer. */
  3585   3532           res = 1;
  3586   3533           if( pIn3->r<0 ){
  3587   3534             if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
  3588   3535               rc = sqlite3BtreeFirst(pC->pCursor, &res);
  3589   3536               if( rc!=SQLITE_OK ) goto abort_due_to_error;
................................................................................
  4155   4102       nZero = pData->u.nZero;
  4156   4103     }else{
  4157   4104       nZero = 0;
  4158   4105     }
  4159   4106     sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
  4160   4107     rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
  4161   4108                             pData->z, pData->n, nZero,
  4162         -                          pOp->p5 & OPFLAG_APPEND, seekResult
         4109  +                          (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
  4163   4110     );
  4164   4111     pC->rowidIsValid = 0;
  4165   4112     pC->deferredMoveto = 0;
  4166   4113     pC->cacheStatus = CACHE_STALE;
  4167   4114   
  4168   4115     /* Invoke the update-hook if required. */
  4169   4116     if( rc==SQLITE_OK && db->xUpdateCallback && op ){
................................................................................
  4315   4262   ** Write into register P2 the current sorter data for sorter cursor P1.
  4316   4263   */
  4317   4264   case OP_SorterData: {
  4318   4265     VdbeCursor *pC;
  4319   4266   
  4320   4267     pOut = &aMem[pOp->p2];
  4321   4268     pC = p->apCsr[pOp->p1];
  4322         -  assert( pC->isSorter );
         4269  +  assert( isSorter(pC) );
  4323   4270     rc = sqlite3VdbeSorterRowkey(pC, pOut);
  4324   4271     break;
  4325   4272   }
  4326   4273   
  4327   4274   /* Opcode: RowData P1 P2 * * *
  4328   4275   ** Synopsis: r[P2]=data
  4329   4276   **
................................................................................
  4355   4302   
  4356   4303     pOut = &aMem[pOp->p2];
  4357   4304     memAboutToChange(p, pOut);
  4358   4305   
  4359   4306     /* Note that RowKey and RowData are really exactly the same instruction */
  4360   4307     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4361   4308     pC = p->apCsr[pOp->p1];
  4362         -  assert( pC->isSorter==0 );
         4309  +  assert( isSorter(pC)==0 );
  4363   4310     assert( pC->isTable || pOp->opcode!=OP_RowData );
  4364         -  assert( pC->isIndex || pOp->opcode==OP_RowData );
         4311  +  assert( pC->isTable==0 || pOp->opcode==OP_RowData );
  4365   4312     assert( pC!=0 );
  4366   4313     assert( pC->nullRow==0 );
  4367   4314     assert( pC->pseudoTableReg==0 );
  4368   4315     assert( pC->pCursor!=0 );
  4369   4316     pCrsr = pC->pCursor;
  4370   4317     assert( sqlite3BtreeCursorIsValid(pCrsr) );
  4371   4318   
................................................................................
  4374   4321     ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
  4375   4322     ** a no-op and can never fail.  But we leave it in place as a safety.
  4376   4323     */
  4377   4324     assert( pC->deferredMoveto==0 );
  4378   4325     rc = sqlite3VdbeCursorMoveto(pC);
  4379   4326     if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
  4380   4327   
  4381         -  if( pC->isIndex ){
         4328  +  if( pC->isTable==0 ){
  4382   4329       assert( !pC->isTable );
  4383   4330       VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
  4384   4331       assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
  4385   4332       if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  4386   4333         goto too_big;
  4387   4334       }
  4388   4335       n = (u32)n64;
................................................................................
  4394   4341       }
  4395   4342     }
  4396   4343     if( sqlite3VdbeMemGrow(pOut, n, 0) ){
  4397   4344       goto no_mem;
  4398   4345     }
  4399   4346     pOut->n = n;
  4400   4347     MemSetTypeFlag(pOut, MEM_Blob);
  4401         -  if( pC->isIndex ){
         4348  +  if( pC->isTable==0 ){
  4402   4349       rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
  4403   4350     }else{
  4404   4351       rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
  4405   4352     }
  4406   4353     pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
  4407   4354     UPDATE_MAX_BLOBSIZE(pOut);
  4408   4355     REGISTER_TRACE(pOp->p2, pOut);
................................................................................
  4467   4414     VdbeCursor *pC;
  4468   4415   
  4469   4416     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4470   4417     pC = p->apCsr[pOp->p1];
  4471   4418     assert( pC!=0 );
  4472   4419     pC->nullRow = 1;
  4473   4420     pC->rowidIsValid = 0;
         4421  +  pC->cacheStatus = CACHE_STALE;
  4474   4422     assert( pC->pCursor || pC->pVtabCursor );
  4475   4423     if( pC->pCursor ){
  4476   4424       sqlite3BtreeClearCursor(pC->pCursor);
  4477   4425     }
  4478   4426     break;
  4479   4427   }
  4480   4428   
................................................................................
  4542   4490     VdbeCursor *pC;
  4543   4491     BtCursor *pCrsr;
  4544   4492     int res;
  4545   4493   
  4546   4494     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4547   4495     pC = p->apCsr[pOp->p1];
  4548   4496     assert( pC!=0 );
  4549         -  assert( pC->isSorter==(pOp->opcode==OP_SorterSort) );
         4497  +  assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
  4550   4498     res = 1;
  4551   4499     if( isSorter(pC) ){
  4552   4500       rc = sqlite3VdbeSorterRewind(db, pC, &res);
  4553   4501     }else{
  4554   4502       pCrsr = pC->pCursor;
  4555   4503       assert( pCrsr );
  4556   4504       rc = sqlite3BtreeFirst(pCrsr, &res);
  4557         -    pC->atFirst = res==0 ?1:0;
  4558   4505       pC->deferredMoveto = 0;
  4559   4506       pC->cacheStatus = CACHE_STALE;
  4560   4507       pC->rowidIsValid = 0;
  4561   4508     }
  4562   4509     pC->nullRow = (u8)res;
  4563   4510     assert( pOp->p2>0 && pOp->p2<p->nOp );
  4564   4511     if( res ){
................................................................................
  4570   4517   /* Opcode: Next P1 P2 * * P5
  4571   4518   **
  4572   4519   ** Advance cursor P1 so that it points to the next key/data pair in its
  4573   4520   ** table or index.  If there are no more key/value pairs then fall through
  4574   4521   ** to the following instruction.  But if the cursor advance was successful,
  4575   4522   ** jump immediately to P2.
  4576   4523   **
  4577         -** The P1 cursor must be for a real table, not a pseudo-table.
         4524  +** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
         4525  +** been opened prior to this opcode or the program will segfault.
  4578   4526   **
  4579   4527   ** P4 is always of type P4_ADVANCE. The function pointer points to
  4580   4528   ** sqlite3BtreeNext().
  4581   4529   **
  4582   4530   ** If P5 is positive and the jump is taken, then event counter
  4583   4531   ** number P5-1 in the prepared statement is incremented.
  4584   4532   **
  4585         -** See also: Prev
         4533  +** See also: Prev, NextIfOpen
         4534  +*/
         4535  +/* Opcode: NextIfOpen P1 P2 * * P5
         4536  +**
         4537  +** This opcode works just like OP_Next except that if cursor P1 is not
         4538  +** open it behaves a no-op.
  4586   4539   */
  4587   4540   /* Opcode: Prev P1 P2 * * P5
  4588   4541   **
  4589   4542   ** Back up cursor P1 so that it points to the previous key/data pair in its
  4590   4543   ** table or index.  If there is no previous key/value pairs then fall through
  4591   4544   ** to the following instruction.  But if the cursor backup was successful,
  4592   4545   ** jump immediately to P2.
  4593   4546   **
  4594         -** The P1 cursor must be for a real table, not a pseudo-table.
         4547  +** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
         4548  +** not open then the behavior is undefined.
  4595   4549   **
  4596   4550   ** P4 is always of type P4_ADVANCE. The function pointer points to
  4597   4551   ** sqlite3BtreePrevious().
  4598   4552   **
  4599   4553   ** If P5 is positive and the jump is taken, then event counter
  4600   4554   ** number P5-1 in the prepared statement is incremented.
  4601   4555   */
  4602         -case OP_SorterNext:    /* jump */
  4603         -case OP_Prev:          /* jump */
  4604         -case OP_Next: {        /* jump */
         4556  +/* Opcode: PrevIfOpen P1 P2 * * P5
         4557  +**
         4558  +** This opcode works just like OP_Prev except that if cursor P1 is not
         4559  +** open it behaves a no-op.
         4560  +*/
         4561  +case OP_SorterNext: {  /* jump */
  4605   4562     VdbeCursor *pC;
  4606   4563     int res;
  4607   4564   
         4565  +  pC = p->apCsr[pOp->p1];
         4566  +  assert( isSorter(pC) );
         4567  +  rc = sqlite3VdbeSorterNext(db, pC, &res);
         4568  +  goto next_tail;
         4569  +case OP_PrevIfOpen:    /* jump */
         4570  +case OP_NextIfOpen:    /* jump */
         4571  +  if( p->apCsr[pOp->p1]==0 ) break;
         4572  +  /* Fall through */
         4573  +case OP_Prev:          /* jump */
         4574  +case OP_Next:          /* jump */
  4608   4575     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4609   4576     assert( pOp->p5<ArraySize(p->aCounter) );
  4610   4577     pC = p->apCsr[pOp->p1];
  4611         -  if( pC==0 ){
  4612         -    break;  /* See ticket #2273 */
  4613         -  }
  4614         -  assert( pC->isSorter==(pOp->opcode==OP_SorterNext) );
  4615         -  if( isSorter(pC) ){
  4616         -    assert( pOp->opcode==OP_SorterNext );
  4617         -    rc = sqlite3VdbeSorterNext(db, pC, &res);
  4618         -  }else{
  4619         -    /* res = 1; // Always initialized by the xAdvance() call */
  4620         -    assert( pC->deferredMoveto==0 );
  4621         -    assert( pC->pCursor );
  4622         -    assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
  4623         -    assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
  4624         -    rc = pOp->p4.xAdvance(pC->pCursor, &res);
  4625         -  }
  4626         -  pC->nullRow = (u8)res;
         4578  +  assert( pC!=0 );
         4579  +  assert( pC->deferredMoveto==0 );
         4580  +  assert( pC->pCursor );
         4581  +  assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
         4582  +  assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
         4583  +  assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
         4584  +  assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
         4585  +  rc = pOp->p4.xAdvance(pC->pCursor, &res);
         4586  +next_tail:
  4627   4587     pC->cacheStatus = CACHE_STALE;
  4628   4588     if( res==0 ){
         4589  +    pC->nullRow = 0;
  4629   4590       pc = pOp->p2 - 1;
  4630   4591       p->aCounter[pOp->p5]++;
  4631   4592   #ifdef SQLITE_TEST
  4632   4593       sqlite3_search_count++;
  4633   4594   #endif
         4595  +  }else{
         4596  +    pC->nullRow = 1;
  4634   4597     }
  4635   4598     pC->rowidIsValid = 0;
  4636   4599     goto check_for_interrupt;
  4637   4600   }
  4638   4601   
  4639   4602   /* Opcode: IdxInsert P1 P2 P3 * P5
  4640   4603   ** Synopsis: key=r[P2]
................................................................................
  4655   4618     BtCursor *pCrsr;
  4656   4619     int nKey;
  4657   4620     const char *zKey;
  4658   4621   
  4659   4622     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4660   4623     pC = p->apCsr[pOp->p1];
  4661   4624     assert( pC!=0 );
  4662         -  assert( pC->isSorter==(pOp->opcode==OP_SorterInsert) );
         4625  +  assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
  4663   4626     pIn2 = &aMem[pOp->p2];
  4664   4627     assert( pIn2->flags & MEM_Blob );
  4665   4628     pCrsr = pC->pCursor;
  4666   4629     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
  4667   4630     assert( pCrsr!=0 );
  4668   4631     assert( pC->isTable==0 );
  4669   4632     rc = ExpandBlob(pIn2);
................................................................................
  5906   5869       /* Initialize sqlite3_vtab_cursor base class */
  5907   5870       pVtabCursor->pVtab = pVtab;
  5908   5871   
  5909   5872       /* Initialize vdbe cursor object */
  5910   5873       pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
  5911   5874       if( pCur ){
  5912   5875         pCur->pVtabCursor = pVtabCursor;
  5913         -      pCur->pModule = pVtabCursor->pVtab->pModule;
  5914   5876       }else{
  5915   5877         db->mallocFailed = 1;
  5916   5878         pModule->xClose(pVtabCursor);
  5917   5879       }
  5918   5880     }
  5919   5881     break;
  5920   5882   }

Changes to src/vdbeInt.h.

    32     32   ** of the following structure.
    33     33   */
    34     34   typedef struct VdbeOp Op;
    35     35   
    36     36   /*
    37     37   ** Boolean values
    38     38   */
    39         -typedef unsigned char Bool;
           39  +typedef unsigned Bool;
    40     40   
    41     41   /* Opaque type used by code in vdbesort.c */
    42     42   typedef struct VdbeSorter VdbeSorter;
    43     43   
    44     44   /* Opaque type used by the explainer */
    45     45   typedef struct Explain Explain;
    46     46   
................................................................................
    49     49   
    50     50   /*
    51     51   ** A cursor is a pointer into a single BTree within a database file.
    52     52   ** The cursor can seek to a BTree entry with a particular key, or
    53     53   ** loop over all entries of the Btree.  You can also insert new BTree
    54     54   ** entries or retrieve the key or data from the entry that the cursor
    55     55   ** is currently pointing to.
           56  +**
           57  +** Cursors can also point to virtual tables, sorters, or "pseudo-tables".
           58  +** A pseudo-table is a single-row table implemented by registers.
    56     59   ** 
    57     60   ** Every cursor that the virtual machine has open is represented by an
    58     61   ** instance of the following structure.
    59     62   */
    60     63   struct VdbeCursor {
    61     64     BtCursor *pCursor;    /* The cursor structure of the backend */
    62     65     Btree *pBt;           /* Separate file holding temporary table */
    63     66     KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
    64         -  int iDb;              /* Index of cursor database in db->aDb[] (or -1) */
           67  +  int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
    65     68     int pseudoTableReg;   /* Register holding pseudotable content. */
    66         -  int nField;           /* Number of fields in the header */
    67         -  Bool zeroed;          /* True if zeroed out and ready for reuse */
    68         -  Bool rowidIsValid;    /* True if lastRowid is valid */
    69         -  Bool atFirst;         /* True if pointing to first entry */
    70         -  Bool useRandomRowid;  /* Generate new record numbers semi-randomly */
    71         -  Bool nullRow;         /* True if pointing to a row with no data */
    72         -  Bool deferredMoveto;  /* A call to sqlite3BtreeMoveto() is needed */
    73         -  Bool isTable;         /* True if a table requiring integer keys */
    74         -  Bool isIndex;         /* True if an index containing keys only - no data */
    75         -  Bool isOrdered;       /* True if the underlying table is BTREE_UNORDERED */
    76         -  Bool isSorter;        /* True if a new-style sorter */
    77         -  Bool multiPseudo;     /* Multi-register pseudo-cursor */
           69  +  i16 nField;           /* Number of fields in the header */
           70  +  u16 nHdrParsed;       /* Number of header fields parsed so far */
           71  +  i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
           72  +  u8 nullRow;           /* True if pointing to a row with no data */
           73  +  u8 rowidIsValid;      /* True if lastRowid is valid */
           74  +  u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
           75  +  Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
           76  +  Bool isTable:1;       /* True if a table requiring integer keys */
           77  +  Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
           78  +  Bool multiPseudo:1;   /* Multi-register pseudo-cursor */
    78     79     sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
    79         -  const sqlite3_module *pModule;     /* Module for cursor pVtabCursor */
    80     80     i64 seqCount;         /* Sequence counter */
    81     81     i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
    82         -  i64 lastRowid;        /* Last rowid from a Next or NextIdx operation */
           82  +  i64 lastRowid;        /* Rowid being deleted by OP_Delete */
    83     83     VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
    84     84   
    85         -  /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists */
    86         -  int seekResult;
    87         -
    88     85     /* Cached information about the header for the data record that the
    89     86     ** cursor is currently pointing to.  Only valid if cacheStatus matches
    90     87     ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
    91     88     ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
    92     89     ** the cache is out of date.
    93     90     **
    94     91     ** aRow might point to (ephemeral) data for the current row, or it might
    95     92     ** be NULL.
    96     93     */
    97     94     u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
    98         -  int payloadSize;      /* Total number of bytes in the record */
    99         -  u32 *aType;           /* Type values for all entries in the record */
   100         -  u32 *aOffset;         /* Cached offsets to the start of each columns data */
   101         -  u8 *aRow;             /* Data for the current row, if all on one page */
           95  +  u32 payloadSize;      /* Total number of bytes in the record */
           96  +  u32 szRow;            /* Byte available in aRow */
           97  +  u32 iHdrOffset;       /* Offset to next unparsed byte of the header */
           98  +  const u8 *aRow;       /* Data for the current row, if all on one page */
           99  +  u32 aType[1];         /* Type values for all entries in the record */
          100  +  /* 2*nField extra array elements allocated for aType[], beyond the one
          101  +  ** static element declared in the structure.  nField total array slots for
          102  +  ** aType[] and nField+1 array slots for aOffset[] */
   102    103   };
   103    104   typedef struct VdbeCursor VdbeCursor;
   104    105   
   105    106   /*
   106    107   ** When a sub-program is executed (OP_Program), a structure of this type
   107    108   ** is allocated to store the current value of the program counter, as
   108    109   ** well as the current memory cell array and various other frame specific
................................................................................
   438    439   int sqlite3VdbeMemStringify(Mem*, int);
   439    440   i64 sqlite3VdbeIntValue(Mem*);
   440    441   int sqlite3VdbeMemIntegerify(Mem*);
   441    442   double sqlite3VdbeRealValue(Mem*);
   442    443   void sqlite3VdbeIntegerAffinity(Mem*);
   443    444   int sqlite3VdbeMemRealify(Mem*);
   444    445   int sqlite3VdbeMemNumerify(Mem*);
   445         -int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
          446  +int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
   446    447   void sqlite3VdbeMemRelease(Mem *p);
   447    448   void sqlite3VdbeMemReleaseExternal(Mem *p);
   448    449   #define VdbeMemRelease(X)  \
   449    450     if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
   450    451       sqlite3VdbeMemReleaseExternal(X);
   451    452   int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
   452    453   const char *sqlite3OpcodeName(int);

Changes to src/vdbeaux.c.

   450    450           assert( pOp[-1].opcode==OP_Integer );
   451    451           n = pOp[-1].p1;
   452    452           if( n>nMaxArgs ) nMaxArgs = n;
   453    453           break;
   454    454         }
   455    455   #endif
   456    456         case OP_Next:
          457  +      case OP_NextIfOpen:
   457    458         case OP_SorterNext: {
   458    459           pOp->p4.xAdvance = sqlite3BtreeNext;
   459    460           pOp->p4type = P4_ADVANCE;
   460    461           break;
   461    462         }
   462         -      case OP_Prev: {
          463  +      case OP_Prev:
          464  +      case OP_PrevIfOpen: {
   463    465           pOp->p4.xAdvance = sqlite3BtreePrevious;
   464    466           pOp->p4type = P4_ADVANCE;
   465    467           break;
   466    468         }
   467    469       }
   468    470   
   469    471       pOp->opflags = sqlite3OpcodeProperty[opcode];
................................................................................
  1671   1673       ** the call above. */
  1672   1674     }else if( pCx->pCursor ){
  1673   1675       sqlite3BtreeCloseCursor(pCx->pCursor);
  1674   1676     }
  1675   1677   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1676   1678     if( pCx->pVtabCursor ){
  1677   1679       sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
  1678         -    const sqlite3_module *pModule = pCx->pModule;
         1680  +    const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
  1679   1681       p->inVtabMethod = 1;
  1680   1682       pModule->xClose(pVtabCursor);
  1681   1683       p->inVtabMethod = 0;
  1682   1684     }
  1683   1685   #endif
  1684   1686   }
  1685   1687   
................................................................................
  2655   2657       if( res!=0 ) return SQLITE_CORRUPT_BKPT;
  2656   2658       p->rowidIsValid = 1;
  2657   2659   #ifdef SQLITE_TEST
  2658   2660       sqlite3_search_count++;
  2659   2661   #endif
  2660   2662       p->deferredMoveto = 0;
  2661   2663       p->cacheStatus = CACHE_STALE;
  2662         -  }else if( ALWAYS(p->pCursor) ){
         2664  +  }else if( p->pCursor ){
  2663   2665       int hasMoved;
  2664   2666       int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
  2665   2667       if( rc ) return rc;
  2666   2668       if( hasMoved ){
  2667   2669         p->cacheStatus = CACHE_STALE;
  2668   2670         p->nullRow = 1;
  2669   2671       }
................................................................................
  2963   2965       case 8:    /* Integer 0 */
  2964   2966       case 9: {  /* Integer 1 */
  2965   2967         pMem->u.i = serial_type-8;
  2966   2968         pMem->flags = MEM_Int;
  2967   2969         return 0;
  2968   2970       }
  2969   2971       default: {
         2972  +      static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
  2970   2973         u32 len = (serial_type-12)/2;
  2971   2974         pMem->z = (char *)buf;
  2972   2975         pMem->n = len;
  2973   2976         pMem->xDel = 0;
  2974         -      if( serial_type&0x01 ){
  2975         -        pMem->flags = MEM_Str | MEM_Ephem;
  2976         -      }else{
  2977         -        pMem->flags = MEM_Blob | MEM_Ephem;
  2978         -      }
         2977  +      pMem->flags = aFlag[serial_type&1];
  2979   2978         return len;
  2980   2979       }
  2981   2980     }
  2982   2981     return 0;
  2983   2982   }
  2984   2983   
  2985   2984   /*
................................................................................
  3113   3112     */
  3114   3113     /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
  3115   3114     
  3116   3115     idx1 = getVarint32(aKey1, szHdr1);
  3117   3116     d1 = szHdr1;
  3118   3117     assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField );
  3119   3118     assert( pKeyInfo->aSortOrder!=0 );
  3120         -  while( idx1<szHdr1 && i<pPKey2->nField ){
         3119  +  assert( idx1<szHdr1 && i<pPKey2->nField );
         3120  +  do{
  3121   3121       u32 serial_type1;
  3122   3122   
  3123   3123       /* Read the serial types for the next element in each key. */
  3124   3124       idx1 += getVarint32( aKey1+idx1, serial_type1 );
  3125   3125   
  3126   3126       /* Verify that there is enough key space remaining to avoid
  3127   3127       ** a buffer overread.  The "d1+serial_type1+2" subexpression will
................................................................................
  3146   3146         assert( mem1.zMalloc==0 );  /* See comment below */
  3147   3147         if( pKeyInfo->aSortOrder[i] ){
  3148   3148           rc = -rc;  /* Invert the result for DESC sort order. */
  3149   3149         }
  3150   3150         return rc;
  3151   3151       }
  3152   3152       i++;
  3153         -  }
         3153  +  }while( idx1<szHdr1 && i<pPKey2->nField );
  3154   3154   
  3155   3155     /* No memory allocation is ever used on mem1.  Prove this using
  3156   3156     ** the following assert().  If the assert() fails, it indicates a
  3157   3157     ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
  3158   3158     */
  3159   3159     assert( mem1.zMalloc==0 );
  3160   3160   
................................................................................
  3204   3204     assert( sqlite3BtreeCursorIsValid(pCur) );
  3205   3205     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
  3206   3206     assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
  3207   3207     assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
  3208   3208   
  3209   3209     /* Read in the complete content of the index entry */
  3210   3210     memset(&m, 0, sizeof(m));
  3211         -  rc = sqlite3VdbeMemFromBtree(pCur, 0, (int)nCellKey, 1, &m);
         3211  +  rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
  3212   3212     if( rc ){
  3213   3213       return rc;
  3214   3214     }
  3215   3215   
  3216   3216     /* The index entry must begin with a header size */
  3217   3217     (void)getVarint32((u8*)m.z, szHdr);
  3218   3218     testcase( szHdr==3 );
................................................................................
  3282   3282     /* nCellKey will always be between 0 and 0xffffffff because of the say
  3283   3283     ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
  3284   3284     if( nCellKey<=0 || nCellKey>0x7fffffff ){
  3285   3285       *res = 0;
  3286   3286       return SQLITE_CORRUPT_BKPT;
  3287   3287     }
  3288   3288     memset(&m, 0, sizeof(m));
  3289         -  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
         3289  +  rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
  3290   3290     if( rc ){
  3291   3291       return rc;
  3292   3292     }
  3293   3293     assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
  3294   3294     *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
  3295   3295     sqlite3VdbeMemRelease(&m);
  3296   3296     return SQLITE_OK;

Changes to src/vdbeblob.c.

    62     62     ** triggering asserts related to mutexes.
    63     63     */
    64     64     assert( v->aVar[0].flags&MEM_Int );
    65     65     v->aVar[0].u.i = iRow;
    66     66   
    67     67     rc = sqlite3_step(p->pStmt);
    68     68     if( rc==SQLITE_ROW ){
    69         -    u32 type = v->apCsr[0]->aType[p->iCol];
           69  +    VdbeCursor *pC = v->apCsr[0];
           70  +    u32 type = pC->aType[p->iCol];
    70     71       if( type<12 ){
    71     72         zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
    72     73             type==0?"null": type==7?"real": "integer"
    73     74         );
    74     75         rc = SQLITE_ERROR;
    75     76         sqlite3_finalize(p->pStmt);
    76     77         p->pStmt = 0;
    77     78       }else{
    78         -      p->iOffset = v->apCsr[0]->aOffset[p->iCol];
           79  +      p->iOffset = pC->aType[p->iCol + pC->nField];
    79     80         p->nByte = sqlite3VdbeSerialTypeLen(type);
    80         -      p->pCsr =  v->apCsr[0]->pCursor;
           81  +      p->pCsr =  pC->pCursor;
    81     82         sqlite3BtreeEnterCursor(p->pCsr);
    82     83         sqlite3BtreeCacheOverflow(p->pCsr);
    83     84         sqlite3BtreeLeaveCursor(p->pCsr);
    84     85       }
    85     86     }
    86     87   
    87     88     if( rc==SQLITE_ROW ){
................................................................................
   328    329       *ppBlob = (sqlite3_blob *)pBlob;
   329    330     }else{
   330    331       if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
   331    332       sqlite3DbFree(db, pBlob);
   332    333     }
   333    334     sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
   334    335     sqlite3DbFree(db, zErr);
          336  +  sqlite3ParserReset(pParse);
   335    337     sqlite3StackFree(db, pParse);
   336    338     rc = sqlite3ApiExit(db, rc);
   337    339     sqlite3_mutex_leave(db->mutex);
   338    340     return rc;
   339    341   }
   340    342   
   341    343   /*

Changes to src/vdbemem.c.

   299    299     p->z = 0;
   300    300     p->zMalloc = 0;
   301    301     p->xDel = 0;
   302    302   }
   303    303   
   304    304   /*
   305    305   ** Convert a 64-bit IEEE double into a 64-bit signed integer.
   306         -** If the double is too large, return 0x8000000000000000.
   307         -**
   308         -** Most systems appear to do this simply by assigning
   309         -** variables and without the extra range tests.  But
   310         -** there are reports that windows throws an expection
   311         -** if the floating point value is out of range. (See ticket #2880.)
   312         -** Because we do not completely understand the problem, we will
   313         -** take the conservative approach and always do range tests
   314         -** before attempting the conversion.
          306  +** If the double is out of range of a 64-bit signed integer then
          307  +** return the closest available 64-bit signed integer.
   315    308   */
   316    309   static i64 doubleToInt64(double r){
   317    310   #ifdef SQLITE_OMIT_FLOATING_POINT
   318    311     /* When floating-point is omitted, double and int64 are the same thing */
   319    312     return r;
   320    313   #else
   321    314     /*
................................................................................
   324    317     ** inconsistently.  And many do not understand the "LL" notation.
   325    318     ** So we define our own static constants here using nothing
   326    319     ** larger than a 32-bit integer constant.
   327    320     */
   328    321     static const i64 maxInt = LARGEST_INT64;
   329    322     static const i64 minInt = SMALLEST_INT64;
   330    323   
   331         -  if( r<(double)minInt ){
          324  +  if( r<=(double)minInt ){
   332    325       return minInt;
   333         -  }else if( r>(double)maxInt ){
   334         -    /* minInt is correct here - not maxInt.  It turns out that assigning
   335         -    ** a very large positive number to an integer results in a very large
   336         -    ** negative integer.  This makes no sense, but it is what x86 hardware
   337         -    ** does so for compatibility we will do the same in software. */
   338         -    return minInt;
          326  +  }else if( r>=(double)maxInt ){
          327  +    return maxInt;
   339    328     }else{
   340    329       return (i64)r;
   341    330     }
   342    331   #endif
   343    332   }
   344    333   
   345    334   /*
................................................................................
   413    402     **
   414    403     **    (1) the round-trip conversion real->int->real is a no-op, and
   415    404     **    (2) The integer is neither the largest nor the smallest
   416    405     **        possible integer (ticket #3922)
   417    406     **
   418    407     ** The second and third terms in the following conditional enforces
   419    408     ** the second condition under the assumption that addition overflow causes
   420         -  ** values to wrap around.  On x86 hardware, the third term is always
   421         -  ** true and could be omitted.  But we leave it in because other
   422         -  ** architectures might behave differently.
          409  +  ** values to wrap around.
   423    410     */
   424    411     if( pMem->r==(double)pMem->u.i
   425    412      && pMem->u.i>SMALLEST_INT64
   426         -#if defined(__i486__) || defined(__x86_64__)
   427         -   && ALWAYS(pMem->u.i<LARGEST_INT64)
   428         -#else
   429    413      && pMem->u.i<LARGEST_INT64
   430         -#endif
   431    414     ){
   432    415       pMem->flags |= MEM_Int;
   433    416     }
   434    417   }
   435    418   
   436    419   /*
   437    420   ** Convert pMem to type integer.  Invalidate any prior representations.
................................................................................
   892    875   ** is overwritten without being freed.
   893    876   **
   894    877   ** If this routine fails for any reason (malloc returns NULL or unable
   895    878   ** to read from the disk) then the pMem is left in an inconsistent state.
   896    879   */
   897    880   int sqlite3VdbeMemFromBtree(
   898    881     BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
   899         -  int offset,       /* Offset from the start of data to return bytes from. */
   900         -  int amt,          /* Number of bytes to return. */
          882  +  u32 offset,       /* Offset from the start of data to return bytes from. */
          883  +  u32 amt,          /* Number of bytes to return. */
   901    884     int key,          /* If true, retrieve from the btree key, not data. */
   902    885     Mem *pMem         /* OUT: Return data in this Mem structure. */
   903    886   ){
   904    887     char *zData;        /* Data from the btree layer */
   905         -  int available = 0;  /* Number of bytes available on the local btree page */
          888  +  u32 available = 0;  /* Number of bytes available on the local btree page */
   906    889     int rc = SQLITE_OK; /* Return code */
   907    890   
   908    891     assert( sqlite3BtreeCursorIsValid(pCur) );
   909    892   
   910    893     /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
   911    894     ** that both the BtShared and database handle mutexes are held. */
   912    895     assert( (pMem->flags & MEM_RowSet)==0 );
................................................................................
   913    896     if( key ){
   914    897       zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
   915    898     }else{
   916    899       zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
   917    900     }
   918    901     assert( zData!=0 );
   919    902   
   920         -  if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
          903  +  if( offset+amt<=available ){
   921    904       sqlite3VdbeMemRelease(pMem);
   922    905       pMem->z = &zData[offset];
   923    906       pMem->flags = MEM_Blob|MEM_Ephem;
   924    907     }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
   925    908       pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
   926    909       pMem->enc = 0;
   927    910       pMem->type = SQLITE_BLOB;
................................................................................
   932    915       }
   933    916       pMem->z[amt] = 0;
   934    917       pMem->z[amt+1] = 0;
   935    918       if( rc!=SQLITE_OK ){
   936    919         sqlite3VdbeMemRelease(pMem);
   937    920       }
   938    921     }
   939         -  pMem->n = amt;
          922  +  pMem->n = (int)amt;
   940    923   
   941    924     return rc;
   942    925   }
   943    926   
   944    927   /* This function is only available internally, it is not part of the
   945    928   ** external API. It works in a similar way to sqlite3_value_text(),
   946    929   ** except the data returned is in the encoding specified by the second

Changes to src/vtab.c.

   734    734       }
   735    735       pParse->declareVtab = 0;
   736    736     
   737    737       if( pParse->pVdbe ){
   738    738         sqlite3VdbeFinalize(pParse->pVdbe);
   739    739       }
   740    740       sqlite3DeleteTable(db, pParse->pNewTable);
          741  +    sqlite3ParserReset(pParse);
   741    742       sqlite3StackFree(db, pParse);
   742    743     }
   743    744   
   744    745     assert( (rc&0xff)==rc );
   745    746     rc = sqlite3ApiExit(db, rc);
   746    747     sqlite3_mutex_leave(db->mutex);
   747    748     return rc;

Changes to src/where.c.

   665    665       ** be the name of an indexed column with TEXT affinity. */
   666    666       return 0;
   667    667     }
   668    668     assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
   669    669   
   670    670     pRight = pList->a[0].pExpr;
   671    671     op = pRight->op;
   672         -  if( op==TK_REGISTER ){
   673         -    op = pRight->op2;
   674         -  }
   675    672     if( op==TK_VARIABLE ){
   676    673       Vdbe *pReprepare = pParse->pReprepare;
   677    674       int iCol = pRight->iColumn;
   678    675       pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
   679    676       if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
   680    677         z = (char *)sqlite3_value_text(pVal);
   681    678       }
................................................................................
  2405   2402         pIn += pLevel->u.in.nIn - 1;
  2406   2403         pIn->iCur = iTab;
  2407   2404         if( eType==IN_INDEX_ROWID ){
  2408   2405           pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
  2409   2406         }else{
  2410   2407           pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
  2411   2408         }
  2412         -      pIn->eEndLoopOp = bRev ? OP_Prev : OP_Next;
         2409  +      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
  2413   2410         sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
  2414   2411       }else{
  2415   2412         pLevel->u.in.nIn = 0;
  2416   2413       }
  2417   2414   #endif
  2418   2415     }
  2419   2416     disableTerm(pLevel, pTerm);
................................................................................
  2755   2752     pLoop = pLevel->pWLoop;
  2756   2753     pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
  2757   2754     iCur = pTabItem->iCursor;
  2758   2755     pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
  2759   2756     bRev = (pWInfo->revMask>>iLevel)&1;
  2760   2757     omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
  2761   2758              && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
  2762         -  VdbeNoopComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
         2759  +  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
  2763   2760   
  2764   2761     /* Create labels for the "break" and "continue" instructions
  2765   2762     ** for the current loop.  Jump to addrBrk to break out of a loop.
  2766   2763     ** Jump to cont to go immediately to the next iteration of the
  2767   2764     ** loop.
  2768   2765     **
  2769   2766     ** When there is an IN operator, we also have a "addrNxt" label that
................................................................................
  2983   2980       };
  2984   2981       static const u8 aEndOp[] = {
  2985   2982         OP_Noop,             /* 0: (!end_constraints) */
  2986   2983         OP_IdxGE,            /* 1: (end_constraints && !bRev) */
  2987   2984         OP_IdxLT             /* 2: (end_constraints && bRev) */
  2988   2985       };
  2989   2986       u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
  2990         -    u16 nSkip = pLoop->u.btree.nSkip; /* Number of left index terms to skip */
  2991   2987       int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
  2992   2988       int regBase;                 /* Base register holding constraint values */
  2993   2989       int r1;                      /* Temp register */
  2994   2990       WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
  2995   2991       WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
  2996   2992       int startEq;                 /* True if range start uses ==, >= or <= */
  2997   2993       int endEq;                   /* True if range end uses ==, >= or <= */
................................................................................
  2998   2994       int start_constraints;       /* Start of range is constrained */
  2999   2995       int nConstraint;             /* Number of constraint terms */
  3000   2996       Index *pIdx;                 /* The index we will be using */
  3001   2997       int iIdxCur;                 /* The VDBE cursor for the index */
  3002   2998       int nExtraReg = 0;           /* Number of extra registers needed */
  3003   2999       int op;                      /* Instruction opcode */
  3004   3000       char *zStartAff;             /* Affinity for start of range constraint */
  3005         -    char *zEndAff;               /* Affinity for end of range constraint */
         3001  +    char cEndAff = 0;            /* Affinity for end of range constraint */
  3006   3002   
  3007   3003       pIdx = pLoop->u.btree.pIndex;
  3008   3004       iIdxCur = pLevel->iIdxCur;
  3009         -    assert( nEq>=nSkip );
         3005  +    assert( nEq>=pLoop->u.btree.nSkip );
  3010   3006   
  3011   3007       /* If this loop satisfies a sort order (pOrderBy) request that 
  3012   3008       ** was passed to this function to implement a "SELECT min(x) ..." 
  3013   3009       ** query, then the caller will only allow the loop to run for
  3014   3010       ** a single iteration. This means that the first row returned
  3015   3011       ** should not have a NULL value stored in 'x'. If column 'x' is
  3016   3012       ** the first one after the nEq equality constraints in the index,
  3017   3013       ** this requires some special handling.
  3018   3014       */
  3019   3015       if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
  3020   3016        && (pWInfo->bOBSat!=0)
  3021   3017        && (pIdx->nKeyCol>nEq)
  3022   3018       ){
  3023         -      assert( nSkip==0 );
         3019  +      assert( pLoop->u.btree.nSkip==0 );
  3024   3020         isMinQuery = 1;
  3025   3021         nExtraReg = 1;
  3026   3022       }
  3027   3023   
  3028   3024       /* Find any inequality constraint terms for the start and end 
  3029   3025       ** of the range. 
  3030   3026       */
................................................................................
  3039   3035       }
  3040   3036   
  3041   3037       /* Generate code to evaluate all constraint terms using == or IN
  3042   3038       ** and store the values of those terms in an array of registers
  3043   3039       ** starting at regBase.
  3044   3040       */
  3045   3041       regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
  3046         -    zEndAff = sqlite3DbStrDup(db, zStartAff);
         3042  +    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
         3043  +    if( zStartAff ) cEndAff = zStartAff[nEq];
  3047   3044       addrNxt = pLevel->addrNxt;
  3048   3045   
  3049   3046       /* If we are doing a reverse order scan on an ascending index, or
  3050   3047       ** a forward order scan on a descending index, interchange the 
  3051   3048       ** start and end terms (pRangeStart and pRangeEnd).
  3052   3049       */
  3053   3050       if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
................................................................................
  3109   3106       if( pRangeEnd ){
  3110   3107         Expr *pRight = pRangeEnd->pExpr->pRight;
  3111   3108         sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
  3112   3109         sqlite3ExprCode(pParse, pRight, regBase+nEq);
  3113   3110         if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
  3114   3111           sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
  3115   3112         }
  3116         -      if( zEndAff ){
  3117         -        if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){
  3118         -          /* Since the comparison is to be performed with no conversions
  3119         -          ** applied to the operands, set the affinity to apply to pRight to 
  3120         -          ** SQLITE_AFF_NONE.  */
  3121         -          zEndAff[nEq] = SQLITE_AFF_NONE;
  3122         -        }
  3123         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){
  3124         -          zEndAff[nEq] = SQLITE_AFF_NONE;
  3125         -        }
  3126         -      }  
  3127         -      codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
         3113  +      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
         3114  +       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
         3115  +      ){
         3116  +        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
         3117  +      }
  3128   3118         nConstraint++;
  3129   3119         testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
  3130   3120       }
  3131   3121       sqlite3DbFree(db, zStartAff);
  3132         -    sqlite3DbFree(db, zEndAff);
  3133   3122   
  3134   3123       /* Top of the loop body */
  3135   3124       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3136   3125   
  3137   3126       /* Check if the index cursor is past the end of the range. */
  3138   3127       op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
  3139   3128       testcase( op==OP_Noop );
................................................................................
  3150   3139       */
  3151   3140       r1 = sqlite3GetTempReg(pParse);
  3152   3141       testcase( pLoop->wsFlags & WHERE_BTM_LIMIT );
  3153   3142       testcase( pLoop->wsFlags & WHERE_TOP_LIMIT );
  3154   3143       if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 
  3155   3144        && (j = pIdx->aiColumn[nEq])>=0 
  3156   3145        && pIdx->pTable->aCol[j].notNull==0 
         3146  +     && (nEq || (pLoop->wsFlags & WHERE_BTM_LIMIT)==0)
  3157   3147       ){
  3158   3148         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
  3159   3149         VdbeComment((v, "%s", pIdx->pTable->aCol[j].zName));
  3160   3150         sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
  3161   3151       }
  3162   3152       sqlite3ReleaseTempReg(pParse, r1);
  3163   3153   
................................................................................
  3467   3457       assert( !ExprHasProperty(pE, EP_FromJoin) );
  3468   3458       assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
  3469   3459       pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
  3470   3460       if( pAlt==0 ) continue;
  3471   3461       if( pAlt->wtFlags & (TERM_CODED) ) continue;
  3472   3462       testcase( pAlt->eOperator & WO_EQ );
  3473   3463       testcase( pAlt->eOperator & WO_IN );
  3474         -    VdbeNoopComment((v, "begin transitive constraint"));
         3464  +    VdbeModuleComment((v, "begin transitive constraint"));
  3475   3465       pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
  3476   3466       if( pEAlt ){
  3477   3467         *pEAlt = *pAlt->pExpr;
  3478   3468         pEAlt->pLeft = pE->pLeft;
  3479   3469         sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
  3480   3470         sqlite3StackFree(db, pEAlt);
  3481   3471       }
................................................................................
  3924   3914     saved_nSkip = pNew->u.btree.nSkip;
  3925   3915     saved_nLTerm = pNew->nLTerm;
  3926   3916     saved_wsFlags = pNew->wsFlags;
  3927   3917     saved_prereq = pNew->prereq;
  3928   3918     saved_nOut = pNew->nOut;
  3929   3919     pNew->rSetup = 0;
  3930   3920     rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
         3921  +
         3922  +  /* Consider using a skip-scan if there are no WHERE clause constraints
         3923  +  ** available for the left-most terms of the index, and if the average
         3924  +  ** number of repeats in the left-most terms is at least 50.
         3925  +  */
  3931   3926     if( pTerm==0
  3932   3927      && saved_nEq==saved_nSkip
  3933   3928      && saved_nEq+1<pProbe->nKeyCol
  3934         -   && pProbe->aiRowEst[saved_nEq+1]>50
         3929  +   && pProbe->aiRowEst[saved_nEq+1]>50  /* TUNING: Minimum for skip-scan */
  3935   3930     ){
  3936   3931       LogEst nIter;
  3937   3932       pNew->u.btree.nEq++;
  3938   3933       pNew->u.btree.nSkip++;
  3939   3934       pNew->aLTerm[pNew->nLTerm++] = 0;
  3940   3935       pNew->wsFlags |= WHERE_SKIPSCAN;
  3941   3936       nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
................................................................................
  5423   5418   #endif
  5424   5419   
  5425   5420     /* Split the WHERE clause into separate subexpressions where each
  5426   5421     ** subexpression is separated by an AND operator.
  5427   5422     */
  5428   5423     initMaskSet(pMaskSet);
  5429   5424     whereClauseInit(&pWInfo->sWC, pWInfo);
  5430         -  sqlite3ExprCodeConstants(pParse, pWhere);
  5431   5425     whereSplit(&pWInfo->sWC, pWhere, TK_AND);
  5432   5426     sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
  5433   5427       
  5434   5428     /* Special case: a WHERE clause that is constant.  Evaluate the
  5435   5429     ** expression and either jump over all of the code or fall thru.
  5436   5430     */
  5437   5431     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
................................................................................
  5738   5732       explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
  5739   5733       pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
  5740   5734       notReady = codeOneLoopStart(pWInfo, ii, notReady);
  5741   5735       pWInfo->iContinue = pLevel->addrCont;
  5742   5736     }
  5743   5737   
  5744   5738     /* Done. */
  5745         -  VdbeNoopComment((v, "Begin WHERE-core"));
         5739  +  VdbeModuleComment((v, "Begin WHERE-core"));
  5746   5740     return pWInfo;
  5747   5741   
  5748   5742     /* Jump here if malloc fails */
  5749   5743   whereBeginError:
  5750   5744     if( pWInfo ){
  5751   5745       pParse->nQueryLoop = pWInfo->savedNQueryLoop;
  5752   5746       whereInfoFree(db, pWInfo);
................................................................................
  5765   5759     WhereLevel *pLevel;
  5766   5760     WhereLoop *pLoop;
  5767   5761     SrcList *pTabList = pWInfo->pTabList;
  5768   5762     sqlite3 *db = pParse->db;
  5769   5763   
  5770   5764     /* Generate loop termination code.
  5771   5765     */
  5772         -  VdbeNoopComment((v, "End WHERE-core"));
         5766  +  VdbeModuleComment((v, "End WHERE-core"));
  5773   5767     sqlite3ExprCacheClear(pParse);
  5774   5768     for(i=pWInfo->nLevel-1; i>=0; i--){
  5775   5769       int addr;
  5776   5770       pLevel = &pWInfo->a[i];
  5777   5771       pLoop = pLevel->pWLoop;
  5778   5772       sqlite3VdbeResolveLabel(v, pLevel->addrCont);
  5779   5773       if( pLevel->op!=OP_Noop ){
................................................................................
  5811   5805         if( pLevel->op==OP_Return ){
  5812   5806           sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
  5813   5807         }else{
  5814   5808           sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
  5815   5809         }
  5816   5810         sqlite3VdbeJumpHere(v, addr);
  5817   5811       }
  5818         -    VdbeNoopComment((v, "End WHERE-loop%d: %s", i,
         5812  +    VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
  5819   5813                        pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
  5820   5814     }
  5821   5815   
  5822   5816     /* The "break" point is here, just past the end of the outer loop.
  5823   5817     ** Set it.
  5824   5818     */
  5825   5819     sqlite3VdbeResolveLabel(v, pWInfo->iBreak);

Changes to test/analyze9.test.

   801    801     reset_db
   802    802     execsql {
   803    803       CREATE TABLE t1(a, UNIQUE(a));
   804    804       INSERT INTO t1 VALUES($two);
   805    805       ANALYZE;
   806    806     }
   807    807     set nByte2 [lindex [sqlite3_db_status db SCHEMA_USED 0] 1]
          808  +  puts -nonewline " (nByte=$nByte nByte2=$nByte2)"
   808    809   
   809         -  expr {$nByte2 > $nByte+900 && $nByte2 < $nByte+1050}
          810  +  expr {$nByte2 > $nByte+900 && $nByte2 < $nByte+1100}
   810    811   } {1}
   811    812   
   812    813   #-------------------------------------------------------------------------
   813    814   # Test that stat4 data may be used with partial indexes.
   814    815   #
   815    816   do_test 17.1 {
   816    817     reset_db

Changes to test/atof1.test.

    11     11   # 
    12     12   # Tests of the sqlite3AtoF() function.
    13     13   #
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17     17   
    18         -if {![info exists __GNUC__]} {
           18  +if {![info exists __GNUC__] || [regexp arm $tcl_platform(machine)]} {
    19     19     finish_test
    20     20     return
    21     21   }
    22     22   
    23     23   expr srand(1)
    24     24   for {set i 1} {$i<20000} {incr i} {
    25     25     set pow [expr {int((rand()-0.5)*100)}]

Changes to test/autoinc.test.

   212    212   do_test autoinc-2.27 {
   213    213     execsql {
   214    214       SELECT * FROM sqlite_sequence;
   215    215     }
   216    216   } {t1 1238}
   217    217   do_test autoinc-2.28 {
   218    218     execsql {
   219         -    UPDATE sqlite_sequence SET seq='12345678901234567890'
          219  +    UPDATE sqlite_sequence SET seq='-12345678901234567890'
   220    220         WHERE name='t1';
   221    221       INSERT INTO t1 VALUES(NULL,6);
   222    222       SELECT * FROM t1;
   223    223     }
   224    224   } {235 1 1235 2 1236 3 1237 4 1238 5 1239 6}
   225    225   do_test autoinc-2.29 {
   226    226     execsql {

Changes to test/default.test.

    60     60       execsql {
    61     61         INSERT INTO t4 DEFAULT VALUES;
    62     62         PRAGMA table_info(t4);
    63     63       }
    64     64     } {0 c {} 0 'abc' 0}
    65     65   }
    66     66   
           67  +do_execsql_test default-3.1 {
           68  +  CREATE TABLE t3(
           69  +    a INTEGER PRIMARY KEY AUTOINCREMENT,
           70  +    b INT DEFAULT 12345 UNIQUE NOT NULL CHECK( b>=0 AND b<99999 ),
           71  +    c VARCHAR(123,456) DEFAULT 'hello' NOT NULL ON CONFLICT REPLACE,
           72  +    d REAL,
           73  +    e FLOATING POINT(5,10) DEFAULT 4.36,
           74  +    f NATIONAL CHARACTER(15) COLLATE RTRIM,
           75  +    g LONG INTEGER DEFAULT( 3600*12 )
           76  +  );
           77  +  INSERT INTO t3 VALUES(null, 5, 'row1', '5.25', 'xyz', 321, '432');
           78  +  SELECT a, typeof(a), b, typeof(b), c, typeof(c), 
           79  +         d, typeof(d), e, typeof(e), f, typeof(f),
           80  +         g, typeof(g) FROM t3;
           81  +} {1 integer 5 integer row1 text 5.25 real xyz text 321 text 432 integer}
           82  +do_execsql_test default-3.2 {
           83  +  DELETE FROM t3;
           84  +  INSERT INTO t3 DEFAULT VALUES;
           85  +  SELECT * FROM t3;
           86  +} {2 12345 hello {} 4.36 {} 43200}
           87  +do_execsql_test default-3.3 {
           88  +  CREATE TABLE t300(
           89  +    a INT DEFAULT 2147483647,
           90  +    b INT DEFAULT 2147483648,
           91  +    c INT DEFAULT +9223372036854775807,
           92  +    d INT DEFAULT -2147483647,
           93  +    e INT DEFAULT -2147483648,
           94  +    f INT DEFAULT -9223372036854775808,
           95  +    g INT DEFAULT (-(-9223372036854775808)),
           96  +    h INT DEFAULT (-(-9223372036854775807))
           97  +  );
           98  +  INSERT INTO t300 DEFAULT VALUES;
           99  +  SELECT * FROM t300;
          100  +} {2147483647 2147483648 9223372036854775807 -2147483647 -2147483648 -9223372036854775808 9.22337203685478e+18 9223372036854775807}
          101  +
    67    102   finish_test

Changes to test/e_expr.test.

  1077   1077       SELECT 'abc' REGEXP 'def'
  1078   1078     } {1 {no such function: REGEXP}}
  1079   1079   }
  1080   1080   
  1081   1081   # EVIDENCE-OF: R-33693-50180 The REGEXP operator is a special syntax for
  1082   1082   # the regexp() user function.
  1083   1083   #
  1084         -# EVIDENCE-OF: R-57289-13578 If a application-defined SQL function named
  1085         -# "regexp" is added at run-time, that function will be called in order
  1086         -# to implement the REGEXP operator.
         1084  +# EVIDENCE-OF: R-65524-61849 If an application-defined SQL function
         1085  +# named "regexp" is added at run-time, then the "X REGEXP Y" operator
         1086  +# will be implemented as a call to "regexp(Y,X)".
  1087   1087   #
  1088   1088   proc regexpfunc {args} {
  1089   1089     eval lappend ::regexpargs $args
  1090   1090     return 1
  1091   1091   }
  1092   1092   db func regexp -argcount 2 regexpfunc
  1093   1093   set ::regexpargs [list]
................................................................................
  1602   1602   do_expr_test e_expr-31.1.3 { CAST(-1.99999 AS INTEGER) } integer -1
  1603   1603   do_expr_test e_expr-31.1.4 { CAST(-0.99999 AS INTEGER) } integer 0
  1604   1604   
  1605   1605   # EVIDENCE-OF: R-49503-28105 If a REAL is too large to be represented as
  1606   1606   # an INTEGER then the result of the cast is the largest negative
  1607   1607   # integer: -9223372036854775808.
  1608   1608   #
  1609         -do_expr_test e_expr-31.2.1 { CAST(2e+50 AS INT) } integer -9223372036854775808
         1609  +do_expr_test e_expr-31.2.1 { CAST(2e+50 AS INT) } integer 9223372036854775807
  1610   1610   do_expr_test e_expr-31.2.2 { CAST(-2e+50 AS INT) } integer -9223372036854775808
  1611   1611   do_expr_test e_expr-31.2.3 { 
  1612   1612     CAST(-9223372036854775809.0 AS INT)
  1613   1613   } integer -9223372036854775808
  1614   1614   do_expr_test e_expr-31.2.4 { 
  1615   1615     CAST(9223372036854775809.0 AS INT)
  1616         -} integer -9223372036854775808
         1616  +} integer 9223372036854775807
  1617   1617   
  1618   1618   
  1619   1619   # EVIDENCE-OF: R-09295-61337 Casting a TEXT or BLOB value into NUMERIC
  1620   1620   # first does a forced conversion into REAL but then further converts the
  1621   1621   # result into INTEGER if and only if the conversion from REAL to INTEGER
  1622   1622   # is lossless and reversible.
  1623   1623   #

Changes to test/func.test.

  1314   1314     db eval {SELECT sum(length(x)) FROM t29}
  1315   1315   } {1000009}
  1316   1316   do_test func-29.6 {
  1317   1317     set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
  1318   1318     if {$x<5} {set x 1}
  1319   1319     set x
  1320   1320   } {1}
         1321  +
         1322  +# The OP_Column opcode has an optimization that avoids loading content
         1323  +# for fields with content-length=0 when the content offset is on an overflow
         1324  +# page.  Make sure the optimization works.
         1325  +#
         1326  +do_execsql_test func-29.10 {
         1327  +  CREATE TABLE t29b(a,b,c,d,e,f,g,h,i);
         1328  +  INSERT INTO t29b 
         1329  +   VALUES(1, hex(randomblob(2000)), null, 0, 1, '', zeroblob(0),'x',x'01');
         1330  +  SELECT typeof(c), typeof(d), typeof(e), typeof(f),
         1331  +         typeof(g), typeof(h), typeof(i) FROM t29b;
         1332  +} {null integer integer text blob text blob}
         1333  +do_execsql_test func-29.11 {
         1334  +  SELECT length(f), length(g), length(h), length(i) FROM t29b;
         1335  +} {0 0 1 1}
         1336  +do_execsql_test func-29.12 {
         1337  +  SELECT quote(f), quote(g), quote(h), quote(i) FROM t29b;
         1338  +} {'' X'' 'x' X'01'}
  1321   1339   
  1322   1340   # EVIDENCE-OF: R-29701-50711 The unicode(X) function returns the numeric
  1323   1341   # unicode code point corresponding to the first character of the string
  1324   1342   # X.
  1325   1343   #
  1326   1344   # EVIDENCE-OF: R-55469-62130 The char(X1,X2,...,XN) function returns a
  1327   1345   # string composed of characters having the unicode code point values of

Added test/func5.test.

            1  +# 2013-11-21
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#*************************************************************************
           11  +#
           12  +# Verify that constant string expressions that get factored into initializing
           13  +# code are not reused between function parameters and other values in the
           14  +# VDBE program, as the function might have changed the encoding.
           15  +#
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +
           19  +do_execsql_test func5-1.1 {
           20  +  PRAGMA encoding=UTF16le;
           21  +  CREATE TABLE t1(x,a,b,c);
           22  +  INSERT INTO t1 VALUES(1,'ab','cd',1);
           23  +  INSERT INTO t1 VALUES(2,'gh','ef',5);
           24  +  INSERT INTO t1 VALUES(3,'pqr','fuzzy',99);
           25  +  INSERT INTO t1 VALUES(4,'abcdefg','xy',22);
           26  +  INSERT INTO t1 VALUES(5,'shoe','mayer',2953);
           27  +  SELECT x FROM t1 WHERE c=instr('abcdefg',b) OR a='abcdefg' ORDER BY +x;
           28  +} {2 4}
           29  +do_execsql_test func5-1.2 {
           30  +  SELECT x FROM t1 WHERE a='abcdefg' OR c=instr('abcdefg',b) ORDER BY +x;
           31  +} {2 4}
           32  +
           33  +finish_test

Changes to test/misc7.test.

   351    351       BEGIN;
   352    352         PRAGMA cache_size = 10;
   353    353         INSERT INTO t3 VALUES( randstr(100, 100), randstr(100, 100) );
   354    354         UPDATE t3 SET a = b;
   355    355       COMMIT;
   356    356     }} msg]
   357    357   
   358         -  if {!$rc || ($rc && [string first "columns" $msg]==0)} {
          358  +  if {!$rc || ($rc && [string first "UNIQUE" $msg]==0)} {
   359    359       set msg
   360    360     } else {
   361    361       error $msg
   362    362     }
   363    363   }
   364    364   
   365    365   sqlite3 db test.db

Added test/run-wordcount.sh.

            1  +#!/bin/sh
            2  +#
            3  +# This script runs the wordcount program in different ways, comparing
            4  +# the output from each.
            5  +#
            6  +
            7  +# Select the source text to be analyzed.
            8  +#
            9  +if test "x$1" = "x";
           10  +then echo "Usage: $0 FILENAME [ARGS...]"; exit 1;
           11  +fi
           12  +
           13  +# Do test runs
           14  +#
           15  +rm -f wcdb1.db
           16  +./wordcount --timer --summary wcdb1.db $* --insert >wc-out.txt
           17  +mv wc-out.txt wc-baseline.txt
           18  +rm -f wcdb2.db
           19  +./wordcount --timer --summary wcdb2.db $* --insert --without-rowid >wc-out.txt
           20  +  if cmp -s wc-out.txt wc-baseline.txt;
           21  +  then echo hi >/dev/null;
           22  +  else echo ERROR:;
           23  +       diff -u wc-baseline.txt wc-out.txt;
           24  +  fi
           25  +
           26  +rm -f wcdb1.db
           27  +./wordcount --timer --summary wcdb1.db $* --replace >wc-out.txt
           28  +  if cmp -s wc-out.txt wc-baseline.txt;
           29  +  then echo hi >/dev/null;
           30  +  else echo ERROR:;
           31  +       diff -u wc-baseline.txt wc-out.txt;
           32  +  fi
           33  +rm -f wcdb2.db
           34  +./wordcount --timer --summary wcdb2.db $* --replace --without-rowid >wc-out.txt
           35  +  if cmp -s wc-out.txt wc-baseline.txt;
           36  +  then echo hi >/dev/null;
           37  +  else echo ERROR:;
           38  +       diff -u wc-baseline.txt wc-out.txt;
           39  +  fi
           40  +
           41  +rm -f wcdb1.db
           42  +./wordcount --timer --summary wcdb1.db $* --select >wc-out.txt
           43  +  if cmp -s wc-out.txt wc-baseline.txt;
           44  +  then echo hi >/dev/null;
           45  +  else echo ERROR:;
           46  +       diff -u wc-baseline.txt wc-out.txt;
           47  +  fi
           48  +
           49  +rm -f wcdb2.db
           50  +./wordcount --timer --summary wcdb2.db $* --select --without-rowid >wc-out.txt
           51  +  if cmp -s wc-out.txt wc-baseline.txt;
           52  +  then echo hi >/dev/null;
           53  +  else echo ERROR:;
           54  +       diff -u wc-baseline.txt wc-out.txt;
           55  +  fi
           56  +
           57  +./wordcount --timer --summary wcdb1.db $* --query >wc-out.txt
           58  +mv wc-out.txt wc-baseline.txt
           59  +./wordcount --timer --summary wcdb2.db $* --query --without-rowid >wc-out.txt
           60  +  if cmp -s wc-out.txt wc-baseline.txt;
           61  +  then echo hi >/dev/null;
           62  +  else echo ERROR:;
           63  +       diff -u wc-baseline.txt wc-out.txt;
           64  +  fi
           65  +
           66  +./wordcount --timer --summary wcdb1.db $* --delete >wc-out.txt
           67  +mv wc-out.txt wc-baseline.txt
           68  +./wordcount --timer --summary wcdb2.db $* --delete --without-rowid >wc-out.txt
           69  +  if cmp -s wc-out.txt wc-baseline.txt;
           70  +  then echo hi >/dev/null;
           71  +  else echo ERROR:;
           72  +       diff -u wc-baseline.txt wc-out.txt;
           73  +  fi
           74  +
           75  +
           76  +# Clean up temporary files created.
           77  +#
           78  +rm -rf wcdb1.db wcdb2.db wc-out.txt wc-baseline.txt

Added test/speedtest1.c.

            1  +/*
            2  +** A program for performance testing.
            3  +**
            4  +** The available command-line options are described below:
            5  +*/
            6  +static const char zHelp[] =
            7  +  "Usage: %s [--options] DATABASE\n"
            8  +  "Options:\n"
            9  +  "  --autovacuum        Enable AUTOVACUUM mode\n"
           10  +  "  --cachesize N       Set the cache size to N\n" 
           11  +  "  --exclusive         Enable locking_mode=EXCLUSIVE\n"
           12  +  "  --heap SZ MIN       Memory allocator uses SZ bytes & min allocation MIN\n"
           13  +  "  --incrvacuum        Enable incremenatal vacuum mode\n"
           14  +  "  --journalmode M     Set the journal_mode to MODE\n"
           15  +  "  --key KEY           Set the encryption key to KEY\n"
           16  +  "  --lookaside N SZ    Configure lookaside for N slots of SZ bytes each\n"
           17  +  "  --nosync            Set PRAGMA synchronous=OFF\n"
           18  +  "  --notnull           Add NOT NULL constraints to table columns\n"
           19  +  "  --pagesize N        Set the page size to N\n"
           20  +  "  --pcache N SZ       Configure N pages of pagecache each of size SZ bytes\n"
           21  +  "  --primarykey        Use PRIMARY KEY instead of UNIQUE where appropriate\n"
           22  +  "  --reprepare         Reprepare each statement upon every invocation\n"
           23  +  "  --scratch N SZ      Configure scratch memory for N slots of SZ bytes each\n"
           24  +  "  --sqlonly           No-op.  Only show the SQL that would have been run.\n"
           25  +  "  --size N            Relative test size.  Default=100\n"
           26  +  "  --stats             Show statistics at the end\n"
           27  +  "  --testset T         Run test-set T\n"
           28  +  "  --trace             Turn on SQL tracing\n"
           29  +  "  --utf16be           Set text encoding to UTF-16BE\n"
           30  +  "  --utf16le           Set text encoding to UTF-16LE\n"
           31  +  "  --without-rowid     Use WITHOUT ROWID where appropriate\n"
           32  +;
           33  +
           34  +
           35  +#include "sqlite3.h"
           36  +#include <assert.h>
           37  +#include <stdio.h>
           38  +#include <stdlib.h>
           39  +#include <stdarg.h>
           40  +#include <string.h>
           41  +#include <ctype.h>
           42  +
           43  +/* All global state is held in this structure */
           44  +static struct Global {
           45  +  sqlite3 *db;               /* The open database connection */
           46  +  sqlite3_stmt *pStmt;       /* Current SQL statement */
           47  +  sqlite3_int64 iStart;      /* Start-time for the current test */
           48  +  sqlite3_int64 iTotal;      /* Total time */
           49  +  int bWithoutRowid;         /* True for --without-rowid */
           50  +  int bReprepare;            /* True to reprepare the SQL on each rerun */
           51  +  int bSqlOnly;              /* True to print the SQL once only */
           52  +  int szTest;                /* Scale factor for test iterations */
           53  +  const char *zWR;           /* Might be WITHOUT ROWID */
           54  +  const char *zNN;           /* Might be NOT NULL */
           55  +  const char *zPK;           /* Might be UNIQUE or PRIMARY KEY */
           56  +  unsigned int x, y;         /* Pseudo-random number generator state */
           57  +  int nResult;               /* Size of the current result */
           58  +  char zResult[3000];        /* Text of the current result */
           59  +} g;
           60  +
           61  +
           62  +/* Print an error message and exit */
           63  +static void fatal_error(const char *zMsg, ...){
           64  +  va_list ap;
           65  +  va_start(ap, zMsg);
           66  +  vfprintf(stderr, zMsg, ap);
           67  +  va_end(ap);
           68  +  exit(1);
           69  +}
           70  +
           71  +/*
           72  +** Return the value of a hexadecimal digit.  Return -1 if the input
           73  +** is not a hex digit.
           74  +*/
           75  +static int hexDigitValue(char c){
           76  +  if( c>='0' && c<='9' ) return c - '0';
           77  +  if( c>='a' && c<='f' ) return c - 'a' + 10;
           78  +  if( c>='A' && c<='F' ) return c - 'A' + 10;
           79  +  return -1;
           80  +}
           81  +
           82  +/*
           83  +** Interpret zArg as an integer value, possibly with suffixes.
           84  +*/
           85  +static int integerValue(const char *zArg){
           86  +  sqlite3_int64 v = 0;
           87  +  static const struct { char *zSuffix; int iMult; } aMult[] = {
           88  +    { "KiB", 1024 },
           89  +    { "MiB", 1024*1024 },
           90  +    { "GiB", 1024*1024*1024 },
           91  +    { "KB",  1000 },
           92  +    { "MB",  1000000 },
           93  +    { "GB",  1000000000 },
           94  +    { "K",   1000 },
           95  +    { "M",   1000000 },
           96  +    { "G",   1000000000 },
           97  +  };
           98  +  int i;
           99  +  int isNeg = 0;
          100  +  if( zArg[0]=='-' ){
          101  +    isNeg = 1;
          102  +    zArg++;
          103  +  }else if( zArg[0]=='+' ){
          104  +    zArg++;
          105  +  }
          106  +  if( zArg[0]=='0' && zArg[1]=='x' ){
          107  +    int x;
          108  +    zArg += 2;
          109  +    while( (x = hexDigitValue(zArg[0]))>=0 ){
          110  +      v = (v<<4) + x;
          111  +      zArg++;
          112  +    }
          113  +  }else{
          114  +    while( isdigit(zArg[0]) ){
          115  +      v = v*10 + zArg[0] - '0';
          116  +      zArg++;
          117  +    }
          118  +  }
          119  +  for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
          120  +    if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
          121  +      v *= aMult[i].iMult;
          122  +      break;
          123  +    }
          124  +  }
          125  +  if( v>=2147483648 ) fatal_error("parameter to large - max 2147483648");
          126  +  return isNeg? -v : v;
          127  +}
          128  +
          129  +/* Return the current wall-clock time, in milliseconds */
          130  +sqlite3_int64 speedtest1_timestamp(void){
          131  +  static sqlite3_vfs *clockVfs = 0;
          132  +  sqlite3_int64 t;
          133  +  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
          134  +  if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
          135  +    clockVfs->xCurrentTimeInt64(clockVfs, &t);
          136  +  }else{
          137  +    double r;
          138  +    clockVfs->xCurrentTime(clockVfs, &r);
          139  +    t = (sqlite3_int64)(r*86400000.0);
          140  +  }
          141  +  return t;
          142  +}
          143  +
          144  +/* Return a pseudo-random unsigned integer */
          145  +unsigned int speedtest1_random(void){
          146  +  g.x = (g.x>>1) ^ ((1+~(g.x&1)) & 0xd0000001);
          147  +  g.y = g.y*1103515245 + 12345;
          148  +  return g.x ^ g.y;
          149  +}
          150  +
          151  +/* Map the value in within the range of 1...limit into another
          152  +** number in a way that is chatic and invertable.
          153  +*/
          154  +unsigned swizzle(unsigned in, unsigned limit){
          155  +  unsigned out = 0;
          156  +  while( limit ){
          157  +    out = (out<<1) | (in&1);
          158  +    in >>= 1;
          159  +    limit >>= 1;
          160  +  }
          161  +  return out;
          162  +}
          163  +
          164  +/* Round up a number so that it is a power of two minus one
          165  +*/
          166  +unsigned roundup_allones(unsigned limit){
          167  +  unsigned m = 1;
          168  +  while( m<limit ) m = (m<<1)+1;
          169  +  return m;
          170  +}
          171  +
          172  +/* The speedtest1_numbername procedure below converts its argment (an integer)
          173  +** into a string which is the English-language name for that number.
          174  +** The returned string should be freed with sqlite3_free().
          175  +**
          176  +** Example:
          177  +**
          178  +**     speedtest1_numbername(123)   ->  "one hundred twenty three"
          179  +*/
          180  +int speedtest1_numbername(unsigned int n, char *zOut, int nOut){
          181  +  static const char *ones[] = {  "zero", "one", "two", "three", "four", "five", 
          182  +                  "six", "seven", "eight", "nine", "ten", "eleven", "twelve", 
          183  +                  "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
          184  +                  "eighteen", "nineteen" };
          185  +  static const char *tens[] = { "", "ten", "twenty", "thirty", "forty",
          186  +                 "fifty", "sixty", "seventy", "eighty", "ninety" };
          187  +  int i = 0;
          188  +
          189  +  if( n>=1000000000 ){
          190  +    i += speedtest1_numbername(n/1000000000, zOut+i, nOut-i);
          191  +    sqlite3_snprintf(nOut-i, zOut+i, " billion");
          192  +    i += (int)strlen(zOut+i);
          193  +    n = n % 1000000000;
          194  +  }
          195  +  if( n>=1000000 ){
          196  +    if( i && i<nOut-1 ) zOut[i++] = ' ';
          197  +    i += speedtest1_numbername(n/1000000, zOut+i, nOut-i);
          198  +    sqlite3_snprintf(nOut-i, zOut+i, " million");
          199  +    i += (int)strlen(zOut+i);
          200  +    n = n % 1000000;
          201  +  }
          202  +  if( n>=1000 ){
          203  +    if( i && i<nOut-1 ) zOut[i++] = ' ';
          204  +    i += speedtest1_numbername(n/1000, zOut+i, nOut-i);
          205  +    sqlite3_snprintf(nOut-i, zOut+i, " thousand");
          206  +    i += (int)strlen(zOut+i);
          207  +    n = n % 1000;
          208  +  }
          209  +  if( n>=100 ){
          210  +    if( i && i<nOut-1 ) zOut[i++] = ' ';
          211  +    sqlite3_snprintf(nOut-i, zOut+i, "%s hundred", ones[n/100]);
          212  +    i += (int)strlen(zOut+i);
          213  +    n = n % 100;
          214  +  }
          215  +  if( n>=20 ){
          216  +    if( i && i<nOut-1 ) zOut[i++] = ' ';
          217  +    sqlite3_snprintf(nOut-i, zOut+i, "%s", tens[n/10]);
          218  +    i += (int)strlen(zOut+i);
          219  +    n = n % 10;
          220  +  }
          221  +  if( n>0 ){
          222  +    if( i && i<nOut-1 ) zOut[i++] = ' ';
          223  +    sqlite3_snprintf(nOut-i, zOut+i, "%s", ones[n]);
          224  +    i += (int)strlen(zOut+i);
          225  +  }
          226  +  if( i==0 ){
          227  +    sqlite3_snprintf(nOut-i, zOut+i, "zero");
          228  +    i += (int)strlen(zOut+i);
          229  +  }
          230  +  return i;
          231  +}
          232  +
          233  +
          234  +/* Start a new test case */
          235  +#define NAMEWIDTH 60
          236  +static const char zDots[] =
          237  +  ".......................................................................";
          238  +void speedtest1_begin_test(int iTestNum, const char *zTestName, ...){
          239  +  int n = (int)strlen(zTestName);
          240  +  char *zName;
          241  +  va_list ap;
          242  +  va_start(ap, zTestName);
          243  +  zName = sqlite3_vmprintf(zTestName, ap);
          244  +  va_end(ap);
          245  +  n = (int)strlen(zName);
          246  +  if( n>NAMEWIDTH ){
          247  +    zName[NAMEWIDTH] = 0;
          248  +    n = NAMEWIDTH;
          249  +  }
          250  +  if( g.bSqlOnly ){
          251  +    printf("/* %4d - %s%.*s */\n", iTestNum, zName, NAMEWIDTH-n, zDots);
          252  +  }else{
          253  +    printf("%4d - %s%.*s ", iTestNum, zName, NAMEWIDTH-n, zDots);
          254  +    fflush(stdout);
          255  +  }
          256  +  sqlite3_free(zName);
          257  +  g.nResult = 0;
          258  +  g.iStart = speedtest1_timestamp();
          259  +  g.x = 2903710987;
          260  +  g.y = 1157229256;
          261  +}
          262  +
          263  +/* Complete a test case */
          264  +void speedtest1_end_test(void){
          265  +  sqlite3_int64 iElapseTime = speedtest1_timestamp() - g.iStart;
          266  +  if( !g.bSqlOnly ){
          267  +    g.iTotal += iElapseTime;
          268  +    printf("%4d.%03ds\n", (int)(iElapseTime/1000), (int)(iElapseTime%1000));
          269  +  }
          270  +  if( g.pStmt ){
          271  +    sqlite3_finalize(g.pStmt);
          272  +    g.pStmt = 0;
          273  +  }
          274  +}
          275  +
          276  +/* Report end of testing */
          277  +void speedtest1_final(void){
          278  +  if( !g.bSqlOnly ){
          279  +    printf("       TOTAL%.*s %4d.%03ds\n", NAMEWIDTH-5, zDots,
          280  +           (int)(g.iTotal/1000), (int)(g.iTotal%1000));
          281  +  }
          282  +}
          283  +
          284  +/* Run SQL */
          285  +void speedtest1_exec(const char *zFormat, ...){
          286  +  va_list ap;
          287  +  char *zSql;
          288  +  va_start(ap, zFormat);
          289  +  zSql = sqlite3_vmprintf(zFormat, ap);
          290  +  va_end(ap);
          291  +  if( g.bSqlOnly ){
          292  +    int n = (int)strlen(zSql);
          293  +    while( n>0 && (zSql[n-1]==';' || isspace(zSql[n-1])) ){ n--; }
          294  +    printf("%.*s;\n", n, zSql);
          295  +  }else{
          296  +    char *zErrMsg = 0;
          297  +    int rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
          298  +    if( zErrMsg ) fatal_error("SQL error: %s\n%s\n", zErrMsg, zSql);
          299  +    if( rc!=SQLITE_OK ) fatal_error("exec error: %s\n", sqlite3_errmsg(g.db));
          300  +  }
          301  +  sqlite3_free(zSql);
          302  +}
          303  +
          304  +/* Prepare an SQL statement */
          305  +void speedtest1_prepare(const char *zFormat, ...){
          306  +  va_list ap;
          307  +  char *zSql;
          308  +  va_start(ap, zFormat);
          309  +  zSql = sqlite3_vmprintf(zFormat, ap);
          310  +  va_end(ap);
          311  +  if( g.bSqlOnly ){
          312  +    int n = (int)strlen(zSql);
          313  +    while( n>0 && (zSql[n-1]==';' || isspace(zSql[n-1])) ){ n--; }
          314  +    printf("%.*s;\n", n, zSql);
          315  +  }else{
          316  +    int rc;
          317  +    if( g.pStmt ) sqlite3_finalize(g.pStmt);
          318  +    rc = sqlite3_prepare_v2(g.db, zSql, -1, &g.pStmt, 0);
          319  +    if( rc ){
          320  +      fatal_error("SQL error: %s\n", sqlite3_errmsg(g.db));
          321  +    }
          322  +  }
          323  +  sqlite3_free(zSql);
          324  +}
          325  +
          326  +/* Run an SQL statement previously prepared */
          327  +void speedtest1_run(void){
          328  +  int i, n, len;
          329  +  if( g.bSqlOnly ) return;
          330  +  assert( g.pStmt );
          331  +  g.nResult = 0;
          332  +  while( sqlite3_step(g.pStmt)==SQLITE_ROW ){
          333  +    n = sqlite3_column_count(g.pStmt);
          334  +    for(i=0; i<n; i++){
          335  +      const char *z = (const char*)sqlite3_column_text(g.pStmt, i);
          336  +      if( z==0 ) z = "nil";
          337  +      len = (int)strlen(z);
          338  +      if( g.nResult+len<sizeof(g.zResult)-2 ){
          339  +        if( g.nResult>0 ) g.zResult[g.nResult++] = ' ';
          340  +        memcpy(g.zResult + g.nResult, z, len+1);
          341  +        g.nResult += len;
          342  +      }
          343  +    }
          344  +  }
          345  +  if( g.bReprepare ){
          346  +    sqlite3_stmt *pNew;
          347  +    sqlite3_prepare_v2(g.db, sqlite3_sql(g.pStmt), -1, &pNew, 0);
          348  +    sqlite3_finalize(g.pStmt);
          349  +    g.pStmt = pNew;
          350  +  }else{
          351  +    sqlite3_reset(g.pStmt);
          352  +  }
          353  +}
          354  +
          355  +/* The sqlite3_trace() callback function */
          356  +static void traceCallback(void *NotUsed, const char *zSql){
          357  +  int n = (int)strlen(zSql);
          358  +  while( n>0 && (zSql[n-1]==';' || isspace(zSql[n-1])) ) n--;
          359  +  fprintf(stderr,"%.*s;\n", n, zSql);
          360  +}
          361  +
          362  +/* Substitute random() function that gives the same random
          363  +** sequence on each run, for repeatability. */
          364  +static void randomFunc(
          365  +  sqlite3_context *context,
          366  +  int NotUsed,
          367  +  sqlite3_value **NotUsed2
          368  +){
          369  +  sqlite3_result_int64(context, (sqlite3_int64)speedtest1_random());
          370  +}
          371  +
          372  +/*
          373  +** The main and default testset
          374  +*/
          375  +void testset_main(void){
          376  +  int i;                        /* Loop counter */
          377  +  int n;                        /* iteration count */
          378  +  int sz;                       /* Size of the tables */
          379  +  int maxb;                     /* Maximum swizzled value */
          380  +  unsigned x1, x2;              /* Parameters */
          381  +  int len;                      /* Length of the zNum[] string */
          382  +  char zNum[2000];              /* A number name */
          383  +
          384  +  sz = n = g.szTest*500;
          385  +  maxb = roundup_allones(sz);
          386  +  speedtest1_begin_test(100, "%d INSERTs into table with no index", n);
          387  +  speedtest1_exec("BEGIN");
          388  +  speedtest1_exec("CREATE TABLE t1(a INTEGER %s, b INTEGER %s, c TEXT %s);",
          389  +                  g.zNN, g.zNN, g.zNN);
          390  +  speedtest1_prepare("INSERT INTO t1 VALUES(?1,?2,?3); --  %d times", n);
          391  +  for(i=1; i<=n; i++){
          392  +    x1 = swizzle(i,maxb);
          393  +    speedtest1_numbername(x1, zNum, sizeof(zNum));
          394  +    sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1);
          395  +    sqlite3_bind_int(g.pStmt, 2, i);
          396  +    sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
          397  +    speedtest1_run();
          398  +  }
          399  +  speedtest1_exec("COMMIT");
          400  +  speedtest1_end_test();
          401  +
          402  +
          403  +  n = sz;
          404  +  speedtest1_begin_test(110, "%d ordered INSERTS with one index/PK", n);
          405  +  speedtest1_exec("BEGIN");
          406  +  speedtest1_exec("CREATE TABLE t2(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s",
          407  +                   g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
          408  +  speedtest1_prepare("INSERT INTO t2 VALUES(?1,?2,?3); -- %d times", n);
          409  +  for(i=1; i<=n; i++){
          410  +    x1 = swizzle(i,maxb);
          411  +    speedtest1_numbername(x1, zNum, sizeof(zNum));
          412  +    sqlite3_bind_int(g.pStmt, 1, i);
          413  +    sqlite3_bind_int64(g.pStmt, 2, (sqlite3_int64)x1);
          414  +    sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
          415  +    speedtest1_run();
          416  +  }
          417  +  speedtest1_exec("COMMIT");
          418  +  speedtest1_end_test();
          419  +
          420  +
          421  +  n = sz;
          422  +  speedtest1_begin_test(120, "%d unordered INSERTS with one index/PK", n);
          423  +  speedtest1_exec("BEGIN");
          424  +  speedtest1_exec("CREATE TABLE t3(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s",
          425  +                   g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
          426  +  speedtest1_prepare("INSERT INTO t3 VALUES(?1,?2,?3); -- %d times", n);
          427  +  for(i=1; i<=n; i++){
          428  +    x1 = swizzle(i,maxb);
          429  +    speedtest1_numbername(x1, zNum, sizeof(zNum));
          430  +    sqlite3_bind_int(g.pStmt, 2, i);
          431  +    sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1);
          432  +    sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
          433  +    speedtest1_run();
          434  +  }
          435  +  speedtest1_exec("COMMIT");
          436  +  speedtest1_end_test();
          437  +
          438  +
          439  +  n = g.szTest/2;
          440  +  speedtest1_begin_test(130, "%d SELECTS, numeric BETWEEN, unindexed", n);
          441  +  speedtest1_exec("BEGIN");
          442  +  speedtest1_prepare(
          443  +    "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
          444  +    " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
          445  +  );
          446  +  for(i=1; i<=n; i++){
          447  +    x1 = speedtest1_random()%maxb;
          448  +    x2 = speedtest1_random()%10 + sz/5000 + x1;
          449  +    sqlite3_bind_int(g.pStmt, 1, x1);
          450  +    sqlite3_bind_int(g.pStmt, 2, x2);
          451  +    speedtest1_run();
          452  +  }
          453  +  speedtest1_exec("COMMIT");
          454  +  speedtest1_end_test();
          455  +
          456  +
          457  +  n = g.szTest/5;
          458  +  speedtest1_begin_test(140, "%d SELECTS, LIKE, unindexed", n);
          459  +  speedtest1_exec("BEGIN");
          460  +  speedtest1_prepare(
          461  +    "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
          462  +    " WHERE c LIKE ?1; -- %d times", n
          463  +  );
          464  +  for(i=1; i<=n; i++){
          465  +    x1 = speedtest1_random()%maxb;
          466  +    zNum[0] = '%';
          467  +    len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2);
          468  +    zNum[len] = '%';
          469  +    zNum[len+1] = 0;
          470  +    sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC);
          471  +    speedtest1_run();
          472  +  }
          473  +  speedtest1_exec("COMMIT");
          474  +  speedtest1_end_test();
          475  +
          476  +
          477  +  speedtest1_begin_test(150, "CREATE INDEX five times");
          478  +  speedtest1_exec(
          479  +      "BEGIN;\n"
          480  +      "CREATE UNIQUE INDEX t1b ON t1(b);\n"
          481  +      "CREATE INDEX t1c ON t1(c);\n"
          482  +      "CREATE UNIQUE INDEX t2b ON t2(b);\n"
          483  +      "CREATE INDEX t2c ON t2(c DESC);\n"
          484  +      "CREATE INDEX t3bc ON t3(b,c);\n"
          485  +      "COMMIT;\n"
          486  +  );
          487  +  speedtest1_end_test();
          488  +
          489  +
          490  +  n = sz/5;
          491  +  speedtest1_begin_test(160, "%d SELECTS, numeric BETWEEN, indexed", n);
          492  +  speedtest1_exec("BEGIN");
          493  +  speedtest1_prepare(
          494  +    "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
          495  +    " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
          496  +  );
          497  +  for(i=1; i<=n; i++){
          498  +    x1 = speedtest1_random()%maxb;
          499  +    x2 = speedtest1_random()%10 + sz/5000 + x1;
          500  +    sqlite3_bind_int(g.pStmt, 1, x1);
          501  +    sqlite3_bind_int(g.pStmt, 2, x2);
          502  +    speedtest1_run();
          503  +  }
          504  +  speedtest1_exec("COMMIT");
          505  +  speedtest1_end_test();
          506  +
          507  +
          508  +  n = sz/5;
          509  +  speedtest1_begin_test(161, "%d SELECTS, numeric BETWEEN, PK", n);
          510  +  speedtest1_exec("BEGIN");
          511  +  speedtest1_prepare(
          512  +    "SELECT count(*), avg(b), sum(length(c)) FROM t2\n"
          513  +    " WHERE a BETWEEN ?1 AND ?2; -- %d times", n
          514  +  );
          515  +  for(i=1; i<=n; i++){
          516  +    x1 = speedtest1_random()%maxb;
          517  +    x2 = speedtest1_random()%10 + sz/5000 + x1;
          518  +    sqlite3_bind_int(g.pStmt, 1, x1);
          519  +    sqlite3_bind_int(g.pStmt, 2, x2);
          520  +    speedtest1_run();
          521  +  }
          522  +  speedtest1_exec("COMMIT");
          523  +  speedtest1_end_test();
          524  +
          525  +
          526  +  n = sz/5;
          527  +  speedtest1_begin_test(170, "%d SELECTS, text BETWEEN, indexed", n);
          528  +  speedtest1_exec("BEGIN");
          529  +  speedtest1_prepare(
          530  +    "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
          531  +    " WHERE c BETWEEN ?1 AND (?1||'~'); -- %d times", n
          532  +  );
          533  +  for(i=1; i<=n; i++){
          534  +    x1 = swizzle(i, maxb);
          535  +    len = speedtest1_numbername(x1, zNum, sizeof(zNum)-1);
          536  +    sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC);
          537  +    speedtest1_run();
          538  +  }
          539  +  speedtest1_exec("COMMIT");
          540  +  speedtest1_end_test();
          541  +
          542  +  n = sz;
          543  +  speedtest1_begin_test(180, "%d INSERTS with three indexes", n);
          544  +  speedtest1_exec("BEGIN");
          545  +  speedtest1_exec(
          546  +    "CREATE TABLE t4(\n"
          547  +    "  a INTEGER %s %s,\n"
          548  +    "  b INTEGER %s,\n"
          549  +    "  c TEXT %s\n"
          550  +    ") %s",
          551  +    g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
          552  +  speedtest1_exec("CREATE INDEX t4b ON t4(b)");
          553  +  speedtest1_exec("CREATE INDEX t4c ON t4(c)");
          554  +  speedtest1_exec("INSERT INTO t4 SELECT * FROM t1");
          555  +  speedtest1_exec("COMMIT");
          556  +  speedtest1_end_test();
          557  +
          558  +  n = sz;
          559  +  speedtest1_begin_test(190, "DELETE and REFILL one table", n);
          560  +  speedtest1_exec(
          561  +    "DELETE FROM t2;"
          562  +    "INSERT INTO t2 SELECT * FROM t1;"
          563  +  );
          564  +  speedtest1_end_test();
          565  +
          566  +
          567  +  speedtest1_begin_test(200, "VACUUM");
          568  +  speedtest1_exec("VACUUM");
          569  +  speedtest1_end_test();
          570  +
          571  +
          572  +  speedtest1_begin_test(210, "ALTER TABLE ADD COLUMN, and query");
          573  +  speedtest1_exec("ALTER TABLE t2 ADD COLUMN d DEFAULT 123");
          574  +  speedtest1_exec("SELECT sum(d) FROM t2");
          575  +  speedtest1_end_test();
          576  +
          577  +
          578  +  n = sz/5;
          579  +  speedtest1_begin_test(230, "%d UPDATES, numeric BETWEEN, indexed", n);
          580  +  speedtest1_exec("BEGIN");
          581  +  speedtest1_prepare(
          582  +    "UPDATE t2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
          583  +  );
          584  +  for(i=1; i<=n; i++){
          585  +    x1 = speedtest1_random()%maxb;
          586  +    x2 = speedtest1_random()%10 + sz/5000 + x1;
          587  +    sqlite3_bind_int(g.pStmt, 1, x1);
          588  +    sqlite3_bind_int(g.pStmt, 2, x2);
          589  +    speedtest1_run();
          590  +  }
          591  +  speedtest1_exec("COMMIT");
          592  +  speedtest1_end_test();
          593  +
          594  +
          595  +  n = sz;
          596  +  speedtest1_begin_test(240, "%d UPDATES of individual rows", n);
          597  +  speedtest1_exec("BEGIN");
          598  +  speedtest1_prepare(
          599  +    "UPDATE t2 SET d=b*3 WHERE a=?1; -- %d times", n
          600  +  );
          601  +  for(i=1; i<=n; i++){
          602  +    x1 = speedtest1_random()%sz + 1;
          603  +    sqlite3_bind_int(g.pStmt, 1, x1);
          604  +    speedtest1_run();
          605  +  }
          606  +  speedtest1_exec("COMMIT");
          607  +  speedtest1_end_test();
          608  +
          609  +  speedtest1_begin_test(250, "One big UPDATE of the whole %d-row table", sz);
          610  +  speedtest1_exec("UPDATE t2 SET d=b*4");
          611  +  speedtest1_end_test();
          612  +
          613  +
          614  +  speedtest1_begin_test(260, "Query added column after filling");
          615  +  speedtest1_exec("SELECT sum(d) FROM t2");
          616  +  speedtest1_end_test();
          617  +
          618  +
          619  +
          620  +  n = sz/5;
          621  +  speedtest1_begin_test(270, "%d DELETEs, numeric BETWEEN, indexed", n);
          622  +  speedtest1_exec("BEGIN");
          623  +  speedtest1_prepare(
          624  +    "DELETE FROM t2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
          625  +  );
          626  +  for(i=1; i<=n; i++){
          627  +    x1 = speedtest1_random()%maxb + 1;
          628  +    x2 = speedtest1_random()%10 + sz/5000 + x1;
          629  +    sqlite3_bind_int(g.pStmt, 1, x1);
          630  +    sqlite3_bind_int(g.pStmt, 2, x2);
          631  +    speedtest1_run();
          632  +  }
          633  +  speedtest1_exec("COMMIT");
          634  +  speedtest1_end_test();
          635  +
          636  +
          637  +  n = sz;
          638  +  speedtest1_begin_test(280, "%d DELETEs of individual rows", n);
          639  +  speedtest1_exec("BEGIN");
          640  +  speedtest1_prepare(
          641  +    "DELETE FROM t3 WHERE a=?1; -- %d times", n
          642  +  );
          643  +  for(i=1; i<=n; i++){
          644  +    x1 = speedtest1_random()%sz + 1;
          645  +    sqlite3_bind_int(g.pStmt, 1, x1);
          646  +    speedtest1_run();
          647  +  }
          648  +  speedtest1_exec("COMMIT");
          649  +  speedtest1_end_test();
          650  +
          651  +
          652  +  speedtest1_begin_test(290, "Refill two %d-row tables using REPLACE", sz);
          653  +  speedtest1_exec("REPLACE INTO t2(a,b,c) SELECT a,b,c FROM t1");
          654  +  speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM t1");
          655  +  speedtest1_end_test();
          656  +
          657  +
          658  +  n = sz/5;
          659  +  speedtest1_begin_test(290, "%d four-ways joins", n);
          660  +  speedtest1_exec("BEGIN");
          661  +  speedtest1_prepare(
          662  +    "SELECT t1.c FROM t1, t2, t3, t4\n"
          663  +    " WHERE t4.a BETWEEN ?1 AND ?2\n"
          664  +    "   AND t3.a=t4.b\n"
          665  +    "   AND t2.a=t3.b\n"
          666  +    "   AND t1.c=t2.c"
          667  +  );
          668  +  for(i=1; i<=n; i++){
          669  +    x1 = speedtest1_random()%sz + 1;
          670  +    x2 = speedtest1_random()%10 + x1 + 4;
          671  +    sqlite3_bind_int(g.pStmt, 1, x1);
          672  +    sqlite3_bind_int(g.pStmt, 2, x2);
          673  +    speedtest1_run();
          674  +  }
          675  +  speedtest1_exec("COMMIT");
          676  +  speedtest1_end_test();
          677  +
          678  +
          679  +
          680  +  speedtest1_begin_test(980, "PRAGMA integrity_check");
          681  +  speedtest1_exec("PRAGMA integrity_check");
          682  +  speedtest1_end_test();
          683  +
          684  +
          685  +  speedtest1_begin_test(990, "ANALYZE");
          686  +  speedtest1_exec("ANALYZE");
          687  +  speedtest1_end_test();
          688  +}
          689  +
          690  +/*
          691  +** A testset used for debugging speedtest1 itself.
          692  +*/
          693  +void testset_debug1(void){
          694  +  unsigned i, n;
          695  +  unsigned x1, x2;
          696  +  char zNum[2000];              /* A number name */
          697  +
          698  +  n = g.szTest;
          699  +  for(i=1; i<=n; i++){
          700  +    x1 = swizzle(i, n);
          701  +    x2 = swizzle(x1, n);
          702  +    speedtest1_numbername(x1, zNum, sizeof(zNum));
          703  +    printf("%5d %5d %5d %s\n", i, x1, x2, zNum);
          704  +  }
          705  +}
          706  +
          707  +int main(int argc, char **argv){
          708  +  int doAutovac = 0;            /* True for --autovacuum */
          709  +  int cacheSize = 0;            /* Desired cache size.  0 means default */
          710  +  int doExclusive = 0;          /* True for --exclusive */
          711  +  int nHeap = 0, mnHeap = 0;    /* Heap size from --heap */
          712  +  int doIncrvac = 0;            /* True for --incrvacuum */
          713  +  const char *zJMode = 0;       /* Journal mode */
          714  +  const char *zKey = 0;         /* Encryption key */
          715  +  int nLook = 0, szLook = 0;    /* --lookaside configuration */
          716  +  int noSync = 0;               /* True for --nosync */
          717  +  int pageSize = 0;             /* Desired page size.  0 means default */
          718  +  int nPCache = 0, szPCache = 0;/* --pcache configuration */
          719  +  int nScratch = 0, szScratch=0;/* --scratch configuration */
          720  +  int showStats = 0;            /* True for --stats */
          721  +  const char *zTSet = "main";   /* Which --testset torun */
          722  +  int doTrace = 0;              /* True for --trace */
          723  +  const char *zEncoding = 0;    /* --utf16be or --utf16le */
          724  +  const char *zDbName = 0;      /* Name of the test database */
          725  +
          726  +  void *pHeap = 0;              /* Allocated heap space */
          727  +  void *pLook = 0;              /* Allocated lookaside space */
          728  +  void *pPCache = 0;            /* Allocated storage for pcache */
          729  +  void *pScratch = 0;           /* Allocated storage for scratch */
          730  +  int iCur, iHi;                /* Stats values, current and "highwater" */
          731  +  int i;                        /* Loop counter */
          732  +  int rc;                       /* API return code */
          733  +
          734  +  /* Process command-line arguments */
          735  +  g.zWR = "";
          736  +  g.zNN = "";
          737  +  g.zPK = "UNIQUE";
          738  +  g.szTest = 100;
          739  +  for(i=1; i<argc; i++){
          740  +    const char *z = argv[i];
          741  +    if( z[0]=='-' ){
          742  +      do{ z++; }while( z[0]=='-' );
          743  +      if( strcmp(z,"autovacuum")==0 ){
          744  +        doAutovac = 1;
          745  +      }else if( strcmp(z,"cachesize")==0 ){
          746  +        if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
          747  +        i++;
          748  +        cacheSize = integerValue(argv[i]);
          749  +      }else if( strcmp(z,"exclusive")==0 ){
          750  +        doExclusive = 1;
          751  +      }else if( strcmp(z,"heap")==0 ){
          752  +        if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
          753  +        nHeap = integerValue(argv[i+1]);
          754  +        mnHeap = integerValue(argv[i+2]);
          755  +        i += 2;
          756  +      }else if( strcmp(z,"incrvacuum")==0 ){
          757  +        doIncrvac = 1;
          758  +      }else if( strcmp(z,"journal")==0 ){
          759  +        if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
          760  +        zJMode = argv[++i];
          761  +      }else if( strcmp(z,"key")==0 ){
          762  +        if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
          763  +        zKey = argv[++i];
          764  +      }else if( strcmp(z,"lookaside")==0 ){
          765  +        if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
          766  +        nLook = integerValue(argv[i+1]);
          767  +        szLook = integerValue(argv[i+2]);
          768  +        i += 2;
          769  +      }else if( strcmp(z,"nosync")==0 ){
          770  +        noSync = 1;
          771  +      }else if( strcmp(z,"notnull")==0 ){
          772  +        g.zNN = "NOT NULL";
          773  +      }else if( strcmp(z,"pagesize")==0 ){
          774  +        if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
          775  +        pageSize = integerValue(argv[++i]);
          776  +      }else if( strcmp(z,"pcache")==0 ){
          777  +        if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
          778  +        nPCache = integerValue(argv[i+1]);
          779  +        szPCache = integerValue(argv[i+2]);
          780  +        i += 2;
          781  +      }else if( strcmp(z,"primarykey")==0 ){
          782  +        g.zPK = "PRIMARY KEY";
          783  +      }else if( strcmp(z,"reprepare")==0 ){
          784  +        g.bReprepare = 1;
          785  +      }else if( strcmp(z,"scratch")==0 ){
          786  +        if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
          787  +        nScratch = integerValue(argv[i+1]);
          788  +        szScratch = integerValue(argv[i+2]);
          789  +        i += 2;
          790  +      }else if( strcmp(z,"sqlonly")==0 ){
          791  +        g.bSqlOnly = 1;
          792  +      }else if( strcmp(z,"size")==0 ){
          793  +        if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
          794  +        g.szTest = integerValue(argv[++i]);
          795  +      }else if( strcmp(z,"stats")==0 ){
          796  +        showStats = 1;
          797  +      }else if( strcmp(z,"testset")==0 ){
          798  +        if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
          799  +        zTSet = argv[++i];
          800  +      }else if( strcmp(z,"trace")==0 ){
          801  +        doTrace = 1;
          802  +      }else if( strcmp(z,"utf16le")==0 ){
          803  +        zEncoding = "utf16le";
          804  +      }else if( strcmp(z,"utf16be")==0 ){
          805  +        zEncoding = "utf16be";
          806  +      }else if( strcmp(z,"without-rowid")==0 ){
          807  +        g.zWR = "WITHOUT ROWID";
          808  +        g.zPK = "PRIMARY KEY";
          809  +      }else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){
          810  +        printf(zHelp, argv[0]);
          811  +        exit(0);
          812  +      }else{
          813  +        fatal_error("unknown option: %s\nUse \"%s -?\" for help\n",
          814  +                    argv[i], argv[0]);
          815  +      }
          816  +    }else if( zDbName==0 ){
          817  +      zDbName = argv[i];
          818  +    }else{
          819  +      fatal_error("surplus argument: %s\nUse \"%s -?\" for help\n",
          820  +                  argv[i], argv[0]);
          821  +    }
          822  +  }
          823  +#if 0
          824  +  if( zDbName==0 ){
          825  +    fatal_error(zHelp, argv[0]);
          826  +  }
          827  +#endif
          828  +  if( nHeap>0 ){
          829  +    pHeap = malloc( nHeap );
          830  +    if( pHeap==0 ) fatal_error("cannot allocate %d-byte heap\n", nHeap);
          831  +    rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
          832  +    if( rc ) fatal_error("heap configuration failed: %d\n", rc);
          833  +  }
          834  +  if( nPCache>0 && szPCache>0 ){
          835  +    pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
          836  +    if( pPCache==0 ) fatal_error("cannot allocate %lld-byte pcache\n",
          837  +                                 nPCache*(sqlite3_int64)szPCache);
          838  +    rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
          839  +    if( rc ) fatal_error("pcache configuration failed: %d\n", rc);
          840  +  }
          841  +  if( nScratch>0 && szScratch>0 ){
          842  +    pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
          843  +    if( pScratch==0 ) fatal_error("cannot allocate %lld-byte scratch\n",
          844  +                                 nScratch*(sqlite3_int64)szScratch);
          845  +    rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
          846  +    if( rc ) fatal_error("scratch configuration failed: %d\n", rc);
          847  +  }
          848  +  if( nLook>0 ){
          849  +    sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
          850  +  }
          851  + 
          852  +  /* Open the database and the input file */
          853  +  if( sqlite3_open(zDbName, &g.db) ){
          854  +    fatal_error("Cannot open database file: %s\n", zDbName);
          855  +  }
          856  +  if( nLook>0 && szLook>0 ){
          857  +    pLook = malloc( nLook*szLook );
          858  +    rc = sqlite3_db_config(g.db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook,nLook);
          859  +    if( rc ) fatal_error("lookaside configuration failed: %d\n", rc);
          860  +  }
          861  +
          862  +  /* Set database connection options */
          863  +  sqlite3_create_function(g.db, "random", 0, SQLITE_UTF8, 0, randomFunc, 0, 0);
          864  +  if( doTrace ) sqlite3_trace(g.db, traceCallback, 0);
          865  +  if( zKey ){
          866  +    speedtest1_exec("PRAGMA key('%s')", zKey);
          867  +  }
          868  +  if( zEncoding ){
          869  +    speedtest1_exec("PRAGMA encoding=%s", zEncoding);
          870  +  }
          871  +  if( doAutovac ){
          872  +    speedtest1_exec("PRAGMA auto_vacuum=FULL");
          873  +  }else if( doIncrvac ){
          874  +    speedtest1_exec("PRAGMA auto_vacuum=INCREMENTAL");
          875  +  }
          876  +  if( pageSize ){
          877  +    speedtest1_exec("PRAGMA page_size=%d", pageSize);
          878  +  }
          879  +  if( cacheSize ){
          880  +    speedtest1_exec("PRAGMA cache_size=%d", cacheSize);
          881  +  }
          882  +  if( noSync ) speedtest1_exec("PRAGMA synchronous=OFF");
          883  +  if( doExclusive ){
          884  +    speedtest1_exec("PRAGMA locking_mode=EXCLUSIVE");
          885  +  }
          886  +  if( zJMode ){
          887  +    speedtest1_exec("PRAGMA journal_mode=%s", zJMode);
          888  +  }
          889  +
          890  +  if( strcmp(zTSet,"main")==0 ){
          891  +    testset_main();
          892  +  }else if( strcmp(zTSet,"debug1")==0 ){
          893  +    testset_debug1();
          894  +  }else{
          895  +    fatal_error("unknown testset: \"%s\"\n", zTSet);
          896  +  }
          897  +  speedtest1_final();
          898  +
          899  +  /* Database connection statistics printed after both prepared statements
          900  +  ** have been finalized */
          901  +  if( showStats ){
          902  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHi, 0);
          903  +    printf("-- Lookaside Slots Used:        %d (max %d)\n", iCur,iHi);
          904  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHi, 0);
          905  +    printf("-- Successful lookasides:       %d\n", iHi);
          906  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHi,0);
          907  +    printf("-- Lookaside size faults:       %d\n", iHi);
          908  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHi,0);
          909  +    printf("-- Lookaside OOM faults:        %d\n", iHi);
          910  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHi, 0);
          911  +    printf("-- Pager Heap Usage:            %d bytes\n", iCur);
          912  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHi, 1);
          913  +    printf("-- Page cache hits:             %d\n", iCur);
          914  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHi, 1);
          915  +    printf("-- Page cache misses:           %d\n", iCur); 
          916  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHi, 1);
          917  +    printf("-- Page cache writes:           %d\n", iCur); 
          918  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHi, 0);
          919  +    printf("-- Schema Heap Usage:           %d bytes\n", iCur); 
          920  +    sqlite3_db_status(g.db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHi, 0);
          921  +    printf("-- Statement Heap Usage:        %d bytes\n", iCur); 
          922  +  }
          923  +
          924  +  sqlite3_close(g.db);
          925  +
          926  +  /* Global memory usage statistics printed after the database connection
          927  +  ** has closed.  Memory usage should be zero at this point. */
          928  +  if( showStats ){
          929  +    sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHi, 0);
          930  +    printf("-- Memory Used (bytes):         %d (max %d)\n", iCur,iHi);
          931  +    sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHi, 0);
          932  +    printf("-- Outstanding Allocations:     %d (max %d)\n", iCur,iHi);
          933  +    sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHi, 0);
          934  +    printf("-- Pcache Overflow Bytes:       %d (max %d)\n", iCur,iHi);
          935  +    sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHi, 0);
          936  +    printf("-- Scratch Overflow Bytes:      %d (max %d)\n", iCur,iHi);
          937  +    sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHi, 0);
          938  +    printf("-- Largest Allocation:          %d bytes\n",iHi);
          939  +    sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHi, 0);
          940  +    printf("-- Largest Pcache Allocation:   %d bytes\n",iHi);
          941  +    sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHi, 0);
          942  +    printf("-- Largest Scratch Allocation:  %d bytes\n", iHi);
          943  +  }
          944  +
          945  +  /* Release memory */
          946  +  free( pLook );
          947  +  free( pPCache );
          948  +  free( pScratch );
          949  +  free( pHeap );
          950  +  return 0;
          951  +}

Added test/win32heap.test.

            1  +# 2013 November 22
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this script is recovery from transient manditory locks
           13  +# that sometimes appear on database files due to anti-virus software.
           14  +#
           15  +
           16  +if {$tcl_platform(platform)!="windows"} return
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +
           21  +ifcapable !win32malloc {
           22  +  finish_test
           23  +  return
           24  +}
           25  +
           26  +set testprefix win32heap
           27  +
           28  +do_test 1.1 {
           29  +  catch {db close}
           30  +  sqlite3_shutdown
           31  +  sqlite3_config_heap_size 1048576
           32  +  sqlite3_initialize
           33  +} {SQLITE_OK}
           34  +
           35  +do_test 1.2 {
           36  +  sqlite3 db test.db
           37  +  catchsql {
           38  +    CREATE TABLE t1(x);
           39  +  }
           40  +} {0 {}}
           41  +
           42  +do_test 1.3 {
           43  +  catchsql {
           44  +    INSERT INTO t1 (x) VALUES(RANDOMBLOB(1048576));
           45  +  }
           46  +} {1 {out of memory}}
           47  +
           48  +do_test 1.4 {
           49  +  catchsql {
           50  +    SELECT COUNT(*) FROM t1;
           51  +  }
           52  +} {0 0}
           53  +
           54  +do_test 1.5 {
           55  +  catch {db close}
           56  +  sqlite3_shutdown
           57  +  sqlite3_config_heap_size 0
           58  +  sqlite3_initialize
           59  +} {SQLITE_OK}
           60  +
           61  +do_test 1.6 {
           62  +  sqlite3 db test.db
           63  +  catchsql {
           64  +    INSERT INTO t1 (x) VALUES(RANDOMBLOB(1048576));
           65  +  }
           66  +} {0 {}}
           67  +
           68  +do_test 1.7 {
           69  +  catchsql {
           70  +    SELECT COUNT(*) FROM t1;
           71  +  }
           72  +} {0 1}
           73  +
           74  +finish_test

Changes to test/wordcount.c.

    15     15   **
    16     16   **     --without-rowid      Use a WITHOUT ROWID table to store the words.
    17     17   **     --insert             Use INSERT mode (the default)
    18     18   **     --replace            Use REPLACE mode
    19     19   **     --select             Use SELECT mode
    20     20   **     --update             Use UPDATE mode
    21     21   **     --delete             Use DELETE mode
           22  +**     --query              Use QUERY mode
    22     23   **     --nocase             Add the NOCASE collating sequence to the words.
    23     24   **     --trace              Enable sqlite3_trace() output.
    24     25   **     --summary            Show summary information on the collected data.
    25     26   **     --stats              Show sqlite3_status() results at the end.
    26     27   **     --pagesize NNN       Use a page size of NNN
    27     28   **     --cachesize NNN      Use a cache size of NNN
    28     29   **     --commit NNN         Commit after every NNN operations
    29     30   **     --nosync             Use PRAGMA synchronous=OFF
    30     31   **     --journal MMMM       Use PRAGMA journal_mode=MMMM
           32  +**     --timer              Time the operation of this program
    31     33   **
    32     34   ** Modes:
    33     35   **
    34     36   ** Insert mode means:
    35     37   **    (1) INSERT OR IGNORE INTO wordcount VALUES($new,1)
    36     38   **    (2) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new -- if (1) is a noop
    37     39   **
................................................................................
    47     49   **    (1) SELECT 1 FROM wordcount WHERE word=$new
    48     50   **    (2) INSERT INTO wordcount VALUES($new,1) -- if (1) returns nothing
    49     51   **    (3) UPDATE wordcount SET cnt=cnt+1 WHERE word=$new  --if (1) return TRUE
    50     52   **
    51     53   ** Delete mode means:
    52     54   **    (1) DELETE FROM wordcount WHERE word=$new
    53     55   **
    54         -** Note that delete mode is only useful for preexisting databases.  The
    55         -** wordcount table is created using IF NOT EXISTS so this utility can be
    56         -** run multiple times on the same database file.  The --without-rowid,
    57         -** --nocase, and --pagesize parameters are only effective when creating
    58         -** a new database and are harmless no-ops on preexisting databases.
           56  +** Query mode means:
           57  +**    (1) SELECT cnt FROM wordcount WHERE word=$new
           58  +**
           59  +** Note that delete mode and query mode are only useful for preexisting
           60  +** databases.  The wordcount table is created using IF NOT EXISTS so this
           61  +** utility can be run multiple times on the same database file.  The
           62  +** --without-rowid, --nocase, and --pagesize parameters are only effective
           63  +** when creating a new database and are harmless no-ops on preexisting
           64  +** databases.
    59     65   **
    60     66   ******************************************************************************
    61     67   **
    62     68   ** Compile as follows:
    63     69   **
    64     70   **    gcc -I. wordcount.c sqlite3.c -ldl -lpthreads
    65     71   **
................................................................................
    70     76   */
    71     77   #include <stdio.h>
    72     78   #include <string.h>
    73     79   #include <ctype.h>
    74     80   #include <stdlib.h>
    75     81   #include <stdarg.h>
    76     82   #include "sqlite3.h"
           83  +
           84  +/* Return the current wall-clock time */
           85  +static sqlite3_int64 realTime(void){
           86  +  static sqlite3_vfs *clockVfs = 0;
           87  +  sqlite3_int64 t;
           88  +  if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
           89  +  if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
           90  +    clockVfs->xCurrentTimeInt64(clockVfs, &t);
           91  +  }else{
           92  +    double r;
           93  +    clockVfs->xCurrentTime(clockVfs, &r);
           94  +    t = (sqlite3_int64)(r*86400000.0);
           95  +  }
           96  +  return t;
           97  +}
    77     98   
    78     99   /* Print an error message and exit */
    79    100   static void fatal_error(const char *zMsg, ...){
    80    101     va_list ap;
    81    102     va_start(ap, zMsg);
    82    103     vfprintf(stderr, zMsg, ap);
    83    104     va_end(ap);
................................................................................
    91    112   
    92    113   /* An sqlite3_exec() callback that prints results on standard output,
    93    114   ** each column separated by a single space. */
    94    115   static int printResult(void *NotUsed, int nArg, char **azArg, char **azNm){
    95    116     int i;
    96    117     printf("--");
    97    118     for(i=0; i<nArg; i++){
    98         -    printf(" %s", azArg[i]);
          119  +    printf(" %s", azArg[i] ? azArg[i] : "(null)");
    99    120     }
   100    121     printf("\n");
   101    122     return 0;
   102    123   }
   103    124   
   104    125   
   105    126   /*
................................................................................
   166    187   
   167    188   /* Define operating modes */
   168    189   #define MODE_INSERT     0
   169    190   #define MODE_REPLACE    1
   170    191   #define MODE_SELECT     2
   171    192   #define MODE_UPDATE     3
   172    193   #define MODE_DELETE     4
          194  +#define MODE_QUERY      5
   173    195   
   174    196   int main(int argc, char **argv){
   175    197     const char *zFileToRead = 0;  /* Input file.  NULL for stdin */
   176    198     const char *zDbName = 0;      /* Name of the database file to create */
   177    199     int useWithoutRowid = 0;      /* True for --without-rowid */
   178    200     int iMode = MODE_INSERT;      /* One of MODE_xxxxx */
   179    201     int useNocase = 0;            /* True for --nocase */
   180    202     int doTrace = 0;              /* True for --trace */
   181    203     int showStats = 0;            /* True for --stats */
   182    204     int showSummary = 0;          /* True for --summary */
          205  +  int showTimer = 0;            /* True for --timer */
   183    206     int cacheSize = 0;            /* Desired cache size.  0 means default */
   184    207     int pageSize = 0;             /* Desired page size.  0 means default */
   185    208     int commitInterval = 0;       /* How often to commit.  0 means never */
   186    209     int noSync = 0;               /* True for --nosync */
   187    210     const char *zJMode = 0;       /* Journal mode */
   188    211     int nOp = 0;                  /* Operation counter */
   189    212     int i, j;                     /* Loop counters */
................................................................................
   192    215     sqlite3_stmt *pInsert = 0;    /* The INSERT statement */
   193    216     sqlite3_stmt *pUpdate = 0;    /* The UPDATE statement */
   194    217     sqlite3_stmt *pSelect = 0;    /* The SELECT statement */
   195    218     sqlite3_stmt *pDelete = 0;    /* The DELETE statement */
   196    219     FILE *in;                     /* The open input file */
   197    220     int rc;                       /* Return code from an SQLite interface */
   198    221     int iCur, iHiwtr;             /* Statistics values, current and "highwater" */
          222  +  sqlite3_int64 sumCnt = 0;     /* Sum in QUERY mode */
          223  +  sqlite3_int64 startTime;
   199    224     char zInput[2000];            /* A single line of input */
   200    225   
   201    226     /* Process command-line arguments */
   202    227     for(i=1; i<argc; i++){
   203    228       const char *z = argv[i];
   204    229       if( z[0]=='-' ){
   205    230         do{ z++; }while( z[0]=='-' );
................................................................................
   211    236           iMode = MODE_SELECT;
   212    237         }else if( strcmp(z,"insert")==0 ){
   213    238           iMode = MODE_INSERT;
   214    239         }else if( strcmp(z,"update")==0 ){
   215    240           iMode = MODE_UPDATE;
   216    241         }else if( strcmp(z,"delete")==0 ){
   217    242           iMode = MODE_DELETE;
          243  +      }else if( strcmp(z,"query")==0 ){
          244  +        iMode = MODE_QUERY;
   218    245         }else if( strcmp(z,"nocase")==0 ){
   219    246           useNocase = 1;
   220    247         }else if( strcmp(z,"trace")==0 ){
   221    248           doTrace = 1;
   222    249         }else if( strcmp(z,"nosync")==0 ){
   223    250           noSync = 1;
   224    251         }else if( strcmp(z,"stats")==0 ){
   225    252           showStats = 1;
   226    253         }else if( strcmp(z,"summary")==0 ){
   227    254           showSummary = 1;
          255  +      }else if( strcmp(z,"timer")==0 ){
          256  +        showTimer = i;
   228    257         }else if( strcmp(z,"cachesize")==0 && i<argc-1 ){
   229    258           i++;
   230    259           cacheSize = atoi(argv[i]);
   231    260         }else if( strcmp(z,"pagesize")==0 && i<argc-1 ){
   232    261           i++;
   233    262           pageSize = atoi(argv[i]);
   234    263         }else if( strcmp(z,"commit")==0 && i<argc-1 ){
................................................................................
   246    275       }else{
   247    276         fatal_error("surplus argument: %s\n", argv[i]);
   248    277       }
   249    278     }
   250    279     if( zDbName==0 ){
   251    280       fatal_error("Usage: %s [--options] DATABASE [INPUTFILE]\n", argv[0]);
   252    281     }
          282  +  startTime = realTime();
   253    283   
   254    284     /* Open the database and the input file */
   255    285     if( sqlite3_open(zDbName, &db) ){
   256    286       fatal_error("Cannot open database file: %s\n", zDbName);
   257    287     }
   258    288     if( zFileToRead ){
   259    289       in = fopen(zFileToRead, "rb");
................................................................................
   299    329     if( zSql==0 ) fatal_error("out of memory\n");
   300    330     rc = sqlite3_exec(db, zSql, 0, 0, 0);
   301    331     if( rc ) fatal_error("Could not create the wordcount table: %s.\n",
   302    332                          sqlite3_errmsg(db));
   303    333     sqlite3_free(zSql);
   304    334   
   305    335     /* Prepare SQL statements that will be needed */
          336  +  if( iMode==MODE_QUERY ){
          337  +    rc = sqlite3_prepare_v2(db,
          338  +          "SELECT cnt FROM wordcount WHERE word=?1",
          339  +          -1, &pSelect, 0);
          340  +    if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n",
          341  +                          sqlite3_errmsg(db));
          342  +  }
   306    343     if( iMode==MODE_SELECT ){
   307    344       rc = sqlite3_prepare_v2(db,
   308    345             "SELECT 1 FROM wordcount WHERE word=?1",
   309    346             -1, &pSelect, 0);
   310    347       if( rc ) fatal_error("Could not prepare the SELECT statement: %s\n",
   311    348                             sqlite3_errmsg(db));
   312    349       rc = sqlite3_prepare_v2(db,
................................................................................
   381    418             if( sqlite3_step(pInsert)!=SQLITE_DONE ){
   382    419               fatal_error("Insert failed: %s\n", sqlite3_errmsg(db));
   383    420             }
   384    421             sqlite3_reset(pInsert);
   385    422           }else{
   386    423             fatal_error("SELECT failed: %s\n", sqlite3_errmsg(db));
   387    424           }
          425  +      }else if( iMode==MODE_QUERY ){
          426  +        sqlite3_bind_text(pSelect, 1, zInput+i, j-i, SQLITE_STATIC);
          427  +        if( sqlite3_step(pSelect)==SQLITE_ROW ){
          428  +          sumCnt += sqlite3_column_int64(pSelect, 0);
          429  +        }
          430  +        sqlite3_reset(pSelect);
   388    431         }else{
   389    432           sqlite3_bind_text(pInsert, 1, zInput+i, j-i, SQLITE_STATIC);
   390    433           if( sqlite3_step(pInsert)!=SQLITE_DONE ){
   391    434             fatal_error("INSERT failed: %s\n", sqlite3_errmsg(db));
   392    435           }
   393    436           sqlite3_reset(pInsert);
   394    437           if( iMode==MODE_UPDATE
................................................................................
   412    455     }
   413    456     sqlite3_exec(db, "COMMIT", 0, 0, 0);
   414    457     if( zFileToRead ) fclose(in);
   415    458     sqlite3_finalize(pInsert);
   416    459     sqlite3_finalize(pUpdate);
   417    460     sqlite3_finalize(pSelect);
   418    461     sqlite3_finalize(pDelete);
          462  +
          463  +  if( iMode==MODE_QUERY ){
          464  +    printf("sum of cnt: %lld\n", sumCnt);
          465  +    rc = sqlite3_prepare_v2(db,"SELECT sum(cnt*cnt) FROM wordcount", -1,
          466  +                            &pSelect, 0);
          467  +    if( rc==SQLITE_OK && sqlite3_step(pSelect)==SQLITE_ROW ){
          468  +      printf("double-check: %lld\n", sqlite3_column_int64(pSelect, 0));
          469  +    }
          470  +    sqlite3_finalize(pSelect);
          471  +  }
          472  +
          473  +
          474  +  if( showTimer ){
          475  +    sqlite3_int64 elapseTime = realTime() - startTime;
          476  +    fprintf(stderr, "%3d.%03d wordcount", (int)(elapseTime/1000),
          477  +                                   (int)(elapseTime%1000));
          478  +    for(i=1; i<argc; i++) if( i!=showTimer ) fprintf(stderr, " %s", argv[i]);
          479  +    fprintf(stderr, "\n");
          480  +  }
   419    481   
   420    482     if( showSummary ){
   421    483       sqlite3_create_function(db, "checksum", -1, SQLITE_UTF8, 0,
   422    484                               0, checksumStep, checksumFinalize);
   423    485       sqlite3_exec(db, 
   424    486         "SELECT 'count(*):  ', count(*) FROM wordcount;\n"
   425    487         "SELECT 'sum(cnt):  ', sum(cnt) FROM wordcount;\n"
   426    488         "SELECT 'max(cnt):  ', max(cnt) FROM wordcount;\n"
   427    489         "SELECT 'avg(cnt):  ', avg(cnt) FROM wordcount;\n"
   428    490         "SELECT 'sum(cnt=1):', sum(cnt=1) FROM wordcount;\n"
   429    491         "SELECT 'top 10:    ', group_concat(word, ', ') FROM "
   430         -         "(SELECT word FROM wordcount ORDER BY cnt DESC LIMIT 10);\n"
          492  +         "(SELECT word FROM wordcount ORDER BY cnt DESC, word LIMIT 10);\n"
   431    493         "SELECT 'checksum:  ', checksum(word, cnt) FROM "
   432    494            "(SELECT word, cnt FROM wordcount ORDER BY word);\n"
   433    495         "PRAGMA integrity_check;\n",
   434    496         printResult, 0, 0);
   435    497     }
   436    498   
   437    499     /* Database connection statistics printed after both prepared statements

Changes to tool/build-all-msvc.bat.

   488    488     GOTO :EOF
   489    489   
   490    490   :fn_SetErrorLevel
   491    491     VERIFY MAYBE 2> NUL
   492    492     GOTO :EOF
   493    493   
   494    494   :fn_CopyVariable
   495         -  SETLOCAL
   496    495     IF NOT DEFINED %1 GOTO :EOF
   497    496     IF "%2" == "" GOTO :EOF
          497  +  SETLOCAL
   498    498     SET __ECHO_CMD=ECHO %%%1%%
   499    499     FOR /F "delims=" %%V IN ('%__ECHO_CMD%') DO (
   500    500       SET VALUE=%%V
   501    501     )
   502    502     ENDLOCAL && SET %2=%VALUE%
   503    503     GOTO :EOF
   504    504