/ Check-in [a24f805b]
Login

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

Overview
Comment:Merge all the latest performance enhancements from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: a24f805b5eb22452a22b937d372abba4426ae771
User & Date: drh 2016-12-12 16:15:40
Context
2016-12-16
21:29
Merge recent enhancements from trunk, and especially the pragma-as-vtab change. check-in: 4b1e7804 user: drh tags: apple-osx
2016-12-12
16:15
Merge all the latest performance enhancements from trunk. check-in: a24f805b user: drh tags: apple-osx
16:08
Faster operation for large in-memory databases. check-in: 9675518b user: drh tags: trunk
2016-11-26
20:44
Merge all recent trunk changes, and especially the new sqlite3_snapshot_recover() interface. check-in: 41a3af54 user: drh tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   429    429     $(TOP)/ext/fts5/fts5_tcl.c \
   430    430     $(TOP)/ext/fts5/fts5_test_mi.c \
   431    431     $(TOP)/ext/fts5/fts5_test_tok.c \
   432    432     $(TOP)/ext/misc/ieee754.c \
   433    433     $(TOP)/ext/misc/nextchar.c \
   434    434     $(TOP)/ext/misc/percentile.c \
   435    435     $(TOP)/ext/misc/regexp.c \
          436  +  $(TOP)/ext/misc/remember.c \
   436    437     $(TOP)/ext/misc/series.c \
   437    438     $(TOP)/ext/misc/spellfix.c \
   438    439     $(TOP)/ext/misc/totype.c \
   439    440     $(TOP)/ext/misc/wholenumber.c
   440    441   
   441    442   # Source code to the library files needed by the test fixture
   442    443   #

Changes to Makefile.msc.

  1392   1392     $(TOP)\ext\fts5\fts5_tcl.c \
  1393   1393     $(TOP)\ext\fts5\fts5_test_mi.c \
  1394   1394     $(TOP)\ext\fts5\fts5_test_tok.c \
  1395   1395     $(TOP)\ext\misc\ieee754.c \
  1396   1396     $(TOP)\ext\misc\nextchar.c \
  1397   1397     $(TOP)\ext\misc\percentile.c \
  1398   1398     $(TOP)\ext\misc\regexp.c \
         1399  +  $(TOP)\ext\misc\remember.c \
  1399   1400     $(TOP)\ext\misc\series.c \
  1400   1401     $(TOP)\ext\misc\spellfix.c \
  1401   1402     $(TOP)\ext\misc\totype.c \
  1402   1403     $(TOP)\ext\misc\wholenumber.c
  1403   1404   
  1404   1405   # Source code to the library files needed by the test fixture
  1405   1406   # (non-amalgamation)

Changes to ext/fts5/fts5_expr.c.

   742    742   }
   743    743   
   744    744   
   745    745   /*
   746    746   ** Initialize all term iterators in the pNear object. If any term is found
   747    747   ** to match no documents at all, return immediately without initializing any
   748    748   ** further iterators.
          749  +**
          750  +** If an error occurs, return an SQLite error code. Otherwise, return
          751  +** SQLITE_OK. It is not considered an error if some term matches zero
          752  +** documents.
   749    753   */
   750    754   static int fts5ExprNearInitAll(
   751    755     Fts5Expr *pExpr,
   752    756     Fts5ExprNode *pNode
   753    757   ){
   754    758     Fts5ExprNearset *pNear = pNode->pNear;
   755         -  int i, j;
   756         -  int rc = SQLITE_OK;
   757         -  int bEof = 1;
          759  +  int i;
   758    760   
   759    761     assert( pNode->bNomatch==0 );
   760         -  for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
          762  +  for(i=0; i<pNear->nPhrase; i++){
   761    763       Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
   762         -    for(j=0; j<pPhrase->nTerm; j++){
   763         -      Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
   764         -      Fts5ExprTerm *p;
   765         -
   766         -      for(p=pTerm; p && rc==SQLITE_OK; p=p->pSynonym){
   767         -        if( p->pIter ){
   768         -          sqlite3Fts5IterClose(p->pIter);
   769         -          p->pIter = 0;
   770         -        }
   771         -        rc = sqlite3Fts5IndexQuery(
   772         -            pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
   773         -            (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
   774         -            (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
   775         -            pNear->pColset,
   776         -            &p->pIter
   777         -        );
   778         -        assert( rc==SQLITE_OK || p->pIter==0 );
   779         -        if( p->pIter && 0==sqlite3Fts5IterEof(p->pIter) ){
   780         -          bEof = 0;
   781         -        }
   782         -      }
   783         -
   784         -      if( bEof ) break;
   785         -    }
   786         -    if( bEof ) break;
   787         -  }
   788         -
   789         -  pNode->bEof = bEof;
   790         -  return rc;
          764  +    if( pPhrase->nTerm==0 ){
          765  +      pNode->bEof = 1;
          766  +      return SQLITE_OK;
          767  +    }else{
          768  +      int j;
          769  +      for(j=0; j<pPhrase->nTerm; j++){
          770  +        Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
          771  +        Fts5ExprTerm *p;
          772  +        int bHit = 0;
          773  +
          774  +        for(p=pTerm; p; p=p->pSynonym){
          775  +          int rc;
          776  +          if( p->pIter ){
          777  +            sqlite3Fts5IterClose(p->pIter);
          778  +            p->pIter = 0;
          779  +          }
          780  +          rc = sqlite3Fts5IndexQuery(
          781  +              pExpr->pIndex, p->zTerm, (int)strlen(p->zTerm),
          782  +              (pTerm->bPrefix ? FTS5INDEX_QUERY_PREFIX : 0) |
          783  +              (pExpr->bDesc ? FTS5INDEX_QUERY_DESC : 0),
          784  +              pNear->pColset,
          785  +              &p->pIter
          786  +          );
          787  +          assert( (rc==SQLITE_OK)==(p->pIter!=0) );
          788  +          if( rc!=SQLITE_OK ) return rc;
          789  +          if( 0==sqlite3Fts5IterEof(p->pIter) ){
          790  +            bHit = 1;
          791  +          }
          792  +        }
          793  +
          794  +        if( bHit==0 ){
          795  +          pNode->bEof = 1;
          796  +          return SQLITE_OK;
          797  +        }
          798  +      }
          799  +    }
          800  +  }
          801  +
          802  +  pNode->bEof = 0;
          803  +  return SQLITE_OK;
   791    804   }
   792    805   
   793    806   /*
   794    807   ** If pExpr is an ASC iterator, this function returns a value with the
   795    808   ** same sign as:
   796    809   **
   797    810   **   (iLhs - iRhs)
................................................................................
  1327   1340   
  1328   1341     p->pIndex = pIdx;
  1329   1342     p->bDesc = bDesc;
  1330   1343     rc = fts5ExprNodeFirst(p, pRoot);
  1331   1344   
  1332   1345     /* If not at EOF but the current rowid occurs earlier than iFirst in
  1333   1346     ** the iteration order, move to document iFirst or later. */
  1334         -  if( pRoot->bEof==0 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 ){
         1347  +  if( rc==SQLITE_OK 
         1348  +   && 0==pRoot->bEof 
         1349  +   && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 
         1350  +  ){
  1335   1351       rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
  1336   1352     }
  1337   1353   
  1338   1354     /* If the iterator is not at a real match, skip forward until it is. */
  1339   1355     while( pRoot->bNomatch ){
  1340   1356       assert( pRoot->bEof==0 && rc==SQLITE_OK );
  1341   1357       rc = fts5ExprNodeNext(p, pRoot, 0, 0);

Changes to ext/fts5/test/fts5faultB.test.

    74     74   
    75     75   do_faultsim_test 2.4 -faults oom* -body {
    76     76     execsql { SELECT mit(matchinfo(t1, 's')) FROM t1('a b c') }
    77     77   } -test {
    78     78     faultsim_test_result {0 {{3 2} {2 3}}} 
    79     79   }
    80     80   
           81  +#-------------------------------------------------------------------------
           82  +#
           83  +reset_db 
           84  +do_execsql_test 3.0 {
           85  +  CREATE VIRTUAL TABLE x1 USING fts5(z);
           86  +}
           87  +
           88  +do_faultsim_test 3.1 -faults oom* -body {
           89  +  execsql {
           90  +    SELECT rowid FROM x1('c') WHERE rowid>1;
           91  +  }
           92  +} -test {
           93  +  faultsim_test_result {0 {}}
           94  +}
           95  +
           96  +do_execsql_test 3.2 {
           97  +  INSERT INTO x1 VALUES('a b c');
           98  +  INSERT INTO x1 VALUES('b c d');
           99  +  INSERT INTO x1 VALUES('c d e');
          100  +  INSERT INTO x1 VALUES('d e f');
          101  +}
          102  +do_faultsim_test 3.3 -faults oom* -body {
          103  +  execsql {
          104  +    SELECT rowid FROM x1('c') WHERE rowid>1;
          105  +  }
          106  +} -test {
          107  +  faultsim_test_result {0 {2 3}}
          108  +}
    81    109   
    82    110   finish_test
    83    111   

Changes to ext/fts5/test/fts5prefix.test.

     5      5   #
     6      6   #    May you do good and not evil.
     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12         -# This file containst tests focused on prefix indexes.
           12  +# This file contains tests focused on prefix indexes.
    13     13   #
    14     14   
    15     15   source [file join [file dirname [info script]] fts5_common.tcl]
    16     16   set testprefix fts5prefix
    17     17   
    18     18   # If SQLITE_ENABLE_FTS5 is defined, omit this file.
    19     19   ifcapable !fts5 {

Changes to ext/fts5/test/fts5simple2.test.

   327    327       INSERT INTO t2(rowid, x) VALUES(1, 'a b c');
   328    328       INSERT INTO t2(rowid, x) VALUES(456, 'a b c');
   329    329       INSERT INTO t2(rowid, x) VALUES(1000, 'a b c');
   330    330     COMMIT;
   331    331     UPDATE t2 SET x=x;
   332    332     DELETE FROM t2;
   333    333   }
          334  +
          335  +#-------------------------------------------------------------------------
          336  +#
          337  +reset_db
          338  +do_execsql_test 17.0 {
          339  +  CREATE VIRTUAL TABLE t2 USING fts5(x, y);
          340  +  BEGIN;
          341  +    INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb');
          342  +    INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb');
          343  +    INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb');
          344  +  COMMIT;
          345  +}
          346  +do_execsql_test 17.1 { SELECT * FROM t2('y:a*') WHERE rowid BETWEEN 10 AND 20 }
          347  +do_execsql_test 17.2 {
          348  +  BEGIN;
          349  +    INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb');
          350  +    SELECT * FROM t2('y:a*') WHERE rowid BETWEEN 10 AND 20 ;
          351  +}
          352  +do_execsql_test 17.3 {
          353  +  COMMIT
          354  +}
          355  +
          356  +reset_db
          357  +do_execsql_test 17.4 {
          358  +  CREATE VIRTUAL TABLE t2 USING fts5(x, y);
          359  +  BEGIN;
          360  +    INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb');
          361  +    INSERT INTO t2 VALUES('a aa aaa', 'b bb bbb');
          362  +    SELECT * FROM t2('y:a*') WHERE rowid>66;
          363  +}
          364  +do_execsql_test 17.5 { SELECT * FROM t2('x:b* OR y:a*') }
          365  +do_execsql_test 17.5 { COMMIT ; SELECT * FROM t2('x:b* OR y:a*') }
          366  +do_execsql_test 17.6 { 
          367  +  SELECT * FROM t2('x:b* OR y:a*') WHERE rowid>55
          368  +}
   334    369   
   335    370   #db eval {SELECT rowid, fts5_decode_none(rowid, block) aS r FROM t2_data} {puts $r}
   336    371     
   337    372   finish_test
   338    373   

Added ext/misc/remember.c.

            1  +/*
            2  +** 2016-08-09
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file demonstrates how to create an SQL function that is a pass-through
           14  +** for integer values (it returns a copy of its argument) but also saves the
           15  +** value that is passed through into a C-language variable.  The address of
           16  +** the C-language variable is supplied as the second argument.
           17  +**
           18  +** This allows, for example, a counter to incremented and the original
           19  +** value retrieved, atomically, using a single statement:
           20  +**
           21  +**    UPDATE counterTab SET cnt=remember(cnt,$PTR)+1 WHERE id=$ID
           22  +**
           23  +** Prepare the above statement once.  Then to use it, bind the address
           24  +** of the output variable to $PTR and the id of the counter to $ID and
           25  +** run the prepared statement.
           26  +**
           27  +** One can imagine doing similar things with floating-point values and
           28  +** strings, but this demonstration extension will stick to using just
           29  +** integers.
           30  +*/
           31  +#include "sqlite3ext.h"
           32  +SQLITE_EXTENSION_INIT1
           33  +#include <assert.h>
           34  +
           35  +/*
           36  +**      remember(V,PTR)
           37  +**
           38  +** Return the integer value V.  Also save the value of V in a
           39  +** C-language variable whose address is PTR.
           40  +*/
           41  +static void rememberFunc(
           42  +  sqlite3_context *pCtx,
           43  +  int argc,
           44  +  sqlite3_value **argv
           45  +){
           46  +  sqlite3_int64 v;
           47  +  sqlite3_int64 ptr;
           48  +  assert( argc==2 );
           49  +  v = sqlite3_value_int64(argv[0]);
           50  +  ptr = sqlite3_value_int64(argv[1]);
           51  +  *((sqlite3_int64*)ptr) = v;
           52  +  sqlite3_result_int64(pCtx, v);
           53  +}
           54  +
           55  +#ifdef _WIN32
           56  +__declspec(dllexport)
           57  +#endif
           58  +int sqlite3_remember_init(
           59  +  sqlite3 *db, 
           60  +  char **pzErrMsg, 
           61  +  const sqlite3_api_routines *pApi
           62  +){
           63  +  int rc = SQLITE_OK;
           64  +  SQLITE_EXTENSION_INIT2(pApi);
           65  +  rc = sqlite3_create_function(db, "remember", 2, SQLITE_UTF8, 0,
           66  +                               rememberFunc, 0, 0);
           67  +  return rc;
           68  +}

Changes to main.mk.

   334    334     $(TOP)/ext/misc/eval.c \
   335    335     $(TOP)/ext/misc/fileio.c \
   336    336     $(TOP)/ext/misc/fuzzer.c \
   337    337     $(TOP)/ext/misc/ieee754.c \
   338    338     $(TOP)/ext/misc/nextchar.c \
   339    339     $(TOP)/ext/misc/percentile.c \
   340    340     $(TOP)/ext/misc/regexp.c \
          341  +  $(TOP)/ext/misc/remember.c \
   341    342     $(TOP)/ext/misc/series.c \
   342    343     $(TOP)/ext/misc/spellfix.c \
   343    344     $(TOP)/ext/misc/totype.c \
   344    345     $(TOP)/ext/misc/wholenumber.c \
   345    346     $(TOP)/ext/misc/vfslog.c \
   346    347     $(TOP)/ext/fts5/fts5_tcl.c \
   347    348     $(TOP)/ext/fts5/fts5_test_mi.c \

Changes to src/bitvec.c.

   289    289   ** Return the value of the iSize parameter specified when Bitvec *p
   290    290   ** was created.
   291    291   */
   292    292   u32 sqlite3BitvecSize(Bitvec *p){
   293    293     return p->iSize;
   294    294   }
   295    295   
   296         -#ifndef SQLITE_OMIT_BUILTIN_TEST
          296  +#ifndef SQLITE_UNTESTABLE
   297    297   /*
   298    298   ** Let V[] be an array of unsigned characters sufficient to hold
   299    299   ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
   300    300   ** Then the following macros can be used to set, clear, or test
   301    301   ** individual bits within V.
   302    302   */
   303    303   #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
................................................................................
   404    404     /* Free allocated structure */
   405    405   bitvec_end:
   406    406     sqlite3_free(pTmpSpace);
   407    407     sqlite3_free(pV);
   408    408     sqlite3BitvecDestroy(pBitvec);
   409    409     return rc;
   410    410   }
   411         -#endif /* SQLITE_OMIT_BUILTIN_TEST */
          411  +#endif /* SQLITE_UNTESTABLE */

Changes to src/btree.c.

  1745   1745     assert( pPage->pBt->db!=0 );
  1746   1746     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  1747   1747     assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
  1748   1748     assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
  1749   1749     assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
  1750   1750   
  1751   1751     if( !pPage->isInit ){
  1752         -    u16 pc;            /* Address of a freeblock within pPage->aData[] */
         1752  +    u32 pc;            /* Address of a freeblock within pPage->aData[] */
  1753   1753       u8 hdr;            /* Offset to beginning of page header */
  1754   1754       u8 *data;          /* Equal to pPage->aData */
  1755   1755       BtShared *pBt;        /* The main btree structure */
  1756   1756       int usableSize;    /* Amount of usable space on each page */
  1757   1757       u16 cellOffset;    /* Offset from start of page to first cell pointer */
  1758   1758       int nFree;         /* Number of unused bytes on the page */
  1759   1759       int top;           /* First byte of the cell content area */
................................................................................
  1825   1825   
  1826   1826       /* Compute the total free space on the page
  1827   1827       ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
  1828   1828       ** start of the first freeblock on the page, or is zero if there are no
  1829   1829       ** freeblocks. */
  1830   1830       pc = get2byte(&data[hdr+1]);
  1831   1831       nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
  1832         -    while( pc>0 ){
  1833         -      u16 next, size;
  1834         -      if( pc<iCellFirst || pc>iCellLast ){
         1832  +    if( pc>0 ){
         1833  +      u32 next, size;
         1834  +      if( pc<iCellFirst ){
  1835   1835           /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
  1836   1836           ** always be at least one cell before the first freeblock.
  1837         -        **
  1838         -        ** Or, the freeblock is off the end of the page
  1839   1837           */
  1840   1838           return SQLITE_CORRUPT_BKPT; 
  1841   1839         }
  1842         -      next = get2byte(&data[pc]);
  1843         -      size = get2byte(&data[pc+2]);
  1844         -      if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
  1845         -        /* Free blocks must be in ascending order. And the last byte of
  1846         -        ** the free-block must lie on the database page.  */
  1847         -        return SQLITE_CORRUPT_BKPT; 
         1840  +      while( 1 ){
         1841  +        if( pc>iCellLast ){
         1842  +          return SQLITE_CORRUPT_BKPT; /* Freeblock off the end of the page */
         1843  +        }
         1844  +        next = get2byte(&data[pc]);
         1845  +        size = get2byte(&data[pc+2]);
         1846  +        nFree = nFree + size;
         1847  +        if( next<=pc+size+3 ) break;
         1848  +        pc = next;
  1848   1849         }
  1849         -      nFree = nFree + size;
  1850         -      pc = next;
         1850  +      if( next>0 ){
         1851  +        return SQLITE_CORRUPT_BKPT;  /* Freeblock not in ascending order */
         1852  +      }
         1853  +      if( pc+size>usableSize ){
         1854  +        return SQLITE_CORRUPT_BKPT;  /* Last freeblock extends past page end */
         1855  +      }
  1851   1856       }
  1852   1857   
  1853   1858       /* At this point, nFree contains the sum of the offset to the start
  1854   1859       ** of the cell-content area plus the number of free bytes within
  1855   1860       ** the cell-content area. If this is greater than the usable-size
  1856   1861       ** of the page, then the page must be corrupted. This check also
  1857   1862       ** serves to verify that the offset to the start of the cell-content
................................................................................
  2284   2289     
  2285   2290       pBt = sqlite3MallocZero( sizeof(*pBt) );
  2286   2291       if( pBt==0 ){
  2287   2292         rc = SQLITE_NOMEM_BKPT;
  2288   2293         goto btree_open_out;
  2289   2294       }
  2290   2295       rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
  2291         -                          EXTRA_SIZE, flags, vfsFlags, pageReinit);
         2296  +                          sizeof(MemPage), flags, vfsFlags, pageReinit);
  2292   2297       if( rc==SQLITE_OK ){
  2293   2298         sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
  2294   2299         rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
  2295   2300       }
  2296   2301       if( rc!=SQLITE_OK ){
  2297   2302         goto btree_open_out;
  2298   2303       }
................................................................................
  5998   6003   ** Free any overflow pages associated with the given Cell.  Write the
  5999   6004   ** local Cell size (the number of bytes on the original page, omitting
  6000   6005   ** overflow) into *pnSize.
  6001   6006   */
  6002   6007   static int clearCell(
  6003   6008     MemPage *pPage,          /* The page that contains the Cell */
  6004   6009     unsigned char *pCell,    /* First byte of the Cell */
  6005         -  u16 *pnSize              /* Write the size of the Cell here */
         6010  +  CellInfo *pInfo          /* Size information about the cell */
  6006   6011   ){
  6007   6012     BtShared *pBt = pPage->pBt;
  6008         -  CellInfo info;
  6009   6013     Pgno ovflPgno;
  6010   6014     int rc;
  6011   6015     int nOvfl;
  6012   6016     u32 ovflPageSize;
  6013   6017   
  6014   6018     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  6015         -  pPage->xParseCell(pPage, pCell, &info);
  6016         -  *pnSize = info.nSize;
  6017         -  if( info.nLocal==info.nPayload ){
         6019  +  pPage->xParseCell(pPage, pCell, pInfo);
         6020  +  if( pInfo->nLocal==pInfo->nPayload ){
  6018   6021       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
  6019   6022     }
  6020         -  if( pCell+info.nSize-1 > pPage->aData+pPage->maskPage ){
         6023  +  if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
  6021   6024       return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
  6022   6025     }
  6023         -  ovflPgno = get4byte(pCell + info.nSize - 4);
         6026  +  ovflPgno = get4byte(pCell + pInfo->nSize - 4);
  6024   6027     assert( pBt->usableSize > 4 );
  6025   6028     ovflPageSize = pBt->usableSize - 4;
  6026         -  nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
         6029  +  nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
  6027   6030     assert( nOvfl>0 || 
  6028         -    (CORRUPT_DB && (info.nPayload + ovflPageSize)<ovflPageSize)
         6031  +    (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
  6029   6032     );
  6030   6033     while( nOvfl-- ){
  6031   6034       Pgno iNext = 0;
  6032   6035       MemPage *pOvfl = 0;
  6033   6036       if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
  6034   6037         /* 0 is not a legal page number and page 1 cannot be an 
  6035   6038         ** overflow page. Therefore if ovflPgno<2 or past the end of the 
................................................................................
  6261   6264     u32 pc;         /* Offset to cell content of cell being deleted */
  6262   6265     u8 *data;       /* pPage->aData */
  6263   6266     u8 *ptr;        /* Used to move bytes around within data[] */
  6264   6267     int rc;         /* The return code */
  6265   6268     int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
  6266   6269   
  6267   6270     if( *pRC ) return;
  6268         -
  6269   6271     assert( idx>=0 && idx<pPage->nCell );
  6270   6272     assert( CORRUPT_DB || sz==cellSize(pPage, idx) );
  6271   6273     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  6272   6274     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  6273   6275     data = pPage->aData;
  6274   6276     ptr = &pPage->aCellIdx[2*idx];
  6275   6277     pc = get2byte(ptr);
................................................................................
  6345   6347         memcpy(pTemp, pCell, sz);
  6346   6348         pCell = pTemp;
  6347   6349       }
  6348   6350       if( iChild ){
  6349   6351         put4byte(pCell, iChild);
  6350   6352       }
  6351   6353       j = pPage->nOverflow++;
  6352         -    assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
         6354  +    /* Comparison against ArraySize-1 since we hold back one extra slot
         6355  +    ** as a contingency.  In other words, never need more than 3 overflow
         6356  +    ** slots but 4 are allocated, just to be safe. */
         6357  +    assert( j < ArraySize(pPage->apOvfl)-1 );
  6353   6358       pPage->apOvfl[j] = pCell;
  6354   6359       pPage->aiOvfl[j] = (u16)i;
  6355   6360   
  6356   6361       /* When multiple overflows occur, they are always sequential and in
  6357   6362       ** sorted order.  This invariants arise because multiple overflows can
  6358   6363       ** only occur when inserting divider cells into the parent page during
  6359   6364       ** balancing, and the dividers are adjacent and sorted.
................................................................................
  7085   7090       if( rc ){
  7086   7091         memset(apOld, 0, (i+1)*sizeof(MemPage*));
  7087   7092         goto balance_cleanup;
  7088   7093       }
  7089   7094       nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
  7090   7095       if( (i--)==0 ) break;
  7091   7096   
  7092         -    if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
         7097  +    if( pParent->nOverflow && ALWAYS(i+nxDiv==pParent->aiOvfl[0]) ){
  7093   7098         apDiv[i] = pParent->apOvfl[0];
  7094   7099         pgno = get4byte(apDiv[i]);
  7095   7100         szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
  7096   7101         pParent->nOverflow = 0;
  7097   7102       }else{
  7098   7103         apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
  7099   7104         pgno = get4byte(apDiv[i]);
................................................................................
  8024   8029       }else if( loc==0 ){
  8025   8030         rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, appendBias, &loc);
  8026   8031         if( rc ) return rc;
  8027   8032       }
  8028   8033     }else if( loc==0 ){
  8029   8034       if( pX->nMem ){
  8030   8035         UnpackedRecord r;
  8031         -      memset(&r, 0, sizeof(r));
  8032   8036         r.pKeyInfo = pCur->pKeyInfo;
  8033   8037         r.aMem = pX->aMem;
  8034   8038         r.nField = pX->nMem;
         8039  +      r.default_rc = 0;
         8040  +      r.errCode = 0;
         8041  +      r.r1 = 0;
         8042  +      r.r2 = 0;
         8043  +      r.eqSeen = 0;
  8035   8044         rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, appendBias, &loc);
  8036   8045       }else{
  8037   8046         rc = btreeMoveto(pCur, pX->pKey, pX->nKey, appendBias, &loc);
  8038   8047       }
  8039   8048       if( rc ) return rc;
  8040   8049     }
  8041   8050     assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
................................................................................
  8052   8061     assert( newCell!=0 );
  8053   8062     rc = fillInCell(pPage, newCell, pX, &szNew);
  8054   8063     if( rc ) goto end_insert;
  8055   8064     assert( szNew==pPage->xCellSize(pPage, newCell) );
  8056   8065     assert( szNew <= MX_CELL_SIZE(pBt) );
  8057   8066     idx = pCur->aiIdx[pCur->iPage];
  8058   8067     if( loc==0 ){
  8059         -    u16 szOld;
         8068  +    CellInfo info;
  8060   8069       assert( idx<pPage->nCell );
  8061   8070       rc = sqlite3PagerWrite(pPage->pDbPage);
  8062   8071       if( rc ){
  8063   8072         goto end_insert;
  8064   8073       }
  8065   8074       oldCell = findCell(pPage, idx);
  8066   8075       if( !pPage->leaf ){
  8067   8076         memcpy(newCell, oldCell, 4);
  8068   8077       }
  8069         -    rc = clearCell(pPage, oldCell, &szOld);
  8070         -    dropCell(pPage, idx, szOld, &rc);
         8078  +    rc = clearCell(pPage, oldCell, &info);
         8079  +    if( info.nSize==szNew && info.nLocal==info.nPayload ){
         8080  +      /* Overwrite the old cell with the new if they are the same size.
         8081  +      ** We could also try to do this if the old cell is smaller, then add
         8082  +      ** the leftover space to the free list.  But experiments show that
         8083  +      ** doing that is no faster then skipping this optimization and just
         8084  +      ** calling dropCell() and insertCell(). */
         8085  +      assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
         8086  +      if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
         8087  +      memcpy(oldCell, newCell, szNew);
         8088  +      return SQLITE_OK;
         8089  +    }
         8090  +    dropCell(pPage, idx, info.nSize, &rc);
  8071   8091       if( rc ) goto end_insert;
  8072   8092     }else if( loc<0 && pPage->nCell>0 ){
  8073   8093       assert( pPage->leaf );
  8074   8094       idx = ++pCur->aiIdx[pCur->iPage];
  8075   8095     }else{
  8076   8096       assert( pPage->leaf );
  8077   8097     }
................................................................................
  8139   8159     Btree *p = pCur->pBtree;
  8140   8160     BtShared *pBt = p->pBt;              
  8141   8161     int rc;                              /* Return code */
  8142   8162     MemPage *pPage;                      /* Page to delete cell from */
  8143   8163     unsigned char *pCell;                /* Pointer to cell to delete */
  8144   8164     int iCellIdx;                        /* Index of cell to delete */
  8145   8165     int iCellDepth;                      /* Depth of node containing pCell */ 
  8146         -  u16 szCell;                          /* Size of the cell being deleted */
         8166  +  CellInfo info;                       /* Size of the cell being deleted */
  8147   8167     int bSkipnext = 0;                   /* Leaf cursor in SKIPNEXT state */
  8148   8168     u8 bPreserve = flags & BTREE_SAVEPOSITION;  /* Keep cursor valid */
  8149   8169   
  8150   8170     assert( cursorOwnsBtShared(pCur) );
  8151   8171     assert( pBt->inTransaction==TRANS_WRITE );
  8152   8172     assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
  8153   8173     assert( pCur->curFlags & BTCF_WriteFlag );
................................................................................
  8211   8231     }
  8212   8232   
  8213   8233     /* Make the page containing the entry to be deleted writable. Then free any
  8214   8234     ** overflow pages associated with the entry and finally remove the cell
  8215   8235     ** itself from within the page.  */
  8216   8236     rc = sqlite3PagerWrite(pPage->pDbPage);
  8217   8237     if( rc ) return rc;
  8218         -  rc = clearCell(pPage, pCell, &szCell);
  8219         -  dropCell(pPage, iCellIdx, szCell, &rc);
         8238  +  rc = clearCell(pPage, pCell, &info);
         8239  +  dropCell(pPage, iCellIdx, info.nSize, &rc);
  8220   8240     if( rc ) return rc;
  8221   8241   
  8222   8242     /* If the cell deleted was not located on a leaf page, then the cursor
  8223   8243     ** is currently pointing to the largest entry in the sub-tree headed
  8224   8244     ** by the child-page of the cell that was just deleted from an internal
  8225   8245     ** node. The cell from the leaf node needs to be moved to the internal
  8226   8246     ** node to replace the deleted cell.  */
................................................................................
  8462   8482     int *pnChange            /* Add number of Cells freed to this counter */
  8463   8483   ){
  8464   8484     MemPage *pPage;
  8465   8485     int rc;
  8466   8486     unsigned char *pCell;
  8467   8487     int i;
  8468   8488     int hdr;
  8469         -  u16 szCell;
         8489  +  CellInfo info;
  8470   8490   
  8471   8491     assert( sqlite3_mutex_held(pBt->mutex) );
  8472   8492     if( pgno>btreePagecount(pBt) ){
  8473   8493       return SQLITE_CORRUPT_BKPT;
  8474   8494     }
  8475   8495     rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
  8476   8496     if( rc ) return rc;
................................................................................
  8482   8502     hdr = pPage->hdrOffset;
  8483   8503     for(i=0; i<pPage->nCell; i++){
  8484   8504       pCell = findCell(pPage, i);
  8485   8505       if( !pPage->leaf ){
  8486   8506         rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
  8487   8507         if( rc ) goto cleardatabasepage_out;
  8488   8508       }
  8489         -    rc = clearCell(pPage, pCell, &szCell);
         8509  +    rc = clearCell(pPage, pCell, &info);
  8490   8510       if( rc ) goto cleardatabasepage_out;
  8491   8511     }
  8492   8512     if( !pPage->leaf ){
  8493   8513       rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
  8494   8514       if( rc ) goto cleardatabasepage_out;
  8495   8515     }else if( pnChange ){
  8496   8516       assert( pPage->intKey || CORRUPT_DB );

Changes to src/btreeInt.h.

   255    255   */
   256    256   #define PTF_INTKEY    0x01
   257    257   #define PTF_ZERODATA  0x02
   258    258   #define PTF_LEAFDATA  0x04
   259    259   #define PTF_LEAF      0x08
   260    260   
   261    261   /*
   262         -** As each page of the file is loaded into memory, an instance of the following
   263         -** structure is appended and initialized to zero.  This structure stores
   264         -** information about the page that is decoded from the raw file page.
          262  +** An instance of this object stores information about each a single database
          263  +** page that has been loaded into memory.  The information in this object
          264  +** is derived from the raw on-disk page content.
   265    265   **
   266         -** The pParent field points back to the parent page.  This allows us to
   267         -** walk up the BTree from any leaf to the root.  Care must be taken to
   268         -** unref() the parent page pointer when this page is no longer referenced.
   269         -** The pageDestructor() routine handles that chore.
          266  +** As each database page is loaded into memory, the pager allocats an
          267  +** instance of this object and zeros the first 8 bytes.  (This is the
          268  +** "extra" information associated with each page of the pager.)
   270    269   **
   271    270   ** Access to all fields of this structure is controlled by the mutex
   272    271   ** stored in MemPage.pBt->mutex.
   273    272   */
   274    273   struct MemPage {
   275    274     u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
   276         -  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
          275  +  u8 bBusy;            /* Prevent endless loops on corrupt database files */
   277    276     u8 intKey;           /* True if table b-trees.  False for index b-trees */
   278    277     u8 intKeyLeaf;       /* True if the leaf of an intKey table */
          278  +  Pgno pgno;           /* Page number for this page */
          279  +  /* Only the first 8 bytes (above) are zeroed by pager.c when a new page
          280  +  ** is allocated. All fields that follow must be initialized before use */
   279    281     u8 leaf;             /* True if a leaf page */
   280    282     u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
   281    283     u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
   282    284     u8 max1bytePayload;  /* min(maxLocal,127) */
   283         -  u8 bBusy;            /* Prevent endless loops on corrupt database files */
          285  +  u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
   284    286     u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
   285    287     u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
   286    288     u16 cellOffset;      /* Index in aData of first cell pointer */
   287    289     u16 nFree;           /* Number of free bytes on the page */
   288    290     u16 nCell;           /* Number of cells on this page, local and ovfl */
   289    291     u16 maskPage;        /* Mask for page offset */
   290         -  u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
          292  +  u16 aiOvfl[4];       /* Insert the i-th overflow cell before the aiOvfl-th
   291    293                          ** non-overflow cell */
   292         -  u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
          294  +  u8 *apOvfl[4];       /* Pointers to the body of overflow cells */
   293    295     BtShared *pBt;       /* Pointer to BtShared that this page is part of */
   294    296     u8 *aData;           /* Pointer to disk image of the page data */
   295    297     u8 *aDataEnd;        /* One byte past the end of usable data */
   296    298     u8 *aCellIdx;        /* The cell index area */
   297    299     u8 *aDataOfst;       /* Same as aData for leaves.  aData+4 for interior */
   298    300     DbPage *pDbPage;     /* Pager page handle */
   299    301     u16 (*xCellSize)(MemPage*,u8*);             /* cellSizePtr method */
   300    302     void (*xParseCell)(MemPage*,u8*,CellInfo*); /* btreeParseCell method */
   301         -  Pgno pgno;           /* Page number for this page */
   302    303   };
   303    304   
   304         -/*
   305         -** The in-memory image of a disk page has the auxiliary information appended
   306         -** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
   307         -** that extra information.
   308         -*/
   309         -#define EXTRA_SIZE sizeof(MemPage)
   310         -
   311    305   /*
   312    306   ** A linked list of the following structures is stored at BtShared.pLock.
   313    307   ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor 
   314    308   ** is opened on the table with root page BtShared.iTable. Locks are removed
   315    309   ** from this list when a transaction is committed or rolled back, or when
   316    310   ** a btree handle is closed.
   317    311   */

Changes to src/ctime.c.

   229    229   #endif
   230    230   #if SQLITE_OMIT_BLOB_LITERAL
   231    231     "OMIT_BLOB_LITERAL",
   232    232   #endif
   233    233   #if SQLITE_OMIT_BTREECOUNT
   234    234     "OMIT_BTREECOUNT",
   235    235   #endif
   236         -#if SQLITE_OMIT_BUILTIN_TEST
   237         -  "OMIT_BUILTIN_TEST",
   238         -#endif
   239    236   #if SQLITE_OMIT_CAST
   240    237     "OMIT_CAST",
   241    238   #endif
   242    239   #if SQLITE_OMIT_CHECK
   243    240     "OMIT_CHECK",
   244    241   #endif
   245    242   #if SQLITE_OMIT_COMPLETE
................................................................................
   393    390     "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
   394    391   #endif
   395    392   #if SQLITE_TEST
   396    393     "TEST",
   397    394   #endif
   398    395   #if defined(SQLITE_THREADSAFE)
   399    396     "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
          397  +#endif
          398  +#if SQLITE_UNTESTABLE
          399  +  "UNTESTABLE"
   400    400   #endif
   401    401   #if SQLITE_USE_ALLOCA
   402    402     "USE_ALLOCA",
   403    403   #endif
   404    404   #if SQLITE_USER_AUTHENTICATION
   405    405     "USER_AUTHENTICATION",
   406    406   #endif

Changes to src/date.c.

    61     61   #endif
    62     62   
    63     63   /*
    64     64   ** A structure for holding a single date and time.
    65     65   */
    66     66   typedef struct DateTime DateTime;
    67     67   struct DateTime {
    68         -  sqlite3_int64 iJD; /* The julian day number times 86400000 */
    69         -  int Y, M, D;       /* Year, month, and day */
    70         -  int h, m;          /* Hour and minutes */
    71         -  int tz;            /* Timezone offset in minutes */
    72         -  double s;          /* Seconds */
    73         -  char validYMD;     /* True (1) if Y,M,D are valid */
    74         -  char validHMS;     /* True (1) if h,m,s are valid */
    75         -  char validJD;      /* True (1) if iJD is valid */
    76         -  char validTZ;      /* True (1) if tz is valid */
    77         -  char tzSet;        /* Timezone was set explicitly */
           68  +  sqlite3_int64 iJD;  /* The julian day number times 86400000 */
           69  +  int Y, M, D;        /* Year, month, and day */
           70  +  int h, m;           /* Hour and minutes */
           71  +  int tz;             /* Timezone offset in minutes */
           72  +  double s;           /* Seconds */
           73  +  char validJD;       /* True (1) if iJD is valid */
           74  +  char rawS;          /* Raw numeric value stored in s */
           75  +  char validYMD;      /* True (1) if Y,M,D are valid */
           76  +  char validHMS;      /* True (1) if h,m,s are valid */
           77  +  char validTZ;       /* True (1) if tz is valid */
           78  +  char tzSet;         /* Timezone was set explicitly */
           79  +  char isError;       /* An overflow has occurred */
    78     80   };
    79     81   
    80     82   
    81     83   /*
    82     84   ** Convert zDate into one or more integers according to the conversion
    83     85   ** specifier zFormat.
    84     86   **
................................................................................
   218    220         }
   219    221         ms /= rScale;
   220    222       }
   221    223     }else{
   222    224       s = 0;
   223    225     }
   224    226     p->validJD = 0;
          227  +  p->rawS = 0;
   225    228     p->validHMS = 1;
   226    229     p->h = h;
   227    230     p->m = m;
   228    231     p->s = s + ms;
   229    232     if( parseTimezone(zDate, p) ) return 1;
   230    233     p->validTZ = (p->tz!=0)?1:0;
   231    234     return 0;
   232    235   }
          236  +
          237  +/*
          238  +** Put the DateTime object into its error state.
          239  +*/
          240  +static void datetimeError(DateTime *p){
          241  +  memset(p, 0, sizeof(*p));
          242  +  p->isError = 1;
          243  +}
   233    244   
   234    245   /*
   235    246   ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
   236    247   ** that the YYYY-MM-DD is according to the Gregorian calendar.
   237    248   **
   238    249   ** Reference:  Meeus page 61
   239    250   */
................................................................................
   245    256       Y = p->Y;
   246    257       M = p->M;
   247    258       D = p->D;
   248    259     }else{
   249    260       Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
   250    261       M = 1;
   251    262       D = 1;
          263  +  }
          264  +  if( Y<-4713 || Y>9999 || p->rawS ){
          265  +    datetimeError(p);
          266  +    return;
   252    267     }
   253    268     if( M<=2 ){
   254    269       Y--;
   255    270       M += 12;
   256    271     }
   257    272     A = Y/100;
   258    273     B = 2 - A + (A/4);
................................................................................
   325    340     if( p->iJD>0 ){
   326    341       p->validJD = 1;
   327    342       return 0;
   328    343     }else{
   329    344       return 1;
   330    345     }
   331    346   }
          347  +
          348  +/*
          349  +** Input "r" is a numeric quantity which might be a julian day number,
          350  +** or the number of seconds since 1970.  If the value if r is within
          351  +** range of a julian day number, install it as such and set validJD.
          352  +** If the value is a valid unix timestamp, put it in p->s and set p->rawS.
          353  +*/
          354  +static void setRawDateNumber(DateTime *p, double r){
          355  +  p->s = r;
          356  +  p->rawS = 1;
          357  +  if( r>=0.0 && r<5373484.5 ){
          358  +    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
          359  +    p->validJD = 1;
          360  +  }
          361  +}
   332    362   
   333    363   /*
   334    364   ** Attempt to parse the given string into a julian day number.  Return
   335    365   ** the number of errors.
   336    366   **
   337    367   ** The following are acceptable forms for the input string:
   338    368   **
................................................................................
   355    385     if( parseYyyyMmDd(zDate,p)==0 ){
   356    386       return 0;
   357    387     }else if( parseHhMmSs(zDate, p)==0 ){
   358    388       return 0;
   359    389     }else if( sqlite3StrICmp(zDate,"now")==0){
   360    390       return setDateTimeToCurrent(context, p);
   361    391     }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
   362         -    p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
   363         -    p->validJD = 1;
          392  +    setRawDateNumber(p, r);
   364    393       return 0;
   365    394     }
   366    395     return 1;
   367    396   }
          397  +
          398  +/*
          399  +** Return TRUE if the given julian day number is within range.
          400  +**
          401  +** The input is the JulianDay times 86400000.
          402  +*/
          403  +static int validJulianDay(sqlite3_int64 iJD){
          404  +  return iJD>=0 && iJD<=464269060799999;
          405  +}
   368    406   
   369    407   /*
   370    408   ** Compute the Year, Month, and Day from the julian day number.
   371    409   */
   372    410   static void computeYMD(DateTime *p){
   373    411     int Z, A, B, C, D, E, X1;
   374    412     if( p->validYMD ) return;
   375    413     if( !p->validJD ){
   376    414       p->Y = 2000;
   377    415       p->M = 1;
   378    416       p->D = 1;
   379    417     }else{
          418  +    assert( validJulianDay(p->iJD) );
   380    419       Z = (int)((p->iJD + 43200000)/86400000);
   381    420       A = (int)((Z - 1867216.25)/36524.25);
   382    421       A = Z + 1 + A - (A/4);
   383    422       B = A + 1524;
   384    423       C = (int)((B - 122.1)/365.25);
   385    424       D = (36525*(C&32767))/100;
   386    425       E = (int)((B-D)/30.6001);
................................................................................
   403    442     p->s = s/1000.0;
   404    443     s = (int)p->s;
   405    444     p->s -= s;
   406    445     p->h = s/3600;
   407    446     s -= p->h*3600;
   408    447     p->m = s/60;
   409    448     p->s += s - p->m*60;
          449  +  p->rawS = 0;
   410    450     p->validHMS = 1;
   411    451   }
   412    452   
   413    453   /*
   414    454   ** Compute both YMD and HMS
   415    455   */
   416    456   static void computeYMD_HMS(DateTime *p){
................................................................................
   464    504   #if !HAVE_LOCALTIME_R && !HAVE_LOCALTIME_S
   465    505     struct tm *pX;
   466    506   #if SQLITE_THREADSAFE>0
   467    507     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
   468    508   #endif
   469    509     sqlite3_mutex_enter(mutex);
   470    510     pX = localtime(t);
   471         -#ifndef SQLITE_OMIT_BUILTIN_TEST
          511  +#ifndef SQLITE_UNTESTABLE
   472    512     if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
   473    513   #endif
   474    514     if( pX ) *pTm = *pX;
   475    515     sqlite3_mutex_leave(mutex);
   476    516     rc = pX==0;
   477    517   #else
   478         -#ifndef SQLITE_OMIT_BUILTIN_TEST
          518  +#ifndef SQLITE_UNTESTABLE
   479    519     if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
   480    520   #endif
   481    521   #if HAVE_LOCALTIME_R
   482    522     rc = localtime_r(t, pTm)==0;
   483    523   #else
   484    524     rc = localtime_s(pTm, t);
   485    525   #endif /* HAVE_LOCALTIME_R */
................................................................................
   542    582     y.D = sLocal.tm_mday;
   543    583     y.h = sLocal.tm_hour;
   544    584     y.m = sLocal.tm_min;
   545    585     y.s = sLocal.tm_sec;
   546    586     y.validYMD = 1;
   547    587     y.validHMS = 1;
   548    588     y.validJD = 0;
          589  +  y.rawS = 0;
   549    590     y.validTZ = 0;
          591  +  y.isError = 0;
   550    592     computeJD(&y);
   551    593     *pRc = SQLITE_OK;
   552    594     return y.iJD - x.iJD;
   553    595   }
   554    596   #endif /* SQLITE_OMIT_LOCALTIME */
          597  +
          598  +/*
          599  +** The following table defines various date transformations of the form
          600  +**
          601  +**            'NNN days'
          602  +**
          603  +** Where NNN is an arbitrary floating-point number and "days" can be one
          604  +** of several units of time.
          605  +*/
          606  +static const struct {
          607  +  u8 eType;           /* Transformation type code */
          608  +  u8 nName;           /* Length of th name */
          609  +  char *zName;        /* Name of the transformation */
          610  +  double rLimit;      /* Maximum NNN value for this transform */
          611  +  double rXform;      /* Constant used for this transform */
          612  +} aXformType[] = {
          613  +  { 0, 6, "second", 464269060800.0, 86400000.0/(24.0*60.0*60.0) },
          614  +  { 0, 6, "minute", 7737817680.0,   86400000.0/(24.0*60.0)      },
          615  +  { 0, 4, "hour",   128963628.0,    86400000.0/24.0             },
          616  +  { 0, 3, "day",    5373485.0,      86400000.0                  },
          617  +  { 1, 5, "month",  176546.0,       30.0*86400000.0             },
          618  +  { 2, 4, "year",   14713.0,        365.0*86400000.0            },
          619  +};
   555    620   
   556    621   /*
   557    622   ** Process a modifier to a date-time stamp.  The modifiers are
   558    623   ** as follows:
   559    624   **
   560    625   **     NNN days
   561    626   **     NNN hours
................................................................................
   573    638   **     utc
   574    639   **
   575    640   ** Return 0 on success and 1 if there is any kind of error. If the error
   576    641   ** is in a system call (i.e. localtime()), then an error message is written
   577    642   ** to context pCtx. If the error is an unrecognized modifier, no error is
   578    643   ** written to pCtx.
   579    644   */
   580         -static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
          645  +static int parseModifier(
          646  +  sqlite3_context *pCtx,      /* Function context */
          647  +  const char *z,              /* The text of the modifier */
          648  +  int n,                      /* Length of zMod in bytes */
          649  +  DateTime *p                 /* The date/time value to be modified */
          650  +){
   581    651     int rc = 1;
   582         -  int n;
   583    652     double r;
   584         -  char *z, zBuf[30];
   585         -  z = zBuf;
   586         -  for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
   587         -    z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
   588         -  }
   589         -  z[n] = 0;
   590         -  switch( z[0] ){
          653  +  switch(sqlite3UpperToLower[(u8)z[0]] ){
   591    654   #ifndef SQLITE_OMIT_LOCALTIME
   592    655       case 'l': {
   593    656         /*    localtime
   594    657         **
   595    658         ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
   596    659         ** show local time.
   597    660         */
   598         -      if( strcmp(z, "localtime")==0 ){
          661  +      if( sqlite3_stricmp(z, "localtime")==0 ){
   599    662           computeJD(p);
   600    663           p->iJD += localtimeOffset(p, pCtx, &rc);
   601    664           clearYMD_HMS_TZ(p);
   602    665         }
   603    666         break;
   604    667       }
   605    668   #endif
   606    669       case 'u': {
   607    670         /*
   608    671         **    unixepoch
   609    672         **
   610         -      ** Treat the current value of p->iJD as the number of
          673  +      ** Treat the current value of p->s as the number of
   611    674         ** seconds since 1970.  Convert to a real julian day number.
   612    675         */
   613         -      if( strcmp(z, "unixepoch")==0 && p->validJD ){
   614         -        p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
   615         -        clearYMD_HMS_TZ(p);
   616         -        rc = 0;
          676  +      if( sqlite3_stricmp(z, "unixepoch")==0 && p->rawS ){
          677  +        r = p->s*1000.0 + 210866760000000.0;
          678  +        if( r>=0.0 && r<464269060800000.0 ){
          679  +          clearYMD_HMS_TZ(p);
          680  +          p->iJD = (sqlite3_int64)r;
          681  +          p->validJD = 1;
          682  +          p->rawS = 0;
          683  +          rc = 0;
          684  +        }
   617    685         }
   618    686   #ifndef SQLITE_OMIT_LOCALTIME
   619         -      else if( strcmp(z, "utc")==0 ){
          687  +      else if( sqlite3_stricmp(z, "utc")==0 ){
   620    688           if( p->tzSet==0 ){
   621    689             sqlite3_int64 c1;
   622    690             computeJD(p);
   623    691             c1 = localtimeOffset(p, pCtx, &rc);
   624    692             if( rc==SQLITE_OK ){
   625    693               p->iJD -= c1;
   626    694               clearYMD_HMS_TZ(p);
................................................................................
   638    706         /*
   639    707         **    weekday N
   640    708         **
   641    709         ** Move the date to the same time on the next occurrence of
   642    710         ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
   643    711         ** date is already on the appropriate weekday, this is a no-op.
   644    712         */
   645         -      if( strncmp(z, "weekday ", 8)==0
          713  +      if( sqlite3_strnicmp(z, "weekday ", 8)==0
   646    714                  && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
   647    715                  && (n=(int)r)==r && n>=0 && r<7 ){
   648    716           sqlite3_int64 Z;
   649    717           computeYMD_HMS(p);
   650    718           p->validTZ = 0;
   651    719           p->validJD = 0;
   652    720           computeJD(p);
................................................................................
   661    729       case 's': {
   662    730         /*
   663    731         **    start of TTTTT
   664    732         **
   665    733         ** Move the date backwards to the beginning of the current day,
   666    734         ** or month or year.
   667    735         */
   668         -      if( strncmp(z, "start of ", 9)!=0 ) break;
          736  +      if( sqlite3_strnicmp(z, "start of ", 9)!=0 ) break;
   669    737         z += 9;
   670    738         computeYMD(p);
   671    739         p->validHMS = 1;
   672    740         p->h = p->m = 0;
   673    741         p->s = 0.0;
   674    742         p->validTZ = 0;
   675    743         p->validJD = 0;
   676         -      if( strcmp(z,"month")==0 ){
          744  +      if( sqlite3_stricmp(z,"month")==0 ){
   677    745           p->D = 1;
   678    746           rc = 0;
   679         -      }else if( strcmp(z,"year")==0 ){
          747  +      }else if( sqlite3_stricmp(z,"year")==0 ){
   680    748           computeYMD(p);
   681    749           p->M = 1;
   682    750           p->D = 1;
   683    751           rc = 0;
   684         -      }else if( strcmp(z,"day")==0 ){
          752  +      }else if( sqlite3_stricmp(z,"day")==0 ){
   685    753           rc = 0;
   686    754         }
   687    755         break;
   688    756       }
   689    757       case '+':
   690    758       case '-':
   691    759       case '0':
................................................................................
   695    763       case '4':
   696    764       case '5':
   697    765       case '6':
   698    766       case '7':
   699    767       case '8':
   700    768       case '9': {
   701    769         double rRounder;
          770  +      int i;
   702    771         for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
   703    772         if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
   704    773           rc = 1;
   705    774           break;
   706    775         }
   707    776         if( z[n]==':' ){
   708    777           /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
................................................................................
   723    792           if( z[0]=='-' ) tx.iJD = -tx.iJD;
   724    793           computeJD(p);
   725    794           clearYMD_HMS_TZ(p);
   726    795           p->iJD += tx.iJD;
   727    796           rc = 0;
   728    797           break;
   729    798         }
          799  +
          800  +      /* If control reaches this point, it means the transformation is
          801  +      ** one of the forms like "+NNN days".  */
   730    802         z += n;
   731    803         while( sqlite3Isspace(*z) ) z++;
   732    804         n = sqlite3Strlen30(z);
   733    805         if( n>10 || n<3 ) break;
   734         -      if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
          806  +      if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
   735    807         computeJD(p);
   736         -      rc = 0;
          808  +      rc = 1;
   737    809         rRounder = r<0 ? -0.5 : +0.5;
   738         -      if( n==3 && strcmp(z,"day")==0 ){
   739         -        p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
   740         -      }else if( n==4 && strcmp(z,"hour")==0 ){
   741         -        p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
   742         -      }else if( n==6 && strcmp(z,"minute")==0 ){
   743         -        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
   744         -      }else if( n==6 && strcmp(z,"second")==0 ){
   745         -        p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
   746         -      }else if( n==5 && strcmp(z,"month")==0 ){
   747         -        int x, y;
   748         -        computeYMD_HMS(p);
   749         -        p->M += (int)r;
   750         -        x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
   751         -        p->Y += x;
   752         -        p->M -= x*12;
   753         -        p->validJD = 0;
   754         -        computeJD(p);
   755         -        y = (int)r;
   756         -        if( y!=r ){
   757         -          p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
   758         -        }
   759         -      }else if( n==4 && strcmp(z,"year")==0 ){
   760         -        int y = (int)r;
   761         -        computeYMD_HMS(p);
   762         -        p->Y += y;
   763         -        p->validJD = 0;
   764         -        computeJD(p);
   765         -        if( y!=r ){
   766         -          p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
   767         -        }
   768         -      }else{
   769         -        rc = 1;
          810  +      for(i=0; i<ArraySize(aXformType); i++){
          811  +        if( aXformType[i].nName==n
          812  +         && sqlite3_strnicmp(aXformType[i].zName, z, n)==0
          813  +         && r>-aXformType[i].rLimit && r<aXformType[i].rLimit
          814  +        ){
          815  +          switch( aXformType[i].eType ){
          816  +            case 1: { /* Special processing to add months */
          817  +              int x;
          818  +              computeYMD_HMS(p);
          819  +              p->M += (int)r;
          820  +              x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
          821  +              p->Y += x;
          822  +              p->M -= x*12;
          823  +              p->validJD = 0;
          824  +              r -= (int)r;
          825  +              break;
          826  +            }
          827  +            case 2: { /* Special processing to add years */
          828  +              int y = (int)r;
          829  +              computeYMD_HMS(p);
          830  +              p->Y += y;
          831  +              p->validJD = 0;
          832  +              r -= (int)r;
          833  +              break;
          834  +            }
          835  +          }
          836  +          computeJD(p);
          837  +          p->iJD += (sqlite3_int64)(r*aXformType[i].rXform + rRounder);
          838  +          rc = 0;
          839  +          break;
          840  +        }
   770    841         }
   771    842         clearYMD_HMS_TZ(p);
   772    843         break;
   773    844       }
   774    845       default: {
   775    846         break;
   776    847       }
................................................................................
   789    860   */
   790    861   static int isDate(
   791    862     sqlite3_context *context, 
   792    863     int argc, 
   793    864     sqlite3_value **argv, 
   794    865     DateTime *p
   795    866   ){
   796         -  int i;
          867  +  int i, n;
   797    868     const unsigned char *z;
   798    869     int eType;
   799    870     memset(p, 0, sizeof(*p));
   800    871     if( argc==0 ){
   801    872       return setDateTimeToCurrent(context, p);
   802    873     }
   803    874     if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
   804    875                      || eType==SQLITE_INTEGER ){
   805         -    p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
   806         -    p->validJD = 1;
          876  +    setRawDateNumber(p, sqlite3_value_double(argv[0]));
   807    877     }else{
   808    878       z = sqlite3_value_text(argv[0]);
   809    879       if( !z || parseDateOrTime(context, (char*)z, p) ){
   810    880         return 1;
   811    881       }
   812    882     }
   813    883     for(i=1; i<argc; i++){
   814    884       z = sqlite3_value_text(argv[i]);
   815         -    if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
          885  +    n = sqlite3_value_bytes(argv[i]);
          886  +    if( z==0 || parseModifier(context, (char*)z, n, p) ) return 1;
   816    887     }
          888  +  computeJD(p);
          889  +  if( p->isError || !validJulianDay(p->iJD) ) return 1;
   817    890     return 0;
   818    891   }
   819    892   
   820    893   
   821    894   /*
   822    895   ** The following routines implement the various date and time functions
   823    896   ** of SQLite.

Changes to src/delete.c.

   160    160     **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
   161    161     ** becomes:
   162    162     **   DELETE FROM table_a WHERE rowid IN ( 
   163    163     **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
   164    164     **   );
   165    165     */
   166    166   
   167         -  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
          167  +  pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0);
   168    168     if( pSelectRowid == 0 ) goto limit_where_cleanup;
   169    169     pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
   170    170     if( pEList == 0 ) goto limit_where_cleanup;
   171    171   
   172    172     /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
   173    173     ** and the SELECT subtree. */
   174    174     pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
................................................................................
   179    179   
   180    180     /* generate the SELECT expression tree. */
   181    181     pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
   182    182                                pOrderBy,0,pLimit,pOffset);
   183    183     if( pSelect == 0 ) return 0;
   184    184   
   185    185     /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
   186         -  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
   187         -  pInClause = pWhereRowid ? sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0) : 0;
          186  +  pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0);
          187  +  pInClause = pWhereRowid ? sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0) : 0;
   188    188     sqlite3PExprAddSelect(pParse, pInClause, pSelect);
   189    189     return pInClause;
   190    190   
   191    191   limit_where_cleanup:
   192    192     sqlite3ExprDelete(pParse->db, pWhere);
   193    193     sqlite3ExprListDelete(pParse->db, pOrderBy);
   194    194     sqlite3ExprDelete(pParse->db, pLimit);
................................................................................
   708    708     ** the update-hook is not invoked for rows removed by REPLACE, but the 
   709    709     ** pre-update-hook is.
   710    710     */ 
   711    711     if( pTab->pSelect==0 ){
   712    712       u8 p5 = 0;
   713    713       sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
   714    714       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
   715         -    sqlite3VdbeChangeP4(v, -1, (char*)pTab, P4_TABLE);
          715  +    sqlite3VdbeAppendP4(v, (char*)pTab, P4_TABLE);
   716    716       if( eMode!=ONEPASS_OFF ){
   717    717         sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
   718    718       }
   719    719       if( iIdxNoSeek>=0 ){
   720    720         sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
   721    721       }
   722    722       if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;

Changes to src/expr.c.

   423    423       ** sqlite3ExprDelete() specifically skips the recursive delete of
   424    424       ** pLeft on TK_SELECT_COLUMN nodes.  But pRight is followed, so pVector
   425    425       ** can be attached to pRight to cause this node to take ownership of
   426    426       ** pVector.  Typically there will be multiple TK_SELECT_COLUMN nodes
   427    427       ** with the same pLeft pointer to the pVector, but only one of them
   428    428       ** will own the pVector.
   429    429       */
   430         -    pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0, 0);
          430  +    pRet = sqlite3PExpr(pParse, TK_SELECT_COLUMN, 0, 0);
   431    431       if( pRet ){
   432    432         pRet->iColumn = iField;
   433    433         pRet->pLeft = pVector;
   434    434       }
   435    435       assert( pRet==0 || pRet->iTable==0 );
   436    436     }else{
   437    437       if( pVector->op==TK_VECTOR ) pVector = pVector->x.pList->a[iField].pExpr;
................................................................................
   815    815   ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
   816    816   ** free the subtrees and return NULL.
   817    817   */
   818    818   Expr *sqlite3PExpr(
   819    819     Parse *pParse,          /* Parsing context */
   820    820     int op,                 /* Expression opcode */
   821    821     Expr *pLeft,            /* Left operand */
   822         -  Expr *pRight,           /* Right operand */
   823         -  const Token *pToken     /* Argument token */
          822  +  Expr *pRight            /* Right operand */
   824    823   ){
   825    824     Expr *p;
   826    825     if( op==TK_AND && pParse->nErr==0 ){
   827    826       /* Take advantage of short-circuit false optimization for AND */
   828    827       p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
   829    828     }else{
   830         -    p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1);
          829  +    p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
          830  +    if( p ){
          831  +      memset(p, 0, sizeof(Expr));
          832  +      p->op = op & TKFLG_MASK;
          833  +      p->iAgg = -1;
          834  +    }
   831    835       sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
   832    836     }
   833    837     if( p ) {
   834    838       sqlite3ExprCheckHeight(pParse, p->nHeight);
   835    839     }
   836    840     return p;
   837    841   }
................................................................................
  2346   2350   **   "sub-select returns N columns - expected M"
  2347   2351   */   
  2348   2352   void sqlite3SubselectError(Parse *pParse, int nActual, int nExpect){
  2349   2353     const char *zFmt = "sub-select returns %d columns - expected %d";
  2350   2354     sqlite3ErrorMsg(pParse, zFmt, nActual, nExpect);
  2351   2355   }
  2352   2356   #endif
         2357  +
         2358  +/*
         2359  +** Expression pExpr is a vector that has been used in a context where
         2360  +** it is not permitted. If pExpr is a sub-select vector, this routine 
         2361  +** loads the Parse object with a message of the form:
         2362  +**
         2363  +**   "sub-select returns N columns - expected 1"
         2364  +**
         2365  +** Or, if it is a regular scalar vector:
         2366  +**
         2367  +**   "row value misused"
         2368  +*/   
         2369  +void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
         2370  +#ifndef SQLITE_OMIT_SUBQUERY
         2371  +  if( pExpr->flags & EP_xIsSelect ){
         2372  +    sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
         2373  +  }else
         2374  +#endif
         2375  +  {
         2376  +    sqlite3ErrorMsg(pParse, "row value misused");
         2377  +  }
         2378  +}
  2353   2379   
  2354   2380   /*
  2355   2381   ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
  2356   2382   ** or IN operators.  Examples:
  2357   2383   **
  2358   2384   **     (SELECT a FROM b)          -- subquery
  2359   2385   **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
................................................................................
  2629   2655     int nVector = sqlite3ExprVectorSize(pIn->pLeft);
  2630   2656     if( (pIn->flags & EP_xIsSelect) ){
  2631   2657       if( nVector!=pIn->x.pSelect->pEList->nExpr ){
  2632   2658         sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
  2633   2659         return 1;
  2634   2660       }
  2635   2661     }else if( nVector!=1 ){
  2636         -    if( (pIn->pLeft->flags & EP_xIsSelect) ){
  2637         -      sqlite3SubselectError(pParse, nVector, 1);
  2638         -    }else{
  2639         -      sqlite3ErrorMsg(pParse, "row value misused");
  2640         -    }
         2662  +    sqlite3VectorErrorMsg(pParse, pIn->pLeft);
  2641   2663       return 1;
  2642   2664     }
  2643   2665     return 0;
  2644   2666   }
  2645   2667   #endif
  2646   2668   
  2647   2669   #ifndef SQLITE_OMIT_SUBQUERY
................................................................................
  2938   2960       sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
  2939   2961     }else{
  2940   2962       int c;
  2941   2963       i64 value;
  2942   2964       const char *z = pExpr->u.zToken;
  2943   2965       assert( z!=0 );
  2944   2966       c = sqlite3DecOrHexToI64(z, &value);
  2945         -    if( c==0 || (c==2 && negFlag) ){
  2946         -      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
  2947         -      sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
  2948         -    }else{
         2967  +    if( c==1 || (c==2 && !negFlag) || (negFlag && value==SMALLEST_INT64)){
  2949   2968   #ifdef SQLITE_OMIT_FLOATING_POINT
  2950   2969         sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
  2951   2970   #else
  2952   2971   #ifndef SQLITE_OMIT_HEX_INTEGER
  2953   2972         if( sqlite3_strnicmp(z,"0x",2)==0 ){
  2954         -        sqlite3ErrorMsg(pParse, "hex literal too big: %s", z);
         2973  +        sqlite3ErrorMsg(pParse, "hex literal too big: %s%s", negFlag?"-":"",z);
  2955   2974         }else
  2956   2975   #endif
  2957   2976         {
  2958   2977           codeReal(v, z, negFlag, iMem);
  2959   2978         }
  2960   2979   #endif
         2980  +    }else{
         2981  +      if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
         2982  +      sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64);
  2961   2983       }
  2962   2984     }
  2963   2985   }
  2964   2986   
  2965   2987   /*
  2966   2988   ** Erase column-cache entry number i
  2967   2989   */
................................................................................
  3406   3428         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  3407   3429         assert( pExpr->u.zToken!=0 );
  3408   3430         assert( pExpr->u.zToken[0]!=0 );
  3409   3431         sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
  3410   3432         if( pExpr->u.zToken[1]!=0 ){
  3411   3433           assert( pExpr->u.zToken[0]=='?' 
  3412   3434                || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
  3413         -        sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
         3435  +        sqlite3VdbeAppendP4(v, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
  3414   3436         }
  3415   3437         return target;
  3416   3438       }
  3417   3439       case TK_REGISTER: {
  3418   3440         return pExpr->iTable;
  3419   3441       }
  3420   3442   #ifndef SQLITE_OMIT_CAST

Changes to src/fault.c.

    22     22   ** is completely recoverable simply by not carrying out the resize. The 
    23     23   ** hash table will continue to function normally.  So a malloc failure 
    24     24   ** during a hash table resize is a benign fault.
    25     25   */
    26     26   
    27     27   #include "sqliteInt.h"
    28     28   
    29         -#ifndef SQLITE_OMIT_BUILTIN_TEST
           29  +#ifndef SQLITE_UNTESTABLE
    30     30   
    31     31   /*
    32     32   ** Global variables.
    33     33   */
    34     34   typedef struct BenignMallocHooks BenignMallocHooks;
    35     35   static SQLITE_WSD struct BenignMallocHooks {
    36     36     void (*xBenignBegin)(void);
................................................................................
    80     80   void sqlite3EndBenignMalloc(void){
    81     81     wsdHooksInit;
    82     82     if( wsdHooks.xBenignEnd ){
    83     83       wsdHooks.xBenignEnd();
    84     84     }
    85     85   }
    86     86   
    87         -#endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
           87  +#endif   /* #ifndef SQLITE_UNTESTABLE */

Changes to src/fkey.c.

   580    580   
   581    581       iCol = pIdx ? pIdx->aiColumn[i] : -1;
   582    582       pLeft = exprTableRegister(pParse, pTab, regData, iCol);
   583    583       iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
   584    584       assert( iCol>=0 );
   585    585       zCol = pFKey->pFrom->aCol[iCol].zName;
   586    586       pRight = sqlite3Expr(db, TK_ID, zCol);
   587         -    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
          587  +    pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
   588    588       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
   589    589     }
   590    590   
   591    591     /* If the child table is the same as the parent table, then add terms
   592    592     ** to the WHERE clause that prevent this entry from being scanned.
   593    593     ** The added WHERE clause terms are like this:
   594    594     **
................................................................................
   602    602     if( pTab==pFKey->pFrom && nIncr>0 ){
   603    603       Expr *pNe;                    /* Expression (pLeft != pRight) */
   604    604       Expr *pLeft;                  /* Value from parent table row */
   605    605       Expr *pRight;                 /* Column ref to child table */
   606    606       if( HasRowid(pTab) ){
   607    607         pLeft = exprTableRegister(pParse, pTab, regData, -1);
   608    608         pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
   609         -      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
          609  +      pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight);
   610    610       }else{
   611    611         Expr *pEq, *pAll = 0;
   612    612         Index *pPk = sqlite3PrimaryKeyIndex(pTab);
   613    613         assert( pIdx!=0 );
   614    614         for(i=0; i<pPk->nKeyCol; i++){
   615    615           i16 iCol = pIdx->aiColumn[i];
   616    616           assert( iCol>=0 );
   617    617           pLeft = exprTableRegister(pParse, pTab, regData, iCol);
   618    618           pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
   619         -        pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
          619  +        pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
   620    620           pAll = sqlite3ExprAnd(db, pAll, pEq);
   621    621         }
   622         -      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
          622  +      pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
   623    623       }
   624    624       pWhere = sqlite3ExprAnd(db, pWhere, pNe);
   625    625     }
   626    626   
   627    627     /* Resolve the references in the WHERE clause. */
   628    628     memset(&sNameContext, 0, sizeof(NameContext));
   629    629     sNameContext.pSrcList = pSrc;
................................................................................
  1201   1201         /* Create the expression "OLD.zToCol = zFromCol". It is important
  1202   1202         ** that the "OLD.zToCol" term is on the LHS of the = operator, so
  1203   1203         ** that the affinity and collation sequence associated with the
  1204   1204         ** parent table are used for the comparison. */
  1205   1205         pEq = sqlite3PExpr(pParse, TK_EQ,
  1206   1206             sqlite3PExpr(pParse, TK_DOT, 
  1207   1207               sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
  1208         -            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
  1209         -          , 0),
         1208  +            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
  1210   1209             sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
  1211         -      , 0);
         1210  +      );
  1212   1211         pWhere = sqlite3ExprAnd(db, pWhere, pEq);
  1213   1212   
  1214   1213         /* For ON UPDATE, construct the next term of the WHEN clause.
  1215   1214         ** The final WHEN clause will be like this:
  1216   1215         **
  1217   1216         **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
  1218   1217         */
  1219   1218         if( pChanges ){
  1220   1219           pEq = sqlite3PExpr(pParse, TK_IS,
  1221   1220               sqlite3PExpr(pParse, TK_DOT, 
  1222   1221                 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
  1223         -              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
  1224         -              0),
         1222  +              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
  1225   1223               sqlite3PExpr(pParse, TK_DOT, 
  1226   1224                 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
  1227         -              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0),
  1228         -              0),
  1229         -            0);
         1225  +              sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
         1226  +            );
  1230   1227           pWhen = sqlite3ExprAnd(db, pWhen, pEq);
  1231   1228         }
  1232   1229     
  1233   1230         if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
  1234   1231           Expr *pNew;
  1235   1232           if( action==OE_Cascade ){
  1236   1233             pNew = sqlite3PExpr(pParse, TK_DOT, 
  1237   1234               sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
  1238         -            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)
  1239         -          , 0);
         1235  +            sqlite3ExprAlloc(db, TK_ID, &tToCol, 0));
  1240   1236           }else if( action==OE_SetDflt ){
  1241   1237             Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
  1242   1238             if( pDflt ){
  1243   1239               pNew = sqlite3ExprDup(db, pDflt, 0);
  1244   1240             }else{
  1245   1241               pNew = sqlite3ExprAlloc(db, TK_NULL, 0, 0);
  1246   1242             }
................................................................................
  1288   1284         pStep->zTarget = (char *)&pStep[1];
  1289   1285         memcpy((char *)pStep->zTarget, zFrom, nFrom);
  1290   1286     
  1291   1287         pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
  1292   1288         pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
  1293   1289         pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
  1294   1290         if( pWhen ){
  1295         -        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
         1291  +        pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0);
  1296   1292           pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
  1297   1293         }
  1298   1294       }
  1299   1295   
  1300   1296       /* Re-enable the lookaside buffer, if it was disabled earlier. */
  1301   1297       db->lookaside.bDisable--;
  1302   1298   

Changes to src/func.c.

   594    594   ** case.  Thus  'a' LIKE 'A' would be true. */
   595    595   static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
   596    596   /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
   597    597   ** is case sensitive causing 'a' LIKE 'A' to be false */
   598    598   static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
   599    599   
   600    600   /*
   601         -** Compare two UTF-8 strings for equality where the first string can
   602         -** potentially be a "glob" or "like" expression.  Return true (1) if they
   603         -** are the same and false (0) if they are different.
          601  +** Possible error returns from patternMatch()
          602  +*/
          603  +#define SQLITE_MATCH             0
          604  +#define SQLITE_NOMATCH           1
          605  +#define SQLITE_NOWILDCARDMATCH   2
          606  +
          607  +/*
          608  +** Compare two UTF-8 strings for equality where the first string is
          609  +** a GLOB or LIKE expression.  Return values:
          610  +**
          611  +**    SQLITE_MATCH:            Match
          612  +**    SQLITE_NOMATCH:          No match
          613  +**    SQLITE_NOWILDCARDMATCH:  No match in spite of having * or % wildcards.
   604    614   **
   605    615   ** Globbing rules:
   606    616   **
   607    617   **      '*'       Matches any sequence of zero or more characters.
   608    618   **
   609    619   **      '?'       Matches exactly one character.
   610    620   **
................................................................................
   647    657     while( (c = Utf8Read(zPattern))!=0 ){
   648    658       if( c==matchAll ){  /* Match "*" */
   649    659         /* Skip over multiple "*" characters in the pattern.  If there
   650    660         ** are also "?" characters, skip those as well, but consume a
   651    661         ** single character of the input string for each "?" skipped */
   652    662         while( (c=Utf8Read(zPattern)) == matchAll || c == matchOne ){
   653    663           if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
   654         -          return 0;
          664  +          return SQLITE_NOWILDCARDMATCH;
   655    665           }
   656    666         }
   657    667         if( c==0 ){
   658         -        return 1;   /* "*" at the end of the pattern matches */
          668  +        return SQLITE_MATCH;   /* "*" at the end of the pattern matches */
   659    669         }else if( c==matchOther ){
   660    670           if( pInfo->matchSet==0 ){
   661    671             c = sqlite3Utf8Read(&zPattern);
   662         -          if( c==0 ) return 0;
          672  +          if( c==0 ) return SQLITE_NOWILDCARDMATCH;
   663    673           }else{
   664    674             /* "[...]" immediately follows the "*".  We have to do a slow
   665    675             ** recursive search in this case, but it is an unusual case. */
   666    676             assert( matchOther<0x80 );  /* '[' is a single-byte character */
   667         -          while( *zString
   668         -                 && patternCompare(&zPattern[-1],zString,pInfo,matchOther)==0 ){
          677  +          while( *zString ){
          678  +            int bMatch = patternCompare(&zPattern[-1],zString,pInfo,matchOther);
          679  +            if( bMatch!=SQLITE_NOMATCH ) return bMatch;
   669    680               SQLITE_SKIP_UTF8(zString);
   670    681             }
   671         -          return *zString!=0;
          682  +          return SQLITE_NOWILDCARDMATCH;
   672    683           }
   673    684         }
   674    685   
   675    686         /* At this point variable c contains the first character of the
   676    687         ** pattern string past the "*".  Search in the input string for the
   677         -      ** first matching character and recursively contine the match from
          688  +      ** first matching character and recursively continue the match from
   678    689         ** that point.
   679    690         **
   680    691         ** For a case-insensitive search, set variable cx to be the same as
   681    692         ** c but in the other case and search the input string for either
   682    693         ** c or cx.
   683    694         */
   684    695         if( c<=0x80 ){
   685    696           u32 cx;
          697  +        int bMatch;
   686    698           if( noCase ){
   687    699             cx = sqlite3Toupper(c);
   688    700             c = sqlite3Tolower(c);
   689    701           }else{
   690    702             cx = c;
   691    703           }
   692    704           while( (c2 = *(zString++))!=0 ){
   693    705             if( c2!=c && c2!=cx ) continue;
   694         -          if( patternCompare(zPattern,zString,pInfo,matchOther) ) return 1;
          706  +          bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
          707  +          if( bMatch!=SQLITE_NOMATCH ) return bMatch;
   695    708           }
   696    709         }else{
          710  +        int bMatch;
   697    711           while( (c2 = Utf8Read(zString))!=0 ){
   698    712             if( c2!=c ) continue;
   699         -          if( patternCompare(zPattern,zString,pInfo,matchOther) ) return 1;
          713  +          bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
          714  +          if( bMatch!=SQLITE_NOMATCH ) return bMatch;
   700    715           }
   701    716         }
   702         -      return 0;
          717  +      return SQLITE_NOWILDCARDMATCH;
   703    718       }
   704    719       if( c==matchOther ){
   705    720         if( pInfo->matchSet==0 ){
   706    721           c = sqlite3Utf8Read(&zPattern);
   707         -        if( c==0 ) return 0;
          722  +        if( c==0 ) return SQLITE_NOMATCH;
   708    723           zEscaped = zPattern;
   709    724         }else{
   710    725           u32 prior_c = 0;
   711    726           int seen = 0;
   712    727           int invert = 0;
   713    728           c = sqlite3Utf8Read(&zString);
   714         -        if( c==0 ) return 0;
          729  +        if( c==0 ) return SQLITE_NOMATCH;
   715    730           c2 = sqlite3Utf8Read(&zPattern);
   716    731           if( c2=='^' ){
   717    732             invert = 1;
   718    733             c2 = sqlite3Utf8Read(&zPattern);
   719    734           }
   720    735           if( c2==']' ){
   721    736             if( c==']' ) seen = 1;
................................................................................
   731    746                 seen = 1;
   732    747               }
   733    748               prior_c = c2;
   734    749             }
   735    750             c2 = sqlite3Utf8Read(&zPattern);
   736    751           }
   737    752           if( c2==0 || (seen ^ invert)==0 ){
   738         -          return 0;
          753  +          return SQLITE_NOMATCH;
   739    754           }
   740    755           continue;
   741    756         }
   742    757       }
   743    758       c2 = Utf8Read(zString);
   744    759       if( c==c2 ) continue;
   745    760       if( noCase  && sqlite3Tolower(c)==sqlite3Tolower(c2) && c<0x80 && c2<0x80 ){
   746    761         continue;
   747    762       }
   748    763       if( c==matchOne && zPattern!=zEscaped && c2!=0 ) continue;
   749         -    return 0;
          764  +    return SQLITE_NOMATCH;
   750    765     }
   751         -  return *zString==0;
          766  +  return *zString==0 ? SQLITE_MATCH : SQLITE_NOMATCH;
   752    767   }
   753    768   
   754    769   /*
   755         -** The sqlite3_strglob() interface.
          770  +** The sqlite3_strglob() interface.  Return 0 on a match (like strcmp()) and
          771  +** non-zero if there is no match.
   756    772   */
   757    773   int sqlite3_strglob(const char *zGlobPattern, const char *zString){
   758         -  return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[')==0;
          774  +  return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, '[');
   759    775   }
   760    776   
   761    777   /*
   762         -** The sqlite3_strlike() interface.
          778  +** The sqlite3_strlike() interface.  Return 0 on a match and non-zero for
          779  +** a miss - like strcmp().
   763    780   */
   764    781   int sqlite3_strlike(const char *zPattern, const char *zStr, unsigned int esc){
   765         -  return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc)==0;
          782  +  return patternCompare((u8*)zPattern, (u8*)zStr, &likeInfoNorm, esc);
   766    783   }
   767    784   
   768    785   /*
   769    786   ** Count the number of times that the LIKE operator (or GLOB which is
   770    787   ** just a variation of LIKE) gets called.  This is used for testing
   771    788   ** only.
   772    789   */
................................................................................
   839    856     }else{
   840    857       escape = pInfo->matchSet;
   841    858     }
   842    859     if( zA && zB ){
   843    860   #ifdef SQLITE_TEST
   844    861       sqlite3_like_count++;
   845    862   #endif
   846         -    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
          863  +    sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
   847    864     }
   848    865   }
   849    866   
   850    867   /*
   851    868   ** Implementation of the NULLIF(x,y) function.  The result is the first
   852    869   ** argument if the arguments are different.  The result is NULL if the
   853    870   ** arguments are equal to each other.

Changes to src/global.c.

   215    215      0,                         /* xSqllog */
   216    216      0,                         /* pSqllogArg */
   217    217   #endif
   218    218   #ifdef SQLITE_VDBE_COVERAGE
   219    219      0,                         /* xVdbeBranch */
   220    220      0,                         /* pVbeBranchArg */
   221    221   #endif
   222         -#ifndef SQLITE_OMIT_BUILTIN_TEST
          222  +#ifndef SQLITE_UNTESTABLE
   223    223      0,                         /* xTestCallback */
   224    224   #endif
   225    225      0,                         /* bLocaltimeFault */
   226    226      0x7ffffffe                 /* iOnceResetThreshold */
   227    227   };
   228    228   
   229    229   /*

Changes to src/insert.c.

  1301   1301         case OE_Abort:
  1302   1302           sqlite3MayAbort(pParse);
  1303   1303           /* Fall through */
  1304   1304         case OE_Rollback:
  1305   1305         case OE_Fail: {
  1306   1306           char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
  1307   1307                                       pTab->aCol[i].zName);
  1308         -        sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
  1309         -                          regNewData+1+i, zMsg, P4_DYNAMIC);
         1308  +        sqlite3VdbeAddOp3(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
         1309  +                          regNewData+1+i);
         1310  +        sqlite3VdbeAppendP4(v, zMsg, P4_DYNAMIC);
  1310   1311           sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
  1311   1312           VdbeCoverage(v);
  1312   1313           break;
  1313   1314         }
  1314   1315         case OE_Ignore: {
  1315   1316           sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
  1316   1317           VdbeCoverage(v);
................................................................................
  1444   1445   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1445   1446             if( HasRowid(pTab) ){
  1446   1447               /* This OP_Delete opcode fires the pre-update-hook only. It does
  1447   1448               ** not modify the b-tree. It is more efficient to let the coming
  1448   1449               ** OP_Insert replace the existing entry than it is to delete the
  1449   1450               ** existing entry and then insert a new one. */
  1450   1451               sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, OPFLAG_ISNOOP);
  1451         -            sqlite3VdbeChangeP4(v, -1, (char *)pTab, P4_TABLE);
         1452  +            sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
  1452   1453             }
  1453   1454   #endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
  1454   1455             if( pTab->pIndex ){
  1455   1456               sqlite3MultiWrite(pParse);
  1456   1457               sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,-1);
  1457   1458             }
  1458   1459           }
................................................................................
  1721   1722       pik_flags |= OPFLAG_APPEND;
  1722   1723     }
  1723   1724     if( useSeekResult ){
  1724   1725       pik_flags |= OPFLAG_USESEEKRESULT;
  1725   1726     }
  1726   1727     sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
  1727   1728     if( !pParse->nested ){
  1728         -    sqlite3VdbeChangeP4(v, -1, (char *)pTab, P4_TABLE);
         1729  +    sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
  1729   1730     }
  1730   1731     sqlite3VdbeChangeP5(v, pik_flags);
  1731   1732   }
  1732   1733   
  1733   1734   /*
  1734   1735   ** Allocate cursors for the pTab table and all its indices and generate
  1735   1736   ** code to open and initialized those cursors.

Changes to src/main.c.

  3657   3657   }
  3658   3658   
  3659   3659   /*
  3660   3660   ** Interface to the testing logic.
  3661   3661   */
  3662   3662   int sqlite3_test_control(int op, ...){
  3663   3663     int rc = 0;
  3664         -#ifdef SQLITE_OMIT_BUILTIN_TEST
         3664  +#ifdef SQLITE_UNTESTABLE
  3665   3665     UNUSED_PARAMETER(op);
  3666   3666   #else
  3667   3667     va_list ap;
  3668   3668     va_start(ap, op);
  3669   3669     switch( op ){
  3670   3670   
  3671   3671       /*
................................................................................
  3994   3994           sqlite3ResetAllSchemasOfConnection(db);
  3995   3995         }
  3996   3996         sqlite3_mutex_leave(db->mutex);
  3997   3997         break;
  3998   3998       }
  3999   3999     }
  4000   4000     va_end(ap);
  4001         -#endif /* SQLITE_OMIT_BUILTIN_TEST */
         4001  +#endif /* SQLITE_UNTESTABLE */
  4002   4002     return rc;
  4003   4003   }
  4004   4004   
  4005   4005   /*
  4006   4006   ** This is a utility routine, useful to VFS implementations, that checks
  4007   4007   ** to see if a database file was a URI that contained a specific query 
  4008   4008   ** parameter, and if so obtains the value of the query parameter.

Changes to src/os_unix.c.

  1345   1345   ** to locate a particular unixInodeInfo object.
  1346   1346   */
  1347   1347   struct unixFileId {
  1348   1348     dev_t dev;                  /* Device number */
  1349   1349   #if OS_VXWORKS
  1350   1350     struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
  1351   1351   #else
  1352         -  ino_t ino;                  /* Inode number */
         1352  +  /* We are told that some versions of Android contain a bug that
         1353  +  ** sizes ino_t at only 32-bits instead of 64-bits. (See
         1354  +  ** https://android-review.googlesource.com/#/c/115351/3/dist/sqlite3.c)
         1355  +  ** To work around this, always allocate 64-bits for the inode number.  
         1356  +  ** On small machines that only have 32-bit inodes, this wastes 4 bytes,
         1357  +  ** but that should not be a big deal. */
         1358  +  /* WAS:  ino_t ino;   */
         1359  +  u64 ino;                   /* Inode number */
  1353   1360   #endif
  1354   1361   };
  1355   1362   
  1356   1363   /*
  1357   1364   ** An instance of the following structure is allocated for each open
  1358   1365   ** inode.  Or, on LinuxThreads, there is one of these structures for
  1359   1366   ** each inode opened by each thread.
................................................................................
  1601   1608   #endif
  1602   1609   
  1603   1610     memset(&fileId, 0, sizeof(fileId));
  1604   1611     fileId.dev = statbuf.st_dev;
  1605   1612   #if OS_VXWORKS
  1606   1613     fileId.pId = pFile->pId;
  1607   1614   #else
  1608         -  fileId.ino = statbuf.st_ino;
         1615  +  fileId.ino = (u64)statbuf.st_ino;
  1609   1616   #endif
  1610   1617     pInode = inodeList;
  1611   1618     while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
  1612   1619       pInode = pInode->pNext;
  1613   1620     }
  1614   1621     if( pInode==0 ){
  1615   1622       pInode = sqlite3_malloc64( sizeof(*pInode) );
................................................................................
  1635   1642   */
  1636   1643   static int fileHasMoved(unixFile *pFile){
  1637   1644   #if OS_VXWORKS
  1638   1645     return pFile->pInode!=0 && pFile->pId!=pFile->pInode->fileId.pId;
  1639   1646   #else
  1640   1647     struct stat buf;
  1641   1648     return pFile->pInode!=0 &&
  1642         -      (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
         1649  +      (osStat(pFile->zPath, &buf)!=0 
         1650  +         || (u64)buf.st_ino!=pFile->pInode->fileId.ino);
  1643   1651   #endif
  1644   1652   }
  1645   1653   
  1646   1654   
  1647   1655   /*
  1648   1656   ** Check a unixFile that is a database.  Verify the following:
  1649   1657   **
................................................................................
  6699   6707     ** not searching for a reusable file descriptor are not dire.  */
  6700   6708     if( 0==osStat(zPath, &sStat) ){
  6701   6709       unixInodeInfo *pInode;
  6702   6710   
  6703   6711       unixEnterMutex();
  6704   6712       pInode = inodeList;
  6705   6713       while( pInode && (pInode->fileId.dev!=sStat.st_dev
  6706         -                     || pInode->fileId.ino!=sStat.st_ino) ){
         6714  +                     || pInode->fileId.ino!=(u64)sStat.st_ino) ){
  6707   6715          pInode = pInode->pNext;
  6708   6716       }
  6709   6717       if( pInode ){
  6710   6718         UnixUnusedFd **pp;
  6711   6719         for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
  6712   6720         pUnused = *pp;
  6713   6721         if( pUnused ){

Changes to src/pager.c.

  2034   2034     }
  2035   2035   #endif
  2036   2036   
  2037   2037     sqlite3BitvecDestroy(pPager->pInJournal);
  2038   2038     pPager->pInJournal = 0;
  2039   2039     pPager->nRec = 0;
  2040   2040     if( rc==SQLITE_OK ){
  2041         -    if( pagerFlushOnCommit(pPager, bCommit) ){
         2041  +    if( MEMDB || pagerFlushOnCommit(pPager, bCommit) ){
  2042   2042         sqlite3PcacheCleanAll(pPager->pPCache);
  2043   2043       }else{
  2044   2044         sqlite3PcacheClearWritable(pPager->pPCache);
  2045   2045       }
  2046   2046       sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
  2047   2047     }
  2048   2048   
................................................................................
  3963   3963   ){
  3964   3964     PgHdr *p;                       /* Memory mapped page to return */
  3965   3965     
  3966   3966     if( pPager->pMmapFreelist ){
  3967   3967       *ppPage = p = pPager->pMmapFreelist;
  3968   3968       pPager->pMmapFreelist = p->pDirty;
  3969   3969       p->pDirty = 0;
  3970         -    memset(p->pExtra, 0, pPager->nExtra);
         3970  +    assert( pPager->nExtra>=8 );
         3971  +    memset(p->pExtra, 0, 8);
  3971   3972     }else{
  3972   3973       *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
  3973   3974       if( p==0 ){
  3974   3975         sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
  3975   3976         return SQLITE_NOMEM_BKPT;
  3976   3977       }
  3977   3978       p->pExtra = (void *)&p[1];
................................................................................
  4563   4564   ** and used as the file to be cached. Temporary files are be deleted
  4564   4565   ** automatically when they are closed. If zFilename is ":memory:" then 
  4565   4566   ** all information is held in cache. It is never written to disk. 
  4566   4567   ** This can be used to implement an in-memory database.
  4567   4568   **
  4568   4569   ** The nExtra parameter specifies the number of bytes of space allocated
  4569   4570   ** along with each page reference. This space is available to the user
  4570         -** via the sqlite3PagerGetExtra() API.
         4571  +** via the sqlite3PagerGetExtra() API.  When a new page is allocated, the
         4572  +** first 8 bytes of this space are zeroed but the remainder is uninitialized.
         4573  +** (The extra space is used by btree as the MemPage object.)
  4571   4574   **
  4572   4575   ** The flags argument is used to specify properties that affect the
  4573   4576   ** operation of the pager. It should be passed some bitwise combination
  4574   4577   ** of the PAGER_* flags.
  4575   4578   **
  4576   4579   ** The vfsFlags parameter is a bitmask to pass to the flags parameter
  4577   4580   ** of the xOpen() method of the supplied VFS when opening files. 
................................................................................
  4793   4796       assert( pPager->memDb==0 );
  4794   4797       rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
  4795   4798       testcase( rc!=SQLITE_OK );
  4796   4799     }
  4797   4800   
  4798   4801     /* Initialize the PCache object. */
  4799   4802     if( rc==SQLITE_OK ){
  4800         -    assert( nExtra<1000 );
  4801   4803       nExtra = ROUND8(nExtra);
         4804  +    assert( nExtra>=8 && nExtra<1000 );
  4802   4805       rc = sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
  4803   4806                          !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
  4804   4807     }
  4805   4808   
  4806   4809     /* If an error occurred above, free the  Pager structure and close the file.
  4807   4810     */
  4808   4811     if( rc!=SQLITE_OK ){
................................................................................
  5980   5983   ** as appropriate. Otherwise, SQLITE_OK.
  5981   5984   */
  5982   5985   int sqlite3PagerWrite(PgHdr *pPg){
  5983   5986     Pager *pPager = pPg->pPager;
  5984   5987     assert( (pPg->flags & PGHDR_MMAP)==0 );
  5985   5988     assert( pPager->eState>=PAGER_WRITER_LOCKED );
  5986   5989     assert( assert_pager_state(pPager) );
  5987         -  if( pPager->errCode ){
  5988         -    return pPager->errCode;
  5989         -  }else if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
         5990  +  if( (pPg->flags & PGHDR_WRITEABLE)!=0 && pPager->dbSize>=pPg->pgno ){
  5990   5991       if( pPager->nSavepoint ) return subjournalPageIfRequired(pPg);
  5991   5992       return SQLITE_OK;
         5993  +  }else if( pPager->errCode ){
         5994  +    return pPager->errCode;
  5992   5995     }else if( pPager->sectorSize > (u32)pPager->pageSize ){
  5993   5996       assert( pPager->tempFile==0 );
  5994   5997       return pagerWriteLargeSector(pPg);
  5995   5998     }else{
  5996   5999       return pager_write(pPg);
  5997   6000     }
  5998   6001   }

Changes to src/parse.y.

   264    264   carglist ::= .
   265    265   ccons ::= CONSTRAINT nm(X).           {pParse->constraintName = X;}
   266    266   ccons ::= DEFAULT term(X).            {sqlite3AddDefaultValue(pParse,&X);}
   267    267   ccons ::= DEFAULT LP expr(X) RP.      {sqlite3AddDefaultValue(pParse,&X);}
   268    268   ccons ::= DEFAULT PLUS term(X).       {sqlite3AddDefaultValue(pParse,&X);}
   269    269   ccons ::= DEFAULT MINUS(A) term(X).      {
   270    270     ExprSpan v;
   271         -  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0, 0);
          271  +  v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, X.pExpr, 0);
   272    272     v.zStart = A.z;
   273    273     v.zEnd = X.zEnd;
   274    274     sqlite3AddDefaultValue(pParse,&v);
   275    275   }
   276    276   ccons ::= DEFAULT id(X).              {
   277    277     ExprSpan v;
   278    278     spanExpr(&v, pParse, TK_STRING, X);
................................................................................
   539    539      sqlite3ExprListSetSpan(pParse,A,&X);
   540    540   }
   541    541   selcollist(A) ::= sclp(A) STAR. {
   542    542     Expr *p = sqlite3Expr(pParse->db, TK_ASTERISK, 0);
   543    543     A = sqlite3ExprListAppend(pParse, A, p);
   544    544   }
   545    545   selcollist(A) ::= sclp(A) nm(X) DOT STAR. {
   546         -  Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0, 0);
   547         -  Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &X);
   548         -  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
          546  +  Expr *pRight = sqlite3PExpr(pParse, TK_ASTERISK, 0, 0);
          547  +  Expr *pLeft = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
          548  +  Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
   549    549     A = sqlite3ExprListAppend(pParse,A, pDot);
   550    550   }
   551    551   
   552    552   // An option "AS <id>" phrase that can follow one of the expressions that
   553    553   // define the result set, or one of the tables in the FROM clause.
   554    554   //
   555    555   %type as {Token}
................................................................................
   866    866   term(A) ::= NULL(X).        {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
   867    867   expr(A) ::= id(X).          {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
   868    868   expr(A) ::= JOIN_KW(X).     {spanExpr(&A,pParse,TK_ID,X); /*A-overwrites-X*/}
   869    869   expr(A) ::= nm(X) DOT nm(Y). {
   870    870     Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
   871    871     Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
   872    872     spanSet(&A,&X,&Y); /*A-overwrites-X*/
   873         -  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
          873  +  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2);
   874    874   }
   875    875   expr(A) ::= nm(X) DOT nm(Y) DOT nm(Z). {
   876    876     Expr *temp1 = sqlite3ExprAlloc(pParse->db, TK_ID, &X, 1);
   877    877     Expr *temp2 = sqlite3ExprAlloc(pParse->db, TK_ID, &Y, 1);
   878    878     Expr *temp3 = sqlite3ExprAlloc(pParse->db, TK_ID, &Z, 1);
   879         -  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
          879  +  Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3);
   880    880     spanSet(&A,&X,&Z); /*A-overwrites-X*/
   881         -  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
          881  +  A.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4);
   882    882   }
   883    883   term(A) ::= FLOAT|BLOB(X). {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
   884    884   term(A) ::= STRING(X).     {spanExpr(&A,pParse,@X,X);/*A-overwrites-X*/}
   885    885   term(A) ::= INTEGER(X). {
   886    886     A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &X, 1);
   887    887     A.zStart = X.z;
   888    888     A.zEnd = X.z + X.n;
................................................................................
   900    900       Token t = X; /*A-overwrites-X*/
   901    901       assert( t.n>=2 );
   902    902       spanSet(&A, &t, &t);
   903    903       if( pParse->nested==0 ){
   904    904         sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t);
   905    905         A.pExpr = 0;
   906    906       }else{
   907         -      A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, 0);
          907  +      A.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0);
   908    908         if( A.pExpr ) sqlite3GetInt32(&t.z[1], &A.pExpr->iTable);
   909    909       }
   910    910     }
   911    911   }
   912    912   expr(A) ::= expr(A) COLLATE ids(C). {
   913    913     A.pExpr = sqlite3ExprAddCollateToken(pParse, A.pExpr, &C, 1);
   914    914     A.zEnd = &C.z[C.n];
   915    915   }
   916    916   %ifndef SQLITE_OMIT_CAST
   917    917   expr(A) ::= CAST(X) LP expr(E) AS typetoken(T) RP(Y). {
   918    918     spanSet(&A,&X,&Y); /*A-overwrites-X*/
   919         -  A.pExpr = sqlite3PExpr(pParse, TK_CAST, E.pExpr, 0, &T);
          919  +  A.pExpr = sqlite3ExprAlloc(pParse->db, TK_CAST, &T, 1);
          920  +  sqlite3ExprAttachSubtrees(pParse->db, A.pExpr, E.pExpr, 0);
   920    921   }
   921    922   %endif  SQLITE_OMIT_CAST
   922    923   expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP(E). {
   923    924     if( Y && Y->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
   924    925       sqlite3ErrorMsg(pParse, "too many arguments on function %T", &X);
   925    926     }
   926    927     A.pExpr = sqlite3ExprFunction(pParse, Y, &X);
................................................................................
   944    945     */
   945    946     static void spanBinaryExpr(
   946    947       Parse *pParse,      /* The parsing context.  Errors accumulate here */
   947    948       int op,             /* The binary operation */
   948    949       ExprSpan *pLeft,    /* The left operand, and output */
   949    950       ExprSpan *pRight    /* The right operand */
   950    951     ){
   951         -    pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
          952  +    pLeft->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr);
   952    953       pLeft->zEnd = pRight->zEnd;
   953    954     }
   954    955   
   955    956     /* If doNot is true, then add a TK_NOT Expr-node wrapper around the
   956    957     ** outside of *ppExpr.
   957    958     */
   958    959     static void exprNot(Parse *pParse, int doNot, ExprSpan *pSpan){
   959    960       if( doNot ){
   960         -      pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0, 0);
          961  +      pSpan->pExpr = sqlite3PExpr(pParse, TK_NOT, pSpan->pExpr, 0);
   961    962       }
   962    963     }
   963    964   }
   964    965   
   965    966   expr(A) ::= LP(L) nexprlist(X) COMMA expr(Y) RP(R). {
   966    967     ExprList *pList = sqlite3ExprListAppend(pParse, X, Y.pExpr);
   967         -  A.pExpr = sqlite3PExpr(pParse, TK_VECTOR, 0, 0, 0);
          968  +  A.pExpr = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
   968    969     if( A.pExpr ){
   969    970       A.pExpr->x.pList = pList;
   970    971       spanSet(&A, &L, &R);
   971    972     }else{
   972    973       sqlite3ExprListDelete(pParse->db, pList);
   973    974     }
   974    975   }
................................................................................
  1017   1018     */
  1018   1019     static void spanUnaryPostfix(
  1019   1020       Parse *pParse,         /* Parsing context to record errors */
  1020   1021       int op,                /* The operator */
  1021   1022       ExprSpan *pOperand,    /* The operand, and output */
  1022   1023       Token *pPostOp         /* The operand token for setting the span */
  1023   1024     ){
  1024         -    pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
         1025  +    pOperand->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
  1025   1026       pOperand->zEnd = &pPostOp->z[pPostOp->n];
  1026   1027     }                           
  1027   1028   }
  1028   1029   
  1029   1030   expr(A) ::= expr(A) ISNULL|NOTNULL(E).   {spanUnaryPostfix(pParse,@E,&A,&E);}
  1030   1031   expr(A) ::= expr(A) NOT NULL(E). {spanUnaryPostfix(pParse,TK_NOTNULL,&A,&E);}
  1031   1032   
................................................................................
  1064   1065       ExprSpan *pOut,        /* Write the new expression node here */
  1065   1066       Parse *pParse,         /* Parsing context to record errors */
  1066   1067       int op,                /* The operator */
  1067   1068       ExprSpan *pOperand,    /* The operand */
  1068   1069       Token *pPreOp         /* The operand token for setting the span */
  1069   1070     ){
  1070   1071       pOut->zStart = pPreOp->z;
  1071         -    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
         1072  +    pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0);
  1072   1073       pOut->zEnd = pOperand->zEnd;
  1073   1074     }
  1074   1075   }
  1075   1076   
  1076   1077   
  1077   1078   
  1078   1079   expr(A) ::= NOT(B) expr(X).  
................................................................................
  1086   1087   
  1087   1088   %type between_op {int}
  1088   1089   between_op(A) ::= BETWEEN.     {A = 0;}
  1089   1090   between_op(A) ::= NOT BETWEEN. {A = 1;}
  1090   1091   expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
  1091   1092     ExprList *pList = sqlite3ExprListAppend(pParse,0, X.pExpr);
  1092   1093     pList = sqlite3ExprListAppend(pParse,pList, Y.pExpr);
  1093         -  A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, A.pExpr, 0, 0);
         1094  +  A.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, A.pExpr, 0);
  1094   1095     if( A.pExpr ){
  1095   1096       A.pExpr->x.pList = pList;
  1096   1097     }else{
  1097   1098       sqlite3ExprListDelete(pParse->db, pList);
  1098   1099     } 
  1099   1100     exprNot(pParse, N, &A);
  1100   1101     A.zEnd = Y.zEnd;
................................................................................
  1110   1111         **      expr1 IN ()
  1111   1112         **      expr1 NOT IN ()
  1112   1113         **
  1113   1114         ** simplify to constants 0 (false) and 1 (true), respectively,
  1114   1115         ** regardless of the value of expr1.
  1115   1116         */
  1116   1117         sqlite3ExprDelete(pParse->db, A.pExpr);
  1117         -      A.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[N]);
         1118  +      A.pExpr = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
  1118   1119       }else if( Y->nExpr==1 ){
  1119   1120         /* Expressions of the form:
  1120   1121         **
  1121   1122         **      expr1 IN (?1)
  1122   1123         **      expr1 NOT IN (?2)
  1123   1124         **
  1124   1125         ** with exactly one value on the RHS can be simplified to something
................................................................................
  1137   1138         sqlite3ExprListDelete(pParse->db, Y);
  1138   1139         /* pRHS cannot be NULL because a malloc error would have been detected
  1139   1140         ** before now and control would have never reached this point */
  1140   1141         if( ALWAYS(pRHS) ){
  1141   1142           pRHS->flags &= ~EP_Collate;
  1142   1143           pRHS->flags |= EP_Generic;
  1143   1144         }
  1144         -      A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A.pExpr, pRHS, 0);
         1145  +      A.pExpr = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A.pExpr, pRHS);
  1145   1146       }else{
  1146         -      A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
         1147  +      A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
  1147   1148         if( A.pExpr ){
  1148   1149           A.pExpr->x.pList = Y;
  1149   1150           sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
  1150   1151         }else{
  1151   1152           sqlite3ExprListDelete(pParse->db, Y);
  1152   1153         }
  1153   1154         exprNot(pParse, N, &A);
  1154   1155       }
  1155   1156       A.zEnd = &E.z[E.n];
  1156   1157     }
  1157   1158     expr(A) ::= LP(B) select(X) RP(E). {
  1158   1159       spanSet(&A,&B,&E); /*A-overwrites-B*/
  1159         -    A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
         1160  +    A.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
  1160   1161       sqlite3PExprAddSelect(pParse, A.pExpr, X);
  1161   1162     }
  1162   1163     expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E).  [IN] {
  1163         -    A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
         1164  +    A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
  1164   1165       sqlite3PExprAddSelect(pParse, A.pExpr, Y);
  1165   1166       exprNot(pParse, N, &A);
  1166   1167       A.zEnd = &E.z[E.n];
  1167   1168     }
  1168   1169     expr(A) ::= expr(A) in_op(N) nm(Y) dbnm(Z) paren_exprlist(E). [IN] {
  1169   1170       SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&Y,&Z);
  1170   1171       Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
  1171   1172       if( E )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, E);
  1172         -    A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0, 0);
         1173  +    A.pExpr = sqlite3PExpr(pParse, TK_IN, A.pExpr, 0);
  1173   1174       sqlite3PExprAddSelect(pParse, A.pExpr, pSelect);
  1174   1175       exprNot(pParse, N, &A);
  1175   1176       A.zEnd = Z.z ? &Z.z[Z.n] : &Y.z[Y.n];
  1176   1177     }
  1177   1178     expr(A) ::= EXISTS(B) LP select(Y) RP(E). {
  1178   1179       Expr *p;
  1179   1180       spanSet(&A,&B,&E); /*A-overwrites-B*/
  1180         -    p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
         1181  +    p = A.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0);
  1181   1182       sqlite3PExprAddSelect(pParse, p, Y);
  1182   1183     }
  1183   1184   %endif SQLITE_OMIT_SUBQUERY
  1184   1185   
  1185   1186   /* CASE expressions */
  1186   1187   expr(A) ::= CASE(C) case_operand(X) case_exprlist(Y) case_else(Z) END(E). {
  1187   1188     spanSet(&A,&C,&E);  /*A-overwrites-C*/
  1188         -  A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0, 0);
         1189  +  A.pExpr = sqlite3PExpr(pParse, TK_CASE, X, 0);
  1189   1190     if( A.pExpr ){
  1190   1191       A.pExpr->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
  1191   1192       sqlite3ExprSetHeightAndFlags(pParse, A.pExpr);
  1192   1193     }else{
  1193   1194       sqlite3ExprListDelete(pParse->db, Y);
  1194   1195       sqlite3ExprDelete(pParse->db, Z);
  1195   1196     }
................................................................................
  1441   1442   // SELECT
  1442   1443   trigger_cmd(A) ::= select(X).
  1443   1444      {A = sqlite3TriggerSelectStep(pParse->db, X); /*A-overwrites-X*/}
  1444   1445   
  1445   1446   // The special RAISE expression that may occur in trigger programs
  1446   1447   expr(A) ::= RAISE(X) LP IGNORE RP(Y).  {
  1447   1448     spanSet(&A,&X,&Y);  /*A-overwrites-X*/
  1448         -  A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); 
         1449  +  A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
  1449   1450     if( A.pExpr ){
  1450   1451       A.pExpr->affinity = OE_Ignore;
  1451   1452     }
  1452   1453   }
  1453   1454   expr(A) ::= RAISE(X) LP raisetype(T) COMMA nm(Z) RP(Y).  {
  1454   1455     spanSet(&A,&X,&Y);  /*A-overwrites-X*/
  1455         -  A.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &Z); 
         1456  +  A.pExpr = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 
  1456   1457     if( A.pExpr ) {
  1457   1458       A.pExpr->affinity = (char)T;
  1458   1459     }
  1459   1460   }
  1460   1461   %endif  !SQLITE_OMIT_TRIGGER
  1461   1462   
  1462   1463   %type raisetype {int}

Changes to src/pcache.c.

   280    280   int sqlite3PcacheSize(void){ return sizeof(PCache); }
   281    281   
   282    282   /*
   283    283   ** Create a new PCache object. Storage space to hold the object
   284    284   ** has already been allocated and is passed in as the p pointer. 
   285    285   ** The caller discovers how much space needs to be allocated by 
   286    286   ** calling sqlite3PcacheSize().
          287  +**
          288  +** szExtra is some extra space allocated for each page.  The first
          289  +** 8 bytes of the extra space will be zeroed as the page is allocated,
          290  +** but remaining content will be uninitialized.  Though it is opaque
          291  +** to this module, the extra space really ends up being the MemPage
          292  +** structure in the pager.
   287    293   */
   288    294   int sqlite3PcacheOpen(
   289    295     int szPage,                  /* Size of every page */
   290    296     int szExtra,                 /* Extra space associated with each page */
   291    297     int bPurgeable,              /* True if pages are on backing store */
   292    298     int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
   293    299     void *pStress,               /* Argument to xStress */
   294    300     PCache *p                    /* Preallocated space for the PCache */
   295    301   ){
   296    302     memset(p, 0, sizeof(PCache));
   297    303     p->szPage = 1;
   298    304     p->szExtra = szExtra;
          305  +  assert( szExtra>=8 );  /* First 8 bytes will be zeroed */
   299    306     p->bPurgeable = bPurgeable;
   300    307     p->eCreate = 2;
   301    308     p->xStress = xStress;
   302    309     p->pStress = pStress;
   303    310     p->szCache = 100;
   304    311     p->szSpill = 1;
   305    312     pcacheTrace(("%p.OPEN szPage %d bPurgeable %d\n",p,szPage,bPurgeable));
................................................................................
   461    468     assert( pPage!=0 );
   462    469     pPgHdr = (PgHdr*)pPage->pExtra;
   463    470     assert( pPgHdr->pPage==0 );
   464    471     memset(&pPgHdr->pDirty, 0, sizeof(PgHdr) - offsetof(PgHdr,pDirty));
   465    472     pPgHdr->pPage = pPage;
   466    473     pPgHdr->pData = pPage->pBuf;
   467    474     pPgHdr->pExtra = (void *)&pPgHdr[1];
   468         -  memset(pPgHdr->pExtra, 0, pCache->szExtra);
          475  +  memset(pPgHdr->pExtra, 0, 8);
   469    476     pPgHdr->pCache = pCache;
   470    477     pPgHdr->pgno = pgno;
   471    478     pPgHdr->flags = PGHDR_CLEAN;
   472    479     return sqlite3PcacheFetchFinish(pCache,pgno,pPage);
   473    480   }
   474    481   
   475    482   /*

Changes to src/random.c.

   102    102       wsdPrng.s[wsdPrng.j] = t;
   103    103       t += wsdPrng.s[wsdPrng.i];
   104    104       *(zBuf++) = wsdPrng.s[t];
   105    105     }while( --N );
   106    106     sqlite3_mutex_leave(mutex);
   107    107   }
   108    108   
   109         -#ifndef SQLITE_OMIT_BUILTIN_TEST
          109  +#ifndef SQLITE_UNTESTABLE
   110    110   /*
   111    111   ** For testing purposes, we sometimes want to preserve the state of
   112    112   ** PRNG and restore the PRNG to its saved state at a later time, or
   113    113   ** to reset the PRNG to its initial state.  These routines accomplish
   114    114   ** those tasks.
   115    115   **
   116    116   ** The sqlite3_test_control() interface calls these routines to
................................................................................
   127    127   void sqlite3PrngRestoreState(void){
   128    128     memcpy(
   129    129       &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
   130    130       &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
   131    131       sizeof(sqlite3Prng)
   132    132     );
   133    133   }
   134         -#endif /* SQLITE_OMIT_BUILTIN_TEST */
          134  +#endif /* SQLITE_UNTESTABLE */

Changes to src/select.c.

   330    330     assert( pSrc->nSrc>iRight );
   331    331     assert( pSrc->a[iLeft].pTab );
   332    332     assert( pSrc->a[iRight].pTab );
   333    333   
   334    334     pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
   335    335     pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
   336    336   
   337         -  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
          337  +  pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
   338    338     if( pEq && isOuterJoin ){
   339    339       ExprSetProperty(pEq, EP_FromJoin);
   340    340       assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
   341    341       ExprSetVVAProperty(pEq, EP_NoReduce);
   342    342       pEq->iRightJoinTable = (i16)pE2->iTable;
   343    343     }
   344    344     *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
................................................................................
  3129   3129     explainComposite(pParse, p->op, iSub1, iSub2, 0);
  3130   3130     return pParse->nErr!=0;
  3131   3131   }
  3132   3132   #endif
  3133   3133   
  3134   3134   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  3135   3135   /* Forward Declarations */
  3136         -static void substExprList(sqlite3*, ExprList*, int, ExprList*);
  3137         -static void substSelect(sqlite3*, Select *, int, ExprList*, int);
         3136  +static void substExprList(Parse*, ExprList*, int, ExprList*);
         3137  +static void substSelect(Parse*, Select *, int, ExprList*, int);
  3138   3138   
  3139   3139   /*
  3140   3140   ** Scan through the expression pExpr.  Replace every reference to
  3141   3141   ** a column in table number iTable with a copy of the iColumn-th
  3142   3142   ** entry in pEList.  (But leave references to the ROWID column 
  3143   3143   ** unchanged.)
  3144   3144   **
................................................................................
  3146   3146   ** whose result set is defined by pEList appears as entry in the
  3147   3147   ** FROM clause of a SELECT such that the VDBE cursor assigned to that
  3148   3148   ** FORM clause entry is iTable.  This routine make the necessary 
  3149   3149   ** changes to pExpr so that it refers directly to the source table
  3150   3150   ** of the subquery rather the result set of the subquery.
  3151   3151   */
  3152   3152   static Expr *substExpr(
  3153         -  sqlite3 *db,        /* Report malloc errors to this connection */
         3153  +  Parse *pParse,      /* Report errors here */
  3154   3154     Expr *pExpr,        /* Expr in which substitution occurs */
  3155   3155     int iTable,         /* Table to be substituted */
  3156   3156     ExprList *pEList    /* Substitute expressions */
  3157   3157   ){
         3158  +  sqlite3 *db = pParse->db;
  3158   3159     if( pExpr==0 ) return 0;
  3159   3160     if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
  3160   3161       if( pExpr->iColumn<0 ){
  3161   3162         pExpr->op = TK_NULL;
  3162   3163       }else{
  3163   3164         Expr *pNew;
         3165  +      Expr *pCopy = pEList->a[pExpr->iColumn].pExpr;
  3164   3166         assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
  3165   3167         assert( pExpr->pLeft==0 && pExpr->pRight==0 );
  3166         -      pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
  3167         -      sqlite3ExprDelete(db, pExpr);
  3168         -      pExpr = pNew;
         3168  +      if( sqlite3ExprIsVector(pCopy) ){
         3169  +        sqlite3VectorErrorMsg(pParse, pCopy);
         3170  +      }else{
         3171  +        pNew = sqlite3ExprDup(db, pCopy, 0);
         3172  +        if( pNew && (pExpr->flags & EP_FromJoin) ){
         3173  +          pNew->iRightJoinTable = pExpr->iRightJoinTable;
         3174  +          pNew->flags |= EP_FromJoin;
         3175  +        }
         3176  +        sqlite3ExprDelete(db, pExpr);
         3177  +        pExpr = pNew;
         3178  +      }
  3169   3179       }
  3170   3180     }else{
  3171         -    pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
  3172         -    pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
         3181  +    pExpr->pLeft = substExpr(pParse, pExpr->pLeft, iTable, pEList);
         3182  +    pExpr->pRight = substExpr(pParse, pExpr->pRight, iTable, pEList);
  3173   3183       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3174         -      substSelect(db, pExpr->x.pSelect, iTable, pEList, 1);
         3184  +      substSelect(pParse, pExpr->x.pSelect, iTable, pEList, 1);
  3175   3185       }else{
  3176         -      substExprList(db, pExpr->x.pList, iTable, pEList);
         3186  +      substExprList(pParse, pExpr->x.pList, iTable, pEList);
  3177   3187       }
  3178   3188     }
  3179   3189     return pExpr;
  3180   3190   }
  3181   3191   static void substExprList(
  3182         -  sqlite3 *db,         /* Report malloc errors here */
         3192  +  Parse *pParse,       /* Report errors here */
  3183   3193     ExprList *pList,     /* List to scan and in which to make substitutes */
  3184   3194     int iTable,          /* Table to be substituted */
  3185   3195     ExprList *pEList     /* Substitute values */
  3186   3196   ){
  3187   3197     int i;
  3188   3198     if( pList==0 ) return;
  3189   3199     for(i=0; i<pList->nExpr; i++){
  3190         -    pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
         3200  +    pList->a[i].pExpr = substExpr(pParse, pList->a[i].pExpr, iTable, pEList);
  3191   3201     }
  3192   3202   }
  3193   3203   static void substSelect(
  3194         -  sqlite3 *db,         /* Report malloc errors here */
         3204  +  Parse *pParse,       /* Report errors here */
  3195   3205     Select *p,           /* SELECT statement in which to make substitutions */
  3196   3206     int iTable,          /* Table to be replaced */
  3197   3207     ExprList *pEList,    /* Substitute values */
  3198   3208     int doPrior          /* Do substitutes on p->pPrior too */
  3199   3209   ){
  3200   3210     SrcList *pSrc;
  3201   3211     struct SrcList_item *pItem;
  3202   3212     int i;
  3203   3213     if( !p ) return;
  3204   3214     do{
  3205         -    substExprList(db, p->pEList, iTable, pEList);
  3206         -    substExprList(db, p->pGroupBy, iTable, pEList);
  3207         -    substExprList(db, p->pOrderBy, iTable, pEList);
  3208         -    p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
  3209         -    p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
         3215  +    substExprList(pParse, p->pEList, iTable, pEList);
         3216  +    substExprList(pParse, p->pGroupBy, iTable, pEList);
         3217  +    substExprList(pParse, p->pOrderBy, iTable, pEList);
         3218  +    p->pHaving = substExpr(pParse, p->pHaving, iTable, pEList);
         3219  +    p->pWhere = substExpr(pParse, p->pWhere, iTable, pEList);
  3210   3220       pSrc = p->pSrc;
  3211   3221       assert( pSrc!=0 );
  3212   3222       for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
  3213         -      substSelect(db, pItem->pSelect, iTable, pEList, 1);
         3223  +      substSelect(pParse, pItem->pSelect, iTable, pEList, 1);
  3214   3224         if( pItem->fg.isTabFunc ){
  3215         -        substExprList(db, pItem->u1.pFuncArg, iTable, pEList);
         3225  +        substExprList(pParse, pItem->u1.pFuncArg, iTable, pEList);
  3216   3226         }
  3217   3227       }
  3218   3228     }while( doPrior && (p = p->pPrior)!=0 );
  3219   3229   }
  3220   3230   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3221   3231   
  3222   3232   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
................................................................................
  3733   3743             sqlite3ExprDup(db, pSub->pHaving, 0), pParent->pHaving
  3734   3744         );
  3735   3745         assert( pParent->pGroupBy==0 );
  3736   3746         pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
  3737   3747       }else{
  3738   3748         pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
  3739   3749       }
  3740         -    substSelect(db, pParent, iParent, pSub->pEList, 0);
         3750  +    substSelect(pParse, pParent, iParent, pSub->pEList, 0);
  3741   3751     
  3742   3752       /* The flattened query is distinct if either the inner or the
  3743   3753       ** outer query is distinct. 
  3744   3754       */
  3745   3755       pParent->selFlags |= pSub->selFlags & SF_Distinct;
  3746   3756     
  3747   3757       /*
................................................................................
  3807   3817   **   (5) The WHERE clause expression originates in the ON or USING clause
  3808   3818   **       of a LEFT JOIN.
  3809   3819   **
  3810   3820   ** Return 0 if no changes are made and non-zero if one or more WHERE clause
  3811   3821   ** terms are duplicated into the subquery.
  3812   3822   */
  3813   3823   static int pushDownWhereTerms(
  3814         -  sqlite3 *db,          /* The database connection (for malloc()) */
         3824  +  Parse *pParse,        /* Parse context (for malloc() and error reporting) */
  3815   3825     Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
  3816   3826     Expr *pWhere,         /* The WHERE clause of the outer query */
  3817   3827     int iCursor           /* Cursor number of the subquery */
  3818   3828   ){
  3819   3829     Expr *pNew;
  3820   3830     int nChng = 0;
  3821   3831     Select *pX;           /* For looping over compound SELECTs in pSubq */
................................................................................
  3828   3838         return 0; /* restrictions (1) and (2) */
  3829   3839       }
  3830   3840     }
  3831   3841     if( pSubq->pLimit!=0 ){
  3832   3842       return 0; /* restriction (3) */
  3833   3843     }
  3834   3844     while( pWhere->op==TK_AND ){
  3835         -    nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor);
         3845  +    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight, iCursor);
  3836   3846       pWhere = pWhere->pLeft;
  3837   3847     }
  3838   3848     if( ExprHasProperty(pWhere,EP_FromJoin) ) return 0; /* restriction 5 */
  3839   3849     if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
  3840   3850       nChng++;
  3841   3851       while( pSubq ){
  3842         -      pNew = sqlite3ExprDup(db, pWhere, 0);
  3843         -      pNew = substExpr(db, pNew, iCursor, pSubq->pEList);
  3844         -      pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew);
         3852  +      pNew = sqlite3ExprDup(pParse->db, pWhere, 0);
         3853  +      pNew = substExpr(pParse, pNew, iCursor, pSubq->pEList);
         3854  +      pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
  3845   3855         pSubq = pSubq->pPrior;
  3846   3856       }
  3847   3857     }
  3848   3858     return nChng;
  3849   3859   }
  3850   3860   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3851   3861   
................................................................................
  4467   4477               }
  4468   4478               pRight = sqlite3Expr(db, TK_ID, zName);
  4469   4479               zColname = zName;
  4470   4480               zToFree = 0;
  4471   4481               if( longNames || pTabList->nSrc>1 ){
  4472   4482                 Expr *pLeft;
  4473   4483                 pLeft = sqlite3Expr(db, TK_ID, zTabName);
  4474         -              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
         4484  +              pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight);
  4475   4485                 if( zSchemaName ){
  4476   4486                   pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
  4477         -                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
         4487  +                pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr);
  4478   4488                 }
  4479   4489                 if( longNames ){
  4480   4490                   zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
  4481   4491                   zToFree = zColname;
  4482   4492                 }
  4483   4493               }else{
  4484   4494                 pExpr = pRight;
................................................................................
  4707   4717   static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
  4708   4718     Vdbe *v = pParse->pVdbe;
  4709   4719     int i;
  4710   4720     struct AggInfo_func *pF;
  4711   4721     for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
  4712   4722       ExprList *pList = pF->pExpr->x.pList;
  4713   4723       assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
  4714         -    sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
  4715         -                      (void*)pF->pFunc, P4_FUNCDEF);
         4724  +    sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
         4725  +    sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
  4716   4726     }
  4717   4727   }
  4718   4728   
  4719   4729   /*
  4720   4730   ** Update the accumulator memory cells for an aggregate based on
  4721   4731   ** the current cursor position.
  4722   4732   */
................................................................................
  4759   4769         }
  4760   4770         if( !pColl ){
  4761   4771           pColl = pParse->db->pDfltColl;
  4762   4772         }
  4763   4773         if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
  4764   4774         sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
  4765   4775       }
  4766         -    sqlite3VdbeAddOp4(v, OP_AggStep0, 0, regAgg, pF->iMem,
  4767         -                      (void*)pF->pFunc, P4_FUNCDEF);
         4776  +    sqlite3VdbeAddOp3(v, OP_AggStep0, 0, regAgg, pF->iMem);
         4777  +    sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
  4768   4778       sqlite3VdbeChangeP5(v, (u8)nArg);
  4769   4779       sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
  4770   4780       sqlite3ReleaseTempRange(pParse, regAgg, nArg);
  4771   4781       if( addrNext ){
  4772   4782         sqlite3VdbeResolveLabel(v, addrNext);
  4773   4783         sqlite3ExprCacheClear(pParse);
  4774   4784       }
................................................................................
  4994   5004       */
  4995   5005       pParse->nHeight += sqlite3SelectExprHeight(p);
  4996   5006   
  4997   5007       /* Make copies of constant WHERE-clause terms in the outer query down
  4998   5008       ** inside the subquery.  This can help the subquery to run more efficiently.
  4999   5009       */
  5000   5010       if( (pItem->fg.jointype & JT_OUTER)==0
  5001         -     && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
         5011  +     && pushDownWhereTerms(pParse, pSub, p->pWhere, pItem->iCursor)
  5002   5012       ){
  5003   5013   #if SELECTTRACE_ENABLED
  5004   5014         if( sqlite3SelectTrace & 0x100 ){
  5005   5015           SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
  5006   5016           sqlite3TreeViewSelect(0, p, 0);
  5007   5017         }
  5008   5018   #endif

Changes to src/shell.c.

  2579   2579       if( f==0 ){
  2580   2580         utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
  2581   2581       }
  2582   2582     }
  2583   2583     return f;
  2584   2584   }
  2585   2585   
  2586         -#if !defined(SQLITE_OMIT_BUILTIN_TEST)
         2586  +#if !defined(SQLITE_UNTESTABLE)
  2587   2587   #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
  2588   2588   /*
  2589   2589   ** A routine for handling output from sqlite3_trace().
  2590   2590   */
  2591   2591   static int sql_trace_callback(
  2592   2592     unsigned mType,
  2593   2593     void *pArg,
................................................................................
  3855   3855       }else if( rc != SQLITE_OK ){
  3856   3856         raw_printf(stderr,
  3857   3857                    "Error: querying sqlite_master and sqlite_temp_master\n");
  3858   3858         rc = 1;
  3859   3859       }
  3860   3860     }else
  3861   3861   
  3862         -#ifndef SQLITE_OMIT_BUILTIN_TEST
         3862  +#ifndef SQLITE_UNTESTABLE
  3863   3863     if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
  3864   3864       char *zSql;
  3865   3865       char *zCollist = 0;
  3866   3866       sqlite3_stmt *pStmt;
  3867   3867       int tnum = 0;
  3868   3868       int i;
  3869   3869       if( nArg!=3 ){
................................................................................
  4791   4791       if( nArg>=2 ){
  4792   4792         sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
  4793   4793       }else{
  4794   4794         sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
  4795   4795       }
  4796   4796     }else
  4797   4797   
  4798         -#ifndef SQLITE_OMIT_BUILTIN_TEST
         4798  +#ifndef SQLITE_UNTESTABLE
  4799   4799     if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
  4800   4800       static const struct {
  4801   4801          const char *zCtrlName;   /* Name of a test-control option */
  4802   4802          int ctrlCode;            /* Integer code for that option */
  4803   4803       } aCtrl[] = {
  4804   4804         { "prng_save",             SQLITE_TESTCTRL_PRNG_SAVE              },
  4805   4805         { "prng_restore",          SQLITE_TESTCTRL_PRNG_RESTORE           },
................................................................................
  4967   4967       if( p->traceOut==0 ){
  4968   4968         sqlite3_trace_v2(p->db, 0, 0, 0);
  4969   4969       }else{
  4970   4970         sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
  4971   4971       }
  4972   4972   #endif
  4973   4973     }else
  4974         -#endif /* !defined(SQLITE_OMIT_BUILTIN_TEST) */
         4974  +#endif /* !defined(SQLITE_UNTESTABLE) */
  4975   4975   
  4976   4976   #if SQLITE_USER_AUTHENTICATION
  4977   4977     if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
  4978   4978       if( nArg<2 ){
  4979   4979         raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
  4980   4980         rc = 1;
  4981   4981         goto meta_command_exit;

Changes to src/sqlite.h.in.

  8260   8260   ** called to get back the underlying "errno" that caused the problem, such
  8261   8261   ** as ENOSPC, EAUTH, EISDIR, and so forth.  
  8262   8262   */
  8263   8263   int sqlite3_system_errno(sqlite3*);
  8264   8264   
  8265   8265   /*
  8266   8266   ** CAPI3REF: Database Snapshot
  8267         -** KEYWORDS: {snapshot}
         8267  +** KEYWORDS: {snapshot} {sqlite3_snapshot}
  8268   8268   ** EXPERIMENTAL
  8269   8269   **
  8270   8270   ** An instance of the snapshot object records the state of a [WAL mode]
  8271   8271   ** database for some specific point in history.
  8272   8272   **
  8273   8273   ** In [WAL mode], multiple [database connections] that are open on the
  8274   8274   ** same database file can each be reading a different historical version

Changes to src/sqliteInt.h.

  1457   1457   #define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
  1458   1458   #define SQLITE_CursorHints    0x2000   /* Add OP_CursorHint opcodes */
  1459   1459   #define SQLITE_AllOpts        0xffff   /* All optimizations */
  1460   1460   
  1461   1461   /*
  1462   1462   ** Macros for testing whether or not optimizations are enabled or disabled.
  1463   1463   */
  1464         -#ifndef SQLITE_OMIT_BUILTIN_TEST
  1465   1464   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
  1466   1465   #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
  1467         -#else
  1468         -#define OptimizationDisabled(db, mask)  0
  1469         -#define OptimizationEnabled(db, mask)   1
  1470         -#endif
  1471   1466   
  1472   1467   /*
  1473   1468   ** Return true if it OK to factor constant expressions into the initialization
  1474   1469   ** code. The argument is a Parse object for the code generator.
  1475   1470   */
  1476   1471   #define ConstFactorOk(P) ((P)->okConstFactor)
  1477   1472   
................................................................................
  3233   3228   #ifdef SQLITE_VDBE_COVERAGE
  3234   3229     /* The following callback (if not NULL) is invoked on every VDBE branch
  3235   3230     ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
  3236   3231     */
  3237   3232     void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
  3238   3233     void *pVdbeBranchArg;                                     /* 1st argument */
  3239   3234   #endif
  3240         -#ifndef SQLITE_OMIT_BUILTIN_TEST
         3235  +#ifndef SQLITE_UNTESTABLE
  3241   3236     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
  3242   3237   #endif
  3243   3238     int bLocaltimeFault;              /* True to fail localtime() calls */
  3244   3239     int iOnceResetThreshold;          /* When to reset OP_Once counters */
  3245   3240   };
  3246   3241   
  3247   3242   /*
................................................................................
  3437   3432   int sqlite3MallocSize(void*);
  3438   3433   int sqlite3DbMallocSize(sqlite3*, void*);
  3439   3434   void *sqlite3ScratchMalloc(int);
  3440   3435   void sqlite3ScratchFree(void*);
  3441   3436   void *sqlite3PageMalloc(int);
  3442   3437   void sqlite3PageFree(void*);
  3443   3438   void sqlite3MemSetDefault(void);
  3444         -#ifndef SQLITE_OMIT_BUILTIN_TEST
         3439  +#ifndef SQLITE_UNTESTABLE
  3445   3440   void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
  3446   3441   #endif
  3447   3442   int sqlite3HeapNearlyFull(void);
  3448   3443   
  3449   3444   /*
  3450   3445   ** On systems with ample stack space and that support alloca(), make
  3451   3446   ** use of alloca() to obtain space for large automatic objects.  By default,
................................................................................
  3548   3543   void sqlite3ClearTempRegCache(Parse*);
  3549   3544   #ifdef SQLITE_DEBUG
  3550   3545   int sqlite3NoTempsInRange(Parse*,int,int);
  3551   3546   #endif
  3552   3547   Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  3553   3548   Expr *sqlite3Expr(sqlite3*,int,const char*);
  3554   3549   void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  3555         -Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
         3550  +Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
  3556   3551   void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
  3557   3552   Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
  3558   3553   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
  3559   3554   void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
  3560   3555   void sqlite3ExprDelete(sqlite3*, Expr*);
  3561   3556   ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  3562   3557   ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
................................................................................
  3592   3587   void sqlite3AddDefaultValue(Parse*,ExprSpan*);
  3593   3588   void sqlite3AddCollateType(Parse*, Token*);
  3594   3589   void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
  3595   3590   int sqlite3ParseUri(const char*,const char*,unsigned int*,
  3596   3591                       sqlite3_vfs**,char**,char **);
  3597   3592   Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
  3598   3593   
  3599         -#ifdef SQLITE_OMIT_BUILTIN_TEST
         3594  +#ifdef SQLITE_UNTESTABLE
  3600   3595   # define sqlite3FaultSim(X) SQLITE_OK
  3601   3596   #else
  3602   3597     int sqlite3FaultSim(int);
  3603   3598   #endif
  3604   3599   
  3605   3600   Bitvec *sqlite3BitvecCreate(u32);
  3606   3601   int sqlite3BitvecTest(Bitvec*, u32);
  3607   3602   int sqlite3BitvecTestNotNull(Bitvec*, u32);
  3608   3603   int sqlite3BitvecSet(Bitvec*, u32);
  3609   3604   void sqlite3BitvecClear(Bitvec*, u32, void*);
  3610   3605   void sqlite3BitvecDestroy(Bitvec*);
  3611   3606   u32 sqlite3BitvecSize(Bitvec*);
  3612         -#ifndef SQLITE_OMIT_BUILTIN_TEST
         3607  +#ifndef SQLITE_UNTESTABLE
  3613   3608   int sqlite3BitvecBuiltinTest(int,int*);
  3614   3609   #endif
  3615   3610   
  3616   3611   RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
  3617   3612   void sqlite3RowSetClear(RowSet*);
  3618   3613   void sqlite3RowSetInsert(RowSet*, i64);
  3619   3614   int sqlite3RowSetTest(RowSet*, int iBatch, i64);
................................................................................
  3725   3720   int sqlite3ExprListCompare(ExprList*, ExprList*, int);
  3726   3721   int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
  3727   3722   void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
  3728   3723   void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
  3729   3724   int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
  3730   3725   int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
  3731   3726   Vdbe *sqlite3GetVdbe(Parse*);
  3732         -#ifndef SQLITE_OMIT_BUILTIN_TEST
         3727  +#ifndef SQLITE_UNTESTABLE
  3733   3728   void sqlite3PrngSaveState(void);
  3734   3729   void sqlite3PrngRestoreState(void);
  3735   3730   #endif
  3736   3731   void sqlite3RollbackAll(sqlite3*,int);
  3737   3732   void sqlite3CodeVerifySchema(Parse*, int);
  3738   3733   void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
  3739   3734   void sqlite3BeginTransaction(Parse*, int);
................................................................................
  4153   4148   ** Available fault injectors.  Should be numbered beginning with 0.
  4154   4149   */
  4155   4150   #define SQLITE_FAULTINJECTOR_MALLOC     0
  4156   4151   #define SQLITE_FAULTINJECTOR_COUNT      1
  4157   4152   
  4158   4153   /*
  4159   4154   ** The interface to the code in fault.c used for identifying "benign"
  4160         -** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
         4155  +** malloc failures. This is only present if SQLITE_UNTESTABLE
  4161   4156   ** is not defined.
  4162   4157   */
  4163         -#ifndef SQLITE_OMIT_BUILTIN_TEST
         4158  +#ifndef SQLITE_UNTESTABLE
  4164   4159     void sqlite3BeginBenignMalloc(void);
  4165   4160     void sqlite3EndBenignMalloc(void);
  4166   4161   #else
  4167   4162     #define sqlite3BeginBenignMalloc()
  4168   4163     #define sqlite3EndBenignMalloc()
  4169   4164   #endif
  4170   4165   
................................................................................
  4311   4306   int sqlite3DbstatRegister(sqlite3*);
  4312   4307   #endif
  4313   4308   
  4314   4309   int sqlite3ExprVectorSize(Expr *pExpr);
  4315   4310   int sqlite3ExprIsVector(Expr *pExpr);
  4316   4311   Expr *sqlite3VectorFieldSubexpr(Expr*, int);
  4317   4312   Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
         4313  +void sqlite3VectorErrorMsg(Parse*, Expr*);
  4318   4314   
  4319   4315   #endif /* SQLITEINT_H */

Changes to src/tclsqlite.c.

  3136   3136     **    $db preupdate_hook count
  3137   3137     **    $db preupdate_hook hook ?SCRIPT?
  3138   3138     **    $db preupdate_hook new INDEX
  3139   3139     **    $db preupdate_hook old INDEX
  3140   3140     */
  3141   3141     case DB_PREUPDATE: {
  3142   3142   #ifndef SQLITE_ENABLE_PREUPDATE_HOOK
  3143         -    Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time");
         3143  +    Tcl_AppendResult(interp, "preupdate_hook was omitted at compile-time", 
         3144  +                     (char*)0);
  3144   3145       rc = TCL_ERROR;
  3145   3146   #else
  3146   3147       static const char *azSub[] = {"count", "depth", "hook", "new", "old", 0};
  3147   3148       enum DbPreupdateSubCmd {
  3148   3149         PRE_COUNT, PRE_DEPTH, PRE_HOOK, PRE_NEW, PRE_OLD
  3149   3150       };
  3150   3151       int iSub;

Changes to src/test1.c.

  7145   7145     extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
  7146   7146     extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
  7147   7147     extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
  7148   7148     extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
  7149   7149     extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
  7150   7150     extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
  7151   7151     extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
         7152  +  extern int sqlite3_remember_init(sqlite3*,char**,const sqlite3_api_routines*);
  7152   7153     extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
  7153   7154     extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
  7154   7155     extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
  7155   7156     extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
  7156   7157     static const struct {
  7157   7158       const char *zExtName;
  7158   7159       int (*pInit)(sqlite3*,char**,const sqlite3_api_routines*);
................................................................................
  7164   7165       { "eval",                  sqlite3_eval_init                 },
  7165   7166       { "fileio",                sqlite3_fileio_init               },
  7166   7167       { "fuzzer",                sqlite3_fuzzer_init               },
  7167   7168       { "ieee754",               sqlite3_ieee_init                 },
  7168   7169       { "nextchar",              sqlite3_nextchar_init             },
  7169   7170       { "percentile",            sqlite3_percentile_init           },
  7170   7171       { "regexp",                sqlite3_regexp_init               },
         7172  +    { "remember",              sqlite3_remember_init             },
  7171   7173       { "series",                sqlite3_series_init               },
  7172   7174       { "spellfix",              sqlite3_spellfix_init             },
  7173   7175       { "totype",                sqlite3_totype_init               },
  7174   7176       { "wholenumber",           sqlite3_wholenumber_init          },
  7175   7177     };
  7176   7178     sqlite3 *db;
  7177   7179     const char *zName;

Changes to src/test_config.c.

   264    264   
   265    265   #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
   266    266     Tcl_SetVar2(interp, "sqlite_options", "between_opt", "0", TCL_GLOBAL_ONLY);
   267    267   #else
   268    268     Tcl_SetVar2(interp, "sqlite_options", "between_opt", "1", TCL_GLOBAL_ONLY);
   269    269   #endif
   270    270   
   271         -#ifdef SQLITE_OMIT_BUILTIN_TEST
          271  +#ifdef SQLITE_UNTESTABLE
   272    272     Tcl_SetVar2(interp, "sqlite_options", "builtin_test", "0", TCL_GLOBAL_ONLY);
   273    273   #else
   274    274     Tcl_SetVar2(interp, "sqlite_options", "builtin_test", "1", TCL_GLOBAL_ONLY);
   275    275   #endif
   276    276   
   277    277   #ifdef SQLITE_OMIT_BLOB_LITERAL
   278    278     Tcl_SetVar2(interp, "sqlite_options", "bloblit", "0", TCL_GLOBAL_ONLY);

Changes to src/test_func.c.

   151    151   }
   152    152   
   153    153   /*
   154    154   ** The following aggregate function, test_agg_errmsg16(), takes zero 
   155    155   ** arguments. It returns the text value returned by the sqlite3_errmsg16()
   156    156   ** API function.
   157    157   */
   158         -#ifndef SQLITE_OMIT_BUILTIN_TEST
          158  +#ifndef SQLITE_UNTESTABLE
   159    159   void sqlite3BeginBenignMalloc(void);
   160    160   void sqlite3EndBenignMalloc(void);
   161    161   #else
   162    162     #define sqlite3BeginBenignMalloc()
   163    163     #define sqlite3EndBenignMalloc()
   164    164   #endif
   165    165   static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){

Changes to src/tokenize.c.

    77     77   #endif
    78     78   #ifdef SQLITE_EBCDIC
    79     79   /*         x0  x1  x2  x3  x4  x5  x6  x7  x8  x9  xa  xb  xc  xd  xe  xf */
    80     80   /* 0x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27,  7,  7, 27, 27,
    81     81   /* 1x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
    82     82   /* 2x */   27, 27, 27, 27, 27,  7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
    83     83   /* 3x */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
    84         -/* 4x */    7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 12, 17, 20, 10,
           84  +/* 4x */    7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 26, 12, 17, 20, 10,
    85     85   /* 5x */   24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15,  4, 21, 18, 19, 27,
    86         -/* 6x */   11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22,  1, 13,  7,
           86  +/* 6x */   11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22,  1, 13,  6,
    87     87   /* 7x */   27, 27, 27, 27, 27, 27, 27, 27, 27,  8,  5,  5,  5,  8, 14,  8,
    88     88   /* 8x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
    89     89   /* 9x */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
    90         -/* 9x */   25,  1,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
           90  +/* Ax */   27, 25,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
    91     91   /* Bx */   27, 27, 27, 27, 27, 27, 27, 27, 27, 27,  9, 27, 27, 27, 27, 27,
    92     92   /* Cx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
    93     93   /* Dx */   27,  1,  1,  1,  1,  1,  1,  1,  1,  1, 27, 27, 27, 27, 27, 27,
    94     94   /* Ex */   27, 27,  1,  1,  1,  1,  1,  0,  1,  1, 27, 27, 27, 27, 27, 27,
    95     95   /* Fx */    3,  3,  3,  3,  3,  3,  3,  3,  3,  3, 27, 27, 27, 27, 27, 27,
    96     96   #endif
    97     97   };

Changes to src/update.c.

    65     65       u8 enc = ENC(sqlite3VdbeDb(v));
    66     66       Column *pCol = &pTab->aCol[i];
    67     67       VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
    68     68       assert( i<pTab->nCol );
    69     69       sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, 
    70     70                            pCol->affinity, &pValue);
    71     71       if( pValue ){
    72         -      sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
           72  +      sqlite3VdbeAppendP4(v, pValue, P4_MEM);
    73     73       }
    74     74   #ifndef SQLITE_OMIT_FLOATING_POINT
    75     75       if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
    76     76         sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
    77     77       }
    78     78   #endif
    79     79     }
................................................................................
   606    606       assert( regNew==regNewRowid+1 );
   607    607   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
   608    608       sqlite3VdbeAddOp3(v, OP_Delete, iDataCur,
   609    609           OPFLAG_ISUPDATE | ((hasFK || chngKey || pPk!=0) ? 0 : OPFLAG_ISNOOP),
   610    610           regNewRowid
   611    611       );
   612    612       if( !pParse->nested ){
   613         -      sqlite3VdbeChangeP4(v, -1, (char*)pTab, P4_TABLE);
          613  +      sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
   614    614       }
   615    615   #else
   616    616       if( hasFK || chngKey || pPk!=0 ){
   617    617         sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
   618    618       }
   619    619   #endif
   620    620       if( bReplace || chngKey ){

Changes to src/util.c.

    38     38   **
    39     39   ** The intent of the integer argument is to let the fault simulator know
    40     40   ** which of multiple sqlite3FaultSim() calls has been hit.
    41     41   **
    42     42   ** Return whatever integer value the test callback returns, or return
    43     43   ** SQLITE_OK if no test callback is installed.
    44     44   */
    45         -#ifndef SQLITE_OMIT_BUILTIN_TEST
           45  +#ifndef SQLITE_UNTESTABLE
    46     46   int sqlite3FaultSim(int iTest){
    47     47     int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
    48     48     return xCallback ? xCallback(iTest) : SQLITE_OK;
    49     49   }
    50     50   #endif
    51     51   
    52     52   #ifndef SQLITE_OMIT_FLOATING_POINT

Changes to src/vdbe.c.

   107    107   int sqlite3_found_count = 0;
   108    108   #endif
   109    109   
   110    110   /*
   111    111   ** Test a register to see if it exceeds the current maximum blob size.
   112    112   ** If it does, record the new maximum blob size.
   113    113   */
   114         -#if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
          114  +#if defined(SQLITE_TEST) && !defined(SQLITE_UNTESTABLE)
   115    115   # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
   116    116   #else
   117    117   # define UPDATE_MAX_BLOBSIZE(P)
   118    118   #endif
   119    119   
   120    120   /*
   121    121   ** Invoke the VDBE coverage callback, if that callback is defined.  This
................................................................................
   217    217     assert( iCur>=0 && iCur<p->nCursor );
   218    218     if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
   219    219       sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   220    220       p->apCsr[iCur] = 0;
   221    221     }
   222    222     if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
   223    223       p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
   224         -    memset(pCx, 0, sizeof(VdbeCursor));
          224  +    memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
   225    225       pCx->eCurType = eCurType;
   226    226       pCx->iDb = iDb;
   227    227       pCx->nField = nField;
   228    228       pCx->aOffset = &pCx->aType[nField];
   229    229       if( eCurType==CURTYPE_BTREE ){
   230    230         pCx->uc.pCursor = (BtCursor*)
   231    231             &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
................................................................................
  3524   3524         SQLITE_OPEN_TRANSIENT_DB;
  3525   3525     assert( pOp->p1>=0 );
  3526   3526     assert( pOp->p2>=0 );
  3527   3527     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
  3528   3528     if( pCx==0 ) goto no_mem;
  3529   3529     pCx->nullRow = 1;
  3530   3530     pCx->isEphemeral = 1;
  3531         -  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt, 
         3531  +  rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, 
  3532   3532                           BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
  3533   3533     if( rc==SQLITE_OK ){
  3534         -    rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
         3534  +    rc = sqlite3BtreeBeginTrans(pCx->pBtx, 1);
  3535   3535     }
  3536   3536     if( rc==SQLITE_OK ){
  3537   3537       /* If a transient index is required, create it by calling
  3538   3538       ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
  3539   3539       ** opening it. If a transient table is required, just use the
  3540   3540       ** automatically created table with root-page 1 (an BLOB_INTKEY table).
  3541   3541       */
  3542         -    if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
         3542  +    if( (pCx->pKeyInfo = pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
  3543   3543         int pgno;
  3544   3544         assert( pOp->p4type==P4_KEYINFO );
  3545         -      rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5); 
         3545  +      rc = sqlite3BtreeCreateTable(pCx->pBtx, &pgno, BTREE_BLOBKEY | pOp->p5); 
  3546   3546         if( rc==SQLITE_OK ){
  3547   3547           assert( pgno==MASTER_ROOT+1 );
  3548   3548           assert( pKeyInfo->db==db );
  3549   3549           assert( pKeyInfo->enc==ENC(db) );
  3550         -        pCx->pKeyInfo = pKeyInfo;
  3551         -        rc = sqlite3BtreeCursor(pCx->pBt, pgno, BTREE_WRCSR,
         3550  +        rc = sqlite3BtreeCursor(pCx->pBtx, pgno, BTREE_WRCSR,
  3552   3551                                   pKeyInfo, pCx->uc.pCursor);
  3553   3552         }
  3554   3553         pCx->isTable = 0;
  3555   3554       }else{
  3556         -      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, BTREE_WRCSR,
         3555  +      rc = sqlite3BtreeCursor(pCx->pBtx, MASTER_ROOT, BTREE_WRCSR,
  3557   3556                                 0, pCx->uc.pCursor);
  3558   3557         pCx->isTable = 1;
  3559   3558       }
  3560   3559     }
  3561   3560     if( rc ) goto abort_due_to_error;
  3562   3561     pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
  3563   3562     break;
................................................................................
  5988   5987   **
  5989   5988   ** if r[P1] is zero or negative, that means there is no LIMIT
  5990   5989   ** and r[P2] is set to -1. 
  5991   5990   **
  5992   5991   ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
  5993   5992   */
  5994   5993   case OP_OffsetLimit: {    /* in1, out2, in3 */
         5994  +  i64 x;
  5995   5995     pIn1 = &aMem[pOp->p1];
  5996   5996     pIn3 = &aMem[pOp->p3];
  5997   5997     pOut = out2Prerelease(p, pOp);
  5998   5998     assert( pIn1->flags & MEM_Int );
  5999   5999     assert( pIn3->flags & MEM_Int );
  6000         -  pOut->u.i = pIn1->u.i<=0 ? -1 : pIn1->u.i+(pIn3->u.i>0?pIn3->u.i:0);
         6000  +  x = pIn1->u.i;
         6001  +  if( x<=0 || sqlite3AddInt64(&x, pIn3->u.i>0?pIn3->u.i:0) ){
         6002  +    /* If the LIMIT is less than or equal to zero, loop forever.  This
         6003  +    ** is documented.  But also, if the LIMIT+OFFSET exceeds 2^63 then
         6004  +    ** also loop forever.  This is undocumented.  In fact, one could argue
         6005  +    ** that the loop should terminate.  But assuming 1 billion iterations
         6006  +    ** per second (far exceeding the capabilities of any current hardware)
         6007  +    ** it would take nearly 300 years to actually reach the limit.  So
         6008  +    ** looping forever is a reasonable approximation. */
         6009  +    pOut->u.i = -1;
         6010  +  }else{
         6011  +    pOut->u.i = x;
         6012  +  }
  6001   6013     break;
  6002   6014   }
  6003   6015   
  6004   6016   /* Opcode: IfNotZero P1 P2 P3 * *
  6005   6017   ** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2
  6006   6018   **
  6007   6019   ** Register P1 must contain an integer.  If the content of register P1 is
................................................................................
  6019   6031     }
  6020   6032     break;
  6021   6033   }
  6022   6034   
  6023   6035   /* Opcode: DecrJumpZero P1 P2 * * *
  6024   6036   ** Synopsis: if (--r[P1])==0 goto P2
  6025   6037   **
  6026         -** Register P1 must hold an integer.  Decrement the value in register P1
  6027         -** then jump to P2 if the new value is exactly zero.
         6038  +** Register P1 must hold an integer.  Decrement the value in P1
         6039  +** and jump to P2 if the new value is exactly zero.
  6028   6040   */
  6029   6041   case OP_DecrJumpZero: {      /* jump, in1 */
  6030   6042     pIn1 = &aMem[pOp->p1];
  6031   6043     assert( pIn1->flags&MEM_Int );
  6032         -  pIn1->u.i--;
         6044  +  if( pIn1->u.i>SMALLEST_INT64 ) pIn1->u.i--;
  6033   6045     VdbeBranchTaken(pIn1->u.i==0, 2);
  6034   6046     if( pIn1->u.i==0 ) goto jump_to_p2;
  6035   6047     break;
  6036   6048   }
  6037   6049   
  6038   6050   
  6039   6051   /* Opcode: AggStep0 * P2 P3 P4 P5

Changes to src/vdbe.h.

   106    106   
   107    107   /*
   108    108   ** Allowed values of VdbeOp.p4type
   109    109   */
   110    110   #define P4_NOTUSED    0   /* The P4 parameter is not used */
   111    111   #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
   112    112   #define P4_STATIC   (-2)  /* Pointer to a static string */
   113         -#define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
   114         -#define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
   115         -#define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
   116         -#define P4_EXPR     (-7)  /* P4 is a pointer to an Expr tree */
   117         -#define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
          113  +#define P4_COLLSEQ  (-3)  /* P4 is a pointer to a CollSeq structure */
          114  +#define P4_FUNCDEF  (-4)  /* P4 is a pointer to a FuncDef structure */
          115  +#define P4_KEYINFO  (-5)  /* P4 is a pointer to a KeyInfo structure */
          116  +#define P4_EXPR     (-6)  /* P4 is a pointer to an Expr tree */
          117  +#define P4_MEM      (-7)  /* P4 is a pointer to a Mem*    structure */
   118    118   #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
   119         -#define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
   120         -#define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
   121         -#define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
   122         -#define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
   123         -#define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
   124         -#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
   125         -#define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
   126         -#define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
   127         -#define P4_TABLE    (-20) /* P4 is a pointer to a Table structure */
   128         -#define P4_FUNCCTX  (-21) /* P4 is a pointer to an sqlite3_context object */
          119  +#define P4_VTAB     (-8) /* P4 is a pointer to an sqlite3_vtab structure */
          120  +#define P4_REAL     (-9) /* P4 is a 64-bit floating point value */
          121  +#define P4_INT64    (-10) /* P4 is a 64-bit signed integer */
          122  +#define P4_INT32    (-11) /* P4 is a 32-bit signed integer */
          123  +#define P4_INTARRAY (-12) /* P4 is a vector of 32-bit integers */
          124  +#define P4_SUBPROGRAM  (-13) /* P4 is a pointer to a SubProgram structure */
          125  +#define P4_ADVANCE  (-14) /* P4 is a pointer to BtreeNext() or BtreePrev() */
          126  +#define P4_TABLE    (-15) /* P4 is a pointer to a Table structure */
          127  +#define P4_FUNCCTX  (-16) /* P4 is a pointer to an sqlite3_context object */
   129    128   
   130    129   /* Error message codes for OP_Halt */
   131    130   #define P5_ConstraintNotNull 1
   132    131   #define P5_ConstraintUnique  2
   133    132   #define P5_ConstraintCheck   3
   134    133   #define P5_ConstraintFK      4
   135    134   
................................................................................
   194    193   void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
   195    194   void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
   196    195   void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
   197    196   void sqlite3VdbeJumpHere(Vdbe*, int addr);
   198    197   int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
   199    198   int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
   200    199   void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
          200  +void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
   201    201   void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
   202    202   void sqlite3VdbeUsesBtree(Vdbe*, int);
   203    203   VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
   204    204   int sqlite3VdbeMakeLabel(Vdbe*);
   205    205   void sqlite3VdbeRunOnlyOnce(Vdbe*);
   206    206   void sqlite3VdbeReusable(Vdbe*);
   207    207   void sqlite3VdbeDelete(Vdbe*);

Changes to src/vdbeInt.h.

    69     69   **          -  On either an index or a table
    70     70   **      * A sorter
    71     71   **      * A virtual table
    72     72   **      * A one-row "pseudotable" stored in a single register
    73     73   */
    74     74   typedef struct VdbeCursor VdbeCursor;
    75     75   struct VdbeCursor {
    76         -  u8 eCurType;          /* One of the CURTYPE_* values above */
    77         -  i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
    78         -  u8 nullRow;           /* True if pointing to a row with no data */
    79         -  u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
    80         -  u8 isTable;           /* True for rowid tables.  False for indexes */
           76  +  u8 eCurType;            /* One of the CURTYPE_* values above */
           77  +  i8 iDb;                 /* Index of cursor database in db->aDb[] (or -1) */
           78  +  u8 nullRow;             /* True if pointing to a row with no data */
           79  +  u8 deferredMoveto;      /* A call to sqlite3BtreeMoveto() is needed */
           80  +  u8 isTable;             /* True for rowid tables.  False for indexes */
    81     81   #ifdef SQLITE_DEBUG
    82         -  u8 seekOp;            /* Most recent seek operation on this cursor */
    83         -  u8 wrFlag;            /* The wrFlag argument to sqlite3BtreeCursor() */
           82  +  u8 seekOp;              /* Most recent seek operation on this cursor */
           83  +  u8 wrFlag;              /* The wrFlag argument to sqlite3BtreeCursor() */
    84     84   #endif
    85         -  Bool isEphemeral:1;   /* True for an ephemeral table */
    86         -  Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
    87         -  Bool isOrdered:1;     /* True if the table is not BTREE_UNORDERED */
    88         -  Pgno pgnoRoot;        /* Root page of the open btree cursor */
    89         -  i16 nField;           /* Number of fields in the header */
    90         -  u16 nHdrParsed;       /* Number of header fields parsed so far */
           85  +  Bool isEphemeral:1;     /* True for an ephemeral table */
           86  +  Bool useRandomRowid:1;  /* Generate new record numbers semi-randomly */
           87  +  Bool isOrdered:1;       /* True if the table is not BTREE_UNORDERED */
           88  +  Btree *pBtx;            /* Separate file holding temporary table */
           89  +  i64 seqCount;           /* Sequence counter */
           90  +  int *aAltMap;           /* Mapping from table to index column numbers */
           91  +
           92  +  /* Cached OP_Column parse information is only valid if cacheStatus matches
           93  +  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
           94  +  ** CACHE_STALE (0) and so setting cacheStatus=CACHE_STALE guarantees that
           95  +  ** the cache is out of date. */
           96  +  u32 cacheStatus;        /* Cache is valid if this matches Vdbe.cacheCtr */
           97  +  int seekResult;         /* Result of previous sqlite3BtreeMoveto() or 0
           98  +                          ** if there have been no prior seeks on the cursor. */
           99  +  /* NB: seekResult does not distinguish between "no seeks have ever occurred
          100  +  ** on this cursor" and "the most recent seek was an exact match". */
          101  +
          102  +  /* When a new VdbeCursor is allocated, only the fields above are zeroed.
          103  +  ** The fields that follow are uninitialized, and must be individually
          104  +  ** initialized prior to first use. */
          105  +  VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
    91    106     union {
    92    107       BtCursor *pCursor;          /* CURTYPE_BTREE.  Btree cursor */
    93    108       sqlite3_vtab_cursor *pVCur; /* CURTYPE_VTAB.   Vtab cursor */
    94    109       int pseudoTableReg;         /* CURTYPE_PSEUDO. Reg holding content. */
    95    110       VdbeSorter *pSorter;        /* CURTYPE_SORTER. Sorter object */
    96    111     } uc;
    97         -  Btree *pBt;           /* Separate file holding temporary table */
    98         -  KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
    99         -  int seekResult;       /* Result of previous sqlite3BtreeMoveto() or 0
   100         -                        ** if there have been no prior seeks on the cursor. */
   101         -  /* NB: seekResult does not distinguish between "no seeks have ever occurred
   102         -  ** on this cursor" and "the most recent seek was an exact match". */
   103         -  i64 seqCount;         /* Sequence counter */
   104         -  i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
   105         -  VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
   106         -  int *aAltMap;           /* Mapping from table to index column numbers */
          112  +  KeyInfo *pKeyInfo;      /* Info about index keys needed by index cursors */
          113  +  u32 iHdrOffset;         /* Offset to next unparsed byte of the header */
          114  +  Pgno pgnoRoot;          /* Root page of the open btree cursor */
          115  +  i16 nField;             /* Number of fields in the header */
          116  +  u16 nHdrParsed;         /* Number of header fields parsed so far */
          117  +  i64 movetoTarget;       /* Argument to the deferred sqlite3BtreeMoveto() */
          118  +  u32 *aOffset;           /* Pointer to aType[nField] */
          119  +  const u8 *aRow;         /* Data for the current row, if all on one page */
          120  +  u32 payloadSize;        /* Total number of bytes in the record */
          121  +  u32 szRow;              /* Byte available in aRow */
   107    122   #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
   108         -  u64 maskUsed;         /* Mask of columns used by this cursor */
          123  +  u64 maskUsed;           /* Mask of columns used by this cursor */
   109    124   #endif
   110    125   
   111         -  /* Cached information about the header for the data record that the
   112         -  ** cursor is currently pointing to.  Only valid if cacheStatus matches
   113         -  ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
   114         -  ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
   115         -  ** the cache is out of date.
   116         -  **
   117         -  ** aRow might point to (ephemeral) data for the current row, or it might
   118         -  ** be NULL.
   119         -  */
   120         -  u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
   121         -  u32 payloadSize;      /* Total number of bytes in the record */
   122         -  u32 szRow;            /* Byte available in aRow */
   123         -  u32 iHdrOffset;       /* Offset to next unparsed byte of the header */
   124         -  const u8 *aRow;       /* Data for the current row, if all on one page */
   125         -  u32 *aOffset;         /* Pointer to aType[nField] */
   126         -  u32 aType[1];         /* Type values for all entries in the record */
   127    126     /* 2*nField extra array elements allocated for aType[], beyond the one
   128    127     ** static element declared in the structure.  nField total array slots for
   129    128     ** aType[] and nField+1 array slots for aOffset[] */
          129  +  u32 aType[1];           /* Type values record decode.  MUST BE LAST */
   130    130   };
   131    131   
   132    132   
   133    133   /*
   134    134   ** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
   135    135   */
   136    136   #define CACHE_STALE 0

Changes to src/vdbeaux.c.

   309    309     int op,             /* The new opcode */
   310    310     int p1,             /* The P1 operand */
   311    311     int p2,             /* The P2 operand */
   312    312     int p3,             /* The P3 operand */
   313    313     int p4              /* The P4 operand as an integer */
   314    314   ){
   315    315     int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
   316         -  sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
          316  +  if( p->db->mallocFailed==0 ){
          317  +    VdbeOp *pOp = &p->aOp[addr];
          318  +    pOp->p4type = P4_INT32;
          319  +    pOp->p4.i = p4;
          320  +  }
   317    321     return addr;
   318    322   }
   319    323   
   320    324   /* Insert the end of a co-routine
   321    325   */
   322    326   void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
   323    327     sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
................................................................................
   820    824       }
   821    825   #ifdef SQLITE_ENABLE_CURSOR_HINTS
   822    826       case P4_EXPR: {
   823    827         sqlite3ExprDelete(db, (Expr*)p4);
   824    828         break;
   825    829       }
   826    830   #endif
   827         -    case P4_MPRINTF: {
   828         -      if( db->pnBytesFreed==0 ) sqlite3_free(p4);
   829         -      break;
   830         -    }
   831    831       case P4_FUNCDEF: {
   832    832         freeEphemeralFunction(db, (FuncDef*)p4);
   833    833         break;
   834    834       }
   835    835       case P4_MEM: {
   836    836         if( db->pnBytesFreed==0 ){
   837    837           sqlite3ValueFree((sqlite3_value*)p4);
................................................................................
   967    967     }else if( zP4!=0 ){
   968    968       assert( n<0 );
   969    969       pOp->p4.p = (void*)zP4;
   970    970       pOp->p4type = (signed char)n;
   971    971       if( n==P4_VTAB ) sqlite3VtabLock((VTable*)zP4);
   972    972     }
   973    973   }
          974  +
          975  +/*
          976  +** Change the P4 operand of the most recently coded instruction 
          977  +** to the value defined by the arguments.  This is a high-speed
          978  +** version of sqlite3VdbeChangeP4().
          979  +**
          980  +** The P4 operand must not have been previously defined.  And the new
          981  +** P4 must not be P4_INT32.  Use sqlite3VdbeChangeP4() in either of
          982  +** those cases.
          983  +*/
          984  +void sqlite3VdbeAppendP4(Vdbe *p, void *pP4, int n){
          985  +  VdbeOp *pOp;
          986  +  assert( n!=P4_INT32 && n!=P4_VTAB );
          987  +  assert( n<=0 );
          988  +  if( p->db->mallocFailed ){
          989  +    freeP4(p->db, n, pP4);
          990  +  }else{
          991  +    assert( pP4!=0 );
          992  +    assert( p->nOp>0 );
          993  +    pOp = &p->aOp[p->nOp-1];
          994  +    assert( pOp->p4type==P4_NOTUSED );
          995  +    pOp->p4type = n;
          996  +    pOp->p4.p = pP4;
          997  +  }
          998  +}
   974    999   
   975   1000   /*
   976   1001   ** Set the P4 on the most recently added opcode to the KeyInfo for the
   977   1002   ** index given.
   978   1003   */
   979   1004   void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
   980   1005     Vdbe *v = pParse->pVdbe;
         1006  +  KeyInfo *pKeyInfo;
   981   1007     assert( v!=0 );
   982   1008     assert( pIdx!=0 );
   983         -  sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
   984         -                      P4_KEYINFO);
         1009  +  pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pIdx);
         1010  +  if( pKeyInfo ) sqlite3VdbeAppendP4(v, pKeyInfo, P4_KEYINFO);
   985   1011   }
   986   1012   
   987   1013   #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
   988   1014   /*
   989   1015   ** Change the comment on the most recently coded instruction.  Or
   990   1016   ** insert a No-op and add the comment to that new instruction.  This
   991   1017   ** makes the code easier to read during debugging.  None of this happens
................................................................................
  1976   2002   ** Close a VDBE cursor and release all the resources that cursor 
  1977   2003   ** happens to hold.
  1978   2004   */
  1979   2005   void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
  1980   2006     if( pCx==0 ){
  1981   2007       return;
  1982   2008     }
  1983         -  assert( pCx->pBt==0 || pCx->eCurType==CURTYPE_BTREE );
         2009  +  assert( pCx->pBtx==0 || pCx->eCurType==CURTYPE_BTREE );
  1984   2010     switch( pCx->eCurType ){
  1985   2011       case CURTYPE_SORTER: {
  1986   2012         sqlite3VdbeSorterClose(p->db, pCx);
  1987   2013         break;
  1988   2014       }
  1989   2015       case CURTYPE_BTREE: {
  1990         -      if( pCx->pBt ){
  1991         -        sqlite3BtreeClose(pCx->pBt);
         2016  +      if( pCx->pBtx ){
         2017  +        sqlite3BtreeClose(pCx->pBtx);
  1992   2018           /* The pCx->pCursor will be close automatically, if it exists, by
  1993   2019           ** the call above. */
  1994   2020         }else{
  1995   2021           assert( pCx->uc.pCursor!=0 );
  1996   2022           sqlite3BtreeCloseCursor(pCx->uc.pCursor);
  1997   2023         }
  1998   2024         break;

Changes to src/vdbemem.c.

  1329   1329           pVal->u.i = -pVal->u.i;
  1330   1330         }
  1331   1331         sqlite3ValueApplyAffinity(pVal, affinity, enc);
  1332   1332       }
  1333   1333     }else if( op==TK_NULL ){
  1334   1334       pVal = valueNew(db, pCtx);
  1335   1335       if( pVal==0 ) goto no_mem;
         1336  +    sqlite3VdbeMemNumerify(pVal);
  1336   1337     }
  1337   1338   #ifndef SQLITE_OMIT_BLOB_LITERAL
  1338   1339     else if( op==TK_BLOB ){
  1339   1340       int nVal;
  1340   1341       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
  1341   1342       assert( pExpr->u.zToken[1]=='\'' );
  1342   1343       pVal = valueNew(db, pCtx);

Changes to src/vdbesort.c.

   955    955     ** to exceed the maximum merge count */
   956    956   #if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
   957    957     if( nWorker>=SORTER_MAX_MERGE_COUNT ){
   958    958       nWorker = SORTER_MAX_MERGE_COUNT-1;
   959    959     }
   960    960   #endif
   961    961   
   962         -  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
          962  +  assert( pCsr->pKeyInfo && pCsr->pBtx==0 );
   963    963     assert( pCsr->eCurType==CURTYPE_SORTER );
   964    964     szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
   965    965     sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);
   966    966   
   967    967     pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
   968    968     pCsr->uc.pSorter = pSorter;
   969    969     if( pSorter==0 ){

Changes to src/wherecode.c.

  1138   1138           codeExprOrVector(pParse, pRight, iTarget, 1);
  1139   1139         }
  1140   1140       }
  1141   1141       sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
  1142   1142       sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
  1143   1143       sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
  1144   1144                         pLoop->u.vtab.idxStr,
  1145         -                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
         1145  +                      pLoop->u.vtab.needFree ? P4_DYNAMIC : P4_STATIC);
  1146   1146       VdbeCoverage(v);
  1147   1147       pLoop->u.vtab.needFree = 0;
  1148   1148       pLevel->p1 = iCur;
  1149   1149       pLevel->op = pWInfo->eOnePass ? OP_Noop : OP_VNext;
  1150   1150       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  1151   1151       iIn = pLevel->u.in.nIn;
  1152   1152       for(j=nConstraint-1; j>=0; j--){
................................................................................
  1171   1171             assert( pOp->opcode!=OP_Rowid || pOp->p2==iReg+j+2 );
  1172   1172             testcase( pOp->opcode==OP_Rowid );
  1173   1173             sqlite3VdbeAddOp3(v, pOp->opcode, pOp->p1, pOp->p2, pOp->p3);
  1174   1174           }
  1175   1175   
  1176   1176           /* Generate code that will continue to the next row if 
  1177   1177           ** the IN constraint is not satisfied */
  1178         -        pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0, 0);
         1178  +        pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
  1179   1179           assert( pCompare!=0 || db->mallocFailed );
  1180   1180           if( pCompare ){
  1181   1181             pCompare->pLeft = pTerm->pExpr->pLeft;
  1182   1182             pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
  1183   1183             if( pRight ){
  1184   1184               pRight->iTable = iReg+j+2;
  1185   1185               sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
................................................................................
  1770   1770           if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
  1771   1771           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
  1772   1772           testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
  1773   1773           pExpr = sqlite3ExprDup(db, pExpr, 0);
  1774   1774           pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
  1775   1775         }
  1776   1776         if( pAndExpr ){
  1777         -        pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr, 0);
         1777  +        pAndExpr = sqlite3PExpr(pParse, TK_AND|TKFLG_DONTFOLD, 0, pAndExpr);
  1778   1778         }
  1779   1779       }
  1780   1780   
  1781   1781       /* Run a separate WHERE clause for each term of the OR clause.  After
  1782   1782       ** eliminating duplicates from other WHERE clauses, the action for each
  1783   1783       ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
  1784   1784       */

Changes to src/whereexpr.c.

   730    730           assert( pOrTerm->u.leftColumn==iColumn );
   731    731           pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
   732    732           pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
   733    733           pLeft = pOrTerm->pExpr->pLeft;
   734    734         }
   735    735         assert( pLeft!=0 );
   736    736         pDup = sqlite3ExprDup(db, pLeft, 0);
   737         -      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
          737  +      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
   738    738         if( pNew ){
   739    739           int idxNew;
   740    740           transferJoinMarkings(pNew, pExpr);
   741    741           assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   742    742           pNew->x.pList = pList;
   743    743           idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
   744    744           testcase( idxNew==0 );
................................................................................
  1028   1028       assert( pList!=0 );
  1029   1029       assert( pList->nExpr==2 );
  1030   1030       for(i=0; i<2; i++){
  1031   1031         Expr *pNewExpr;
  1032   1032         int idxNew;
  1033   1033         pNewExpr = sqlite3PExpr(pParse, ops[i], 
  1034   1034                                sqlite3ExprDup(db, pExpr->pLeft, 0),
  1035         -                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
         1035  +                             sqlite3ExprDup(db, pList->a[i].pExpr, 0));
  1036   1036         transferJoinMarkings(pNewExpr, pExpr);
  1037   1037         idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1038   1038         testcase( idxNew==0 );
  1039   1039         exprAnalyze(pSrc, pWC, idxNew);
  1040   1040         pTerm = &pWC->a[idxTerm];
  1041   1041         markTermAsChild(pWC, idxNew, idxTerm);
  1042   1042       }
................................................................................
  1113   1113         }
  1114   1114         *pC = c + 1;
  1115   1115       }
  1116   1116       zCollSeqName = noCase ? "NOCASE" : "BINARY";
  1117   1117       pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
  1118   1118       pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
  1119   1119              sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
  1120         -           pStr1, 0);
         1120  +           pStr1);
  1121   1121       transferJoinMarkings(pNewExpr1, pExpr);
  1122   1122       idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
  1123   1123       testcase( idxNew1==0 );
  1124   1124       exprAnalyze(pSrc, pWC, idxNew1);
  1125   1125       pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
  1126   1126       pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
  1127   1127              sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
  1128         -           pStr2, 0);
         1128  +           pStr2);
  1129   1129       transferJoinMarkings(pNewExpr2, pExpr);
  1130   1130       idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
  1131   1131       testcase( idxNew2==0 );
  1132   1132       exprAnalyze(pSrc, pWC, idxNew2);
  1133   1133       pTerm = &pWC->a[idxTerm];
  1134   1134       if( isComplete ){
  1135   1135         markTermAsChild(pWC, idxNew1, idxTerm);
................................................................................
  1154   1154       pRight = pExpr->x.pList->a[0].pExpr;
  1155   1155       pLeft = pExpr->x.pList->a[1].pExpr;
  1156   1156       prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
  1157   1157       prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
  1158   1158       if( (prereqExpr & prereqColumn)==0 ){
  1159   1159         Expr *pNewExpr;
  1160   1160         pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
  1161         -                              0, sqlite3ExprDup(db, pRight, 0), 0);
         1161  +                              0, sqlite3ExprDup(db, pRight, 0));
  1162   1162         idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1163   1163         testcase( idxNew==0 );
  1164   1164         pNewTerm = &pWC->a[idxNew];
  1165   1165         pNewTerm->prereqRight = prereqExpr;
  1166   1166         pNewTerm->leftCursor = pLeft->iTable;
  1167   1167         pNewTerm->u.leftColumn = pLeft->iColumn;
  1168   1168         pNewTerm->eOperator = WO_MATCH;
................................................................................
  1193   1193       assert( nLeft==sqlite3ExprVectorSize(pExpr->pRight) );
  1194   1194       for(i=0; i<nLeft; i++){
  1195   1195         int idxNew;
  1196   1196         Expr *pNew;
  1197   1197         Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
  1198   1198         Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
  1199   1199   
  1200         -      pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight, 0);
         1200  +      pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
  1201   1201         transferJoinMarkings(pNew, pExpr);
  1202   1202         idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
  1203   1203         exprAnalyze(pSrc, pWC, idxNew);
  1204   1204       }
  1205   1205       pTerm = &pWC->a[idxTerm];
  1206   1206       pTerm->wtFlags = TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
  1207   1207       pTerm->eOperator = 0;
................................................................................
  1245   1245       Expr *pNewExpr;
  1246   1246       Expr *pLeft = pExpr->pLeft;
  1247   1247       int idxNew;
  1248   1248       WhereTerm *pNewTerm;
  1249   1249   
  1250   1250       pNewExpr = sqlite3PExpr(pParse, TK_GT,
  1251   1251                               sqlite3ExprDup(db, pLeft, 0),
  1252         -                            sqlite3ExprAlloc(db, TK_NULL, 0, 0), 0);
         1252  +                            sqlite3ExprAlloc(db, TK_NULL, 0, 0));
  1253   1253   
  1254   1254       idxNew = whereClauseInsert(pWC, pNewExpr,
  1255   1255                                 TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
  1256   1256       if( idxNew ){
  1257   1257         pNewTerm = &pWC->a[idxNew];
  1258   1258         pNewTerm->prereqRight = 0;
  1259   1259         pNewTerm->leftCursor = pLeft->iTable;
................................................................................
  1431   1431       }
  1432   1432       pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
  1433   1433       if( pColRef==0 ) return;
  1434   1434       pColRef->iTable = pItem->iCursor;
  1435   1435       pColRef->iColumn = k++;
  1436   1436       pColRef->pTab = pTab;
  1437   1437       pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
  1438         -                         sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0), 0);
         1438  +                         sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
  1439   1439       whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
  1440   1440     }
  1441   1441   }

Changes to test/analyzeF.test.

   116    116     SELECT * FROM t1 WHERE x = dstr() AND y = 11;
   117    117   } {1 {string or blob too big}}
   118    118   
   119    119   do_catchsql_test 4.4 {
   120    120     SELECT * FROM t1 WHERE x = test_zeroblob(1100000) AND y = 4;
   121    121   } {1 {string or blob too big}}
   122    122   
          123  +# 2016-12-08: Constraints of the form "x=? AND x IS NOT NULL" were being
          124  +# mishandled.  The sqlite3Stat4ProbeSetValue() routine was assuming that
          125  +# valueNew() was returning a Mem object that was preset to NULL, which is
          126  +# not the case.  The consequence was the the "x IS NOT NULL" constraint
          127  +# was used to drive the index (via the "x>NULL" pseudo-constraint) rather
          128  +# than the "x=?" constraint.
          129  +#
          130  +do_execsql_test 5.1 {
          131  +  DROP TABLE IF EXISTS t1;
          132  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT, c INT);
          133  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<10000)
          134  +    INSERT INTO t1(a, c) SELECT x, x FROM c;
          135  +  UPDATE t1 SET b=printf('x%02x',a/500) WHERE a>4000;
          136  +  UPDATE t1 SET b='xyz' where a>=9998;
          137  +  CREATE INDEX t1b ON t1(b);
          138  +  ANALYZE;
          139  +  SELECT count(*), b FROM t1 GROUP BY 2 ORDER BY 2;
          140  +} {4000 {} 499 x08 500 x09 500 x0a 500 x0b 500 x0c 500 x0d 500 x0e 500 x0f 500 x10 500 x11 500 x12 498 x13 3 xyz}
          141  +do_execsql_test 5.2 {
          142  +  explain query plan
          143  +  SELECT * FROM t1 WHERE b='xyz' AND b IS NOT NULL ORDER BY +a;
          144  +  /*                  v---- Should be "=", not ">"  */
          145  +} {/USING INDEX t1b .b=/}
          146  +do_execsql_test 5.3 {
          147  +  SELECT * FROM t1 WHERE b='xyz' AND b IS NOT NULL ORDER BY +a;
          148  +} {9998 xyz 9998 9999 xyz 9999 10000 xyz 10000}
   123    149   
   124    150   finish_test

Changes to test/date.test.

    58     58   datetest 1.18.4 {julianday('2000-01-01T 12:00:00')} 2451545.0
    59     59   datetest 1.18.4 {julianday('2000-01-01 T 12:00:00')} 2451545.0
    60     60   datetest 1.19 {julianday('2000-01-01 12:00:00.1')}   2451545.00000116
    61     61   datetest 1.20 {julianday('2000-01-01 12:00:00.01')}  2451545.00000012
    62     62   datetest 1.21 {julianday('2000-01-01 12:00:00.001')} 2451545.00000001
    63     63   datetest 1.22 {julianday('2000-01-01 12:00:00.')} NULL
    64     64   datetest 1.23 julianday(12345.6) 12345.6
    65         -datetest 1.23b julianday('12345.6') 12345.6
           65  +datetest 1.23b julianday(1721059.5) 1721059.5
    66     66   datetest 1.24 {julianday('2001-01-01 12:00:00 bogus')} NULL
    67     67   datetest 1.25 {julianday('2001-01-01 bogus')} NULL
    68     68   datetest 1.26 {julianday('2001-01-01 12:60:00')} NULL
    69     69   datetest 1.27 {julianday('2001-01-01 12:59:60')} NULL
    70     70   datetest 1.28 {julianday('2001-00-01')} NULL
    71     71   datetest 1.29 {julianday('2001-01-00')} NULL
    72     72   
................................................................................
   555    555   } {0.0}
   556    556   do_test date-15.2 {
   557    557     db eval {
   558    558        SELECT a==b FROM (SELECT current_timestamp AS a,
   559    559                                  sleeper(), current_timestamp AS b);
   560    560     }
   561    561   } {1}
          562  +
          563  +# Tests of extreme values in date/time functions.  Run with UBSan or the
          564  +# equivalent to verify no signed interger overflow warnings.
          565  +#
          566  +datetest 16.1 {date(147483649)} NULL
          567  +datetest 16.2 {datetime(0)} {-4713-11-24 12:00:00}
          568  +datetest 16.3 {datetime(5373484.49999999)} {9999-12-31 23:59:59}
          569  +datetest 16.4 {julianday('-4713-11-24 12:00:00')} 0.0
          570  +datetest 16.5 {julianday('9999-12-31 23:59:59.999')} 5373484.49999999
          571  +datetest 16.6 {datetime(0,'+464269060799 seconds')} {9999-12-31 23:59:59}
          572  +datetest 16.7 {datetime(0,'+464269060800 seconds')} NULL
          573  +datetest 16.8 {datetime(0,'+7737817679 minutes')} {9999-12-31 23:59:00}
          574  +datetest 16.9 {datetime(0,'+7737817680 minutes')} NULL
          575  +datetest 16.10 {datetime(0,'+128963627 hours')} {9999-12-31 23:00:00}
          576  +datetest 16.11 {datetime(0,'+128963628 hours')} NULL
          577  +datetest 16.12 {datetime(0,'+5373484 days')} {9999-12-31 12:00:00}
          578  +datetest 16.13 {datetime(0,'+5373485 days')} NULL
          579  +datetest 16.14 {datetime(0,'+176545 months')} {9999-12-24 12:00:00}
          580  +datetest 16.15 {datetime(0,'+176546 months')} NULL
          581  +datetest 16.16 {datetime(0,'+14712 years')} {9999-11-24 12:00:00}
          582  +datetest 16.17 {datetime(0,'+14713 years')} NULL
          583  +datetest 16.20 {datetime(5373484.4999999,'-464269060799 seconds')} \
          584  +                {-4713-11-24 12:00:00}
          585  +datetest 16.21 {datetime(5373484,'-464269060800 seconds')} NULL
          586  +datetest 16.22 {datetime(5373484.4999999,'-7737817679 minutes')} \
          587  +               {-4713-11-24 12:00:59}
          588  +datetest 16.23 {datetime(5373484,'-7737817680 minutes')} NULL
          589  +datetest 16.24 {datetime(5373484.4999999,'-128963627 hours')} \
          590  +               {-4713-11-24 12:59:59}
          591  +datetest 16.25 {datetime(5373484,'-128963628 hours')} NULL
          592  +datetest 16.26 {datetime(5373484,'-5373484 days')} {-4713-11-24 12:00:00}
          593  +datetest 16.27 {datetime(5373484,'-5373485 days')} NULL
          594  +datetest 16.28 {datetime(5373484,'-176545 months')} {-4713-12-01 12:00:00}
          595  +datetest 16.29 {datetime(5373484,'-176546 months')} NULL
          596  +datetest 16.30 {datetime(5373484,'-14712 years')} {-4713-12-31 12:00:00}
          597  +datetest 16.31 {datetime(5373484,'-14713 years')} NULL
          598  +
   562    599   
   563    600   finish_test

Changes to test/fuzzdata5.db.

cannot compute difference between binary files

Changes to test/hexlit.test.

   108    108   #
   109    109   do_catchsql_test hexlist-400 {
   110    110     SELECT 0x10000000000000000;
   111    111   } {1 {hex literal too big: 0x10000000000000000}}
   112    112   do_catchsql_test hexlist-401 {
   113    113     SELECT DISTINCT 0x10000000000000000;
   114    114   } {1 {hex literal too big: 0x10000000000000000}}
          115  +do_catchsql_test hexlist-402 {
          116  +  SELECT DISTINCT -0x08000000000000000;
          117  +} {1 {hex literal too big: -0x08000000000000000}}
   115    118   do_catchsql_test hexlist-410 {
   116    119     DROP TABLE IF EXISTS t1;
   117    120     CREATE TABLE t1(x);
   118    121     INSERT INTO t1 VALUES(1+0x10000000000000000);
   119    122   } {1 {hex literal too big: 0x10000000000000000}}
   120    123   
   121    124   
   122    125   finish_test

Changes to test/join5.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.
    12     12   #
    13     13   # This file implements tests for left outer joins containing ON
    14     14   # clauses that restrict the scope of the left term of the join.
    15     15   #
    16         -# $Id: join5.test,v 1.2 2007/06/08 00:20:48 drh Exp $
    17     16   
    18     17   set testdir [file dirname $argv0]
    19     18   source $testdir/tester.tcl
           19  +set testprefix join5
    20     20   
    21     21   
    22     22   do_test join5-1.1 {
    23     23     execsql {
    24     24       BEGIN;
    25     25       CREATE TABLE t1(a integer primary key, b integer, c integer);
    26     26       CREATE TABLE t2(x integer primary key, y);
................................................................................
   179    179     ) c ON b.fruit='banana';
   180    180   } {apple apple {} banana banana 1}
   181    181   do_execsql_test join6-4.2 {
   182    182     SELECT *
   183    183       FROM (SELECT 'apple' fruit UNION ALL SELECT 'banana')
   184    184            LEFT JOIN (SELECT 1) ON fruit='banana';
   185    185   } {apple {} banana 1}
          186  +
          187  +#-------------------------------------------------------------------------
          188  +do_execsql_test 5.0 {
          189  +  CREATE TABLE y1(x, y, z);
          190  +  INSERT INTO y1 VALUES(0, 0, 1);
          191  +  CREATE TABLE y2(a);
          192  +}
          193  +
          194  +do_execsql_test 5.1 {
          195  +  SELECT count(z) FROM y1 LEFT JOIN y2 ON x GROUP BY y;
          196  +} 1
          197  +
          198  +do_execsql_test 5.2 {
          199  +  SELECT count(z) FROM ( SELECT * FROM y1 ) LEFT JOIN y2 ON x GROUP BY y;
          200  +} 1
          201  +
          202  +do_execsql_test 5.3 {
          203  +  CREATE VIEW v1 AS SELECT x, y, z FROM y1;
          204  +  SELECT count(z) FROM v1 LEFT JOIN y2 ON x GROUP BY y;
          205  +} 1
          206  +
          207  +do_execsql_test 5.4 {
          208  +  SELECT count(z) FROM ( SELECT * FROM y1 ) LEFT JOIN y2 ON x
          209  +} 1
          210  +
          211  +do_execsql_test 5.5 {
          212  +  SELECT * FROM ( SELECT * FROM y1 ) LEFT JOIN y2 ON x
          213  +} {0 0 1 {}}
   186    214   
   187    215   finish_test

Changes to test/like.test.

   976    976   do_execsql_test like-13.3 {
   977    977     SELECT char(0x304d) LIKE char(0x6d);
   978    978   } {0}
   979    979   do_execsql_test like-13.4 {
   980    980     SELECT char(0x4d) LIKE char(0x6d);
   981    981   } {1}
   982    982   
   983         -
          983  +# Performance testing for patterns with many wildcards.  These LIKE and GLOB
          984  +# patterns were quite slow with SQLite 3.15.2 and earlier.
          985  +#
          986  +do_test like-14.1 {
          987  +  set x [lindex [time {
          988  +    db one {SELECT 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz'GLOB'*a*a*a*a*a*a*a*a*y'}
          989  +  }] 0]
          990  +  puts -nonewline " ($x ms - want less than 1000) "
          991  +  expr {$x<1000}
          992  +} {1}
          993  +ifcapable !icu {
          994  +  do_test like-14.2 {
          995  +    set x [lindex [time {
          996  +      db one {SELECT 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz'LIKE'%a%a%a%a%a%a%a%a%y'}
          997  +    }] 0]
          998  +    puts -nonewline " ($x ms - want less than 1000) "
          999  +    expr {$x<1000}
         1000  +  } {1}
         1001  +}
   984   1002   
   985   1003   finish_test

Changes to test/rowvalue.test.

   286    286   do_execsql_test 14.2 "SELECT CASE (2,2) WHEN (1, 1) THEN 2 ELSE 1 END" 1
   287    287   do_execsql_test 14.3 "SELECT CASE (SELECT 2,2) WHEN (1, 1) THEN 2 ELSE 1 END" 1
   288    288   do_execsql_test 14.4 "SELECT 1 WHERE (SELECT 2,2) BETWEEN (1,1) AND (3,3)" 1
   289    289   do_execsql_test 14.5 "SELECT 1 FROM t12 WHERE (x,1) BETWEEN (1,1) AND (3,3)" 1
   290    290   do_execsql_test 14.6 {
   291    291     SELECT 1 FROM t12 WHERE (1,x) BETWEEN (1,1) AND (3,3)
   292    292   } {1 1}
          293  +
          294  +#-------------------------------------------------------------------------
          295  +# Test that errors are not concealed by the SELECT flattening or
          296  +# WHERE-clause push-down optimizations.
          297  +do_execsql_test 14.1 {
          298  +  CREATE TABLE x1(a PRIMARY KEY, b);
          299  +  CREATE TABLE x2(a INTEGER PRIMARY KEY, b);
          300  +}
          301  +
          302  +foreach {tn n sql} {
          303  +  1 0 "SELECT * FROM (SELECT (1, 1) AS c FROM x1) WHERE c=1"
          304  +  2 2 "SELECT * FROM (SELECT 1 AS x, (SELECT 8,9) AS y) WHERE y<1"
          305  +  3 3 "SELECT * FROM (SELECT 1 AS x, (SELECT 8,9,10) AS y) WHERE y<1"
          306  +  4 0 "SELECT * FROM (SELECT (a, b) AS c FROM x1), x2 WHERE c=a"
          307  +  5 0 "SELECT * FROM (SELECT a AS c, (1, 2, 3) FROM x1), x2 WHERE c=a"
          308  +  6 0 "SELECT * FROM (SELECT 1 AS c, (1, 2, 3) FROM x1) WHERE c=1"
          309  +} {
          310  +  if {$n==0} {
          311  +    set err "row value misused"
          312  +  } else {
          313  +    set err "sub-select returns $n columns - expected 1"
          314  +  }
          315  +  do_catchsql_test 14.2.$tn $sql [list 1 $err]
          316  +}
          317  +
   293    318   
   294    319   finish_test
   295    320   
   296    321   

Changes to test/tabfunc01.test.

    19     19   
    20     20   ifcapable !vtab {
    21     21     finish_test
    22     22     return
    23     23   }
    24     24   load_static_extension db series
    25     25   load_static_extension db carray
           26  +load_static_extension db remember
    26     27   
    27     28   do_execsql_test tabfunc01-1.1 {
    28     29     SELECT *, '|' FROM generate_series WHERE start=1 AND stop=9 AND step=2;
    29     30   } {1 | 3 | 5 | 7 | 9 |}
    30     31   do_execsql_test tabfunc01-1.2 {
    31     32     SELECT *, '|' FROM generate_series LIMIT 5;
    32     33   } {0 | 1 | 2 | 3 | 4 |}
................................................................................
   168    169   
   169    170   do_test tabfunc01-720 {
   170    171     set PTR [int64array_addr 5 7 13 17 23]
   171    172     db eval {
   172    173       SELECT b FROM t600, carray($PTR,5,'int64') WHERE a=value;
   173    174     }
   174    175   } {(005) (007) (013) (017) (023)}
          176  +do_test tabfunc01-721 {
          177  +  db eval {
          178  +    SELECT remember(123,$PTR);
          179  +    SELECT value FROM carray($PTR,5,'int64');
          180  +  }
          181  +} {123 123 7 13 17 23}
          182  +do_test tabfunc01-722 {
          183  +  set PTR2 [expr {$PTR+16}]
          184  +  db eval {
          185  +    SELECT remember(987,$PTR2);
          186  +    SELECT value FROM carray($PTR,5,'int64');
          187  +  }
          188  +} {987 123 7 987 17 23}
   175    189   
   176    190   do_test tabfunc01-730 {
   177    191     set PTR [doublearray_addr 5.0 7.0 13.0 17.0 23.0]
   178    192     db eval {
   179    193       SELECT b FROM t600, carray($PTR,5,'double') WHERE a=value;
   180    194     }
   181    195   } {(005) (007) (013) (017) (023)}

Changes to tool/cg_anno.tcl.

     1      1   #!/usr/bin/tclsh
     2      2   #
     3      3   # A wrapper around cg_annotate that sets appropriate command-line options
     4      4   # and rearranges the output so that annotated files occur in a consistent
     5         -# sorted order.  Used by the run-speed-test.tcl script.
            5  +# sorted order.  Used by the speed-check.tcl script.
     6      6   #
     7      7   
     8      8   set in [open "|cg_annotate --show=Ir --auto=yes --context=40 $argv" r]
     9      9   set dest !
    10     10   set out(!) {}
           11  +set linenum 0
           12  +set cntlines 0      ;# true to remember cycle counts on each line
           13  +set seenSqlite3 0   ;# true if we have seen the sqlite3.c file
    11     14   while {![eof $in]} {
    12     15     set line [string map {\t {        }} [gets $in]]
    13     16     if {[regexp {^-- Auto-annotated source: (.*)} $line all name]} {
    14     17       set dest $name
    15         -  } elseif {[regexp {^-- line \d+ ------} $line]} {
           18  +    if {[string match */sqlite3.c $dest]} {
           19  +      set cntlines 1
           20  +      set seenSqlite3 1
           21  +    } else {
           22  +      set cntlines 0
           23  +    }
           24  +  } elseif {[regexp {^-- line (\d+) ------} $line all ln]} {
    16     25       set line [lreplace $line 2 2 {#}]
           26  +    set linenum [expr {$ln-1}]
    17     27     } elseif {[regexp {^The following files chosen for } $line]} {
    18     28       set dest !
    19     29     }
    20     30     append out($dest) $line\n
           31  +  if {$cntlines} {
           32  +    incr linenum
           33  +    if {[regexp {^ *([0-9,]+) } $line all x]} {
           34  +      set x [string map {, {}} $x]
           35  +      set cycles($linenum) $x
           36  +    }
           37  +  }
    21     38   }
    22     39   foreach x [lsort [array names out]] {
    23     40     puts $out($x)
    24     41   }
           42  +
           43  +# If the sqlite3.c file has been seen, then output a summary of the
           44  +# cycle counts for each file that went into making up sqlite3.c
           45  +#
           46  +if {$seenSqlite3} {
           47  +  close $in
           48  +  set in [open sqlite3.c]
           49  +  set linenum 0
           50  +  set fn sqlite3.c
           51  +  set pattern1 {^/\*+ Begin file ([^ ]+) \*}
           52  +  set pattern2 {^/\*+ Continuing where we left off in ([^ ]+) \*}
           53  +  while {![eof $in]} {
           54  +    set line [gets $in]
           55  +    incr linenum
           56  +    if {[regexp $pattern1 $line all newfn]} {
           57  +      set fn $newfn
           58  +    } elseif {[regexp $pattern2 $line all newfn]} {
           59  +      set fn $newfn
           60  +    } elseif {[info exists cycles($linenum)]} {
           61  +      incr fcycles($fn) $cycles($linenum)
           62  +    }
           63  +  }
           64  +  close $in
           65  +  puts {**********************************************************************}
           66  +  set lx {}
           67  +  set sum 0
           68  +  foreach {fn cnt} [array get fcycles] {
           69  +    lappend lx [list $cnt $fn]
           70  +    incr sum $cnt
           71  +  }
           72  +  puts [format {%20s %14d  %8.3f%%} TOTAL $sum 100]
           73  +  foreach entry [lsort -index 0 -integer -decreasing $lx] {
           74  +    foreach {cnt fn} $entry break
           75  +    puts [format {%20s %14d  %8.3f%%} $fn $cnt [expr {$cnt*100.0/$sum}]]
           76  +  }
           77  +}

Changes to tool/lempar.c.

   534    534   }
   535    535   
   536    536   /*
   537    537   ** The following routine is called if the stack overflows.
   538    538   */
   539    539   static void yyStackOverflow(yyParser *yypParser){
   540    540      ParseARG_FETCH;
   541         -   yypParser->yytos--;
   542    541   #ifndef NDEBUG
   543    542      if( yyTraceFILE ){
   544    543        fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
   545    544      }
   546    545   #endif
   547    546      while( yypParser->yytos>yypParser->yystack ) yy_pop_parser_stack(yypParser);
   548    547      /* Here code is inserted which will execute if the parser
................................................................................
   588    587     if( (int)(yypParser->yytos - yypParser->yystack)>yypParser->yyhwm ){
   589    588       yypParser->yyhwm++;
   590    589       assert( yypParser->yyhwm == (int)(yypParser->yytos - yypParser->yystack) );
   591    590     }
   592    591   #endif
   593    592   #if YYSTACKDEPTH>0 
   594    593     if( yypParser->yytos>=&yypParser->yystack[YYSTACKDEPTH] ){
          594  +    yypParser->yytos--;
   595    595       yyStackOverflow(yypParser);
   596    596       return;
   597    597     }
   598    598   #else
   599    599     if( yypParser->yytos>=&yypParser->yystack[yypParser->yystksz] ){
   600    600       if( yyGrowStack(yypParser) ){
          601  +      yypParser->yytos--;
   601    602         yyStackOverflow(yypParser);
   602    603         return;
   603    604       }
   604    605     }
   605    606   #endif
   606    607     if( yyNewState > YY_MAX_SHIFT ){
   607    608       yyNewState += YY_MIN_REDUCE - YY_MIN_SHIFTREDUCE;

Changes to tool/showstat4.c.

   126    126         }else if( (iVal&1)==0 ){
   127    127           printf("%sx'", zSep);
   128    128           for(j=0; j<sz; j++){
   129    129             printf("%02x", aSample[y+j]);
   130    130           }
   131    131           printf("'");
   132    132         }else{
   133         -        printf("%s\"", zSep);
          133  +        printf("%s'", zSep);
   134    134           for(j=0; j<sz; j++){
   135    135             char c = (char)aSample[y+j];
   136    136             if( ISPRINT(c) ){
   137         -            if( c=='"' || c=='\\' ) putchar('\\');
          137  +            if( c=='\'' || c=='\\' ) putchar('\\');
   138    138               putchar(c);
   139    139             }else if( c=='\n' ){
   140    140               printf("\\n");
   141    141             }else if( c=='\t' ){
   142    142               printf("\\t");
   143    143             }else if( c=='\r' ){
   144    144               printf("\\r");
   145    145             }else{
   146    146               printf("\\%03o", c);
   147    147             }
   148    148           }
   149         -        printf("\"");
          149  +        printf("'");
   150    150         }
   151    151         zSep = ",";
   152    152         y += sz;
   153    153       }
   154    154       printf("\n");
   155    155     }
   156    156     sqlite3_free(zIdx);
   157    157     sqlite3_finalize(pStmt);
   158    158     sqlite3_close(db);
   159    159     return 0;
   160    160   }