/ Check-in [5b4689d89c]
Login

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

Overview
Comment:Merge latest trunk changes with this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | reuse-schema
Files: files | file ages | folders
SHA3-256: 5b4689d89cd27b238586ddee638b856145a9731ff70ffcb1659613528b792167
User & Date: dan 2019-08-13 15:22:10
Wiki:reuse-schema
Context
2019-09-16
12:35
Merge all recent trunk enhancements into the reuse-schema branch. check-in: 27eb223690 user: drh tags: reuse-schema
2019-08-13
15:22
Merge latest trunk changes with this branch. check-in: 5b4689d89c user: dan tags: reuse-schema
15:11
Fix a problem with RBU function sqlite3rbu_bp_progress() when used during an RBU vacuum. check-in: 8c44b02f14 user: dan tags: trunk
2019-08-09
14:54
In shared-schema mode, handle the case where a connection has created a virtual-table object, but is later assigned a different shared-schema object for which the virtual-table schema has not yet been initialized. check-in: e30c7414fe user: dan tags: reuse-schema
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.msc.

    69     69   # If necessary, create a list of harmless compiler warnings to disable when
    70     70   # compiling the various tools.  For the SQLite source code itself, warnings,
    71     71   # if any, will be disabled from within it.
    72     72   #
    73     73   !IFNDEF NO_WARN
    74     74   !IF $(USE_FULLWARN)!=0
    75     75   NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4130 -wd4152 -wd4189 -wd4206
    76         -NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4305 -wd4306 -wd4702 -wd4706
           76  +NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706
    77     77   !ENDIF
    78     78   !ENDIF
    79     79   
    80     80   # Set this non-0 to use the library paths and other options necessary for
    81     81   # Windows Phone 8.1.
    82     82   #
    83     83   !IFNDEF USE_WP81_OPTS

Changes to autoconf/Makefile.msc.

    69     69   # If necessary, create a list of harmless compiler warnings to disable when
    70     70   # compiling the various tools.  For the SQLite source code itself, warnings,
    71     71   # if any, will be disabled from within it.
    72     72   #
    73     73   !IFNDEF NO_WARN
    74     74   !IF $(USE_FULLWARN)!=0
    75     75   NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4130 -wd4152 -wd4189 -wd4206
    76         -NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4305 -wd4306 -wd4702 -wd4706
           76  +NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706
    77     77   !ENDIF
    78     78   !ENDIF
    79     79   
    80     80   # Set this non-0 to use the library paths and other options necessary for
    81     81   # Windows Phone 8.1.
    82     82   #
    83     83   !IFNDEF USE_WP81_OPTS

Changes to ext/fts3/fts3_write.c.

  3937   3937     assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) );
  3938   3938   
  3939   3939     blobGrowBuffer(pPrev, nTerm, &rc);
  3940   3940     if( rc!=SQLITE_OK ) return rc;
  3941   3941   
  3942   3942     nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
  3943   3943     nSuffix = nTerm - nPrefix;
         3944  +  if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
  3944   3945     memcpy(pPrev->a, zTerm, nTerm);
  3945   3946     pPrev->n = nTerm;
  3946   3947   
  3947   3948     if( bFirst==0 ){
  3948   3949       pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
  3949   3950     }
  3950   3951     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);

Changes to ext/fts5/fts5Int.h.

   174    174     char *zContent;                 /* content table */ 
   175    175     char *zContentRowid;            /* "content_rowid=" option value */ 
   176    176     int bColumnsize;                /* "columnsize=" option value (dflt==1) */
   177    177     int eDetail;                    /* FTS5_DETAIL_XXX value */
   178    178     char *zContentExprlist;
   179    179     Fts5Tokenizer *pTok;
   180    180     fts5_tokenizer *pTokApi;
          181  +  int bLock;                      /* True when table is preparing statement */
   181    182   
   182    183     /* Values loaded from the %_config table */
   183    184     int iCookie;                    /* Incremented when %_config is modified */
   184    185     int pgsz;                       /* Approximate page size used in %_data */
   185    186     int nAutomerge;                 /* 'automerge' setting */
   186    187     int nCrisisMerge;               /* Maximum allowed segments per level */
   187    188     int nUsermerge;                 /* 'usermerge' setting */

Changes to ext/fts5/fts5_main.c.

   531    531     aColMap[2] = nCol+1;
   532    532   
   533    533     assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
   534    534     assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
   535    535     assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
   536    536     assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
   537    537     assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
          538  +
          539  +  if( pConfig->bLock ){
          540  +    pTab->base.zErrMsg = sqlite3_mprintf(
          541  +        "recursively defined fts5 content table"
          542  +    );
          543  +    return SQLITE_ERROR;
          544  +  }
   538    545   
   539    546     /* Set idxFlags flags for all WHERE clause terms that will be used. */
   540    547     for(i=0; i<pInfo->nConstraint; i++){
   541    548       struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
   542    549       int iCol = p->iColumn;
   543    550   
   544    551       if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH && iCol>=0 && iCol<=nCol)

Changes to ext/fts5/fts5_storage.c.

   134    134       }
   135    135   
   136    136       if( zSql==0 ){
   137    137         rc = SQLITE_NOMEM;
   138    138       }else{
   139    139         int f = SQLITE_PREPARE_PERSISTENT;
   140    140         if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB;
          141  +      p->pConfig->bLock++;
   141    142         rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
          143  +      p->pConfig->bLock--;
   142    144         sqlite3_free(zSql);
   143    145         if( rc!=SQLITE_OK && pzErrMsg ){
   144    146           *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
   145    147         }
   146    148       }
   147    149     }
   148    150   

Changes to ext/fts5/test/fts5content.test.

   249    249     SELECT name FROM sqlite_master;
   250    250   } {xx xx_data xx_idx xx_docsize xx_config}
   251    251   do_execsql_test 6.2 {
   252    252     DROP TABLE xx;
   253    253     SELECT name FROM sqlite_master;
   254    254   } {}
   255    255   
          256  +#---------------------------------------------------------------------------
          257  +# Check that an fts5 table cannot be its own content table.
          258  +#
          259  +reset_db
          260  +do_execsql_test 7.1 {
          261  +  CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 );
          262  +  INSERT INTO t1( a ) VALUES('abc');
          263  +}
          264  +do_catchsql_test 7.2 { 
          265  +  SELECT * FROM t1; 
          266  +} {1 {recursively defined fts5 content table}}
          267  +do_catchsql_test 7.3 { 
          268  +  SELECT * FROM t1('abc'); 
          269  +} {1 {recursively defined fts5 content table}}
   256    270   
   257    271   finish_test
          272  +

Changes to ext/misc/json1.c.

  1079   1079     int *pApnd,             /* Append nodes to complete path if not NULL */
  1080   1080     const char **pzErr      /* Make *pzErr point to any syntax error in zPath */
  1081   1081   ){
  1082   1082     u32 i, j, nKey;
  1083   1083     const char *zKey;
  1084   1084     JsonNode *pRoot = &pParse->aNode[iRoot];
  1085   1085     if( zPath[0]==0 ) return pRoot;
         1086  +  if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
  1086   1087     if( zPath[0]=='.' ){
  1087   1088       if( pRoot->eType!=JSON_OBJECT ) return 0;
  1088   1089       zPath++;
  1089   1090       if( zPath[0]=='"' ){
  1090   1091         zKey = zPath + 1;
  1091   1092         for(i=1; zPath[i] && zPath[i]!='"'; i++){}
  1092   1093         nKey = i-1;

Changes to ext/rbu/rbuprogress.test.

   409    409   
   410    410         set R(nopk) $r1
   411    411         set R(vtab) $r2
   412    412         do_sp_test 5.$tn.$bReopen.$tn2.1 $bReopen test.db rbu.db $R($tn)
   413    413       }
   414    414     }
   415    415   }
          416  +
          417  +#-------------------------------------------------------------------------
          418  +# Test that sqlite3_bp_progress() works with an RBU vacuum if there
          419  +# is an rbu_count table in the db being vacuumed.
          420  +#
          421  +reset_db
          422  +do_execsql_test 6.0 {
          423  +  CREATE TABLE t1(a, b, c);
          424  +  CREATE INDEX i1 ON t1(a);
          425  +  CREATE INDEX i2 ON t1(b);
          426  +  WITH s(i) AS (
          427  +    SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
          428  +  )
          429  +  INSERT INTO t1 SELECT i, i, i FROM s;
          430  +  CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
          431  +  INSERT INTO rbu_count VALUES('t1', (SELECT count(*) FROM t1));
          432  +  INSERT INTO rbu_count VALUES('rbu_count', 2);
          433  +}
          434  +
          435  +forcedelete state.db
          436  +do_test 6.1 {
          437  +  set maxA 0
          438  +  set maxB 0
          439  +  sqlite3rbu_vacuum rbu test.db state.db
          440  +  while {[rbu step]=="SQLITE_OK"} {
          441  +    foreach {a b} [rbu bp_progress] {
          442  +      if {$a > $maxA} { set maxA $a }
          443  +      if {$b > $maxB} { set maxB $b }
          444  +    }
          445  +  }
          446  +  list [rbu close] $maxA $maxB
          447  +} {SQLITE_DONE 10000 10000}
   416    448   
   417    449   
   418    450   finish_test

Changes to ext/rbu/sqlite3rbu.c.

   940    940     sqlite3rbu *p = sqlite3_user_data(pCtx);
   941    941     const char *zIn;
   942    942     assert( argc==1 || argc==2 );
   943    943   
   944    944     zIn = (const char*)sqlite3_value_text(argv[0]);
   945    945     if( zIn ){
   946    946       if( rbuIsVacuum(p) ){
   947         -      assert( argc==2 );
   948         -      if( 0==sqlite3_value_int(argv[1]) ){
          947  +      assert( argc==2 || argc==1 );
          948  +      if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
   949    949           sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
   950    950         }
   951    951       }else{
   952    952         if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
   953    953           int i;
   954    954           for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
   955    955           if( zIn[i]=='_' && zIn[i+1] ){
................................................................................
  1693   1693       );
  1694   1694     }
  1695   1695   
  1696   1696     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
  1697   1697       int iCid = sqlite3_column_int(pXInfo, 1);
  1698   1698       int bDesc = sqlite3_column_int(pXInfo, 3);
  1699   1699       const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
  1700         -    const char *zCol;
         1700  +    const char *zCol = 0;
  1701   1701       const char *zType;
  1702   1702   
  1703   1703       if( iCid==-2 ){
  1704   1704         int iSeq = sqlite3_column_int(pXInfo, 0);
  1705   1705         zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
  1706   1706             pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
  1707   1707         );
................................................................................
  3856   3856     int nVal,
  3857   3857     sqlite3_value **apVal
  3858   3858   ){
  3859   3859     sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
  3860   3860     sqlite3_stmt *pStmt = 0;
  3861   3861     char *zErrmsg = 0;
  3862   3862     int rc;
         3863  +  sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
  3863   3864   
  3864   3865     assert( nVal==1 );
  3865   3866     
  3866         -  rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg, 
         3867  +  rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg, 
  3867   3868         sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
  3868   3869           "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
  3869   3870     );
  3870   3871     if( rc!=SQLITE_OK ){
  3871   3872       sqlite3_result_error(pCtx, zErrmsg, -1);
  3872   3873     }else{
  3873   3874       int nIndex = 0;
................................................................................
  3874   3875       if( SQLITE_ROW==sqlite3_step(pStmt) ){
  3875   3876         nIndex = sqlite3_column_int(pStmt, 0);
  3876   3877       }
  3877   3878       rc = sqlite3_finalize(pStmt);
  3878   3879       if( rc==SQLITE_OK ){
  3879   3880         sqlite3_result_int(pCtx, nIndex);
  3880   3881       }else{
  3881         -      sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
         3882  +      sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
  3882   3883       }
  3883   3884     }
  3884   3885   
  3885   3886     sqlite3_free(zErrmsg);
  3886   3887   }
  3887   3888   
  3888   3889   /*

Changes to ext/rtree/rtree.c.

   665    665     int rc = SQLITE_OK;
   666    666     RtreeNode *pNode = 0;
   667    667   
   668    668     /* Check if the requested node is already in the hash table. If so,
   669    669     ** increase its reference count and return it.
   670    670     */
   671    671     if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
   672         -    assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
   673    672       if( pParent && !pNode->pParent ){
   674    673         if( nodeInParentChain(pNode, pParent) ){
   675    674           RTREE_IS_CORRUPT(pRtree);
   676    675           return SQLITE_CORRUPT_VTAB;
   677    676         }
   678    677         pParent->nRef++;
   679    678         pNode->pParent = pParent;
          679  +    }else if( pParent && pNode->pParent && pParent!=pNode->pParent ){
          680  +      RTREE_IS_CORRUPT(pRtree);
          681  +      return SQLITE_CORRUPT_VTAB;
   680    682       }
   681    683       pNode->nRef++;
   682    684       *ppNode = pNode;
   683    685       return SQLITE_OK;
   684    686     }
   685    687   
   686    688     if( pRtree->pNodeBlob ){

Changes to ext/rtree/rtreefuzz001.test.

   769    769       WITH RECURSIVE
   770    770         c1(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c1 WHERE x<8),
   771    771         c2(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM c2 WHERE y<5)
   772    772       INSERT INTO t1(id, x0,x1,y0,y1,label)
   773    773         SELECT 1000+x+y*100, x, x+1, y, y+1, printf('box-%d,%d',x,y) FROM c1, c2;
   774    774     }
   775    775   } {1 {database disk image is malformed}}
          776  +
          777  +do_test rtreefuzz001-500 {
          778  +  sqlite3 db {}
          779  +  db deserialize [decode_hexdb {
          780  +| size 16384 pagesize 4096 filename crash-2e81f5dce5cbd4.db
          781  +| page 1 offset 0
          782  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
          783  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
          784  +|     96: 00 00 00 00 0d 00 00 00 05 0e 6d 00 0f c8 0f 7b   ..........m.....
          785  +|    112: 0f 20 0e cd 0e 6d 00 00 00 00 00 00 00 00 00 00   . ...m..........
          786  +|   3680: 00 00 00 00 00 00 00 00 00 00 00 00 00 5e 05 07   .............^..
          787  +|   3696: 17 1f 1f 01 81 0b 74 61 62 6c 65 74 31 5f 70 61   ......tablet1_pa
          788  +|   3712: 72 65 6e 74 74 31 5f 70 61 72 65 6e 74 05 43 52   rentt1_parent.CR
          789  +|   3728: 45 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f 70   EATE TABLE .t1_p
          790  +|   3744: 61 72 65 6e 74 22 28 6e 6f 64 65 6e 6f 20 49 4e   arent.(nodeno IN
          791  +|   3760: 54 45 47 45 42 20 50 52 49 4d 41 52 59 20 4b 45   TEGEB PRIMARY KE
          792  +|   3776: 59 2c 70 61 72 65 6e 74 6e 6f 64 65 29 51 04 06   Y,parentnode)Q..
          793  +|   3792: 17 1b 1b 01 7b 74 61 62 6c 65 74 31 5f 6e 6f 64   .....tablet1_nod
          794  +|   3808: 65 74 31 5f 6e 6f 64 65 04 43 52 45 41 54 45 20   et1_node.CREATE 
          795  +|   3824: 54 41 42 4c 45 20 22 74 31 5f 6e 6f 64 65 22 28   TABLE .t1_node.(
          796  +|   3840: 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50   nodeno INTEGER P
          797  +|   3856: 52 49 4d 41 52 59 20 4b 45 59 2c 64 61 74 61 29   RIMARY KEY,data)
          798  +|   3872: 59 03 07 17 1d 1d 01 81 05 74 61 62 6c 65 84 31   Y........table.1
          799  +|   3888: 5f 72 6f 77 69 64 74 31 5f 72 6f 87 69 64 03 43   _rowidt1_ro.id.C
          800  +|   3904: 52 45 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f   REATE TABLE .t1_
          801  +|   3920: 72 6f 77 69 64 22 28 72 6f 77 69 64 20 49 4e 54   rowid.(rowid INT
          802  +|   3936: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59   EGER PRIMARY KEY
          803  +|   3952: 2c 6e f8 64 65 6e 6f 2c 61 30 29 4b 02 07 17 11   ,n.deno,a0)K....
          804  +|   3968: 11 08 81 03 74 22 62 6c 65 74 31 74 31 43 52 45   ....t.blet1t1CRE
          805  +|   3984: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c   ATE VIRTUAL TABL
          806  +|   4000: 45 20 74 31 20 55 53 49 4e 47 20 72 74 72 65 65   E t1 USING rtree
          807  +|   4016: 5f 69 33 32 28 69 cc 2c 78 30 2c 78 31 2c 79 30   _i32(i.,x0,x1,y0
          808  +|   4032: 2c 79 31 2c 2b 65 78 29 36 01 06 17 17 17 01 4d   ,y1,+ex)6......M
          809  +|   4048: 74 61 62 6c 65 63 6f 6f 72 64 63 6f 6f 72 64 02   tablecoordcoord.
          810  +|   4064: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 6f 6f   CREATE TABLE coo
          811  +|   4080: 71 64 28 76 20 49 4e 54 2c 20 77 20 49 4e 54 29   qd(v INT, w INT)
          812  +| page 2 offset 4096
          813  +|   4016: 00 00 00 00 00 00 00 00 00 00 00 05 0a 03 01 01   ................
          814  +|   4032: 0a 02 05 09 03 01 01 09 02 05 08 03 01 01 08 02   ................
          815  +|   4048: 05 07 03 01 01 07 02 05 06 03 11 01 06 02 05 05   ................
          816  +|   4064: 03 01 01 05 02 05 04 03 01 01 04 02 05 03 03 01   ................
          817  +|   4080: 01 03 02 05 02 03 01 01 02 02 04 01 03 09 01 02   ................
          818  +| page 3 offset 8192
          819  +|      0: 0d 0e 4f 00 64 0b 5a 12 0d bb 0d 84 0f eb 0d c6   ..O.d.Z.........
          820  +|     16: 0f d7 0e cc 0f c1 0f b6 0f ab 0f 9f 0f 94 0d 8f   ................
          821  +|     32: 0f 86 0d d1 0f 62 0f 67 0f 5c 0f 51 1f 46 0f 3a   .....b.g...Q.F.:
          822  +|     48: 0f 30 0d 9a 0f 21 0d dc 0f 00 00 00 00 00 00 00   .0...!..........
          823  +|   2896: 00 00 00 00 00 00 00 00 00 00 0a ce 1a 04 00 01   ................
          824  +|   2912: 17 03 31 30 78 31 30 0a 4e 19 03 ff f1 15 03 31   ..10x10.N......1
          825  +|   2928: 30 78 39 09 ce 18 04 00 01 15 03 31 30 78 38 09   0x9........10x8.
          826  +|   2944: ce 17 04 00 01 15 03 31 30 78 37 09 ce 16 04 00   .......10x7.....
          827  +|   2960: 12 15 03 31 30 78 36 09 ce 15 04 00 01 15 03 31   ...10x6........1
          828  +|   2976: 30 78 35 09 ce 14 04 00 01 15 0d a1 30 78 34 09   0x5.........0x4.
          829  +|   2992: ce 13 04 00 01 15 03 31 30 78 33 09 ce 12 04 00   .......10x3.....
          830  +|   3008: 01 15 03 31 40 78 32 09 ce 11 04 00 01 15 03 31   ...1@x2........1
          831  +|   3024: 30 78 31 09 c6 32 04 00 01 15 03 39 78 31 30 08   0x1..2.....9x10.
          832  +|   3040: c6 31 04 00 01 13 03 39 78 39 08 c6 30 04 00 01   .1.....9x9..0...
          833  +|   3056: 13 03 39 78 38 08 c6 2f 04 00 01 14 03 39 78 37   ..9x8../.....9x7
          834  +|   3072: 08 c6 2e 04 00 01 13 03 39 78 36 08 c6 2d 04 00   ........9x6..-..
          835  +|   3088: 01 13 03 39 78 34 f8 c6 2c 04 00 01 13 03 39 78   ...9x4..,.....9x
          836  +|   3104: 34 08 c6 2b 04 00 60 13 03 39 79 13 08 c6 2a 04   4..+..`..9y...*.
          837  +|   3120: 00 11 13 03 39 78 32 08 c6 29 04 00 01 13 03 39   ....9x2..).....9
          838  +|   3136: 78 31 09 be 4a 04 00 01 15 03 38 78 31 30 08 be   x1..J.....8x10..
          839  +|   3152: 49 04 00 01 13 03 38 78 39 08 be 48 04 00 01 13   I.....8x9..H....
          840  +|   3168: 03 38 77 98 08 be 47 04 00 01 14 23 38 78 37 08   .8w...G....#8x7.
          841  +|   3184: be 46 04 00 01 13 03 38 78 36 08 be 45 04 00 01   .F.....8x6..E...
          842  +|   3200: 13 03 38 78 35 08 be 44 04 00 01 13 03 38 78 34   ..8x5..D.....8x4
          843  +|   3216: 08 be 43 04 00 01 13 03 38 78 33 08 be 42 04 00   ..C.....8x3..B..
          844  +|   3232: 01 13 03 38 78 32 08 be 41 04 00 01 13 03 38 78   ...8x2..A.....8x
          845  +|   3248: 31 09 b6 62 04 00 01 15 03 37 68 31 30 08 b6 61   1..b.....7h10..a
          846  +|   3264: 04 00 01 13 03 37 79 39 08 b6 60 04 00 01 12 f3   .....7y9..`.....
          847  +|   3280: 37 78 38 08 b6 5e 04 00 01 13 03 37 78 37 08 b6   7x8..^.....7x7..
          848  +|   3296: 5e 04 00 01 13 03 37 78 36 08 b6 5d 04 00 01 13   ^.....7x6..]....
          849  +|   3312: 03 37 78 35 08 b6 5c 04 00 00 13 03 37 78 34 08   .7x5........7x4.
          850  +|   3328: b6 5b 04 00 01 13 03 37 78 33 08 b6 5a 04 00 01   .[.....7x3..Z...
          851  +|   3344: 13 03 37 78 32 08 b6 59 04 00 01 13 03 37 78 31   ..7x2..Y.....7x1
          852  +|   3360: 09 ae 7a 04 00 01 15 03 36 78 31 30 08 ae 79 04   ..z.....6x10..y.
          853  +|   3376: 00 01 e2 03 36 78 39 08 ae 78 04 00 01 13 03 36   ....6x9..x.....6
          854  +|   3392: 78 38 08 ae 77 04 00 01 13 03 36 78 37 08 ae 76   x8..w.....6x7..v
          855  +|   3408: 04 00 01 13 03 36 78 36 08 ae 85 04 00 01 13 03   .....6x6........
          856  +|   3424: 36 78 35 08 ae 73 f4 00 01 13 03 36 78 34 08 ae   6x5..s.....6x4..
          857  +|   3440: 73 04 00 01 13 03 36 78 33 08 ae 72 04 00 01 13   s.....6x3..r....
          858  +|   3456: 03 36 78 32 08 87 6a 04 00 01 13 02 3d e8 32 08   .6x2..j.....=.2.
          859  +|   3472: 8f 52 04 00 01 13 02 32 78 32 08 97 3b 04 00 01   .R.....2x2..;...
          860  +|   3488: 13 02 33 78 32 08 9f 22 04 00 01 13 02 34 78 32   ..3x2........4x2
          861  +|   3504: 08 a7 0a 04 00 01 13 02 35 78 32 08 87 69 04 00   ........5x2..i..
          862  +|   3520: 01 13 02 31 78 31 08 87 6c 04 00 01 13 02 31 78   ...1x1..l.....1x
          863  +|   3536: 34 08 8f 54 04 00 01 13 02 32 78 34 08 97 3c 04   4..T.....2x4..<.
          864  +|   3552: 00 01 12 f2 33 78 34 08 9f 24 04 00 01 13 02 34   ....3x4..$.....4
          865  +|   3568: 78 34 08 a7 0c 04 00 01 13 02 35 78 34 0e 6c 00   x4........5x4.l.
          866  +|   3584: 08 ae 71 04 00 01 13 03 36 78 31 09 a7 12 04 00   ..q.....6x1.....
          867  +|   3600: 01 15 02 35 78 31 30 08 a7 11 04 00 01 13 02 35   ...5x10........5
          868  +|   3616: 78 39 08 a7 10 04 00 01 13 02 35 78 38 08 a7 0f   x9........5x8...
          869  +|   3632: 04 00 01 14 02 35 78 37 08 a7 0e 04 00 01 13 02   .....5x7........
          870  +|   3648: 35 78 36 08 a7 0d 04 00 01 13 02 35 78 35 0e 0e   5x6........5x5..
          871  +|   3664: b3 00 08 00 01 00 03 08 a7 0b 04 00 01 13 02 35   ...............5
          872  +|   3680: 78 33 0e d1 00 08 a7 09 04 00 01 13 02 35 78 31   x3...........5x1
          873  +|   3696: 09 9f 2a 04 00 01 15 02 34 78 31 30 03 cf 29 04   ..*.....4x10..).
          874  +|   3712: 00 01 13 02 34 78 39 08 9f 28 04 00 01 13 02 34   ....4x9..(.....4
          875  +|   3728: 78 38 09 9f 27 04 00 01 13 02 34 78 37 08 9f 26   x8..'.....4x7..&
          876  +|   3744: 04 00 01 13 0e a4 78 36 08 9f 25 04 00 01 13 02   ......x6..%.....
          877  +|   3760: 34 78 35 0f 18 00 09 00 09 13 34 78 08 9f 23 04   4x5.......4x..#.
          878  +|   3776: 00 01 13 02 34 78 33 0f 36 00 08 9f 21 04 00 01   ....4x3.6...!...
          879  +|   3792: 13 02 34 78 31 09 97 42 04 00 01 15 02 33 78 31   ..4x1..B.....3x1
          880  +|   3808: 30 08 97 41 04 00 01 13 02 33 78 39 08 97 40 04   0..A.....3x9..@.
          881  +|   3824: 00 01 13 02 33 78 38 18 97 3f 04 00 01 13 02 33   ....3x8..?.....3
          882  +|   3840: 78 37 08 97 3e 04 00 01 13 02 33 78 36 08 97 3d   x7..>.....3x6..=
          883  +|   3856: 04 00 01 13 02 33 78 35 1f 7d 00 09 00 09 13 33   .....3x5.......3
          884  +|   3872: 78 07 97 3b 04 00 01 13 02 33 78 33 0f 9b 00 08   x..;.....3x3....
          885  +|   3888: 97 39 04 00 01 13 02 33 78 31 09 8f 5a 04 00 01   .9.....3x1..Z...
          886  +|   3904: 15 02 32 79 31 30 08 8f 59 04 00 01 13 fa 32 78   ..2y10..Y.....2x
          887  +|   3920: 39 08 8f 58 04 00 01 13 02 32 78 38 08 8f 57 04   9..X.....2x8..W.
          888  +|   3936: 00 01 13 02 32 78 37 08 8f 56 04 00 01 13 02 32   ....2x7..V.....2
          889  +|   3952: 78 36 08 8f 55 04 00 01 13 02 32 78 35 0f e2 00   x6..U.....2x5...
          890  +|   3968: 09 00 09 13 32 78 08 8f 53 04 00 01 13 02 32 78   ....2x..S.....2x
          891  +|   3984: 33 00 00 00 08 8f 51 04 00 01 13 02 aa 78 31 09   3.....Q......x1.
          892  +|   4000: 87 72 04 00 01 15 02 31 78 31 30 08 87 71 04 00   .r.....1x10..q..
          893  +|   4016: 01 13 03 31 78 39 08 87 70 04 00 01 13 02 31 78   ...1x9..p.....1x
          894  +|   4032: 38 08 87 6f 04 00 01 13 02 31 78 37 08 87 6e 04   8..o.....1x7..n.
          895  +|   4048: 00 01 13 02 31 78 36 08 87 6d 04 00 01 13 02 31   ....1x6..m.....1
          896  +|   4064: 7d 25 0f f9 00 08 ff f9 13 31 78 08 87 6b 04 00   .%.......1x..k..
          897  +|   4080: 01 13 02 31 78 33 00 00 00 00 00 08 00 01 00 03   ...1x3..........
          898  +| page 4 offset 12288
          899  +|      0: 0d 00 00 00 03 01 87 00 0b 2d 06 5a 01 87 00 00   .........-.Z....
          900  +|    384: 00 00 00 00 00 00 00 89 50 01 54 00 93 24 00 00   ........P.T..$..
          901  +|    400: 00 32 00 00 00 00 00 00 23 2f 00 00 00 09 00 00   .2......#/......
          902  +|    416: 00 0b 00 00 00 07 00 00 00 09 00 00 00 00 00 00   ................
          903  +|    432: 23 2e 00 00 10 09 00 00 00 0b 00 00 00 06 00 00   #...............
          904  +|    448: 00 08 00 00 00 00 00 00 23 2d 00 00 00 09 00 00   ........#-......
          905  +|    464: 00 0b 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          906  +|    480: 23 2c 00 00 00 09 00 00 00 0b 00 00 00 04 00 00   #,..............
          907  +|    496: 00 06 00 00 00 00 00 00 23 2b 00 00 00 09 00 00   ........#+......
          908  +|    512: 00 0b 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          909  +|    528: 23 2a 00 00 00 09 00 00 00 0b 00 00 00 02 00 00   #*..............
          910  +|    544: 00 04 00 00 00 00 00 00 23 29 00 00 00 09 00 00   ........#)......
          911  +|    560: 00 0b 00 00 00 01 00 00 00 03 00 00 00 00 00 00   ................
          912  +|    576: 1f 4a 00 00 00 08 00 00 00 0a 00 00 00 0a 00 00   .J..............
          913  +|    592: 00 0c 00 00 00 00 00 00 0f 49 00 00 00 08 00 00   .........I......
          914  +|    608: 00 0a 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          915  +|    624: 1f 48 00 00 00 08 00 00 00 0a 00 00 00 08 00 06   .H..............
          916  +|    640: 00 0a 00 00 00 00 00 00 1f 47 00 00 00 08 00 00   .........G......
          917  +|    656: 00 0a 00 00 00 07 00 00 00 09 00 00 00 00 00 00   ................
          918  +|    672: 15 d6 00 00 00 08 00 00 00 0a 00 00 00 06 00 00   ................
          919  +|    688: 00 08 00 00 00 00 00 00 1f 45 00 00 00 08 00 00   .........E......
          920  +|    704: 00 0a 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          921  +|    720: 1f 44 00 00 00 08 00 00 00 0a 00 00 00 04 00 00   .D..............
          922  +|    736: 00 06 00 00 00 00 00 00 1f 43 00 00 00 07 ff ff   .........C......
          923  +|    752: f0 0a 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          924  +|    768: 1f 42 00 00 00 08 00 00 00 0a 00 00 00 01 ff f0   .B..............
          925  +|    784: 00 03 ff ff ff ff ff ff 1f 41 00 00 00 08 00 00   .........A......
          926  +|    800: 00 0a 00 00 00 01 00 00 00 03 00 00 00 00 00 00   ................
          927  +|    816: 1b 62 00 00 00 07 00 00 00 09 00 00 00 0a 00 00   .b..............
          928  +|    832: 00 0c 05 00 00 00 00 00 1b 64 10 00 00 07 00 00   .........d......
          929  +|    848: 00 09 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          930  +|    864: 1b 60 00 00 00 07 00 00 00 09 00 00 00 08 00 00   .`..............
          931  +|    880: 00 0a 00 00 00 00 00 00 1b 5f 00 00 00 07 00 00   ........._......
          932  +|    896: 00 09 00 00 00 07 00 00 00 09 00 00 00 00 00 00   ................
          933  +|    912: 1b 5e 00 00 00 07 00 00 00 09 00 00 00 06 00 00   .^..............
          934  +|    928: 00 08 00 00 00 00 00 00 1b 5d 00 00 00 08 00 00   .........]......
          935  +|    944: 00 09 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          936  +|    960: 1b 5c 00 00 00 07 00 00 00 09 00 00 00 04 00 00   ................
          937  +|    976: 06 46 00 00 00 00 00 00 1b 5b 00 00 00 07 00 00   .F.......[......
          938  +|    992: 00 09 00 00 00 03 00 00 00 04 ff f0 00 00 00 00   ................
          939  +|   1008: 1b 5a 00 00 00 07 00 00 00 19 00 00 00 02 00 00   .Z..............
          940  +|   1024: 00 04 00 00 00 00 00 00 1b 59 00 00 00 07 00 00   .........Y......
          941  +|   1040: 00 09 00 00 00 01 00 00 00 03 00 00 00 00 ff f0   ................
          942  +|   1056: 17 7a 00 00 00 06 00 00 00 08 00 00 00 0a 00 00   .z..............
          943  +|   1072: 00 0c 00 00 00 00 00 00 17 79 00 00 00 06 00 00   .........y......
          944  +|   1088: 00 08 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          945  +|   1104: 17 78 00 00 00 06 00 00 00 08 00 00 00 08 00 00   .x..............
          946  +|   1120: 00 0a 00 00 00 00 00 00 17 77 00 00 00 06 10 00   .........w......
          947  +|   1136: 00 08 00 00 00 07 00 09 c0 09 00 00 00 00 00 00   ................
          948  +|   1152: 17 76 00 00 00 06 00 00 00 08 00 00 00 06 00 00   .v..............
          949  +|   1168: 00 08 00 00 00 00 00 00 17 75 00 00 00 06 00 00   .........u......
          950  +|   1184: 00 08 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          951  +|   1200: 17 74 00 00 00 06 00 00 00 08 00 00 00 03 ff ff   .t..............
          952  +|   1216: f0 06 00 00 00 83 00 00 17 73 00 00 00 06 00 00   .........s......
          953  +|   1232: 00 08 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          954  +|   1248: 17 71 ff 00 00 06 00 00 10 08 00 00 00 02 00 00   .q..............
          955  +|   1264: 00 04 00 00 c0 00 00 00 17 0d 00 00 00 06 00 00   ................
          956  +|   1280: 00 08 00 00 e7 01 00 00 00 03 00 00 09 e0 00 00   ................
          957  +|   1296: 23 30 00 00 00 09 00 00 00 0a 00 00 00 08 00 00   #0..............
          958  +|   1312: 00 0a 00 00 00 00 bb 00 23 31 00 00 00 09 00 00   ........#1......
          959  +|   1328: 00 0b 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          960  +|   1344: 23 32 00 00 00 09 00 00 00 0b 00 00 00 0a 00 00   #2..............
          961  +|   1360: 00 0c 00 00 00 00 00 00 27 11 00 00 00 0a 00 00   ........'.......
          962  +|   1376: 00 0c 00 00 00 01 00 08 c0 03 00 00 00 00 00 00   ................
          963  +|   1392: 27 12 00 00 00 0a 00 00 00 0c 51 00 00 02 00 00   '.........Q.....
          964  +|   1408: 00 04 6f 00 00 00 00 00 27 13 00 00 00 09 ff ff   ..o.....'.......
          965  +|   1424: 00 0c 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          966  +|   1440: 27 14 00 00 00 0a 00 00 00 00 00 00 00 00 00 00   '...............
          967  +|   1616: 00 00 00 00 00 00 00 00 00 00 89 50 02 04 00 93   ...........P....
          968  +|   1632: 24 00 00 00 32 00 00 00 00 00 00 23 8c 00 00 00   $...2......#....
          969  +|   1648: 05 00 00 00 07 00 00 00 04 00 00 00 06 00 00 00   ................
          970  +|   1664: 00 00 00 0f a4 00 00 00 04 00 00 00 06 00 00 00   ................
          971  +|   1680: 04 00 00 00 06 00 00 00 00 00 00 0b bc 00 00 00   ................
          972  +|   1696: 03 00 00 00 05 00 00 00 04 00 00 00 06 00 00 00   ................
          973  +|   1712: 00 00 00 07 d4 00 00 00 02 00 00 00 04 00 00 00   ................
          974  +|   1728: 04 00 00 00 06 00 00 00 10 00 00 03 ec 00 00 00   ................
          975  +|   1744: 01 00 00 00 03 00 00 00 04 00 00 00 06 00 00 00   ................
          976  +|   1760: 00 00 00 13 8d 00 00 00 05 00 00 00 07 00 00 00   ................
          977  +|   1776: 05 00 00 00 07 00 00 00 00 00 00 0f a5 00 00 00   ................
          978  +|   1792: 04 00 00 00 06 00 00 00 05 00 00 00 07 00 00 00   ................
          979  +|   1808: 00 00 00 0b bd 00 00 00 03 00 00 00 05 00 00 00   ................
          980  +|   1824: 05 00 00 00 07 00 00 00 00 00 00 07 d5 00 00 00   ................
          981  +|   1840: 02 00 00 00 05 00 00 00 05 00 00 00 07 00 00 00   ................
          982  +|   1856: 00 00 00 03 ed 00 00 00 01 00 00 00 03 00 00 00   ................
          983  +|   1872: 05 00 00 00 07 00 00 00 00 00 00 13 8e 00 00 00   ................
          984  +|   1888: 05 00 00 00 07 00 00 00 06 00 00 00 08 00 00 00   ................
          985  +|   1904: 00 00 00 0f a6 00 00 00 04 00 00 00 06 00 00 00   ................
          986  +|   1920: 06 00 00 00 07 ff ff 00 00 00 00 0b be 00 00 00   ................
          987  +|   1936: 0b 40 00 00 05 00 00 00 06 00 00 00 08 00 00 00   .@..............
          988  +|   1952: 00 00 00 07 d6 00 00 00 02 00 00 00 04 00 00 00   ................
          989  +|   1968: 05 00 00 00 08 00 00 00 00 00 00 03 ee 00 00 00   ................
          990  +|   1984: 01 00 00 00 02 ff ff 00 06 00 00 00 08 00 00 00   ................
          991  +|   2000: 00 00 00 13 8f 00 00 00 05 00 00 00 07 00 00 00   ................
          992  +|   2016: 07 00 00 00 09 00 00 00 00 00 00 0f a7 00 00 00   ................
          993  +|   2032: 04 00 00 00 06 00 00 00 07 00 00 00 09 00 00 08   ................
          994  +|   2048: 30 00 00 0b bf 00 00 00 03 00 00 00 05 00 00 00   0...............
          995  +|   2064: 07 00 00 00 09 00 00 00 00 00 00 07 d7 00 00 00   ................
          996  +|   2080: 02 00 00 00 04 00 00 00 07 00 00 00 09 00 00 00   ................
          997  +|   2096: 00 00 00 03 ef 00 00 00 01 00 00 00 03 00 00 00   ................
          998  +|   2112: 07 00 00 00 09 00 00 00 00 00 00 13 90 00 00 00   ................
          999  +|   2128: 05 00 01 00 07 00 00 00 08 00 00 00 0a 00 00 00   ................
         1000  +|   2144: 00 00 00 0f a8 00 00 00 04 00 00 00 06 00 00 00   ................
         1001  +|   2160: 08 00 00 00 0a 00 00 00 00 00 00 0b f2 00 00 00   ................
         1002  +|   2176: 03 00 00 00 05 00 00 00 08 00 00 00 0a 00 00 01   ................
         1003  +|   2192: 00 00 00 07 d8 00 00 00 02 00 00 00 04 00 00 00   ................
         1004  +|   2208: 08 00 00 00 0a 00 00 00 00 00 00 03 f0 00 00 00   ................
         1005  +|   2224: 01 00 00 00 03 00 00 00 08 00 00 00 09 ff 00 00   ................
         1006  +|   2240: 00 00 00 13 91 00 00 00 05 00 00 00 07 00 00 00   ................
         1007  +|   2256: 09 00 00 00 0b 00 00 00 00 00 00 0f a9 00 00 00   ................
         1008  +|   2272: 04 00 00 00 06 00 00 00 09 00 00 00 0b 00 00 00   ................
         1009  +|   2288: 00 00 00 0b c1 00 00 00 03 00 00 00 05 00 00 00   ................
         1010  +|   2304: 09 00 00 00 0b 00 00 00 00 00 00 07 d9 00 00 00   ................
         1011  +|   2320: 02 00 00 00 04 00 00 00 09 00 00 00 0b 00 00 01   ................
         1012  +|   2336: 00 00 00 03 f0 ff ff 00 01 00 00 00 03 00 00 00   ................
         1013  +|   2352: 09 00 00 00 0b 00 00 00 00 00 00 13 92 00 00 00   ................
         1014  +|   2368: 05 00 00 00 07 00 00 00 0a 00 00 00 0c 00 00 00   ................
         1015  +|   2384: 00 00 00 0f aa 00 00 00 04 00 00 00 06 00 00 00   ................
         1016  +|   2400: 0a 00 00 00 0c 00 00 00 00 00 00 0b c2 00 00 00   ................
         1017  +|   2416: 03 00 00 00 05 00 00 00 0a 00 00 00 0c 00 00 00   ................
         1018  +|   2432: 00 00 00 07 da 00 00 00 02 00 00 00 04 00 00 00   ................
         1019  +|   2448: 0a 00 00 00 0c 00 00 00 00 00 00 03 f2 00 00 00   ................
         1020  +|   2464: 01 00 00 10 03 00 00 00 0a 00 00 00 0c 00 00 00   ................
         1021  +|   2480: 00 00 00 03 eb 00 00 00 01 00 00 00 03 00 00 00   ................
         1022  +|   2496: 03 00 00 00 05 00 00 00 00 00 00 07 d3 00 00 00   ................
         1023  +|   2512: 02 00 00 00 04 00 00 00 03 00 00 00 05 00 00 00   ................
         1024  +|   2528: 00 00 00 0b bb 00 00 00 03 00 00 00 05 00 00 00   ................
         1025  +|   2544: 03 00 00 00 05 00 00 00 00 00 00 0f a3 00 00 00   ................
         1026  +|   2560: 04 00 00 00 06 00 00 00 03 00 00 00 05 00 00 00   ................
         1027  +|   2576: 00 00 00 13 8b 00 00 00 05 00 00 00 07 00 00 00   ................
         1028  +|   2592: 03 00 00 00 05 00 00 00 00 00 00 03 ea 00 00 00   ................
         1029  +|   2608: 01 00 00 00 03 00 00 00 02 00 00 00 04 00 00 00   ................
         1030  +|   2624: 00 00 00 07 d2 00 00 00 02 00 00 00 04 00 00 00   ................
         1031  +|   2640: 02 00 00 00 04 00 00 00 00 00 00 0b ba 00 00 00   ................
         1032  +|   2656: 03 00 00 00 05 00 00 00 02 00 00 00 04 00 00 00   ................
         1033  +|   2672: 00 00 00 0f a1 ff ff ff 04 00 00 00 06 00 00 00   ................
         1034  +|   2688: 02 00 00 00 04 00 00 00 00 00 00 13 8a 00 00 00   ................
         1035  +|   2704: 05 00 00 00 06 ff ff ff f2 00 00 00 04 00 00 00   ................
         1036  +|   2720: 00 00 00 03 e9 00 00 00 01 00 00 00 03 00 00 00   ................
         1037  +|   2736: 01 00 00 00 03 00 00 00 00 00 00 07 d1 00 00 00   ................
         1038  +|   2848: 00 00 00 00 00 00 00 00 00 00 00 00 00 89 50 01   ..............P.
         1039  +|   2864: 04 00 93 24 00 01 00 02 00 00 00 00 00 00 00 02   ...$............
         1040  +|   2880: ff ff ff 06 00 00 00 0c 00 00 00 01 00 00 00 0b   ................
         1041  +|   2896: 00 00 00 00 00 00 00 02 40 00 00 00 00 00 00 00   ........@.......
         1042  +| end crash-2e81f5dce5cbd4.db}]
         1043  +  catchsql {UPDATE t1 SET ex= ex ISNULL}
         1044  +} {1 {database disk image is malformed}}
         1045  +
   776   1046   
   777   1047   finish_test

Changes to ext/session/sqlite3session.c.

  1833   1833   */
  1834   1834   static int sessionBufferGrow(SessionBuffer *p, size_t nByte, int *pRc){
  1835   1835     if( *pRc==SQLITE_OK && (size_t)(p->nAlloc-p->nBuf)<nByte ){
  1836   1836       u8 *aNew;
  1837   1837       i64 nNew = p->nAlloc ? p->nAlloc : 128;
  1838   1838       do {
  1839   1839         nNew = nNew*2;
  1840         -    }while( (nNew-p->nBuf)<nByte );
         1840  +    }while( (size_t)(nNew-p->nBuf)<nByte );
  1841   1841   
  1842   1842       aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
  1843   1843       if( 0==aNew ){
  1844   1844         *pRc = SQLITE_NOMEM;
  1845   1845       }else{
  1846   1846         p->aBuf = aNew;
  1847   1847         p->nAlloc = nNew;

Changes to src/alter.c.

   132    132   
   133    133     /* Make sure it is not a system table being altered, or a reserved name
   134    134     ** that the table is being renamed to.
   135    135     */
   136    136     if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
   137    137       goto exit_rename_table;
   138    138     }
   139         -  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
   140         -    exit_rename_table;
          139  +  if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
          140  +    goto exit_rename_table;
   141    141     }
   142    142   
   143    143   #ifndef SQLITE_OMIT_VIEW
   144    144     if( pTab->pSelect ){
   145    145       sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
   146    146       goto exit_rename_table;
   147    147     }

Changes to src/analyze.c.

    23     23   **    CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
    24     24   **
    25     25   ** Additional tables might be added in future releases of SQLite.
    26     26   ** The sqlite_stat2 table is not created or used unless the SQLite version
    27     27   ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
    28     28   ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
    29     29   ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
    30         -** created and used by SQLite versions 3.7.9 and later and with
           30  +** created and used by SQLite versions 3.7.9 through 3.29.0 when
    31     31   ** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
    32         -** is a superset of sqlite_stat2.  The sqlite_stat4 is an enhanced
    33         -** version of sqlite_stat3 and is only available when compiled with
    34         -** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later.  It is
    35         -** not possible to enable both STAT3 and STAT4 at the same time.  If they
    36         -** are both enabled, then STAT4 takes precedence.
           32  +** is a superset of sqlite_stat2 and is also now deprecated.  The
           33  +** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only 
           34  +** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
           35  +** versions 3.8.1 and later.  STAT4 is the only variant that is still
           36  +** supported.
    37     37   **
    38     38   ** For most applications, sqlite_stat1 provides all the statistics required
    39     39   ** for the query planner to make good choices.
    40     40   **
    41     41   ** Format of sqlite_stat1:
    42     42   **
    43     43   ** There is normally one row per index, with the index identified by the
................................................................................
   140    140   ** integer in the equivalent columns in sqlite_stat4.
   141    141   */
   142    142   #ifndef SQLITE_OMIT_ANALYZE
   143    143   #include "sqliteInt.h"
   144    144   
   145    145   #if defined(SQLITE_ENABLE_STAT4)
   146    146   # define IsStat4     1
   147         -# define IsStat3     0
   148         -#elif defined(SQLITE_ENABLE_STAT3)
   149         -# define IsStat4     0
   150         -# define IsStat3     1
   151    147   #else
   152    148   # define IsStat4     0
   153         -# define IsStat3     0
   154    149   # undef SQLITE_STAT4_SAMPLES
   155    150   # define SQLITE_STAT4_SAMPLES 1
   156    151   #endif
   157         -#define IsStat34    (IsStat3+IsStat4)  /* 1 for STAT3 or STAT4. 0 otherwise */
   158    152   
   159    153   /*
   160    154   ** This routine generates code that opens the sqlite_statN tables.
   161    155   ** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
   162    156   ** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
   163    157   ** appropriate compile-time options are provided.
   164    158   **
................................................................................
   179    173     static const struct {
   180    174       const char *zName;
   181    175       const char *zCols;
   182    176     } aTable[] = {
   183    177       { "sqlite_stat1", "tbl,idx,stat" },
   184    178   #if defined(SQLITE_ENABLE_STAT4)
   185    179       { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
   186         -    { "sqlite_stat3", 0 },
   187         -#elif defined(SQLITE_ENABLE_STAT3)
   188         -    { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
   189         -    { "sqlite_stat4", 0 },
   190    180   #else
   191         -    { "sqlite_stat3", 0 },
   192    181       { "sqlite_stat4", 0 },
   193    182   #endif
          183  +    { "sqlite_stat3", 0 },
   194    184     };
   195    185     int i;
   196    186     sqlite3 *db = pParse->db;
   197    187     Db *pDb;
   198    188     Vdbe *v = sqlite3GetVdbe(pParse);
   199    189     int aRoot[ArraySize(aTable)];
   200    190     u8 aCreateTbl[ArraySize(aTable)];
................................................................................
   268    258   ** information.
   269    259   */
   270    260   typedef struct Stat4Accum Stat4Accum;
   271    261   typedef struct Stat4Sample Stat4Sample;
   272    262   struct Stat4Sample {
   273    263     tRowcnt *anEq;                  /* sqlite_stat4.nEq */
   274    264     tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
   275         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          265  +#ifdef SQLITE_ENABLE_STAT4
   276    266     tRowcnt *anLt;                  /* sqlite_stat4.nLt */
   277    267     union {
   278    268       i64 iRowid;                     /* Rowid in main table of the key */
   279    269       u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
   280    270     } u;
   281    271     u32 nRowid;                     /* Sizeof aRowid[] */
   282    272     u8 isPSample;                   /* True if a periodic sample */
................................................................................
   299    289     int iGet;                 /* Index of current sample accessed by stat_get() */
   300    290     Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
   301    291     sqlite3 *db;              /* Database connection, for malloc() */
   302    292   };
   303    293   
   304    294   /* Reclaim memory used by a Stat4Sample
   305    295   */
   306         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          296  +#ifdef SQLITE_ENABLE_STAT4
   307    297   static void sampleClear(sqlite3 *db, Stat4Sample *p){
   308    298     assert( db!=0 );
   309    299     if( p->nRowid ){
   310    300       sqlite3DbFree(db, p->u.aRowid);
   311    301       p->nRowid = 0;
   312    302     }
   313    303   }
   314    304   #endif
   315    305   
   316    306   /* Initialize the BLOB value of a ROWID
   317    307   */
   318         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          308  +#ifdef SQLITE_ENABLE_STAT4
   319    309   static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
   320    310     assert( db!=0 );
   321    311     if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
   322    312     p->u.aRowid = sqlite3DbMallocRawNN(db, n);
   323    313     if( p->u.aRowid ){
   324    314       p->nRowid = n;
   325    315       memcpy(p->u.aRowid, pData, n);
................................................................................
   327    317       p->nRowid = 0;
   328    318     }
   329    319   }
   330    320   #endif
   331    321   
   332    322   /* Initialize the INTEGER value of a ROWID.
   333    323   */
   334         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          324  +#ifdef SQLITE_ENABLE_STAT4
   335    325   static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
   336    326     assert( db!=0 );
   337    327     if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
   338    328     p->nRowid = 0;
   339    329     p->u.iRowid = iRowid;
   340    330   }
   341    331   #endif
   342    332   
   343    333   
   344    334   /*
   345    335   ** Copy the contents of object (*pFrom) into (*pTo).
   346    336   */
   347         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          337  +#ifdef SQLITE_ENABLE_STAT4
   348    338   static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
   349    339     pTo->isPSample = pFrom->isPSample;
   350    340     pTo->iCol = pFrom->iCol;
   351    341     pTo->iHash = pFrom->iHash;
   352    342     memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
   353    343     memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
   354    344     memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
................................................................................
   361    351   #endif
   362    352   
   363    353   /*
   364    354   ** Reclaim all memory of a Stat4Accum structure.
   365    355   */
   366    356   static void stat4Destructor(void *pOld){
   367    357     Stat4Accum *p = (Stat4Accum*)pOld;
   368         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          358  +#ifdef SQLITE_ENABLE_STAT4
   369    359     int i;
   370    360     for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
   371    361     for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
   372    362     sampleClear(p->db, &p->current);
   373    363   #endif
   374    364     sqlite3DbFree(p->db, p);
   375    365   }
................................................................................
   381    371   **     K:    The number of columns in the index excluding the rowid/pk.
   382    372   **     C:    The number of rows in the index (note 2)
   383    373   **
   384    374   ** Note 1:  In the special case of the covering index that implements a
   385    375   ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
   386    376   ** total number of columns in the table.
   387    377   **
   388         -** Note 2:  C is only used for STAT3 and STAT4.
          378  +** Note 2:  C is only used for STAT4.
   389    379   **
   390    380   ** For indexes on ordinary rowid tables, N==K+1.  But for indexes on
   391    381   ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
   392    382   ** PRIMARY KEY of the table.  The covering index that implements the
   393    383   ** original WITHOUT ROWID table as N==K as a special case.
   394    384   **
   395    385   ** This routine allocates the Stat4Accum object in heap memory. The return 
................................................................................
   404    394   ){
   405    395     Stat4Accum *p;
   406    396     int nCol;                       /* Number of columns in index being sampled */
   407    397     int nKeyCol;                    /* Number of key columns */
   408    398     int nColUp;                     /* nCol rounded up for alignment */
   409    399     int n;                          /* Bytes of space to allocate */
   410    400     sqlite3 *db;                    /* Database connection */
   411         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          401  +#ifdef SQLITE_ENABLE_STAT4
   412    402     int mxSample = SQLITE_STAT4_SAMPLES;
   413    403   #endif
   414    404   
   415    405     /* Decode the three function arguments */
   416    406     UNUSED_PARAMETER(argc);
   417    407     nCol = sqlite3_value_int(argv[0]);
   418    408     assert( nCol>0 );
................................................................................
   421    411     assert( nKeyCol<=nCol );
   422    412     assert( nKeyCol>0 );
   423    413   
   424    414     /* Allocate the space required for the Stat4Accum object */
   425    415     n = sizeof(*p) 
   426    416       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
   427    417       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
   428         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          418  +#ifdef SQLITE_ENABLE_STAT4
   429    419       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
   430    420       + sizeof(Stat4Sample)*(nCol+mxSample)     /* Stat4Accum.aBest[], a[] */
   431    421       + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
   432    422   #endif
   433    423     ;
   434    424     db = sqlite3_context_db_handle(context);
   435    425     p = sqlite3DbMallocZero(db, n);
................................................................................
   441    431     p->db = db;
   442    432     p->nRow = 0;
   443    433     p->nCol = nCol;
   444    434     p->nKeyCol = nKeyCol;
   445    435     p->current.anDLt = (tRowcnt*)&p[1];
   446    436     p->current.anEq = &p->current.anDLt[nColUp];
   447    437   
   448         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          438  +#ifdef SQLITE_ENABLE_STAT4
   449    439     {
   450    440       u8 *pSpace;                     /* Allocated space not yet assigned */
   451    441       int i;                          /* Used to iterate through p->aSample[] */
   452    442   
   453    443       p->iGet = -1;
   454    444       p->mxSample = mxSample;
   455    445       p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
................................................................................
   476    466     /* Return a pointer to the allocated object to the caller.  Note that
   477    467     ** only the pointer (the 2nd parameter) matters.  The size of the object
   478    468     ** (given by the 3rd parameter) is never used and can be any positive
   479    469     ** value. */
   480    470     sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
   481    471   }
   482    472   static const FuncDef statInitFuncdef = {
   483         -  2+IsStat34,      /* nArg */
          473  +  2+IsStat4,       /* nArg */
   484    474     SQLITE_UTF8,     /* funcFlags */
   485    475     0,               /* pUserData */
   486    476     0,               /* pNext */
   487    477     statInit,        /* xSFunc */
   488    478     0,               /* xFinalize */
   489    479     0, 0,            /* xValue, xInverse */
   490    480     "stat_init",     /* zName */
................................................................................
   516    506       if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
   517    507     }
   518    508     if( pNew->iHash>pOld->iHash ) return 1;
   519    509     return 0;
   520    510   }
   521    511   #endif
   522    512   
   523         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          513  +#ifdef SQLITE_ENABLE_STAT4
   524    514   /*
   525    515   ** Return true if pNew is to be preferred over pOld.
   526    516   **
   527    517   ** This function assumes that for each argument sample, the contents of
   528    518   ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. 
   529    519   */
   530    520   static int sampleIsBetter(
................................................................................
   535    525     tRowcnt nEqNew = pNew->anEq[pNew->iCol];
   536    526     tRowcnt nEqOld = pOld->anEq[pOld->iCol];
   537    527   
   538    528     assert( pOld->isPSample==0 && pNew->isPSample==0 );
   539    529     assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
   540    530   
   541    531     if( (nEqNew>nEqOld) ) return 1;
   542         -#ifdef SQLITE_ENABLE_STAT4
   543    532     if( nEqNew==nEqOld ){
   544    533       if( pNew->iCol<pOld->iCol ) return 1;
   545    534       return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
   546    535     }
   547    536     return 0;
   548         -#else
   549         -  return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
   550         -#endif
   551    537   }
   552    538   
   553    539   /*
   554    540   ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
   555    541   ** remove the least desirable sample from p->a[] to make room.
   556    542   */
   557    543   static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
   558    544     Stat4Sample *pSample = 0;
   559    545     int i;
   560    546   
   561    547     assert( IsStat4 || nEqZero==0 );
   562    548   
   563         -#ifdef SQLITE_ENABLE_STAT4
   564    549     /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
   565    550     ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
   566    551     ** other words, if nMaxEqZero is n, then it is guaranteed that there
   567    552     ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
   568    553     if( nEqZero>p->nMaxEqZero ){
   569    554       p->nMaxEqZero = nEqZero;
   570    555     }
................................................................................
   590    575       }
   591    576       if( pUpgrade ){
   592    577         pUpgrade->iCol = pNew->iCol;
   593    578         pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
   594    579         goto find_new_min;
   595    580       }
   596    581     }
   597         -#endif
   598    582   
   599    583     /* If necessary, remove sample iMin to make room for the new sample. */
   600    584     if( p->nSample>=p->mxSample ){
   601    585       Stat4Sample *pMin = &p->a[p->iMin];
   602    586       tRowcnt *anEq = pMin->anEq;
   603    587       tRowcnt *anLt = pMin->anLt;
   604    588       tRowcnt *anDLt = pMin->anDLt;
................................................................................
   611    595       pSample->anLt = anLt;
   612    596       p->nSample = p->mxSample-1;
   613    597     }
   614    598   
   615    599     /* The "rows less-than" for the rowid column must be greater than that
   616    600     ** for the last sample in the p->a[] array. Otherwise, the samples would
   617    601     ** be out of order. */
   618         -#ifdef SQLITE_ENABLE_STAT4
   619    602     assert( p->nSample==0 
   620    603          || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
   621         -#endif
   622    604   
   623    605     /* Insert the new sample */
   624    606     pSample = &p->a[p->nSample];
   625    607     sampleCopy(p, pSample, pNew);
   626    608     p->nSample++;
   627    609   
   628    610     /* Zero the first nEqZero entries in the anEq[] array. */
   629    611     memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
   630    612   
   631         -#ifdef SQLITE_ENABLE_STAT4
   632         - find_new_min:
   633         -#endif
          613  +find_new_min:
   634    614     if( p->nSample>=p->mxSample ){
   635    615       int iMin = -1;
   636    616       for(i=0; i<p->mxSample; i++){
   637    617         if( p->a[i].isPSample ) continue;
   638    618         if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
   639    619           iMin = i;
   640    620         }
   641    621       }
   642    622       assert( iMin>=0 );
   643    623       p->iMin = iMin;
   644    624     }
   645    625   }
   646         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
          626  +#endif /* SQLITE_ENABLE_STAT4 */
   647    627   
   648    628   /*
   649    629   ** Field iChng of the index being scanned has changed. So at this point
   650    630   ** p->current contains a sample that reflects the previous row of the
   651    631   ** index. The value of anEq[iChng] and subsequent anEq[] elements are
   652    632   ** correct at this point.
   653    633   */
................................................................................
   680    660           if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
   681    661         }
   682    662       }
   683    663       p->nMaxEqZero = iChng;
   684    664     }
   685    665   #endif
   686    666   
   687         -#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
   688         -  if( iChng==0 ){
   689         -    tRowcnt nLt = p->current.anLt[0];
   690         -    tRowcnt nEq = p->current.anEq[0];
   691         -
   692         -    /* Check if this is to be a periodic sample. If so, add it. */
   693         -    if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
   694         -      p->current.isPSample = 1;
   695         -      sampleInsert(p, &p->current, 0);
   696         -      p->current.isPSample = 0;
   697         -    }else 
   698         -
   699         -    /* Or if it is a non-periodic sample. Add it in this case too. */
   700         -    if( p->nSample<p->mxSample 
   701         -     || sampleIsBetter(p, &p->current, &p->a[p->iMin]) 
   702         -    ){
   703         -      sampleInsert(p, &p->current, 0);
   704         -    }
   705         -  }
   706         -#endif
   707         -
   708         -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
          667  +#ifndef SQLITE_ENABLE_STAT4
   709    668     UNUSED_PARAMETER( p );
   710    669     UNUSED_PARAMETER( iChng );
   711    670   #endif
   712    671   }
   713    672   
   714    673   /*
   715    674   ** Implementation of the stat_push SQL function:  stat_push(P,C,R)
................................................................................
   721    680   **          WITHOUT ROWID tables.
   722    681   **
   723    682   ** This SQL function always returns NULL.  It's purpose it to accumulate
   724    683   ** statistical data and/or samples in the Stat4Accum object about the
   725    684   ** index being analyzed.  The stat_get() SQL function will later be used to
   726    685   ** extract relevant information for constructing the sqlite_statN tables.
   727    686   **
   728         -** The R parameter is only used for STAT3 and STAT4
          687  +** The R parameter is only used for STAT4
   729    688   */
   730    689   static void statPush(
   731    690     sqlite3_context *context,
   732    691     int argc,
   733    692     sqlite3_value **argv
   734    693   ){
   735    694     int i;
................................................................................
   753    712       /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
   754    713       ** to the current row of the index. */
   755    714       for(i=0; i<iChng; i++){
   756    715         p->current.anEq[i]++;
   757    716       }
   758    717       for(i=iChng; i<p->nCol; i++){
   759    718         p->current.anDLt[i]++;
   760         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          719  +#ifdef SQLITE_ENABLE_STAT4
   761    720         p->current.anLt[i] += p->current.anEq[i];
   762    721   #endif
   763    722         p->current.anEq[i] = 1;
   764    723       }
   765    724     }
   766    725     p->nRow++;
   767         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          726  +#ifdef SQLITE_ENABLE_STAT4
   768    727     if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
   769    728       sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
   770    729     }else{
   771    730       sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
   772    731                                          sqlite3_value_blob(argv[2]));
   773    732     }
   774    733     p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
................................................................................
   793    752           sampleCopy(p, &p->aBest[i], &p->current);
   794    753         }
   795    754       }
   796    755     }
   797    756   #endif
   798    757   }
   799    758   static const FuncDef statPushFuncdef = {
   800         -  2+IsStat34,      /* nArg */
          759  +  2+IsStat4,       /* nArg */
   801    760     SQLITE_UTF8,     /* funcFlags */
   802    761     0,               /* pUserData */
   803    762     0,               /* pNext */
   804    763     statPush,        /* xSFunc */
   805    764     0,               /* xFinalize */
   806    765     0, 0,            /* xValue, xInverse */
   807    766     "stat_push",     /* zName */
................................................................................
   824    783   **
   825    784   ** The stat_get(P,J) function is not available to generic SQL.  It is
   826    785   ** inserted as part of a manually constructed bytecode program.  (See
   827    786   ** the callStatGet() routine below.)  It is guaranteed that the P
   828    787   ** parameter will always be a poiner to a Stat4Accum object, never a
   829    788   ** NULL.
   830    789   **
   831         -** If neither STAT3 nor STAT4 are enabled, then J is always
          790  +** If STAT4 is not enabled, then J is always
   832    791   ** STAT_GET_STAT1 and is hence omitted and this routine becomes
   833    792   ** a one-parameter function, stat_get(P), that always returns the
   834    793   ** stat1 table entry information.
   835    794   */
   836    795   static void statGet(
   837    796     sqlite3_context *context,
   838    797     int argc,
   839    798     sqlite3_value **argv
   840    799   ){
   841    800     Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
   842         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   843         -  /* STAT3 and STAT4 have a parameter on this routine. */
          801  +#ifdef SQLITE_ENABLE_STAT4
          802  +  /* STAT4 has a parameter on this routine. */
   844    803     int eCall = sqlite3_value_int(argv[1]);
   845    804     assert( argc==2 );
   846    805     assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ 
   847    806          || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
   848    807          || eCall==STAT_GET_NDLT 
   849    808     );
   850    809     if( eCall==STAT_GET_STAT1 )
................................................................................
   891    850         z += sqlite3Strlen30(z);
   892    851         assert( p->current.anEq[i] );
   893    852       }
   894    853       assert( z[0]=='\0' && z>zRet );
   895    854   
   896    855       sqlite3_result_text(context, zRet, -1, sqlite3_free);
   897    856     }
   898         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          857  +#ifdef SQLITE_ENABLE_STAT4
   899    858     else if( eCall==STAT_GET_ROWID ){
   900    859       if( p->iGet<0 ){
   901    860         samplePushPrevious(p, 0);
   902    861         p->iGet = 0;
   903    862       }
   904    863       if( p->iGet<p->nSample ){
   905    864         Stat4Sample *pS = p->a + p->iGet;
................................................................................
   920    879         default: {
   921    880           aCnt = p->a[p->iGet].anDLt; 
   922    881           p->iGet++;
   923    882           break;
   924    883         }
   925    884       }
   926    885   
   927         -    if( IsStat3 ){
   928         -      sqlite3_result_int64(context, (i64)aCnt[0]);
   929         -    }else{
          886  +    {
   930    887         char *zRet = sqlite3MallocZero(p->nCol * 25);
   931    888         if( zRet==0 ){
   932    889           sqlite3_result_error_nomem(context);
   933    890         }else{
   934    891           int i;
   935    892           char *z = zRet;
   936    893           for(i=0; i<p->nCol; i++){
................................................................................
   939    896           }
   940    897           assert( z[0]=='\0' && z>zRet );
   941    898           z[-1] = '\0';
   942    899           sqlite3_result_text(context, zRet, -1, sqlite3_free);
   943    900         }
   944    901       }
   945    902     }
   946         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
          903  +#endif /* SQLITE_ENABLE_STAT4 */
   947    904   #ifndef SQLITE_DEBUG
   948    905     UNUSED_PARAMETER( argc );
   949    906   #endif
   950    907   }
   951    908   static const FuncDef statGetFuncdef = {
   952         -  1+IsStat34,      /* nArg */
          909  +  1+IsStat4,       /* nArg */
   953    910     SQLITE_UTF8,     /* funcFlags */
   954    911     0,               /* pUserData */
   955    912     0,               /* pNext */
   956    913     statGet,         /* xSFunc */
   957    914     0,               /* xFinalize */
   958    915     0, 0,            /* xValue, xInverse */
   959    916     "stat_get",      /* zName */
   960    917     {0}
   961    918   };
   962    919   
   963    920   static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
   964    921     assert( regOut!=regStat4 && regOut!=regStat4+1 );
   965         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          922  +#ifdef SQLITE_ENABLE_STAT4
   966    923     sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
   967    924   #elif SQLITE_DEBUG
   968    925     assert( iParam==STAT_GET_STAT1 );
   969    926   #else
   970    927     UNUSED_PARAMETER( iParam );
   971    928   #endif
   972    929     sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut,
   973    930                       (char*)&statGetFuncdef, P4_FUNCDEF);
   974         -  sqlite3VdbeChangeP5(v, 1 + IsStat34);
          931  +  sqlite3VdbeChangeP5(v, 1 + IsStat4);
   975    932   }
   976    933   
   977    934   /*
   978    935   ** Generate code to do an analysis of all indices associated with
   979    936   ** a single table.
   980    937   */
   981    938   static void analyzeOneTable(
................................................................................
   994    951     int i;                       /* Loop counter */
   995    952     int jZeroRows = -1;          /* Jump from here if number of rows is zero */
   996    953     int iDb;                     /* Index of database containing pTab */
   997    954     u8 needTableCnt = 1;         /* True to count the table */
   998    955     int regNewRowid = iMem++;    /* Rowid for the inserted record */
   999    956     int regStat4 = iMem++;       /* Register to hold Stat4Accum object */
  1000    957     int regChng = iMem++;        /* Index of changed index field */
  1001         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          958  +#ifdef SQLITE_ENABLE_STAT4
  1002    959     int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
  1003    960   #endif
  1004    961     int regTemp = iMem++;        /* Temporary use register */
  1005    962     int regTabname = iMem++;     /* Register containing table name */
  1006    963     int regIdxname = iMem++;     /* Register containing index name */
  1007    964     int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
  1008    965     int regPrev = iMem;          /* MUST BE LAST (see below) */
................................................................................
  1128   1085       ** 
  1129   1086       **    (1) the number of columns in the index including the rowid
  1130   1087       **        (or for a WITHOUT ROWID table, the number of PK columns),
  1131   1088       **    (2) the number of columns in the key without the rowid/pk
  1132   1089       **    (3) the number of rows in the index,
  1133   1090       **
  1134   1091       **
  1135         -    ** The third argument is only used for STAT3 and STAT4
         1092  +    ** The third argument is only used for STAT4
  1136   1093       */
  1137         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1094  +#ifdef SQLITE_ENABLE_STAT4
  1138   1095       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
  1139   1096   #endif
  1140   1097       sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
  1141   1098       sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
  1142   1099       sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4,
  1143   1100                        (char*)&statInitFuncdef, P4_FUNCDEF);
  1144         -    sqlite3VdbeChangeP5(v, 2+IsStat34);
         1101  +    sqlite3VdbeChangeP5(v, 2+IsStat4);
  1145   1102   
  1146   1103       /* Implementation of the following:
  1147   1104       **
  1148   1105       **   Rewind csr
  1149   1106       **   if eof(csr) goto end_of_scan;
  1150   1107       **   regChng = 0
  1151   1108       **   goto next_push_0;
................................................................................
  1208   1165         }
  1209   1166         sqlite3VdbeResolveLabel(v, endDistinctTest);
  1210   1167         sqlite3DbFree(db, aGotoChng);
  1211   1168       }
  1212   1169     
  1213   1170       /*
  1214   1171       **  chng_addr_N:
  1215         -    **   regRowid = idx(rowid)            // STAT34 only
  1216         -    **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
         1172  +    **   regRowid = idx(rowid)            // STAT4 only
         1173  +    **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT4 only
  1217   1174       **   Next csr
  1218   1175       **   if !eof(csr) goto next_row;
  1219   1176       */
  1220         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1177  +#ifdef SQLITE_ENABLE_STAT4
  1221   1178       assert( regRowid==(regStat4+2) );
  1222   1179       if( HasRowid(pTab) ){
  1223   1180         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  1224   1181       }else{
  1225   1182         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  1226   1183         int j, k, regKey;
  1227   1184         regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
................................................................................
  1234   1191         sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
  1235   1192         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
  1236   1193       }
  1237   1194   #endif
  1238   1195       assert( regChng==(regStat4+1) );
  1239   1196       sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp,
  1240   1197                        (char*)&statPushFuncdef, P4_FUNCDEF);
  1241         -    sqlite3VdbeChangeP5(v, 2+IsStat34);
         1198  +    sqlite3VdbeChangeP5(v, 2+IsStat4);
  1242   1199       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
  1243   1200   
  1244   1201       /* Add the entry to the stat1 table. */
  1245   1202       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
  1246   1203       assert( "BBB"[0]==SQLITE_AFF_TEXT );
  1247   1204       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
  1248   1205       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1249   1206       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1250   1207   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1251   1208       sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
  1252   1209   #endif
  1253   1210       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1254   1211   
  1255         -    /* Add the entries to the stat3 or stat4 table. */
  1256         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1212  +    /* Add the entries to the stat4 table. */
         1213  +#ifdef SQLITE_ENABLE_STAT4
  1257   1214       {
  1258   1215         int regEq = regStat1;
  1259   1216         int regLt = regStat1+1;
  1260   1217         int regDLt = regStat1+2;
  1261   1218         int regSample = regStat1+3;
  1262   1219         int regCol = regStat1+4;
  1263   1220         int regSampleRowid = regCol + nCol;
................................................................................
  1272   1229         addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
  1273   1230         VdbeCoverage(v);
  1274   1231         callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
  1275   1232         callStatGet(v, regStat4, STAT_GET_NLT, regLt);
  1276   1233         callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
  1277   1234         sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
  1278   1235         VdbeCoverage(v);
  1279         -#ifdef SQLITE_ENABLE_STAT3
  1280         -      sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
  1281         -#else
  1282   1236         for(i=0; i<nCol; i++){
  1283   1237           sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
  1284   1238         }
  1285   1239         sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
  1286         -#endif
  1287   1240         sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
  1288   1241         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
  1289   1242         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
  1290   1243         sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
  1291   1244         sqlite3VdbeJumpHere(v, addrIsNull);
  1292   1245       }
  1293         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1246  +#endif /* SQLITE_ENABLE_STAT4 */
  1294   1247   
  1295   1248       /* End of analysis */
  1296   1249       sqlite3VdbeJumpHere(v, addrRewind);
  1297   1250     }
  1298   1251   
  1299   1252   
  1300   1253     /* Create a single sqlite_stat1 entry containing NULL as the index
................................................................................
  1461   1414     Index *pIndex          /* Handle extra flags for this index, if not NULL */
  1462   1415   ){
  1463   1416     char *z = zIntArray;
  1464   1417     int c;
  1465   1418     int i;
  1466   1419     tRowcnt v;
  1467   1420   
  1468         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1421  +#ifdef SQLITE_ENABLE_STAT4
  1469   1422     if( z==0 ) z = "";
  1470   1423   #else
  1471   1424     assert( z!=0 );
  1472   1425   #endif
  1473   1426     for(i=0; *z && i<nOut; i++){
  1474   1427       v = 0;
  1475   1428       while( (c=z[0])>='0' && c<='9' ){
  1476   1429         v = v*10 + c - '0';
  1477   1430         z++;
  1478   1431       }
  1479         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1432  +#ifdef SQLITE_ENABLE_STAT4
  1480   1433       if( aOut ) aOut[i] = v;
  1481   1434       if( aLog ) aLog[i] = sqlite3LogEst(v);
  1482   1435   #else
  1483   1436       assert( aOut==0 );
  1484   1437       UNUSED_PARAMETER(aOut);
  1485   1438       assert( aLog!=0 );
  1486   1439       aLog[i] = sqlite3LogEst(v);
  1487   1440   #endif
  1488   1441       if( *z==' ' ) z++;
  1489   1442     }
  1490         -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
         1443  +#ifndef SQLITE_ENABLE_STAT4
  1491   1444     assert( pIndex!=0 ); {
  1492   1445   #else
  1493   1446     if( pIndex ){
  1494   1447   #endif
  1495   1448       pIndex->bUnordered = 0;
  1496   1449       pIndex->noSkipScan = 0;
  1497   1450       while( z[0] ){
................................................................................
  1548   1501       pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
  1549   1502     }
  1550   1503     z = argv[2];
  1551   1504   
  1552   1505     if( pIndex ){
  1553   1506       tRowcnt *aiRowEst = 0;
  1554   1507       int nCol = pIndex->nKeyCol+1;
  1555         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1508  +#ifdef SQLITE_ENABLE_STAT4
  1556   1509       /* Index.aiRowEst may already be set here if there are duplicate 
  1557   1510       ** sqlite_stat1 entries for this index. In that case just clobber
  1558   1511       ** the old data with the new instead of allocating a new array.  */
  1559   1512       if( pIndex->aiRowEst==0 ){
  1560   1513         pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
  1561   1514         if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
  1562   1515       }
................................................................................
  1584   1537   }
  1585   1538   
  1586   1539   /*
  1587   1540   ** If the Index.aSample variable is not NULL, delete the aSample[] array
  1588   1541   ** and its contents.
  1589   1542   */
  1590   1543   void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
  1591         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1544  +#ifdef SQLITE_ENABLE_STAT4
  1592   1545     if( pIdx->aSample ){
  1593   1546       int j;
  1594   1547       for(j=0; j<pIdx->nSample; j++){
  1595   1548         IndexSample *p = &pIdx->aSample[j];
  1596   1549         sqlite3DbFree(db, p->p);
  1597   1550       }
  1598   1551       sqlite3DbFree(db, pIdx->aSample);
................................................................................
  1600   1553     if( db && db->pnBytesFreed==0 ){
  1601   1554       pIdx->nSample = 0;
  1602   1555       pIdx->aSample = 0;
  1603   1556     }
  1604   1557   #else
  1605   1558     UNUSED_PARAMETER(db);
  1606   1559     UNUSED_PARAMETER(pIdx);
  1607         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1560  +#endif /* SQLITE_ENABLE_STAT4 */
  1608   1561   }
  1609   1562   
  1610         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1563  +#ifdef SQLITE_ENABLE_STAT4
  1611   1564   /*
  1612   1565   ** Populate the pIdx->aAvgEq[] array based on the samples currently
  1613   1566   ** stored in pIdx->aSample[]. 
  1614   1567   */
  1615   1568   static void initAvgEq(Index *pIdx){
  1616   1569     if( pIdx ){
  1617   1570       IndexSample *aSample = pIdx->aSample;
................................................................................
  1681   1634       Table *pTab = sqlite3FindTable(db, zName, zDb);
  1682   1635       if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
  1683   1636     }
  1684   1637     return pIdx;
  1685   1638   }
  1686   1639   
  1687   1640   /*
  1688         -** Load the content from either the sqlite_stat4 or sqlite_stat3 table 
         1641  +** Load the content from either the sqlite_stat4
  1689   1642   ** into the relevant Index.aSample[] arrays.
  1690   1643   **
  1691   1644   ** Arguments zSql1 and zSql2 must point to SQL statements that return
  1692         -** data equivalent to the following (statements are different for stat3,
  1693         -** see the caller of this function for details):
         1645  +** data equivalent to the following:
  1694   1646   **
  1695   1647   **    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
  1696   1648   **    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
  1697   1649   **
  1698   1650   ** where %Q is replaced with the database name before the SQL is executed.
  1699   1651   */
  1700   1652   static int loadStatTbl(
  1701   1653     sqlite3 *db,                  /* Database handle */
  1702         -  int bStat3,                   /* Assume single column records only */
  1703   1654     const char *zSql1,            /* SQL statement 1 (see above) */
  1704   1655     const char *zSql2,            /* SQL statement 2 (see above) */
  1705   1656     const char *zDb               /* Database name (e.g. "main") */
  1706   1657   ){
  1707   1658     int rc;                       /* Result codes from subroutines */
  1708   1659     sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
  1709   1660     char *zSql;                   /* Text of the SQL statement */
................................................................................
  1729   1680       int i;          /* Bytes of space required */
  1730   1681       tRowcnt *pSpace;
  1731   1682   
  1732   1683       zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1733   1684       if( zIndex==0 ) continue;
  1734   1685       nSample = sqlite3_column_int(pStmt, 1);
  1735   1686       pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
  1736         -    assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
  1737         -    /* Index.nSample is non-zero at this point if data has already been
  1738         -    ** loaded from the stat4 table. In this case ignore stat3 data.  */
  1739         -    if( pIdx==0 || pIdx->nSample ) continue;
  1740         -    if( bStat3==0 ){
  1741         -      assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
  1742         -      if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
  1743         -        nIdxCol = pIdx->nKeyCol;
  1744         -      }else{
  1745         -        nIdxCol = pIdx->nColumn;
  1746         -      }
         1687  +    assert( pIdx==0 || pIdx->nSample==0 );
         1688  +    if( pIdx==0 ) continue;
         1689  +    assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
         1690  +    if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
         1691  +      nIdxCol = pIdx->nKeyCol;
         1692  +    }else{
         1693  +      nIdxCol = pIdx->nColumn;
  1747   1694       }
  1748   1695       pIdx->nSampleCol = nIdxCol;
  1749   1696       nByte = sizeof(IndexSample) * nSample;
  1750   1697       nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
  1751   1698       nByte += nIdxCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
  1752   1699   
  1753   1700       pIdx->aSample = sqlite3DbMallocZero(db, nByte);
................................................................................
  1781   1728       int nCol = 1;                 /* Number of columns in index */
  1782   1729   
  1783   1730       zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1784   1731       if( zIndex==0 ) continue;
  1785   1732       pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
  1786   1733       if( pIdx==0 ) continue;
  1787   1734       /* This next condition is true if data has already been loaded from 
  1788         -    ** the sqlite_stat4 table. In this case ignore stat3 data.  */
         1735  +    ** the sqlite_stat4 table. */
  1789   1736       nCol = pIdx->nSampleCol;
  1790         -    if( bStat3 && nCol>1 ) continue;
  1791   1737       if( pIdx!=pPrevIdx ){
  1792   1738         initAvgEq(pPrevIdx);
  1793   1739         pPrevIdx = pIdx;
  1794   1740       }
  1795   1741       pSample = &pIdx->aSample[pIdx->nSample];
  1796   1742       decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
  1797   1743       decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
................................................................................
  1816   1762     }
  1817   1763     rc = sqlite3_finalize(pStmt);
  1818   1764     if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
  1819   1765     return rc;
  1820   1766   }
  1821   1767   
  1822   1768   /*
  1823         -** Load content from the sqlite_stat4 and sqlite_stat3 tables into 
         1769  +** Load content from the sqlite_stat4 table into 
  1824   1770   ** the Index.aSample[] arrays of all indices.
  1825   1771   */
  1826   1772   static int loadStat4(sqlite3 *db, const char *zDb){
  1827   1773     int rc = SQLITE_OK;             /* Result codes from subroutines */
  1828   1774   
  1829   1775     assert( db->lookaside.bDisable );
  1830   1776     if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
  1831         -    rc = loadStatTbl(db, 0,
         1777  +    rc = loadStatTbl(db,
  1832   1778         "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", 
  1833   1779         "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
  1834   1780         zDb
  1835   1781       );
  1836   1782     }
  1837         -
  1838         -  if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
  1839         -    rc = loadStatTbl(db, 1,
  1840         -      "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", 
  1841         -      "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
  1842         -      zDb
  1843         -    );
  1844         -  }
  1845         -
  1846   1783     return rc;
  1847   1784   }
  1848         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1785  +#endif /* SQLITE_ENABLE_STAT4 */
  1849   1786   
  1850   1787   /*
  1851         -** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
         1788  +** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
  1852   1789   ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
  1853         -** arrays. The contents of sqlite_stat3/4 are used to populate the
         1790  +** arrays. The contents of sqlite_stat4 are used to populate the
  1854   1791   ** Index.aSample[] arrays.
  1855   1792   **
  1856   1793   ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
  1857         -** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined 
  1858         -** during compilation and the sqlite_stat3/4 table is present, no data is 
         1794  +** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined 
         1795  +** during compilation and the sqlite_stat4 table is present, no data is 
  1859   1796   ** read from it.
  1860   1797   **
  1861         -** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the 
         1798  +** If SQLITE_ENABLE_STAT4 was defined during compilation and the 
  1862   1799   ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
  1863   1800   ** returned. However, in this case, data is read from the sqlite_stat1
  1864   1801   ** table (if it is present) before returning.
  1865   1802   **
  1866   1803   ** If an OOM error occurs, this function always sets db->mallocFailed.
  1867   1804   ** This means if the caller does not care about other errors, the return
  1868   1805   ** code may be ignored.
................................................................................
  1882   1819     for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
  1883   1820       Table *pTab = sqliteHashData(i);
  1884   1821       pTab->tabFlags &= ~TF_HasStat1;
  1885   1822     }
  1886   1823     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1887   1824       Index *pIdx = sqliteHashData(i);
  1888   1825       pIdx->hasStat1 = 0;
  1889         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1826  +#ifdef SQLITE_ENABLE_STAT4
  1890   1827       sqlite3DeleteIndexSamples(db, pIdx);
  1891   1828       pIdx->aSample = 0;
  1892   1829   #endif
  1893   1830     }
  1894   1831   
  1895   1832     /* Load new statistics out of the sqlite_stat1 table */
  1896   1833     sInfo.db = db;
................................................................................
  1910   1847     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1911   1848     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1912   1849       Index *pIdx = sqliteHashData(i);
  1913   1850       if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
  1914   1851     }
  1915   1852   
  1916   1853     /* Load the statistics from the sqlite_stat4 table. */
  1917         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1854  +#ifdef SQLITE_ENABLE_STAT4
  1918   1855     if( rc==SQLITE_OK ){
  1919   1856       db->lookaside.bDisable++;
  1920   1857       rc = loadStat4(db, sInfo.zDatabase);
  1921   1858       db->lookaside.bDisable--;
  1922   1859     }
  1923   1860     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1924   1861       Index *pIdx = sqliteHashData(i);

Changes to src/auth.c.

    74     74   ){
    75     75   #ifdef SQLITE_ENABLE_API_ARMOR
    76     76     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
    77     77   #endif
    78     78     sqlite3_mutex_enter(db->mutex);
    79     79     db->xAuth = (sqlite3_xauth)xAuth;
    80     80     db->pAuthArg = pArg;
    81         -  sqlite3ExpirePreparedStatements(db, 0);
           81  +  if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
    82     82     sqlite3_mutex_leave(db->mutex);
    83     83     return SQLITE_OK;
    84     84   }
    85     85   
    86     86   /*
    87     87   ** Write an error message into pParse->zErrMsg that explains that the
    88     88   ** user-supplied authorization function returned an illegal value.

Changes to src/btree.c.

  1643   1643     */
  1644   1644     testcase( gap+2==top );
  1645   1645     testcase( gap+1==top );
  1646   1646     testcase( gap==top );
  1647   1647     if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
  1648   1648       u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
  1649   1649       if( pSpace ){
  1650         -      assert( pSpace>=data && (pSpace - data)<65536 );
  1651         -      *pIdx = (int)(pSpace - data);
  1652         -      return SQLITE_OK;
         1650  +      assert( pSpace+nByte<=data+pPage->pBt->usableSize );
         1651  +      if( (*pIdx = (int)(pSpace-data))<=gap ){
         1652  +        return SQLITE_CORRUPT_PAGE(pPage);
         1653  +      }else{
         1654  +        return SQLITE_OK;
         1655  +      }
  1653   1656       }else if( rc ){
  1654   1657         return rc;
  1655   1658       }
  1656   1659     }
  1657   1660   
  1658   1661     /* The request could not be fulfilled using a freelist slot.  Check
  1659   1662     ** to see if defragmentation is necessary.
................................................................................
  6892   6895     u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
  6893   6896     u8 *pData;
  6894   6897     int k;                          /* Current slot in pCArray->apEnd[] */
  6895   6898     u8 *pSrcEnd;                    /* Current pCArray->apEnd[k] value */
  6896   6899   
  6897   6900     assert( i<iEnd );
  6898   6901     j = get2byte(&aData[hdr+5]);
  6899         -  if( NEVER(j>(u32)usableSize) ){ j = 0; }
         6902  +  if( j>(u32)usableSize ){ j = 0; }
  6900   6903     memcpy(&pTmp[j], &aData[j], usableSize - j);
  6901   6904   
  6902   6905     for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
  6903   6906     pSrcEnd = pCArray->apEnd[k];
  6904   6907   
  6905   6908     pData = pEnd;
  6906   6909     while( 1/*exit by break*/ ){
................................................................................
  8709   8712         goto end_insert;
  8710   8713       }
  8711   8714       oldCell = findCell(pPage, idx);
  8712   8715       if( !pPage->leaf ){
  8713   8716         memcpy(newCell, oldCell, 4);
  8714   8717       }
  8715   8718       rc = clearCell(pPage, oldCell, &info);
         8719  +    testcase( pCur->curFlags & BTCF_ValidOvfl );
         8720  +    invalidateOverflowCache(pCur);
  8716   8721       if( info.nSize==szNew && info.nLocal==info.nPayload 
  8717   8722        && (!ISAUTOVACUUM || szNew<pPage->minLocal)
  8718   8723       ){
  8719   8724         /* Overwrite the old cell with the new if they are the same size.
  8720   8725         ** We could also try to do this if the old cell is smaller, then add
  8721   8726         ** the leftover space to the free list.  But experiments show that
  8722   8727         ** doing that is no faster then skipping this optimization and just

Changes to src/build.c.

   507    507   #ifndef SQLITE_OMIT_ANALYZE
   508    508     sqlite3DeleteIndexSamples(db, p);
   509    509   #endif
   510    510     sqlite3ExprDelete(db, p->pPartIdxWhere);
   511    511     sqlite3ExprListDelete(db, p->aColExpr);
   512    512     sqlite3DbFree(db, p->zColAff);
   513    513     if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
   514         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          514  +#ifdef SQLITE_ENABLE_STAT4
   515    515     sqlite3_free(p->aiRowEst);
   516    516   #endif
   517    517     sqlite3DbFree(db, p);
   518    518   }
   519    519   
   520    520   /*
   521    521   ** For the index called zIdxName which is found in the database iDb,
................................................................................
   880    880   
   881    881   /*
   882    882   ** This routine is used to check if the UTF-8 string zName is a legal
   883    883   ** unqualified name for a new schema object (table, index, view or
   884    884   ** trigger). All names are legal except those that begin with the string
   885    885   ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
   886    886   ** is reserved for internal use.
          887  +**
          888  +** When parsing the sqlite_master table, this routine also checks to
          889  +** make sure the "type", "name", and "tbl_name" columns are consistent
          890  +** with the SQL.
   887    891   */
   888         -int sqlite3CheckObjectName(Parse *pParse, const char *zName){
   889         -  if( !pParse->db->init.busy && pParse->nested==0 
   890         -          && sqlite3WritableSchema(pParse->db)==0
   891         -          && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
   892         -    sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
   893         -    return SQLITE_ERROR;
          892  +int sqlite3CheckObjectName(
          893  +  Parse *pParse,            /* Parsing context */
          894  +  const char *zName,        /* Name of the object to check */
          895  +  const char *zType,        /* Type of this object */
          896  +  const char *zTblName      /* Parent table name for triggers and indexes */
          897  +){
          898  +  sqlite3 *db = pParse->db;
          899  +  if( sqlite3WritableSchema(db) || db->init.imposterTable ){
          900  +    /* Skip these error checks for writable_schema=ON */
          901  +    return SQLITE_OK;
          902  +  }
          903  +  if( db->init.busy ){
          904  +    if( sqlite3_stricmp(zType, db->init.azInit[0])
          905  +     || sqlite3_stricmp(zName, db->init.azInit[1])
          906  +     || sqlite3_stricmp(zTblName, db->init.azInit[2])
          907  +    ){
          908  +      if( sqlite3Config.bExtraSchemaChecks ){
          909  +        sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
          910  +        return SQLITE_ERROR;
          911  +      }
          912  +    }
          913  +  }else{
          914  +    if( pParse->nested==0 
          915  +     && 0==sqlite3StrNICmp(zName, "sqlite_", 7)
          916  +    ){
          917  +      sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
          918  +                      zName);
          919  +      return SQLITE_ERROR;
          920  +    }
   894    921     }
   895    922     return SQLITE_OK;
   896    923   }
   897    924   
   898    925   /*
   899    926   ** Return the PRIMARY KEY index of a table
   900    927   */
................................................................................
   967    994       zName = sqlite3NameFromToken(db, pName);
   968    995       if( IN_RENAME_OBJECT ){
   969    996         sqlite3RenameTokenMap(pParse, (void*)zName, pName);
   970    997       }
   971    998     }
   972    999     pParse->sNameToken = *pName;
   973   1000     if( zName==0 ) return;
   974         -  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
         1001  +  if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
   975   1002       goto begin_table_error;
   976   1003     }
   977   1004     if( db->init.iDb==1 ) isTemp = 1;
   978   1005   #ifndef SQLITE_OMIT_AUTHORIZATION
   979   1006     assert( isTemp==0 || isTemp==1 );
   980   1007     assert( isView==0 || isView==1 );
   981   1008     {
................................................................................
  2211   2238         sqlite3MayAbort(pParse);
  2212   2239         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
  2213   2240         sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
  2214   2241         pParse->nTab = 2;
  2215   2242         addrTop = sqlite3VdbeCurrentAddr(v) + 1;
  2216   2243         sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
  2217   2244         if( pParse->nErr ) return;
  2218         -      pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
         2245  +      pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
  2219   2246         if( pSelTab==0 ) return;
  2220   2247         assert( p->aCol==0 );
  2221   2248         p->nCol = pSelTab->nCol;
  2222   2249         p->aCol = pSelTab->aCol;
  2223   2250         pSelTab->nCol = 0;
  2224   2251         pSelTab->aCol = 0;
  2225   2252         sqlite3DeleteTable(db, pSelTab);
................................................................................
  2475   2502       n = pParse->nTab;
  2476   2503       sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
  2477   2504       pTable->nCol = -1;
  2478   2505       db->lookaside.bDisable++;
  2479   2506   #ifndef SQLITE_OMIT_AUTHORIZATION
  2480   2507       xAuth = db->xAuth;
  2481   2508       db->xAuth = 0;
  2482         -    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
         2509  +    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
  2483   2510       db->xAuth = xAuth;
  2484   2511   #else
  2485         -    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
         2512  +    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
  2486   2513   #endif
  2487   2514       pParse->nTab = n;
  2488   2515       if( pTable->pCheck ){
  2489   2516         /* CREATE VIEW name(arglist) AS ...
  2490   2517         ** The names of the columns in the table are taken from
  2491   2518         ** arglist which is stored in pTable->pCheck.  The pCheck field
  2492   2519         ** normally holds CHECK constraints on an ordinary table, but for
................................................................................
  2494   2521         */
  2495   2522         sqlite3ColumnsFromExprList(pParse, pTable->pCheck, 
  2496   2523                                    &pTable->nCol, &pTable->aCol);
  2497   2524         if( db->mallocFailed==0 
  2498   2525          && pParse->nErr==0
  2499   2526          && pTable->nCol==pSel->pEList->nExpr
  2500   2527         ){
  2501         -        sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
         2528  +        sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel,
         2529  +                                               SQLITE_AFF_NONE);
  2502   2530         }
  2503   2531       }else if( pSelTab ){
  2504   2532         /* CREATE VIEW name AS...  without an argument list.  Construct
  2505   2533         ** the column names from the SELECT statement that defines the view.
  2506   2534         */
  2507   2535         assert( pTable->aCol==0 );
  2508   2536         pTable->nCol = pSelTab->nCol;
................................................................................
  3326   3354     ** dealing with a primary key or UNIQUE constraint.  We have to invent our
  3327   3355     ** own name.
  3328   3356     */
  3329   3357     if( pName ){
  3330   3358       zName = sqlite3NameFromToken(db, pName);
  3331   3359       if( zName==0 ) goto exit_create_index;
  3332   3360       assert( pName->z!=0 );
  3333         -    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
         3361  +    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
  3334   3362         goto exit_create_index;
  3335   3363       }
  3336   3364       if( !IN_RENAME_OBJECT ){
  3337   3365         if( !db->init.busy ){
  3338   3366           if( sqlite3FindTable(db, zName, 0)!=0 ){
  3339   3367             sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
  3340   3368             goto exit_create_index;

Changes to src/ctime.c.

   305    305     "ENABLE_SORTER_REFERENCES",
   306    306   #endif
   307    307   #if SQLITE_ENABLE_SQLLOG
   308    308     "ENABLE_SQLLOG",
   309    309   #endif
   310    310   #if defined(SQLITE_ENABLE_STAT4)
   311    311     "ENABLE_STAT4",
   312         -#elif defined(SQLITE_ENABLE_STAT3)
   313         -  "ENABLE_STAT3",
   314    312   #endif
   315    313   #if SQLITE_ENABLE_STMTVTAB
   316    314     "ENABLE_STMTVTAB",
   317    315   #endif
   318    316   #if SQLITE_ENABLE_STMT_SCANSTATUS
   319    317     "ENABLE_STMT_SCANSTATUS",
   320    318   #endif

Changes to src/expr.c.

    67     67     }
    68     68     if( op==TK_SELECT_COLUMN ){
    69     69       assert( pExpr->pLeft->flags&EP_xIsSelect );
    70     70       return sqlite3ExprAffinity(
    71     71           pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
    72     72       );
    73     73     }
    74         -  return pExpr->affinity;
           74  +  return pExpr->affExpr;
    75     75   }
    76     76   
    77     77   /*
    78     78   ** Set the collating sequence for expression pExpr to be the collating
    79     79   ** sequence named by pToken.   Return a pointer to a new Expr node that
    80     80   ** implements the COLLATE operator.
    81     81   **
................................................................................
   226    226   /*
   227    227   ** pExpr is an operand of a comparison operator.  aff2 is the
   228    228   ** type affinity of the other operand.  This routine returns the
   229    229   ** type affinity that should be used for the comparison operator.
   230    230   */
   231    231   char sqlite3CompareAffinity(Expr *pExpr, char aff2){
   232    232     char aff1 = sqlite3ExprAffinity(pExpr);
   233         -  if( aff1 && aff2 ){
          233  +  if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
   234    234       /* Both sides of the comparison are columns. If one has numeric
   235    235       ** affinity, use that. Otherwise use no affinity.
   236    236       */
   237    237       if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
   238    238         return SQLITE_AFF_NUMERIC;
   239    239       }else{
   240    240         return SQLITE_AFF_BLOB;
   241    241       }
   242         -  }else if( !aff1 && !aff2 ){
   243         -    /* Neither side of the comparison is a column.  Compare the
   244         -    ** results directly.
   245         -    */
   246         -    return SQLITE_AFF_BLOB;
   247    242     }else{
   248    243       /* One side is a column, the other is not. Use the columns affinity. */
   249         -    assert( aff1==0 || aff2==0 );
   250         -    return (aff1 + aff2);
          244  +    assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE );
          245  +    return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE;
   251    246     }
   252    247   }
   253    248   
   254    249   /*
   255    250   ** pExpr is a comparison operator.  Return the type affinity that should
   256    251   ** be applied to both operands prior to doing the comparison.
   257    252   */
................................................................................
   276    271   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
   277    272   ** idx_affinity is the affinity of an indexed column. Return true
   278    273   ** if the index with affinity idx_affinity may be used to implement
   279    274   ** the comparison in pExpr.
   280    275   */
   281    276   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
   282    277     char aff = comparisonAffinity(pExpr);
   283         -  switch( aff ){
   284         -    case SQLITE_AFF_BLOB:
   285         -      return 1;
   286         -    case SQLITE_AFF_TEXT:
   287         -      return idx_affinity==SQLITE_AFF_TEXT;
   288         -    default:
   289         -      return sqlite3IsNumericAffinity(idx_affinity);
          278  +  if( aff<SQLITE_AFF_TEXT ){
          279  +    return 1;
   290    280     }
          281  +  if( aff==SQLITE_AFF_TEXT ){
          282  +    return idx_affinity==SQLITE_AFF_TEXT;
          283  +  }
          284  +  return sqlite3IsNumericAffinity(idx_affinity);
   291    285   }
   292    286   
   293    287   /*
   294    288   ** Return the P5 value that should be used for a binary comparison
   295    289   ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
   296    290   */
   297    291   static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
................................................................................
  2796   2790       */
  2797   2791       char affinity;            /* Affinity of the LHS of the IN */
  2798   2792       int i;
  2799   2793       ExprList *pList = pExpr->x.pList;
  2800   2794       struct ExprList_item *pItem;
  2801   2795       int r1, r2, r3;
  2802   2796       affinity = sqlite3ExprAffinity(pLeft);
  2803         -    if( !affinity ){
         2797  +    if( affinity<=SQLITE_AFF_NONE ){
  2804   2798         affinity = SQLITE_AFF_BLOB;
  2805   2799       }
  2806   2800       if( pKeyInfo ){
  2807   2801         assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  2808   2802         pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  2809   2803       }
  2810   2804   
................................................................................
  3486   3480           ** constraints, and that constant is coded by the pExpr->pLeft
  3487   3481           ** expresssion.  However, make sure the constant has the correct
  3488   3482           ** datatype by applying the Affinity of the table column to the
  3489   3483           ** constant.
  3490   3484           */
  3491   3485           int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
  3492   3486           int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
  3493         -        if( aff!=SQLITE_AFF_BLOB ){
         3487  +        if( aff>SQLITE_AFF_BLOB ){
  3494   3488             static const char zAff[] = "B\000C\000D\000E";
  3495   3489             assert( SQLITE_AFF_BLOB=='A' );
  3496   3490             assert( SQLITE_AFF_TEXT=='B' );
  3497   3491             if( iReg!=target ){
  3498   3492               sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
  3499   3493               iReg = target;
  3500   3494             }
................................................................................
  3789   3783         */
  3790   3784         if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
  3791   3785           const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
  3792   3786           char aff;
  3793   3787           assert( nFarg==1 );
  3794   3788           aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
  3795   3789           sqlite3VdbeLoadString(v, target, 
  3796         -                              aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
         3790  +                (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
  3797   3791           return target;
  3798   3792         }
  3799   3793   #endif
  3800   3794   
  3801   3795         for(i=0; i<nFarg; i++){
  3802   3796           if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
  3803   3797             testcase( i==31 );
................................................................................
  4095   4089         }
  4096   4090         sqlite3ExprDelete(db, pDel);
  4097   4091         sqlite3VdbeResolveLabel(v, endLabel);
  4098   4092         break;
  4099   4093       }
  4100   4094   #ifndef SQLITE_OMIT_TRIGGER
  4101   4095       case TK_RAISE: {
  4102         -      assert( pExpr->affinity==OE_Rollback 
  4103         -           || pExpr->affinity==OE_Abort
  4104         -           || pExpr->affinity==OE_Fail
  4105         -           || pExpr->affinity==OE_Ignore
         4096  +      assert( pExpr->affExpr==OE_Rollback 
         4097  +           || pExpr->affExpr==OE_Abort
         4098  +           || pExpr->affExpr==OE_Fail
         4099  +           || pExpr->affExpr==OE_Ignore
  4106   4100         );
  4107   4101         if( !pParse->pTriggerTab ){
  4108   4102           sqlite3ErrorMsg(pParse,
  4109   4103                          "RAISE() may only be used within a trigger-program");
  4110   4104           return 0;
  4111   4105         }
  4112         -      if( pExpr->affinity==OE_Abort ){
         4106  +      if( pExpr->affExpr==OE_Abort ){
  4113   4107           sqlite3MayAbort(pParse);
  4114   4108         }
  4115   4109         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  4116         -      if( pExpr->affinity==OE_Ignore ){
         4110  +      if( pExpr->affExpr==OE_Ignore ){
  4117   4111           sqlite3VdbeAddOp4(
  4118   4112               v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
  4119   4113           VdbeCoverage(v);
  4120   4114         }else{
  4121   4115           sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
  4122         -                              pExpr->affinity, pExpr->u.zToken, 0, 0);
         4116  +                              pExpr->affExpr, pExpr->u.zToken, 0, 0);
  4123   4117         }
  4124   4118   
  4125   4119         break;
  4126   4120       }
  4127   4121   #endif
  4128   4122     }
  4129   4123     sqlite3ReleaseTempReg(pParse, regFree1);
................................................................................
  4940   4934     Expr *p,            /* The expression to be checked */
  4941   4935     Expr *pNN,          /* The expression that is NOT NULL */
  4942   4936     int iTab,           /* Table being evaluated */
  4943   4937     int seenNot         /* True if p is an operand of NOT */
  4944   4938   ){
  4945   4939     assert( p );
  4946   4940     assert( pNN );
  4947         -  if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ) return 1;
         4941  +  if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
         4942  +    return pNN->op!=TK_NULL;
         4943  +  }
  4948   4944     switch( p->op ){
  4949   4945       case TK_IN: {
  4950   4946         if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
  4951   4947         assert( ExprHasProperty(p,EP_xIsSelect)
  4952   4948              || (p->x.pList!=0 && p->x.pList->nExpr>0) );
  4953   4949         return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
  4954   4950       }

Changes to src/fkey.c.

   474    474     sqlite3 *db = pParse->db;
   475    475   
   476    476     pExpr = sqlite3Expr(db, TK_REGISTER, 0);
   477    477     if( pExpr ){
   478    478       if( iCol>=0 && iCol!=pTab->iPKey ){
   479    479         pCol = &pTab->aCol[iCol];
   480    480         pExpr->iTable = regBase + iCol + 1;
   481         -      pExpr->affinity = pCol->affinity;
          481  +      pExpr->affExpr = pCol->affinity;
   482    482         zColl = pCol->zColl;
   483    483         if( zColl==0 ) zColl = db->pDfltColl->zName;
   484    484         pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
   485    485       }else{
   486    486         pExpr->iTable = regBase;
   487         -      pExpr->affinity = SQLITE_AFF_INTEGER;
          487  +      pExpr->affExpr = SQLITE_AFF_INTEGER;
   488    488       }
   489    489     }
   490    490     return pExpr;
   491    491   }
   492    492   
   493    493   /*
   494    494   ** Return an Expr object that refers to column iCol of table pTab which
................................................................................
  1283   1283         Token tFrom;
  1284   1284         Expr *pRaise; 
  1285   1285   
  1286   1286         tFrom.z = zFrom;
  1287   1287         tFrom.n = nFrom;
  1288   1288         pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
  1289   1289         if( pRaise ){
  1290         -        pRaise->affinity = OE_Abort;
         1290  +        pRaise->affExpr = OE_Abort;
  1291   1291         }
  1292   1292         pSelect = sqlite3SelectNew(pParse, 
  1293   1293             sqlite3ExprListAppend(pParse, 0, pRaise),
  1294   1294             sqlite3SrcListAppend(pParse, 0, &tFrom, 0),
  1295   1295             pWhere,
  1296   1296             0, 0, 0, 0, 0
  1297   1297         );

Changes to src/func.c.

  1982   1982       FUNCTION(coalesce,           0, 0, 0, 0                ),
  1983   1983       FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
  1984   1984     };
  1985   1985   #ifndef SQLITE_OMIT_ALTERTABLE
  1986   1986     sqlite3AlterFunctions();
  1987   1987   #endif
  1988   1988     sqlite3WindowFunctions();
  1989         -#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
  1990         -  sqlite3AnalyzeFunctions();
  1991         -#endif
  1992   1989     sqlite3RegisterDateTimeFunctions();
  1993   1990     sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
  1994   1991   
  1995   1992   #if 0  /* Enable to print out how the built-in functions are hashed */
  1996   1993     {
  1997   1994       int i;
  1998   1995       FuncDef *p;

Changes to src/global.c.

   210    210   SQLITE_WSD struct Sqlite3Config sqlite3Config = {
   211    211      SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
   212    212      1,                         /* bCoreMutex */
   213    213      SQLITE_THREADSAFE==1,      /* bFullMutex */
   214    214      SQLITE_USE_URI,            /* bOpenUri */
   215    215      SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
   216    216      0,                         /* bSmallMalloc */
          217  +   1,                         /* bExtraSchemaChecks */
   217    218      0x7ffffffe,                /* mxStrlen */
   218    219      0,                         /* neverCorrupt */
   219    220      SQLITE_DEFAULT_LOOKASIDE,  /* szLookaside, nLookaside */
   220    221      SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
   221    222      {0,0,0,0,0,0,0,0},         /* m */
   222    223      {0,0,0,0,0,0,0,0,0},       /* mutex */
   223    224      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
................................................................................
   256    257   #ifndef SQLITE_UNTESTABLE
   257    258      0,                         /* xTestCallback */
   258    259   #endif
   259    260      0,                         /* bLocaltimeFault */
   260    261      0,                         /* bInternalFunctions */
   261    262      0x7ffffffe,                /* iOnceResetThreshold */
   262    263      SQLITE_DEFAULT_SORTERREF_SIZE,   /* szSorterRef */
          264  +   0,                         /* iPrngSeed */
   263    265   };
   264    266   
   265    267   /*
   266    268   ** Hash table for global functions - functions common to all
   267    269   ** database connections.  After initialization, this table is
   268    270   ** read-only.
   269    271   */

Changes to src/insert.c.

    84     84       pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
    85     85       if( !pIdx->zColAff ){
    86     86         sqlite3OomFault(db);
    87     87         return 0;
    88     88       }
    89     89       for(n=0; n<pIdx->nColumn; n++){
    90     90         i16 x = pIdx->aiColumn[n];
           91  +      char aff;
    91     92         if( x>=0 ){
    92         -        pIdx->zColAff[n] = pTab->aCol[x].affinity;
           93  +        aff = pTab->aCol[x].affinity;
    93     94         }else if( x==XN_ROWID ){
    94         -        pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
           95  +        aff = SQLITE_AFF_INTEGER;
    95     96         }else{
    96         -        char aff;
    97     97           assert( x==XN_EXPR );
    98     98           assert( pIdx->aColExpr!=0 );
    99     99           aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
   100         -        if( aff==0 ) aff = SQLITE_AFF_BLOB;
   101         -        pIdx->zColAff[n] = aff;
   102    100         }
          101  +      if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
          102  +      pIdx->zColAff[n] = aff;
   103    103       }
   104    104       pIdx->zColAff[n] = 0;
   105    105     }
   106    106    
   107    107     return pIdx->zColAff;
   108    108   }
   109    109   
................................................................................
   135    135       zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
   136    136       if( !zColAff ){
   137    137         sqlite3OomFault(db);
   138    138         return;
   139    139       }
   140    140   
   141    141       for(i=0; i<pTab->nCol; i++){
          142  +      assert( pTab->aCol[i].affinity!=0 );
   142    143         zColAff[i] = pTab->aCol[i].affinity;
   143    144       }
   144    145       do{
   145    146         zColAff[i--] = 0;
   146         -    }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
          147  +    }while( i>=0 && zColAff[i]<=SQLITE_AFF_BLOB );
   147    148       pTab->zColAff = zColAff;
   148    149     }
   149    150     assert( zColAff!=0 );
   150    151     i = sqlite3Strlen30NN(zColAff);
   151    152     if( i ){
   152    153       if( iReg ){
   153    154         sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);

Changes to src/main.c.

  3848   3848       ** this verb acts like PRNG_RESET.
  3849   3849       */
  3850   3850       case SQLITE_TESTCTRL_PRNG_RESTORE: {
  3851   3851         sqlite3PrngRestoreState();
  3852   3852         break;
  3853   3853       }
  3854   3854   
  3855         -    /*
  3856         -    ** Reset the PRNG back to its uninitialized state.  The next call
  3857         -    ** to sqlite3_randomness() will reseed the PRNG using a single call
  3858         -    ** to the xRandomness method of the default VFS.
         3855  +    /*  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
         3856  +    **
         3857  +    ** Control the seed for the pseudo-random number generator (PRNG) that
         3858  +    ** is built into SQLite.  Cases:
         3859  +    **
         3860  +    **    x!=0 && db!=0       Seed the PRNG to the current value of the
         3861  +    **                        schema cookie in the main database for db, or
         3862  +    **                        x if the schema cookie is zero.  This case
         3863  +    **                        is convenient to use with database fuzzers
         3864  +    **                        as it allows the fuzzer some control over the
         3865  +    **                        the PRNG seed.
         3866  +    **
         3867  +    **    x!=0 && db==0       Seed the PRNG to the value of x.
         3868  +    **
         3869  +    **    x==0 && db==0       Revert to default behavior of using the
         3870  +    **                        xRandomness method on the primary VFS.
         3871  +    **
         3872  +    ** This test-control also resets the PRNG so that the new seed will
         3873  +    ** be used for the next call to sqlite3_randomness().
  3859   3874       */
  3860         -    case SQLITE_TESTCTRL_PRNG_RESET: {
         3875  +    case SQLITE_TESTCTRL_PRNG_SEED: {
         3876  +      int x = va_arg(ap, int);
         3877  +      int y;
         3878  +      sqlite3 *db = va_arg(ap, sqlite3*);
         3879  +      assert( db==0 || db->aDb[0].pSchema!=0 );
         3880  +      if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
         3881  +      sqlite3Config.iPrngSeed = x;
  3861   3882         sqlite3_randomness(0,0);
  3862   3883         break;
  3863   3884       }
  3864   3885   
  3865   3886       /*
  3866   3887       **  sqlite3_test_control(BITVEC_TEST, size, program)
  3867   3888       **
................................................................................
  4065   4086       ** testing causes certain assert() statements in the code to be activated
  4066   4087       ** that demonstrat invariants on well-formed database files.
  4067   4088       */
  4068   4089       case SQLITE_TESTCTRL_NEVER_CORRUPT: {
  4069   4090         sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
  4070   4091         break;
  4071   4092       }
         4093  +
         4094  +    /*   sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
         4095  +    **
         4096  +    ** Set or clear a flag that causes SQLite to verify that type, name,
         4097  +    ** and tbl_name fields of the sqlite_master table.  This is normally
         4098  +    ** on, but it is sometimes useful to turn it off for testing.
         4099  +    */
         4100  +    case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: {
         4101  +      sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int);
         4102  +      break;
         4103  +    }
  4072   4104   
  4073   4105       /* Set the threshold at which OP_Once counters reset back to zero.
  4074   4106       ** By default this is 0x7ffffffe (over 2 billion), but that value is
  4075   4107       ** too big to test in a reasonable amount of time, so this control is
  4076   4108       ** provided to set a small and easily reachable reset value.
  4077   4109       */
  4078   4110       case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {

Changes to src/os.c.

   254    254     return pVfs->xDlSym(pVfs, pHdle, zSym);
   255    255   }
   256    256   void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
   257    257     pVfs->xDlClose(pVfs, pHandle);
   258    258   }
   259    259   #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   260    260   int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
   261         -  return pVfs->xRandomness(pVfs, nByte, zBufOut);
          261  +  if( sqlite3Config.iPrngSeed ){
          262  +    memset(zBufOut, 0, nByte);
          263  +    if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
          264  +    memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
          265  +    return SQLITE_OK;
          266  +  }else{
          267  +    return pVfs->xRandomness(pVfs, nByte, zBufOut);
          268  +  }
          269  +  
   262    270   }
   263    271   int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
   264    272     return pVfs->xSleep(pVfs, nMicro);
   265    273   }
   266    274   int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
   267    275     return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
   268    276   }

Changes to src/os_unix.c.

  5766   5766                        || pInode->fileId.ino!=(u64)sStat.st_ino) ){
  5767   5767          pInode = pInode->pNext;
  5768   5768       }
  5769   5769       if( pInode ){
  5770   5770         UnixUnusedFd **pp;
  5771   5771         assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
  5772   5772         sqlite3_mutex_enter(pInode->pLockMutex);
         5773  +      flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
  5773   5774         for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
  5774   5775         pUnused = *pp;
  5775   5776         if( pUnused ){
  5776   5777           *pp = pUnused->pNext;
  5777   5778         }
  5778   5779         sqlite3_mutex_leave(pInode->pLockMutex);
  5779   5780       }
................................................................................
  6069   6070     assert( fd>=0 );
  6070   6071     if( pOutFlags ){
  6071   6072       *pOutFlags = flags;
  6072   6073     }
  6073   6074   
  6074   6075     if( p->pPreallocatedUnused ){
  6075   6076       p->pPreallocatedUnused->fd = fd;
  6076         -    p->pPreallocatedUnused->flags = flags;
         6077  +    p->pPreallocatedUnused->flags = 
         6078  +                          flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
  6077   6079     }
  6078   6080   
  6079   6081     if( isDelete ){
  6080   6082   #if OS_VXWORKS
  6081   6083       zPath = zName;
  6082   6084   #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
  6083   6085       zPath = sqlite3_mprintf("%s", zName);

Changes to src/parse.y.

   945    945     ** that created the expression.
   946    946     */
   947    947     static Expr *tokenExpr(Parse *pParse, int op, Token t){
   948    948       Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
   949    949       if( p ){
   950    950         /* memset(p, 0, sizeof(Expr)); */
   951    951         p->op = (u8)op;
   952         -      p->affinity = 0;
          952  +      p->affExpr = 0;
   953    953         p->flags = EP_Leaf;
   954    954         p->iAgg = -1;
   955    955         p->pLeft = p->pRight = 0;
   956    956         p->x.pList = 0;
   957    957         p->pAggInfo = 0;
   958    958         p->y.pTab = 0;
   959    959         p->op2 = 0;
................................................................................
  1504   1504   trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
  1505   1505      {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
  1506   1506   
  1507   1507   // The special RAISE expression that may occur in trigger programs
  1508   1508   expr(A) ::= RAISE LP IGNORE RP.  {
  1509   1509     A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
  1510   1510     if( A ){
  1511         -    A->affinity = OE_Ignore;
         1511  +    A->affExpr = OE_Ignore;
  1512   1512     }
  1513   1513   }
  1514   1514   expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
  1515   1515     A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 
  1516   1516     if( A ) {
  1517         -    A->affinity = (char)T;
         1517  +    A->affExpr = (char)T;
  1518   1518     }
  1519   1519   }
  1520   1520   %endif  !SQLITE_OMIT_TRIGGER
  1521   1521   
  1522   1522   %type raisetype {int}
  1523   1523   raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
  1524   1524   raisetype(A) ::= ABORT.     {A = OE_Abort;}

Changes to src/prepare.c.

    87     87   /*
    88     88   ** This is the callback routine for the code that initializes the
    89     89   ** database.  See sqlite3Init() below for additional information.
    90     90   ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
    91     91   **
    92     92   ** Each callback contains the following information:
    93     93   **
    94         -**     argv[0] = name of thing being created
    95         -**     argv[1] = root page number for table or index. 0 for trigger or view.
    96         -**     argv[2] = SQL text for the CREATE statement.
           94  +**     argv[0] = type of object: "table", "index", "trigger", or "view".
           95  +**     argv[1] = name of thing being created
           96  +**     argv[2] = associated table if an index or trigger
           97  +**     argv[3] = root page number for table or index. 0 for trigger or view.
           98  +**     argv[4] = SQL text for the CREATE statement.
    97     99   **
    98    100   */
    99    101   int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
   100    102     InitData *pData = (InitData*)pInit;
   101    103     sqlite3 *db = pData->db;
   102    104     int iDb = pData->iDb;
   103    105   
   104         -  assert( argc==3 );
          106  +  assert( argc==5 );
   105    107     UNUSED_PARAMETER2(NotUsed, argc);
   106    108     assert( sqlite3_mutex_held(db->mutex) );
   107    109     DbClearProperty(db, iDb, DB_Empty);
   108    110     pData->nInitRow++;
   109    111     if( db->mallocFailed ){
   110         -    corruptSchema(pData, argv[0], 0);
          112  +    corruptSchema(pData, argv[1], 0);
   111    113       return 1;
   112    114     }
   113    115   
   114    116     assert( iDb>=0 && iDb<db->nDb );
   115    117     if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
   116         -  if( argv[1]==0 ){
   117         -    corruptSchema(pData, argv[0], 0);
   118         -  }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
          118  +  if( argv[3]==0 ){
          119  +    corruptSchema(pData, argv[1], 0);
          120  +  }else if( sqlite3_strnicmp(argv[4],"create ",7)==0 ){
   119    121       /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
   120    122       ** But because db->init.busy is set to 1, no VDBE code is generated
   121    123       ** or executed.  All the parser does is build the internal data
   122    124       ** structures that describe the table, index, or view.
   123    125       */
   124    126       int rc;
   125    127       u8 saved_iDb = db->init.iDb;
   126    128       sqlite3_stmt *pStmt;
   127    129       TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
   128    130   
   129    131       assert( db->init.busy );
   130    132       db->init.iDb = iDb;
   131         -    db->init.newTnum = sqlite3Atoi(argv[1]);
          133  +    db->init.newTnum = sqlite3Atoi(argv[3]);
   132    134       db->init.orphanTrigger = 0;
   133         -    TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
          135  +    db->init.azInit = argv;
          136  +    TESTONLY(rcp = ) sqlite3_prepare(db, argv[4], -1, &pStmt, 0);
   134    137       rc = db->errCode;
   135    138       assert( (rc&0xFF)==(rcp&0xFF) );
   136    139       db->init.iDb = saved_iDb;
   137    140       /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
   138    141       if( SQLITE_OK!=rc ){
   139    142         if( db->init.orphanTrigger ){
   140    143           assert( iDb==1 );
................................................................................
   144    147             sqlite3OomFault(db);
   145    148           }else if( rc!=SQLITE_INTERRUPT 
   146    149   #ifdef SQLITE_ENABLE_SHARED_SCHEMA
   147    150                  && (rc&0xFF)!=SQLITE_LOCKED 
   148    151                  && (rc&0xFF)!=SQLITE_IOERR
   149    152   #endif
   150    153           ){
   151         -          corruptSchema(pData, argv[0], sqlite3_errmsg(db));
          154  +          corruptSchema(pData, argv[1], sqlite3_errmsg(db));
   152    155           }
   153    156         }
   154    157       }
   155    158       sqlite3_finalize(pStmt);
   156         -  }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
   157         -    corruptSchema(pData, argv[0], 0);
          159  +  }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
          160  +    corruptSchema(pData, argv[1], 0);
   158    161     }else{
   159    162       /* If the SQL column is blank it means this is an index that
   160    163       ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
   161    164       ** constraint for a CREATE TABLE.  The index should have already
   162    165       ** been created when we processed the CREATE TABLE.  All we have
   163    166       ** to do here is record the root page number for that index.
   164    167       */
   165    168       Index *pIndex;
   166         -    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
          169  +    pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
   167    170       if( pIndex==0
   168         -     || sqlite3GetInt32(argv[1],&pIndex->tnum)==0
          171  +     || sqlite3GetInt32(argv[3],&pIndex->tnum)==0
   169    172        || pIndex->tnum<2
   170    173        || sqlite3IndexHasDuplicateRootPage(pIndex)
   171    174       ){
   172         -      corruptSchema(pData, argv[0], pIndex?"invalid rootpage":"orphan index");
          175  +      corruptSchema(pData, argv[1], pIndex?"invalid rootpage":"orphan index");
   173    176       }
   174    177     }
   175    178   
   176    179   #ifdef SQLITE_ENABLE_SHARED_SCHEMA
   177    180     if( IsSharedSchema(db) && iDb!=1 ){
   178    181       schemaUpdateChecksum(pData, argv[0], argv[1], argv[2]);
   179    182     }
................................................................................
   192    195   int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
   193    196     int rc;
   194    197     int i;
   195    198   #ifndef SQLITE_OMIT_DEPRECATED
   196    199     int size;
   197    200   #endif
   198    201     Db *pDb;
   199         -  char const *azArg[4];
          202  +  char const *azArg[6];
   200    203     int meta[5];
   201    204     InitData initData;
   202    205     const char *zMasterName;
   203    206     int openedTransaction = 0;
   204    207   
   205    208     assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
   206    209     assert( iDb>=0 && iDb<db->nDb );
................................................................................
   229    232     db->init.busy = 1;
   230    233   
   231    234     /* Construct the in-memory representation schema tables (sqlite_master or
   232    235     ** sqlite_temp_master) by invoking the parser directly.  The appropriate
   233    236     ** table name will be inserted automatically by the parser so we can just
   234    237     ** use the abbreviation "x" here.  The parser will also automatically tag
   235    238     ** the schema table as read-only. */
   236         -  azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
   237         -  azArg[1] = "1";
   238         -  azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
          239  +  azArg[0] = "table";
          240  +  azArg[1] = zMasterName = SCHEMA_TABLE(iDb);
          241  +  azArg[2] = azArg[1];
          242  +  azArg[3] = "1";
          243  +  azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
   239    244                               "rootpage int,sql text)";
   240         -  azArg[3] = 0;
          245  +  azArg[5] = 0;
   241    246     initData.db = db;
   242    247     initData.iDb = iDb;
   243    248     initData.rc = SQLITE_OK;
   244    249     initData.pzErrMsg = pzErrMsg;
   245    250     initData.mInitFlags = mFlags;
   246    251     initData.nInitRow = 0;
   247    252     initData.cksum = 0;
   248         -  sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
          253  +  sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
   249    254     if( initData.rc ){
   250    255       rc = initData.rc;
   251    256       goto error_out;
   252    257     }
   253    258   
   254    259     /* Create a cursor to hold the database open
   255    260     */
................................................................................
   366    371   
   367    372     /* Read the schema information out of the schema tables
   368    373     */
   369    374     assert( db->init.busy );
   370    375     {
   371    376       char *zSql;
   372    377       zSql = sqlite3MPrintf(db, 
   373         -        "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
          378  +        "SELECT*FROM\"%w\".%s ORDER BY rowid",
   374    379           db->aDb[iDb].zDbSName, zMasterName);
   375    380   #ifndef SQLITE_OMIT_AUTHORIZATION
   376    381       {
   377    382         sqlite3_xauth xAuth;
   378    383         xAuth = db->xAuth;
   379    384         db->xAuth = 0;
   380    385   #endif

Changes to src/resolve.c.

   377    377                 ExprSetProperty(pExpr, EP_Alias);
   378    378               }
   379    379             }else
   380    380   #endif /* SQLITE_OMIT_UPSERT */
   381    381             {
   382    382   #ifndef SQLITE_OMIT_TRIGGER
   383    383               if( iCol<0 ){
   384         -              pExpr->affinity = SQLITE_AFF_INTEGER;
          384  +              pExpr->affExpr = SQLITE_AFF_INTEGER;
   385    385               }else if( pExpr->iTable==0 ){
   386    386                 testcase( iCol==31 );
   387    387                 testcase( iCol==32 );
   388    388                 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
   389    389               }else{
   390    390                 testcase( iCol==31 );
   391    391                 testcase( iCol==32 );
................................................................................
   409    409        && pMatch
   410    410        && (pNC->ncFlags & NC_IdxExpr)==0
   411    411        && sqlite3IsRowid(zCol)
   412    412        && VisibleRowid(pMatch->pTab)
   413    413       ){
   414    414         cnt = 1;
   415    415         pExpr->iColumn = -1;
   416         -      pExpr->affinity = SQLITE_AFF_INTEGER;
          416  +      pExpr->affExpr = SQLITE_AFF_INTEGER;
   417    417       }
   418    418   
   419    419       /*
   420    420       ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
   421    421       ** might refer to an result-set alias.  This happens, for example, when
   422    422       ** we are resolving names in the WHERE clause of the following command:
   423    423       **
................................................................................
   685    685         assert( pSrcList && pSrcList->nSrc==1 );
   686    686         pItem = pSrcList->a;
   687    687         assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
   688    688         pExpr->op = TK_COLUMN;
   689    689         pExpr->y.pTab = pItem->pTab;
   690    690         pExpr->iTable = pItem->iCursor;
   691    691         pExpr->iColumn = -1;
   692         -      pExpr->affinity = SQLITE_AFF_INTEGER;
          692  +      pExpr->affExpr = SQLITE_AFF_INTEGER;
   693    693         break;
   694    694       }
   695    695   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
   696    696             && !defined(SQLITE_OMIT_SUBQUERY) */
   697    697   
   698    698       /* A column name:                    ID
   699    699       ** Or table name and column name:    ID.ID
................................................................................
   930    930               sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
   931    931             }
   932    932   #endif
   933    933             while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
   934    934               pExpr->op2++;
   935    935               pNC2 = pNC2->pNext;
   936    936             }
   937         -          assert( pDef!=0 );
   938         -          if( pNC2 ){
          937  +          assert( pDef!=0 || IN_RENAME_OBJECT );
          938  +          if( pNC2 && pDef ){
   939    939               assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
   940    940               testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
   941    941               pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
   942    942   
   943    943             }
   944    944           }
   945    945           pNC->ncFlags |= savedAllowFlags;
................................................................................
  1274   1274     const char *zType     /* "ORDER" or "GROUP" */
  1275   1275   ){
  1276   1276     int i;
  1277   1277     sqlite3 *db = pParse->db;
  1278   1278     ExprList *pEList;
  1279   1279     struct ExprList_item *pItem;
  1280   1280   
  1281         -  if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
         1281  +  if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
  1282   1282     if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  1283   1283       sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
  1284   1284       return 1;
  1285   1285     }
  1286   1286     pEList = pSelect->pEList;
  1287   1287     assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
  1288   1288     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
................................................................................
  1299   1299   }
  1300   1300   
  1301   1301   #ifndef SQLITE_OMIT_WINDOWFUNC
  1302   1302   /*
  1303   1303   ** Walker callback for windowRemoveExprFromSelect().
  1304   1304   */
  1305   1305   static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
         1306  +  UNUSED_PARAMETER(pWalker);
  1306   1307     if( ExprHasProperty(pExpr, EP_WinFunc) ){
  1307   1308       Window *pWin = pExpr->y.pWin;
  1308   1309       sqlite3WindowUnlinkFromSelect(pWin);
  1309   1310     }
  1310   1311     return WRC_Continue;
  1311   1312   }
  1312   1313   

Changes to src/select.c.

  1640   1640     char const *zOrigDb = 0;
  1641   1641     char const *zOrigTab = 0;
  1642   1642     char const *zOrigCol = 0;
  1643   1643   #endif
  1644   1644   
  1645   1645     assert( pExpr!=0 );
  1646   1646     assert( pNC->pSrcList!=0 );
  1647         -  assert( pExpr->op!=TK_AGG_COLUMN );  /* This routine runes before aggregates
  1648         -                                       ** are processed */
  1649   1647     switch( pExpr->op ){
  1650   1648       case TK_COLUMN: {
  1651   1649         /* The expression is a column. Locate the table the column is being
  1652   1650         ** extracted from in NameContext.pSrcList. This table may be real
  1653   1651         ** database table or a subquery.
  1654   1652         */
  1655   1653         Table *pTab = 0;            /* Table structure column is extracted from */
................................................................................
  1963   1961         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1964   1962       }else{
  1965   1963         Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1966   1964         while( pColExpr->op==TK_DOT ){
  1967   1965           pColExpr = pColExpr->pRight;
  1968   1966           assert( pColExpr!=0 );
  1969   1967         }
  1970         -      assert( pColExpr->op!=TK_AGG_COLUMN );
  1971   1968         if( pColExpr->op==TK_COLUMN ){
  1972   1969           /* For columns use the column name name */
  1973   1970           int iCol = pColExpr->iColumn;
  1974   1971           Table *pTab = pColExpr->y.pTab;
  1975   1972           assert( pTab!=0 );
  1976   1973           if( iCol<0 ) iCol = pTab->iPKey;
  1977   1974           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
................................................................................
  2031   2028   **
  2032   2029   ** This routine requires that all identifiers in the SELECT
  2033   2030   ** statement be resolved.
  2034   2031   */
  2035   2032   void sqlite3SelectAddColumnTypeAndCollation(
  2036   2033     Parse *pParse,        /* Parsing contexts */
  2037   2034     Table *pTab,          /* Add column type information to this table */
  2038         -  Select *pSelect       /* SELECT used to determine types and collations */
         2035  +  Select *pSelect,      /* SELECT used to determine types and collations */
         2036  +  char aff              /* Default affinity for columns */
  2039   2037   ){
  2040   2038     sqlite3 *db = pParse->db;
  2041   2039     NameContext sNC;
  2042   2040     Column *pCol;
  2043   2041     CollSeq *pColl;
  2044   2042     int i;
  2045   2043     Expr *p;
................................................................................
  2064   2062         n = sqlite3Strlen30(pCol->zName);
  2065   2063         pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
  2066   2064         if( pCol->zName ){
  2067   2065           memcpy(&pCol->zName[n+1], zType, m+1);
  2068   2066           pCol->colFlags |= COLFLAG_HASTYPE;
  2069   2067         }
  2070   2068       }
  2071         -    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
         2069  +    if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
  2072   2070       pColl = sqlite3ExprCollSeq(pParse, p);
  2073   2071       if( pColl && pCol->zColl==0 ){
  2074   2072         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
  2075   2073       }
  2076   2074     }
  2077   2075     pTab->szTabRow = 1; /* Any non-zero value works */
  2078   2076   }
  2079   2077   
  2080   2078   /*
  2081   2079   ** Given a SELECT statement, generate a Table structure that describes
  2082   2080   ** the result set of that SELECT.
  2083   2081   */
  2084         -Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
         2082  +Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
  2085   2083     Table *pTab;
  2086   2084     sqlite3 *db = pParse->db;
  2087   2085     u64 savedFlags;
  2088   2086   
  2089   2087     savedFlags = db->flags;
  2090   2088     db->flags &= ~(u64)SQLITE_FullColNames;
  2091   2089     db->flags |= SQLITE_ShortColNames;
................................................................................
  2097   2095     if( pTab==0 ){
  2098   2096       return 0;
  2099   2097     }
  2100   2098     pTab->nTabRef = 1;
  2101   2099     pTab->zName = 0;
  2102   2100     pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
  2103   2101     sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
  2104         -  sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect);
         2102  +  sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
  2105   2103     pTab->iPKey = -1;
  2106   2104     if( db->mallocFailed ){
  2107   2105       sqlite3DeleteTable(db, pTab);
  2108   2106       return 0;
  2109   2107     }
  2110   2108     return pTab;
  2111   2109   }
................................................................................
  2962   2960                              pIn->iSdst, pIn->nSdst);
  2963   2961         sqlite3ReleaseTempReg(pParse, r1);
  2964   2962         break;
  2965   2963       }
  2966   2964   
  2967   2965       /* If this is a scalar select that is part of an expression, then
  2968   2966       ** store the results in the appropriate memory cell and break out
  2969         -    ** of the scan loop.
         2967  +    ** of the scan loop.  Note that the select might return multiple columns
         2968  +    ** if it is the RHS of a row-value IN operator.
  2970   2969       */
  2971   2970       case SRT_Mem: {
  2972         -      assert( pIn->nSdst==1 || pParse->nErr>0 );  testcase( pIn->nSdst!=1 );
  2973         -      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
         2971  +      if( pParse->nErr==0 ){
         2972  +        testcase( pIn->nSdst>1 );
         2973  +        sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
         2974  +      }
  2974   2975         /* The LIMIT clause will jump out of the loop for us */
  2975   2976         break;
  2976   2977       }
  2977   2978   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
  2978   2979   
  2979   2980       /* The results are stored in a sequence of registers
  2980   2981       ** starting at pDest->iSdst.  Then the co-routine yields.
................................................................................
  3470   3471           pNew = sqlite3ExprDup(db, pCopy, 0);
  3471   3472           if( pNew && pSubst->isLeftJoin ){
  3472   3473             ExprSetProperty(pNew, EP_CanBeNull);
  3473   3474           }
  3474   3475           if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
  3475   3476             pNew->iRightJoinTable = pExpr->iRightJoinTable;
  3476   3477             ExprSetProperty(pNew, EP_FromJoin);
         3478  +        }
         3479  +        if( pNew && ExprHasProperty(pExpr,EP_Generic) ){
         3480  +          ExprSetProperty(pNew, EP_Generic);
  3477   3481           }
  3478   3482           sqlite3ExprDelete(db, pExpr);
  3479   3483           pExpr = pNew;
  3480   3484         }
  3481   3485       }
  3482   3486     }else{
  3483   3487       if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
................................................................................
  5191   5195       Table *pTab = pFrom->pTab;
  5192   5196       assert( pTab!=0 );
  5193   5197       if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
  5194   5198         /* A sub-query in the FROM clause of a SELECT */
  5195   5199         Select *pSel = pFrom->pSelect;
  5196   5200         if( pSel ){
  5197   5201           while( pSel->pPrior ) pSel = pSel->pPrior;
  5198         -        sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel);
         5202  +        sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel,
         5203  +                                               SQLITE_AFF_NONE);
  5199   5204         }
  5200   5205       }
  5201   5206     }
  5202   5207   }
  5203   5208   #endif
  5204   5209   
  5205   5210   
................................................................................
  5851   5856       ** have a column named by the empty string, in which case there is no way to
  5852   5857       ** distinguish between an unreferenced table and an actual reference to the
  5853   5858       ** "" column. The original design was for the fake column name to be a NULL,
  5854   5859       ** which would be unambiguous.  But legacy authorization callbacks might
  5855   5860       ** assume the column name is non-NULL and segfault.  The use of an empty
  5856   5861       ** string for the fake column name seems safer.
  5857   5862       */
  5858         -    if( pItem->colUsed==0 ){
         5863  +    if( pItem->colUsed==0 && pItem->zName!=0 ){
  5859   5864         sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
  5860   5865       }
  5861   5866   
  5862   5867   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  5863   5868       /* Generate code for all sub-queries in the FROM clause
  5864   5869       */
  5865   5870       pSub = pItem->pSelect;

Changes to src/shell.c.in.

  3979   3979       sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
  3980   3980                          + ((sqlite3_int64)a[1]<<16)
  3981   3981                          + ((sqlite3_int64)a[2]<< 8)
  3982   3982                          + ((sqlite3_int64)a[3]<< 0);
  3983   3983       sqlite3_result_int64(context, iVal);
  3984   3984     }
  3985   3985   }
         3986  +
         3987  +/*
         3988  +** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
         3989  +** using "..." with internal double-quote characters doubled.
         3990  +*/
         3991  +static void shellIdQuote(
         3992  +  sqlite3_context *context, 
         3993  +  int argc, 
         3994  +  sqlite3_value **argv
         3995  +){
         3996  +  const char *zName = (const char*)sqlite3_value_text(argv[0]);
         3997  +  UNUSED_PARAMETER(argc);
         3998  +  if( zName ){
         3999  +    char *z = sqlite3_mprintf("\"%w\"", zName);
         4000  +    sqlite3_result_text(context, z, -1, sqlite3_free);
         4001  +  }
         4002  +}
  3986   4003   
  3987   4004   /*
  3988   4005   ** Scalar function "shell_escape_crnl" used by the .recover command.
  3989   4006   ** The argument passed to this function is the output of built-in
  3990   4007   ** function quote(). If the first character of the input is "'", 
  3991   4008   ** indicating that the value passed to quote() was a text value,
  3992   4009   ** then this function searches the input for "\n" and "\r" characters
................................................................................
  4161   4178                               shellModuleSchema, 0, 0);
  4162   4179       sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
  4163   4180                               shellPutsFunc, 0, 0);
  4164   4181       sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
  4165   4182                               shellEscapeCrnl, 0, 0);
  4166   4183       sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
  4167   4184                               shellInt32, 0, 0);
         4185  +    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
         4186  +                            shellIdQuote, 0, 0);
  4168   4187   #ifndef SQLITE_NOHAVE_SYSTEM
  4169   4188       sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
  4170   4189                               editFunc, 0, 0);
  4171   4190       sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
  4172   4191                               editFunc, 0, 0);
  4173   4192   #endif
  4174   4193       if( p->openMode==SHELL_OPEN_ZIPFILE ){
................................................................................
  6569   6588     pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
  6570   6589     if( rc==SQLITE_OK ){
  6571   6590       int nSqlCol = 0;
  6572   6591       int bSqlIntkey = 0;
  6573   6592       sqlite3_stmt *pStmt = 0;
  6574   6593       
  6575   6594       rc = sqlite3_open("", &dbtmp);
         6595  +    if( rc==SQLITE_OK ){
         6596  +      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
         6597  +                              shellIdQuote, 0, 0);
         6598  +    }
  6576   6599       if( rc==SQLITE_OK ){
  6577   6600         rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
  6578   6601       }
  6579   6602       if( rc==SQLITE_OK ){
  6580   6603         rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
  6581   6604         if( rc==SQLITE_ERROR ){
  6582   6605           rc = SQLITE_OK;
................................................................................
  6625   6648           );
  6626   6649           if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
  6627   6650             pTab->iPk = sqlite3_column_int(pPkFinder, 0);
  6628   6651             zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
  6629   6652           }
  6630   6653         }
  6631   6654   
  6632         -      pTab->zQuoted = shellMPrintf(&rc, "%Q", zName);
         6655  +      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
  6633   6656         pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
  6634   6657         pTab->nCol = nSqlCol;
  6635   6658   
  6636   6659         if( bIntkey ){
  6637         -        pTab->azlCol[0] = shellMPrintf(&rc, "%Q", zPk);
         6660  +        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
  6638   6661         }else{
  6639   6662           pTab->azlCol[0] = shellMPrintf(&rc, "");
  6640   6663         }
  6641   6664         i = 1;
  6642   6665         shellPreparePrintf(dbtmp, &rc, &pStmt, 
  6643         -          "SELECT %Q || group_concat(quote(name), ', ') "
         6666  +          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
  6644   6667             "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
  6645   6668             "FROM pragma_table_info(%Q)", 
  6646   6669             bIntkey ? ", " : "", pTab->iPk, 
  6647   6670             bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
  6648   6671             zName
  6649   6672         );
  6650   6673         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
................................................................................
  6750   6773         zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
  6751   6774         sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
  6752   6775       }
  6753   6776       shellFinalize(pRc, pTest);
  6754   6777   
  6755   6778       pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
  6756   6779       if( pTab ){
  6757         -      pTab->zQuoted = shellMPrintf(pRc, "%Q", zTab);
         6780  +      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
  6758   6781         pTab->nCol = nCol;
  6759   6782         pTab->iPk = -2;
  6760   6783         if( nCol>0 ){
  6761   6784           pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
  6762   6785           if( pTab->azlCol ){
  6763   6786             pTab->azlCol[nCol] = shellMPrintf(pRc, "");
  6764   6787             for(i=nCol-1; i>=0; i--){
................................................................................
  6947   6970       "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
  6948   6971     );
  6949   6972   
  6950   6973     /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 
  6951   6974     ** CREATE TABLE statements that extracted from the existing schema.  */
  6952   6975     if( rc==SQLITE_OK ){
  6953   6976       sqlite3_stmt *pStmt = 0;
         6977  +    /* ".recover" might output content in an order which causes immediate
         6978  +    ** foreign key constraints to be violated. So disable foreign-key
         6979  +    ** constraint enforcement to prevent problems when running the output
         6980  +    ** script. */
         6981  +    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
  6954   6982       raw_printf(pState->out, "BEGIN;\n");
  6955   6983       raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
  6956   6984       shellPrepare(pState->db, &rc,
  6957   6985           "SELECT sql FROM recovery.schema "
  6958   6986           "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
  6959   6987       );
  6960   6988       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
................................................................................
  7009   7037         if( pOrphan==0 ){
  7010   7038           pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
  7011   7039         }
  7012   7040         pTab = pOrphan;
  7013   7041         if( pTab==0 ) break;
  7014   7042       }
  7015   7043   
  7016         -    if( 0==sqlite3_stricmp(pTab->zQuoted, "'sqlite_sequence'") ){
         7044  +    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
  7017   7045         raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
  7018   7046       }
  7019   7047       sqlite3_bind_int(pPages, 1, iRoot);
  7020   7048       sqlite3_bind_int(pCells, 2, pTab->iPk);
  7021   7049   
  7022   7050       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
  7023   7051         int iPgno = sqlite3_column_int(pPages, 0);
................................................................................
  7719   7747       }else{
  7720   7748         raw_printf(p->out, "ANALYZE sqlite_master;\n");
  7721   7749         sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
  7722   7750                      callback, &data, &zErrMsg);
  7723   7751         data.cMode = data.mode = MODE_Insert;
  7724   7752         data.zDestTable = "sqlite_stat1";
  7725   7753         shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
  7726         -      data.zDestTable = "sqlite_stat3";
  7727         -      shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
  7728   7754         data.zDestTable = "sqlite_stat4";
  7729   7755         shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
  7730   7756         raw_printf(p->out, "ANALYZE sqlite_master;\n");
  7731   7757       }
  7732   7758     }else
  7733   7759   
  7734   7760     if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
................................................................................
  9144   9170                              " ORDER BY name;", 0);
  9145   9171         }else if( strcmp(zTab, "sqlite_sequence")==0 ){
  9146   9172           appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
  9147   9173                              " ORDER BY name;", 0);
  9148   9174         }else if( strcmp(zTab, "sqlite_stat1")==0 ){
  9149   9175           appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
  9150   9176                              " ORDER BY tbl,idx;", 0);
  9151         -      }else if( strcmp(zTab, "sqlite_stat3")==0
  9152         -             || strcmp(zTab, "sqlite_stat4")==0 ){
         9177  +      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
  9153   9178           appendText(&sQuery, "SELECT * FROM ", 0);
  9154   9179           appendText(&sQuery, zTab, 0);
  9155   9180           appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
  9156   9181         }
  9157   9182         appendText(&sSql, zSep, 0);
  9158   9183         appendText(&sSql, sQuery.z, '\'');
  9159   9184         sQuery.n = 0;
................................................................................
  9389   9414          const char *zUsage;      /* Usage notes */
  9390   9415       } aCtrl[] = {
  9391   9416         { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
  9392   9417         { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
  9393   9418       /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
  9394   9419       /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
  9395   9420         { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
         9421  +      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"       },
  9396   9422       /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
  9397   9423         { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
  9398   9424         { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"       },
  9399   9425         { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
  9400   9426         { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
  9401   9427         { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
  9402   9428   #ifdef YYCOVERAGE
  9403   9429         { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
  9404   9430   #endif
  9405   9431         { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
  9406         -      { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
  9407   9432         { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
  9408   9433         { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
         9434  +      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"          },
  9409   9435         { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
  9410   9436       };
  9411   9437       int testctrl = -1;
  9412   9438       int iCtrl = -1;
  9413   9439       int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
  9414   9440       int isOk = 0;
  9415   9441       int i, n2;
................................................................................
  9482   9508           case SQLITE_TESTCTRL_PENDING_BYTE:
  9483   9509             if( nArg==3 ){
  9484   9510               unsigned int opt = (unsigned int)integerValue(azArg[2]);
  9485   9511               rc2 = sqlite3_test_control(testctrl, opt);
  9486   9512               isOk = 3;
  9487   9513             }
  9488   9514             break;
         9515  +
         9516  +        /* sqlite3_test_control(int, int, sqlite3*) */
         9517  +        case SQLITE_TESTCTRL_PRNG_SEED:
         9518  +          if( nArg==3 || nArg==4 ){
         9519  +            int ii = (int)integerValue(azArg[2]);
         9520  +            sqlite3 *db;
         9521  +            if( ii==0 && strcmp(azArg[2],"random")==0 ){
         9522  +              sqlite3_randomness(sizeof(ii),&ii);
         9523  +              printf("-- random seed: %d\n", ii);
         9524  +            }
         9525  +            if( nArg==3 ){
         9526  +              db = 0;
         9527  +            }else{
         9528  +              db = p->db;
         9529  +              /* Make sure the schema has been loaded */
         9530  +              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
         9531  +            }
         9532  +            rc2 = sqlite3_test_control(testctrl, ii, db);
         9533  +            isOk = 3;
         9534  +          }
         9535  +          break;
  9489   9536   
  9490   9537           /* sqlite3_test_control(int, int) */
  9491   9538           case SQLITE_TESTCTRL_ASSERT:
  9492   9539           case SQLITE_TESTCTRL_ALWAYS:
  9493   9540           case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
  9494   9541             if( nArg==3 ){
  9495   9542               int opt = booleanValue(azArg[2]);

Changes to src/sqlite.h.in.

  3812   3812   ** WHERE clause might influence the choice of query plan for a statement,
  3813   3813   ** then the statement will be automatically recompiled, as if there had been 
  3814   3814   ** a schema change, on the first  [sqlite3_step()] call following any change
  3815   3815   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3816   3816   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3817   3817   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3818   3818   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3819         -** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
         3819  +** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
  3820   3820   ** </li>
  3821   3821   ** </ol>
  3822   3822   **
  3823   3823   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  3824   3824   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  3825   3825   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  3826   3826   ** sqlite3_prepare_v2() interface works exactly the same as
................................................................................
  7333   7333   ** without notice.  These values are for testing purposes only.
  7334   7334   ** Applications should not use any of these parameters or the
  7335   7335   ** [sqlite3_test_control()] interface.
  7336   7336   */
  7337   7337   #define SQLITE_TESTCTRL_FIRST                    5
  7338   7338   #define SQLITE_TESTCTRL_PRNG_SAVE                5
  7339   7339   #define SQLITE_TESTCTRL_PRNG_RESTORE             6
  7340         -#define SQLITE_TESTCTRL_PRNG_RESET               7
         7340  +#define SQLITE_TESTCTRL_PRNG_RESET               7  /* NOT USED */
  7341   7341   #define SQLITE_TESTCTRL_BITVEC_TEST              8
  7342   7342   #define SQLITE_TESTCTRL_FAULT_INSTALL            9
  7343   7343   #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  7344   7344   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  7345   7345   #define SQLITE_TESTCTRL_ASSERT                  12
  7346   7346   #define SQLITE_TESTCTRL_ALWAYS                  13
  7347   7347   #define SQLITE_TESTCTRL_RESERVE                 14
................................................................................
  7356   7356   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  7357   7357   #define SQLITE_TESTCTRL_BYTEORDER               22
  7358   7358   #define SQLITE_TESTCTRL_ISINIT                  23
  7359   7359   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  7360   7360   #define SQLITE_TESTCTRL_IMPOSTER                25
  7361   7361   #define SQLITE_TESTCTRL_PARSER_COVERAGE         26
  7362   7362   #define SQLITE_TESTCTRL_RESULT_INTREAL          27
  7363         -#define SQLITE_TESTCTRL_LAST                    27  /* Largest TESTCTRL */
         7363  +#define SQLITE_TESTCTRL_PRNG_SEED               28
         7364  +#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS     29
         7365  +#define SQLITE_TESTCTRL_LAST                    29  /* Largest TESTCTRL */
  7364   7366   
  7365   7367   /*
  7366   7368   ** CAPI3REF: SQL Keyword Checking
  7367   7369   **
  7368   7370   ** These routines provide access to the set of SQL language keywords 
  7369   7371   ** recognized by SQLite.  Applications can uses these routines to determine
  7370   7372   ** whether or not a specific identifier needs to be escaped (for example,

Changes to src/sqliteInt.h.

   931    931   # define SQLITE_DEFAULT_MMAP_SIZE 0
   932    932   #endif
   933    933   #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
   934    934   # undef SQLITE_DEFAULT_MMAP_SIZE
   935    935   # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
   936    936   #endif
   937    937   
   938         -/*
   939         -** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
   940         -** Priority is given to SQLITE_ENABLE_STAT4.  If either are defined, also
   941         -** define SQLITE_ENABLE_STAT3_OR_STAT4
   942         -*/
   943         -#ifdef SQLITE_ENABLE_STAT4
   944         -# undef SQLITE_ENABLE_STAT3
   945         -# define SQLITE_ENABLE_STAT3_OR_STAT4 1
   946         -#elif SQLITE_ENABLE_STAT3
   947         -# define SQLITE_ENABLE_STAT3_OR_STAT4 1
   948         -#elif SQLITE_ENABLE_STAT3_OR_STAT4
   949         -# undef SQLITE_ENABLE_STAT3_OR_STAT4
   950         -#endif
   951         -
   952    938   /*
   953    939   ** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
   954    940   ** the Select query generator tracing logic is turned on.
   955    941   */
   956    942   #if defined(SQLITE_ENABLE_SELECTTRACE)
   957    943   # define SELECTTRACE_ENABLED 1
   958    944   #else
................................................................................
  1416   1402     struct sqlite3InitInfo {      /* Information used during initialization */
  1417   1403       int newTnum;                /* Rootpage of table being initialized */
  1418   1404       u8 iDb;                     /* Which db file is being initialized */
  1419   1405       u8 busy;                    /* TRUE if currently initializing */
  1420   1406       unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
  1421   1407       unsigned imposterTable : 1; /* Building an imposter table */
  1422   1408       unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
         1409  +    char **azInit;              /* "type", "name", and "tbl_name" columns */
  1423   1410     } init;
  1424   1411     int nVdbeActive;              /* Number of VDBEs currently running */
  1425   1412     int nVdbeRead;                /* Number of active VDBEs that read or write */
  1426   1413     int nVdbeWrite;               /* Number of active VDBEs that read and write */
  1427   1414     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
  1428   1415     int nVDestroy;                /* Number of active OP_VDestroy operations */
  1429   1416     int nExtension;               /* Number of loaded extensions */
................................................................................
  1600   1587   #define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
  1601   1588   #define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
  1602   1589   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
  1603   1590   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
  1604   1591   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
  1605   1592   #define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
  1606   1593   #define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
  1607         -#define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
  1608         -   /* TH3 expects the Stat34  ^^^^^^ value to be 0x0800.  Don't change it */
         1594  +#define SQLITE_Stat4          0x0800   /* Use STAT4 data */
         1595  +   /* TH3 expects the Stat4   ^^^^^^ value to be 0x0800.  Don't change it */
  1609   1596   #define SQLITE_PushDown       0x1000   /* The push-down optimization */
  1610   1597   #define SQLITE_SimplifyJoin   0x2000   /* Convert LEFT JOIN to JOIN */
  1611   1598   #define SQLITE_SkipScan       0x4000   /* Skip-scans */
  1612   1599   #define SQLITE_PropagateConst 0x8000   /* The constant propagation opt */
  1613   1600   #define SQLITE_AllOpts        0xffff   /* All optimizations */
  1614   1601   
  1615   1602   /*
................................................................................
  1887   1874   ** But rather than start with 0 or 1, we begin with 'A'.  That way,
  1888   1875   ** when multiple affinity types are concatenated into a string and
  1889   1876   ** used as the P4 operand, they will be more readable.
  1890   1877   **
  1891   1878   ** Note also that the numeric types are grouped together so that testing
  1892   1879   ** for a numeric type is a single comparison.  And the BLOB type is first.
  1893   1880   */
  1894         -#define SQLITE_AFF_BLOB     'A'
  1895         -#define SQLITE_AFF_TEXT     'B'
  1896         -#define SQLITE_AFF_NUMERIC  'C'
  1897         -#define SQLITE_AFF_INTEGER  'D'
  1898         -#define SQLITE_AFF_REAL     'E'
         1881  +#define SQLITE_AFF_NONE     0x40  /* '@' */
         1882  +#define SQLITE_AFF_BLOB     0x41  /* 'A' */
         1883  +#define SQLITE_AFF_TEXT     0x42  /* 'B' */
         1884  +#define SQLITE_AFF_NUMERIC  0x43  /* 'C' */
         1885  +#define SQLITE_AFF_INTEGER  0x44  /* 'D' */
         1886  +#define SQLITE_AFF_REAL     0x45  /* 'E' */
  1899   1887   
  1900   1888   #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
  1901   1889   
  1902   1890   /*
  1903   1891   ** The SQLITE_AFF_MASK values masks off the significant bits of an
  1904   1892   ** affinity value.
  1905   1893   */
................................................................................
  2273   2261     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  2274   2262     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
  2275   2263     unsigned isCovering:1;   /* True if this is a covering index */
  2276   2264     unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
  2277   2265     unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
  2278   2266     unsigned bNoQuery:1;     /* Do not use this index to optimize queries */
  2279   2267     unsigned bAscKeyBug:1;   /* True if the bba7b69f9849b5bf bug applies */
  2280         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         2268  +#ifdef SQLITE_ENABLE_STAT4
  2281   2269     int nSample;             /* Number of elements in aSample[] */
  2282   2270     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
  2283   2271     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
  2284   2272     IndexSample *aSample;    /* Samples of the left-most key */
  2285   2273     tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
  2286   2274     tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
  2287   2275   #endif
................................................................................
  2305   2293   /* The Index.aiColumn[] values are normally positive integer.  But
  2306   2294   ** there are some negative values that have special meaning:
  2307   2295   */
  2308   2296   #define XN_ROWID     (-1)     /* Indexed column is the rowid */
  2309   2297   #define XN_EXPR      (-2)     /* Indexed column is an expression */
  2310   2298   
  2311   2299   /*
  2312         -** Each sample stored in the sqlite_stat3 table is represented in memory
         2300  +** Each sample stored in the sqlite_stat4 table is represented in memory
  2313   2301   ** using a structure of this type.  See documentation at the top of the
  2314   2302   ** analyze.c source file for additional information.
  2315   2303   */
  2316   2304   struct IndexSample {
  2317   2305     void *p;          /* Pointer to sampled record */
  2318   2306     int n;            /* Size of record in bytes */
  2319   2307     tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
................................................................................
  2463   2451   ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
  2464   2452   ** are contained within the same memory allocation.  Note, however, that
  2465   2453   ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  2466   2454   ** allocated, regardless of whether or not EP_Reduced is set.
  2467   2455   */
  2468   2456   struct Expr {
  2469   2457     u8 op;                 /* Operation performed by this node */
  2470         -  char affinity;         /* The affinity of the column or 0 if not a column */
         2458  +  char affExpr;          /* affinity, or RAISE type */
  2471   2459     u32 flags;             /* Various flags.  EP_* See below */
  2472   2460     union {
  2473   2461       char *zToken;          /* Token value. Zero terminated and dequoted */
  2474   2462       int iValue;            /* Non-negative integer value if EP_IntValue */
  2475   2463     } u;
  2476   2464   
  2477   2465     /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
................................................................................
  2920   2908   #define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
  2921   2909   #define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
  2922   2910   #define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
  2923   2911   #define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
  2924   2912   #define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
  2925   2913   #define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
  2926   2914   #define SF_ComplexResult  0x40000  /* Result contains subquery or function */
         2915  +#define SF_WhereBegin     0x80000  /* Really a WhereBegin() call.  Debug Only */
  2927   2916   
  2928   2917   /*
  2929   2918   ** The results of a SELECT can be distributed in several ways, as defined
  2930   2919   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2931   2920   ** Type".
  2932   2921   **
  2933   2922   **     SRT_Union       Store results as a key in a temporary index
................................................................................
  3427   3416   /*
  3428   3417   ** Structure containing global configuration data for the SQLite library.
  3429   3418   **
  3430   3419   ** This structure also contains some state information.
  3431   3420   */
  3432   3421   struct Sqlite3Config {
  3433   3422     int bMemstat;                     /* True to enable memory status */
  3434         -  int bCoreMutex;                   /* True to enable core mutexing */
  3435         -  int bFullMutex;                   /* True to enable full mutexing */
  3436         -  int bOpenUri;                     /* True to interpret filenames as URIs */
  3437         -  int bUseCis;                      /* Use covering indices for full-scans */
  3438         -  int bSmallMalloc;                 /* Avoid large memory allocations if true */
         3423  +  u8 bCoreMutex;                    /* True to enable core mutexing */
         3424  +  u8 bFullMutex;                    /* True to enable full mutexing */
         3425  +  u8 bOpenUri;                      /* True to interpret filenames as URIs */
         3426  +  u8 bUseCis;                       /* Use covering indices for full-scans */
         3427  +  u8 bSmallMalloc;                  /* Avoid large memory allocations if true */
         3428  +  u8 bExtraSchemaChecks;            /* Verify type,name,tbl_name in schema */
  3439   3429     int mxStrlen;                     /* Maximum string length */
  3440   3430     int neverCorrupt;                 /* Database is always well-formed */
  3441   3431     int szLookaside;                  /* Default lookaside buffer size */
  3442   3432     int nLookaside;                   /* Default lookaside buffer count */
  3443   3433     int nStmtSpill;                   /* Stmt-journal spill-to-disk threshold */
  3444   3434     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  3445   3435     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
................................................................................
  3483   3473   #ifndef SQLITE_UNTESTABLE
  3484   3474     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
  3485   3475   #endif
  3486   3476     int bLocaltimeFault;              /* True to fail localtime() calls */
  3487   3477     int bInternalFunctions;           /* Internal SQL functions are visible */
  3488   3478     int iOnceResetThreshold;          /* When to reset OP_Once counters */
  3489   3479     u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
         3480  +  unsigned int iPrngSeed;           /* Alternative fixed seed for the PRNG */
  3490   3481   };
  3491   3482   
  3492   3483   /*
  3493   3484   ** This macro is used inside of assert() statements to indicate that
  3494   3485   ** the assert is only valid on a well-formed database.  Instead of:
  3495   3486   **
  3496   3487   **     assert( X );
................................................................................
  3928   3919   #endif
  3929   3920   void sqlite3ResetAllSchemasOfConnection(sqlite3*);
  3930   3921   void sqlite3ResetOneSchema(sqlite3*,int);
  3931   3922   void sqlite3CollapseDatabaseArray(sqlite3*);
  3932   3923   void sqlite3CommitInternalChanges(sqlite3*);
  3933   3924   void sqlite3DeleteColumnNames(sqlite3*,Table*);
  3934   3925   int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
  3935         -void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
  3936         -Table *sqlite3ResultSetOfSelect(Parse*,Select*);
         3926  +void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
         3927  +Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
  3937   3928   void sqlite3OpenMasterTable(Parse *, int);
  3938   3929   Index *sqlite3PrimaryKeyIndex(Table*);
  3939   3930   i16 sqlite3ColumnOfIndex(Index*, i16);
  3940   3931   void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
  3941   3932   #if SQLITE_ENABLE_HIDDEN_COLUMNS
  3942   3933     void sqlite3ColumnPropertiesFromName(Table*, Column*);
  3943   3934   #else
................................................................................
  4230   4221   u32 sqlite3Utf8Read(const u8**);
  4231   4222   LogEst sqlite3LogEst(u64);
  4232   4223   LogEst sqlite3LogEstAdd(LogEst,LogEst);
  4233   4224   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4234   4225   LogEst sqlite3LogEstFromDouble(double);
  4235   4226   #endif
  4236   4227   #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  4237         -    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
         4228  +    defined(SQLITE_ENABLE_STAT4) || \
  4238   4229       defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  4239   4230   u64 sqlite3LogEstToInt(LogEst);
  4240   4231   #endif
  4241   4232   VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
  4242   4233   const char *sqlite3VListNumToName(VList*,int);
  4243   4234   int sqlite3VListNameToNum(VList*,const char*,int);
  4244   4235   
................................................................................
  4298   4289   CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
  4299   4290   int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
  4300   4291   Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
  4301   4292   Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
  4302   4293   Expr *sqlite3ExprSkipCollate(Expr*);
  4303   4294   int sqlite3CheckCollSeq(Parse *, CollSeq *);
  4304   4295   int sqlite3WritableSchema(sqlite3*);
  4305         -int sqlite3CheckObjectName(Parse *, const char *);
         4296  +int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
  4306   4297   void sqlite3VdbeSetChanges(sqlite3 *, int);
  4307   4298   int sqlite3AddInt64(i64*,i64);
  4308   4299   int sqlite3SubInt64(i64*,i64);
  4309   4300   int sqlite3MulInt64(i64*,i64);
  4310   4301   int sqlite3AbsInt32(int);
  4311   4302   #ifdef SQLITE_ENABLE_8_3_NAMES
  4312   4303   void sqlite3FileSuffix3(const char*, char*);
................................................................................
  4441   4432   
  4442   4433   #ifndef SQLITE_OMIT_SUBQUERY
  4443   4434   int sqlite3ExprCheckIN(Parse*, Expr*);
  4444   4435   #else
  4445   4436   # define sqlite3ExprCheckIN(x,y) SQLITE_OK
  4446   4437   #endif
  4447   4438   
  4448         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  4449         -void sqlite3AnalyzeFunctions(void);
         4439  +#ifdef SQLITE_ENABLE_STAT4
  4450   4440   int sqlite3Stat4ProbeSetValue(
  4451   4441       Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
  4452   4442   int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
  4453   4443   void sqlite3Stat4ProbeFree(UnpackedRecord*);
  4454   4444   int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
  4455   4445   char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
  4456   4446   #endif

Changes to src/test1.c.

  6369   6369   */
  6370   6370   static int SQLITE_TCLAPI reset_prng_state(
  6371   6371     ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  6372   6372     Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  6373   6373     int objc,              /* Number of arguments */
  6374   6374     Tcl_Obj *CONST objv[]  /* Command arguments */
  6375   6375   ){
  6376         -  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
         6376  +  sqlite3_randomness(0,0);
         6377  +  return TCL_OK;
         6378  +}
         6379  +/*
         6380  +** tclcmd:  prng_seed INT ?DB?
         6381  +**
         6382  +** Set up the SQLITE_TESTCTRL_PRNG_SEED pragma with parameter INT and DB.
         6383  +** INT is an integer.  DB is a database connection, or a NULL pointer if
         6384  +** omitted.
         6385  +**
         6386  +** When INT!=0 and DB!=0, set the PRNG seed to the value of the schema
         6387  +** cookie for DB, or to INT if the schema cookie happens to be zero.
         6388  +**
         6389  +** When INT!=0 and DB==0, set the PRNG seed to just INT.
         6390  +**
         6391  +** If INT==0 and DB==0 then use the default procedure of calling the
         6392  +** xRandomness method on the default VFS to get the PRNG seed.
         6393  +*/
         6394  +static int SQLITE_TCLAPI prng_seed(
         6395  +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
         6396  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         6397  +  int objc,              /* Number of arguments */
         6398  +  Tcl_Obj *CONST objv[]  /* Command arguments */
         6399  +){
         6400  +  int i = 0;
         6401  +  sqlite3 *db = 0;
         6402  +  if( objc!=2 && objc!=3 ){
         6403  +    Tcl_WrongNumArgs(interp, 1, objv, "SEED ?DB?");
         6404  +    return TCL_ERROR;
         6405  +  }
         6406  +  if( Tcl_GetIntFromObj(interp,objv[0],&i) ) return TCL_ERROR;
         6407  +  if( objc==3 && getDbPointer(interp, Tcl_GetString(objv[2]), &db) ){
         6408  +    return TCL_ERROR;
         6409  +  }
         6410  +  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, i, db);
  6377   6411     return TCL_OK;
  6378   6412   }
  6379   6413   
  6380   6414   /*
  6381   6415   ** tclcmd:  database_may_be_corrupt
  6382   6416   **
  6383   6417   ** Indicate that database files might be corrupt. In other words, set the normal
................................................................................
  7135   7169       { "groupby-order",       SQLITE_GroupByOrder   },
  7136   7170       { "factor-constants",    SQLITE_FactorOutConst },
  7137   7171       { "distinct-opt",        SQLITE_DistinctOpt    },
  7138   7172       { "cover-idx-scan",      SQLITE_CoverIdxScan   },
  7139   7173       { "order-by-idx-join",   SQLITE_OrderByIdxJoin },
  7140   7174       { "transitive",          SQLITE_Transitive     },
  7141   7175       { "omit-noop-join",      SQLITE_OmitNoopJoin   },
  7142         -    { "stat3",               SQLITE_Stat34         },
  7143         -    { "stat4",               SQLITE_Stat34         },
         7176  +    { "stat4",               SQLITE_Stat4          },
  7144   7177       { "skip-scan",           SQLITE_SkipScan       },
  7145   7178     };
  7146   7179   
  7147   7180     if( objc!=4 ){
  7148   7181       Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
  7149   7182       return TCL_ERROR;
  7150   7183     }
................................................................................
  7919   7952        { "sqlite3_extended_result_codes", test_extended_result_codes, 0},
  7920   7953        { "sqlite3_limit",                 test_limit,                 0},
  7921   7954        { "dbconfig_maindbname_icecube",   test_dbconfig_maindbname_icecube },
  7922   7955   
  7923   7956        { "save_prng_state",               save_prng_state,    0 },
  7924   7957        { "restore_prng_state",            restore_prng_state, 0 },
  7925   7958        { "reset_prng_state",              reset_prng_state,   0 },
         7959  +     { "prng_seed",                     prng_seed,          0 },
  7926   7960        { "database_never_corrupt",        database_never_corrupt, 0},
  7927   7961        { "database_may_be_corrupt",       database_may_be_corrupt, 0},
  7928   7962        { "optimization_control",          optimization_control,0},
  7929   7963   #if SQLITE_OS_WIN
  7930   7964        { "lock_win32_file",               win32_file_lock,    0 },
  7931   7965        { "exists_win32_path",             win32_exists_path,  0 },
  7932   7966        { "find_win32_file",               win32_find_file,    0 },

Changes to src/test_config.c.

   581    581   #endif
   582    582   
   583    583   #ifdef SQLITE_ENABLE_STAT4
   584    584     Tcl_SetVar2(interp, "sqlite_options", "stat4", "1", TCL_GLOBAL_ONLY);
   585    585   #else
   586    586     Tcl_SetVar2(interp, "sqlite_options", "stat4", "0", TCL_GLOBAL_ONLY);
   587    587   #endif
   588         -#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
   589         -  Tcl_SetVar2(interp, "sqlite_options", "stat3", "1", TCL_GLOBAL_ONLY);
   590         -#else
   591         -  Tcl_SetVar2(interp, "sqlite_options", "stat3", "0", TCL_GLOBAL_ONLY);
   592         -#endif
   593         -
   594    588   #if defined(SQLITE_ENABLE_STMTVTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
   595    589     Tcl_SetVar2(interp, "sqlite_options", "stmtvtab", "1", TCL_GLOBAL_ONLY);
   596    590   #else
   597    591     Tcl_SetVar2(interp, "sqlite_options", "stmtvtab", "0", TCL_GLOBAL_ONLY);
   598    592   #endif
   599    593   
   600    594   #ifdef SQLITE_ENABLE_STMT_SCANSTATUS

Changes to src/treeview.c.

   172    172     pView = sqlite3TreeViewPush(pView, moreToFollow);
   173    173     if( p->pWith ){
   174    174       sqlite3TreeViewWith(pView, p->pWith, 1);
   175    175       cnt = 1;
   176    176       sqlite3TreeViewPush(pView, 1);
   177    177     }
   178    178     do{
   179         -    sqlite3TreeViewLine(pView,
   180         -      "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
   181         -      ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
   182         -      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
   183         -      p->selId, p, p->selFlags,
   184         -      (int)p->nSelectRow
   185         -    );
          179  +    if( p->selFlags & SF_WhereBegin ){
          180  +      sqlite3TreeViewLine(pView, "sqlite3WhereBegin()");
          181  +    }else{
          182  +      sqlite3TreeViewLine(pView,
          183  +        "SELECT%s%s (%u/%p) selFlags=0x%x nSelectRow=%d",
          184  +        ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
          185  +        ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
          186  +        p->selId, p, p->selFlags,
          187  +        (int)p->nSelectRow
          188  +      );
          189  +    }
   186    190       if( cnt++ ) sqlite3TreeViewPop(pView);
   187    191       if( p->pPrior ){
   188    192         n = 1000;
   189    193       }else{
   190    194         n = 0;
   191    195         if( p->pSrc && p->pSrc->nSrc ) n++;
   192    196         if( p->pWhere ) n++;
................................................................................
   195    199         if( p->pOrderBy ) n++;
   196    200         if( p->pLimit ) n++;
   197    201   #ifndef SQLITE_OMIT_WINDOWFUNC
   198    202         if( p->pWin ) n++;
   199    203         if( p->pWinDefn ) n++;
   200    204   #endif
   201    205       }
   202         -    sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
          206  +    if( p->pEList ){
          207  +      sqlite3TreeViewExprList(pView, p->pEList, n>0, "result-set");
          208  +    }
          209  +    n--;
   203    210   #ifndef SQLITE_OMIT_WINDOWFUNC
   204    211       if( p->pWin ){
   205    212         Window *pX;
   206    213         pView = sqlite3TreeViewPush(pView, (n--)>0);
   207    214         sqlite3TreeViewLine(pView, "window-functions");
   208    215         for(pX=p->pWin; pX; pX=pX->pNextWin){
   209    216           sqlite3TreeViewWinFunc(pView, pX, pX->pNextWin!=0);
................................................................................
   391    398     char zFlgs[60];
   392    399     pView = sqlite3TreeViewPush(pView, moreToFollow);
   393    400     if( pExpr==0 ){
   394    401       sqlite3TreeViewLine(pView, "nil");
   395    402       sqlite3TreeViewPop(pView);
   396    403       return;
   397    404     }
   398         -  if( pExpr->flags ){
          405  +  if( pExpr->flags || pExpr->affExpr ){
   399    406       if( ExprHasProperty(pExpr, EP_FromJoin) ){
   400         -      sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x iRJT=%d",
   401         -                       pExpr->flags, pExpr->iRightJoinTable);
          407  +      sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  fg.af=%x.%c iRJT=%d",
          408  +                       pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n',
          409  +                       pExpr->iRightJoinTable);
   402    410       }else{
   403         -      sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  flags=0x%x",pExpr->flags);
          411  +      sqlite3_snprintf(sizeof(zFlgs),zFlgs,"  fg.af=%x.%c",
          412  +                       pExpr->flags, pExpr->affExpr ? pExpr->affExpr : 'n');
   404    413       }
   405    414     }else{
   406    415       zFlgs[0] = 0;
   407    416     }
   408    417     switch( pExpr->op ){
   409    418       case TK_AGG_COLUMN: {
   410    419         sqlite3TreeViewLine(pView, "AGG{%d:%d}%s",
................................................................................
   624    633         sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
   625    634         sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
   626    635         break;
   627    636       }
   628    637   #ifndef SQLITE_OMIT_TRIGGER
   629    638       case TK_RAISE: {
   630    639         const char *zType = "unk";
   631         -      switch( pExpr->affinity ){
          640  +      switch( pExpr->affExpr ){
   632    641           case OE_Rollback:   zType = "rollback";  break;
   633    642           case OE_Abort:      zType = "abort";     break;
   634    643           case OE_Fail:       zType = "fail";      break;
   635    644           case OE_Ignore:     zType = "ignore";    break;
   636    645         }
   637    646         sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
   638    647         break;
................................................................................
   665    674     }
   666    675     if( zBinOp ){
   667    676       sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
   668    677       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
   669    678       sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
   670    679     }else if( zUniOp ){
   671    680       sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
   672         -    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          681  +   sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
   673    682     }
   674    683     sqlite3TreeViewPop(pView);
   675    684   }
   676    685   
   677    686   
   678    687   /*
   679    688   ** Generate a human-readable explanation of an expression list.

Changes to src/trigger.c.

   188    188       sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
   189    189       goto trigger_cleanup;
   190    190     }
   191    191   
   192    192     /* Check that the trigger name is not reserved and that no trigger of the
   193    193     ** specified name exists */
   194    194     zName = sqlite3NameFromToken(db, pName);
   195         -  if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
          195  +  if( zName==0 ){
          196  +    assert( db->mallocFailed );
          197  +    goto trigger_cleanup;
          198  +  }
          199  +  if( sqlite3CheckObjectName(pParse, zName, "trigger", pTab->zName) ){
   196    200       goto trigger_cleanup;
   197    201     }
   198    202     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   199    203     if( !IN_RENAME_OBJECT ){
   200    204       if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),zName) ){
   201    205         if( !noErr ){
   202    206           sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);

Changes to src/util.c.

  1494   1494     memcpy(&a, &x, 8);
  1495   1495     e = (a>>52) - 1022;
  1496   1496     return e*10;
  1497   1497   }
  1498   1498   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  1499   1499   
  1500   1500   #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  1501         -    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
         1501  +    defined(SQLITE_ENABLE_STAT4) || \
  1502   1502       defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  1503   1503   /*
  1504   1504   ** Convert a LogEst into an integer.
  1505   1505   **
  1506   1506   ** Note that this routine is only used when one or more of various
  1507   1507   ** non-standard compile-time options is enabled.
  1508   1508   */
................................................................................
  1512   1512     x /= 10;
  1513   1513     if( n>=5 ) n -= 2;
  1514   1514     else if( n>=1 ) n -= 1;
  1515   1515   #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  1516   1516       defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  1517   1517     if( x>60 ) return (u64)LARGEST_INT64;
  1518   1518   #else
  1519         -  /* If only SQLITE_ENABLE_STAT3_OR_STAT4 is on, then the largest input
         1519  +  /* If only SQLITE_ENABLE_STAT4 is on, then the largest input
  1520   1520     ** possible to this routine is 310, resulting in a maximum x of 31 */
  1521   1521     assert( x<=60 );
  1522   1522   #endif
  1523   1523     return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
  1524   1524   }
  1525   1525   #endif /* defined SCANSTAT or STAT4 or ESTIMATED_ROWS */
  1526   1526   

Changes to src/vdbe.c.

   339    339   **    always preferred, even if the affinity is REAL, because
   340    340   **    an integer representation is more space efficient on disk.
   341    341   **
   342    342   ** SQLITE_AFF_TEXT:
   343    343   **    Convert pRec to a text representation.
   344    344   **
   345    345   ** SQLITE_AFF_BLOB:
          346  +** SQLITE_AFF_NONE:
   346    347   **    No-op.  pRec is unchanged.
   347    348   */
   348    349   static void applyAffinity(
   349    350     Mem *pRec,          /* The value to apply affinity to */
   350    351     char affinity,      /* The affinity to be applied */
   351    352     u8 enc              /* Use this text encoding */
   352    353   ){
................................................................................
  2808   2809     const char *zAffinity;   /* The affinity to be applied */
  2809   2810   
  2810   2811     zAffinity = pOp->p4.z;
  2811   2812     assert( zAffinity!=0 );
  2812   2813     assert( pOp->p2>0 );
  2813   2814     assert( zAffinity[pOp->p2]==0 );
  2814   2815     pIn1 = &aMem[pOp->p1];
  2815         -  while( 1 /*edit-by-break*/ ){
         2816  +  while( 1 /*exit-by-break*/ ){
  2816   2817       assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
  2817   2818       assert( memIsValid(pIn1) );
  2818   2819       applyAffinity(pIn1, zAffinity[0], encoding);
  2819   2820       if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
  2820         -      /* When applying REAL affinity, if the result is still MEM_Int, 
  2821         -      ** indicate that REAL is actually desired */
  2822         -      pIn1->flags |= MEM_IntReal;
  2823         -      pIn1->flags &= ~MEM_Int;
         2821  +      /* When applying REAL affinity, if the result is still an MEM_Int
         2822  +      ** that will fit in 6 bytes, then change the type to MEM_IntReal
         2823  +      ** so that we keep the high-resolution integer value but know that
         2824  +      ** the type really wants to be REAL. */
         2825  +      testcase( pIn1->u.i==140737488355328LL );
         2826  +      testcase( pIn1->u.i==140737488355327LL );
         2827  +      testcase( pIn1->u.i==-140737488355328LL );
         2828  +      testcase( pIn1->u.i==-140737488355329LL );
         2829  +      if( pIn1->u.i<=140737488355327LL && pIn1->u.i>=-140737488355328LL ){
         2830  +        pIn1->flags |= MEM_IntReal;
         2831  +        pIn1->flags &= ~MEM_Int;
         2832  +      }else{
         2833  +        pIn1->u.r = (double)pIn1->u.i;
         2834  +        pIn1->flags |= MEM_Real;
         2835  +        pIn1->flags &= ~MEM_Int;
         2836  +      }
  2824   2837       }
  2825   2838       REGISTER_TRACE((int)(pIn1-aMem), pIn1);
  2826   2839       zAffinity++;
  2827   2840       if( zAffinity[0]==0 ) break;
  2828   2841       pIn1++;
  2829   2842     }
  2830   2843     break;
................................................................................
  2968   2981         }else{
  2969   2982           pRec->uTemp = 0;
  2970   2983         }
  2971   2984         nHdr++;
  2972   2985       }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
  2973   2986         /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  2974   2987         i64 i = pRec->u.i;
  2975         -      u64 u;
         2988  +      u64 uu;
  2976   2989         testcase( pRec->flags & MEM_Int );
  2977   2990         testcase( pRec->flags & MEM_IntReal );
  2978   2991         if( i<0 ){
  2979         -        u = ~i;
         2992  +        uu = ~i;
  2980   2993         }else{
  2981         -        u = i;
         2994  +        uu = i;
  2982   2995         }
  2983   2996         nHdr++;
  2984         -      testcase( u==127 );               testcase( u==128 );
  2985         -      testcase( u==32767 );             testcase( u==32768 );
  2986         -      testcase( u==8388607 );           testcase( u==8388608 );
  2987         -      testcase( u==2147483647 );        testcase( u==2147483648 );
  2988         -      testcase( u==140737488355327LL ); testcase( u==140737488355328LL );
  2989         -      if( u<=127 ){
         2997  +      testcase( uu==127 );               testcase( uu==128 );
         2998  +      testcase( uu==32767 );             testcase( uu==32768 );
         2999  +      testcase( uu==8388607 );           testcase( uu==8388608 );
         3000  +      testcase( uu==2147483647 );        testcase( uu==2147483648 );
         3001  +      testcase( uu==140737488355327LL ); testcase( uu==140737488355328LL );
         3002  +      if( uu<=127 ){
  2990   3003           if( (i&1)==i && file_format>=4 ){
  2991         -          pRec->uTemp = 8+(u32)u;
         3004  +          pRec->uTemp = 8+(u32)uu;
  2992   3005           }else{
  2993   3006             nData++;
  2994   3007             pRec->uTemp = 1;
  2995   3008           }
  2996         -      }else if( u<=32767 ){
         3009  +      }else if( uu<=32767 ){
  2997   3010           nData += 2;
  2998   3011           pRec->uTemp = 2;
  2999         -      }else if( u<=8388607 ){
         3012  +      }else if( uu<=8388607 ){
  3000   3013           nData += 3;
  3001   3014           pRec->uTemp = 3;
  3002         -      }else if( u<=2147483647 ){
         3015  +      }else if( uu<=2147483647 ){
  3003   3016           nData += 4;
  3004   3017           pRec->uTemp = 4;
  3005         -      }else if( u<=140737488355327LL ){
         3018  +      }else if( uu<=140737488355327LL ){
  3006   3019           nData += 6;
  3007   3020           pRec->uTemp = 5;
  3008   3021         }else{
  3009   3022           nData += 8;
  3010   3023           if( pRec->flags & MEM_IntReal ){
  3011   3024             /* If the value is IntReal and is going to take up 8 bytes to store
  3012   3025             ** as an integer, then we might as well make it an 8-byte floating
................................................................................
  5946   5959     {
  5947   5960       zMaster = MASTER_NAME;
  5948   5961       initData.db = db;
  5949   5962       initData.iDb = iDb;
  5950   5963       initData.pzErrMsg = &p->zErrMsg;
  5951   5964       initData.mInitFlags = 0;
  5952   5965       zSql = sqlite3MPrintf(db,
  5953         -       "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
         5966  +       "SELECT*FROM\"%w\".%s WHERE %s ORDER BY rowid",
  5954   5967          db->aDb[iDb].zDbSName, zMaster, pOp->p4.z);
  5955   5968       if( zSql==0 ){
  5956   5969         rc = SQLITE_NOMEM_BKPT;
  5957   5970       }else{
  5958   5971         assert( db->init.busy==0 );
  5959   5972         db->init.busy = 1;
  5960   5973         initData.rc = SQLITE_OK;

Changes to src/vdbe.h.

   218    218   #endif
   219    219   #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_EXPLAIN)
   220    220     void sqlite3ExplainBreakpoint(const char*,const char*);
   221    221   #else
   222    222   # define sqlite3ExplainBreakpoint(A,B) /*no-op*/
   223    223   #endif
   224    224   void sqlite3VdbeAddParseSchemaOp(Parse*,int,char*);
   225         -void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
   226         -void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
   227         -void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
   228         -void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
          225  +void sqlite3VdbeChangeOpcode(Vdbe*, int addr, u8);
          226  +void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
          227  +void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
          228  +void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
   229    229   void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
   230    230   void sqlite3VdbeJumpHere(Vdbe*, int addr);
   231    231   int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
   232    232   int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
   233    233   void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
   234    234   void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
   235    235   void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);

Changes to src/vdbeInt.h.

   482    482   void sqlite3VdbeError(Vdbe*, const char *, ...);
   483    483   void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
   484    484   void sqliteVdbePopStack(Vdbe*,int);
   485    485   int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
   486    486   int sqlite3VdbeCursorRestore(VdbeCursor*);
   487    487   u32 sqlite3VdbeSerialTypeLen(u32);
   488    488   u8 sqlite3VdbeOneByteSerialTypeLen(u8);
   489         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   490         -u32 sqlite3VdbeSerialType(Mem*, int, u32*);
   491         -#endif
   492    489   u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
   493    490   u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   494    491   void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
   495    492   
   496    493   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   497    494   int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
   498    495   int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);

Changes to src/vdbeapi.c.

   840    840   ** is requested more than once within the same run of a single prepared
   841    841   ** statement, the exact same time is returned for each invocation regardless
   842    842   ** of the amount of time that elapses between invocations.  In other words,
   843    843   ** the time returned is always the time of the first call.
   844    844   */
   845    845   sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
   846    846     int rc;
   847         -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
          847  +#ifndef SQLITE_ENABLE_STAT4
   848    848     sqlite3_int64 *piTime = &p->pVdbe->iCurrentTime;
   849    849     assert( p->pVdbe!=0 );
   850    850   #else
   851    851     sqlite3_int64 iTime = 0;
   852    852     sqlite3_int64 *piTime = p->pVdbe!=0 ? &p->pVdbe->iCurrentTime : &iTime;
   853    853   #endif
   854    854     if( *piTime==0 ){
................................................................................
   905    905   ** auxiliary data pointers that is available to all functions within a
   906    906   ** single prepared statement.  The iArg values must match.
   907    907   */
   908    908   void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   909    909     AuxData *pAuxData;
   910    910   
   911    911     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   912         -#if SQLITE_ENABLE_STAT3_OR_STAT4
          912  +#if SQLITE_ENABLE_STAT4
   913    913     if( pCtx->pVdbe==0 ) return 0;
   914    914   #else
   915    915     assert( pCtx->pVdbe!=0 );
   916    916   #endif
   917    917     for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
   918    918       if(  pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){
   919    919         return pAuxData->pAux;
................................................................................
   939    939     void *pAux, 
   940    940     void (*xDelete)(void*)
   941    941   ){
   942    942     AuxData *pAuxData;
   943    943     Vdbe *pVdbe = pCtx->pVdbe;
   944    944   
   945    945     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   946         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          946  +#ifdef SQLITE_ENABLE_STAT4
   947    947     if( pVdbe==0 ) goto failed;
   948    948   #else
   949    949     assert( pVdbe!=0 );
   950    950   #endif
   951    951   
   952    952     for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNextAux){
   953    953       if( pAuxData->iAuxArg==iArg && (pAuxData->iAuxOp==pCtx->iOp || iArg<0) ){

Changes to src/vdbeaux.c.

   975    975   #endif
   976    976   
   977    977   
   978    978   /*
   979    979   ** Change the value of the opcode, or P1, P2, P3, or P5 operands
   980    980   ** for a specific instruction.
   981    981   */
   982         -void sqlite3VdbeChangeOpcode(Vdbe *p, u32 addr, u8 iNewOpcode){
          982  +void sqlite3VdbeChangeOpcode(Vdbe *p, int addr, u8 iNewOpcode){
   983    983     sqlite3VdbeGetOp(p,addr)->opcode = iNewOpcode;
   984    984   }
   985         -void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
          985  +void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
   986    986     sqlite3VdbeGetOp(p,addr)->p1 = val;
   987    987   }
   988         -void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
          988  +void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
   989    989     sqlite3VdbeGetOp(p,addr)->p2 = val;
   990    990   }
   991         -void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
          991  +void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
   992    992     sqlite3VdbeGetOp(p,addr)->p3 = val;
   993    993   }
   994    994   void sqlite3VdbeChangeP5(Vdbe *p, u16 p5){
   995    995     assert( p->nOp>0 || p->db->mallocFailed );
   996    996     if( p->nOp>0 ) p->aOp[p->nOp-1].p5 = p5;
   997    997   }
   998    998   
................................................................................
  3431   3431   **    N>=12 and even       (N-12)/2        BLOB
  3432   3432   **    N>=13 and odd        (N-13)/2        text
  3433   3433   **
  3434   3434   ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
  3435   3435   ** of SQLite will not understand those serial types.
  3436   3436   */
  3437   3437   
  3438         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         3438  +#if 0 /* Inlined into the OP_MakeRecord opcode */
  3439   3439   /*
  3440   3440   ** Return the serial-type for the value stored in pMem.
  3441   3441   **
  3442   3442   ** This routine might convert a large MEM_IntReal value into MEM_Real.
  3443   3443   **
  3444   3444   ** 2019-07-11:  The primary user of this subroutine was the OP_MakeRecord
  3445   3445   ** opcode in the byte-code engine.  But by moving this routine in-line, we
  3446   3446   ** can omit some redundant tests and make that opcode a lot faster.  So
  3447         -** this routine is now only used by the STAT3/4 logic.
         3447  +** this routine is now only used by the STAT3 logic and STAT3 support has
         3448  +** ended.  The code is kept here for historical reference only.
  3448   3449   */
  3449   3450   u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
  3450   3451     int flags = pMem->flags;
  3451   3452     u32 n;
  3452   3453   
  3453   3454     assert( pLen!=0 );
  3454   3455     if( flags&MEM_Null ){
................................................................................
  3501   3502     n = (u32)pMem->n;
  3502   3503     if( flags & MEM_Zero ){
  3503   3504       n += pMem->u.nZero;
  3504   3505     }
  3505   3506     *pLen = n;
  3506   3507     return ((n*2) + 12 + ((flags&MEM_Str)!=0));
  3507   3508   }
  3508         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         3509  +#endif /* inlined into OP_MakeRecord */
  3509   3510   
  3510   3511   /*
  3511   3512   ** The sizes for serial types less than 128
  3512   3513   */
  3513   3514   static const u8 sqlite3SmallTypeSizes[] = {
  3514   3515           /*  0   1   2   3   4   5   6   7   8   9 */   
  3515   3516   /*   0 */   0,  1,  2,  3,  4,  6,  8,  8,  0,  0,
................................................................................
  4906   4907   **
  4907   4908   ** OP_PureFunc means that the function must be deterministic, and should
  4908   4909   ** throw an error if it is given inputs that would make it non-deterministic.
  4909   4910   ** This routine is invoked by date/time functions that use non-deterministic
  4910   4911   ** features such as 'now'.
  4911   4912   */
  4912   4913   int sqlite3NotPureFunc(sqlite3_context *pCtx){
  4913         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         4914  +#ifdef SQLITE_ENABLE_STAT4
  4914   4915     if( pCtx->pVdbe==0 ) return 1;
  4915   4916   #endif
  4916   4917     if( pCtx->pVdbe->aOp[pCtx->iOp].opcode==OP_PureFunc ){
  4917   4918       sqlite3_result_error(pCtx, 
  4918   4919          "non-deterministic function in index expression or CHECK constraint",
  4919   4920          -1);
  4920   4921       return 0;

Changes to src/vdbemem.c.

  1299   1299   ** Otherwise, if the second argument is non-zero, then this function is 
  1300   1300   ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
  1301   1301   ** already been allocated, allocate the UnpackedRecord structure that 
  1302   1302   ** that function will return to its caller here. Then return a pointer to
  1303   1303   ** an sqlite3_value within the UnpackedRecord.a[] array.
  1304   1304   */
  1305   1305   static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
  1306         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1306  +#ifdef SQLITE_ENABLE_STAT4
  1307   1307     if( p ){
  1308   1308       UnpackedRecord *pRec = p->ppRec[0];
  1309   1309   
  1310   1310       if( pRec==0 ){
  1311   1311         Index *pIdx = p->pIdx;      /* Index being probed */
  1312   1312         int nByte;                  /* Bytes of space to allocate */
  1313   1313         int i;                      /* Counter variable */
................................................................................
  1335   1335       }
  1336   1336     
  1337   1337       pRec->nField = p->iVal+1;
  1338   1338       return &pRec->aMem[p->iVal];
  1339   1339     }
  1340   1340   #else
  1341   1341     UNUSED_PARAMETER(p);
  1342         -#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
         1342  +#endif /* defined(SQLITE_ENABLE_STAT4) */
  1343   1343     return sqlite3ValueNew(db);
  1344   1344   }
  1345   1345   
  1346   1346   /*
  1347   1347   ** The expression object indicated by the second argument is guaranteed
  1348   1348   ** to be a scalar SQL function. If
  1349   1349   **
................................................................................
  1359   1359   ** If the result is a text value, the sqlite3_value object uses encoding 
  1360   1360   ** enc.
  1361   1361   **
  1362   1362   ** If the conditions above are not met, this function returns SQLITE_OK
  1363   1363   ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
  1364   1364   ** NULL and an SQLite error code returned.
  1365   1365   */
  1366         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1366  +#ifdef SQLITE_ENABLE_STAT4
  1367   1367   static int valueFromFunction(
  1368   1368     sqlite3 *db,                    /* The database connection */
  1369   1369     Expr *p,                        /* The expression to evaluate */
  1370   1370     u8 enc,                         /* Encoding to use */
  1371   1371     u8 aff,                         /* Affinity to use */
  1372   1372     sqlite3_value **ppVal,          /* Write the new value here */
  1373   1373     struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
................................................................................
  1442   1442     }
  1443   1443   
  1444   1444     *ppVal = pVal;
  1445   1445     return rc;
  1446   1446   }
  1447   1447   #else
  1448   1448   # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
  1449         -#endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
         1449  +#endif /* defined(SQLITE_ENABLE_STAT4) */
  1450   1450   
  1451   1451   /*
  1452   1452   ** Extract a value from the supplied expression in the manner described
  1453   1453   ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
  1454   1454   ** using valueNew().
  1455   1455   **
  1456   1456   ** If pCtx is NULL and an error occurs after the sqlite3_value object
................................................................................
  1471   1471     sqlite3_value *pVal = 0;
  1472   1472     int negInt = 1;
  1473   1473     const char *zNeg = "";
  1474   1474     int rc = SQLITE_OK;
  1475   1475   
  1476   1476     assert( pExpr!=0 );
  1477   1477     while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
  1478         -#if defined(SQLITE_ENABLE_STAT3_OR_STAT4)
         1478  +#if defined(SQLITE_ENABLE_STAT4)
  1479   1479     if( op==TK_REGISTER ) op = pExpr->op2;
  1480   1480   #else
  1481   1481     if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
  1482   1482   #endif
  1483   1483   
  1484   1484     /* Compressed expressions only appear when parsing the DEFAULT clause
  1485   1485     ** on a table column definition, and hence only when pCtx==0.  This
................................................................................
  1564   1564       zVal = &pExpr->u.zToken[2];
  1565   1565       nVal = sqlite3Strlen30(zVal)-1;
  1566   1566       assert( zVal[nVal]=='\'' );
  1567   1567       sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
  1568   1568                            0, SQLITE_DYNAMIC);
  1569   1569     }
  1570   1570   #endif
  1571         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1571  +#ifdef SQLITE_ENABLE_STAT4
  1572   1572     else if( op==TK_FUNCTION && pCtx!=0 ){
  1573   1573       rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
  1574   1574     }
  1575   1575   #endif
  1576   1576     else if( op==TK_TRUEFALSE ){
  1577   1577       pVal = valueNew(db, pCtx);
  1578   1578       if( pVal ){
................................................................................
  1581   1581       }
  1582   1582     }
  1583   1583   
  1584   1584     *ppVal = pVal;
  1585   1585     return rc;
  1586   1586   
  1587   1587   no_mem:
  1588         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1588  +#ifdef SQLITE_ENABLE_STAT4
  1589   1589     if( pCtx==0 || pCtx->pParse->nErr==0 )
  1590   1590   #endif
  1591   1591       sqlite3OomFault(db);
  1592   1592     sqlite3DbFree(db, zVal);
  1593   1593     assert( *ppVal==0 );
  1594         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1594  +#ifdef SQLITE_ENABLE_STAT4
  1595   1595     if( pCtx==0 ) sqlite3ValueFree(pVal);
  1596   1596   #else
  1597   1597     assert( pCtx==0 ); sqlite3ValueFree(pVal);
  1598   1598   #endif
  1599   1599     return SQLITE_NOMEM_BKPT;
  1600   1600   }
  1601   1601   
................................................................................
  1615   1615     u8 enc,                   /* Encoding to use */
  1616   1616     u8 affinity,              /* Affinity to use */
  1617   1617     sqlite3_value **ppVal     /* Write the new value here */
  1618   1618   ){
  1619   1619     return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
  1620   1620   }
  1621   1621   
  1622         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1623         -/*
  1624         -** The implementation of the sqlite_record() function. This function accepts
  1625         -** a single argument of any type. The return value is a formatted database 
  1626         -** record (a blob) containing the argument value.
  1627         -**
  1628         -** This is used to convert the value stored in the 'sample' column of the
  1629         -** sqlite_stat3 table to the record format SQLite uses internally.
  1630         -*/
  1631         -static void recordFunc(
  1632         -  sqlite3_context *context,
  1633         -  int argc,
  1634         -  sqlite3_value **argv
  1635         -){
  1636         -  const int file_format = 1;
  1637         -  u32 iSerial;                    /* Serial type */
  1638         -  int nSerial;                    /* Bytes of space for iSerial as varint */
  1639         -  u32 nVal;                       /* Bytes of space required for argv[0] */
  1640         -  int nRet;
  1641         -  sqlite3 *db;
  1642         -  u8 *aRet;
  1643         -
  1644         -  UNUSED_PARAMETER( argc );
  1645         -  iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
  1646         -  nSerial = sqlite3VarintLen(iSerial);
  1647         -  db = sqlite3_context_db_handle(context);
  1648         -
  1649         -  nRet = 1 + nSerial + nVal;
  1650         -  aRet = sqlite3DbMallocRawNN(db, nRet);
  1651         -  if( aRet==0 ){
  1652         -    sqlite3_result_error_nomem(context);
  1653         -  }else{
  1654         -    aRet[0] = nSerial+1;
  1655         -    putVarint32(&aRet[1], iSerial);
  1656         -    sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
  1657         -    sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
  1658         -    sqlite3DbFreeNN(db, aRet);
  1659         -  }
  1660         -}
  1661         -
  1662         -/*
  1663         -** Register built-in functions used to help read ANALYZE data.
  1664         -*/
  1665         -void sqlite3AnalyzeFunctions(void){
  1666         -  static FuncDef aAnalyzeTableFuncs[] = {
  1667         -    FUNCTION(sqlite_record,   1, 0, 0, recordFunc),
  1668         -  };
  1669         -  sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs));
  1670         -}
  1671         -
         1622  +#ifdef SQLITE_ENABLE_STAT4
  1672   1623   /*
  1673   1624   ** Attempt to extract a value from pExpr and use it to construct *ppVal.
  1674   1625   **
  1675   1626   ** If pAlloc is not NULL, then an UnpackedRecord object is created for
  1676   1627   ** pAlloc if one does not exist and the new value is added to the
  1677   1628   ** UnpackedRecord object.
  1678   1629   **

Changes to src/where.c.

  1073   1073     }
  1074   1074     sqlite3_free(pVtab->zErrMsg);
  1075   1075     pVtab->zErrMsg = 0;
  1076   1076     return rc;
  1077   1077   }
  1078   1078   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
  1079   1079   
  1080         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1080  +#ifdef SQLITE_ENABLE_STAT4
  1081   1081   /*
  1082   1082   ** Estimate the location of a particular key among all keys in an
  1083   1083   ** index.  Store the results in aStat as follows:
  1084   1084   **
  1085   1085   **    aStat[0]      Est. number of rows less than pRec
  1086   1086   **    aStat[1]      Est. number of rows equal to pRec
  1087   1087   **
................................................................................
  1266   1266       aStat[1] = pIdx->aAvgEq[nField-1];
  1267   1267     }
  1268   1268   
  1269   1269     /* Restore the pRec->nField value before returning.  */
  1270   1270     pRec->nField = nField;
  1271   1271     return i;
  1272   1272   }
  1273         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1273  +#endif /* SQLITE_ENABLE_STAT4 */
  1274   1274   
  1275   1275   /*
  1276   1276   ** If it is not NULL, pTerm is a term that provides an upper or lower
  1277   1277   ** bound on a range scan. Without considering pTerm, it is estimated 
  1278   1278   ** that the scan will visit nNew rows. This function returns the number
  1279   1279   ** estimated to be visited after taking pTerm into account.
  1280   1280   **
................................................................................
  1292   1292         nRet -= 20;        assert( 20==sqlite3LogEst(4) );
  1293   1293       }
  1294   1294     }
  1295   1295     return nRet;
  1296   1296   }
  1297   1297   
  1298   1298   
  1299         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1299  +#ifdef SQLITE_ENABLE_STAT4
  1300   1300   /*
  1301   1301   ** Return the affinity for a single column of an index.
  1302   1302   */
  1303   1303   char sqlite3IndexColumnAffinity(sqlite3 *db, Index *pIdx, int iCol){
  1304   1304     assert( iCol>=0 && iCol<pIdx->nColumn );
  1305   1305     if( !pIdx->zColAff ){
  1306   1306       if( sqlite3IndexAffinityStr(db, pIdx)==0 ) return SQLITE_AFF_BLOB;
  1307   1307     }
         1308  +  assert( pIdx->zColAff[iCol]!=0 );
  1308   1309     return pIdx->zColAff[iCol];
  1309   1310   }
  1310   1311   #endif
  1311   1312   
  1312   1313   
  1313         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1314  +#ifdef SQLITE_ENABLE_STAT4
  1314   1315   /* 
  1315   1316   ** This function is called to estimate the number of rows visited by a
  1316   1317   ** range-scan on a skip-scan index. For example:
  1317   1318   **
  1318   1319   **   CREATE INDEX i1 ON t1(a, b, c);
  1319   1320   **   SELECT * FROM t1 WHERE a=? AND c BETWEEN ? AND ?;
  1320   1321   **
................................................................................
  1412   1413   
  1413   1414     sqlite3ValueFree(p1);
  1414   1415     sqlite3ValueFree(p2);
  1415   1416     sqlite3ValueFree(pVal);
  1416   1417   
  1417   1418     return rc;
  1418   1419   }
  1419         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1420  +#endif /* SQLITE_ENABLE_STAT4 */
  1420   1421   
  1421   1422   /*
  1422   1423   ** This function is used to estimate the number of rows that will be visited
  1423   1424   ** by scanning an index for a range of values. The range may have an upper
  1424   1425   ** bound, a lower bound, or both. The WHERE clause terms that set the upper
  1425   1426   ** and lower bounds are represented by pLower and pUpper respectively. For
  1426   1427   ** example, assuming that index p is on t1(a):
................................................................................
  1465   1466     WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
  1466   1467     WhereLoop *pLoop     /* Modify the .nOut and maybe .rRun fields */
  1467   1468   ){
  1468   1469     int rc = SQLITE_OK;
  1469   1470     int nOut = pLoop->nOut;
  1470   1471     LogEst nNew;
  1471   1472   
  1472         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1473  +#ifdef SQLITE_ENABLE_STAT4
  1473   1474     Index *p = pLoop->u.btree.pIndex;
  1474   1475     int nEq = pLoop->u.btree.nEq;
  1475   1476   
  1476         -  if( p->nSample>0 && nEq<p->nSampleCol
  1477         -   && OptimizationEnabled(pParse->db, SQLITE_Stat34)
         1477  +  if( p->nSample>0 && ALWAYS(nEq<p->nSampleCol)
         1478  +   && OptimizationEnabled(pParse->db, SQLITE_Stat4)
  1478   1479     ){
  1479   1480       if( nEq==pBuilder->nRecValid ){
  1480   1481         UnpackedRecord *pRec = pBuilder->pRec;
  1481   1482         tRowcnt a[2];
  1482   1483         int nBtm = pLoop->u.btree.nBtm;
  1483   1484         int nTop = pLoop->u.btree.nTop;
  1484   1485   
................................................................................
  1568   1569         pBuilder->pRec = pRec;
  1569   1570         if( rc==SQLITE_OK ){
  1570   1571           if( iUpper>iLower ){
  1571   1572             nNew = sqlite3LogEst(iUpper - iLower);
  1572   1573             /* TUNING:  If both iUpper and iLower are derived from the same
  1573   1574             ** sample, then assume they are 4x more selective.  This brings
  1574   1575             ** the estimated selectivity more in line with what it would be
  1575         -          ** if estimated without the use of STAT3/4 tables. */
         1576  +          ** if estimated without the use of STAT4 tables. */
  1576   1577             if( iLwrIdx==iUprIdx ) nNew -= 20;  assert( 20==sqlite3LogEst(4) );
  1577   1578           }else{
  1578   1579             nNew = 10;        assert( 10==sqlite3LogEst(2) );
  1579   1580           }
  1580   1581           if( nNew<nOut ){
  1581   1582             nOut = nNew;
  1582   1583           }
................................................................................
  1617   1618                       pLoop->nOut, nOut));
  1618   1619     }
  1619   1620   #endif
  1620   1621     pLoop->nOut = (LogEst)nOut;
  1621   1622     return rc;
  1622   1623   }
  1623   1624   
  1624         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1625  +#ifdef SQLITE_ENABLE_STAT4
  1625   1626   /*
  1626   1627   ** Estimate the number of rows that will be returned based on
  1627   1628   ** an equality constraint x=VALUE and where that VALUE occurs in
  1628   1629   ** the histogram data.  This only works when x is the left-most
  1629         -** column of an index and sqlite_stat3 histogram data is available
         1630  +** column of an index and sqlite_stat4 histogram data is available
  1630   1631   ** for that index.  When pExpr==NULL that means the constraint is
  1631   1632   ** "x IS NULL" instead of "x=VALUE".
  1632   1633   **
  1633   1634   ** Write the estimated row count into *pnRow and return SQLITE_OK. 
  1634   1635   ** If unable to make an estimate, leave *pnRow unchanged and return
  1635   1636   ** non-zero.
  1636   1637   **
................................................................................
  1680   1681     whereKeyStats(pParse, p, pRec, 0, a);
  1681   1682     WHERETRACE(0x10,("equality scan regions %s(%d): %d\n",
  1682   1683                      p->zName, nEq-1, (int)a[1]));
  1683   1684     *pnRow = a[1];
  1684   1685     
  1685   1686     return rc;
  1686   1687   }
  1687         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1688  +#endif /* SQLITE_ENABLE_STAT4 */
  1688   1689   
  1689         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1690  +#ifdef SQLITE_ENABLE_STAT4
  1690   1691   /*
  1691   1692   ** Estimate the number of rows that will be returned based on
  1692   1693   ** an IN constraint where the right-hand side of the IN operator
  1693   1694   ** is a list of values.  Example:
  1694   1695   **
  1695   1696   **        WHERE x IN (1,2,3,4)
  1696   1697   **
................................................................................
  1729   1730       if( nRowEst > nRow0 ) nRowEst = nRow0;
  1730   1731       *pnRow = nRowEst;
  1731   1732       WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
  1732   1733     }
  1733   1734     assert( pBuilder->nRecValid==nRecValid );
  1734   1735     return rc;
  1735   1736   }
  1736         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1737  +#endif /* SQLITE_ENABLE_STAT4 */
  1737   1738   
  1738   1739   
  1739   1740   #ifdef WHERETRACE_ENABLED
  1740   1741   /*
  1741   1742   ** Print the content of a WhereTerm object
  1742   1743   */
  1743   1744   static void whereTermPrint(WhereTerm *pTerm, int iTerm){
................................................................................
  2450   2451     rSize = pProbe->aiRowLogEst[0];
  2451   2452     rLogSize = estLog(rSize);
  2452   2453     for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
  2453   2454       u16 eOp = pTerm->eOperator;   /* Shorthand for pTerm->eOperator */
  2454   2455       LogEst rCostIdx;
  2455   2456       LogEst nOutUnadjusted;        /* nOut before IN() and WHERE adjustments */
  2456   2457       int nIn = 0;
  2457         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         2458  +#ifdef SQLITE_ENABLE_STAT4
  2458   2459       int nRecValid = pBuilder->nRecValid;
  2459   2460   #endif
  2460   2461       if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
  2461   2462        && indexColumnNotNull(pProbe, saved_nEq)
  2462   2463       ){
  2463   2464         continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
  2464   2465       }
................................................................................
  2608   2609       /* At this point pNew->nOut is set to the number of rows expected to
  2609   2610       ** be visited by the index scan before considering term pTerm, or the
  2610   2611       ** values of nIn and nInMul. In other words, assuming that all 
  2611   2612       ** "x IN(...)" terms are replaced with "x = ?". This block updates
  2612   2613       ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul).  */
  2613   2614       assert( pNew->nOut==saved_nOut );
  2614   2615       if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
  2615         -      /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
         2616  +      /* Adjust nOut using stat4 data. Or, if there is no stat4
  2616   2617         ** data, using some other estimate.  */
  2617   2618         whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
  2618   2619       }else{
  2619   2620         int nEq = ++pNew->u.btree.nEq;
  2620   2621         assert( eOp & (WO_ISNULL|WO_EQ|WO_IN|WO_IS) );
  2621   2622   
  2622   2623         assert( pNew->nOut==saved_nOut );
  2623   2624         if( pTerm->truthProb<=0 && pProbe->aiColumn[saved_nEq]>=0 ){
  2624   2625           assert( (eOp & WO_IN) || nIn==0 );
  2625   2626           testcase( eOp & WO_IN );
  2626   2627           pNew->nOut += pTerm->truthProb;
  2627   2628           pNew->nOut -= nIn;
  2628   2629         }else{
  2629         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         2630  +#ifdef SQLITE_ENABLE_STAT4
  2630   2631           tRowcnt nOut = 0;
  2631   2632           if( nInMul==0 
  2632   2633            && pProbe->nSample 
  2633   2634            && pNew->u.btree.nEq<=pProbe->nSampleCol
  2634   2635            && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
  2635         -         && OptimizationEnabled(db, SQLITE_Stat34)
         2636  +         && OptimizationEnabled(db, SQLITE_Stat4)
  2636   2637           ){
  2637   2638             Expr *pExpr = pTerm->pExpr;
  2638   2639             if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
  2639   2640               testcase( eOp & WO_EQ );
  2640   2641               testcase( eOp & WO_IS );
  2641   2642               testcase( eOp & WO_ISNULL );
  2642   2643               rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
................................................................................
  2690   2691   
  2691   2692       if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
  2692   2693        && pNew->u.btree.nEq<pProbe->nColumn
  2693   2694       ){
  2694   2695         whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
  2695   2696       }
  2696   2697       pNew->nOut = saved_nOut;
  2697         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         2698  +#ifdef SQLITE_ENABLE_STAT4
  2698   2699       pBuilder->nRecValid = nRecValid;
  2699   2700   #endif
  2700   2701     }
  2701   2702     pNew->prereq = saved_prereq;
  2702   2703     pNew->u.btree.nEq = saved_nEq;
  2703   2704     pNew->u.btree.nBtm = saved_nBtm;
  2704   2705     pNew->u.btree.nTop = saved_nTop;
................................................................................
  3063   3064       if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
  3064   3065         /* If a non-unique index is used, or if a prefix of the key for
  3065   3066         ** unique index is used (making the index functionally non-unique)
  3066   3067         ** then the sqlite_stat1 data becomes important for scoring the
  3067   3068         ** plan */
  3068   3069         pTab->tabFlags |= TF_StatsUsed;
  3069   3070       }
  3070         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         3071  +#ifdef SQLITE_ENABLE_STAT4
  3071   3072       sqlite3Stat4ProbeFree(pBuilder->pRec);
  3072   3073       pBuilder->nRecValid = 0;
  3073   3074       pBuilder->pRec = 0;
  3074   3075   #endif
  3075   3076     }
  3076   3077     return rc;
  3077   3078   }
................................................................................
  3691   3692         if( wctrlFlags & WHERE_ORDERBY_LIMIT ) continue;
  3692   3693       }else{
  3693   3694         pLoop = pLast;
  3694   3695       }
  3695   3696       if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
  3696   3697         if( pLoop->u.vtab.isOrdered ) obSat = obDone;
  3697   3698         break;
  3698         -    }else{
  3699         -      pLoop->u.btree.nIdxCol = 0;
         3699  +    }else if( wctrlFlags & WHERE_DISTINCTBY ){
         3700  +      pLoop->u.btree.nDistinctCol = 0;
  3700   3701       }
  3701   3702       iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
  3702   3703   
  3703   3704       /* Mark off any ORDER BY term X that is a column in the table of
  3704   3705       ** the current loop for which there is term in the WHERE
  3705   3706       ** clause of the form X IS NULL or X=? that reference only outer
  3706   3707       ** loops.
................................................................................
  3740   3741           return 0;
  3741   3742         }else{
  3742   3743           nKeyCol = pIndex->nKeyCol;
  3743   3744           nColumn = pIndex->nColumn;
  3744   3745           assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
  3745   3746           assert( pIndex->aiColumn[nColumn-1]==XN_ROWID
  3746   3747                             || !HasRowid(pIndex->pTable));
  3747         -        isOrderDistinct = IsUniqueIndex(pIndex);
         3748  +        isOrderDistinct = IsUniqueIndex(pIndex)
         3749  +                          && (pLoop->wsFlags & WHERE_SKIPSCAN)==0;
  3748   3750         }
  3749   3751   
  3750   3752         /* Loop through all columns of the index and deal with the ones
  3751   3753         ** that are not constrained by == or IN.
  3752   3754         */
  3753   3755         rev = revSet = 0;
  3754   3756         distinctColumns = 0;
................................................................................
  3834   3836                 continue;
  3835   3837               }
  3836   3838             }
  3837   3839             if( iColumn!=XN_ROWID ){
  3838   3840               pColl = sqlite3ExprNNCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
  3839   3841               if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
  3840   3842             }
  3841         -          pLoop->u.btree.nIdxCol = j+1;
         3843  +          if( wctrlFlags & WHERE_DISTINCTBY ){
         3844  +            pLoop->u.btree.nDistinctCol = j+1;
         3845  +          }
  3842   3846             isMatch = 1;
  3843   3847             break;
  3844   3848           }
  3845   3849           if( isMatch && (wctrlFlags & WHERE_GROUPBY)==0 ){
  3846   3850             /* Make sure the sort order is compatible in an ORDER BY clause.
  3847   3851             ** Sort order is irrelevant for a GROUP BY clause. */
  3848   3852             if( revSet ){
................................................................................
  3855   3859           }
  3856   3860           if( isMatch ){
  3857   3861             if( iColumn==XN_ROWID ){
  3858   3862               testcase( distinctColumns==0 );
  3859   3863               distinctColumns = 1;
  3860   3864             }
  3861   3865             obSat |= MASKBIT(i);
         3866  +          if( (wctrlFlags & WHERE_ORDERBY_MIN) && j==pLoop->u.btree.nEq ){
         3867  +            pLoop->wsFlags |= WHERE_MIN_ORDERED;
         3868  +          }
  3862   3869           }else{
  3863   3870             /* No match found */
  3864   3871             if( j==0 || j<nKeyCol ){
  3865   3872               testcase( isOrderDistinct!=0 );
  3866   3873               isOrderDistinct = 0;
  3867   3874             }
  3868   3875             break;
................................................................................
  4762   4769   #if defined(WHERETRACE_ENABLED)
  4763   4770     if( sqlite3WhereTrace & 0xffff ){
  4764   4771       sqlite3DebugPrintf("*** Optimizer Start *** (wctrlFlags: 0x%x",wctrlFlags);
  4765   4772       if( wctrlFlags & WHERE_USE_LIMIT ){
  4766   4773         sqlite3DebugPrintf(", limit: %d", iAuxArg);
  4767   4774       }
  4768   4775       sqlite3DebugPrintf(")\n");
         4776  +    if( sqlite3WhereTrace & 0x100 ){
         4777  +      Select sSelect;
         4778  +      memset(&sSelect, 0, sizeof(sSelect));
         4779  +      sSelect.selFlags = SF_WhereBegin;
         4780  +      sSelect.pSrc = pTabList;
         4781  +      sSelect.pWhere = pWhere;
         4782  +      sSelect.pOrderBy = pOrderBy;
         4783  +      sSelect.pEList = pResultSet;
         4784  +      sqlite3TreeViewSelect(0, &sSelect, 0);
         4785  +    }
  4769   4786     }
  4770   4787     if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
  4771   4788       sqlite3WhereClausePrint(sWLB.pWC);
  4772   4789     }
  4773   4790   #endif
  4774   4791   
  4775   4792     if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
................................................................................
  5155   5172         int addrSeek = 0;
  5156   5173         Index *pIdx;
  5157   5174         int n;
  5158   5175         if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
  5159   5176          && i==pWInfo->nLevel-1  /* Ticket [ef9318757b152e3] 2017-10-21 */
  5160   5177          && (pLoop->wsFlags & WHERE_INDEXED)!=0
  5161   5178          && (pIdx = pLoop->u.btree.pIndex)->hasStat1
  5162         -       && (n = pLoop->u.btree.nIdxCol)>0
         5179  +       && (n = pLoop->u.btree.nDistinctCol)>0
  5163   5180          && pIdx->aiRowLogEst[n]>=36
  5164   5181         ){
  5165   5182           int r1 = pParse->nMem+1;
  5166   5183           int j, op;
  5167   5184           for(j=0; j<n; j++){
  5168   5185             sqlite3VdbeAddOp3(v, OP_Column, pLevel->iIdxCur, j, r1+j);
  5169   5186           }

Changes to src/whereInt.h.

   124    124     LogEst rRun;          /* Cost of running each loop */
   125    125     LogEst nOut;          /* Estimated number of output rows */
   126    126     union {
   127    127       struct {               /* Information for internal btree tables */
   128    128         u16 nEq;               /* Number of equality constraints */
   129    129         u16 nBtm;              /* Size of BTM vector */
   130    130         u16 nTop;              /* Size of TOP vector */
   131         -      u16 nIdxCol;           /* Index column used for ORDER BY */
          131  +      u16 nDistinctCol;      /* Index columns used to sort for DISTINCT */
   132    132         Index *pIndex;         /* Index used, or NULL */
   133    133       } btree;
   134    134       struct {               /* Information for virtual tables */
   135    135         int idxNum;            /* Index number */
   136    136         u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
   137    137         i8 isOrdered;          /* True if satisfies ORDER BY */
   138    138         u16 omitMask;          /* Terms that may be omitted */
................................................................................
   275    275   #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
   276    276   #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
   277    277   #define TERM_CODED      0x04   /* This term is already coded */
   278    278   #define TERM_COPIED     0x08   /* Has a child */
   279    279   #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
   280    280   #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
   281    281   #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
   282         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          282  +#ifdef SQLITE_ENABLE_STAT4
   283    283   #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
   284    284   #else
   285         -#  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
          285  +#  define TERM_VNULL    0x00   /* Disabled if not using stat4 */
   286    286   #endif
   287    287   #define TERM_LIKEOPT    0x100  /* Virtual terms from the LIKE optimization */
   288    288   #define TERM_LIKECOND   0x200  /* Conditionally this LIKE operator term */
   289    289   #define TERM_LIKE       0x400  /* The original LIKE operator */
   290    290   #define TERM_IS         0x800  /* Term.pExpr is an IS operator */
   291    291   #define TERM_VARSELECT  0x1000 /* Term.pExpr contains a correlated sub-query */
   292    292   
................................................................................
   395    395   */
   396    396   struct WhereLoopBuilder {
   397    397     WhereInfo *pWInfo;        /* Information about this WHERE */
   398    398     WhereClause *pWC;         /* WHERE clause terms */
   399    399     ExprList *pOrderBy;       /* ORDER BY clause */
   400    400     WhereLoop *pNew;          /* Template WhereLoop */
   401    401     WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
   402         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          402  +#ifdef SQLITE_ENABLE_STAT4
   403    403     UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
   404    404     int nRecValid;            /* Number of valid fields currently in pRec */
   405    405   #endif
   406    406     unsigned int bldFlags;    /* SQLITE_BLDF_* flags */
   407    407     unsigned int iPlanLimit;  /* Search limiter */
   408    408   };
   409    409   
................................................................................
   582    582   #define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
   583    583   #define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
   584    584   #define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
   585    585   #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
   586    586   #define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
   587    587   #define WHERE_PARTIALIDX   0x00020000  /* The automatic index is partial */
   588    588   #define WHERE_IN_EARLYOUT  0x00040000  /* Perhaps quit IN loops early */
          589  +#define WHERE_MIN_ORDERED  0x00080000  /* Column nEq of index is min() expr */
   589    590   
   590    591   #endif /* !defined(SQLITE_WHEREINT_H) */

Changes to src/wherecode.c.

   314    314     }
   315    315   }
   316    316   
   317    317   /*
   318    318   ** Code an OP_Affinity opcode to apply the column affinity string zAff
   319    319   ** to the n registers starting at base. 
   320    320   **
   321         -** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
   322         -** beginning and end of zAff are ignored.  If all entries in zAff are
   323         -** SQLITE_AFF_BLOB, then no code gets generated.
          321  +** As an optimization, SQLITE_AFF_BLOB and SQLITE_AFF_NONE entries (which
          322  +** are no-ops) at the beginning and end of zAff are ignored.  If all entries
          323  +** in zAff are SQLITE_AFF_BLOB or SQLITE_AFF_NONE, then no code gets generated.
   324    324   **
   325    325   ** This routine makes its own copy of zAff so that the caller is free
   326    326   ** to modify zAff after this routine returns.
   327    327   */
   328    328   static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
   329    329     Vdbe *v = pParse->pVdbe;
   330    330     if( zAff==0 ){
   331    331       assert( pParse->db->mallocFailed );
   332    332       return;
   333    333     }
   334    334     assert( v!=0 );
   335    335   
   336         -  /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
   337         -  ** and end of the affinity string.
          336  +  /* Adjust base and n to skip over SQLITE_AFF_BLOB and SQLITE_AFF_NONE
          337  +  ** entries at the beginning and end of the affinity string.
   338    338     */
   339         -  while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
          339  +  assert( SQLITE_AFF_NONE<SQLITE_AFF_BLOB );
          340  +  while( n>0 && zAff[0]<=SQLITE_AFF_BLOB ){
   340    341       n--;
   341    342       base++;
   342    343       zAff++;
   343    344     }
   344         -  while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
          345  +  while( n>1 && zAff[n-1]<=SQLITE_AFF_BLOB ){
   345    346       n--;
   346    347     }
   347    348   
   348    349     /* Code the OP_Affinity opcode if there is anything left to do. */
   349    350     if( n>0 ){
   350    351       sqlite3VdbeAddOp4(v, OP_Affinity, base, n, 0, zAff, n);
   351    352     }
................................................................................
  1558   1559       ** was passed to this function to implement a "SELECT min(x) ..." 
  1559   1560       ** query, then the caller will only allow the loop to run for
  1560   1561       ** a single iteration. This means that the first row returned
  1561   1562       ** should not have a NULL value stored in 'x'. If column 'x' is
  1562   1563       ** the first one after the nEq equality constraints in the index,
  1563   1564       ** this requires some special handling.
  1564   1565       */
  1565         -    assert( pWInfo->pOrderBy==0
  1566         -         || pWInfo->pOrderBy->nExpr==1
         1566  +    assert( (pWInfo->pOrderBy!=0 && pWInfo->pOrderBy->nExpr==1)
  1567   1567            || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
  1568         -    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
  1569         -     && pWInfo->nOBSat>0
  1570         -     && (pIdx->nKeyCol>nEq)
  1571         -    ){
         1568  +    if( pLoop->wsFlags & WHERE_MIN_ORDERED ){
         1569  +      assert( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN) );
         1570  +      assert( pWInfo->nOBSat );
         1571  +      assert( pIdx->nColumn>nEq );
  1572   1572         assert( pLoop->nSkip==0 );
  1573   1573         bSeekPastNull = 1;
  1574   1574         nExtraReg = 1;
  1575   1575       }
  1576   1576   
  1577   1577       /* Find any inequality constraint terms for the start and end 
  1578   1578       ** of the range. 
................................................................................
  1690   1690         VdbeCoverage(v);
  1691   1691         VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
  1692   1692         VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
  1693   1693         VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
  1694   1694         VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
  1695   1695         VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
  1696   1696         VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
         1697  +
         1698  +      if( bSeekPastNull && (pLoop->wsFlags & WHERE_TOP_LIMIT)==0 ){
         1699  +        /* If bSeekPastNull is set only to skip past the NULL values for
         1700  +        ** a query like "SELECT min(a), b FROM t1", then add code so that
         1701  +        ** if there are no rows with (a IS NOT NULL), then do the seek 
         1702  +        ** without jumping past NULLs instead. This allows the code in 
         1703  +        ** select.c to pick a value for "b" in the above query.  */
         1704  +        assert( startEq==0 && (op==OP_SeekGT || op==OP_SeekLT) );
         1705  +        assert( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 && pWInfo->nOBSat>0 );
         1706  +        sqlite3VdbeChangeP2(v, -1, sqlite3VdbeCurrentAddr(v)+1);
         1707  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2);
         1708  +
         1709  +        op = aStartOp[(start_constraints<<2) + (1<<1) + bRev];
         1710  +        assert( op==OP_SeekGE || op==OP_SeekLE );
         1711  +        sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
         1712  +        VdbeCoverage(v);
         1713  +        VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
         1714  +        VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
         1715  +      }
  1697   1716       }
  1698   1717   
  1699   1718       /* Load the value for the inequality constraint at the end of the
  1700   1719       ** range (if any).
  1701   1720       */
  1702   1721       nConstraint = nEq;
  1703   1722       if( pRangeEnd ){

Changes to src/whereexpr.c.

  1377   1377         idxNew = whereClauseInsert(pWC, pExpr, TERM_VIRTUAL);
  1378   1378         pWC->a[idxNew].iField = i+1;
  1379   1379         exprAnalyze(pSrc, pWC, idxNew);
  1380   1380         markTermAsChild(pWC, idxNew, idxTerm);
  1381   1381       }
  1382   1382     }
  1383   1383   
  1384         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1385         -  /* When sqlite_stat3 histogram data is available an operator of the
         1384  +#ifdef SQLITE_ENABLE_STAT4
         1385  +  /* When sqlite_stat4 histogram data is available an operator of the
  1386   1386     ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
  1387   1387     ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
  1388   1388     ** virtual term of that form.
  1389   1389     **
  1390   1390     ** Note that the virtual term must be tagged with TERM_VNULL.
  1391   1391     */
  1392   1392     if( pExpr->op==TK_NOTNULL
  1393   1393      && pExpr->pLeft->op==TK_COLUMN
  1394   1394      && pExpr->pLeft->iColumn>=0
  1395   1395      && !ExprHasProperty(pExpr, EP_FromJoin)
  1396         -   && OptimizationEnabled(db, SQLITE_Stat34)
         1396  +   && OptimizationEnabled(db, SQLITE_Stat4)
  1397   1397     ){
  1398   1398       Expr *pNewExpr;
  1399   1399       Expr *pLeft = pExpr->pLeft;
  1400   1400       int idxNew;
  1401   1401       WhereTerm *pNewTerm;
  1402   1402   
  1403   1403       pNewExpr = sqlite3PExpr(pParse, TK_GT,
................................................................................
  1414   1414         pNewTerm->eOperator = WO_GT;
  1415   1415         markTermAsChild(pWC, idxNew, idxTerm);
  1416   1416         pTerm = &pWC->a[idxTerm];
  1417   1417         pTerm->wtFlags |= TERM_COPIED;
  1418   1418         pNewTerm->prereqAll = pTerm->prereqAll;
  1419   1419       }
  1420   1420     }
  1421         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1421  +#endif /* SQLITE_ENABLE_STAT4 */
  1422   1422   
  1423   1423     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1424   1424     ** an index for tables to the left of the join.
  1425   1425     */
  1426   1426     testcase( pTerm!=&pWC->a[idxTerm] );
  1427   1427     pTerm = &pWC->a[idxTerm];
  1428   1428     pTerm->prereqRight |= extraRight;

Changes to src/window.c.

   930    930       p->selFlags &= ~SF_Aggregate;
   931    931   
   932    932       /* Create the ORDER BY clause for the sub-select. This is the concatenation
   933    933       ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
   934    934       ** redundant, remove the ORDER BY from the parent SELECT.  */
   935    935       pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
   936    936       pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
   937         -    if( pSort && p->pOrderBy ){
          937  +    if( pSort && p->pOrderBy && p->pOrderBy->nExpr<=pSort->nExpr ){
          938  +      int nSave = pSort->nExpr;
          939  +      pSort->nExpr = p->pOrderBy->nExpr;
   938    940         if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
   939    941           sqlite3ExprListDelete(db, p->pOrderBy);
   940    942           p->pOrderBy = 0;
   941    943         }
          944  +      pSort->nExpr = nSave;
   942    945       }
   943    946   
   944    947       /* Assign a cursor number for the ephemeral table used to buffer rows.
   945    948       ** The OpenEphemeral instruction is coded later, after it is known how
   946    949       ** many columns the table will have.  */
   947    950       pMWin->iEphCsr = pParse->nTab++;
   948    951       pParse->nTab += 3;
................................................................................
   990    993       );
   991    994       p->pSrc = sqlite3SrcListAppend(pParse, 0, 0, 0);
   992    995       if( p->pSrc ){
   993    996         Table *pTab2;
   994    997         p->pSrc->a[0].pSelect = pSub;
   995    998         sqlite3SrcListAssignCursors(pParse, p->pSrc);
   996    999         pSub->selFlags |= SF_Expanded;
   997         -      pTab2 = sqlite3ResultSetOfSelect(pParse, pSub);
         1000  +      pTab2 = sqlite3ResultSetOfSelect(pParse, pSub, SQLITE_AFF_NONE);
   998   1001         if( pTab2==0 ){
   999   1002           rc = SQLITE_NOMEM;
  1000   1003         }else{
  1001   1004           memcpy(pTab, pTab2, sizeof(Table));
  1002   1005           pTab->tabFlags |= TF_Ephemeral;
  1003   1006           p->pSrc->a[0].pTab = pTab;
  1004   1007           pTab = pTab2;

Changes to test/alter.test.

   852    852   
   853    853   #-------------------------------------------------------------------------
   854    854   # Test that it is not possible to use ALTER TABLE on any system table.
   855    855   #
   856    856   set system_table_list {1 sqlite_master}
   857    857   catchsql ANALYZE
   858    858   ifcapable analyze { lappend system_table_list 2 sqlite_stat1 }
   859         -ifcapable stat3   { lappend system_table_list 3 sqlite_stat3 }
   860    859   ifcapable stat4   { lappend system_table_list 4 sqlite_stat4 }
   861    860   
   862    861   foreach {tn tbl} $system_table_list {
   863    862     do_test alter-15.$tn.1 {
   864    863       catchsql "ALTER TABLE $tbl RENAME TO xyz"
   865    864     } [list 1 "table $tbl may not be altered"]
   866    865   

Changes to test/altertab.test.

   590    590     ALTER TABLE t0 RENAME COLUMN c0 TO c1;
   591    591   }
   592    592   do_execsql_test 18.2.2 {
   593    593     SELECT sql FROM sqlite_master;
   594    594   } {{CREATE TABLE t0 (c1 INTEGER, PRIMARY KEY(c1))}}
   595    595   
   596    596   finish_test
   597         -

Changes to test/altertab3.test.

   341    341     END;
   342    342   }
   343    343   
   344    344   do_catchsql_test 14.2 {
   345    345     ALTER TABLE t1 RENAME TO t1x;
   346    346   } {1 {error in trigger AFTER: no such column: a}}
   347    347   
   348         -finish_test
          348  +#-------------------------------------------------------------------------
          349  +reset_db
          350  +
          351  +do_execsql_test 16.1 {
          352  +  CREATE TABLE t1(x);
          353  +  CREATE TRIGGER AFTER INSERT ON t1 BEGIN
          354  +    SELECT (WITH t2 AS (WITH t3 AS (SELECT true)
          355  +          SELECT * FROM t3 ORDER BY true COLLATE nocase)
          356  +        SELECT 11);
          357  +
          358  +    WITH t4 AS (SELECT * FROM t1) SELECT 33;
          359  +  END;
          360  +}
          361  +do_execsql_test 16.2 {
          362  +  ALTER TABLE t1 RENAME TO t1x;
          363  +}
          364  +
          365  +#-------------------------------------------------------------------------
          366  +reset_db
          367  +do_execsql_test 17.1 {
          368  +  CREATE TABLE t1(a,b,c);
          369  +  CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN
          370  +    SELECT a () FILTER (WHERE a>0) FROM t1;
          371  +  END;
          372  +}
          373  +
          374  +do_execsql_test 17.2 {
          375  +  ALTER TABLE t1 RENAME TO t1x;
          376  +  ALTER TABLE t1x RENAME a TO aaa;
          377  +  SELECT sql FROM sqlite_master WHERE type='trigger';
          378  +} {
          379  +{CREATE TRIGGER AFTER INSERT ON "t1x" WHEN new.aaa NOT NULL BEGIN
          380  +    SELECT a () FILTER (WHERE aaa>0) FROM "t1x";
          381  +  END}
          382  +}
          383  +
   349    384   
          385  +finish_test

Changes to test/analyze.test.

   284    284     sqlite3 db test.db
   285    285     execsql {
   286    286       SELECT * FROM t4 WHERE x=1234;
   287    287     }
   288    288   } {}
   289    289   
   290    290   # Verify that DROP TABLE and DROP INDEX remove entries from the 
   291         -# sqlite_stat1, sqlite_stat3 and sqlite_stat4 tables.
          291  +# sqlite_stat1 and sqlite_stat4 tables.
   292    292   #
   293    293   do_test analyze-5.0 {
   294    294     execsql {
   295    295       DELETE FROM t3;
   296    296       DELETE FROM t4;
   297    297       INSERT INTO t3 VALUES(1,2,3,4);
   298    298       INSERT INTO t3 VALUES(5,6,7,8);
................................................................................
   302    302       INSERT INTO t3 SELECT a+64, b+64, c+64, d+64 FROM t3;
   303    303       INSERT INTO t4 SELECT a, b, c FROM t3;
   304    304       ANALYZE;
   305    305       SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
   306    306       SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
   307    307     }
   308    308   } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
   309         -ifcapable stat4||stat3 {
   310         -  ifcapable stat4 {set stat sqlite_stat4} else {set stat sqlite_stat3}
          309  +ifcapable stat4 {
   311    310     do_test analyze-5.1 {
   312         -    execsql "
   313         -      SELECT DISTINCT idx FROM $stat ORDER BY 1;
   314         -      SELECT DISTINCT tbl FROM $stat ORDER BY 1;
   315         -    "
          311  +    execsql {
          312  +      SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1;
          313  +      SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1;
          314  +    }
   316    315     } {t3i1 t3i2 t3i3 t4i1 t4i2 t3 t4}
   317    316   }
   318    317   do_test analyze-5.2 {
   319    318     execsql {
   320    319       DROP INDEX t3i2;
   321    320       SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
   322    321       SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
   323    322     }
   324    323   } {t3i1 t3i3 t4i1 t4i2 t3 t4}
   325         -ifcapable stat4||stat3 {
          324  +ifcapable stat4 {
   326    325     do_test analyze-5.3 {
   327         -    execsql "
   328         -      SELECT DISTINCT idx FROM $stat ORDER BY 1;
   329         -      SELECT DISTINCT tbl FROM $stat ORDER BY 1;
   330         -    "
          326  +    execsql {
          327  +      SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1;
          328  +      SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1;
          329  +    }
   331    330     } {t3i1 t3i3 t4i1 t4i2 t3 t4}
   332    331   }
   333    332   do_test analyze-5.4 {
   334    333     execsql {
   335    334       DROP TABLE t3;
   336    335       SELECT DISTINCT idx FROM sqlite_stat1 ORDER BY 1;
   337    336       SELECT DISTINCT tbl FROM sqlite_stat1 ORDER BY 1;
   338    337     }
   339    338   } {t4i1 t4i2 t4}
   340         -ifcapable stat4||stat3 {
          339  +ifcapable stat4 {
   341    340     do_test analyze-5.5 {
   342         -    execsql "
   343         -      SELECT DISTINCT idx FROM $stat ORDER BY 1;
   344         -      SELECT DISTINCT tbl FROM $stat ORDER BY 1;
   345         -    "
          341  +    execsql {
          342  +      SELECT DISTINCT idx FROM sqlite_stat4 ORDER BY 1;
          343  +      SELECT DISTINCT tbl FROM sqlite_stat4 ORDER BY 1;
          344  +    }
   346    345     } {t4i1 t4i2 t4}
   347    346   }
   348    347   
   349    348   # This test corrupts the database file so it must be the last test
   350    349   # in the series.
   351    350   #
   352    351   do_test analyze-5.99 {

Changes to test/analyze3.test.

    14     14   # instead of literal constant arguments.
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   set testprefix analyze3
    20     20   
    21         -ifcapable !stat4&&!stat3 {
           21  +ifcapable !stat4 {
    22     22     finish_test
    23     23     return
    24     24   }
    25     25   
    26     26   #----------------------------------------------------------------------
    27     27   # Test Organization:
    28     28   #
................................................................................
    96     96       execsql { INSERT INTO t1 VALUES($i+100, $i) }
    97     97     }
    98     98     execsql {
    99     99       COMMIT;
   100    100       ANALYZE;
   101    101     }
   102    102   
   103         -  ifcapable stat4 {
   104         -    execsql { SELECT count(*)>0 FROM sqlite_stat4; }
   105         -  } else {
   106         -    execsql { SELECT count(*)>0 FROM sqlite_stat3; }
   107         -  }
          103  +  execsql { SELECT count(*)>0 FROM sqlite_stat4; }
   108    104   } {1}
   109    105   
   110    106   do_execsql_test analyze3-1.1.x {
   111    107     SELECT count(*) FROM t1 WHERE x>200 AND x<300;
   112    108     SELECT count(*) FROM t1 WHERE x>0 AND x<1100;
   113    109   } {99 1000}
   114    110   

Changes to test/analyze5.test.

    13     13   # in this file is the use of the sqlite_stat4 histogram data on tables
    14     14   # with many repeated values and only a few distinct values.
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20         -ifcapable !stat4&&!stat3 {
           20  +ifcapable !stat4 {
    21     21     finish_test
    22     22     return
    23     23   }
    24     24   
    25     25   set testprefix analyze5
    26     26   
    27     27   proc eqp {sql {db db}} {
................................................................................
    63     63       CREATE INDEX t1v ON t1(v);  -- mixed case text
    64     64       CREATE INDEX t1w ON t1(w);  -- integers 0, 1, 2 and a few NULLs
    65     65       CREATE INDEX t1x ON t1(x);  -- integers 1, 2, 3 and many NULLs
    66     66       CREATE INDEX t1y ON t1(y);  -- integers 0 and very few 1s
    67     67       CREATE INDEX t1z ON t1(z);  -- integers 0, 1, 2, and 3
    68     68       ANALYZE;
    69     69     }
    70         -  ifcapable stat4 {
    71         -    db eval {
    72         -      SELECT DISTINCT lindex(test_decode(sample),0) 
    73         -        FROM sqlite_stat4 WHERE idx='t1u' ORDER BY nlt;
    74         -    }
    75         -  } else {
    76         -    db eval {
    77         -      SELECT sample FROM sqlite_stat3 WHERE idx='t1u' ORDER BY nlt;
    78         -    }
           70  +  db eval {
           71  +    SELECT DISTINCT lindex(test_decode(sample),0) 
           72  +      FROM sqlite_stat4 WHERE idx='t1u' ORDER BY nlt;
    79     73     }
    80     74   } {alpha bravo charlie delta}
    81     75   
    82     76   do_test analyze5-1.1 {
    83         -  ifcapable stat4 {
    84         -    db eval {
    85         -      SELECT DISTINCT lower(lindex(test_decode(sample), 0)) 
    86         -        FROM sqlite_stat4 WHERE idx='t1v' ORDER BY 1
    87         -    }
    88         -  } else {
    89         -    db eval {
    90         -      SELECT lower(sample) FROM sqlite_stat3 WHERE idx='t1v' ORDER BY 1
    91         -    }
           77  +  db eval {
           78  +    SELECT DISTINCT lower(lindex(test_decode(sample), 0)) 
           79  +      FROM sqlite_stat4 WHERE idx='t1v' ORDER BY 1
    92     80     }
    93     81   } {alpha bravo charlie delta}
    94         -ifcapable stat4 {
    95         -  do_test analyze5-1.2 {
    96         -    db eval {SELECT idx, count(*) FROM sqlite_stat4 GROUP BY 1 ORDER BY 1}
    97         -  } {t1t 8 t1u 8 t1v 8 t1w 8 t1x 8 t1y 9 t1z 8}
    98         -} else {
    99         -  do_test analyze5-1.2 {
   100         -    db eval {SELECT idx, count(*) FROM sqlite_stat3 GROUP BY 1 ORDER BY 1}
   101         -  } {t1t 4 t1u 4 t1v 4 t1w 4 t1x 4 t1y 2 t1z 4}
   102         -}
           82  +do_test analyze5-1.2 {
           83  +  db eval {SELECT idx, count(*) FROM sqlite_stat4 GROUP BY 1 ORDER BY 1}
           84  +} {t1t 8 t1u 8 t1v 8 t1w 8 t1x 8 t1y 9 t1z 8}
   103     85   
   104     86   # Verify that range queries generate the correct row count estimates
   105     87   #
   106     88   foreach {testid where index rows} {
   107     89       1  {z>=0 AND z<=0}       t1z  400
   108     90       2  {z>=1 AND z<=1}       t1z  300
   109     91       3  {z>=2 AND z<=2}       t1z  175

Changes to test/analyze6.test.

    13     13   # in this file a corner-case query planner optimization involving the
    14     14   # join order of two tables of different sizes.
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   
    20         -ifcapable !stat4&&!stat3 {
           20  +ifcapable !stat4 {
    21     21     finish_test
    22     22     return
    23     23   }
    24     24   
    25     25   set testprefix analyze6
    26     26   
    27     27   proc eqp {sql {db db}} {

Changes to test/analyze7.test.

    78     78   } {/*SEARCH TABLE t1 USING INDEX t1a (a=?)*/}
    79     79   do_test analyze7-3.1 {
    80     80     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
    81     81   } {/*SEARCH TABLE t1 USING INDEX t1b (b=?)*/}
    82     82   do_test analyze7-3.2.1 {
    83     83     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;}
    84     84   } {/*SEARCH TABLE t1 USING INDEX t1cd (c=?)*/}
    85         -ifcapable stat4||stat3 {
           85  +ifcapable stat4 {
    86     86     # If ENABLE_STAT4 is defined, SQLite comes up with a different estimated
    87     87     # row count for (c=2) than it does for (c=?).
    88     88     do_test analyze7-3.2.2 {
    89     89       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    90     90     } {/*SEARCH TABLE t1 USING INDEX t1cd (c=?)*/}
    91     91   } else {
    92     92     # If ENABLE_STAT4 is not defined, the expected row count for (c=2) is the
................................................................................
    95     95       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    96     96     } {/*SEARCH TABLE t1 USING INDEX t1cd (c=?)*/}
    97     97   }
    98     98   do_test analyze7-3.3 {
    99     99     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
   100    100   } {/*SEARCH TABLE t1 USING INDEX t1a (a=?)*/}
   101    101   
   102         -ifcapable {!stat4 && !stat3} {
          102  +ifcapable {!stat4} {
   103    103     do_test analyze7-3.4 {
   104    104       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
   105    105     } {/*SEARCH TABLE t1 USING INDEX t1b (b=?)*/}
   106    106     do_test analyze7-3.5 {
   107    107       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123}
   108    108     } {/*SEARCH TABLE t1 USING INDEX t1a (a=?)*/}
   109    109   }
   110    110   do_test analyze7-3.6 {
   111    111     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123}
   112    112   } {/*SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?)*/}
   113    113   
   114    114   finish_test

Changes to test/analyze8.test.

     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     12   # This file implements tests for SQLite library.  The focus of the tests
    13         -# in this file is testing the capabilities of sqlite_stat3.
           13  +# in this file is testing the capabilities of sqlite_stat4.
    14     14   #
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19         -ifcapable !stat4&&!stat3 {
           19  +ifcapable !stat4 {
    20     20     finish_test
    21     21     return
    22     22   }
    23     23   
    24     24   set testprefix analyze8
    25     25   
    26     26   proc eqp {sql {db db}} {

Deleted test/analyzeA.test.

     1         -# 2013 August 3
     2         -#
     3         -# The author disclaims copyright to this source code.  In place of
     4         -# a legal notice, here is a blessing:
     5         -#
     6         -#    May you do good and not evil.
     7         -#    May you find forgiveness for yourself and forgive others.
     8         -#    May you share freely, never taking more than you give.
     9         -#
    10         -#***********************************************************************
    11         -#
    12         -# This file contains automated tests used to verify that the current build
    13         -# (which must be either ENABLE_STAT3 or ENABLE_STAT4) works with both stat3
    14         -# and stat4 data.
    15         -#
    16         -
    17         -set testdir [file dirname $argv0]
    18         -source $testdir/tester.tcl
    19         -set testprefix analyzeA
    20         -
    21         -ifcapable !stat4&&!stat3 {
    22         -  finish_test
    23         -  return
    24         -}
    25         -
    26         -# Populate the stat3 table according to the current contents of the db
    27         -#
    28         -proc populate_stat3 {{bDropTable 1}} {
    29         -  # Open a second connection on database "test.db" and run ANALYZE. If this
    30         -  # is an ENABLE_STAT3 build, this is all that is required to create and
    31         -  # populate the sqlite_stat3 table. 
    32         -  # 
    33         -  sqlite3 db2 test.db
    34         -  execsql { ANALYZE }
    35         -
    36         -  # Now, if this is an ENABLE_STAT4 build, create and populate the 
    37         -  # sqlite_stat3 table based on the stat4 data gathered by the ANALYZE
    38         -  # above. Then drop the sqlite_stat4 table.
    39         -  #
    40         -  ifcapable stat4 {
    41         -    db2 func lindex lindex
    42         -    execsql {
    43         -      PRAGMA writable_schema = on;
    44         -      CREATE TABLE sqlite_stat3(tbl,idx,neq,nlt,ndlt,sample);
    45         -      INSERT INTO sqlite_stat3 
    46         -      SELECT DISTINCT tbl, idx, 
    47         -        lindex(neq,0), lindex(nlt,0), lindex(ndlt,0), test_extract(sample, 0)
    48         -      FROM sqlite_stat4;
    49         -    } db2
    50         -    if {$bDropTable} { execsql {DROP TABLE sqlite_stat4} db2 }
    51         -    execsql { PRAGMA writable_schema = off }
    52         -  }
    53         -
    54         -  # Modify the database schema cookie to ensure that the other connection
    55         -  # reloads the schema.
    56         -  #
    57         -  execsql {
    58         -    CREATE TABLE obscure_tbl_nm(x);
    59         -    DROP TABLE obscure_tbl_nm;
    60         -  } db2
    61         -  db2 close
    62         -}
    63         -
    64         -# Populate the stat4 table according to the current contents of the db
    65         -#
    66         -proc populate_stat4 {{bDropTable 1}} {
    67         -  sqlite3 db2 test.db
    68         -  execsql { ANALYZE }
    69         -
    70         -  ifcapable stat3 {
    71         -    execsql {
    72         -      PRAGMA writable_schema = on;
    73         -      CREATE TABLE sqlite_stat4(tbl,idx,neq,nlt,ndlt,sample);
    74         -      INSERT INTO sqlite_stat4 
    75         -      SELECT tbl, idx, neq, nlt, ndlt, sqlite_record(sample) 
    76         -      FROM sqlite_stat3;
    77         -    } db2
    78         -    if {$bDropTable} { execsql {DROP TABLE sqlite_stat3} db2 }
    79         -    execsql { PRAGMA writable_schema = off }
    80         -  }
    81         - 
    82         -  # Modify the database schema cookie to ensure that the other connection
    83         -  # reloads the schema.
    84         -  #
    85         -  execsql {
    86         -    CREATE TABLE obscure_tbl_nm(x);
    87         -    DROP TABLE obscure_tbl_nm;
    88         -  } db2
    89         -  db2 close
    90         -}
    91         -
    92         -# Populate the stat4 table according to the current contents of the db.
    93         -# Leave deceptive data in the stat3 table. This data should be ignored
    94         -# in favour of that from the stat4 table.
    95         -#
    96         -proc populate_both {} {
    97         -  ifcapable stat4 { populate_stat3 0 }
    98         -  ifcapable stat3 { populate_stat4 0 }
    99         -
   100         -  sqlite3 db2 test.db
   101         -  execsql {
   102         -    PRAGMA writable_schema = on;
   103         -    UPDATE sqlite_stat3 SET idx = 
   104         -      CASE idx WHEN 't1b' THEN 't1c' ELSE 't1b'
   105         -    END;
   106         -    PRAGMA writable_schema = off;
   107         -    CREATE TABLE obscure_tbl_nm(x);
   108         -    DROP TABLE obscure_tbl_nm;
   109         -  } db2
   110         -  db2 close
   111         -}
   112         -
   113         -foreach {tn analyze_cmd} {
   114         -  1 populate_stat4 
   115         -  2 populate_stat3
   116         -  3 populate_both
   117         -} {
   118         -  reset_db
   119         -  do_test 1.$tn.1 {
   120         -    execsql { CREATE TABLE t1(a INTEGER PRIMARY KEY, b INT, c INT) }
   121         -    for {set i 0} {$i < 100} {incr i} {
   122         -      set c [expr int(pow(1.1,$i)/100)]
   123         -      set b [expr 125 - int(pow(1.1,99-$i))/100]
   124         -      execsql {INSERT INTO t1 VALUES($i, $b, $c)}
   125         -    }
   126         -  } {}
   127         -
   128         -  execsql { CREATE INDEX t1b ON t1(b) }
   129         -  execsql { CREATE INDEX t1c ON t1(c) }
   130         -  $analyze_cmd
   131         -
   132         -  do_execsql_test 1.$tn.2.1 { SELECT count(*) FROM t1 WHERE b=31 } 1
   133         -  do_execsql_test 1.$tn.2.2 { SELECT count(*) FROM t1 WHERE c=0  } 49
   134         -  do_execsql_test 1.$tn.2.3 { SELECT count(*) FROM t1 WHERE b=125  } 49
   135         -  do_execsql_test 1.$tn.2.4 { SELECT count(*) FROM t1 WHERE c=16  } 1
   136         -
   137         -  do_eqp_test 1.$tn.2.5 {
   138         -    SELECT * FROM t1 WHERE b = 31 AND c = 0;
   139         -  } {SEARCH TABLE t1 USING INDEX t1b (b=?)}
   140         -  do_eqp_test 1.$tn.2.6 {
   141         -    SELECT * FROM t1 WHERE b = 125 AND c = 16;
   142         -  } {SEARCH TABLE t1 USING INDEX t1c (c=?)}
   143         -
   144         -  do_execsql_test 1.$tn.3.1 { 
   145         -    SELECT count(*) FROM t1 WHERE b BETWEEN 0 AND 50
   146         -  } {6}
   147         -  do_execsql_test 1.$tn.3.2 { 
   148         -    SELECT count(*) FROM t1 WHERE c BETWEEN 0 AND 50
   149         -  } {90}
   150         -  do_execsql_test 1.$tn.3.3 { 
   151         -    SELECT count(*) FROM t1 WHERE b BETWEEN 75 AND 125
   152         -  } {90}
   153         -  do_execsql_test 1.$tn.3.4 { 
   154         -    SELECT count(*) FROM t1 WHERE c BETWEEN 75 AND 125
   155         -  } {6}
   156         -
   157         -  do_eqp_test 1.$tn.3.5 {
   158         -    SELECT * FROM t1 WHERE b BETWEEN 0 AND 50 AND c BETWEEN 0 AND 50
   159         -  } {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}
   160         -
   161         -  do_eqp_test 1.$tn.3.6 {
   162         -    SELECT * FROM t1 WHERE b BETWEEN 75 AND 125 AND c BETWEEN 75 AND 125
   163         -  } {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}
   164         -
   165         -  do_eqp_test 1.$tn.3.7 {
   166         -    SELECT * FROM t1 WHERE b BETWEEN +0 AND +50 AND c BETWEEN +0 AND +50
   167         -  } {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}
   168         -
   169         -  do_eqp_test 1.$tn.3.8 {
   170         -    SELECT * FROM t1
   171         -     WHERE b BETWEEN cast('0' AS int) AND cast('50.0' AS real)
   172         -       AND c BETWEEN cast('0' AS numeric) AND cast('50.0' AS real)
   173         -  } {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}
   174         -
   175         -  do_eqp_test 1.$tn.3.9 {
   176         -    SELECT * FROM t1 WHERE b BETWEEN +75 AND +125 AND c BETWEEN +75 AND +125
   177         -  } {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}
   178         -
   179         -  do_eqp_test 1.$tn.3.10 {
   180         -    SELECT * FROM t1
   181         -     WHERE b BETWEEN cast('75' AS int) AND cast('125.0' AS real)
   182         -       AND c BETWEEN cast('75' AS numeric) AND cast('125.0' AS real)
   183         -  } {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}
   184         -}
   185         -
   186         -finish_test

Deleted test/analyzeB.test.

     1         -# 2013 August 3
     2         -#
     3         -# The author disclaims copyright to this source code.  In place of
     4         -# a legal notice, here is a blessing:
     5         -#
     6         -#    May you do good and not evil.
     7         -#    May you find forgiveness for yourself and forgive others.
     8         -#    May you share freely, never taking more than you give.
     9         -#
    10         -#***********************************************************************
    11         -#
    12         -# This file contains automated tests used to verify that the sqlite_stat3
    13         -# functionality is working. The tests in this file are based on a subset
    14         -# of the sqlite_stat4 tests in analyze9.test.
    15         -#
    16         -
    17         -set testdir [file dirname $argv0]
    18         -source $testdir/tester.tcl
    19         -set testprefix analyzeB
    20         -
    21         -ifcapable !stat3 {
    22         -  finish_test
    23         -  return
    24         -}
    25         -
    26         -do_execsql_test 1.0 {
    27         -  CREATE TABLE t1(a TEXT, b TEXT); 
    28         -  INSERT INTO t1 VALUES('(0)', '(0)');
    29         -  INSERT INTO t1 VALUES('(1)', '(1)');
    30         -  INSERT INTO t1 VALUES('(2)', '(2)');
    31         -  INSERT INTO t1 VALUES('(3)', '(3)');
    32         -  INSERT INTO t1 VALUES('(4)', '(4)');
    33         -  CREATE INDEX i1 ON t1(a, b);
    34         -} {}
    35         -
    36         -
    37         -do_execsql_test 1.1 {
    38         -  ANALYZE;
    39         -} {}
    40         -
    41         -do_execsql_test 1.2 {
    42         -  SELECT tbl,idx,nEq,nLt,nDLt,quote(sample) FROM sqlite_stat3;
    43         -} {
    44         -  t1 i1 1 0 0 '(0)'
    45         -  t1 i1 1 1 1 '(1)'
    46         -  t1 i1 1 2 2 '(2)'
    47         -  t1 i1 1 3 3 '(3)'
    48         -  t1 i1 1 4 4 '(4)'
    49         -}
    50         -
    51         -if {[permutation] != "utf16"} {
    52         -  do_execsql_test 1.3 {
    53         -    SELECT tbl,idx,nEq,nLt,nDLt,quote(sample) FROM sqlite_stat3;
    54         -  } {
    55         -    t1 i1 1 0 0 '(0)'
    56         -    t1 i1 1 1 1 '(1)'
    57         -    t1 i1 1 2 2 '(2)'
    58         -    t1 i1 1 3 3 '(3)'
    59         -    t1 i1 1 4 4 '(4)'
    60         -  }
    61         -}
    62         -
    63         -
    64         -#-------------------------------------------------------------------------
    65         -# This is really just to test SQL user function "test_decode".
    66         -#
    67         -reset_db
    68         -do_execsql_test 2.1 {
    69         -  CREATE TABLE t1(a, b, c);
    70         -  INSERT INTO t1(a) VALUES('some text');
    71         -  INSERT INTO t1(a) VALUES(14);
    72         -  INSERT INTO t1(a) VALUES(NULL);
    73         -  INSERT INTO t1(a) VALUES(22.0);
    74         -  INSERT INTO t1(a) VALUES(x'656667');
    75         -  CREATE INDEX i1 ON t1(a, b, c);
    76         -  ANALYZE;
    77         -  SELECT quote(sample) FROM sqlite_stat3;
    78         -} {
    79         -  NULL 14 22.0 {'some text'} X'656667' 
    80         -}
    81         -
    82         -#-------------------------------------------------------------------------
    83         -# 
    84         -reset_db
    85         -do_execsql_test 3.1 {
    86         -  CREATE TABLE t2(a, b);
    87         -  CREATE INDEX i2 ON t2(a, b);
    88         -  BEGIN;
    89         -}
    90         -
    91         -do_test 3.2 {
    92         -  for {set i 0} {$i < 1000} {incr i} {
    93         -    set a [expr $i / 10]
    94         -    set b [expr int(rand() * 15.0)]
    95         -    execsql { INSERT INTO t2 VALUES($a, $b) }
    96         -  }
    97         -  execsql COMMIT
    98         -} {}
    99         -
   100         -db func lindex lindex
   101         -
   102         -# Each value of "a" occurs exactly 10 times in the table.
   103         -#
   104         -do_execsql_test 3.3.1 {
   105         -  SELECT count(*) FROM t2 GROUP BY a;
   106         -} [lrange [string repeat "10 " 100] 0 99]
   107         -
   108         -# The first element in the "nEq" list of all samples should therefore be 10.
   109         -#
   110         -do_execsql_test 3.3.2 {
   111         -  ANALYZE;
   112         -  SELECT nEq FROM sqlite_stat3;
   113         -} [lrange [string repeat "10 " 100] 0 23]
   114         -
   115         -#-------------------------------------------------------------------------
   116         -# 
   117         -do_execsql_test 3.4 {
   118         -  DROP TABLE IF EXISTS t1;
   119         -  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
   120         -  INSERT INTO t1 VALUES(1, 1, 'one-a');
   121         -  INSERT INTO t1 VALUES(11, 1, 'one-b');
   122         -  INSERT INTO t1 VALUES(21, 1, 'one-c');
   123         -  INSERT INTO t1 VALUES(31, 1, 'one-d');
   124         -  INSERT INTO t1 VALUES(41, 1, 'one-e');
   125         -  INSERT INTO t1 VALUES(51, 1, 'one-f');
   126         -  INSERT INTO t1 VALUES(61, 1, 'one-g');
   127         -  INSERT INTO t1 VALUES(71, 1, 'one-h');
   128         -  INSERT INTO t1 VALUES(81, 1, 'one-i');
   129         -  INSERT INTO t1 VALUES(91, 1, 'one-j');
   130         -  INSERT INTO t1 SELECT a+1,2,'two' || substr(c,4) FROM t1;
   131         -  INSERT INTO t1 SELECT a+2,3,'three'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
   132         -  INSERT INTO t1 SELECT a+3,4,'four'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
   133         -  INSERT INTO t1 SELECT a+4,5,'five'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';
   134         -  INSERT INTO t1 SELECT a+5,6,'six'||substr(c,4) FROM t1 WHERE c GLOB 'one-*';	
   135         -  CREATE INDEX t1b ON t1(b);
   136         -  ANALYZE;
   137         -  SELECT c FROM t1 WHERE b=3 AND a BETWEEN 30 AND 60;
   138         -} {three-d three-e three-f}
   139         -
   140         -
   141         -#-------------------------------------------------------------------------
   142         -# These tests verify that the sample selection for stat3 appears to be 
   143         -# working as designed.
   144         -#
   145         -
   146         -reset_db
   147         -db func lindex lindex
   148         -db func lrange lrange
   149         -
   150         -do_execsql_test 4.0 {
   151         -  DROP TABLE IF EXISTS t1;
   152         -  CREATE TABLE t1(a, b, c);
   153         -  CREATE INDEX i1 ON t1(c, b, a);
   154         -}
   155         -
   156         -
   157         -proc insert_filler_rows_n {iStart args} {
   158         -  set A(-ncopy) 1
   159         -  set A(-nval) 1
   160         -
   161         -  foreach {k v} $args {
   162         -    if {[info exists A($k)]==0} { error "no such option: $k" }
   163         -    set A($k) $v
   164         -  }
   165         -  if {[llength $args] % 2} {
   166         -    error "option requires an argument: [lindex $args end]"
   167         -  }
   168         -
   169         -  for {set i 0} {$i < $A(-nval)} {incr i} {
   170         -    set iVal [expr $iStart+$i]
   171         -    for {set j 0} {$j < $A(-ncopy)} {incr j} {
   172         -      execsql { INSERT INTO t1 VALUES($iVal, $iVal, $iVal) }
   173         -    }
   174         -  }
   175         -}
   176         -
   177         -do_test 4.1 {
   178         -  execsql { BEGIN }
   179         -  insert_filler_rows_n  0  -ncopy 10 -nval 19
   180         -  insert_filler_rows_n 20  -ncopy  1 -nval 100
   181         -
   182         -  execsql {
   183         -    INSERT INTO t1(c, b, a) VALUES(200, 1, 'a');
   184         -    INSERT INTO t1(c, b, a) VALUES(200, 1, 'b');
   185         -    INSERT INTO t1(c, b, a) VALUES(200, 1, 'c');
   186         -
   187         -    INSERT INTO t1(c, b, a) VALUES(200, 2, 'e');
   188         -    INSERT INTO t1(c, b, a) VALUES(200, 2, 'f');
   189         -
   190         -    INSERT INTO t1(c, b, a) VALUES(201, 3, 'g');
   191         -    INSERT INTO t1(c, b, a) VALUES(201, 4, 'h');
   192         -
   193         -    ANALYZE;
   194         -    SELECT count(*) FROM sqlite_stat3;
   195         -    SELECT count(*) FROM t1;
   196         -  }
   197         -} {24 297}
   198         -
   199         -do_execsql_test 4.2 {
   200         -  SELECT neq, nlt, ndlt, sample FROM sqlite_stat3 ORDER BY rowid LIMIT 16;
   201         -} {
   202         -  10 0 0 0
   203         -  10 10 1 1
   204         -  10 20 2 2
   205         -  10 30 3 3
   206         -  10 40 4 4
   207         -  10 50 5 5
   208         -  10 60 6 6
   209         -  10 70 7 7
   210         -  10 80 8 8
   211         -  10 90 9 9
   212         -  10 100 10 10
   213         -  10 110 11 11
   214         -  10 120 12 12
   215         -  10 130 13 13
   216         -  10 140 14 14
   217         -  10 150 15 15
   218         -}
   219         -
   220         -do_execsql_test 4.3 {
   221         -  SELECT neq, nlt, ndlt, sample FROM sqlite_stat3
   222         -  ORDER BY rowid DESC LIMIT 2;
   223         -} {
   224         -  2 295 120 201
   225         -  5 290 119 200
   226         -}
   227         -
   228         -do_execsql_test 4.4 { SELECT count(DISTINCT c) FROM t1 WHERE c<201 } 120
   229         -do_execsql_test 4.5 { SELECT count(DISTINCT c) FROM t1 WHERE c<200 } 119
   230         -
   231         -reset_db
   232         -do_test 4.7 {
   233         -  execsql { 
   234         -    BEGIN;
   235         -    CREATE TABLE t1(o,t INTEGER PRIMARY KEY);
   236         -    CREATE INDEX i1 ON t1(o);
   237         -  }
   238         -  for {set i 0} {$i<10000} {incr i [expr (($i<1000)?1:10)]} {
   239         -    execsql { INSERT INTO t1 VALUES('x', $i) }
   240         -  }
   241         -  execsql {
   242         -    COMMIT;
   243         -    ANALYZE;
   244         -    SELECT count(*) FROM sqlite_stat3;
   245         -  }
   246         -} {1}
   247         -do_execsql_test 4.8 {
   248         -  SELECT sample FROM sqlite_stat3;
   249         -} {x}
   250         -
   251         -
   252         -#-------------------------------------------------------------------------
   253         -# The following would cause a crash at one point.
   254         -#
   255         -reset_db
   256         -do_execsql_test 5.1 {
   257         -  PRAGMA encoding = 'utf-16';
   258         -  CREATE TABLE t0(v);
   259         -  ANALYZE;
   260         -}
   261         -
   262         -#-------------------------------------------------------------------------
   263         -# This was also crashing (corrupt sqlite_stat3 table).
   264         -#
   265         -reset_db
   266         -do_execsql_test 6.1 {
   267         -  CREATE TABLE t1(a, b);
   268         -  CREATE INDEX i1 ON t1(a);
   269         -  CREATE INDEX i2 ON t1(b);
   270         -  INSERT INTO t1 VALUES(1, 1);
   271         -  INSERT INTO t1 VALUES(2, 2);
   272         -  INSERT INTO t1 VALUES(3, 3);
   273         -  INSERT INTO t1 VALUES(4, 4);
   274         -  INSERT INTO t1 VALUES(5, 5);
   275         -  ANALYZE;
   276         -  PRAGMA writable_schema = 1;
   277         -  CREATE TEMP TABLE x1 AS
   278         -    SELECT tbl,idx,neq,nlt,ndlt,sample FROM sqlite_stat3
   279         -    ORDER BY (rowid%5), rowid;
   280         -  DELETE FROM sqlite_stat3;
   281         -  INSERT INTO sqlite_stat3 SELECT * FROM x1;
   282         -  PRAGMA writable_schema = 0;
   283         -  ANALYZE sqlite_master;
   284         -}
   285         -do_execsql_test 6.2 {
   286         -  SELECT * FROM t1 WHERE a = 'abc';
   287         -}
   288         -
   289         -#-------------------------------------------------------------------------
   290         -# The following tests experiment with adding corrupted records to the
   291         -# 'sample' column of the sqlite_stat3 table.
   292         -#
   293         -reset_db
   294         -sqlite3_db_config_lookaside db 0 0 0
   295         -
   296         -do_execsql_test 7.1 {
   297         -  CREATE TABLE t1(a, b);
   298         -  CREATE INDEX i1 ON t1(a, b);
   299         -  INSERT INTO t1 VALUES(1, 1);
   300         -  INSERT INTO t1 VALUES(2, 2);
   301         -  INSERT INTO t1 VALUES(3, 3);
   302         -  INSERT INTO t1 VALUES(4, 4);
   303         -  INSERT INTO t1 VALUES(5, 5);
   304         -  ANALYZE;
   305         -  UPDATE sqlite_stat3 SET sample = X'' WHERE rowid = 1;
   306         -  ANALYZE sqlite_master;
   307         -}
   308         -
   309         -do_execsql_test 7.2 {
   310         -  UPDATE sqlite_stat3 SET sample = X'FFFF';
   311         -  ANALYZE sqlite_master;
   312         -  SELECT * FROM t1 WHERE a = 1;
   313         -} {1 1}
   314         -
   315         -do_execsql_test 7.3 {
   316         -  ANALYZE;
   317         -  UPDATE sqlite_stat3 SET neq = '0 0 0';
   318         -  ANALYZE sqlite_master;
   319         -  SELECT * FROM t1 WHERE a = 1;
   320         -} {1 1}
   321         -
   322         -do_execsql_test 7.4 {
   323         -  ANALYZE;
   324         -  UPDATE sqlite_stat3 SET ndlt = '0 0 0';
   325         -  ANALYZE sqlite_master;
   326         -  SELECT * FROM t1 WHERE a = 3;
   327         -} {3 3}
   328         -
   329         -do_execsql_test 7.5 {
   330         -  ANALYZE;
   331         -  UPDATE sqlite_stat3 SET nlt = '0 0 0';
   332         -  ANALYZE sqlite_master;
   333         -  SELECT * FROM t1 WHERE a = 5;
   334         -} {5 5}
   335         -
   336         -#-------------------------------------------------------------------------
   337         -#
   338         -reset_db
   339         -do_execsql_test 8.1 {
   340         -  CREATE TABLE t1(x TEXT);
   341         -  CREATE INDEX i1 ON t1(x);
   342         -  INSERT INTO t1 VALUES('1');
   343         -  INSERT INTO t1 VALUES('2');
   344         -  INSERT INTO t1 VALUES('3');
   345         -  INSERT INTO t1 VALUES('4');
   346         -  ANALYZE;
   347         -}
   348         -do_execsql_test 8.2 {
   349         -  SELECT * FROM t1 WHERE x = 3;
   350         -} {3}
   351         -
   352         -#-------------------------------------------------------------------------
   353         -#
   354         -reset_db
   355         -do_execsql_test 9.1 {
   356         -  CREATE TABLE t1(a, b, c, d, e);
   357         -  CREATE INDEX i1 ON t1(a, b, c, d);
   358         -  CREATE INDEX i2 ON t1(e);
   359         -}
   360         -do_test 9.2 {
   361         -  execsql BEGIN;
   362         -  for {set i 0} {$i < 100} {incr i} {
   363         -    execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])"
   364         -  }
   365         -  for {set i 0} {$i < 20} {incr i} {
   366         -    execsql "INSERT INTO t1 VALUES('x', 'y', 'z', 101, $i)"
   367         -  }
   368         -  for {set i 102} {$i < 200} {incr i} {
   369         -    execsql "INSERT INTO t1 VALUES('x', 'y', 'z', $i, [expr $i/2])"
   370         -  }
   371         -  execsql COMMIT
   372         -  execsql ANALYZE
   373         -} {}
   374         -
   375         -do_eqp_test 9.3.1 {
   376         -  SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=101 AND e=5;
   377         -} {/t1 USING INDEX i1/}
   378         -do_eqp_test 9.3.2 {
   379         -  SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=99 AND e=5;
   380         -} {/t1 USING INDEX i1/}
   381         -
   382         -set value_d [expr 101]
   383         -do_eqp_test 9.4.1 {
   384         -  SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5
   385         -} {/t1 USING INDEX i1/}
   386         -set value_d [expr 99]
   387         -do_eqp_test 9.4.2 {
   388         -  SELECT * FROM t1 WHERE a='x' AND b='y' AND c='z' AND d=$value_d AND e=5
   389         -} {/t1 USING INDEX i1/}
   390         -
   391         -#-------------------------------------------------------------------------
   392         -# Check that the planner takes stat3 data into account when considering
   393         -# "IS NULL" and "IS NOT NULL" constraints.
   394         -#
   395         -do_execsql_test 10.1.1 {
   396         -  DROP TABLE IF EXISTS t3;
   397         -  CREATE TABLE t3(a, b);
   398         -  CREATE INDEX t3a ON t3(a);
   399         -  CREATE INDEX t3b ON t3(b);
   400         -}
   401         -do_test 10.1.2 {
   402         -  for {set i 1} {$i < 100} {incr i} {
   403         -    if {$i>90} { set a $i } else { set a NULL }
   404         -    set b [expr $i % 5]
   405         -    execsql "INSERT INTO t3 VALUES($a, $b)"
   406         -  }
   407         -  execsql ANALYZE
   408         -} {}
   409         -do_eqp_test 10.1.3 {
   410         -  SELECT * FROM t3 WHERE a IS NULL AND b = 2
   411         -} {/t3 USING INDEX t3b/}
   412         -do_eqp_test 10.1.4 {
   413         -  SELECT * FROM t3 WHERE a IS NOT NULL AND b = 2
   414         -} {/t3 USING INDEX t3a/}
   415         -
   416         -#-------------------------------------------------------------------------
   417         -# Check that stat3 data is used correctly with non-default collation
   418         -# sequences.
   419         -#
   420         -foreach {tn schema} {
   421         -  1 {
   422         -    CREATE TABLE t4(a COLLATE nocase, b);
   423         -    CREATE INDEX t4a ON t4(a);
   424         -    CREATE INDEX t4b ON t4(b);
   425         -  }
   426         -  2 {
   427         -    CREATE TABLE t4(a, b);
   428         -    CREATE INDEX t4a ON t4(a COLLATE nocase);
   429         -    CREATE INDEX t4b ON t4(b);
   430         -  }
   431         -} {
   432         -  drop_all_tables
   433         -  do_test 11.$tn.1 { execsql $schema } {}
   434         -
   435         -  do_test 11.$tn.2 {
   436         -    for {set i 0} {$i < 100} {incr i} {
   437         -      if { ($i % 10)==0 } { set a ABC } else { set a DEF }
   438         -      set b [expr $i % 5]
   439         -        execsql { INSERT INTO t4 VALUES($a, $b) }
   440         -    }
   441         -    execsql ANALYZE
   442         -  } {}
   443         -
   444         -  do_eqp_test 11.$tn.3 {
   445         -    SELECT * FROM t4 WHERE a = 'def' AND b = 3;
   446         -  } {/t4 USING INDEX t4b/}
   447         -
   448         -  if {$tn==1} {
   449         -    set sql "SELECT * FROM t4 WHERE a = 'abc' AND b = 3;"
   450         -    do_eqp_test 11.$tn.4 $sql {/t4 USING INDEX t4a/}
   451         -  } else {
   452         -
   453         -    set sql "SELECT * FROM t4 WHERE a = 'abc' COLLATE nocase AND b = 3;"
   454         -    do_eqp_test 11.$tn.5 $sql {/t4 USING INDEX t4a/}
   455         -
   456         -    set sql "SELECT * FROM t4 WHERE a COLLATE nocase = 'abc' AND b = 3;"
   457         -    do_eqp_test 11.$tn.6 $sql {/t4 USING INDEX t4a/}
   458         -  }
   459         -}
   460         -
   461         -#-------------------------------------------------------------------------
   462         -# Test that nothing untoward happens if the stat3 table contains entries
   463         -# for indexes that do not exist. Or NULL values in the idx column.
   464         -# Or NULL values in any of the other columns.
   465         -#
   466         -drop_all_tables
   467         -do_execsql_test 15.1 {
   468         -  CREATE TABLE x1(a, b, UNIQUE(a, b));
   469         -  INSERT INTO x1 VALUES(1, 2);
   470         -  INSERT INTO x1 VALUES(3, 4);
   471         -  INSERT INTO x1 VALUES(5, 6);
   472         -  ANALYZE;
   473         -  INSERT INTO sqlite_stat3 VALUES(NULL, NULL, NULL, NULL, NULL, NULL);
   474         -}
   475         -db close
   476         -sqlite3 db test.db
   477         -do_execsql_test 15.2 { SELECT * FROM x1 } {1 2 3 4 5 6}
   478         -
   479         -do_execsql_test 15.3 {
   480         -  INSERT INTO sqlite_stat3 VALUES(42, 42, 42, 42, 42, 42);
   481         -}
   482         -db close
   483         -sqlite3 db test.db
   484         -do_execsql_test 15.4 { SELECT * FROM x1 } {1 2 3 4 5 6}
   485         -
   486         -do_execsql_test 15.5 {
   487         -  UPDATE sqlite_stat1 SET stat = NULL;
   488         -}
   489         -db close
   490         -sqlite3 db test.db
   491         -do_execsql_test 15.6 { SELECT * FROM x1 } {1 2 3 4 5 6}
   492         -
   493         -do_execsql_test 15.7 {
   494         -  ANALYZE;
   495         -  UPDATE sqlite_stat1 SET tbl = 'no such tbl';
   496         -}
   497         -db close
   498         -sqlite3 db test.db
   499         -do_execsql_test 15.8 { SELECT * FROM x1 } {1 2 3 4 5 6}
   500         -
   501         -do_execsql_test 15.9 {
   502         -  ANALYZE;
   503         -  UPDATE sqlite_stat3 SET neq = NULL, nlt=NULL, ndlt=NULL;
   504         -}
   505         -db close
   506         -sqlite3 db test.db
   507         -do_execsql_test 15.10 { SELECT * FROM x1 } {1 2 3 4 5 6}
   508         -
   509         -# This is just for coverage....
   510         -do_execsql_test 15.11 {
   511         -  ANALYZE;
   512         -  UPDATE sqlite_stat1 SET stat = stat || ' unordered';
   513         -}
   514         -db close
   515         -sqlite3 db test.db
   516         -do_execsql_test 15.12 { SELECT * FROM x1 } {1 2 3 4 5 6}
   517         -
   518         -#-------------------------------------------------------------------------
   519         -# Test that allocations used for sqlite_stat3 samples are included in
   520         -# the quantity returned by SQLITE_DBSTATUS_SCHEMA_USED.
   521         -#
   522         -set one [string repeat x 1000]
   523         -set two [string repeat x 2000]
   524         -do_test 16.1 {
   525         -  reset_db
   526         -  execsql {
   527         -    CREATE TABLE t1(a, UNIQUE(a));
   528         -    INSERT INTO t1 VALUES($one);
   529         -    ANALYZE;
   530         -  }
   531         -  set nByte [lindex [sqlite3_db_status db SCHEMA_USED 0] 1]
   532         -
   533         -  reset_db
   534         -  execsql {
   535         -    CREATE TABLE t1(a, UNIQUE(a));
   536         -    INSERT INTO t1 VALUES($two);
   537         -    ANALYZE;
   538         -  }
   539         -  set nByte2 [lindex [sqlite3_db_status db SCHEMA_USED 0] 1]
   540         -
   541         -  expr {$nByte2 > $nByte+950 && $nByte2 < $nByte+1050}
   542         -} {1}
   543         -
   544         -#-------------------------------------------------------------------------
   545         -# Test that stat3 data may be used with partial indexes.
   546         -#
   547         -do_test 17.1 {
   548         -  reset_db
   549         -  execsql {
   550         -    CREATE TABLE t1(a, b, c, d);
   551         -    CREATE INDEX i1 ON t1(a, b) WHERE d IS NOT NULL;
   552         -    INSERT INTO t1 VALUES(-1, -1, -1, NULL);
   553         -    INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
   554         -    INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
   555         -    INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
   556         -    INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
   557         -    INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
   558         -    INSERT INTO t1 SELECT 2*a,2*b,2*c,d FROM t1;
   559         -  }
   560         -
   561         -  for {set i 0} {$i < 32} {incr i} {
   562         -    execsql { INSERT INTO t1 VALUES($i%2, $b, $i/2, 'abc') }
   563         -  }
   564         -  execsql {ANALYZE main.t1}
   565         -} {}
   566         -
   567         -do_catchsql_test 17.1.2 {
   568         -  ANALYZE temp.t1;
   569         -} {1 {no such table: temp.t1}}
   570         -
   571         -do_eqp_test 17.2 {
   572         -  SELECT * FROM t1 WHERE d IS NOT NULL AND a=0;
   573         -} {/USING INDEX i1/}
   574         -do_eqp_test 17.3 {
   575         -  SELECT * FROM t1 WHERE d IS NOT NULL AND a=0;
   576         -} {/USING INDEX i1/}
   577         -
   578         -do_execsql_test 17.4 {
   579         -  CREATE INDEX i2 ON t1(c) WHERE d IS NOT NULL;
   580         -  ANALYZE main.i2;
   581         -}
   582         -do_eqp_test 17.5 {
   583         -  SELECT * FROM t1 WHERE d IS NOT NULL AND a=0;
   584         -} {/USING INDEX i1/}
   585         -do_eqp_test 17.6 {
   586         -  SELECT * FROM t1 WHERE d IS NOT NULL AND a=0 AND b=0 AND c=10;
   587         -} {/USING INDEX i2/}
   588         -
   589         -#-------------------------------------------------------------------------
   590         -#
   591         -do_test 18.1 {
   592         -  reset_db
   593         -  execsql {
   594         -    CREATE TABLE t1(a, b);
   595         -    CREATE INDEX i1 ON t1(a, b);
   596         -  }
   597         -  for {set i 0} {$i < 9} {incr i} {
   598         -    execsql {
   599         -      INSERT INTO t1 VALUES($i, 0);
   600         -      INSERT INTO t1 VALUES($i, 0);
   601         -      INSERT INTO t1 VALUES($i, 0);
   602         -      INSERT INTO t1 VALUES($i, 0);
   603         -      INSERT INTO t1 VALUES($i, 0);
   604         -      INSERT INTO t1 VALUES($i, 0);
   605         -      INSERT INTO t1 VALUES($i, 0);
   606         -      INSERT INTO t1 VALUES($i, 0);
   607         -      INSERT INTO t1 VALUES($i, 0);
   608         -      INSERT INTO t1 VALUES($i, 0);
   609         -      INSERT INTO t1 VALUES($i, 0);
   610         -      INSERT INTO t1 VALUES($i, 0);
   611         -      INSERT INTO t1 VALUES($i, 0);
   612         -      INSERT INTO t1 VALUES($i, 0);
   613         -      INSERT INTO t1 VALUES($i, 0);
   614         -    }
   615         -  }
   616         -  execsql ANALYZE
   617         -  execsql { SELECT count(*) FROM sqlite_stat3 }
   618         -} {9}
   619         -
   620         -#-------------------------------------------------------------------------
   621         -# For coverage.
   622         -#
   623         -ifcapable view {
   624         -  do_test 19.1 {
   625         -    reset_db 
   626         -    execsql {
   627         -      CREATE TABLE t1(x, y);
   628         -      CREATE INDEX i1 ON t1(x, y);
   629         -      CREATE VIEW v1 AS SELECT * FROM t1;
   630         -      ANALYZE;
   631         -    }
   632         -  } {}
   633         -}
   634         -ifcapable auth {
   635         -  proc authproc {op args} {
   636         -    if {$op == "SQLITE_ANALYZE"} { return "SQLITE_DENY" }
   637         -    return "SQLITE_OK"
   638         -  }
   639         -  do_test 19.2 {
   640         -    reset_db 
   641         -    db auth authproc
   642         -    execsql {
   643         -      CREATE TABLE t1(x, y);
   644         -      CREATE VIEW v1 AS SELECT * FROM t1;
   645         -    }
   646         -    catchsql ANALYZE
   647         -  } {1 {not authorized}}
   648         -}
   649         -
   650         -#-------------------------------------------------------------------------
   651         -#
   652         -reset_db
   653         -proc r {args} { expr rand() }
   654         -db func r r
   655         -db func lrange lrange
   656         -do_test 20.1 {
   657         -  execsql {
   658         -    CREATE TABLE t1(a,b,c,d);
   659         -    CREATE INDEX i1 ON t1(a,b,c,d);
   660         -  }
   661         -  for {set i 0} {$i < 16} {incr i} {
   662         -    execsql {
   663         -      INSERT INTO t1 VALUES($i, r(), r(), r());
   664         -      INSERT INTO t1 VALUES($i, $i,  r(), r());
   665         -      INSERT INTO t1 VALUES($i, $i,  $i,  r());
   666         -      INSERT INTO t1 VALUES($i, $i,  $i,  $i);
   667         -      INSERT INTO t1 VALUES($i, $i,  $i,  $i);
   668         -      INSERT INTO t1 VALUES($i, $i,  $i,  r());
   669         -      INSERT INTO t1 VALUES($i, $i,  r(), r());
   670         -      INSERT INTO t1 VALUES($i, r(), r(), r());
   671         -    }
   672         -  }
   673         -} {}
   674         -do_execsql_test 20.2 { ANALYZE }
   675         -for {set i 0} {$i<16} {incr i} {
   676         -    set val $i
   677         -    do_execsql_test 20.3.$i {
   678         -      SELECT count(*) FROM sqlite_stat3 WHERE sample=$val
   679         -    } {1}
   680         -}
   681         -
   682         -finish_test

Changes to test/auth.test.

  2446   2446           DROP TABLE v1chng;
  2447   2447         }
  2448   2448       }
  2449   2449     }
  2450   2450     ifcapable stat4 {
  2451   2451       set stat4 "sqlite_stat4 "
  2452   2452     } else {
  2453         -    ifcapable stat3 {
  2454         -      set stat4 "sqlite_stat3 "
  2455         -    } else {
  2456         -      set stat4 ""
  2457         -    }
         2453  +    set stat4 ""
  2458   2454     }
  2459   2455     do_test auth-5.2 {
  2460   2456       execsql {
  2461   2457         SELECT name FROM (
  2462   2458           SELECT * FROM sqlite_master UNION ALL SELECT * FROM temp.sqlite_master)
  2463   2459         WHERE type='table'
  2464   2460         ORDER BY name

Changes to test/colname.test.

   395    395   } {Bbb 123}
   396    396   ifcapable vtab {
   397    397     do_execsql_test colname-9.320 {
   398    398       CREATE TABLE t2 AS SELECT BBb FROM (SELECT aaa AS Bbb FROM t1);
   399    399       SELECT name FROM pragma_table_info('t2');
   400    400     } {Bbb}
   401    401   }
          402  +do_execsql_test colname-9.330 { -- added 2019-08-10 to invalidate
          403  +  DROP TABLE IF EXISTS t1;      -- a couple assert()s that were
          404  +  CREATE TABLE t1(a);           -- added by ticket 3b44500725
          405  +  INSERT INTO t1 VALUES(17),(2),(99),(-3),(7);
          406  +  SELECT (SELECT avg(a) UNION SELECT min(a) OVER()) FROM t1;
          407  +} {17}
   402    408   
   403    409   # Issue detected by OSSFuzz on 2017-12-24 (Christmas Eve)
   404    410   # caused by check-in https://sqlite.org/src/info/6b2ff26c25
   405    411   #
   406    412   # Prior to being fixed, the following CREATE TABLE was dereferencing
   407    413   # a NULL pointer and segfaulting.
   408    414   #

Changes to test/corruptL.test.

   224    224   |   4080: 01 04 04 03 08 01 13 04 03 08 01 02 03 03 08 09   ................
   225    225   | page 5 offset 16384
   226    226   |      0: 0d 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00   ................
   227    227   | end crash.txt.db
   228    228   }]} {}
   229    229   
   230    230   do_execsql_test 2.1 {
          231  +  PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking
   231    232     INSERT INTO t1(b) VALUES(X'a0fee3669f9fddefc5cba913e4225d4b6ce2b04f26b87fad3ee6f9b7d90a1ea62a169bf41e5d32707a6ca5c3d05e4bde05c9d89eaaa8c50e74333d2e9fcd7dfe95528a3a016aac1102d825c5cd70cf99d8a88e0ea7f798d4334386518b7ad359beb168b93aba059a2a3bd93112d65b44c12b9904ea786b204d80531cdf0504bf9b203dbe927061974caf7b9f30cbc3397b61f802e732012a6663d41c3607d6f1c0dbcfd489adac05ca500c0b04439d894cd93a840159225ef73b627e178b9f84b3ffe66cf22a963a8368813ff7961fc47f573211ccec95e0220dcbb3bf429f4a50ba54d7a53784ac51bfef346e6ac8ae0d0e7c3175946e62ba2b');
   232    233   }
   233    234   
   234    235   do_catchsql_test 2.2 {
   235    236     SELECT b,c FROM t1 ORDER BY a;
   236    237   } {1 {database disk image is malformed}}
   237    238   
................................................................................
   373    374   |    464: 05 01 01 09 09 02 02 19 04 05 17 17 17 17 10 65   ...............e
   374    375   |    480: 76 65 6e 65 69 67 68 74 65 40 18 00 00 00 00 01   veneighte@......
   375    376   |    496: 02 03 07 04 01 01 01 03 04 02 05 04 09 01 ff fd   ................
   376    377   | end crash-6b48ba69806134.db
   377    378   }]} {}
   378    379   
   379    380   do_catchsql_test 4.1 {
          381  +  PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking
   380    382     INSERT INTO t3 SELECT * FROM t2;
   381    383   } {1 {database disk image is malformed}}
   382    384   
   383    385   
   384    386   #-------------------------------------------------------------------------
   385    387   reset_db
   386    388   do_test 5.0 {
................................................................................
   601    603   |   3808: 05 43 52 45 41 54 45 20 49 4e 44 45 58 20 74 31   .CREATE INDEX t1
   602    604   |   3824: 62 20 4f 4e 20 74 31 28 62 29 50 03 06 17 2b 2b   b ON t1(b)P...++
   603    605   |   3840: 01 59 74 61 62 6c 65 73 71 6c 69 74 65 5f 73 65   .Ytablesqlite_se
   604    606   |   3856: 71 75 65 6e 63 65 73 71 6c 69 74 65 5f 73 65 71   quencesqlite_seq
   605    607   |   3872: 75 65 6e 63 65 04 43 52 45 41 54 45 20 54 41 42   uence.CREATE TAB
   606    608   |   3888: 4c 45 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e   LE sqlite_sequen
   607    609   |   3904: 63 65 28 6e 61 6d 65 2c 73 65 71 29 81 04 01 07   ce(name,seq)....
   608         -|   3920: 17 11 11 01 81 73 74 61 c2 6c 65 74 31 74 31 02   .....sta.let1t1.
          610  +|   3920: 17 11 11 01 81 73 74 61 62 6c 65 74 31 74 31 02   .....stablet1t1.
   609    611   |   3936: 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28   CREATE TABLE t1(
   610    612   |   3952: 61 20 52 45 41 4c 20 4e 4f 54 20 4e 55 4c 4c 20   a REAL NOT NULL 
   611    613   |   3968: 44 45 46 41 55 4c 54 28 32 35 2b 33 32 29 2c 62   DEFAULT(25+32),b
   612    614   |   3984: 20 46 4c 4f 41 54 2c 63 20 44 4f 55 42 4c 45 20    FLOAT,c DOUBLE 
   613    615   |   4000: 55 4e 49 51 55 45 2c 0a 64 20 43 4c 4f 42 2c 65   UNIQUE,.d CLOB,e
   614    616   |   4016: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59    INTEGER PRIMARY
   615    617   |   4032: 20 4b 45 59 20 41 55 54 4f 49 4e 43 52 45 4d 45    KEY AUTOINCREME
................................................................................
   831    833   | page 4 offset 1536
   832    834   |      0: 0d 00 39 00 00 02 00 00 00 00 00 00 00 00 00 00   ..9.............
   833    835   | end a.db
   834    836   }]} {}
   835    837   
   836    838   
   837    839   do_catchsql_test 8.1 {
          840  +  PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking
   838    841     INSERT INTO t3 SELECT * FROM t2;
   839    842   } {1 {database disk image is malformed}}
   840    843   
   841    844   #-------------------------------------------------------------------------
   842    845   reset_db
   843    846   do_test 9.0 {
   844    847     sqlite3 db {}
................................................................................
  1000   1003   |   2512: 00 00 00 00 00 00 00 96 00 00 00 00 00 00 00 00   ................
  1001   1004   | page 44 offset 176128
  1002   1005   |   2512: 00 00 00 00 00 00 00 00 aa 00 00 00 00 00 00 00   ................
  1003   1006   | end crash-41390d95d613b6.db
  1004   1007   }]} {}
  1005   1008   
  1006   1009   do_catchsql_test 10.1 {
         1010  +  PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking
  1007   1011     SELECT * FROM t1 WHERE a<='2019-05-09' ORDER BY a DESC;
  1008   1012   } {1 {database disk image is malformed}}
  1009   1013   
  1010   1014   
  1011   1015   #-------------------------------------------------------------------------
  1012   1016   reset_db
  1013   1017   do_test 11.0 {
................................................................................
  1053   1057   |     48: 3f 69 33 74 6e 65 78 78 74 64 33 ff 43 52 45 a0   ?i3tnexxtd3.CRE.
  1054   1058   |     64: a0 a0 a0 a0 a0 a0 a0 a0 a0 a0 a0 a0 74 13 11 01   ............t...
  1055   1059   |     80: 49 45 74 00 00 00 00 00 00 00 00 00 00 00 00 00   IEt.............
  1056   1060   | end x.db
  1057   1061   }]} {}
  1058   1062   
  1059   1063   do_catchsql_test 11.1 {
         1064  +  PRAGMA writable_schema=ON; -- bypass improved sqlite_master consistency checking
  1060   1065     DELETE FROM t3 WHERE x IN (SELECT x FROM t4);
  1061   1066   } {1 {database disk image is malformed}}
  1062   1067   
  1063   1068   finish_test

Added test/corruptM.test.

            1  +# 2019-08-12
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# Check to ensure that the type, name, and tbl_name fields of the
           13  +# sqlite_master table are validated and errors are reported if they
           14  +# are inconsistent with the sql.
           15  +#
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +set testprefix corruptM
           20  +
           21  +# These tests deal with corrupt database files
           22  +#
           23  +database_may_be_corrupt
           24  +
           25  +db close
           26  +forcedelete test.db
           27  +sqlite3 db test.db
           28  +do_execsql_test corruptM-100 {
           29  +  CREATE TABLE t1(a,b,c);
           30  +  INSERT INTO t1 VALUES(111,222,333);
           31  +  CREATE INDEX i1 ON t1(b);
           32  +  CREATE VIEW v2 AS SELECT 15,22;
           33  +  CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT 5; END;
           34  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
           35  +} {table t1 t1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
           36  +do_execsql_test corruptM-101 {
           37  +  PRAGMA writable_schema=on;
           38  +  UPDATE sqlite_master SET tbl_name=NULL WHERE name='t1';
           39  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
           40  +} {table t1 {} | index i1 t1 | view v2 v2 | trigger r1 t1 |}
           41  +sqlite3 db2 test.db
           42  +do_test corruptM-102 {
           43  +  catchsql {
           44  +    PRAGMA quick_check;
           45  +  } db2
           46  +} {1 {malformed database schema (t1)}}
           47  +db2 close
           48  +
           49  +do_execsql_test corruptM-110 {
           50  +  UPDATE sqlite_master SET tbl_name='tx' WHERE name='t1';
           51  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
           52  +} {table t1 tx | index i1 t1 | view v2 v2 | trigger r1 t1 |}
           53  +sqlite3 db2 test.db
           54  +do_test corruptM-111 {
           55  +  catchsql {
           56  +    PRAGMA quick_check;
           57  +  } db2
           58  +} {1 {malformed database schema (t1)}}
           59  +db2 close
           60  +do_execsql_test corruptM-112 {
           61  +  UPDATE sqlite_master SET tbl_name='t1', type='tabl' WHERE name='t1';
           62  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
           63  +} {tabl t1 t1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
           64  +sqlite3 db2 test.db
           65  +do_test corruptM-113 {
           66  +  catchsql {
           67  +    PRAGMA quick_check;
           68  +  } db2
           69  +} {1 {malformed database schema (t1)}}
           70  +db2 close
           71  +do_execsql_test corruptM-114 {
           72  +  UPDATE sqlite_master SET tbl_name='t9',type='table',name='t9'WHERE name='t1';
           73  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
           74  +} {table t9 t9 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
           75  +sqlite3 db2 test.db
           76  +do_test corruptM-114 {
           77  +  catchsql {
           78  +    PRAGMA quick_check;
           79  +  } db2
           80  +} {1 {malformed database schema (t9)}}
           81  +db2 close
           82  +
           83  +do_execsql_test corruptM-120 {
           84  +  UPDATE sqlite_master SET name='t1',tbl_name='T1' WHERE name='t9';
           85  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
           86  +} {table t1 T1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
           87  +sqlite3 db2 test.db
           88  +do_test corruptM-121 {
           89  +  catchsql {
           90  +    PRAGMA quick_check;
           91  +    SELECT * FROM t1, v2;
           92  +  } db2
           93  +} {0 {ok 111 222 333 15 22}}
           94  +db2 close
           95  +
           96  +do_execsql_test corruptM-130 {
           97  +  UPDATE sqlite_master SET type='view' WHERE name='t1';
           98  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
           99  +} {view t1 T1 | index i1 t1 | view v2 v2 | trigger r1 t1 |}
          100  +sqlite3 db2 test.db
          101  +do_test corruptM-131 {
          102  +  catchsql {
          103  +    PRAGMA quick_check;
          104  +    SELECT * FROM t1, v2;
          105  +  } db2
          106  +} {1 {malformed database schema (t1)}}
          107  +db2 close
          108  +
          109  +do_execsql_test corruptM-140 {
          110  +  UPDATE sqlite_master SET type='table', tbl_name='t1' WHERE name='t1';
          111  +  UPDATE sqlite_master SET tbl_name='tx' WHERE name='i1';
          112  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
          113  +} {table t1 t1 | index i1 tx | view v2 v2 | trigger r1 t1 |}
          114  +sqlite3 db2 test.db
          115  +do_test corruptM-141 {
          116  +  catchsql {
          117  +    PRAGMA quick_check;
          118  +    SELECT * FROM t1, v2;
          119  +  } db2
          120  +} {1 {malformed database schema (i1)}}
          121  +db2 close
          122  +
          123  +do_execsql_test corruptM-150 {
          124  +  UPDATE sqlite_master SET type='table', tbl_name='t1' WHERE name='i1';
          125  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
          126  +} {table t1 t1 | table i1 t1 | view v2 v2 | trigger r1 t1 |}
          127  +sqlite3 db2 test.db
          128  +do_test corruptM-151 {
          129  +  catchsql {
          130  +    PRAGMA quick_check;
          131  +    SELECT * FROM t1, v2;
          132  +  } db2
          133  +} {1 {malformed database schema (i1)}}
          134  +db2 close
          135  +
          136  +do_execsql_test corruptM-160 {
          137  +  UPDATE sqlite_master SET type='view', tbl_name='t1' WHERE name='i1';
          138  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
          139  +} {table t1 t1 | view i1 t1 | view v2 v2 | trigger r1 t1 |}
          140  +sqlite3 db2 test.db
          141  +do_test corruptM-161 {
          142  +  catchsql {
          143  +    PRAGMA quick_check;
          144  +    SELECT * FROM t1, v2;
          145  +  } db2
          146  +} {1 {malformed database schema (i1)}}
          147  +db2 close
          148  +
          149  +do_execsql_test corruptM-170 {
          150  +  UPDATE sqlite_master SET type='index', tbl_name='t1' WHERE name='i1';
          151  +  UPDATE sqlite_master SET type='table', tbl_name='v2' WHERE name='v2';
          152  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
          153  +} {table t1 t1 | index i1 t1 | table v2 v2 | trigger r1 t1 |}
          154  +sqlite3 db2 test.db
          155  +do_test corruptM-171 {
          156  +  catchsql {
          157  +    PRAGMA quick_check;
          158  +    SELECT * FROM t1, v2;
          159  +  } db2
          160  +} {1 {malformed database schema (v2)}}
          161  +db2 close
          162  +
          163  +do_execsql_test corruptM-180 {
          164  +  UPDATE sqlite_master SET type='view',name='v3',tbl_name='v3' WHERE name='v2';
          165  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
          166  +} {table t1 t1 | index i1 t1 | view v3 v3 | trigger r1 t1 |}
          167  +sqlite3 db2 test.db
          168  +do_test corruptM-181 {
          169  +  catchsql {
          170  +    PRAGMA quick_check;
          171  +    SELECT * FROM t1, v2;
          172  +  } db2
          173  +} {1 {malformed database schema (v3)}}
          174  +db2 close
          175  +
          176  +do_execsql_test corruptM-190 {
          177  +  UPDATE sqlite_master SET type='view',name='v2',tbl_name='v2' WHERE name='v3';
          178  +  UPDATE sqlite_master SET type='view' WHERE name='r1';
          179  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
          180  +} {table t1 t1 | index i1 t1 | view v2 v2 | view r1 t1 |}
          181  +sqlite3 db2 test.db
          182  +do_test corruptM-191 {
          183  +  catchsql {
          184  +    PRAGMA quick_check;
          185  +    SELECT * FROM t1, v2;
          186  +  } db2
          187  +} {1 {malformed database schema (r1)}}
          188  +db2 close
          189  +do_execsql_test corruptM-192 {
          190  +  UPDATE sqlite_master SET type='trigger',tbl_name='v2' WHERE name='r1';
          191  +  SELECT type, name, tbl_name, '|' FROM sqlite_master;
          192  +} {table t1 t1 | index i1 t1 | view v2 v2 | trigger r1 v2 |}
          193  +sqlite3 db2 test.db
          194  +do_test corruptM-193 {
          195  +  catchsql {
          196  +    PRAGMA quick_check;
          197  +    SELECT * FROM t1, v2;
          198  +  } db2
          199  +} {1 {malformed database schema (r1)}}
          200  +db2 close
          201  +
          202  +finish_test

Changes to test/dbfuzz001.test.

   301    301   |    320: 41 54 45 20 49 4e 44 45 58 20 74 33 78 20 4f 4e   ATE INDEX t3x ON
   302    302   |    336: 20 74 33 28 78 29 2e 04 06 17 15 11 01 45 69 6e    t3(x).......Ein
   303    303   |    352: 64 65 78 74 32 63 64 74 32 05 43 52 45 41 54 45   dext2cdt2.CREATE
   304    304   |    368: 20 49 4e 44 45 58 20 74 32 63 64 20 4f 4e 20 74    INDEX t2cd ON t
   305    305   |    384: 32 28 63 2c 64 29 28 05 06 17 11 11 01 3d 74 61   2(c,d)(......=ta
   306    306   |    400: 62 6c 65 74 33 74 33 07 43 52 45 41 54 45 20 54   blet3t3.CREATE T
   307    307   |    416: 41 42 4c 45 20 74 33 28 63 2c 78 2c 65 2c 66 29   ABLE t3(c,x,e,f)
   308         -|    432: 28 02 06 17 11 11 01 3d 74 61 74 65 6c 03 62 74   (......=tatel.bt
          308  +|    432: 28 02 06 17 11 11 01 3d 74 61 62 6c 65 74 32 74   (......=tablet2t
   309    309   |    448: 32 32 43 52 45 41 54 45 20 54 41 42 4c 45 20 74   22CREATE TABLE t
   310    310   |    464: 32 28 63 2c 64 2c 65 2c 66 29 24 01 06 17 11 11   2(c,d,e,f)$.....
   311    311   |    480: 01 35 74 61 62 6c 65 74 31 74 31 02 43 52 45 41   .5tablet1t1.CREA
   312    312   |    496: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 62 29   TE TABLE t1(a,b)
   313    313   | page 2 offset 512
   314    314   |      0: 0d 00 00 00 04 01 cf 00 01 fa 01 f3 01 de 01 cf   ................
   315    315   |    160: 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00   .. .............

Changes to test/dbfuzz2.c.

   207    207   #endif
   208    208     if( bVdbeDebug ){
   209    209       sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
   210    210     }
   211    211     if( mxCb>0 ){
   212    212       sqlite3_progress_handler(db, 10, progress_handler, 0);
   213    213     }
          214  +#ifdef SQLITE_TESTCTRL_PRNG_SEED
          215  +  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db);
          216  +#endif
   214    217     for(i=0; i<sizeof(azSql)/sizeof(azSql[0]); i++){
   215    218       if( eVerbosity>=1 ){
   216    219         printf("%s\n", azSql[i]);
   217    220         fflush(stdout);
   218    221       }
   219    222       zErr = 0;
   220    223       nCb = 0;

Changes to test/dbstatus.test.

    59     59   
    60     60   proc lookaside {db} {
    61     61     expr { $::lookaside_buffer_size *
    62     62       [lindex [sqlite3_db_status $db SQLITE_DBSTATUS_LOOKASIDE_USED 0] 1]
    63     63     }
    64     64   }
    65     65   
    66         -ifcapable stat4||stat3 {
           66  +ifcapable stat4 {
    67     67     set STAT3 1
    68     68   } else {
    69     69     set STAT3 0
    70     70   }
    71     71   
    72     72   #---------------------------------------------------------------------------
    73     73   # Run the dbstatus-2 and dbstatus-3 tests with several of different

Changes to test/distinct2.test.

   224    224     INSERT INTO t1(a, b) VALUES(1, 'yes');
   225    225     CREATE TABLE t2(x PRIMARY KEY);
   226    226     INSERT INTO t2 VALUES('yes');
   227    227     SELECT DISTINCT a FROM t1, t2 WHERE x=b;
   228    228     ANALYZE;
   229    229     SELECT DISTINCT a FROM t1, t2 WHERE x=b;
   230    230   } {1 1}
          231  +
          232  +#-------------------------------------------------------------------------
          233  +reset_db
          234  +
          235  +do_execsql_test 2000 {
          236  +  CREATE TABLE t0 (c0, c1, c2, PRIMARY KEY (c0, c1));
          237  +  CREATE TABLE t1 (c2);
          238  +  INSERT INTO t0(c2) VALUES (0),(1),(3),(4),(5),(6),(7),(8),(9),(10),(11);
          239  +  INSERT INTO t0(c1) VALUES ('a');
          240  +  INSERT INTO t1(c2) VALUES (0);
          241  +}
          242  +do_execsql_test 2010 {
          243  +  SELECT DISTINCT t0.c0, t1._rowid_, t0.c1 FROM t1 CROSS JOIN t0 ORDER BY t0.c0;
          244  +} {{} 1 {} {} 1 a}
          245  +do_execsql_test 1.2 {
          246  +  ANALYZE;
          247  +}
          248  +do_execsql_test 2020 {
          249  +  SELECT DISTINCT t0.c0, t1._rowid_, t0.c1 FROM t1 CROSS JOIN t0 ORDER BY t0.c0;
          250  +} {{} 1 {} {} 1 a}
          251  +
          252  +
          253  +do_execsql_test 2030 {
          254  +  CREATE TABLE t2(a, b, c);
          255  +  CREATE INDEX t2ab ON t2(a, b);
          256  +  
          257  +  WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<64)
          258  +    INSERT INTO t2 SELECT 'one', i%2, 'one' FROM c;
          259  +
          260  +  WITH c(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM c WHERE i<64)
          261  +    INSERT INTO t2 SELECT 'two', i%2, 'two' FROM c;
          262  +
          263  +  CREATE TABLE t3(x INTEGER PRIMARY KEY);
          264  +  INSERT INTO t3 VALUES(1);
          265  +
          266  +  ANALYZE;
          267  +}
          268  +do_execsql_test 2040 {
          269  +  SELECT DISTINCT a, b, x FROM t3 CROSS JOIN t2 ORDER BY a; 
          270  +} {
          271  +  one 0 1
          272  +  one 1 1
          273  +  two 0 1
          274  +  two 1 1
          275  +}
          276  +
   231    277   
   232    278   
   233    279   finish_test

Changes to test/filter1.test.

    98     98   } {1 {misuse of window function max()}}
    99     99   
   100    100   do_catchsql_test 2.3 {
   101    101     SELECT sum(a) FILTER (WHERE 1 - count(a)) FROM t1
   102    102   } {1 {misuse of aggregate function count()}}
   103    103   
   104    104   finish_test
   105         -
   106         -

Changes to test/fkey8.test.

   225    225     COMMIT;
   226    226   }
   227    227   do_execsql_test 5.3 {
   228    228     PRAGMA integrity_check;
   229    229   } {ok}
   230    230   
   231    231   finish_test
   232         -

Changes to test/fts3corrupt4.test.

    22     22   # If SQLITE_ENABLE_FTS3 is defined, omit this file.
    23     23   ifcapable !fts3 {
    24     24     finish_test
    25     25     return
    26     26   }
    27     27   
    28     28   sqlite3_fts3_may_be_corrupt 1
           29  +database_may_be_corrupt
    29     30   
    30     31   do_execsql_test 1.0 {
    31     32     BEGIN;
    32     33       CREATE VIRTUAL TABLE ft USING fts3;
    33     34       INSERT INTO ft VALUES('aback');
    34     35       INSERT INTO ft VALUES('abaft');
    35     36       INSERT INTO ft VALUES('abandon');
................................................................................
  5090   5091       INSERT INTO t1(a) SELECT randomblob(2829) FROM c;
  5091   5092   } {0 {}}
  5092   5093   
  5093   5094   do_catchsql_test 28.1 {
  5094   5095     INSERT INTO t1(t1) SELECT x FROM t2;
  5095   5096   } {0 {}}
  5096   5097   
  5097         -finish_test
         5098  +#-------------------------------------------------------------------------
         5099  +#
         5100  +reset_db
         5101  +do_test 29.0 {
         5102  +  sqlite3 db {}
         5103  +  db deserialize [decode_hexdb {
         5104  +.open --hexdb
         5105  +| size 28672 pagesize 4096 filename crash-53f41622dd3bf6.db
         5106  +| page 1 offset 0
         5107  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         5108  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
         5109  +|     96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21   ...............!
         5110  +|    112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00   .....~..........
         5111  +|   3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c   ...........1tabl
         5112  +|   3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42   et2t2.CREATE TAB
         5113  +|   3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01   LE t2(x).3......
         5114  +|   3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 54 69 72   .5tablet1_segTir
         5115  +|   3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45   t1_segdir.CREATE
         5116  +|   3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69    TABLE 't1_segdi
         5117  +|   3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52   r'(level INTEGER
         5118  +|   3600: 2c 69 64 78 20 49 4d 54 45 47 45 52 2c 73 74 61   ,idx IMTEGER,sta
         5119  +|   3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52   rt_block INTEGER
         5120  +|   3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63   ,leaves_end_bloc
         5121  +|   3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c   k INTEGER,end_bl
         5122  +|   3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74   ock INTEGER,root
         5123  +|   3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45    BLOB,PRIMARY KE
         5124  +|   3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06   Y(level, idx))1.
         5125  +|   3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74   ..E...indexsqlit
         5126  +|   3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73   e_autoindex_t1_s
         5127  +|   3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72   egdir_1t1_segdir
         5128  +|   3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01   .........f...##.
         5129  +|   3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e   ..tablet1_segmen
         5130  +|   3792: 74 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52   tst1_segments.CR
         5131  +|   3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73   EATE TABLE 't1_s
         5132  +|   3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64   egments'(blockid
         5133  +|   3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59    INTEGER PRIMARY
         5134  +|   3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42    KEY, block BLOB
         5135  +|   3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74   )j...!!...tablet
         5136  +|   3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74   1_contentt1_cont
         5137  +|   3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45   ent.CREATE TABLE
         5138  +|   3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f    't1_content'(do
         5139  +|   3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 39 4d   cid INTEGER PR9M
         5140  +|   3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20   ARY KEY, 'c0a', 
         5141  +|   3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 12 06   'c1b', 'c2c')8..
         5142  +|   3984: 17 11 11 08 5f 74 61 6b 3c 65 74 31 74 31 43 52   ...._tak<et1t1CR
         5143  +|   4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42   EATE VIRTUAL TAB
         5144  +|   4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33   LE t1 USING fts3
         5145  +|   4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00   (a,b,c).........
         5146  +| page 3 offset 8192
         5147  +|      0: 0d 00 00 00 25 0b 48 00 0f d8 0f af 0f 86 0f 74   ....%.H........t
         5148  +|     16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5   .a.N./..........
         5149  +|     32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5   ...t.[.@.$......
         5150  +|     48: 0d bb 0d a0 0d 84 03 28 0d 4f 0d 35 0d 1b 0c fb   .......(.O.5....
         5151  +|     64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a   .......x.W.>.$..
         5152  +|     80: 0b 48 00 00 00 00 00 00 00 00 00 00 00 00 00 00   .H..............
         5153  +|   2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00   .........?%..r..
         5154  +|   2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e   .COMPILER=gcc-5.
         5155  +|   2912: 34 2e 30 20 32 30 31 36 30 36 30 39 21 44 45 42   4.0 20160609!DEB
         5156  +|   2928: 55 47 20 45 4e 41 42 4c 45 20 44 42 53 54 41 54   UG ENABLE DBSTAT
         5157  +|   2944: 20 56 54 41 42 20 45 4e 41 42 4c 46 20 46 54 53    VTAB ENABLF FTS
         5158  +|   2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e   4 ENABLE FTS5 EN
         5159  +|   2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41   ABLE GEOPOLY ENA
         5160  +|   2992: 42 4c 55 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45   BLU JSON1 ENABLE
         5161  +|   3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20    MEMSYS5 ENABLE 
         5162  +|   3024: 52 54 52 45 45 56 4d 41 58 20 4d 45 4d 4f 52 59   RTREEVMAX MEMORY
         5163  +|   3040: 3d 35 30 30 30 30 30 30 30 20 4f 4d 49 54 20 4c   =50000000 OMIT L
         5164  +|   3056: 4f 42 43 20 45 58 54 45 4e 53 49 4f 4e 20 54 48   OBC EXTENSION TH
         5165  +|   3072: 52 45 41 44 53 41 46 45 3d 40 18 24 05 00 25 0f   READSAFE=@.$..%.
         5166  +|   3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49   .THREADSAFE=0XBI
         5167  +|   3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41   NARY.#..%..THREA
         5168  +|   3120: 44 53 41 46 45 3d 31 58 4e 4f 43 41 53 45 17 22   DSAFE=1XNOCASE..
         5169  +|   3136: 05 00 25 0f 17 54 48 52 45 41 44 43 41 46 45 3d   ..%..THREADCAFE=
         5170  +|   3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d   0XRTRIM.!..3..OM
         5171  +|   3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f   IT LOAD EXTENSIO
         5172  +|   3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f   NXBINARY. ..3..O
         5173  +|   3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 48   MIT LOAD EXTENSH
         5174  +|   3216: cf 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17   .NXNOCASE....3..
         5175  +|   3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53   OMIT LOAD EXTENS
         5176  +|   3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19   IONXRTRIM....3..
         5177  +|   3264: 4d 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30 30 30   MAX MEMORY-50000
         5178  +|   3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f   000XBINARY....3.
         5179  +|   3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30   .MAX MEMORY=5000
         5180  +|   3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33   0000XNOCASE....3
         5181  +|   3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30   ..MAX MEMORY=500
         5182  +|   3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25   00000XRTRIM....%
         5183  +|   3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42   ..ENABLE RTREEXB
         5184  +|   3376: 49 4e 41 52 49 18 1a 05 0d a5 0f 19 45 4e 41 42   INARI.......ENAB
         5185  +|   3392: 4c 45 20 52 54 52 45 45 58 4e 4f be 31 53 45 17   LE RTREEXNO.1SE.
         5186  +|   3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 51   ...%..ENABLE RTQ
         5187  +|   3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45   EEXRTRIM....)..E
         5188  +|   3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49   NABLE MEMSYS5XBI
         5189  +|   3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         5190  +|   3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45   E MEMSYS5XNOCASE
         5191  +|   3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45   ....)..ENABLE ME
         5192  +|   3504: 4d 53 59 53 37 f8 52 54 52 49 4d 18 14 05 00 25   MSYS7.RTRIM....%
         5193  +|   3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42   ..ENABLE JSON1XB
         5194  +|   3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         5195  +|   3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17   LE JSO>1XNOCASE.
         5196  +|   3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f   ...%..ENABLE JSO
         5197  +|   3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45   N1XRTRIM....)..E
         5198  +|   3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49   NABLE GEOPOLYXBI
         5199  +|   3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         5200  +|   3632: 48 c0 47 45 4f 50 4f 4c 40 58 4e 4f 43 41 53 45   H.GEOPOL@XNOCASE
         5201  +|   3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45   ....)..ENABLE GE
         5202  +|   3664: 4f 50 4f 4c 59 58 52 54 51 49 4d 17 0f 05 00 23   OPOLYXRTQIM....#
         5203  +|   3680: 0f 19 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49   ..ENABLE FTS5XBI
         5204  +|   3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c   NARY....#..ENABL
         5205  +|   3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05   E FTS5XNOCASE...
         5206  +|   3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58   .#..ENABLE FTS5X
         5207  +|   3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42   RTRIM....#..ENAB
         5208  +|   3760: 4c 45 20 46 54 53 34 58 42 49 4d 41 52 59 17 0b   LE FTS4XBIMARY..
         5209  +|   3776: 05 00 23 0f 19 45 4e 31 42 4c 45 20 46 1a 53 34   ..#..EN1BLE F.S4
         5210  +|   3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e   XNOCASE....#..EN
         5211  +|   3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e   ABLE FTS4XRTRIM.
         5212  +|   3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         5213  +|   3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e   TAT VTABXBINARY.
         5214  +|   3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         5215  +|   3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d   TAT VTABXNOCASE.
         5216  +|   3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         5217  +|   3904: 54 96 54 20 56 54 41 42 58 52 54 52 49 4d 11 06   T.T VTABXRTRIM..
         5218  +|   3920: 05 00 17 0f 1e e4 45 42 55 47 58 42 49 4e 41 52   ......EBUGXBINAR
         5219  +|   3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f   Y.......DEBUGXNO
         5220  +|   3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47   CASE.......DEBUG
         5221  +|   3968: 58 52 54 52 49 4d 27 03 05 01 43 0f 19 43 4f 4d   XRTRIM'...C..COM
         5222  +|   3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20   PILER=gcc-5.4.0 
         5223  +|   4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27   20160609XBINARY'
         5224  +|   4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67   ...C..COMPILER=g
         5225  +|   4032: 63 63 2d 35 2e 34 2e 30 40 32 30 31 36 30 36 30   cc-5.4.0@2016060
         5226  +|   4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43   9XNOCASE&...C..C
         5227  +|   4064: 4f 4d 4f 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e   OMOILER=gcc-5.4.
         5228  +|   4080: 30 20 32 30 31 36 30 36 30 39 58 52 54 52 49 4d   0 20160609XRTRIM
         5229  +| page 4 offset 12288
         5230  +|      0: 0d 00 00 01 00 10 00 00 00 00 00 00 00 00 00 00   ................
         5231  +| page 5 offset 16384
         5232  +|      0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00   ................
         5233  +|   2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33   ...........0 253
         5234  +|   2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36   ..0.%.....201606
         5235  +|   3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35   09.%....4.%....5
         5236  +|   3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a   .%....0000000.%.
         5237  +|   3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00   ...compiler.%...
         5238  +|   3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75   .dbstat.%....ebu
         5239  +|   3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09   g.%....enable.%.
         5240  +|   3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f   .........xtensio
         5241  +|   3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03   n.%....fts4.%...
         5242  +|   3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01   .5.%....gcc.%...
         5243  +|   3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f   .eopoly.%....jso
         5244  +|   3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00   n1.%....load.%..
         5245  +|   3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6d 6f 72 79   ..max.%....emory
         5246  +|   3184: 03 25 19 00 03 04 73 79 73 4d 03 25 15 00 00 04   .%....sysM.%....
         5247  +|   3200: 6e 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03   nmit.%....rtree.
         5248  +|   3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03   %....threadsafe.
         5249  +|   3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01   %....vtab.%...P.
         5250  +|   3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30   ........0 835..0
         5251  +|   3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 00   ................
         5252  +|   3280: 01 03 00 00 08 32 30 31 36 30 36 30 39 09 01 bd   .....20160609...
         5253  +|   3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05   .........4......
         5254  +|   3312: 00 01 06 00 00 01 35 09 01 04 00 01 04 00 02 04   ......5.........
         5255  +|   3328: 00 01 07 30 30 e6 30 30 30 30 09 1c 04 00 01 04   ...00.0000......
         5256  +|   3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02   ......binary<...
         5257  +|   3360: 02 00 03 01 02 02 00 04 01 02 02 10 03 01 02 02   ................
         5258  +|   3376: 00 0f 71 02 12 00 03 01 02 02 00 03 01 65 02 00   ..q..........e..
         5259  +|   3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03   ................
         5260  +|   3408: 01 0d a2 00 03 01 02 02 00 00 08 63 3b 6d 70 69   ...........c;mpi
         5261  +|   3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 64   ler............d
         5262  +|   3440: 62 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01   bstat...........
         5263  +|   3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00   .ebug...........
         5264  +|   3472: 06 65 6e 61 62 6c 65 3f 07 02 00 01 02 00 01 02   .enable?........
         5265  +|   3488: 00 01 02 00 01 02 00 01 01 f0 01 02 00 01 02 00   ................
         5266  +|   3504: 01 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01   ................
         5267  +|   3520: 02 00 01 02 00 01 02 00 01 02 00 01 02 00 01 02   ................
         5268  +|   3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f   .........xtensio
         5269  +|   3552: 6e 09 1f 04 00 01 04 00 01 04 00 00 04 66 74 73   n............fts
         5270  +|   3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d   4............5..
         5271  +|   3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03   ..........gcc...
         5272  +|   3600: 00 01 03 00 01 03 00 01 06 65 6f 70 6f 6c 79 09   .........eopoly.
         5273  +|   3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31   ...........json1
         5274  +|   3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64   ............load
         5275  +|   3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09   ............max.
         5276  +|   3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79   ...........emory
         5277  +|   3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35   ............sys5
         5278  +|   3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61   ............noca
         5279  +|   3712: 73 65 3c 02 01 02 02 00 03 01 02 02 00 03 01 02   se<.............
         5280  +|   3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         5281  +|   3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 01 f0   ................
         5282  +|   3760: 03 01 02 02 05 93 01 02 02 00 03 01 02 02 00 00   ................
         5283  +|   3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00   .omit...........
         5284  +|   3792: 05 72 8a 72 65 65 09 19 03 00 01 03 00 11 03 00   .r.ree..........
         5285  +|   3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03   ..im<...........
         5286  +|   3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01   ................
         5287  +|   3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02   ................
         5288  +|   3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         5289  +|   3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02   ...threadsafe...
         5290  +|   3888: 00 01 02 00 01 02 00 00 04 76 75 61 62 09 07 04   .........vuab...
         5291  +|   3904: 00 01 04 00 01 04 00 00 61 78 b4 01 01 01 01 02   ........ax......
         5292  +|   3920: 00 01 01 01 02 00 00 f1 01 02 00 01 01 01 02 00   ................
         5293  +|   3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01   ................
         5294  +|   3952: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01   ................
         5295  +|   3968: 01 02 00 01 01 01 01 ff 01 01 01 02 00 01 01 01   ................
         5296  +|   3984: 02 00 01 01 01 02 00 01 01 01 02 09 01 01 01 02   ................
         5297  +|   4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00   ................
         5298  +|   4016: 01 01 01 02 00 01 02 01 02 00 01 01 01 02 00 01   ................
         5299  +|   4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01   ................
         5300  +|   4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01   ................
         5301  +|   4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02   ................
         5302  +|   4080: 00 01 01 11 02 00 01 01 01 02 00 01 01 01 02 00   ................
         5303  +| page 6 offset 20480
         5304  +|      0: 0a 00 00 00 02 0f f5 00 0f fb 1f f5 00 00 00 00   ................
         5305  +|   4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09   ................
         5306  +| page 7 offset 24576
         5307  +|      0: 0d 00 00 00 05 0f b8 00 0f f4 0f e9 10 d6 0f c7   ................
         5308  +|   4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f   ...........#auto
         5309  +|   4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65   merge=5...#merge
         5310  +|   4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00   =1..............
         5311  +| end crash-53f41622dd3bf6.db
         5312  +}]} {}
         5313  +
         5314  +do_catchsql_test 29.1 {
         5315  +  INSERT INTO t1(a) SELECT X'819192E578DE3F';
         5316  +  UPDATE t1 SET b=quote(zeroblob(current_date)) WHERE t1 MATCH 't*';
         5317  +  INSERT INTO t1(b) VALUES(x'78');
         5318  +  INSERT INTO t1(t1) SELECT x FROM t2;
         5319  +} {1 {database disk image is malformed}}
  5098   5320   
         5321  +finish_test

Changes to test/fts3corrupt5.test.

    53     53     if {$bCorrupt} { set res {1 {database disk image is malformed}}}
    54     54     do_catchsql_test 1.3.$tn.2 {
    55     55       SELECT * FROM ft WHERE ft MATCH $q
    56     56     } $res
    57     57   }
    58     58   
    59     59   finish_test
    60         -

Changes to test/fts3expr5.test.

    60     60     test_fts3expr {(a:123)(b:234)(c:456)}
    61     61   } {AND {AND {PHRASE 0 0 123} {PHRASE 1 0 234}} {PHRASE 2 0 456}}
    62     62   do_test 2.2 {
    63     63     list [catch { test_fts3expr {"123" AND ( )} } msg] $msg
    64     64   } {1 {Error parsing expression}}
    65     65   
    66     66   finish_test
    67         -

Changes to test/fts4rename.test.

    37     37   
    38     38   do_catchsql_test 1.3 {
    39     39     ROLLBACK;
    40     40     DROP TABLE t1;
    41     41   } {0 {}}
    42     42   
    43     43   finish_test
    44         -

Changes to test/fuzzcheck.c.

  1802   1802             setAlarm(iTimeout);
  1803   1803   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  1804   1804             if( sqlFuzz || vdbeLimitFlag ){
  1805   1805               sqlite3_progress_handler(db, 100000, progressHandler,
  1806   1806                                        &vdbeLimitFlag);
  1807   1807             }
  1808   1808   #endif
         1809  +#ifdef SQLITE_TESTCTRL_PRNG_SEED
         1810  +          sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db);
         1811  +#endif
  1809   1812             do{
  1810   1813               runSql(db, (char*)pSql->a, runFlags);
  1811   1814             }while( timeoutTest );
  1812   1815             setAlarm(0);
  1813   1816             sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0);
  1814   1817             sqlite3_close(db);
  1815   1818           }

Changes to test/fuzzdata8.db.

cannot compute difference between binary files

Changes to test/index6.test.

   155    155   } {500}
   156    156   do_test index6-2.2 {
   157    157     execsql {
   158    158       EXPLAIN QUERY PLAN
   159    159       SELECT * FROM t2 WHERE a=5;
   160    160     }
   161    161   } {/.* TABLE t2 USING INDEX t2a1 .*/}
   162         -ifcapable stat4||stat3 {
          162  +ifcapable stat4 {
   163    163     execsql ANALYZE
   164    164     do_test index6-2.3stat4 {
   165    165       execsql {
   166    166         EXPLAIN QUERY PLAN
   167    167         SELECT * FROM t2 WHERE a IS NOT NULL;
   168    168       }
   169    169     } {/.* TABLE t2 USING INDEX t2a1 .*/}
................................................................................
   434    434   } {{} row}
   435    435   
   436    436   do_execsql_test index6-14.2 {
   437    437     SELECT * FROM t0 WHERE CASE c0 WHEN 0 THEN 0 ELSE 1 END;
   438    438   } {{} row}
   439    439   
   440    440   finish_test
   441         -

Changes to test/index7.test.

   199    199   } {800}
   200    200   do_test index7-2.2 {
   201    201     execsql {
   202    202       EXPLAIN QUERY PLAN
   203    203       SELECT * FROM t2 WHERE a=5;
   204    204     }
   205    205   } {/.* TABLE t2 USING COVERING INDEX t2a1 .*/}
   206         -ifcapable stat4||stat3 {
          206  +ifcapable stat4 {
   207    207     do_test index7-2.3stat4 {
   208    208       execsql {
   209    209         EXPLAIN QUERY PLAN
   210    210         SELECT * FROM t2 WHERE a IS NOT NULL;
   211    211       }
   212    212     } {/.* TABLE t2 USING COVERING INDEX t2a1 .*/}
   213    213   } else {

Changes to test/indexexpr1.test.

   441    441     INSERT INTO t1 VALUES('1234',0),('001234',2),('01234',1);
   442    442     SELECT b FROM t1 WHERE lower(a)='1234' ORDER BY +b;
   443    443   } {0 1 2 3}
   444    444   do_execsql_test indexexpr-1620 {
   445    445     SELECT b FROM t1 WHERE lower(a)='01234' ORDER BY +b;
   446    446   } {}
   447    447   
          448  +# 2019-08-09 https://www.sqlite.org/src/info/9080b6227fabb466
          449  +# ExprImpliesExpr theorem prover bug:
          450  +# "(NULL IS FALSE) IS FALSE" does not imply "NULL IS NULL"
          451  +#
          452  +do_execsql_test indexexpr-1700 {
          453  +  DROP TABLE IF EXISTS t0;
          454  +  CREATE TABLE t0(c0);
          455  +  INSERT INTO t0(c0) VALUES (0);
          456  +  CREATE INDEX i0 ON t0(NULL > c0) WHERE (NULL NOT NULL);
          457  +  SELECT * FROM t0 WHERE ((NULL IS FALSE) IS FALSE);
          458  +} {0}
   448    459   
   449    460   finish_test

Changes to test/insert.test.

   455    455     DROP TABLE IF EXISTS t14;
   456    456     CREATE TABLE t14(x INTEGER PRIMARY KEY);
   457    457     INSERT INTO t14 VALUES(CASE WHEN 1 THEN null END);
   458    458     SELECT x FROM t14;
   459    459   } {1}
   460    460   
   461    461   integrity_check insert-99.0
          462  +
          463  +# 2019-08-12.
          464  +#
          465  +do_execsql_test insert-15.1 {
          466  +  DROP TABLE IF EXISTS t1;
          467  +  DROP TABLE IF EXISTS t2;
          468  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);
          469  +  CREATE INDEX i1 ON t1(b);
          470  +  CREATE TABLE t2(a, b);
          471  +  INSERT INTO t2 VALUES(4, randomblob(31000));
          472  +  INSERT INTO t2 VALUES(4, randomblob(32000));
          473  +  INSERT INTO t2 VALUES(4, randomblob(33000));
          474  +  REPLACE INTO t1 SELECT a, b FROM t2;
          475  +  SELECT a, length(b) FROM t1;
          476  +} {4 33000}
          477  +
   462    478   
   463    479   finish_test

Changes to test/intreal.test.

    78     78     SELECT * FROM t0, t1 
    79     79     WHERE (
    80     80           t1.c1 >= CAST(8366271098608253588 AS REAL) 
    81     81       AND t1.c1 <= CAST(8366271098608253588 AS REAL)
    82     82     );
    83     83   } [list a $D]
    84     84   
           85  +# 2019-07-29 ticket ba2f4585cf495231
           86  +#
           87  +db close
           88  +sqlite3 db :memory:
           89  +do_execsql_test 3.0 {
           90  +  CREATE TABLE t0 (c0 REAL, c1);
           91  +  CREATE UNIQUE INDEX i0 ON t0(c1, 0 | c0);
           92  +  INSERT INTO t0(c0) VALUES (4750228396194493326), (0);
           93  +  UPDATE OR REPLACE t0 SET c0 = 'a', c1 = '';
           94  +  SELECT * FROM t0 ORDER BY t0.c1;
           95  +  PRAGMA integrity_check;
           96  +} {a {} ok}
    85     97   
    86     98   finish_test

Changes to test/json104.test.

   149    149   do_execsql_test 405 {
   150    150     UPDATE obj SET x = json_set(x, '$."d"', 4);
   151    151     SELECT json_extract(x, '$."d"') FROM obj;
   152    152   } {4}
   153    153   
   154    154   
   155    155   finish_test
   156         -
   157         -

Changes to test/like.test.

  1110   1110     SELECT * FROM t1 WHERE a LIKE ' 1%';
  1111   1111   } {{ 1x} { 1-}}
  1112   1112   do_execsql_test 16.2 {
  1113   1113     SELECT * FROM t1 WHERE a LIKE ' 1-';
  1114   1114   } {{ 1-}}
  1115   1115   
  1116   1116   finish_test
  1117         -

Changes to test/mallocA.test.

    92     92     faultsim_test_result [list 0 2]
    93     93   }
    94     94   do_faultsim_test 6.2 -faults oom* -body {
    95     95     execsql { SELECT rowid FROM t1 WHERE a='abc' AND b<'y' }
    96     96   } -test {
    97     97     faultsim_test_result [list 0 {1 2}]
    98     98   }
    99         -ifcapable stat3 {
   100         -  do_test 6.3-prep {
   101         -    execsql {
   102         -      PRAGMA writable_schema = 1;
   103         -      CREATE TABLE sqlite_stat4 AS 
   104         -      SELECT tbl, idx, neq, nlt, ndlt, sqlite_record(sample) AS sample 
   105         -      FROM sqlite_stat3;
   106         -    }
   107         -  } {}
   108         -  do_faultsim_test 6.3 -faults oom* -body {
   109         -    execsql { 
   110         -      ANALYZE sqlite_master;
   111         -      SELECT rowid FROM t1 WHERE a='abc' AND b<'y';
   112         -    }
   113         -  } -test {
   114         -    faultsim_test_result [list 0 {1 2}]
   115         -  }
   116         -}
   117     99   
   118    100   do_execsql_test 7.0 {
   119    101     PRAGMA cache_size = 5;
   120    102   }
   121    103   do_faultsim_test 7 -faults oom-trans* -prep {
   122    104   } -body {
   123    105     execsql {

Changes to test/minmax4.test.

    15     15   #
    16     16   # Demonstration that the value returned for p is on the same row as 
    17     17   # the maximum q.
    18     18   #
    19     19   
    20     20   set testdir [file dirname $argv0]
    21     21   source $testdir/tester.tcl
           22  +set testprefix minmax4
    22     23   
    23     24   ifcapable !compound {
    24     25     finish_test
    25     26     return
    26     27   }
    27     28   
    28     29   do_test minmax4-1.1 {
................................................................................
   144    145   } {1 2 1 4 4 2 3 3 5 5}
   145    146   do_test minmax4-2.7 {
   146    147     db eval {
   147    148       SELECT a, min(b), b, min(c), c FROM t2 GROUP BY a ORDER BY a;
   148    149     }
   149    150   } {1 1 {} 2 2 2 3 3 5 5}
   150    151   
          152  +#-------------------------------------------------------------------------
          153  +foreach {tn sql} {
          154  +  1 { CREATE INDEX i1 ON t1(a) }
          155  +  2 { CREATE INDEX i1 ON t1(a DESC) }
          156  +  3 { }
          157  +} {
          158  +  reset_db
          159  +  do_execsql_test 3.$tn.0 {
          160  +    CREATE TABLE t1(a, b);
          161  +    INSERT INTO t1 VALUES(NULL, 1);
          162  +  }
          163  +  execsql $sql
          164  +  do_execsql_test 3.$tn.1 {
          165  +    SELECT min(a), b FROM t1;
          166  +  } {{} 1}
          167  +  do_execsql_test 3.$tn.2 {
          168  +    SELECT min(a), b FROM t1 WHERE a<50;
          169  +  } {{} {}}
          170  +  do_execsql_test 3.$tn.3 {
          171  +    INSERT INTO t1 VALUES(2, 2);
          172  +  }
          173  +  do_execsql_test 3.$tn.4 {
          174  +    SELECT min(a), b FROM t1;
          175  +  } {2 2}
          176  +  do_execsql_test 3.$tn.5 {
          177  +    SELECT min(a), b FROM t1 WHERE a<50;
          178  +  } {2 2}
          179  +}
          180  +
          181  +#-------------------------------------------------------------------------
          182  +reset_db
          183  +do_execsql_test 4.0 {
          184  +  CREATE TABLE t0 (c0, c1);
          185  +  CREATE INDEX i0 ON t0(c1, c1 + 1 DESC);
          186  +  INSERT INTO t0(c0) VALUES (1);
          187  +}
          188  +do_execsql_test 4.1 {
          189  +  SELECT MIN(t0.c1), t0.c0 FROM t0 WHERE t0.c1 ISNULL; 
          190  +} {{} 1}
   151    191   
          192  +#-------------------------------------------------------------------------
          193  +reset_db
          194  +do_execsql_test 5.0 {
          195  +  CREATE TABLE t1 (a, b);
          196  +  INSERT INTO t1 VALUES(123, NULL);
          197  +  CREATE INDEX i1 ON t1(a, b DESC);
          198  +}
          199  +do_execsql_test 5.1 {
          200  +  SELECT MIN(a) FROM t1 WHERE a=123;
          201  +} {123}
   152    202   
   153    203   finish_test

Changes to test/releasetest_data.tcl.

     1         -
     2         -# This file contains Configuration data used by "wapptest.tcl" and
     3         -# "releasetest.tcl".
            1  +# 2019 August 01
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file implements a program that produces scripts (either shell scripts
           13  +# or batch files) to implement a particular test that is part of the SQLite
           14  +# release testing procedure. For example, to run veryquick.test with a 
           15  +# specified set of -D compiler switches.
           16  +#
           17  +# A "configuration" is a set of options passed to [./configure] and [make]
           18  +# to build the SQLite library in a particular fashion. A "platform" is a
           19  +# list of tests; most platforms are named after the hardware/OS platform
           20  +# that the tests will be run on as part of the release procedure. Each 
           21  +# "test" is a combination of a configuration and a makefile target (e.g.
           22  +# "fulltest"). The program may be invoked as follows:
     4     23   #
           24  +set USAGE {
           25  +$argv0 platforms
           26  +    List available platforms.
           27  +
           28  +$argv0 tests ?-nodebug? PLATFORM
           29  +    List tests in a specified platform. If the -nodebug switch is 
           30  +    specified, synthetic debug/ndebug configurations are omitted. Each
           31  +    test is a combination of a configuration and a makefile target.
           32  +
           33  +$argv0 script ?-msvc? CONFIGURATION TARGET
           34  +    Given a configuration and make target, return a bash (or, if -msvc
           35  +    is specified, batch) script to execute the test. The first argument
           36  +    passed to the script must be a directory containing SQLite source code.
           37  +
           38  +$argv0 configurations
           39  +    List available configurations.
           40  +}
     5     41   
     6     42   # Omit comments (text between # and \n) in a long multi-line string.
     7     43   #
     8     44   proc strip_comments {in} {
     9     45     regsub -all {#[^\n]*\n} $in {} out
    10     46     return $out
    11     47   }
................................................................................
   154    190     "Apple" {
   155    191       -Os
   156    192       -DHAVE_GMTIME_R=1
   157    193       -DHAVE_ISNAN=1
   158    194       -DHAVE_LOCALTIME_R=1
   159    195       -DHAVE_PREAD=1
   160    196       -DHAVE_PWRITE=1
   161         -    -DHAVE_USLEEP=1
   162         -    -DHAVE_USLEEP=1
   163    197       -DHAVE_UTIME=1
   164    198       -DSQLITE_DEFAULT_CACHE_SIZE=1000
   165    199       -DSQLITE_DEFAULT_CKPTFULLFSYNC=1
   166    200       -DSQLITE_DEFAULT_MEMSTATUS=1
   167    201       -DSQLITE_DEFAULT_PAGE_SIZE=1024
   168    202       -DSQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS=1
   169    203       -DSQLITE_ENABLE_API_ARMOR=1
   170    204       -DSQLITE_ENABLE_AUTO_PROFILE=1
   171    205       -DSQLITE_ENABLE_FLOCKTIMEOUT=1
   172    206       -DSQLITE_ENABLE_FTS3=1
   173    207       -DSQLITE_ENABLE_FTS3_PARENTHESIS=1
   174    208       -DSQLITE_ENABLE_FTS3_TOKENIZER=1
   175         -    if:os=="Darwin" -DSQLITE_ENABLE_LOCKING_STYLE=1
   176    209       -DSQLITE_ENABLE_PERSIST_WAL=1
   177    210       -DSQLITE_ENABLE_PURGEABLE_PCACHE=1
   178    211       -DSQLITE_ENABLE_RTREE=1
   179    212       -DSQLITE_ENABLE_SNAPSHOT=1
   180    213       # -DSQLITE_ENABLE_SQLLOG=1
   181    214       -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1
   182    215       -DSQLITE_MAX_LENGTH=2147483645
................................................................................
   208    241       -DSQLITE_DISABLE_FTS4_DEFERRED
   209    242       -DSQLITE_ENABLE_RTREE
   210    243       --enable-json1 --enable-fts5
   211    244     }
   212    245     "No-lookaside" {
   213    246       -DSQLITE_TEST_REALLOC_STRESS=1
   214    247       -DSQLITE_OMIT_LOOKASIDE=1
   215         -    -DHAVE_USLEEP=1
   216    248     }
   217    249     "Valgrind" {
   218    250       -DSQLITE_ENABLE_STAT4
   219    251       -DSQLITE_ENABLE_FTS4
   220    252       -DSQLITE_ENABLE_RTREE
   221    253       -DSQLITE_ENABLE_HIDDEN_COLUMNS
   222    254       --enable-json1
   223    255     }
          256  +
          257  +  "Windows-Memdebug" {
          258  +    MEMDEBUG=1
          259  +    DEBUG=3
          260  +  }
          261  +  "Windows-Win32Heap" {
          262  +    WIN32HEAP=1
          263  +    DEBUG=4
          264  +  }
   224    265   
   225    266     # The next group of configurations are used only by the
   226    267     # Failure-Detection platform.  They are all the same, but we need
   227    268     # different names for them all so that they results appear in separate
   228    269     # subdirectories.
   229    270     #
   230         -  Fail0 {-O0}
   231         -  Fail2 {-O0}
   232         -  Fail3 {-O0}
   233         -  Fail4 {-O0}
          271  +  Fail0     {-O0}
          272  +  Fail2     {-O0}
          273  +  Fail3     {-O0}
          274  +  Fail4     {-O0}
   234    275     FuzzFail1 {-O0}
   235    276     FuzzFail2 {-O0}
   236    277   }]
          278  +if {$tcl_platform(os)=="Darwin"} {
          279  +  lappend Configs(Apple -DSQLITE_ENABLE_LOCKING_STYLE=1
          280  +}
   237    281   
   238    282   array set ::Platforms [strip_comments {
   239    283     Linux-x86_64 {
   240         -    "Check-Symbols"           checksymbols
          284  +    "Check-Symbols*"          checksymbols
   241    285       "Fast-One"                "fuzztest test"
   242    286       "Debug-One"               "mptest test"
   243    287       "Have-Not"                test
   244    288       "Secure-Delete"           test
   245    289       "Unlock-Notify"           "QUICKTEST_INCLUDE=notify2.test test"
   246    290       "User-Auth"               tcltest
   247    291       "Update-Delete-Limit"     test
................................................................................
   249    293       "Device-Two"              "threadtest test"
   250    294       "No-lookaside"            test
   251    295       "Devkit"                  test
   252    296       "Apple"                   test
   253    297       "Sanitize"                {QUICKTEST_OMIT=func4.test,nan.test test}
   254    298       "Device-One"              fulltest
   255    299       "Default"                 "threadtest fulltest"
   256         -    "Valgrind"                valgrindtest
          300  +    "Valgrind*"               valgrindtest
   257    301     }
   258    302     Linux-i686 {
   259    303       "Devkit"                  test
   260    304       "Have-Not"                test
   261    305       "Unlock-Notify"           "QUICKTEST_INCLUDE=notify2.test test"
   262    306       "Device-One"              test
   263    307       "Device-Two"              test
................................................................................
   272    316       "Locking-Style"           "mptest test"
   273    317       "Have-Not"                test
   274    318       "Apple"                   "threadtest fulltest"
   275    319     }
   276    320     "Windows NT-intel" {
   277    321       "Stdcall"                 test
   278    322       "Have-Not"                test
          323  +    "Windows-Memdebug*"       test
          324  +    "Windows-Win32Heap*"      test
   279    325       "Default"                 "mptest fulltestonly"
   280    326     }
   281    327     "Windows NT-amd64" {
   282    328       "Stdcall"                 test
   283    329       "Have-Not"                test
          330  +    "Windows-Memdebug*"       test
          331  +    "Windows-Win32Heap*"      test
   284    332       "Default"                 "mptest fulltestonly"
   285    333     }
   286    334   
   287    335     # The Failure-Detection platform runs various tests that deliberately
   288    336     # fail.  This is used as a test of this script to verify that this script
   289    337     # correctly identifies failures.
   290    338     #
   291    339     Failure-Detection {
   292         -    Fail0     "TEST_FAILURE=0 test"
   293         -    Sanitize  "TEST_FAILURE=1 test"
   294         -    Fail2     "TEST_FAILURE=2 valgrindtest"
   295         -    Fail3     "TEST_FAILURE=3 valgrindtest"
   296         -    Fail4     "TEST_FAILURE=4 test"
   297         -    FuzzFail1 "TEST_FAILURE=5 test"
   298         -    FuzzFail2 "TEST_FAILURE=5 valgrindtest"
   299         -  }
   300         -}]
   301         -
   302         -proc make_test_suite {msvc withtcl name testtarget config} {
   303         -
   304         -  # Tcl variable $opts is used to build up the value used to set the
   305         -  # OPTS Makefile variable. Variable $cflags holds the value for
   306         -  # CFLAGS. The makefile will pass OPTS to both gcc and lemon, but
   307         -  # CFLAGS is only passed to gcc.
   308         -  #
   309         -  set makeOpts ""
   310         -  set cflags [expr {$msvc ? "-Zi" : "-g"}]
   311         -  set opts ""
   312         -  set title ${name}($testtarget)
   313         -  set configOpts $withtcl
   314         -  set skip 0
   315         -
   316         -  regsub -all {#[^\n]*\n} $config \n config
   317         -  foreach arg $config {
   318         -    if {$skip} {
   319         -      set skip 0
   320         -      continue
   321         -    }
   322         -    if {[regexp {^-[UD]} $arg]} {
   323         -      lappend opts $arg
   324         -    } elseif {[regexp {^[A-Z]+=} $arg]} {
   325         -      lappend testtarget $arg
   326         -    } elseif {[regexp {^if:([a-z]+)(.*)} $arg all key tail]} {
   327         -      # Arguments of the form 'if:os=="Linux"' will cause the subsequent
   328         -      # argument to be skipped if the $tcl_platform(os) is not "Linux", for
   329         -      # example...
   330         -      set skip [expr !(\$::tcl_platform($key)$tail)]
   331         -    } elseif {[regexp {^--(enable|disable)-} $arg]} {
   332         -      if {$msvc} {
   333         -        if {$arg eq "--disable-amalgamation"} {
   334         -          lappend makeOpts USE_AMALGAMATION=0
   335         -          continue
   336         -        }
   337         -        if {$arg eq "--disable-shared"} {
   338         -          lappend makeOpts USE_CRT_DLL=0 DYNAMIC_SHELL=0
   339         -          continue
   340         -        }
   341         -        if {$arg eq "--enable-fts5"} {
   342         -          lappend opts -DSQLITE_ENABLE_FTS5
   343         -          continue
   344         -        }
   345         -        if {$arg eq "--enable-json1"} {
   346         -          lappend opts -DSQLITE_ENABLE_JSON1
   347         -          continue
   348         -        }
   349         -        if {$arg eq "--enable-shared"} {
   350         -          lappend makeOpts USE_CRT_DLL=1 DYNAMIC_SHELL=1
   351         -          continue
   352         -        }
   353         -      }
   354         -      lappend configOpts $arg
   355         -    } else {
   356         -      if {$msvc} {
   357         -        if {$arg eq "-g"} {
   358         -          lappend cflags -Zi
   359         -          continue
   360         -        }
   361         -        if {[regexp -- {^-O(\d+)$} $arg all level]} then {
   362         -          lappend makeOpts OPTIMIZATIONS=$level
   363         -          continue
   364         -        }
   365         -      }
   366         -      lappend cflags $arg
   367         -    }
   368         -  }
   369         -
   370         -  # Disable sync to make testing faster.
   371         -  #
   372         -  lappend opts -DSQLITE_NO_SYNC=1
   373         -
   374         -  # Some configurations already set HAVE_USLEEP; in that case, skip it.
   375         -  #
   376         -  if {[lsearch -regexp $opts {^-DHAVE_USLEEP(?:=|$)}]==-1} {
   377         -    lappend opts -DHAVE_USLEEP=1
   378         -  }
   379         -
   380         -  # Add the define for this platform.
   381         -  #
   382         -  if {$::tcl_platform(platform)=="windows"} {
   383         -    lappend opts -DSQLITE_OS_WIN=1
   384         -  } else {
   385         -    lappend opts -DSQLITE_OS_UNIX=1
   386         -  }
   387         -
   388         -  # Set the sub-directory to use.
   389         -  #
   390         -  set dir [string tolower [string map {- _ " " _ "(" _ ")" _} $name]]
   391         -
   392         -  # Join option lists into strings, using space as delimiter.
   393         -  #
   394         -  set makeOpts [join $makeOpts " "]
   395         -  set cflags   [join $cflags " "]
   396         -  set opts     [join $opts " "]
   397         -
   398         -  return [list $title $dir $configOpts $testtarget $makeOpts $cflags $opts]
   399         -}
          340  +    Fail0*     "TEST_FAILURE=0 test"
          341  +    Sanitize*  "TEST_FAILURE=1 test"
          342  +    Fail2*     "TEST_FAILURE=2 valgrindtest"
          343  +    Fail3*     "TEST_FAILURE=3 valgrindtest"
          344  +    Fail4*     "TEST_FAILURE=4 test"
          345  +    FuzzFail1* "TEST_FAILURE=5 test"
          346  +    FuzzFail2* "TEST_FAILURE=5 valgrindtest"
          347  +  }
          348  +}]
   400    349   
   401    350   # Configuration verification: Check that each entry in the list of configs
   402    351   # specified for each platforms exists.
   403    352   #
   404    353   foreach {key value} [array get ::Platforms] {
   405    354     foreach {v t} $value {
          355  +    if {[string range $v end end]=="*"} {
          356  +      set v [string range $v 0 end-1]
          357  +    }
   406    358       if {0==[info exists ::Configs($v)]} {
   407    359         puts stderr "No such configuration: \"$v\""
   408    360         exit -1
   409    361       }
   410    362     }
   411    363   }
          364  +
          365  +proc usage {} {
          366  +  global argv0
          367  +  puts stderr [subst $::USAGE]
          368  +  exit 1
          369  +}
          370  +
          371  +proc is_prefix {p str min} {
          372  +  set n [string length $p]
          373  +  if {$n<$min} { return 0 }
          374  +  if {[string range $str 0 [expr $n-1]]!=$p} { return 0 }
          375  +  return 1
          376  +}
          377  +
          378  +proc main_configurations {} {
          379  +  foreach k [lsort [array names ::Configs]] {
          380  +    puts $k
          381  +  }
          382  +}
          383  +
          384  +proc main_platforms {} {
          385  +  foreach k [lsort [array names ::Platforms]] {
          386  +    puts "\"$k\""
          387  +  }
          388  +}
          389  +
          390  +proc main_script {args} {
          391  +  set bMsvc 0
          392  +  set nArg [llength $args]
          393  +  if {$nArg==3} {
          394  +    if {![is_prefix [lindex $args 0] -msvc 2]} usage
          395  +    set bMsvc 1
          396  +  } elseif {$nArg<2 || $nArg>3} {
          397  +    usage
          398  +  }
          399  +  set config [lindex $args end-1]
          400  +  set target [lindex $args end]
          401  +
          402  +  set opts       [list]                         ;# OPTS value
          403  +  set cflags     [expr {$bMsvc ? "-Zi" : "-g"}] ;# CFLAGS value
          404  +  set makeOpts   [list]                         ;# Extra args for [make]
          405  +  set configOpts [list]                         ;# Extra args for [configure]
          406  +
          407  +  if {$::tcl_platform(platform)=="windows" || $bMsvc} {
          408  +    lappend opts -DSQLITE_OS_WIN=1
          409  +  } else {
          410  +    lappend opts -DSQLITE_OS_UNIX=1
          411  +  }
          412  +
          413  +  # Figure out if this is a synthetic ndebug or debug configuration.
          414  +  #
          415  +  set bRemoveDebug 0
          416  +  if {[string match *-ndebug $config]} {
          417  +    set bRemoveDebug 1
          418  +    set config [string range $config 0 end-7]
          419  +  }
          420  +  if {[string match *-debug $config]} {
          421  +    lappend opts -DSQLITE_DEBUG
          422  +    lappend opts -DSQLITE_EXTRA_IFNULLROW
          423  +    set config [string range $config 0 end-6]
          424  +  }
          425  +
          426  +  # Ensure that the named configuration exists.
          427  +  #
          428  +  if {![info exists ::Configs($config)]} {
          429  +    puts stderr "No such config: $config"
          430  +    exit 1
          431  +  }
          432  +
          433  +  # Loop through the parameters of the nominated configuration, updating
          434  +  # $opts, $cflags, $makeOpts and $configOpts along the way. Rules are as
          435  +  # follows:
          436  +  #
          437  +  #   1. If the parameter begins with a "*", discard it.
          438  +  #
          439  +  #   2. If $bRemoveDebug is set and the parameter is -DSQLITE_DEBUG or
          440  +  #      -DSQLITE_DEBUG=1, discard it
          441  +  #
          442  +  #   3. If the parameter begins with "-D", add it to $opts.
          443  +  #
          444  +  #   4. If the parameter begins with "--" add it to $configOpts. Unless
          445  +  #      this command is preparing a script for MSVC - then add an 
          446  +  #      equivalent to $makeOpts or $opts.
          447  +  #
          448  +  #   5. If the parameter begins with "-" add it to $cflags. If in MSVC
          449  +  #      mode and the parameter is an -O<integer> option, instead add
          450  +  #      an OPTIMIZATIONS=<integer> switch to $makeOpts.
          451  +  #
          452  +  #   6. If none of the above apply, add the parameter to $makeOpts
          453  +  #
          454  +  foreach param $::Configs($config) {
          455  +    if {[string range $param 0 0]=="*"} continue
          456  +
          457  +    if {$bRemoveDebug} {
          458  +      if {$param=="-DSQLITE_DEBUG" || $param=="-DSQLITE_DEBUG=1"
          459  +       || $param=="-DSQLITE_MEMDEBUG" || $param=="-DSQLITE_MEMDEBUG=1"
          460  +      } {
          461  +        continue
          462  +      }
          463  +    }
          464  +
          465  +    if {[string range $param 0 1]=="-D"} {
          466  +      lappend opts $param
          467  +      continue
          468  +    }
          469  +
          470  +    if {[string range $param 0 1]=="--"} {
          471  +      if {$bMsvc} {
          472  +        switch -- $param {
          473  +          --disable-amalgamation {
          474  +            lappend makeOpts USE_AMALGAMATION=0
          475  +          }
          476  +          --disable-shared {
          477  +            lappend makeOpts USE_CRT_DLL=0 DYNAMIC_SHELL=0
          478  +          }
          479  +          --enable-fts5 {
          480  +            lappend opts -DSQLITE_ENABLE_FTS5
          481  +          } 
          482  +          --enable-json1 {
          483  +            lappend opts -DSQLITE_ENABLE_JSON1
          484  +          } 
          485  +          --enable-shared {
          486  +            lappend makeOpts USE_CRT_DLL=1 DYNAMIC_SHELL=1
          487  +          }
          488  +          --enable-session {
          489  +            lappend opts -DSQLITE_ENABLE_PREUPDATE_HOOK
          490  +            lappend opts -DSQLITE_ENABLE_SESSION
          491  +          }
          492  +          default {
          493  +            error "Cannot translate $param for MSVC"
          494  +          }
          495  +        }
          496  +      } else {
          497  +        lappend configOpts $param
          498  +      }
          499  +
          500  +      continue
          501  +    }
          502  +
          503  +    if {[string range $param 0 0]=="-"} {
          504  +      if {$bMsvc && [regexp -- {^-O(\d+)$} $param -> level]} {
          505  +        lappend makeOpts OPTIMIZATIONS=$level
          506  +      } else {
          507  +        lappend cflags $param
          508  +      }
          509  +      continue
          510  +    }
          511  +
          512  +    lappend makeOpts $param
          513  +  }
          514  +
          515  +  # Some configurations specify -DHAVE_USLEEP=0. For all others, add
          516  +  # -DHAVE_USLEEP=1.
          517  +  #
          518  +  if {[lsearch $opts "-DHAVE_USLEEP=0"]<0} {
          519  +    lappend opts -DHAVE_USLEEP=1
          520  +  }
          521  +
          522  +  if {$bMsvc==0} {
          523  +    puts {set -e}
          524  +    puts {}
          525  +    puts {if [ "$#" -ne 1 ] ; then}
          526  +    puts {  echo "Usage: $0 <sqlite-src-dir>" }
          527  +    puts {  exit -1 }
          528  +    puts {fi }
          529  +    puts {SRCDIR=$1}
          530  +    puts {}
          531  +    puts "TCL=\"[::tcl::pkgconfig get libdir,install]\""
          532  +
          533  +    puts "\$SRCDIR/configure --with-tcl=\$TCL $configOpts"
          534  +    puts {}
          535  +    puts {OPTS="      -DSQLITE_NO_SYNC=1"}
          536  +    foreach o $opts { 
          537  +      puts "OPTS=\"\$OPTS $o\"" 
          538  +    }
          539  +    puts {}
          540  +    puts "CFLAGS=\"$cflags\""
          541  +    puts {}
          542  +    puts "make $target \"CFLAGS=\$CFLAGS\" \"OPTS=\$OPTS\" $makeOpts"
          543  +  } else {
          544  +
          545  +    puts {set SRCDIR=%1}
          546  +    set makecmd    "nmake /f %SRCDIR%\\Makefile.msc TOP=%SRCDIR% $target "
          547  +    append makecmd "\"CFLAGS=$cflags\" \"OPTS=$opts\" $makeOpts"
          548  +
          549  +    puts "set TMP=%CD%"
          550  +    puts $makecmd
          551  +  }
          552  +}
          553  +
          554  +proc main_tests {args} {
          555  +  set bNodebug 0
          556  +  set nArg [llength $args]
          557  +  if {$nArg==2} {
          558  +    if {[is_prefix [lindex $args 0] -nodebug 2]} {
          559  +      set bNodebug 1
          560  +    } elseif {[is_prefix [lindex $args 0] -debug 2]} {
          561  +      set bNodebug 0
          562  +    } else usage
          563  +  } elseif {$nArg==0 || $nArg>2} {
          564  +    usage
          565  +  }
          566  +  set p [lindex $args end]
          567  +  if {![info exists ::Platforms($p)]} {
          568  +    puts stderr "No such platform: $p"
          569  +    exit 1
          570  +  }
          571  +
          572  +  foreach {config target} $::Platforms($p) {
          573  +    set bNosynthetic 0
          574  +    if {[string range $config end end]=="*"} {
          575  +      set bNosynthetic 1
          576  +      set config [string range $config 0 end-1]
          577  +    }
          578  +    puts "$config \"$target\""
          579  +    if {$bNodebug==0 && $bNosynthetic==0} {
          580  +      set iHas [string first SQLITE_DEBUG $::Configs($config)]
          581  +      if {$iHas>=0} {
          582  +        puts "$config-ndebug \"test\""
          583  +      } else {
          584  +        puts "$config-debug \"test\""
          585  +      }
          586  +    }
          587  +  }
          588  +}
          589  +
          590  +if {[llength $argv]==0} { usage }
          591  +set cmd [lindex $argv 0]
          592  +set n [expr [llength $argv]-1]
          593  +if {[string match ${cmd}* configurations] && $n==0} {
          594  +  main_configurations 
          595  +} elseif {[string match ${cmd}* script]} {
          596  +  main_script {*}[lrange $argv 1 end]
          597  +} elseif {[string match ${cmd}* platforms] && $n==0} {
          598  +  main_platforms
          599  +} elseif {[string match ${cmd}* tests]} {
          600  +  main_tests {*}[lrange $argv 1 end]
          601  +} else {
          602  +  usage
          603  +}
          604  +
   412    605   

Changes to test/rowvalue.test.

   552    552   #
   553    553   do_execsql_test 21.0 {
   554    554     DROP TABLE IF EXISTS t1;
   555    555     CREATE TABLE t1(a,b,PRIMARY KEY(b,b));
   556    556     INSERT INTO t1 VALUES(1,2),(3,4),(5,6);
   557    557     SELECT * FROM t1 WHERE (a,b) IN (VALUES(1,2));  
   558    558   } {1 2}
          559  +
          560  +# 2019-08-09: Multi-column subquery on the RHS of an IN operator.
          561  +#
          562  +do_execsql_test 22.100 {
          563  +  SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1) IN (SELECT 3,4);
          564  +  SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1) IN (SELECT 5,6);
          565  +  SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1) IN (SELECT 3,4);
          566  +  SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1) IN (SELECT 5,6);
          567  +  SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1 DESC) IN (SELECT 3,4);
          568  +  SELECT (SELECT 3,4 UNION SELECT 5,6 ORDER BY 1 DESC) IN (SELECT 5,6);
          569  +  SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1 DESC) IN (SELECT 3,4);
          570  +  SELECT (SELECT 5,6 UNION SELECT 3,4 ORDER BY 1 DESC) IN (SELECT 5,6);
          571  +} {1 0 1 0 0 1 0 1}
   559    572   
   560    573   finish_test

Changes to test/schema.test.

   205    205     do_test schema-7.4 {
   206    206       sqlite3_finalize $::STMT
   207    207     } {SQLITE_SCHEMA}
   208    208   }
   209    209   
   210    210   #---------------------------------------------------------------------
   211    211   # Tests 8.1 and 8.2 check that prepared statements are invalidated when
   212         -# the authorization function is set.
          212  +# the authorization function is set to a non-null function.  Tests 8.11
          213  +# and 8.12 verify that no invalidations occur when the authorizer is
          214  +# cleared.
   213    215   #
   214    216   ifcapable auth {
          217  +  proc noop_auth {args} {return SQLITE_OK}
   215    218     do_test schema-8.1 {
   216    219       set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
          220  +    db auth noop_auth
          221  +    sqlite3_step $::STMT
          222  +  } {SQLITE_ERROR}
          223  +  do_test schema-8.2 {
          224  +    sqlite3_finalize $::STMT
          225  +  } {SQLITE_SCHEMA}
          226  +  do_test schema-8.11 {
          227  +    set ::STMT [sqlite3_prepare $::DB {SELECT * FROM sqlite_master} -1 TAIL]
   217    228       db auth {}
   218    229       sqlite3_step $::STMT
   219         -  } {SQLITE_ERROR}
   220         -  do_test schema-8.3 {
          230  +  } {SQLITE_ROW}
          231  +  do_test schema-8.12 {
   221    232       sqlite3_finalize $::STMT
   222         -  } {SQLITE_SCHEMA}
          233  +  } {SQLITE_OK}
          234  +
   223    235   }
   224    236   
   225    237   #---------------------------------------------------------------------
   226    238   # schema-9.1: Test that if a table is dropped by one database connection, 
   227    239   #             other database connections are aware of the schema change.
   228    240   # schema-9.2: Test that if a view is dropped by one database connection,
   229    241   #             other database connections are aware of the schema change.

Changes to test/skipscan1.test.

   230    230     EXPLAIN QUERY PLAN
   231    231       SELECT xh, loc FROM t5 WHERE loc >= 'M' AND loc < 'N';
   232    232   } {/.*COVERING INDEX t5i1 .*/}
   233    233   do_execsql_test skipscan1-5.2 {
   234    234     ANALYZE;
   235    235     DELETE FROM sqlite_stat1;
   236    236     DROP TABLE IF EXISTS sqlite_stat4;
   237         -  DROP TABLE IF EXISTS sqlite_stat3;
   238    237     INSERT INTO sqlite_stat1 VALUES('t5','t5i1','2702931 3 2 2 2 2');
   239    238     INSERT INTO sqlite_stat1 VALUES('t5','t5i2','2702931 686 2 2 2');
   240    239     ANALYZE sqlite_master;
   241    240   } {}
   242    241   db cache flush
   243    242   do_execsql_test skipscan1-5.3 {
   244    243     EXPLAIN QUERY PLAN
................................................................................
   368    367   do_execsql_test skipscan1-2.3eqp {
   369    368     EXPLAIN QUERY PLAN
   370    369     SELECT a,b,c,d,'|' FROM t6 WHERE d<>99 AND b=345 ORDER BY a DESC;
   371    370   } {/* USING INDEX t6abc (ANY(a) AND b=?)*/}
   372    371   do_execsql_test skipscan1-2.3 {
   373    372     SELECT a,b,c,d,'|' FROM t6 WHERE d<>99 AND b=345 ORDER BY a DESC;
   374    373   } {}
          374  +
          375  +# 2019-07-29 Ticket ced41c7c7d6b4d36
          376  +# A skipscan query is not order-distinct
          377  +#
          378  +db close
          379  +sqlite3 db :memory:
          380  +do_execsql_test skipscan1-3.1 {
          381  +  CREATE TABLE t1 (c1, c2, c3, c4, PRIMARY KEY(c4, c3));
          382  +  INSERT INTO t1 VALUES(3,0,1,NULL);
          383  +  INSERT INTO t1 VALUES(0,4,1,NULL);
          384  +  INSERT INTO t1 VALUES(5,6,1,NULL);
          385  +  INSERT INTO t1 VALUES(0,4,1,NULL);
          386  +  ANALYZE sqlite_master;
          387  +  INSERT INTO sqlite_stat1 VALUES('t1','sqlite_autoindex_t1_1','18 18 6');
          388  +  ANALYZE sqlite_master;
          389  +  SELECT DISTINCT quote(c1), quote(c2), quote(c3), quote(c4), '|'
          390  +    FROM t1 WHERE t1.c3 = 1;
          391  +} {3 0 1 NULL | 0 4 1 NULL | 5 6 1 NULL |}
          392  +do_eqp_test skipscan1-3.2 {
          393  +  SELECT DISTINCT quote(c1), quote(c2), quote(c3), quote(c4), '|'
          394  +    FROM t1 WHERE t1.c3 = 1;
          395  +} {
          396  +  QUERY PLAN
          397  +  |--SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (ANY(c4) AND c3=?)
          398  +  `--USE TEMP B-TREE FOR DISTINCT
          399  +}
   375    400   
   376    401   finish_test

Changes to test/tempdb2.test.

    93     93   }
    94     94   
    95     95   do_execsql_test 2.2 {
    96     96     SELECT b FROM t1 WHERE a = 10001;
    97     97   } "[int2str 1001][int2str 1001][int2str 1001]"
    98     98   
    99     99   finish_test
   100         -

Changes to test/tkt-cbd054fa6b.test.

    12     12   # This file implements tests to verify that ticket [cbd054fa6b] has been
    13     13   # fixed.  
    14     14   #
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19         -ifcapable !stat4&&!stat3 {
           19  +ifcapable !stat4 {
    20     20     finish_test
    21     21     return
    22     22   }
    23     23   
    24     24   proc s {blob} {
    25     25     set ret ""
    26     26     binary scan $blob c* bytes
................................................................................
    51     51       INSERT INTO t1 VALUES (NULL, 'H');
    52     52       INSERT INTO t1 VALUES (NULL, 'I');
    53     53       SELECT count(*) FROM t1;
    54     54     }
    55     55   } {10}
    56     56   do_test tkt-cbd05-1.2 {
    57     57     db eval { ANALYZE; }
    58         -  ifcapable stat4 {
    59         -    db eval {
    60         -      PRAGMA writable_schema = 1;
    61         -      CREATE VIEW vvv AS 
    62         -      SELECT tbl,idx,neq,nlt,ndlt,test_extract(sample,0) AS sample
    63         -      FROM sqlite_stat4;
    64         -      PRAGMA writable_schema = 0;
    65         -    }
    66         -  } else {
    67         -    db eval {
    68         -      CREATE VIEW vvv AS 
    69         -      SELECT tbl,idx,neq,nlt,ndlt,sample FROM sqlite_stat3;
    70         -    }
           58  +  db eval {
           59  +    PRAGMA writable_schema = 1;
           60  +    CREATE VIEW vvv AS 
           61  +    SELECT tbl,idx,neq,nlt,ndlt,test_extract(sample,0) AS sample
           62  +    FROM sqlite_stat4;
           63  +    PRAGMA writable_schema = 0;
    71     64     }
    72     65   } {}
    73     66   do_test tkt-cbd05-1.3 {
    74     67     execsql { 
    75     68       SELECT tbl,idx,group_concat(s(sample),' ') 
    76     69       FROM vvv 
    77     70       WHERE idx = 't1_x' 

Changes to test/triggerC.test.

  1068   1068   }
  1069   1069   do_catchsql_test 17.1 {
  1070   1070     INSERT INTO xyz VALUES('hello', 2, 3);
  1071   1071   } {1 {datatype mismatch}}
  1072   1072   
  1073   1073   
  1074   1074   finish_test
  1075         -

Changes to test/view.test.

   719    719     WITH v17(x,y) AS (SELECT max(a), min(b) FROM t16 GROUP BY c)
   720    720     SELECT * FROM v17 AS one, v17 AS two WHERE one.x=1;
   721    721   } {
   722    722     1 1 1 1 
   723    723     1 1 2 2 
   724    724     1 1 3 3
   725    725   }
          726  +
          727  +#-------------------------------------------------------------------------
          728  +reset_db
          729  +do_execsql_test view-27.0 {
          730  +  CREATE TABLE t0(c0 TEXT, c1);
          731  +  INSERT INTO t0(c0, c1) VALUES (-1, 0);
          732  +  CREATE VIEW v0(c0, c1) AS SELECT t0.c0, AVG(t0.c1) FROM t0;
          733  +}
          734  +
          735  +do_execsql_test view-27.1 {
          736  +  SELECT c0, typeof(c0), c1, typeof(c1) FROM v0;
          737  +} {
          738  +  -1   text
          739  +   0.0 real
          740  +}
          741  +
          742  +do_execsql_test view-27.2 { SELECT c0<c1 FROM v0 } 1
          743  +do_execsql_test view-27.3 { SELECT c1<c0 FROM v0 } 0
          744  +do_execsql_test view-27.4 {
          745  +  SELECT 1 FROM v0 WHERE c1<c0
          746  +} {}
          747  +do_execsql_test view-27.5 {
          748  +  SELECT 1 FROM v0 WHERE c0<c1
          749  +} {1}
          750  +
          751  +do_execsql_test view-27.6 { 
          752  +  SELECT c0<c1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) 
          753  +} 1
          754  +do_execsql_test view-27.7 { 
          755  +  SELECT c1<c0 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) 
          756  +} 0
          757  +do_execsql_test view-27.8 {
          758  +  SELECT 1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) WHERE c1<c0
          759  +} {}
          760  +do_execsql_test view-27.9 {
          761  +  SELECT 1 FROM (SELECT t0.c0 AS c0, AVG(t0.c1) AS c1 FROM t0) WHERE c0<c1
          762  +} {1}
          763  +
          764  +#-------------------------------------------------------------------------
          765  +reset_db
          766  +do_execsql_test view-28.0 {
          767  +  CREATE TABLE t0(c0 TEXT);
          768  +  CREATE VIEW v0(c0) AS SELECT t0.c0 FROM t0;
          769  +  INSERT INTO t0(c0) VALUES ('0');
          770  +}
          771  +do_execsql_test view-28.1 {
          772  +  SELECT 0 IN (c0) FROM t0;
          773  +} {0}
          774  +do_execsql_test view-28.2 {
          775  +  SELECT 0 IN (c0) FROM (SELECT c0 FROM t0);
          776  +} {0}
   726    777   
   727    778   finish_test

Changes to test/walvfs.test.

   422    422     catchsql { SELECT count(*) FROM t1 } db2
   423    423   } {1 {disk I/O error}}
   424    424   
   425    425   db close
   426    426   db2 close
   427    427   tvfs delete
   428    428   finish_test
   429         -

Changes to test/wapptest.tcl.

     1      1   #!/bin/sh
     2      2   # \
     3      3   exec wapptclsh "$0" ${1+"$@"}
     4      4   
     5      5   # package required wapp
     6      6   source [file join [file dirname [info script]] wapp.tcl]
     7      7   
     8         -# Read the data from the releasetest_data.tcl script.
     9         -#
    10         -source [file join [file dirname [info script]] releasetest_data.tcl]
    11         -
    12      8   # Variables set by the "control" form:
    13      9   #
    14     10   #   G(platform) - User selected platform.
    15     11   #   G(test)     - Set to "Normal", "Veryquick", "Smoketest" or "Build-Only".
    16     12   #   G(keep)     - Boolean. True to delete no files after each test.
    17     13   #   G(msvc)     - Boolean. True to use MSVC as the compiler.
    18     14   #   G(tcl)      - Use Tcl from this directory for builds.
................................................................................
    64     60     foreach t $G(test_array) {
    65     61       set config [dict get $t config]
    66     62       set target [dict get $t target]
    67     63       wapptest_output [format "    %-25s%s" $config $target]
    68     64     }
    69     65     wapptest_output [string repeat * 70]
    70     66   }
           67  +
           68  +proc releasetest_data {args} {
           69  +  global G
           70  +  set rtd [file join $G(srcdir) test releasetest_data.tcl]
           71  +  set fd [open "|[info nameofexecutable] $rtd $args" r+]
           72  +  set ret [read $fd]
           73  +  close $fd
           74  +  return $ret
           75  +}
    71     76   
    72     77   # Generate the text for the box at the top of the UI. The current SQLite
    73     78   # version, according to fossil, along with a warning if there are 
    74     79   # uncommitted changes in the checkout.
    75     80   #
    76     81   proc generate_fossil_info {} {
    77     82     global G
................................................................................
   104    109   # app is in some other state ("running" or "stopped"), this command
   105    110   # is a no-op.
   106    111   #
   107    112   proc set_test_array {} {
   108    113     global G
   109    114     if { $G(state)=="config" } {
   110    115       set G(test_array) [list]
   111         -    foreach {config target} $::Platforms($G(platform)) {
          116  +    set debug "-debug"
          117  +    if {$G(debug)==0} { set debug "-nodebug"}
          118  +    foreach {config target} [releasetest_data tests $debug $G(platform)] {
   112    119   
   113    120         # If using MSVC, do not run sanitize or valgrind tests. Or the
   114    121         # checksymbols test.
   115    122         if {$G(msvc) && (
   116    123             "Sanitize" == $config 
   117    124          || "checksymbols" in $target
   118    125          || "valgrindtest" in $target
................................................................................
   132    139                 set target testfixture.exe
   133    140               }
   134    141             }
   135    142           }
   136    143         }
   137    144   
   138    145         lappend G(test_array) [dict create config $config target $target]
   139         -
   140         -      set exclude [list checksymbols valgrindtest fuzzoomtest]
   141         -      if {$G(debug) && !($target in $exclude)} {
   142         -        set debug_idx [lsearch -glob $::Configs($config) -DSQLITE_DEBUG*]
   143         -        set xtarget $target
   144         -        regsub -all {fulltest[a-z]*} $xtarget test xtarget
   145         -        if {$debug_idx<0} {
   146         -          lappend G(test_array) [
   147         -            dict create config $config-(Debug) target $xtarget
   148         -          ]
   149         -        } else {
   150         -          lappend G(test_array) [
   151         -            dict create config $config-(NDebug) target $xtarget
   152         -          ]
   153         -        }
   154         -      }
   155    146       }
   156    147     }
   157    148   }
   158    149   
   159    150   proc count_tests_and_errors {name logfile} {
   160    151     global G
   161    152   
................................................................................
   309    300       if {[string trim $line] != ""} { puts "Trace   : $name - \"$line\"" }
   310    301     }
   311    302   
   312    303     do_some_stuff
   313    304   }
   314    305   
   315    306   # Return the contents of the "slave script" - the script run by slave 
   316         -# processes to actually perform the test. It does two things:
   317         -#
   318         -#   1. Reads and [exec]s the contents of file wapptest_configure.sh.
   319         -#   2. Reads and [exec]s the contents of file wapptest_make.sh.
   320         -#
   321         -# Step 1 is omitted if the test uses MSVC (which does not use configure).
          307  +# processes to actually perform the test. All it does is execute the
          308  +# test script already written to disk (wapptest_cmd.sh or wapptest_cmd.bat).
   322    309   #
   323    310   proc wapptest_slave_script {} {
   324    311     global G
   325         -  set res {
   326         -    proc readfile {filename} {
   327         -      set fd [open $filename]
   328         -      set data [read $fd]
   329         -      close $fd
   330         -      return $data
   331         -    }
          312  +  if {$G(msvc)==0} {
          313  +    set dir [file join .. $G(srcdir)]
          314  +    set res [subst -nocommands {
          315  +      set rc [catch "exec sh wapptest_cmd.sh {$dir} >>& test.log" ]
          316  +      exit [set rc]
          317  +    }]
          318  +  } else {
          319  +    set dir [file nativename [file normalize $G(srcdir)]]
          320  +    set dir [string map [list "\\" "\\\\"] $dir]
          321  +    set res [subst -nocommands {
          322  +      set rc [catch "exec wapptest_cmd.bat {$dir} >>& test.log" ]
          323  +      exit [set rc]
          324  +    }]
   332    325     }
   333    326   
   334         -  if {$G(msvc)==0} { 
   335         -    append res {
   336         -      set cfg  [readfile wapptest_configure.sh]
   337         -      set rc [catch { exec {*}$cfg >& test.log } msg]
   338         -      if {$rc==0} {
   339         -        set make [readfile wapptest_make.sh]
   340         -        set rc [catch { exec {*}$make >>& test.log }]
   341         -      }
   342         -    } 
   343         -  } else { 
   344         -    append res {
   345         -      set make [readfile wapptest_make.sh]
   346         -      set rc [catch { exec {*}$make >>& test.log }]
   347         -    }
   348         -  }
   349         -
   350         -  append res { exit $rc }
   351         -
   352    327     set res
   353    328   }
   354    329   
   355    330   
   356    331   # Launch a slave process to run a test.
   357    332   #
   358         -proc slave_launch {
   359         -  name wtcl title dir configOpts testtarget makeOpts cflags opts
   360         -} {
          333  +proc slave_launch {name target dir} {
   361    334     global G
   362    335   
   363    336     catch { file mkdir $dir } msg
   364    337     foreach f [glob -nocomplain [file join $dir *]] {
   365    338       catch { file delete -force $f }
   366    339     }
   367    340     set G(test.$name.dir) $dir
   368    341   
   369         -  # Write the configure command to wapptest_configure.sh. This file
   370         -  # is empty if using MSVC - MSVC does not use configure.
          342  +  # Write the test command to wapptest_cmd.sh|bat.
   371    343     #
   372         -  set fd1 [open [file join $dir wapptest_configure.sh] w]
   373         -  if {$G(msvc)==0} {
   374         -    puts $fd1 "[file join .. $G(srcdir) configure] $wtcl $configOpts"
          344  +  set ext sh
          345  +  if {$G(msvc)} { set ext bat }
          346  +  set fd1 [open [file join $dir wapptest_cmd.$ext] w]
          347  +  if {$G(msvc)} {
          348  +    puts $fd1 [releasetest_data script -msvc $name $target]
          349  +  } else {
          350  +    puts $fd1 [releasetest_data script $name $target]
   375    351     }
   376    352     close $fd1
   377    353   
   378         -  # Write the make command to wapptest_make.sh. Using nmake for MSVC and
   379         -  # make for all other systems.
   380         -  #
   381         -  set makecmd "make"
   382         -  if {$G(msvc)} { 
   383         -    set nativedir [file nativename $G(srcdir)]
   384         -    set nativedir [string map [list "\\" "\\\\"] $nativedir]
   385         -    set makecmd "nmake /f [file join $nativedir Makefile.msc] TOP=$nativedir"
   386         -  }
   387         -  set fd2 [open [file join $dir wapptest_make.sh] w]
   388         -  puts $fd2 "$makecmd $makeOpts $testtarget \"CFLAGS=$cflags\" \"OPTS=$opts\""
   389         -  close $fd2
   390         -
   391    354     # Write the wapptest_run.tcl script to the test directory. To run the
   392    355     # commands in the other two files.
   393    356     #
   394    357     set fd3 [open [file join $dir wapptest_run.tcl] w]
   395    358     puts $fd3 [wapptest_slave_script]
   396    359     close $fd3
   397    360   
................................................................................
   444    407         if {$nLaunch<=0} break
   445    408         set name [dict get $j config]
   446    409         if { ![info exists G(test.$name.channel)]
   447    410           && ![info exists G(test.$name.done)]
   448    411         } {
   449    412   
   450    413           set target [dict get $j target]
          414  +        set dir [string tolower [string map {" " _ "-" _} $name]]
   451    415           set G(test.$name.start) [clock seconds]
   452         -        set wtcl ""
   453         -        if {$G(tcl)!=""} { set wtcl "--with-tcl=$G(tcl)" }
          416  +        set G(test.$name.log) [file join $dir test.log]
   454    417   
   455         -        # If this configuration is named <name>-(Debug) or <name>-(NDebug),
   456         -        # then add or remove the SQLITE_DEBUG option from the base
   457         -        # configuration before running the test.
   458         -        if {[regexp -- {(.*)-(\(.*\))} $name -> head tail]} {
   459         -          set opts $::Configs($head)
   460         -          if {$tail=="(Debug)"} {
   461         -            append opts " -DSQLITE_DEBUG=1 -DSQLITE_EXTRA_IFNULLROW=1"
   462         -          } else {
   463         -            regsub { *-DSQLITE_MEMDEBUG[^ ]* *} $opts { } opts
   464         -            regsub { *-DSQLITE_DEBUG[^ ]* *} $opts { } opts
   465         -          }
   466         -        } else {
   467         -          set opts $::Configs($name)
   468         -        }
          418  +        slave_launch $name $target $dir
   469    419   
   470         -        set L [make_test_suite $G(msvc) $wtcl $name $target $opts]
   471         -        set G(test.$name.log) [file join [lindex $L 1] test.log]
   472         -        slave_launch $name $wtcl {*}$L
   473         -
   474         -        set G(test.$name.log) [file join [lindex $L 1] test.log]
   475    420           incr nLaunch -1
   476    421         }
   477    422       }
   478    423     }
   479    424   }
   480    425   
   481    426   proc generate_select_widget {label id lOpt opt} {
................................................................................
   513    458     wapp-trim {
   514    459       </div>
   515    460       <div class="border" id=controls> 
   516    461       <form action="control" method="post" name="control">
   517    462     }
   518    463   
   519    464     # Build the "platform" select widget. 
   520         -  set lOpt [array names ::Platforms]
          465  +  set lOpt [releasetest_data platforms]
   521    466     generate_select_widget Platform control_platform $lOpt $G(platform)
   522    467   
   523    468     # Build the "test" select widget. 
   524    469     set lOpt [list Normal Veryquick Smoketest Build-Only] 
   525    470     generate_select_widget Test control_test $lOpt $G(test)
   526    471   
   527    472     # Build the "jobs" select widget. Options are 1 to 8.
................................................................................
   882    827   
   883    828   for {set i 0} {$i < [llength $lTestArg]} {incr i} {
   884    829     switch -- [lindex $lTestArg $i] {
   885    830       -platform {
   886    831         if {$i==[llength $lTestArg]-1} { wapptest_usage }
   887    832         incr i
   888    833         set arg [lindex $lTestArg $i]
   889         -      set lPlatform [array names ::Platforms]
          834  +      set lPlatform [releasetest_data platforms]
   890    835         if {[lsearch $lPlatform $arg]<0} {
   891    836           puts stderr "No such platform: $arg. Platforms are: $lPlatform"
   892    837           exit -1
   893    838         }
   894    839         set G(platform) $arg
   895    840       }
   896    841   

Changes to test/where.test.

  1535   1535   } {0 {}}
  1536   1536   do_catchsql_test where-25.5 {
  1537   1537     INSERT INTO t1 VALUES(4, 'four', 'iii') 
  1538   1538       ON CONFLICT(c) DO UPDATE SET b=NULL
  1539   1539   } {1 {corrupt database}}
  1540   1540   
  1541   1541   finish_test
  1542         -

Changes to test/where9.test.

   783    783        WHERE (+b IS NULL AND c NOT NULL AND d NOT NULL)
   784    784           OR (b NOT NULL AND c IS NULL AND d NOT NULL)
   785    785           OR (b NOT NULL AND c NOT NULL AND d IS NULL)
   786    786     }
   787    787   } {1 {no query solution}}
   788    788   
   789    789   set solution_possible 0
   790         -ifcapable stat4||stat3 {
          790  +ifcapable stat4 {
   791    791     if {[permutation] != "no_optimization"} { set solution_possible 1 }
   792    792   }
   793    793   if $solution_possible {
   794    794     # When STAT3 is enabled, the "b NOT NULL" terms get translated
   795    795     # into b>NULL, which can be satified by the index t1b.  It is a very
   796    796     # expensive way to do the query, but it works, and so a solution is possible.
   797    797     do_test where9-6.8.3-stat4 {
................................................................................
   856    856       CREATE INDEX t5ye ON t5(y, e);
   857    857       CREATE INDEX t5yf ON t5(y, f);
   858    858       CREATE INDEX t5yg ON t5(y, g);
   859    859       CREATE TABLE t6(a, b, c, e, d, f, g, x, y);
   860    860       INSERT INTO t6 SELECT * FROM t5;
   861    861       ANALYZE t5;
   862    862     }
   863         -  ifcapable stat3 {
   864         -    sqlite3 db2 test.db
   865         -    db2 eval { DROP TABLE IF EXISTS sqlite_stat3 }
   866         -    db2 close
   867         -  }
   868    863   } {}
   869    864   do_test where9-7.1.1 {
   870    865     count_steps {
   871    866       SELECT a FROM t5 WHERE x='y' AND (b=913 OR c=27027) ORDER BY a;
   872    867     }
   873    868   } {79 81 83 scan 0 sort 1}
   874    869   do_test where9-7.1.2 {

Deleted test/wild001.test.

     1         -# 2013-07-01
     2         -#
     3         -# The author disclaims copyright to this source code.  In place of
     4         -# a legal notice, here is a blessing:
     5         -#
     6         -#    May you do good and not evil.
     7         -#    May you find forgiveness for yourself and forgive others.
     8         -#    May you share freely, never taking more than you give.
     9         -#
    10         -#***********************************************************************
    11         -# 
    12         -# This is a test case from content taken "from the wild".  In this
    13         -# particular instance, the query was provided with permission by
    14         -# Elan Feingold on 2013-06-27.  His message on the SQLite mailing list
    15         -# on that date reads:
    16         -#
    17         -#------------------------------------------------------------------------------
    18         -# > Can you send (1) the schema (2) the query that is giving problems, and (3)
    19         -# > the content of the sqlite_stat1 table after you have run ANALYZE?   If you
    20         -# > can combine all of the above into a script, that would be great!
    21         -# >
    22         -# > If you send (1..3) above and you give us written permission to include the
    23         -# > query in our test suite, that would be off-the-chain terrific.
    24         -#
    25         -# Please find items 1..3 in this file: http://www.plexapp.com/elan/sqlite_bug.txt
    26         -# 
    27         -# You have our permission to include the query in your test suite.
    28         -# 
    29         -# Thanks for an amazing product.
    30         -#-----------------------------------------------------------------------------
    31         -#
    32         -# This test case merely creates the schema and populates SQLITE_STAT1 and
    33         -# SQLITE_STAT3 then runs an EXPLAIN QUERY PLAN to ensure that the right plan
    34         -# is discovered.  This test case may need to be adjusted for future revisions
    35         -# of the query planner manage to select a better query plan.  The query plan
    36         -# shown here is known to be very fast with the original data.
    37         -#
    38         -# This test should work the same with and without SQLITE_ENABLE_STAT3
    39         -#
    40         -###############################################################################
    41         -
    42         -set testdir [file dirname $argv0]
    43         -source $testdir/tester.tcl
    44         -
    45         -ifcapable !stat3 {
    46         -  finish_test
    47         -  return
    48         -}
    49         -
    50         -do_execsql_test wild001.01 {
    51         -  CREATE TABLE "items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "secid" integer, "parent_id" integer, "metadata_type" integer, "guid" varchar(255), "media_item_count" integer, "title" varchar(255), "title_sort" varchar(255) COLLATE NOCASE, "original_title" varchar(255), "studio" varchar(255), "rating" float, "rating_count" integer, "tagline" varchar(255), "summary" text, "trivia" text, "quotes" text, "content_rating" varchar(255), "content_rating_age" integer, "index" integer, "absolute_index" integer, "duration" integer, "user_thumb_url" varchar(255), "user_art_url" varchar(255), "user_banner_url" varchar(255), "user_music_url" varchar(255), "user_fields" varchar(255), "tags_genre" varchar(255), "tags_collection" varchar(255), "tags_director" varchar(255), "tags_writer" varchar(255), "tags_star" varchar(255), "originally_available_at" datetime, "available_at" datetime, "expires_at" datetime, "refreshed_at" datetime, "year" integer, "added_at" datetime, "created_at" datetime, "updated_at" datetime, "deleted_at" datetime, "tags_country" varchar(255), "extra_data" varchar(255), "hash" varchar(255));
    52         -  CREATE INDEX "i_secid" ON "items" ("secid" );
    53         -  CREATE INDEX "i_parent_id" ON "items" ("parent_id" );
    54         -  CREATE INDEX "i_created_at" ON "items" ("created_at" );
    55         -  CREATE INDEX "i_index" ON "items" ("index" );
    56         -  CREATE INDEX "i_title" ON "items" ("title" );
    57         -  CREATE INDEX "i_title_sort" ON "items" ("title_sort" );
    58         -  CREATE INDEX "i_guid" ON "items" ("guid" );
    59         -  CREATE INDEX "i_metadata_type" ON "items" ("metadata_type" );
    60         -  CREATE INDEX "i_deleted_at" ON "items" ("deleted_at" );
    61         -  CREATE INDEX "i_secid_ex1" ON "items" ("secid", "metadata_type", "added_at" );
    62         -  CREATE INDEX "i_hash" ON "items" ("hash" );
    63         -  CREATE TABLE "settings" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "account_id" integer, "guid" varchar(255), "rating" float, "view_offset" integer, "view_count" integer, "last_viewed_at" datetime, "created_at" datetime, "updated_at" datetime);
    64         -  CREATE INDEX "s_account_id" ON "settings" ("account_id" );
    65         -  CREATE INDEX "s_guid" ON "settings" ("guid" );
    66         -  ANALYZE;
    67         -  INSERT INTO sqlite_stat1 VALUES('settings','s_guid','4740 1');
    68         -  INSERT INTO sqlite_stat1 VALUES('settings','s_account_id','4740 4740');
    69         -  INSERT INTO sqlite_stat1 VALUES('items','i_hash','27316 2');
    70         -  INSERT INTO sqlite_stat1 VALUES('items','i_secid_ex1','27316 6829 4553 3');
    71         -  INSERT INTO sqlite_stat1 VALUES('items','i_deleted_at','27316 27316');
    72         -  INSERT INTO sqlite_stat1 VALUES('items','i_metadata_type','27316 6829');
    73         -  INSERT INTO sqlite_stat1 VALUES('items','i_guid','27316 2');
    74         -  INSERT INTO sqlite_stat1 VALUES('items','i_title_sort','27316 2');
    75         -  INSERT INTO sqlite_stat1 VALUES('items','i_title','27316 2');
    76         -  INSERT INTO sqlite_stat1 VALUES('items','i_index','27316 144');
    77         -  INSERT INTO sqlite_stat1 VALUES('items','i_created_at','27316 2');
    78         -  INSERT INTO sqlite_stat1 VALUES('items','i_parent_id','27316 15');
    79         -  INSERT INTO sqlite_stat1 VALUES('items','i_secid','27316 6829');
    80         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,150,150,'com.plexapp.agents.thetvdb://153021/2/9?lang=en');
    81         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,198,198,'com.plexapp.agents.thetvdb://194031/1/10?lang=en');
    82         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,526,526,'com.plexapp.agents.thetvdb://71256/12/92?lang=en');
    83         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,923,923,'com.plexapp.agents.thetvdb://71256/15/16?lang=en');
    84         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1008,1008,'com.plexapp.agents.thetvdb://71256/15/93?lang=en');
    85         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1053,1053,'com.plexapp.agents.thetvdb://71256/16/21?lang=en');
    86         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1068,1068,'com.plexapp.agents.thetvdb://71256/16/35?lang=en');
    87         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1235,1235,'com.plexapp.agents.thetvdb://71256/17/44?lang=en');
    88         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1255,1255,'com.plexapp.agents.thetvdb://71256/17/62?lang=en');
    89         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1573,1573,'com.plexapp.agents.thetvdb://71663/20/9?lang=en');
    90         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,1580,1580,'com.plexapp.agents.thetvdb://71663/21/16?lang=en');
    91         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2000,2000,'com.plexapp.agents.thetvdb://73141/9/8?lang=en');
    92         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2107,2107,'com.plexapp.agents.thetvdb://73244/6/17?lang=en');
    93         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2256,2256,'com.plexapp.agents.thetvdb://74845/4/7?lang=en');
    94         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2408,2408,'com.plexapp.agents.thetvdb://75978/2/21?lang=en');
    95         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2634,2634,'com.plexapp.agents.thetvdb://79126/1/1?lang=en');
    96         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,2962,2962,'com.plexapp.agents.thetvdb://79274/3/94?lang=en');
    97         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3160,3160,'com.plexapp.agents.thetvdb://79274/5/129?lang=en');
    98         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3161,3161,'com.plexapp.agents.thetvdb://79274/5/12?lang=en');
    99         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3688,3688,'com.plexapp.agents.thetvdb://79274/8/62?lang=en');
   100         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,3714,3714,'com.plexapp.agents.thetvdb://79274/8/86?lang=en');
   101         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,4002,4002,'com.plexapp.agents.thetvdb://79590/13/17?lang=en');
   102         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,4215,4215,'com.plexapp.agents.thetvdb://80727/3/6?lang=en');
   103         -  INSERT INTO sqlite_stat3 VALUES('settings','s_guid',1,4381,4381,'com.plexapp.agents.thetvdb://83462/3/24?lang=en');
   104         -  INSERT INTO sqlite_stat3 VALUES('settings','s_account_id',4740,0,0,1);
   105         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,1879,1879,'1113f632ccd52ec8b8d7ca3d6d56da4701e48018');
   106         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,2721,2721,'1936154b97bb5567163edaebc2806830ae419ccf');
   107         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,3035,3035,'1c122331d4b7bfa0dc2c003ab5fb4f7152b9987a');
   108         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,3393,3393,'1f81bdbc9acc3321dc592b1a109ca075731b549a');
   109         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,6071,6070,'393cf7713efb4519c7a3d1d5403f0d945d15a16a');
   110         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,7462,7461,'4677dd37011f8bd9ae7fbbdd3af6dcd8a5b4ab2d');
   111         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,8435,8434,'4ffa339485334e81a5e12e03a63b6508d76401cf');
   112         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,8716,8714,'52a093852e6599dd5004857b7ff5b5b82c7cdb25');
   113         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,9107,9104,'561183e39f866d97ec728e9ff16ac4ad01466111');
   114         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,10942,10939,'66e99b72e29610f49499ae09ee04a376210d1f08');
   115         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,12143,12139,'71f0602427e173dc2c551535f73fdb6885fe4302');
   116         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,14962,14958,'8ca8e4dfba696019830c19ab8a32c7ece9d8534b');
   117         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,15179,15174,'8ebf1a5cf33f8ada1fc5853ac06ac4d7e074f825');
   118         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,15375,15370,'908bc211bebdf21c79d2d2b54ebaa442ac1f5cae');
   119         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,18215,18210,'ab29e4e18ec5a14fef95aa713d69e31c045a22c1');
   120         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,18615,18610,'ae84c008cc0c338bf4f28d798a88575746452f6d');
   121         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,18649,18644,'aec7c901353e115aa5307e94018ba7507bec3a45');
   122         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,19517,19512,'b75025fbf2e9c504e3c1197ff1b69250402a31f8');
   123         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,21251,21245,'c7d32f0e3a8f3a0a3dbd00833833d2ccee62f0fd');
   124         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,23616,23610,'dd5ff61479a9bd4100de802515d9dcf72d46f07a');
   125         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,24287,24280,'e3db00034301b7555419d4ef6f64769298d5845e');
   126         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,24949,24942,'ea336abd197ecd7013854a25a4f4eb9dea7927c6');
   127         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',1,25574,25567,'f018ea5182ec3f32768ca1c3cefbf3ad160ec20b');
   128         -  INSERT INTO sqlite_stat3 VALUES('items','i_hash',2,26139,26132,'f53709a8d81c12cb0f4f8d58004a25dd063de67c');
   129         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',25167,0,0,2);
   130         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',736,25167,1,3);
   131         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',15,25903,2,4);
   132         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid_ex1',1398,25918,3,5);
   133         -  INSERT INTO sqlite_stat3 VALUES('items','i_deleted_at',27316,0,0,NULL);
   134         -  INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',2149,0,0,1);
   135         -  INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',411,2149,1,2);
   136         -  INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',1440,2560,2,3);
   137         -  INSERT INTO sqlite_stat3 VALUES('items','i_metadata_type',23316,4000,3,4);
   138         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,215,215,'com.plexapp.agents.imdb://tt0065702?lang=en');
   139         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,711,711,'com.plexapp.agents.imdb://tt0198781?lang=en');
   140         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,987,986,'com.plexapp.agents.imdb://tt0454876?lang=en');
   141         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1004,1002,'com.plexapp.agents.imdb://tt0464154?lang=en');
   142         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1056,1053,'com.plexapp.agents.imdb://tt0499549?lang=en');
   143         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1120,1116,'com.plexapp.agents.imdb://tt0903624?lang=en');
   144         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1250,1245,'com.plexapp.agents.imdb://tt1268799?lang=en');
   145         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1270,1264,'com.plexapp.agents.imdb://tt1320261?lang=en');
   146         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',2,1376,1369,'com.plexapp.agents.imdb://tt1772341?lang=en');
   147         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,3035,3027,'com.plexapp.agents.thetvdb://153021/3/14?lang=en');
   148         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,6071,6063,'com.plexapp.agents.thetvdb://71173/1/18?lang=en');
   149         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,6342,6334,'com.plexapp.agents.thetvdb://71256/13/4?lang=en');
   150         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,9107,9099,'com.plexapp.agents.thetvdb://72389/2/19?lang=en');
   151         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,11740,11732,'com.plexapp.agents.thetvdb://73893/2/13?lang=en');
   152         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,12143,12135,'com.plexapp.agents.thetvdb://73976/4/23?lang=en');
   153         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,15179,15171,'com.plexapp.agents.thetvdb://75897/16/12?lang=en');
   154         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,17408,17400,'com.plexapp.agents.thetvdb://76808/2/16?lang=en');
   155         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,17984,17976,'com.plexapp.agents.thetvdb://77068/1/16?lang=en');
   156         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,18215,18207,'com.plexapp.agents.thetvdb://77259/1/1?lang=en');
   157         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,21251,21243,'com.plexapp.agents.thetvdb://78957/8/2?lang=en');
   158         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,24287,24279,'com.plexapp.agents.thetvdb://80337/5/8?lang=en');
   159         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,25513,25505,'com.plexapp.agents.thetvdb://82226/6?lang=en');
   160         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,25548,25540,'com.plexapp.agents.thetvdb://82339/2/10?lang=en');
   161         -  INSERT INTO sqlite_stat3 VALUES('items','i_guid',1,26770,26762,'com.plexapp.agents.thetvdb://86901/1/3?lang=en');
   162         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1524,0,0,'');
   163         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',2,3034,1391,'Attack of the Giant Squid');
   164         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',51,4742,2895,'Brad Sherwood');
   165         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',11,4912,2996,'Brian Williams');
   166         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',39,5847,3857,'Chip Esten');
   167         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,6071,4015,'Chuck Versus the DeLorean');
   168         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',12,7625,5436,'Denny Siegel');
   169         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',30,8924,6618,'Episode 1');
   170         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',29,9015,6629,'Episode 2');
   171         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',32,9082,6643,'Episode 3');
   172         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',28,9135,6654,'Episode 4');
   173         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',26,9183,6665,'Episode 5');
   174         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',27,9229,6677,'Episode 6');
   175         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',22,9266,6688,'Episode 7');
   176         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',20,9298,6699,'Episode 8');
   177         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',55,11750,8817,'Greg Proops');
   178         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,12143,9120,'Hardware Jungle');
   179         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',33,14712,11435,'Kathy Greenwood');
   180         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',3,15179,11840,'Last Call');
   181         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,18215,14601,'Nature or Nurture?');
   182         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',12,18241,14623,'Neil DeGrasse Tyson');
   183         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',68,19918,16144,'Pilot');
   184         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',7,21251,17298,'Reza Aslan');
   185         -  INSERT INTO sqlite_stat3 VALUES('items','i_title_sort',1,24287,20035,'Technoviking');
   186         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',1524,0,0,'');
   187         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',1,3035,1429,'Anderson Can''t Dance');
   188         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',51,4782,2991,'Brad Sherwood');
   189         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',11,4936,3079,'Brian Williams');
   190         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',39,5694,3783,'Chip Esten');
   191         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',1,6071,4100,'Clive Warren');
   192         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',12,7144,5078,'Denny Siegel');
   193         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',30,8249,6097,'Episode 1');
   194         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',29,8340,6108,'Episode 2');
   195         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',32,8407,6122,'Episode 3');
   196         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',28,8460,6133,'Episode 4');
   197         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',26,8508,6144,'Episode 5');
   198         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',27,8554,6156,'Episode 6');
   199         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',22,8591,6167,'Episode 7');
   200         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',20,8623,6178,'Episode 8');
   201         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',1,9107,6537,'Fat Albert and the Cosby Kids');
   202         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',55,10539,7843,'Greg Proops');
   203         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',1,12143,9276,'Iron Age Remains');
   204         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',33,13118,10143,'Kathy Greenwood');
   205         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',1,15179,11972,'Mink');
   206         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',68,17411,14035,'Pilot');
   207         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',2,18214,14727,'Reflections');
   208         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',4,21250,17481,'The Apartment');
   209         -  INSERT INTO sqlite_stat3 VALUES('items','i_title',1,24287,20283,'The Simpsons Already Did It');
   210         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',4315,95,2,1);
   211         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1553,4410,3,2);
   212         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1485,5963,4,3);
   213         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1414,7448,5,4);
   214         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1367,8862,6,5);
   215         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1328,10229,7,6);
   216         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1161,11557,8,7);
   217         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1108,12718,9,8);
   218         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1033,13826,10,9);
   219         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',1014,14859,11,10);
   220         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',929,15873,12,11);
   221         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',906,16802,13,12);
   222         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',844,17708,14,13);
   223         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',690,18552,15,14);
   224         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',655,19242,16,15);
   225         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',625,19897,17,16);
   226         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',579,20522,18,17);
   227         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',555,21101,19,18);
   228         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',526,21656,20,19);
   229         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',501,22182,21,20);
   230         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',459,22683,22,21);
   231         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',439,23142,23,22);
   232         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',315,23581,24,23);
   233         -  INSERT INTO sqlite_stat3 VALUES('items','i_index',192,24177,26,25);
   234         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1851,0,0,NULL);
   235         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',373,1857,2,'2011-10-22 14:54:39');
   236         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',595,2230,3,'2011-10-22 14:54:41');
   237         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',337,2825,4,'2011-10-22 14:54:43');
   238         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',361,3378,8,'2011-10-22 14:54:54');
   239         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',160,3739,9,'2011-10-22 14:54:56');
   240         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',315,4000,11,'2011-10-22 14:54:59');
   241         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',321,4334,13,'2011-10-22 14:55:02');
   242         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1292,4723,16,'2011-10-22 14:55:06');
   243         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',161,6015,17,'2011-10-22 14:55:07');
   244         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1,9107,2677,'2012-09-04 18:07:50');
   245         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',313,9717,3270,'2012-10-18 16:50:21');
   246         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',450,10030,3271,'2012-10-18 16:50:22');
   247         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',389,10668,3275,'2012-10-18 16:50:26');
   248         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',796,11057,3276,'2012-10-18 16:51:06');
   249         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',161,12041,3280,'2012-10-19 19:52:37');
   250         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',135,13281,4186,'2013-02-19 00:56:10');
   251         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1063,13416,4187,'2013-02-19 00:56:11');
   252         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',797,14479,4188,'2013-02-19 00:56:13');
   253         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',147,15276,4189,'2013-02-19 00:56:15');
   254         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',346,15423,4190,'2013-02-19 00:56:16');
   255         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1,18215,6436,'2013-05-05 14:09:54');
   256         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',2,21251,8122,'2013-05-24 15:25:45');
   257         -  INSERT INTO sqlite_stat3 VALUES('items','i_created_at',1,24287,11116,'2013-05-26 14:17:39');
   258         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',2560,0,0,NULL);
   259         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',18,3022,31,2350);
   260         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',10,6068,285,8150);
   261         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',158,6346,315,8949);
   262         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',34,9094,562,18831);
   263         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',20,12139,794,22838);
   264         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',134,14033,886,24739);
   265         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',159,14167,887,24740);
   266         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,14326,888,24741);
   267         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,14487,889,24742);
   268         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',124,14648,890,24743);
   269         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',157,14772,891,24744);
   270         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',126,15043,894,24747);
   271         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',40,15169,895,24748);
   272         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,15243,898,24753);
   273         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',138,15404,899,24754);
   274         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',160,15542,900,24755);
   275         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,15702,901,24756);
   276         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',161,15863,902,24757);
   277         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',124,16024,903,24758);
   278         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',155,16148,904,24759);
   279         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',26,18208,1043,29704);
   280         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',2,21251,1282,32952);
   281         -  INSERT INTO sqlite_stat3 VALUES('items','i_parent_id',13,24279,1583,36068);
   282         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid',25167,0,0,2);
   283         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid',736,25167,1,3);
   284         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid',15,25903,2,4);
   285         -  INSERT INTO sqlite_stat3 VALUES('items','i_secid',1398,25918,3,5);
   286         -  ANALYZE sqlite_master;
   287         -  
   288         -  explain query plan
   289         -  select items.title
   290         -    from items
   291         -         join items as child on child.parent_id=items.id
   292         -         join items as grandchild on grandchild.parent_id=child.id
   293         -         join settings
   294         -                    on settings.guid=grandchild.guid
   295         -                   and settings.account_id=1
   296         -   where items.metadata_type=2
   297         -     and items.secid=2
   298         -     and settings.last_viewed_at is not null
   299         -   group by items.id
   300         -   order by settings.last_viewed_at desc
   301         -   limit 10;
   302         -} [list \
   303         - 0 0 3 {SEARCH TABLE settings USING INDEX s_account_id (account_id=?)} \
   304         - 0 1 2 {SEARCH TABLE items AS grandchild USING INDEX i_guid (guid=?)} \
   305         - 0 2 1 {SEARCH TABLE items AS child USING INTEGER PRIMARY KEY (rowid=?)} \
   306         - 0 3 0 {SEARCH TABLE items USING INTEGER PRIMARY KEY (rowid=?)} \
   307         - 0 0 0 {USE TEMP B-TREE FOR GROUP BY} \
   308         - 0 0 0 {USE TEMP B-TREE FOR ORDER BY}]
   309         -
   310         -
   311         -finish_test

Changes to test/window4.tcl.

   381    381   
   382    382   execsql_test 11.4 {
   383    383     SELECT * FROM (
   384    384       SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8
   385    385     ) sub;
   386    386   }
   387    387   
          388  +execsql_test 12.0 {
          389  +  DROP TABLE IF EXISTS t2;
          390  +  CREATE TABLE t2(a INTEGER);
          391  +  INSERT INTO t2 VALUES(1), (2), (3);
          392  +}
          393  +
          394  +execsql_test 12.1 {
          395  +  SELECT (SELECT min(a) OVER ()) FROM t2
          396  +}
          397  +
          398  +execsql_float_test 12.2 {
          399  +  SELECT (SELECT avg(a)) FROM t2 ORDER BY 1
          400  +}
          401  +
          402  +execsql_float_test 12.3 {
          403  +  SELECT 
          404  +    (SELECT avg(a) UNION SELECT min(a) OVER ()) 
          405  +  FROM t2 GROUP BY a
          406  +  ORDER BY 1
          407  +}
   388    408   
   389    409   finish_test
   390    410   

Changes to test/window4.test.

  1319   1319   } {0   1   2}
  1320   1320   
  1321   1321   do_execsql_test 11.4 {
  1322   1322     SELECT * FROM (
  1323   1323       SELECT NTILE(256) OVER (ORDER BY total) - 1 AS nt FROM t8
  1324   1324     ) sub;
  1325   1325   } {0   1   2}
         1326  +
         1327  +do_execsql_test 12.0 {
         1328  +  DROP TABLE IF EXISTS t2;
         1329  +  CREATE TABLE t2(a INTEGER);
         1330  +  INSERT INTO t2 VALUES(1), (2), (3);
         1331  +} {}
         1332  +
         1333  +do_execsql_test 12.1 {
         1334  +  SELECT (SELECT min(a) OVER ()) FROM t2
         1335  +} {1   2   3}
         1336  +
         1337  +
         1338  +do_test 12.2 {
         1339  +  set myres {}
         1340  +  foreach r [db eval {SELECT (SELECT avg(a)) FROM t2 ORDER BY 1}] {
         1341  +    lappend myres [format %.4f [set r]]
         1342  +  }
         1343  +  set res2 {2.0000}
         1344  +  set i 0
         1345  +  foreach r [set myres] r2 [set res2] {
         1346  +    if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} {
         1347  +      error "list element [set i] does not match: got=[set r] expected=[set r2]"
         1348  +    }
         1349  +    incr i
         1350  +  }
         1351  +  set {} {}
         1352  +} {}
         1353  +
         1354  +
         1355  +do_test 12.3 {
         1356  +  set myres {}
         1357  +  foreach r [db eval {SELECT 
         1358  +    (SELECT avg(a) UNION SELECT min(a) OVER ()) 
         1359  +  FROM t2 GROUP BY a
         1360  +  ORDER BY 1}] {
         1361  +    lappend myres [format %.4f [set r]]
         1362  +  }
         1363  +  set res2 {1.0000 2.0000 3.0000}
         1364  +  set i 0
         1365  +  foreach r [set myres] r2 [set res2] {
         1366  +    if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} {
         1367  +      error "list element [set i] does not match: got=[set r] expected=[set r2]"
         1368  +    }
         1369  +    incr i
         1370  +  }
         1371  +  set {} {}
         1372  +} {}
  1326   1373   
  1327   1374   finish_test

Changes to test/window6.test.

   364    364   } {
   365    365     fifteen fifteen 
   366    366     ten     fifteen.ten 
   367    367     thirty  fifteen.ten.thirty
   368    368   }
   369    369   
   370    370   finish_test
   371         -

Changes to test/window9.test.

   127    127           SELECT min(a) OVER (),
   128    128           (abs(row_number() OVER())+22)/19,
   129    129           max(a) OVER () FROM t1
   130    130           ) AS y FROM t2
   131    131         );
   132    132   } {1 {sub-select returns 3 columns - expected 1}}
   133    133   
          134  +#-------------------------------------------------------------------------
          135  +reset_db
          136  +do_execsql_test 4.0 {
          137  +  CREATE TABLE t1(a, b TEXT);
          138  +  INSERT INTO t1 VALUES('A', 1), ('A', 2), ('2', 1), ('2', 2);
          139  +}
          140  +
          141  +do_execsql_test 4.1.1 {
          142  +  SELECT b, b=count(*), '1,2'                   FROM t1 GROUP BY b;
          143  +} {1 0 1,2 2 1 1,2}
          144  +do_execsql_test 4.1.2 {
          145  +  SELECT b, b=count(*), group_concat(b) OVER () FROM t1 GROUP BY b;
          146  +} {1 0 1,2 2 1 1,2}
          147  +
          148  +#--------------------------------------------------------------------------
          149  +reset_db
          150  +do_execsql_test 5.0 {
          151  +  CREATE TABLE t1(a, b, c, d, e);
          152  +  CREATE INDEX i1 ON t1(a, b, c, d, e);
          153  +}
          154  +
          155  +foreach {tn sql} {
          156  +  1 {
          157  +    SELECT 
          158  +      sum(e) OVER (),
          159  +      sum(e) OVER (ORDER BY a),
          160  +      sum(e) OVER (PARTITION BY a ORDER BY b),
          161  +      sum(e) OVER (PARTITION BY a, b ORDER BY c),
          162  +      sum(e) OVER (PARTITION BY a, b, c ORDER BY d)
          163  +    FROM t1;
          164  +  }
          165  +  2 {
          166  +    SELECT sum(e) OVER (PARTITION BY a ORDER BY b) FROM t1 ORDER BY a;
          167  +  }
          168  +} {
          169  +  do_test 5.1.$tn {
          170  +    execsql "EXPLAIN QUERY PLAN $sql"
          171  +  } {~/ORDER/}
          172  +}
          173  +
          174  +
   134    175   
   135    176   finish_test
   136         -

Changes to test/without_rowid1.test.

    98     98   
    99     99   # Verify that ANALYZE works
   100    100   #
   101    101   do_execsql_test without_rowid1-1.50 {
   102    102     ANALYZE;
   103    103     SELECT * FROM sqlite_stat1 ORDER BY idx;
   104    104   } {t1 t1 {4 2 1} t1 t1bd {4 2 2}}
   105         -ifcapable stat3 {
   106         -  do_execsql_test without_rowid1-1.51 {
   107         -    SELECT DISTINCT tbl, idx FROM sqlite_stat3 ORDER BY idx;
   108         -  } {t1 t1 t1 t1bd}
   109         -}
   110    105   ifcapable stat4 {
   111    106     do_execsql_test without_rowid1-1.52 {
   112    107       SELECT DISTINCT tbl, idx FROM sqlite_stat4 ORDER BY idx;
   113    108     } {t1 t1 t1 t1bd}
   114    109   }
   115    110   
   116    111   #----------