/ Check-in [7e1acb39]
Login

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

Overview
Comment:Merge in the latest trunk changes, including partial indexes, the MAX_PATH fix in os_win.c, and the sqlite3_cancel_auto_extension() API.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 7e1acb390770d1bd189fac7a3a7f96106f96e3a4
User & Date: drh 2013-08-02 20:44:48
Context
2013-08-06
14:52
Merge in the latest changes from trunk. check-in: 69d5bed0 user: drh tags: sessions
2013-08-02
20:44
Merge in the latest trunk changes, including partial indexes, the MAX_PATH fix in os_win.c, and the sqlite3_cancel_auto_extension() API. check-in: 7e1acb39 user: drh tags: sessions
20:11
Add NEVER() and ALWAYS() macros on some unreachable yet prudent branches. check-in: c5c0a8ab user: drh tags: trunk
2013-07-11
15:31
Merge recent trunk changes (such as the query_only PRAGMA, the defer_foreign_keys PRAGMA, and the SQLITE_DBSTATUS_DEFERRED_FKS parameter to sqlite3_db_status()) into the sessions branch. check-in: 8dfc0b78 user: drh tags: sessions
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.msc.

   472    472   NAWK = gawk.exe
   473    473   
   474    474   # You should not have to change anything below this line
   475    475   ###############################################################################
   476    476   
   477    477   # Object files for the SQLite library (non-amalgamation).
   478    478   #
   479         -LIBOBJS0 = alter.lo analyze.lo attach.lo auth.lo \
          479  +LIBOBJS0 = vdbe.lo parse.lo alter.lo analyze.lo attach.lo auth.lo \
   480    480            backup.lo bitvec.lo btmutex.lo btree.lo build.lo \
   481    481            callback.lo complete.lo ctime.lo date.lo delete.lo \
   482    482            expr.lo fault.lo fkey.lo \
   483    483            fts3.lo fts3_aux.lo fts3_expr.lo fts3_hash.lo fts3_icu.lo \
   484    484            fts3_porter.lo fts3_snippet.lo fts3_tokenizer.lo fts3_tokenizer1.lo \
   485    485            fts3_tokenize_vtab.lo fts3_unicode.lo fts3_unicode2.lo fts3_write.lo \
   486    486            func.lo global.lo hash.lo \
   487    487            icu.lo insert.lo journal.lo legacy.lo loadext.lo \
   488    488            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   489    489            memjournal.lo \
   490    490            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
   491    491            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   492         -         pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
          492  +         pager.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   493    493            random.lo resolve.lo rowset.lo rtree.lo \
   494    494            sqlite3session.lo select.lo status.lo \
   495    495            table.lo tokenize.lo trigger.lo \
   496    496            update.lo util.lo vacuum.lo \
   497         -         vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
          497  +         vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   498    498            vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo
   499    499   
   500    500   # Object files for the amalgamation.
   501    501   #
   502    502   LIBOBJS1 = sqlite3.lo
   503    503   
   504    504   # Determine the real value of LIBOBJ based on the 'configure' script
................................................................................
  1258   1258   
  1259   1259   soaktest:	testfixture.exe sqlite3.exe
  1260   1260   	.\testfixture.exe $(TOP)\test\all.test -soak=1
  1261   1261   
  1262   1262   fulltestonly:	testfixture.exe sqlite3.exe
  1263   1263   	.\testfixture.exe $(TOP)\test\full.test
  1264   1264   
         1265  +queryplantest:	testfixture.exe sqlite3.exe
         1266  +	.\testfixture.exe $(TOP)\test\permutations.test queryplanner
         1267  +
  1265   1268   test:	testfixture.exe sqlite3.exe
  1266   1269   	.\testfixture.exe $(TOP)\test\veryquick.test
  1267   1270   
  1268   1271   sqlite3_analyzer.c: sqlite3.c $(TOP)\src\test_stat.c $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
  1269   1272   	copy sqlite3.c + $(TOP)\src\test_stat.c + $(TOP)\src\tclsqlite.c $@
  1270   1273   	echo static const char *tclsh_main_loop(void){ >> $@
  1271   1274   	echo static const char *zMainloop = >> $@

Changes to ext/misc/closure.c.

   492    492     while( isspace(zStr[i]) ){ i++; }
   493    493     return zStr+i;
   494    494   }
   495    495   
   496    496   /*
   497    497   ** xConnect/xCreate method for the closure module. Arguments are:
   498    498   **
   499         -**   argv[0]    -> module name  ("approximate_match")
          499  +**   argv[0]    -> module name  ("transitive_closure")
   500    500   **   argv[1]    -> database name
   501    501   **   argv[2]    -> table name
   502    502   **   argv[3...] -> arguments
   503    503   */
   504    504   static int closureConnect(
   505    505     sqlite3 *db,
   506    506     void *pAux,
................................................................................
   903    903     if( seenMatch && (iPlan&1)==0 ) rCost *= 1e30;
   904    904     pIdxInfo->estimatedCost = rCost;
   905    905      
   906    906     return SQLITE_OK;
   907    907   }
   908    908   
   909    909   /*
   910         -** A virtual table module that implements the "approximate_match".
          910  +** A virtual table module that implements the "transitive_closure".
   911    911   */
   912    912   static sqlite3_module closureModule = {
   913    913     0,                      /* iVersion */
   914    914     closureConnect,         /* xCreate */
   915    915     closureConnect,         /* xConnect */
   916    916     closureBestIndex,       /* xBestIndex */
   917    917     closureDisconnect,      /* xDisconnect */

Changes to ext/misc/regexp.c.

   709    709     int argc, 
   710    710     sqlite3_value **argv
   711    711   ){
   712    712     ReCompiled *pRe;          /* Compiled regular expression */
   713    713     const char *zPattern;     /* The regular expression */
   714    714     const unsigned char *zStr;/* String being searched */
   715    715     const char *zErr;         /* Compile error message */
          716  +  int setAux = 0;           /* True to invoke sqlite3_set_auxdata() */
   716    717   
   717    718     pRe = sqlite3_get_auxdata(context, 0);
   718    719     if( pRe==0 ){
   719    720       zPattern = (const char*)sqlite3_value_text(argv[0]);
   720    721       if( zPattern==0 ) return;
   721    722       zErr = re_compile(&pRe, zPattern, 0);
   722    723       if( zErr ){
................................................................................
   724    725         sqlite3_result_error(context, zErr, -1);
   725    726         return;
   726    727       }
   727    728       if( pRe==0 ){
   728    729         sqlite3_result_error_nomem(context);
   729    730         return;
   730    731       }
   731         -    sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
          732  +    setAux = 1;
   732    733     }
   733    734     zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
   734    735     if( zStr!=0 ){
   735    736       sqlite3_result_int(context, re_match(pRe, zStr, -1));
   736    737     }
          738  +  if( setAux ){
          739  +    sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
          740  +  }
   737    741   }
   738    742   
   739    743   /*
   740    744   ** Invoke this routine to register the regexp() function with the
   741    745   ** SQLite database connection.
   742    746   */
   743    747   #ifdef _WIN32

Changes to main.mk.

    47     47   TCCX =  $(TCC) $(OPTS) -I. -I$(TOP)/src -I$(TOP) 
    48     48   TCCX += -I$(TOP)/ext/rtree -I$(TOP)/ext/icu -I$(TOP)/ext/fts3
    49     49   TCCX += -I$(TOP)/ext/async
    50     50   TCCX += -I$(TOP)/ext/session
    51     51   
    52     52   # Object files for the SQLite library.
    53     53   #
    54         -LIBOBJ+= alter.o analyze.o attach.o auth.o \
           54  +LIBOBJ+= vdbe.o parse.o \
           55  +         alter.o analyze.o attach.o auth.o \
    55     56            backup.o bitvec.o btmutex.o btree.o build.o \
    56     57            callback.o complete.o ctime.o date.o delete.o expr.o fault.o fkey.o \
    57     58            fts3.o fts3_aux.o fts3_expr.o fts3_hash.o fts3_icu.o fts3_porter.o \
    58     59            fts3_snippet.o fts3_tokenizer.o fts3_tokenizer1.o \
    59     60            fts3_tokenize_vtab.o \
    60     61   	 fts3_unicode.o fts3_unicode2.o \
    61     62            fts3_write.o func.o global.o hash.o \
    62     63            icu.o insert.o journal.o legacy.o loadext.o \
    63     64            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
    64     65            memjournal.o \
    65     66            mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
    66     67            notify.o opcodes.o os.o os_unix.o os_win.o \
    67         -         pager.o parse.o pcache.o pcache1.o pragma.o prepare.o printf.o \
           68  +         pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
    68     69            random.o resolve.o rowset.o rtree.o select.o status.o \
    69     70            table.o tokenize.o trigger.o \
    70     71            update.o util.o vacuum.o \
    71         -         vdbe.o vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
           72  +         vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
    72     73   	 vdbetrace.o wal.o walker.o where.o utf.o vtab.o
    73     74   
    74     75   
    75     76   
    76     77   # All of the source code files.
    77     78   #
    78     79   SRC = \

Changes to src/analyze.c.

   437    437     int iIdxCur;                 /* Cursor open on index being analyzed */
   438    438     Vdbe *v;                     /* The virtual machine being built up */
   439    439     int i;                       /* Loop counter */
   440    440     int topOfLoop;               /* The top of the loop */
   441    441     int endOfLoop;               /* The end of the loop */
   442    442     int jZeroRows = -1;          /* Jump from here if number of rows is zero */
   443    443     int iDb;                     /* Index of database containing pTab */
          444  +  u8 needTableCnt = 1;         /* True to count the table */
   444    445     int regTabname = iMem++;     /* Register containing table name */
   445    446     int regIdxname = iMem++;     /* Register containing index name */
   446    447     int regStat1 = iMem++;       /* The stat column of sqlite_stat1 */
   447    448   #ifdef SQLITE_ENABLE_STAT3
   448    449     int regNumEq = regStat1;     /* Number of instances.  Same as regStat1 */
   449    450     int regNumLt = iMem++;       /* Number of keys less than regSample */
   450    451     int regNumDLt = iMem++;      /* Number of distinct keys less than regSample */
................................................................................
   496    497     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
   497    498       int nCol;
   498    499       KeyInfo *pKey;
   499    500       int addrIfNot = 0;           /* address of OP_IfNot */
   500    501       int *aChngAddr;              /* Array of jump instruction addresses */
   501    502   
   502    503       if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
          504  +    if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
   503    505       VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
   504    506       nCol = pIdx->nColumn;
   505    507       aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
   506    508       if( aChngAddr==0 ) continue;
   507    509       pKey = sqlite3IndexKeyinfo(pParse, pIdx);
   508    510       if( iMem+1+(nCol*2)>pParse->nMem ){
   509    511         pParse->nMem = iMem+1+(nCol*2);
................................................................................
   655    657       **        I = (K+D-1)/D
   656    658       **
   657    659       ** If K==0 then no entry is made into the sqlite_stat1 table.  
   658    660       ** If K>0 then it is always the case the D>0 so division by zero
   659    661       ** is never possible.
   660    662       */
   661    663       sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
   662         -    if( jZeroRows<0 ){
   663         -      jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
   664         -    }
          664  +    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
   665    665       for(i=0; i<nCol; i++){
   666    666         sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
   667    667         sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
   668    668         sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
   669    669         sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
   670    670         sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
   671    671         sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
   672    672         sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
   673    673       }
          674  +    if( pIdx->pPartIdxWhere!=0 ) sqlite3VdbeJumpHere(v, jZeroRows);
   674    675       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
   675    676       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
   676    677       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
   677    678       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
          679  +    if( pIdx->pPartIdxWhere==0 ) sqlite3VdbeJumpHere(v, jZeroRows);
   678    680     }
   679    681   
   680         -  /* If the table has no indices, create a single sqlite_stat1 entry
   681         -  ** containing NULL as the index name and the row count as the content.
          682  +  /* Create a single sqlite_stat1 entry containing NULL as the index
          683  +  ** name and the row count as the content.
   682    684     */
   683         -  if( pTab->pIndex==0 ){
          685  +  if( pOnlyIdx==0 && needTableCnt ){
   684    686       sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
   685    687       VdbeComment((v, "%s", pTab->zName));
   686    688       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
   687    689       sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
   688    690       jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
   689         -  }else{
          691  +    sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
          692  +    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
          693  +    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
          694  +    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
          695  +    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   690    696       sqlite3VdbeJumpHere(v, jZeroRows);
   691         -    jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
   692    697     }
   693         -  sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
   694         -  sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
   695         -  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
   696         -  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
   697         -  sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   698    698     if( pParse->nMem<regRec ) pParse->nMem = regRec;
   699         -  sqlite3VdbeJumpHere(v, jZeroRows);
   700    699   }
   701    700   
   702    701   
   703    702   /*
   704    703   ** Generate code that will cause the most recent index analysis to
   705    704   ** be loaded into internal hash tables where is can be used.
   706    705   */
................................................................................
   875    874     z = argv[2];
   876    875     for(i=0; *z && i<=n; i++){
   877    876       v = 0;
   878    877       while( (c=z[0])>='0' && c<='9' ){
   879    878         v = v*10 + c - '0';
   880    879         z++;
   881    880       }
   882         -    if( i==0 ) pTable->nRowEst = v;
   883         -    if( pIndex==0 ) break;
          881  +    if( i==0 && (pIndex==0 || pIndex->pPartIdxWhere==0) ){
          882  +      if( v>0 ) pTable->nRowEst = v;
          883  +      if( pIndex==0 ) break;
          884  +    }
   884    885       pIndex->aiRowEst[i] = v;
   885    886       if( *z==' ' ) z++;
   886    887       if( strcmp(z, "unordered")==0 ){
   887    888         pIndex->bUnordered = 1;
   888    889         break;
   889    890       }
   890    891     }

Changes to src/build.c.

   378    378   /*
   379    379   ** Reclaim the memory used by an index
   380    380   */
   381    381   static void freeIndex(sqlite3 *db, Index *p){
   382    382   #ifndef SQLITE_OMIT_ANALYZE
   383    383     sqlite3DeleteIndexSamples(db, p);
   384    384   #endif
          385  +  sqlite3ExprDelete(db, p->pPartIdxWhere);
   385    386     sqlite3DbFree(db, p->zColAff);
   386    387     sqlite3DbFree(db, p);
   387    388   }
   388    389   
   389    390   /*
   390    391   ** For the index called zIdxName which is found in the database iDb,
   391    392   ** unlike that index from its Table then remove the index from
................................................................................
  1221   1222     }else if( autoInc ){
  1222   1223   #ifndef SQLITE_OMIT_AUTOINCREMENT
  1223   1224       sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
  1224   1225          "INTEGER PRIMARY KEY");
  1225   1226   #endif
  1226   1227     }else{
  1227   1228       Index *p;
  1228         -    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);
         1229  +    p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
         1230  +                           0, sortOrder, 0);
  1229   1231       if( p ){
  1230   1232         p->autoIndex = 2;
  1231   1233       }
  1232   1234       pList = 0;
  1233   1235     }
  1234   1236   
  1235   1237   primary_key_exit:
................................................................................
  1516   1518   
  1517   1519     iDb = sqlite3SchemaToIndex(db, p->pSchema);
  1518   1520   
  1519   1521   #ifndef SQLITE_OMIT_CHECK
  1520   1522     /* Resolve names in all CHECK constraint expressions.
  1521   1523     */
  1522   1524     if( p->pCheck ){
  1523         -    SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
  1524         -    NameContext sNC;                /* Name context for pParse->pNewTable */
  1525         -    ExprList *pList;                /* List of all CHECK constraints */
  1526         -    int i;                          /* Loop counter */
  1527         -
  1528         -    memset(&sNC, 0, sizeof(sNC));
  1529         -    memset(&sSrc, 0, sizeof(sSrc));
  1530         -    sSrc.nSrc = 1;
  1531         -    sSrc.a[0].zName = p->zName;
  1532         -    sSrc.a[0].pTab = p;
  1533         -    sSrc.a[0].iCursor = -1;
  1534         -    sNC.pParse = pParse;
  1535         -    sNC.pSrcList = &sSrc;
  1536         -    sNC.ncFlags = NC_IsCheck;
  1537         -    pList = p->pCheck;
  1538         -    for(i=0; i<pList->nExpr; i++){
  1539         -      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
  1540         -        return;
  1541         -      }
  1542         -    }
         1525  +    sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
  1543   1526     }
  1544   1527   #endif /* !defined(SQLITE_OMIT_CHECK) */
  1545   1528   
  1546   1529     /* If the db->init.busy is 1 it means we are reading the SQL off the
  1547   1530     ** "sqlite_master" or "sqlite_temp_master" table on the disk.
  1548   1531     ** So do not write to the disk again.  Extract the root page number
  1549   1532     ** for the table from the db->init.newTnum field.  (The page number
................................................................................
  2387   2370     Table *pTab = pIndex->pTable;  /* The table that is indexed */
  2388   2371     int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
  2389   2372     int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
  2390   2373     int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
  2391   2374     int addr1;                     /* Address of top of loop */
  2392   2375     int addr2;                     /* Address to jump to for next iteration */
  2393   2376     int tnum;                      /* Root page of index */
         2377  +  int iPartIdxLabel;             /* Jump to this label to skip a row */
  2394   2378     Vdbe *v;                       /* Generate code into this virtual machine */
  2395   2379     KeyInfo *pKey;                 /* KeyInfo for index */
  2396   2380     int regRecord;                 /* Register holding assemblied index record */
  2397   2381     sqlite3 *db = pParse->db;      /* The database connection */
  2398   2382     int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
  2399   2383   
  2400   2384   #ifndef SQLITE_OMIT_AUTHORIZATION
................................................................................
  2426   2410   
  2427   2411     /* Open the table. Loop through all rows of the table, inserting index
  2428   2412     ** records into the sorter. */
  2429   2413     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2430   2414     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
  2431   2415     regRecord = sqlite3GetTempReg(pParse);
  2432   2416   
  2433         -  sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
         2417  +  sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1, &iPartIdxLabel);
  2434   2418     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
         2419  +  sqlite3VdbeResolveLabel(v, iPartIdxLabel);
  2435   2420     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
  2436   2421     sqlite3VdbeJumpHere(v, addr1);
  2437   2422     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
  2438   2423     if( pIndex->onError!=OE_None ){
  2439   2424       int j2 = sqlite3VdbeCurrentAddr(v) + 3;
  2440   2425       sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
  2441   2426       addr2 = sqlite3VdbeCurrentAddr(v);
................................................................................
  2478   2463     Parse *pParse,     /* All information about this parse */
  2479   2464     Token *pName1,     /* First part of index name. May be NULL */
  2480   2465     Token *pName2,     /* Second part of index name. May be NULL */
  2481   2466     SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
  2482   2467     ExprList *pList,   /* A list of columns to be indexed */
  2483   2468     int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  2484   2469     Token *pStart,     /* The CREATE token that begins this statement */
  2485         -  Token *pEnd,       /* The ")" that closes the CREATE INDEX statement */
         2470  +  Expr *pPIWhere,    /* WHERE clause for partial indices */
  2486   2471     int sortOrder,     /* Sort order of primary key when pList==NULL */
  2487   2472     int ifNotExist     /* Omit error if index already exists */
  2488   2473   ){
  2489   2474     Index *pRet = 0;     /* Pointer to return */
  2490   2475     Table *pTab = 0;     /* Table to be indexed */
  2491   2476     Index *pIndex = 0;   /* The index to be created */
  2492   2477     char *zName = 0;     /* Name of the index */
................................................................................
  2500   2485     int iDb;             /* Index of the database that is being written */
  2501   2486     Token *pName = 0;    /* Unqualified name of the index to create */
  2502   2487     struct ExprList_item *pListItem; /* For looping over pList */
  2503   2488     int nCol;
  2504   2489     int nExtra = 0;
  2505   2490     char *zExtra;
  2506   2491   
  2507         -  assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */
  2508   2492     assert( pParse->nErr==0 );      /* Never called with prior errors */
  2509   2493     if( db->mallocFailed || IN_DECLARE_VTAB ){
  2510   2494       goto exit_create_index;
  2511   2495     }
  2512   2496     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  2513   2497       goto exit_create_index;
  2514   2498     }
................................................................................
  2546   2530         /* Because the parser constructs pTblName from a single identifier,
  2547   2531         ** sqlite3FixSrcList can never fail. */
  2548   2532         assert(0);
  2549   2533       }
  2550   2534       pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
  2551   2535       assert( db->mallocFailed==0 || pTab==0 );
  2552   2536       if( pTab==0 ) goto exit_create_index;
  2553         -    assert( db->aDb[iDb].pSchema==pTab->pSchema );
         2537  +    if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
         2538  +      sqlite3ErrorMsg(pParse, 
         2539  +           "cannot create a TEMP index on non-TEMP table \"%s\"",
         2540  +           pTab->zName);
         2541  +      goto exit_create_index;
         2542  +    }
  2554   2543     }else{
  2555   2544       assert( pName==0 );
  2556   2545       assert( pStart==0 );
  2557   2546       pTab = pParse->pNewTable;
  2558   2547       if( !pTab ) goto exit_create_index;
  2559   2548       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  2560   2549     }
................................................................................
  2695   2684     memcpy(pIndex->zName, zName, nName+1);
  2696   2685     pIndex->pTable = pTab;
  2697   2686     pIndex->nColumn = pList->nExpr;
  2698   2687     pIndex->onError = (u8)onError;
  2699   2688     pIndex->uniqNotNull = onError==OE_Abort;
  2700   2689     pIndex->autoIndex = (u8)(pName==0);
  2701   2690     pIndex->pSchema = db->aDb[iDb].pSchema;
         2691  +  if( pPIWhere ){
         2692  +    sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
         2693  +    pIndex->pPartIdxWhere = pPIWhere;
         2694  +    pPIWhere = 0;
         2695  +  }
  2702   2696     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  2703   2697   
  2704   2698     /* Check to see if we should honor DESC requests on index columns
  2705   2699     */
  2706   2700     if( pDb->pSchema->file_format>=4 ){
  2707   2701       sortOrderMask = -1;   /* Honor DESC */
  2708   2702     }else{
................................................................................
  2850   2844     ** we don't want to recreate it.
  2851   2845     **
  2852   2846     ** If pTblName==0 it means this index is generated as a primary key
  2853   2847     ** or UNIQUE constraint of a CREATE TABLE statement.  Since the table
  2854   2848     ** has just been created, it contains no data and the index initialization
  2855   2849     ** step can be skipped.
  2856   2850     */
  2857         -  else{ /* if( db->init.busy==0 ) */
         2851  +  else if( pParse->nErr==0 ){
  2858   2852       Vdbe *v;
  2859   2853       char *zStmt;
  2860   2854       int iMem = ++pParse->nMem;
  2861   2855   
  2862   2856       v = sqlite3GetVdbe(pParse);
  2863   2857       if( v==0 ) goto exit_create_index;
  2864   2858   
................................................................................
  2868   2862       sqlite3BeginWriteOperation(pParse, 1, iDb);
  2869   2863       sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
  2870   2864   
  2871   2865       /* Gather the complete text of the CREATE INDEX statement into
  2872   2866       ** the zStmt variable
  2873   2867       */
  2874   2868       if( pStart ){
  2875         -      assert( pEnd!=0 );
         2869  +      int n = (pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
         2870  +      if( pName->z[n-1]==';' ) n--;
  2876   2871         /* A named index with an explicit CREATE INDEX statement */
  2877   2872         zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
  2878         -        onError==OE_None ? "" : " UNIQUE",
  2879         -        (int)(pEnd->z - pName->z) + 1,
  2880         -        pName->z);
         2873  +        onError==OE_None ? "" : " UNIQUE", n, pName->z);
  2881   2874       }else{
  2882   2875         /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
  2883   2876         /* zStmt = sqlite3MPrintf(""); */
  2884   2877         zStmt = 0;
  2885   2878       }
  2886   2879   
  2887   2880       /* Add an entry in sqlite_master for this index
................................................................................
  2929   2922       }
  2930   2923       pRet = pIndex;
  2931   2924       pIndex = 0;
  2932   2925     }
  2933   2926   
  2934   2927     /* Clean up before exiting */
  2935   2928   exit_create_index:
  2936         -  if( pIndex ){
  2937         -    sqlite3DbFree(db, pIndex->zColAff);
  2938         -    sqlite3DbFree(db, pIndex);
  2939         -  }
         2929  +  if( pIndex ) freeIndex(db, pIndex);
         2930  +  sqlite3ExprDelete(db, pPIWhere);
  2940   2931     sqlite3ExprListDelete(db, pList);
  2941   2932     sqlite3SrcListDelete(db, pTblName);
  2942   2933     sqlite3DbFree(db, zName);
  2943   2934     return pRet;
  2944   2935   }
  2945   2936   
  2946   2937   /*

Changes to src/delete.c.

   599    599     Table *pTab,       /* Table containing the row to be deleted */
   600    600     int iCur,          /* Cursor number for the table */
   601    601     int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
   602    602   ){
   603    603     int i;
   604    604     Index *pIdx;
   605    605     int r1;
          606  +  int iPartIdxLabel;
          607  +  Vdbe *v = pParse->pVdbe;
   606    608   
   607    609     for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
   608    610       if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue;
   609         -    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0);
   610         -    sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1);
          611  +    r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0, &iPartIdxLabel);
          612  +    sqlite3VdbeAddOp3(v, OP_IdxDelete, iCur+i, r1, pIdx->nColumn+1);
          613  +    sqlite3VdbeResolveLabel(v, iPartIdxLabel);
   611    614     }
   612    615   }
   613    616   
   614    617   /*
   615    618   ** Generate code that will assemble an index key and put it in register
   616    619   ** regOut.  The key with be for index pIdx which is an index on pTab.
   617    620   ** iCur is the index of a cursor open on the pTab table and pointing to
   618    621   ** the entry that needs indexing.
   619    622   **
   620    623   ** Return a register number which is the first in a block of
   621    624   ** registers that holds the elements of the index key.  The
   622    625   ** block of registers has already been deallocated by the time
   623    626   ** this routine returns.
          627  +**
          628  +** If *piPartIdxLabel is not NULL, fill it in with a label and jump
          629  +** to that label if pIdx is a partial index that should be skipped.
          630  +** A partial index should be skipped if its WHERE clause evaluates
          631  +** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
          632  +** will be set to zero which is an empty label that is ignored by
          633  +** sqlite3VdbeResolveLabel().
   624    634   */
   625    635   int sqlite3GenerateIndexKey(
   626         -  Parse *pParse,     /* Parsing context */
   627         -  Index *pIdx,       /* The index for which to generate a key */
   628         -  int iCur,          /* Cursor number for the pIdx->pTable table */
   629         -  int regOut,        /* Write the new index key to this register */
   630         -  int doMakeRec      /* Run the OP_MakeRecord instruction if true */
          636  +  Parse *pParse,       /* Parsing context */
          637  +  Index *pIdx,         /* The index for which to generate a key */
          638  +  int iCur,            /* Cursor number for the pIdx->pTable table */
          639  +  int regOut,          /* Write the new index key to this register */
          640  +  int doMakeRec,       /* Run the OP_MakeRecord instruction if true */
          641  +  int *piPartIdxLabel  /* OUT: Jump to this label to skip partial index */
   631    642   ){
   632    643     Vdbe *v = pParse->pVdbe;
   633    644     int j;
   634    645     Table *pTab = pIdx->pTable;
   635    646     int regBase;
   636    647     int nCol;
   637    648   
          649  +  if( piPartIdxLabel ){
          650  +    if( pIdx->pPartIdxWhere ){
          651  +      *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
          652  +      pParse->iPartIdxTab = iCur;
          653  +      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
          654  +                         SQLITE_JUMPIFNULL);
          655  +    }else{
          656  +      *piPartIdxLabel = 0;
          657  +    }
          658  +  }
   638    659     nCol = pIdx->nColumn;
   639    660     regBase = sqlite3GetTempRange(pParse, nCol+1);
   640    661     sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol);
   641    662     for(j=0; j<nCol; j++){
   642    663       int idx = pIdx->aiColumn[j];
   643    664       if( idx==pTab->iPKey ){
   644    665         sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j);

Changes to src/expr.c.

  2358   2358           sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
  2359   2359                                 pCol->iSorterColumn, target);
  2360   2360           break;
  2361   2361         }
  2362   2362         /* Otherwise, fall thru into the TK_COLUMN case */
  2363   2363       }
  2364   2364       case TK_COLUMN: {
  2365         -      if( pExpr->iTable<0 ){
  2366         -        /* This only happens when coding check constraints */
  2367         -        assert( pParse->ckBase>0 );
  2368         -        inReg = pExpr->iColumn + pParse->ckBase;
  2369         -      }else{
  2370         -        inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
  2371         -                                 pExpr->iColumn, pExpr->iTable, target,
  2372         -                                 pExpr->op2);
         2365  +      int iTab = pExpr->iTable;
         2366  +      if( iTab<0 ){
         2367  +        if( pParse->ckBase>0 ){
         2368  +          /* Generating CHECK constraints or inserting into partial index */
         2369  +          inReg = pExpr->iColumn + pParse->ckBase;
         2370  +          break;
         2371  +        }else{
         2372  +          /* Deleting from a partial index */
         2373  +          iTab = pParse->iPartIdxTab;
         2374  +        }
  2373   2375         }
         2376  +      inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
         2377  +                               pExpr->iColumn, iTab, target,
         2378  +                               pExpr->op2);
  2374   2379         break;
  2375   2380       }
  2376   2381       case TK_INTEGER: {
  2377   2382         codeInteger(pParse, pExpr, 0, target);
  2378   2383         break;
  2379   2384       }
  2380   2385   #ifndef SQLITE_OMIT_FLOATING_POINT
................................................................................
  3789   3794   
  3790   3795   /*
  3791   3796   ** Do a deep comparison of two expression trees.  Return 0 if the two
  3792   3797   ** expressions are completely identical.  Return 1 if they differ only
  3793   3798   ** by a COLLATE operator at the top level.  Return 2 if there are differences
  3794   3799   ** other than the top-level COLLATE operator.
  3795   3800   **
         3801  +** If any subelement of pB has Expr.iTable==(-1) then it is allowed
         3802  +** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
         3803  +**
         3804  +** The pA side might be using TK_REGISTER.  If that is the case and pB is
         3805  +** not using TK_REGISTER but is otherwise equivalent, then still return 0.
         3806  +**
  3796   3807   ** Sometimes this routine will return 2 even if the two expressions
  3797   3808   ** really are equivalent.  If we cannot prove that the expressions are
  3798   3809   ** identical, we return 2 just to be safe.  So if this routine
  3799   3810   ** returns 2, then you do not really know for certain if the two
  3800   3811   ** expressions are the same.  But if you get a 0 or 1 return, then you
  3801   3812   ** can be sure the expressions are the same.  In the places where
  3802   3813   ** this routine is used, it does not hurt to get an extra 2 - that
  3803   3814   ** just might result in some slightly slower code.  But returning
  3804   3815   ** an incorrect 0 or 1 could lead to a malfunction.
  3805   3816   */
  3806         -int sqlite3ExprCompare(Expr *pA, Expr *pB){
         3817  +int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
  3807   3818     if( pA==0||pB==0 ){
  3808   3819       return pB==pA ? 0 : 2;
  3809   3820     }
  3810   3821     assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) );
  3811   3822     assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) );
  3812   3823     if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){
  3813   3824       return 2;
  3814   3825     }
  3815   3826     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
  3816         -  if( pA->op!=pB->op ){
  3817         -    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB)<2 ){
         3827  +  if( pA->op!=pB->op && (pA->op!=TK_REGISTER || pA->op2!=pB->op) ){
         3828  +    if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
  3818   3829         return 1;
  3819   3830       }
  3820         -    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft)<2 ){
         3831  +    if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
  3821   3832         return 1;
  3822   3833       }
  3823   3834       return 2;
  3824   3835     }
  3825         -  if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2;
  3826         -  if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2;
  3827         -  if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2;
  3828         -  if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2;
         3836  +  if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
         3837  +  if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
         3838  +  if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
         3839  +  if( pA->iColumn!=pB->iColumn ) return 2;
         3840  +  if( pA->iTable!=pB->iTable 
         3841  +   && pA->op!=TK_REGISTER
         3842  +   && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
  3829   3843     if( ExprHasProperty(pA, EP_IntValue) ){
  3830   3844       if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
  3831   3845         return 2;
  3832   3846       }
  3833   3847     }else if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken){
  3834   3848       if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
  3835   3849       if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
................................................................................
  3839   3853     return 0;
  3840   3854   }
  3841   3855   
  3842   3856   /*
  3843   3857   ** Compare two ExprList objects.  Return 0 if they are identical and 
  3844   3858   ** non-zero if they differ in any way.
  3845   3859   **
         3860  +** If any subelement of pB has Expr.iTable==(-1) then it is allowed
         3861  +** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
         3862  +**
  3846   3863   ** This routine might return non-zero for equivalent ExprLists.  The
  3847   3864   ** only consequence will be disabled optimizations.  But this routine
  3848   3865   ** must never return 0 if the two ExprList objects are different, or
  3849   3866   ** a malfunction will result.
  3850   3867   **
  3851   3868   ** Two NULL pointers are considered to be the same.  But a NULL pointer
  3852   3869   ** always differs from a non-NULL pointer.
  3853   3870   */
  3854         -int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){
         3871  +int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
  3855   3872     int i;
  3856   3873     if( pA==0 && pB==0 ) return 0;
  3857   3874     if( pA==0 || pB==0 ) return 1;
  3858   3875     if( pA->nExpr!=pB->nExpr ) return 1;
  3859   3876     for(i=0; i<pA->nExpr; i++){
  3860   3877       Expr *pExprA = pA->a[i].pExpr;
  3861   3878       Expr *pExprB = pB->a[i].pExpr;
  3862   3879       if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
  3863         -    if( sqlite3ExprCompare(pExprA, pExprB) ) return 1;
         3880  +    if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
         3881  +  }
         3882  +  return 0;
         3883  +}
         3884  +
         3885  +/*
         3886  +** Return true if we can prove the pE2 will always be true if pE1 is
         3887  +** true.  Return false if we cannot complete the proof or if pE2 might
         3888  +** be false.  Examples:
         3889  +**
         3890  +**     pE1: x==5       pE2: x==5             Result: true
         3891  +**     pE1: x>0        pE2: x==5             Result: false
         3892  +**     pE1: x=21       pE2: x=21 OR y=43     Result: true
         3893  +**     pE1: x!=123     pE2: x IS NOT NULL    Result: true
         3894  +**     pE1: x!=?1      pE2: x IS NOT NULL    Result: true
         3895  +**     pE1: x IS NULL  pE2: x IS NOT NULL    Result: false
         3896  +**     pE1: x IS ?2    pE2: x IS NOT NULL    Reuslt: false
         3897  +**
         3898  +** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
         3899  +** Expr.iTable<0 then assume a table number given by iTab.
         3900  +**
         3901  +** When in doubt, return false.  Returning true might give a performance
         3902  +** improvement.  Returning false might cause a performance reduction, but
         3903  +** it will always give the correct answer and is hence always safe.
         3904  +*/
         3905  +int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
         3906  +  if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
         3907  +    return 1;
         3908  +  }
         3909  +  if( pE2->op==TK_OR
         3910  +   && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
         3911  +             || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
         3912  +  ){
         3913  +    return 1;
         3914  +  }
         3915  +  if( pE2->op==TK_NOTNULL
         3916  +   && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
         3917  +   && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
         3918  +  ){
         3919  +    return 1;
  3864   3920     }
  3865   3921     return 0;
  3866   3922   }
  3867   3923   
  3868   3924   /*
  3869   3925   ** An instance of the following structure is used by the tree walker
  3870   3926   ** to count references to table columns in the arguments of an 
................................................................................
  4041   4097          && pWalker->walkerDepth==pExpr->op2
  4042   4098         ){
  4043   4099           /* Check to see if pExpr is a duplicate of another aggregate 
  4044   4100           ** function that is already in the pAggInfo structure
  4045   4101           */
  4046   4102           struct AggInfo_func *pItem = pAggInfo->aFunc;
  4047   4103           for(i=0; i<pAggInfo->nFunc; i++, pItem++){
  4048         -          if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){
         4104  +          if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
  4049   4105               break;
  4050   4106             }
  4051   4107           }
  4052   4108           if( i>=pAggInfo->nFunc ){
  4053   4109             /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
  4054   4110             */
  4055   4111             u8 enc = ENC(pParse->db);

Changes to src/insert.c.

  1385   1385     /* Test all UNIQUE constraints by creating entries for each UNIQUE
  1386   1386     ** index and making sure that duplicate entries do not already exist.
  1387   1387     ** Add the new records to the indices as we go.
  1388   1388     */
  1389   1389     for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){
  1390   1390       int regIdx;
  1391   1391       int regR;
         1392  +    int addrSkipRow = 0;
  1392   1393   
  1393   1394       if( aRegIdx[iCur]==0 ) continue;  /* Skip unused indices */
         1395  +
         1396  +    if( pIdx->pPartIdxWhere ){
         1397  +      sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[iCur]);
         1398  +      addrSkipRow = sqlite3VdbeMakeLabel(v);
         1399  +      pParse->ckBase = regData;
         1400  +      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrSkipRow,
         1401  +                         SQLITE_JUMPIFNULL);
         1402  +      pParse->ckBase = 0;
         1403  +    }
  1394   1404   
  1395   1405       /* Create a key for accessing the index entry */
  1396   1406       regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1);
  1397   1407       for(i=0; i<pIdx->nColumn; i++){
  1398   1408         int idx = pIdx->aiColumn[i];
  1399   1409         if( idx==pTab->iPKey ){
  1400   1410           sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i);
................................................................................
  1407   1417       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
  1408   1418       sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1);
  1409   1419   
  1410   1420       /* Find out what action to take in case there is an indexing conflict */
  1411   1421       onError = pIdx->onError;
  1412   1422       if( onError==OE_None ){ 
  1413   1423         sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1);
         1424  +      sqlite3VdbeResolveLabel(v, addrSkipRow);
  1414   1425         continue;  /* pIdx is not a UNIQUE index */
  1415   1426       }
  1416   1427       if( overrideError!=OE_Default ){
  1417   1428         onError = overrideError;
  1418   1429       }else if( onError==OE_Default ){
  1419   1430         onError = OE_Abort;
  1420   1431       }
................................................................................
  1476   1487               pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace
  1477   1488           );
  1478   1489           seenReplace = 1;
  1479   1490           break;
  1480   1491         }
  1481   1492       }
  1482   1493       sqlite3VdbeJumpHere(v, j3);
         1494  +    sqlite3VdbeResolveLabel(v, addrSkipRow);
  1483   1495       sqlite3ReleaseTempReg(pParse, regR);
  1484   1496     }
  1485   1497     
  1486   1498     if( pbMayReplace ){
  1487   1499       *pbMayReplace = seenReplace;
  1488   1500     }
  1489   1501   }
................................................................................
  1505   1517     int *aRegIdx,       /* Register used by each index.  0 for unused indices */
  1506   1518     int isUpdate,       /* True for UPDATE, False for INSERT */
  1507   1519     int appendBias,     /* True if this is likely to be an append */
  1508   1520     int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
  1509   1521   ){
  1510   1522     int i;
  1511   1523     Vdbe *v;
  1512         -  int nIdx;
  1513   1524     Index *pIdx;
  1514   1525     u8 pik_flags;
  1515   1526     int regData;
  1516   1527     int regRec;
  1517   1528   
  1518   1529     v = sqlite3GetVdbe(pParse);
  1519   1530     assert( v!=0 );
  1520   1531     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1521         -  for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
  1522         -  for(i=nIdx-1; i>=0; i--){
         1532  +  for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1523   1533       if( aRegIdx[i]==0 ) continue;
         1534  +    if( pIdx->pPartIdxWhere ){
         1535  +      sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
         1536  +    }
  1524   1537       sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]);
  1525   1538       if( useSeekResult ){
  1526   1539         sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  1527   1540       }
  1528   1541     }
  1529   1542     regData = regRowid + 1;
  1530   1543     regRec = sqlite3GetTempReg(pParse);
................................................................................
  1618   1631   ** for index pDest in an insert transfer optimization.  The rules
  1619   1632   ** for a compatible index:
  1620   1633   **
  1621   1634   **    *   The index is over the same set of columns
  1622   1635   **    *   The same DESC and ASC markings occurs on all columns
  1623   1636   **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
  1624   1637   **    *   The same collating sequence on each column
         1638  +**    *   The index has the exact same WHERE clause
  1625   1639   */
  1626   1640   static int xferCompatibleIndex(Index *pDest, Index *pSrc){
  1627   1641     int i;
  1628   1642     assert( pDest && pSrc );
  1629   1643     assert( pDest->pTable!=pSrc->pTable );
  1630   1644     if( pDest->nColumn!=pSrc->nColumn ){
  1631   1645       return 0;   /* Different number of columns */
................................................................................
  1639   1653       }
  1640   1654       if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
  1641   1655         return 0;   /* Different sort orders */
  1642   1656       }
  1643   1657       if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
  1644   1658         return 0;   /* Different collating sequences */
  1645   1659       }
         1660  +  }
         1661  +  if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
         1662  +    return 0;     /* Different WHERE clauses */
  1646   1663     }
  1647   1664   
  1648   1665     /* If no test above fails then the indices must be compatible */
  1649   1666     return 1;
  1650   1667   }
  1651   1668   
  1652   1669   /*
................................................................................
  1795   1812         if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
  1796   1813       }
  1797   1814       if( pSrcIdx==0 ){
  1798   1815         return 0;    /* pDestIdx has no corresponding index in pSrc */
  1799   1816       }
  1800   1817     }
  1801   1818   #ifndef SQLITE_OMIT_CHECK
  1802         -  if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck, pDest->pCheck) ){
         1819  +  if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
  1803   1820       return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
  1804   1821     }
  1805   1822   #endif
  1806   1823   #ifndef SQLITE_OMIT_FOREIGN_KEY
  1807   1824     /* Disallow the transfer optimization if the destination table constains
  1808   1825     ** any foreign key constraints.  This is more restrictive than necessary.
  1809   1826     ** But the main beneficiary of the transfer optimization is the VACUUM 

Changes to src/loadext.c.

   663    663         }
   664    664       }
   665    665       sqlite3_mutex_leave(mutex);
   666    666       assert( (rc&0xff)==rc );
   667    667       return rc;
   668    668     }
   669    669   }
          670  +
          671  +/*
          672  +** Cancel a prior call to sqlite3_auto_extension.  Remove xInit from the
          673  +** set of routines that is invoked for each new database connection, if it
          674  +** is currently on the list.  If xInit is not on the list, then this
          675  +** routine is a no-op.
          676  +**
          677  +** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
          678  +** was not on the list.
          679  +*/
          680  +int sqlite3_cancel_auto_extension(void (*xInit)(void)){
          681  +#if SQLITE_THREADSAFE
          682  +  sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
          683  +#endif
          684  +  int i;
          685  +  int n = 0;
          686  +  wsdAutoextInit;
          687  +  sqlite3_mutex_enter(mutex);
          688  +  for(i=wsdAutoext.nExt-1; i>=0; i--){
          689  +    if( wsdAutoext.aExt[i]==xInit ){
          690  +      wsdAutoext.nExt--;
          691  +      wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
          692  +      n++;
          693  +      break;
          694  +    }
          695  +  }
          696  +  sqlite3_mutex_leave(mutex);
          697  +  return n;
          698  +}
   670    699   
   671    700   /*
   672    701   ** Reset the automatic extension loading mechanism.
   673    702   */
   674    703   void sqlite3_reset_auto_extension(void){
   675    704   #ifndef SQLITE_OMIT_AUTOINIT
   676    705     if( sqlite3_initialize()==SQLITE_OK )

Changes to src/main.c.

  1096   1096         case SQLITE_IOERR_SHMOPEN:      zName = "SQLITE_IOERR_SHMOPEN";     break;
  1097   1097         case SQLITE_IOERR_SHMSIZE:      zName = "SQLITE_IOERR_SHMSIZE";     break;
  1098   1098         case SQLITE_IOERR_SHMLOCK:      zName = "SQLITE_IOERR_SHMLOCK";     break;
  1099   1099         case SQLITE_IOERR_SHMMAP:       zName = "SQLITE_IOERR_SHMMAP";      break;
  1100   1100         case SQLITE_IOERR_SEEK:         zName = "SQLITE_IOERR_SEEK";        break;
  1101   1101         case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
  1102   1102         case SQLITE_IOERR_MMAP:         zName = "SQLITE_IOERR_MMAP";        break;
         1103  +      case SQLITE_IOERR_GETTEMPPATH:  zName = "SQLITE_IOERR_GETTEMPPATH"; break;
  1103   1104         case SQLITE_CORRUPT:            zName = "SQLITE_CORRUPT";           break;
  1104   1105         case SQLITE_CORRUPT_VTAB:       zName = "SQLITE_CORRUPT_VTAB";      break;
  1105   1106         case SQLITE_NOTFOUND:           zName = "SQLITE_NOTFOUND";          break;
  1106   1107         case SQLITE_FULL:               zName = "SQLITE_FULL";              break;
  1107   1108         case SQLITE_CANTOPEN:           zName = "SQLITE_CANTOPEN";          break;
  1108   1109         case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
  1109   1110         case SQLITE_CANTOPEN_ISDIR:     zName = "SQLITE_CANTOPEN_ISDIR";    break;
................................................................................
  2472   2473     assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
  2473   2474     memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
  2474   2475     db->autoCommit = 1;
  2475   2476     db->nextAutovac = -1;
  2476   2477     db->szMmap = sqlite3GlobalConfig.szMmap;
  2477   2478     db->nextPagesize = 0;
  2478   2479     db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger
  2479         -#if !defined(SQLITE_DEAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
         2480  +#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
  2480   2481                    | SQLITE_AutoIndex
  2481   2482   #endif
  2482   2483   #if SQLITE_DEFAULT_FILE_FORMAT<4
  2483   2484                    | SQLITE_LegacyFileFmt
  2484   2485   #endif
  2485   2486   #ifdef SQLITE_ENABLE_LOAD_EXTENSION
  2486   2487                    | SQLITE_LoadExtension

Changes to src/os_win.c.

    13     13   ** This file contains code that is specific to Windows.
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   #if SQLITE_OS_WIN               /* This file is used for Windows only */
    17     17   
    18     18   #ifdef __CYGWIN__
    19     19   # include <sys/cygwin.h>
           20  +# include <errno.h>
    20     21   #endif
    21     22   
    22     23   /*
    23     24   ** Include code that is common to all os_*.c files
    24     25   */
    25     26   #include "os_common.h"
    26     27   
................................................................................
   225    226   /*
   226    227    * The extra flags to use in calls to the Win32 heap APIs.  This value may be
   227    228    * zero for the default behavior.
   228    229    */
   229    230   #ifndef SQLITE_WIN32_HEAP_FLAGS
   230    231   #  define SQLITE_WIN32_HEAP_FLAGS     (0)
   231    232   #endif
          233  +
   232    234   
   233    235   /*
   234    236   ** The winMemData structure stores information required by the Win32-specific
   235    237   ** sqlite3_mem_methods implementation.
   236    238   */
   237    239   typedef struct winMemData winMemData;
   238    240   struct winMemData {
................................................................................
  3690   3692                          "winMapfile", pFd->zPath);
  3691   3693         /* Log the error, but continue normal operation using xRead/xWrite */
  3692   3694         OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=SQLITE_IOERR_MMAP\n",
  3693   3695                  osGetCurrentProcessId(), pFd));
  3694   3696         return SQLITE_OK;
  3695   3697       }
  3696   3698       assert( (nMap % winSysInfo.dwPageSize)==0 );
         3699  +    assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
  3697   3700   #if SQLITE_OS_WINRT
  3698         -    pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, nMap);
         3701  +    pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
  3699   3702   #else
  3700         -    assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
  3701   3703       pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
  3702   3704   #endif
  3703   3705       if( pNew==NULL ){
  3704   3706         osCloseHandle(pFd->hMap);
  3705   3707         pFd->hMap = NULL;
  3706   3708         pFd->lastErrno = osGetLastError();
  3707   3709         winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
................................................................................
  3862   3864       zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
  3863   3865     }
  3864   3866   #endif
  3865   3867     /* caller will handle out of memory */
  3866   3868     return zConverted;
  3867   3869   }
  3868   3870   
         3871  +/*
         3872  +** Maximum pathname length (in bytes) for windows.  The MAX_PATH macro is
         3873  +** in characters, so we allocate 3 bytes per character assuming worst-case
         3874  +** 3-bytes-per-character UTF8.
         3875  +*/
         3876  +#ifndef SQLITE_WIN32_MAX_PATH
         3877  +#  define SQLITE_WIN32_MAX_PATH   (MAX_PATH*3)
         3878  +#endif
         3879  +
  3869   3880   /*
  3870   3881   ** Create a temporary file name in zBuf.  zBuf must be big enough to
  3871   3882   ** hold at pVfs->mxPathname characters.
  3872   3883   */
  3873   3884   static int getTempname(int nBuf, char *zBuf){
  3874   3885     static char zChars[] =
  3875   3886       "abcdefghijklmnopqrstuvwxyz"
  3876   3887       "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  3877   3888       "0123456789";
  3878   3889     size_t i, j;
  3879   3890     int nTempPath;
  3880         -  char zTempPath[MAX_PATH+2];
         3891  +  char zTempPath[SQLITE_WIN32_MAX_PATH+2];
  3881   3892   
  3882   3893     /* It's odd to simulate an io-error here, but really this is just
  3883   3894     ** using the io-error infrastructure to test that SQLite handles this
  3884   3895     ** function failing. 
  3885   3896     */
  3886   3897     SimulateIOError( return SQLITE_IOERR );
  3887   3898   
  3888         -  memset(zTempPath, 0, MAX_PATH+2);
  3889         -
  3890   3899     if( sqlite3_temp_directory ){
  3891         -    sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
         3900  +    sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s",
         3901  +                     sqlite3_temp_directory);
  3892   3902     }
  3893   3903   #if !SQLITE_OS_WINRT
  3894   3904     else if( isNT() ){
  3895   3905       char *zMulti;
  3896   3906       WCHAR zWidePath[MAX_PATH];
  3897         -    osGetTempPathW(MAX_PATH-30, zWidePath);
         3907  +    if( osGetTempPathW(MAX_PATH-30, zWidePath)==0 ){
         3908  +      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
         3909  +      return SQLITE_IOERR_GETTEMPPATH;
         3910  +    }
  3898   3911       zMulti = unicodeToUtf8(zWidePath);
  3899   3912       if( zMulti ){
  3900         -      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
         3913  +      sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zMulti);
  3901   3914         sqlite3_free(zMulti);
  3902   3915       }else{
  3903   3916         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  3904   3917         return SQLITE_IOERR_NOMEM;
  3905   3918       }
  3906   3919     }
  3907   3920   #ifdef SQLITE_WIN32_HAS_ANSI
  3908   3921     else{
  3909   3922       char *zUtf8;
  3910         -    char zMbcsPath[MAX_PATH];
  3911         -    osGetTempPathA(MAX_PATH-30, zMbcsPath);
         3923  +    char zMbcsPath[SQLITE_WIN32_MAX_PATH];
         3924  +    if( osGetTempPathA(SQLITE_WIN32_MAX_PATH-30, zMbcsPath)==0 ){
         3925  +      OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
         3926  +      return SQLITE_IOERR_GETTEMPPATH;
         3927  +    }
  3912   3928       zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
  3913   3929       if( zUtf8 ){
  3914         -      sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
         3930  +      sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zUtf8);
  3915   3931         sqlite3_free(zUtf8);
  3916   3932       }else{
  3917   3933         OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
  3918   3934         return SQLITE_IOERR_NOMEM;
  3919   3935       }
  3920   3936     }
  3921         -#endif
  3922         -#endif
         3937  +#else
         3938  +  else{
         3939  +    /*
         3940  +    ** Compiled without ANSI support and the current operating system
         3941  +    ** is not Windows NT; therefore, just zero the temporary buffer.
         3942  +    */
         3943  +    memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
         3944  +  }
         3945  +#endif /* SQLITE_WIN32_HAS_ANSI */
         3946  +#else
         3947  +  else{
         3948  +    /*
         3949  +    ** Compiled for WinRT and the sqlite3_temp_directory is not set;
         3950  +    ** therefore, just zero the temporary buffer.
         3951  +    */
         3952  +    memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
         3953  +  }
         3954  +#endif /* !SQLITE_OS_WINRT */
  3923   3955   
  3924   3956     /* Check that the output buffer is large enough for the temporary file 
  3925   3957     ** name. If it is not, return SQLITE_ERROR.
  3926   3958     */
  3927   3959     nTempPath = sqlite3Strlen30(zTempPath);
  3928   3960   
  3929   3961     if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
................................................................................
  4001   4033     void *zConverted;              /* Filename in OS encoding */
  4002   4034     const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
  4003   4035     int cnt = 0;
  4004   4036   
  4005   4037     /* If argument zPath is a NULL pointer, this function is required to open
  4006   4038     ** a temporary file. Use this buffer to store the file name in.
  4007   4039     */
  4008         -  char zTmpname[MAX_PATH+2];     /* Buffer used to create temp filename */
         4040  +  char zTmpname[SQLITE_WIN32_MAX_PATH+2];     /* Buffer used to create temp filename */
  4009   4041   
  4010   4042     int rc = SQLITE_OK;            /* Function Return Code */
  4011   4043   #if !defined(NDEBUG) || SQLITE_OS_WINCE
  4012   4044     int eType = flags&0xFFFFFF00;  /* Type of file to open */
  4013   4045   #endif
  4014   4046   
  4015   4047     int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
................................................................................
  4067   4099   #endif
  4068   4100   
  4069   4101     /* If the second argument to this function is NULL, generate a 
  4070   4102     ** temporary file name to use 
  4071   4103     */
  4072   4104     if( !zUtf8Name ){
  4073   4105       assert(isDelete && !isOpenJournal);
  4074         -    memset(zTmpname, 0, MAX_PATH+2);
  4075         -    rc = getTempname(MAX_PATH+2, zTmpname);
         4106  +    rc = getTempname(SQLITE_WIN32_MAX_PATH+2, zTmpname);
  4076   4107       if( rc!=SQLITE_OK ){
  4077   4108         OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
  4078   4109         return rc;
  4079   4110       }
  4080   4111       zUtf8Name = zTmpname;
  4081   4112     }
  4082   4113   
................................................................................
  4499   4530     int nFull,                    /* Size of output buffer in bytes */
  4500   4531     char *zFull                   /* Output buffer */
  4501   4532   ){
  4502   4533     
  4503   4534   #if defined(__CYGWIN__)
  4504   4535     SimulateIOError( return SQLITE_ERROR );
  4505   4536     UNUSED_PARAMETER(nFull);
  4506         -  assert( pVfs->mxPathname>=MAX_PATH );
         4537  +  assert( pVfs->mxPathname>=SQLITE_WIN32_MAX_PATH );
  4507   4538     assert( nFull>=pVfs->mxPathname );
  4508   4539     if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
  4509   4540       /*
  4510   4541       ** NOTE: We are dealing with a relative path name and the data
  4511   4542       **       directory has been set.  Therefore, use it as the basis
  4512   4543       **       for converting the relative path name to an absolute
  4513   4544       **       one by prepending the data directory and a slash.
  4514   4545       */
  4515         -    char zOut[MAX_PATH+1];
  4516         -    memset(zOut, 0, MAX_PATH+1);
  4517         -    cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
  4518         -                     MAX_PATH+1);
         4546  +    char zOut[SQLITE_WIN32_MAX_PATH+1];
         4547  +    if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
         4548  +                         SQLITE_WIN32_MAX_PATH+1)<0 ){
         4549  +      winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
         4550  +                  zRelative);
         4551  +      return SQLITE_CANTOPEN_FULLPATH;
         4552  +    }
  4519   4553       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
  4520   4554                        sqlite3_data_directory, zOut);
  4521   4555     }else{
  4522         -    cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull);
         4556  +    if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
         4557  +      winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
         4558  +                  zRelative);
         4559  +      return SQLITE_CANTOPEN_FULLPATH;
         4560  +    }
  4523   4561     }
  4524   4562     return SQLITE_OK;
  4525   4563   #endif
  4526   4564   
  4527   4565   #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
  4528   4566     SimulateIOError( return SQLITE_ERROR );
  4529   4567     /* WinCE has no concept of a relative pathname, or so I am told. */
................................................................................
  4857   4895   /*
  4858   4896   ** Initialize and deinitialize the operating system interface.
  4859   4897   */
  4860   4898   int sqlite3_os_init(void){
  4861   4899     static sqlite3_vfs winVfs = {
  4862   4900       3,                   /* iVersion */
  4863   4901       sizeof(winFile),     /* szOsFile */
  4864         -    MAX_PATH,            /* mxPathname */
         4902  +    SQLITE_WIN32_MAX_PATH, /* mxPathname */
  4865   4903       0,                   /* pNext */
  4866   4904       "win32",             /* zName */
  4867   4905       0,                   /* pAppData */
  4868   4906       winOpen,             /* xOpen */
  4869   4907       winDelete,           /* xDelete */
  4870   4908       winAccess,           /* xAccess */
  4871   4909       winFullPathname,     /* xFullPathname */

Changes to src/parse.y.

  1121   1121   nexprlist(A) ::= expr(Y).
  1122   1122       {A = sqlite3ExprListAppend(pParse,0,Y.pExpr);}
  1123   1123   
  1124   1124   
  1125   1125   ///////////////////////////// The CREATE INDEX command ///////////////////////
  1126   1126   //
  1127   1127   cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
  1128         -        ON nm(Y) LP idxlist(Z) RP(E). {
         1128  +        ON nm(Y) LP idxlist(Z) RP where_opt(W). {
  1129   1129     sqlite3CreateIndex(pParse, &X, &D, 
  1130   1130                        sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
  1131         -                      &S, &E, SQLITE_SO_ASC, NE);
         1131  +                      &S, W, SQLITE_SO_ASC, NE);
  1132   1132   }
  1133   1133   
  1134   1134   %type uniqueflag {int}
  1135   1135   uniqueflag(A) ::= UNIQUE.  {A = OE_Abort;}
  1136   1136   uniqueflag(A) ::= .        {A = OE_None;}
  1137   1137   
  1138   1138   %type idxlist {ExprList*}

Changes to src/pragma.c.

  1396   1396           for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1397   1397             sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
  1398   1398             cnt++;
  1399   1399           }
  1400   1400         }
  1401   1401   
  1402   1402         /* Make sure sufficient number of registers have been allocated */
  1403         -      if( pParse->nMem < cnt+4 ){
  1404         -        pParse->nMem = cnt+4;
  1405         -      }
         1403  +      pParse->nMem = MAX( pParse->nMem, cnt+7 );
  1406   1404   
  1407   1405         /* Do the b-tree integrity checks */
  1408   1406         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
  1409   1407         sqlite3VdbeChangeP5(v, (u8)i);
  1410   1408         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
  1411   1409         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1412   1410            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
................................................................................
  1423   1421           Index *pIdx;
  1424   1422           int loopTop;
  1425   1423   
  1426   1424           if( pTab->pIndex==0 ) continue;
  1427   1425           addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
  1428   1426           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1429   1427           sqlite3VdbeJumpHere(v, addr);
         1428  +        sqlite3ExprCacheClear(pParse);
  1430   1429           sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
  1431         -        sqlite3VdbeAddOp2(v, OP_Integer, 0, 2);  /* reg(2) will count entries */
  1432         -        loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
  1433         -        sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1);   /* increment entry count */
         1430  +        for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
         1431  +          sqlite3VdbeAddOp2(v, OP_Integer, 0, 7+j); /* index entries counter */
         1432  +        }
         1433  +        pParse->nMem = MAX(pParse->nMem, 7+j);
         1434  +        loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0) + 1;
  1434   1435           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1435         -          int jmp2;
         1436  +          int jmp2, jmp3;
  1436   1437             int r1;
  1437   1438             static const VdbeOpList idxErr[] = {
  1438   1439               { OP_AddImm,      1, -1,  0},
  1439   1440               { OP_String8,     0,  3,  0},    /* 1 */
  1440   1441               { OP_Rowid,       1,  4,  0},
  1441   1442               { OP_String8,     0,  5,  0},    /* 3 */
  1442   1443               { OP_String8,     0,  6,  0},    /* 4 */
................................................................................
  1443   1444               { OP_Concat,      4,  3,  3},
  1444   1445               { OP_Concat,      5,  3,  3},
  1445   1446               { OP_Concat,      6,  3,  3},
  1446   1447               { OP_ResultRow,   3,  1,  0},
  1447   1448               { OP_IfPos,       1,  0,  0},    /* 9 */
  1448   1449               { OP_Halt,        0,  0,  0},
  1449   1450             };
  1450         -          r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0);
         1451  +          r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0, &jmp3);
         1452  +          sqlite3VdbeAddOp2(v, OP_AddImm, 7+j, 1);  /* increment entry count */
  1451   1453             jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1);
  1452   1454             addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr);
  1453   1455             sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC);
  1454   1456             sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC);
  1455   1457             sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_TRANSIENT);
  1456   1458             sqlite3VdbeJumpHere(v, addr+9);
  1457   1459             sqlite3VdbeJumpHere(v, jmp2);
         1460  +          sqlite3VdbeResolveLabel(v, jmp3);
  1458   1461           }
  1459         -        sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1);
  1460         -        sqlite3VdbeJumpHere(v, loopTop);
         1462  +        sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop);
         1463  +        sqlite3VdbeJumpHere(v, loopTop-1);
         1464  +#ifndef SQLITE_OMIT_BTREECOUNT
         1465  +        sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, 
         1466  +                     "wrong # of entries in index ", P4_STATIC);
  1461   1467           for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
  1462         -          static const VdbeOpList cntIdx[] = {
  1463         -             { OP_Integer,      0,  3,  0},
  1464         -             { OP_Rewind,       0,  0,  0},  /* 1 */
  1465         -             { OP_AddImm,       3,  1,  0},
  1466         -             { OP_Next,         0,  0,  0},  /* 3 */
  1467         -             { OP_Eq,           2,  0,  3},  /* 4 */
  1468         -             { OP_AddImm,       1, -1,  0},
  1469         -             { OP_String8,      0,  2,  0},  /* 6 */
  1470         -             { OP_String8,      0,  3,  0},  /* 7 */
  1471         -             { OP_Concat,       3,  2,  2},
  1472         -             { OP_ResultRow,    2,  1,  0},
  1473         -          };
  1474         -          addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
         1468  +          addr = sqlite3VdbeCurrentAddr(v);
         1469  +          sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
  1475   1470             sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
  1476         -          sqlite3VdbeJumpHere(v, addr);
  1477         -          addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
  1478         -          sqlite3VdbeChangeP1(v, addr+1, j+2);
  1479         -          sqlite3VdbeChangeP2(v, addr+1, addr+4);
  1480         -          sqlite3VdbeChangeP1(v, addr+3, j+2);
  1481         -          sqlite3VdbeChangeP2(v, addr+3, addr+2);
  1482         -          sqlite3VdbeJumpHere(v, addr+4);
  1483         -          sqlite3VdbeChangeP4(v, addr+6, 
  1484         -                     "wrong # of entries in index ", P4_STATIC);
  1485         -          sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_TRANSIENT);
         1471  +          sqlite3VdbeAddOp2(v, OP_Count, j+2, 3);
         1472  +          sqlite3VdbeAddOp3(v, OP_Eq, 7+j, addr+8, 3);
         1473  +          sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
         1474  +          sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
         1475  +          sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
         1476  +          sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
  1486   1477           }
         1478  +#endif /* SQLITE_OMIT_BTREECOUNT */
  1487   1479         } 
  1488   1480       }
  1489   1481       addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
  1490   1482       sqlite3VdbeChangeP2(v, addr, -mxErr);
  1491   1483       sqlite3VdbeJumpHere(v, addr+1);
  1492   1484       sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
  1493   1485     }else

Changes to src/prepare.c.

   805    805     const char *zTail8 = 0;
   806    806     int rc = SQLITE_OK;
   807    807   
   808    808     assert( ppStmt );
   809    809     *ppStmt = 0;
   810    810     if( !sqlite3SafetyCheckOk(db) ){
   811    811       return SQLITE_MISUSE_BKPT;
          812  +  }
          813  +  if( nBytes>=0 ){
          814  +    int sz;
          815  +    const char *z = (const char*)zSql;
          816  +    for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
          817  +    nBytes = sz;
   812    818     }
   813    819     sqlite3_mutex_enter(db->mutex);
   814    820     zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
   815    821     if( zSql8 ){
   816    822       rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
   817    823     }
   818    824   

Changes to src/resolve.c.

   236    236     ExprSetIrreducible(pExpr);
   237    237   
   238    238     /* Translate the schema name in zDb into a pointer to the corresponding
   239    239     ** schema.  If not found, pSchema will remain NULL and nothing will match
   240    240     ** resulting in an appropriate error message toward the end of this routine
   241    241     */
   242    242     if( zDb ){
   243         -    for(i=0; i<db->nDb; i++){
   244         -      assert( db->aDb[i].zName );
   245         -      if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
   246         -        pSchema = db->aDb[i].pSchema;
   247         -        break;
          243  +    testcase( pNC->ncFlags & NC_PartIdx );
          244  +    testcase( pNC->ncFlags & NC_IsCheck );
          245  +    if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
          246  +      /* Silently ignore database qualifiers inside CHECK constraints and partial
          247  +      ** indices.  Do not raise errors because that might break legacy and
          248  +      ** because it does not hurt anything to just ignore the database name. */
          249  +      zDb = 0;
          250  +    }else{
          251  +      for(i=0; i<db->nDb; i++){
          252  +        assert( db->aDb[i].zName );
          253  +        if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
          254  +          pSchema = db->aDb[i].pSchema;
          255  +          break;
          256  +        }
   248    257         }
   249    258       }
   250    259     }
   251    260   
   252    261     /* Start at the inner-most context and move outward until a match is found */
   253    262     while( pNC && cnt==0 ){
   254    263       ExprList *pEList;
................................................................................
   517    526         testcase( iCol==BMS-1 );
   518    527         pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
   519    528       }
   520    529       ExprSetProperty(p, EP_Resolved);
   521    530     }
   522    531     return p;
   523    532   }
          533  +
          534  +/*
          535  +** Report an error that an expression is not valid for a partial index WHERE
          536  +** clause.
          537  +*/
          538  +static void notValidPartIdxWhere(
          539  +  Parse *pParse,       /* Leave error message here */
          540  +  NameContext *pNC,    /* The name context */
          541  +  const char *zMsg     /* Type of error */
          542  +){
          543  +  if( (pNC->ncFlags & NC_PartIdx)!=0 ){
          544  +    sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
          545  +                    zMsg);
          546  +  }
          547  +}
          548  +
          549  +#ifndef SQLITE_OMIT_CHECK
          550  +/*
          551  +** Report an error that an expression is not valid for a CHECK constraint.
          552  +*/
          553  +static void notValidCheckConstraint(
          554  +  Parse *pParse,       /* Leave error message here */
          555  +  NameContext *pNC,    /* The name context */
          556  +  const char *zMsg     /* Type of error */
          557  +){
          558  +  if( (pNC->ncFlags & NC_IsCheck)!=0 ){
          559  +    sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
          560  +  }
          561  +}
          562  +#else
          563  +# define notValidCheckConstraint(P,N,M)
          564  +#endif
          565  +
   524    566   
   525    567   /*
   526    568   ** This routine is callback for sqlite3WalkExpr().
   527    569   **
   528    570   ** Resolve symbolic names into TK_COLUMN operators for the current
   529    571   ** node in the expression tree.  Return 0 to continue the search down
   530    572   ** the tree or 2 to abort the tree walk.
................................................................................
   617    659         int nId;                    /* Number of characters in function name */
   618    660         const char *zId;            /* The function name. */
   619    661         FuncDef *pDef;              /* Information about the function */
   620    662         u8 enc = ENC(pParse->db);   /* The database encoding */
   621    663   
   622    664         testcase( pExpr->op==TK_CONST_FUNC );
   623    665         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
          666  +      notValidPartIdxWhere(pParse, pNC, "functions");
   624    667         zId = pExpr->u.zToken;
   625    668         nId = sqlite3Strlen30(zId);
   626    669         pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
   627    670         if( pDef==0 ){
   628    671           pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
   629    672           if( pDef==0 ){
   630    673             no_such_func = 1;
................................................................................
   682    725       case TK_SELECT:
   683    726       case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
   684    727   #endif
   685    728       case TK_IN: {
   686    729         testcase( pExpr->op==TK_IN );
   687    730         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   688    731           int nRef = pNC->nRef;
   689         -#ifndef SQLITE_OMIT_CHECK
   690         -        if( (pNC->ncFlags & NC_IsCheck)!=0 ){
   691         -          sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
   692         -        }
   693         -#endif
          732  +        notValidCheckConstraint(pParse, pNC, "subqueries");
          733  +        notValidPartIdxWhere(pParse, pNC, "subqueries");
   694    734           sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
   695    735           assert( pNC->nRef>=nRef );
   696    736           if( nRef!=pNC->nRef ){
   697    737             ExprSetProperty(pExpr, EP_VarSelect);
   698    738           }
   699    739         }
   700    740         break;
   701    741       }
   702         -#ifndef SQLITE_OMIT_CHECK
   703    742       case TK_VARIABLE: {
   704         -      if( (pNC->ncFlags & NC_IsCheck)!=0 ){
   705         -        sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
   706         -      }
          743  +      notValidCheckConstraint(pParse, pNC, "parameters");
          744  +      notValidPartIdxWhere(pParse, pNC, "parameters");
   707    745         break;
   708    746       }
   709         -#endif
   710    747     }
   711    748     return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
   712    749   }
   713    750   
   714    751   /*
   715    752   ** pEList is a list of expressions which are really the result set of the
   716    753   ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
................................................................................
   793    830     if( rc ) return 0;
   794    831   
   795    832     /* Try to match the ORDER BY expression against an expression
   796    833     ** in the result set.  Return an 1-based index of the matching
   797    834     ** result-set entry.
   798    835     */
   799    836     for(i=0; i<pEList->nExpr; i++){
   800         -    if( sqlite3ExprCompare(pEList->a[i].pExpr, pE)<2 ){
          837  +    if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
   801    838         return i+1;
   802    839       }
   803    840     }
   804    841   
   805    842     /* If no match, return 0. */
   806    843     return 0;
   807    844   }
................................................................................
  1021   1058   
  1022   1059       /* Otherwise, treat the ORDER BY term as an ordinary expression */
  1023   1060       pItem->iOrderByCol = 0;
  1024   1061       if( sqlite3ResolveExprNames(pNC, pE) ){
  1025   1062         return 1;
  1026   1063       }
  1027   1064       for(j=0; j<pSelect->pEList->nExpr; j++){
  1028         -      if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr)==0 ){
         1065  +      if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
  1029   1066           pItem->iOrderByCol = j+1;
  1030   1067         }
  1031   1068       }
  1032   1069     }
  1033   1070     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
  1034   1071   }
  1035   1072   
................................................................................
  1327   1364     memset(&w, 0, sizeof(w));
  1328   1365     w.xExprCallback = resolveExprStep;
  1329   1366     w.xSelectCallback = resolveSelectStep;
  1330   1367     w.pParse = pParse;
  1331   1368     w.u.pNC = pOuterNC;
  1332   1369     sqlite3WalkSelect(&w, p);
  1333   1370   }
         1371  +
         1372  +/*
         1373  +** Resolve names in expressions that can only reference a single table:
         1374  +**
         1375  +**    *   CHECK constraints
         1376  +**    *   WHERE clauses on partial indices
         1377  +**
         1378  +** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
         1379  +** is set to -1 and the Expr.iColumn value is set to the column number.
         1380  +**
         1381  +** Any errors cause an error message to be set in pParse.
         1382  +*/
         1383  +void sqlite3ResolveSelfReference(
         1384  +  Parse *pParse,      /* Parsing context */
         1385  +  Table *pTab,        /* The table being referenced */
         1386  +  int type,           /* NC_IsCheck or NC_PartIdx */
         1387  +  Expr *pExpr,        /* Expression to resolve.  May be NULL. */
         1388  +  ExprList *pList     /* Expression list to resolve.  May be NUL. */
         1389  +){
         1390  +  SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
         1391  +  NameContext sNC;                /* Name context for pParse->pNewTable */
         1392  +  int i;                          /* Loop counter */
         1393  +
         1394  +  assert( type==NC_IsCheck || type==NC_PartIdx );
         1395  +  memset(&sNC, 0, sizeof(sNC));
         1396  +  memset(&sSrc, 0, sizeof(sSrc));
         1397  +  sSrc.nSrc = 1;
         1398  +  sSrc.a[0].zName = pTab->zName;
         1399  +  sSrc.a[0].pTab = pTab;
         1400  +  sSrc.a[0].iCursor = -1;
         1401  +  sNC.pParse = pParse;
         1402  +  sNC.pSrcList = &sSrc;
         1403  +  sNC.ncFlags = type;
         1404  +  if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
         1405  +  if( pList ){
         1406  +    for(i=0; i<pList->nExpr; i++){
         1407  +      if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
         1408  +        return;
         1409  +      }
         1410  +    }
         1411  +  }
         1412  +}

Changes to src/select.c.

  4173   4173     /* If there is both a GROUP BY and an ORDER BY clause and they are
  4174   4174     ** identical, then disable the ORDER BY clause since the GROUP BY
  4175   4175     ** will cause elements to come out in the correct order.  This is
  4176   4176     ** an optimization - the correct answer should result regardless.
  4177   4177     ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
  4178   4178     ** to disable this optimization for testing purposes.
  4179   4179     */
  4180         -  if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
         4180  +  if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy, -1)==0
  4181   4181            && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
  4182   4182       pOrderBy = 0;
  4183   4183     }
  4184   4184   
  4185   4185     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
  4186   4186     ** if the select-list is the same as the ORDER BY list, then this query
  4187   4187     ** can be rewritten as a GROUP BY. In other words, this:
................................................................................
  4194   4194     **
  4195   4195     ** The second form is preferred as a single index (or temp-table) may be 
  4196   4196     ** used for both the ORDER BY and DISTINCT processing. As originally 
  4197   4197     ** written the query must use a temp-table for at least one of the ORDER 
  4198   4198     ** BY and DISTINCT, and an index or separate temp-table for the other.
  4199   4199     */
  4200   4200     if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
  4201         -   && sqlite3ExprListCompare(pOrderBy, p->pEList)==0
         4201  +   && sqlite3ExprListCompare(pOrderBy, p->pEList, -1)==0
  4202   4202     ){
  4203   4203       p->selFlags &= ~SF_Distinct;
  4204   4204       p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
  4205   4205       pGroupBy = p->pGroupBy;
  4206   4206       pOrderBy = 0;
  4207   4207       /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
  4208   4208       ** the sDistinct.isTnct is still set.  Hence, isTnct represents the

Changes to src/shell.c.

    61     61   #endif
    62     62   
    63     63   #if defined(_WIN32) || defined(WIN32)
    64     64   # include <io.h>
    65     65   #define isatty(h) _isatty(h)
    66     66   #define access(f,m) _access((f),(m))
    67     67   #undef popen
    68         -#define popen(a,b) _popen((a),(b))
           68  +#define popen _popen
    69     69   #undef pclose
    70     70   #define pclose _pclose
    71     71   #else
    72     72   /* Make sure isatty() has a prototype.
    73     73   */
    74     74   extern int isatty(int);
    75     75   #endif
           76  +
           77  +/* popen and pclose are not C89 functions and so are sometimes omitted from
           78  +** the <stdio.h> header */
           79  +FILE *popen(const char*,const char*);
           80  +int pclose(FILE*);
    76     81   
    77     82   #if defined(_WIN32_WCE)
    78     83   /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
    79     84    * thus we always assume that we have a console. That can be
    80     85    * overridden with the -batch command line option.
    81     86    */
    82     87   #define isatty(x) 1
................................................................................
  1717   1722         }
  1718   1723         if( (c==cSep && pc==cQuote)
  1719   1724          || (c=='\n' && pc==cQuote)
  1720   1725          || (c=='\n' && pc=='\r' && p->n>2 && p->z[p->n-2]==cQuote)
  1721   1726          || (c==EOF && pc==cQuote)
  1722   1727         ){
  1723   1728           do{ p->n--; }while( p->z[p->n]!=cQuote );
  1724         -        p->z[p->n] = 0;
  1725   1729           p->cTerm = c;
  1726   1730           break;
  1727   1731         }
  1728   1732         if( pc==cQuote && c!='\r' ){
  1729   1733           fprintf(stderr, "%s:%d: unescaped %c character\n",
  1730   1734                   p->zFile, p->nLine, cQuote);
  1731   1735         }
  1732   1736         if( c==EOF ){
  1733   1737           fprintf(stderr, "%s:%d: unterminated %c-quoted field\n",
  1734   1738                   p->zFile, startLine, cQuote);
  1735         -        p->z[p->n] = 0;
  1736   1739           p->cTerm = EOF;
  1737   1740           break;
  1738   1741         }
  1739   1742         csv_append_char(p, c);
  1740   1743         pc = c;
  1741   1744       }
  1742   1745     }else{
................................................................................
  1744   1747         csv_append_char(p, c);
  1745   1748         c = fgetc(p->in);
  1746   1749       }
  1747   1750       if( c=='\n' ){
  1748   1751         p->nLine++;
  1749   1752         if( p->n>1 && p->z[p->n-1]=='\r' ) p->n--;
  1750   1753       }
  1751         -    p->z[p->n] = 0;
  1752   1754       p->cTerm = c;
  1753   1755     }
         1756  +  if( p->z ) p->z[p->n] = 0;
  1754   1757     return p->z;
  1755   1758   }
  1756   1759   
  1757   1760   /*
  1758   1761   ** If an input line begins with "." then invoke this routine to
  1759   1762   ** process that line.
  1760   1763   **
................................................................................
  1795   1798     */
  1796   1799     if( nArg==0 ) return 0; /* no tokens, no error */
  1797   1800     n = strlen30(azArg[0]);
  1798   1801     c = azArg[0][0];
  1799   1802     if( c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0 ){
  1800   1803       const char *zDestFile = 0;
  1801   1804       const char *zDb = 0;
  1802         -    const char *zKey = 0;
  1803   1805       sqlite3 *pDest;
  1804   1806       sqlite3_backup *pBackup;
  1805   1807       int j;
  1806   1808       for(j=1; j<nArg; j++){
  1807   1809         const char *z = azArg[j];
  1808   1810         if( z[0]=='-' ){
  1809   1811           while( z[0]=='-' ) z++;
  1810         -        if( strcmp(z,"key")==0 && j<nArg-1 ){
  1811         -          zKey = azArg[++j];
  1812         -        }else
         1812  +        /* No options to process at this time */
  1813   1813           {
  1814   1814             fprintf(stderr, "unknown option: %s\n", azArg[j]);
  1815   1815             return 1;
  1816   1816           }
  1817   1817         }else if( zDestFile==0 ){
  1818   1818           zDestFile = azArg[j];
  1819   1819         }else if( zDb==0 ){
................................................................................
  1831   1831       if( zDb==0 ) zDb = "main";
  1832   1832       rc = sqlite3_open(zDestFile, &pDest);
  1833   1833       if( rc!=SQLITE_OK ){
  1834   1834         fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
  1835   1835         sqlite3_close(pDest);
  1836   1836         return 1;
  1837   1837       }
  1838         -#ifdef SQLITE_HAS_CODEC
  1839         -    sqlite3_key(pDest, zKey, (int)strlen(zKey));
  1840         -#else
  1841         -    (void)zKey;
  1842         -#endif
  1843   1838       open_db(p);
  1844   1839       pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
  1845   1840       if( pBackup==0 ){
  1846   1841         fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
  1847   1842         sqlite3_close(pDest);
  1848   1843         return 1;
  1849   1844       }
................................................................................
  2665   2660               fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
  2666   2661             }
  2667   2662             break;
  2668   2663   
  2669   2664           /* sqlite3_test_control(int, uint) */
  2670   2665           case SQLITE_TESTCTRL_PENDING_BYTE:        
  2671   2666             if( nArg==3 ){
  2672         -            unsigned int opt = (unsigned int)integerValue(azArg[2]);        
         2667  +            unsigned int opt = (unsigned int)integerValue(azArg[2]);
  2673   2668               rc = sqlite3_test_control(testctrl, opt);
  2674   2669               fprintf(p->out, "%d (0x%08x)\n", rc, rc);
  2675   2670             } else {
  2676   2671               fprintf(stderr,"Error: testctrl %s takes a single unsigned"
  2677   2672                              " int option\n", azArg[1]);
  2678   2673             }
  2679   2674             break;

Changes to src/sqlite.h.in.

   469    469   #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
   470    470   #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
   471    471   #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
   472    472   #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
   473    473   #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
   474    474   #define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
   475    475   #define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
          476  +#define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
   476    477   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
   477    478   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   478    479   #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
   479    480   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
   480    481   #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
   481    482   #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
   482    483   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
................................................................................
  2550   2551   ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
  2551   2552   ** function X to be invoked periodically during long running calls to
  2552   2553   ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
  2553   2554   ** database connection D.  An example use for this
  2554   2555   ** interface is to keep a GUI updated during a large query.
  2555   2556   **
  2556   2557   ** ^The parameter P is passed through as the only parameter to the 
  2557         -** callback function X.  ^The parameter N is the number of 
         2558  +** callback function X.  ^The parameter N is the approximate number of 
  2558   2559   ** [virtual machine instructions] that are evaluated between successive
  2559         -** invocations of the callback X.
         2560  +** invocations of the callback X.  ^If N is less than one then the progress
         2561  +** handler is disabled.
  2560   2562   **
  2561   2563   ** ^Only a single progress handler may be defined at one time per
  2562   2564   ** [database connection]; setting a new progress handler cancels the
  2563   2565   ** old one.  ^Setting parameter X to NULL disables the progress handler.
  2564   2566   ** ^The progress handler is also disabled by setting N to a value less
  2565   2567   ** than 1.
  2566   2568   **
................................................................................
  4172   4174   ** registered the application defined function.
  4173   4175   */
  4174   4176   sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
  4175   4177   
  4176   4178   /*
  4177   4179   ** CAPI3REF: Function Auxiliary Data
  4178   4180   **
  4179         -** The following two functions may be used by scalar SQL functions to
         4181  +** These functions may be used by (non-aggregate) SQL functions to
  4180   4182   ** associate metadata with argument values. If the same value is passed to
  4181   4183   ** multiple invocations of the same SQL function during query execution, under
  4182         -** some circumstances the associated metadata may be preserved. This may
  4183         -** be used, for example, to add a regular-expression matching scalar
  4184         -** function. The compiled version of the regular expression is stored as
  4185         -** metadata associated with the SQL value passed as the regular expression
  4186         -** pattern.  The compiled regular expression can be reused on multiple
  4187         -** invocations of the same function so that the original pattern string
  4188         -** does not need to be recompiled on each invocation.
         4184  +** some circumstances the associated metadata may be preserved.  An example
         4185  +** of where this might be useful is in a regular-expression matching
         4186  +** function. The compiled version of the regular expression can be stored as
         4187  +** metadata associated with the pattern string.  
         4188  +** Then as long as the pattern string remains the same,
         4189  +** the compiled regular expression can be reused on multiple
         4190  +** invocations of the same function.
  4189   4191   **
  4190   4192   ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
  4191   4193   ** associated by the sqlite3_set_auxdata() function with the Nth argument
  4192         -** value to the application-defined function. ^If no metadata has been ever
  4193         -** been set for the Nth argument of the function, or if the corresponding
  4194         -** function parameter has changed since the meta-data was set,
  4195         -** then sqlite3_get_auxdata() returns a NULL pointer.
  4196         -**
  4197         -** ^The sqlite3_set_auxdata() interface saves the metadata
  4198         -** pointed to by its 3rd parameter as the metadata for the N-th
  4199         -** argument of the application-defined function.  Subsequent
  4200         -** calls to sqlite3_get_auxdata() might return this data, if it has
  4201         -** not been destroyed.
  4202         -** ^If it is not NULL, SQLite will invoke the destructor
  4203         -** function given by the 4th parameter to sqlite3_set_auxdata() on
  4204         -** the metadata when the corresponding function parameter changes
  4205         -** or when the SQL statement completes, whichever comes first.
  4206         -**
  4207         -** SQLite is free to call the destructor and drop metadata on any
  4208         -** parameter of any function at any time.  ^The only guarantee is that
  4209         -** the destructor will be called before the metadata is dropped.
         4194  +** value to the application-defined function. ^If there is no metadata
         4195  +** associated with the function argument, this sqlite3_get_auxdata() interface
         4196  +** returns a NULL pointer.
         4197  +**
         4198  +** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
         4199  +** argument of the application-defined function.  ^Subsequent
         4200  +** calls to sqlite3_get_auxdata(C,N) return P from the most recent
         4201  +** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
         4202  +** NULL if the metadata has been discarded.
         4203  +** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
         4204  +** SQLite will invoke the destructor function X with parameter P exactly
         4205  +** once, when the metadata is discarded.
         4206  +** SQLite is free to discard the metadata at any time, including: <ul>
         4207  +** <li> when the corresponding function parameter changes, or
         4208  +** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
         4209  +**      SQL statement, or
         4210  +** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
         4211  +** <li> during the original sqlite3_set_auxdata() call when a memory 
         4212  +**      allocation error occurs. </ul>)^
         4213  +**
         4214  +** Note the last bullet in particular.  The destructor X in 
         4215  +** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
         4216  +** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
         4217  +** should be called near the end of the function implementation and the
         4218  +** function implementation should not make any use of P after
         4219  +** sqlite3_set_auxdata() has been called.
  4210   4220   **
  4211   4221   ** ^(In practice, metadata is preserved between function calls for
  4212         -** expressions that are constant at compile time. This includes literal
  4213         -** values and [parameters].)^
         4222  +** function parameters that are compile-time constants, including literal
         4223  +** values and [parameters] and expressions composed from the same.)^
  4214   4224   **
  4215   4225   ** These routines must be called from the same thread in which
  4216   4226   ** the SQL function is running.
  4217   4227   */
  4218   4228   void *sqlite3_get_auxdata(sqlite3_context*, int N);
  4219   4229   void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
  4220   4230   
................................................................................
  5119   5129   ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
  5120   5130   ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
  5121   5131   **
  5122   5132   ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
  5123   5133   ** on the list of automatic extensions is a harmless no-op. ^No entry point
  5124   5134   ** will be called more than once for each database connection that is opened.
  5125   5135   **
  5126         -** See also: [sqlite3_reset_auto_extension()].
         5136  +** See also: [sqlite3_reset_auto_extension()]
         5137  +** and [sqlite3_cancel_auto_extension()]
  5127   5138   */
  5128   5139   int sqlite3_auto_extension(void (*xEntryPoint)(void));
  5129   5140   
         5141  +/*
         5142  +** CAPI3REF: Cancel Automatic Extension Loading
         5143  +**
         5144  +** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
         5145  +** initialization routine X that was registered using a prior call to
         5146  +** [sqlite3_auto_extension(X)].  ^The [sqlite3_cancel_auto_extension(X)]
         5147  +** routine returns 1 if initialization routine X was successfully 
         5148  +** unregistered and it returns 0 if X was not on the list of initialization
         5149  +** routines.
         5150  +*/
         5151  +int sqlite3_cancel_auto_extension(void (*xEntryPoint)(void));
         5152  +
  5130   5153   /*
  5131   5154   ** CAPI3REF: Reset Automatic Extension Loading
  5132   5155   **
  5133   5156   ** ^This interface disables all automatic extensions previously
  5134   5157   ** registered using [sqlite3_auto_extension()].
  5135   5158   */
  5136   5159   void sqlite3_reset_auto_extension(void);
................................................................................
  6237   6260   ** transaction rollback or database recovery operations are not included.
  6238   6261   ** If an IO or other error occurs while writing a page to disk, the effect
  6239   6262   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined). ^The
  6240   6263   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  6241   6264   ** </dd>
  6242   6265   **
  6243   6266   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  6244         -** <dd>This parameter returns the zero for the current value if and only if
  6245         -** there all foreign key constraints (deferred or immediate) have been
  6246         -** resolved.  The highwater mark is always 0.
         6267  +** <dd>This parameter returns zero for the current value if and only if
         6268  +** all foreign key constraints (deferred or immediate) have been
         6269  +** resolved.)^  ^The highwater mark is always 0.
  6247   6270   ** </dd>
  6248   6271   ** </dl>
  6249   6272   */
  6250   6273   #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
  6251   6274   #define SQLITE_DBSTATUS_CACHE_USED           1
  6252   6275   #define SQLITE_DBSTATUS_SCHEMA_USED          2
  6253   6276   #define SQLITE_DBSTATUS_STMT_USED            3

Changes to src/sqliteInt.h.

   157    157   **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
   158    158   **
   159    159   ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
   160    160   ** assert() macro is enabled, each call into the Win32 native heap subsystem
   161    161   ** will cause HeapValidate to be called.  If heap validation should fail, an
   162    162   ** assertion will be triggered.
   163    163   **
   164         -** (Historical note:  There used to be several other options, but we've
   165         -** pared it down to just these three.)
   166         -**
   167    164   ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
   168    165   ** the default.
   169    166   */
   170    167   #if defined(SQLITE_SYSTEM_MALLOC) \
   171    168     + defined(SQLITE_WIN32_MALLOC) \
   172    169     + defined(SQLITE_ZERO_MALLOC) \
   173    170     + defined(SQLITE_MEMDEBUG)>1
................................................................................
   197    194   ** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
   198    195   ** it.
   199    196   */
   200    197   #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
   201    198   #  define _XOPEN_SOURCE 600
   202    199   #endif
   203    200   
   204         -/*
   205         -** The TCL headers are only needed when compiling the TCL bindings.
   206         -*/
   207         -#if defined(SQLITE_TCL) || defined(TCLSH)
   208         -# include <tcl.h>
   209         -#endif
   210         -
   211    201   /*
   212    202   ** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
   213    203   ** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
   214    204   ** make it true by defining or undefining NDEBUG.
   215    205   **
   216         -** Setting NDEBUG makes the code smaller and run faster by disabling the
   217         -** number assert() statements in the code.  So we want the default action
          206  +** Setting NDEBUG makes the code smaller and faster by disabling the
          207  +** assert() statements in the code.  So we want the default action
   218    208   ** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
   219    209   ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
   220    210   ** feature.
   221    211   */
   222    212   #if !defined(NDEBUG) && !defined(SQLITE_DEBUG) 
   223    213   # define NDEBUG 1
   224    214   #endif
................................................................................
   280    270   ** of SQLite to unexpected behavior - to make the code "self-healing"
   281    271   ** or "ductile" rather than being "brittle" and crashing at the first
   282    272   ** hint of unplanned behavior.
   283    273   **
   284    274   ** In other words, ALWAYS and NEVER are added for defensive code.
   285    275   **
   286    276   ** When doing coverage testing ALWAYS and NEVER are hard-coded to
   287         -** be true and false so that the unreachable code then specify will
          277  +** be true and false so that the unreachable code they specify will
   288    278   ** not be counted as untested code.
   289    279   */
   290    280   #if defined(SQLITE_COVERAGE_TEST)
   291    281   # define ALWAYS(X)      (1)
   292    282   # define NEVER(X)       (0)
   293    283   #elif !defined(NDEBUG)
   294    284   # define ALWAYS(X)      ((X)?1:(assert(0),0))
................................................................................
   304    294   ** macros to verify that we have tested SQLite for large-file support.
   305    295   */
   306    296   #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
   307    297   
   308    298   /*
   309    299   ** The macro unlikely() is a hint that surrounds a boolean
   310    300   ** expression that is usually false.  Macro likely() surrounds
   311         -** a boolean expression that is usually true.  GCC is able to
   312         -** use these hints to generate better code, sometimes.
          301  +** a boolean expression that is usually true.  These hints could,
          302  +** in theory, be used by the compiler to generate better code, but
          303  +** currently they are just comments for human readers.
   313    304   */
   314         -#if defined(__GNUC__) && 0
   315         -# define likely(X)    __builtin_expect((X),1)
   316         -# define unlikely(X)  __builtin_expect((X),0)
   317         -#else
   318         -# define likely(X)    !!(X)
   319         -# define unlikely(X)  !!(X)
   320         -#endif
          305  +#define likely(X)    (X)
          306  +#define unlikely(X)  (X)
   321    307   
   322    308   #include "sqlite3.h"
   323    309   #include "hash.h"
   324    310   #include "parse.h"
   325    311   #include <stdio.h>
   326    312   #include <stdlib.h>
   327    313   #include <string.h>
................................................................................
  1556   1542     tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
  1557   1543     Table *pTable;           /* The SQL table being indexed */
  1558   1544     char *zColAff;           /* String defining the affinity of each column */
  1559   1545     Index *pNext;            /* The next index associated with the same table */
  1560   1546     Schema *pSchema;         /* Schema containing this index */
  1561   1547     u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
  1562   1548     char **azColl;           /* Array of collation sequence names for index */
         1549  +  Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
  1563   1550     int tnum;                /* DB Page containing root of this index */
  1564   1551     u16 nColumn;             /* Number of columns in table used by this index */
  1565   1552     u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  1566   1553     unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  1567   1554     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
  1568   1555     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  1569   1556   #ifdef SQLITE_ENABLE_STAT3
................................................................................
  2036   2023   */
  2037   2024   #define NC_AllowAgg  0x01    /* Aggregate functions are allowed here */
  2038   2025   #define NC_HasAgg    0x02    /* One or more aggregate functions seen */
  2039   2026   #define NC_IsCheck   0x04    /* True if resolving names in a CHECK constraint */
  2040   2027   #define NC_InAggFunc 0x08    /* True if analyzing arguments to an agg func */
  2041   2028   #define NC_AsMaybe   0x10    /* Resolve to AS terms of the result set only
  2042   2029                                ** if no other resolution is available */
         2030  +#define NC_PartIdx   0x20    /* True if resolving a partial index WHERE */
  2043   2031   
  2044   2032   /*
  2045   2033   ** An instance of the following structure contains all information
  2046   2034   ** needed to generate code for a single SELECT statement.
  2047   2035   **
  2048   2036   ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
  2049   2037   ** If there is a LIMIT clause, the parser sets nLimit to the value of the
................................................................................
  2220   2208     int iRangeReg;       /* First register in temporary register block */
  2221   2209     int nErr;            /* Number of errors seen */
  2222   2210     int nTab;            /* Number of previously allocated VDBE cursors */
  2223   2211     int nMem;            /* Number of memory cells used so far */
  2224   2212     int nSet;            /* Number of sets used so far */
  2225   2213     int nOnce;           /* Number of OP_Once instructions so far */
  2226   2214     int ckBase;          /* Base register of data during check constraints */
         2215  +  int iPartIdxTab;     /* Table corresponding to a partial index */
  2227   2216     int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
  2228   2217     int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
  2229   2218     struct yColCache {
  2230   2219       int iTable;           /* Table cursor number */
  2231   2220       int iColumn;          /* Table column number */
  2232   2221       u8 tempReg;           /* iReg is a temp register that needs to be freed */
  2233   2222       int iLevel;           /* Nesting level */
................................................................................
  2805   2794   void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
  2806   2795   int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
  2807   2796   void sqlite3SrcListShiftJoinType(SrcList*);
  2808   2797   void sqlite3SrcListAssignCursors(Parse*, SrcList*);
  2809   2798   void sqlite3IdListDelete(sqlite3*, IdList*);
  2810   2799   void sqlite3SrcListDelete(sqlite3*, SrcList*);
  2811   2800   Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
  2812         -                        Token*, int, int);
         2801  +                          Expr*, int, int);
  2813   2802   void sqlite3DropIndex(Parse*, SrcList*, int);
  2814   2803   int sqlite3Select(Parse*, Select*, SelectDest*);
  2815   2804   Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
  2816   2805                            Expr*,ExprList*,u16,Expr*,Expr*);
  2817   2806   void sqlite3SelectDelete(sqlite3*, Select*);
  2818   2807   Table *sqlite3SrcListLookup(Parse*, SrcList*);
  2819   2808   int sqlite3IsReadOnly(Parse*, Table*, int);
................................................................................
  2853   2842   Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
  2854   2843   Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  2855   2844   void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
  2856   2845   void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
  2857   2846   void sqlite3Vacuum(Parse*);
  2858   2847   int sqlite3RunVacuum(char**, sqlite3*);
  2859   2848   char *sqlite3NameFromToken(sqlite3*, Token*);
  2860         -int sqlite3ExprCompare(Expr*, Expr*);
  2861         -int sqlite3ExprListCompare(ExprList*, ExprList*);
         2849  +int sqlite3ExprCompare(Expr*, Expr*, int);
         2850  +int sqlite3ExprListCompare(ExprList*, ExprList*, int);
         2851  +int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
  2862   2852   void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
  2863   2853   void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
  2864   2854   int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
  2865   2855   Vdbe *sqlite3GetVdbe(Parse*);
  2866   2856   void sqlite3PrngSaveState(void);
  2867   2857   void sqlite3PrngRestoreState(void);
  2868   2858   void sqlite3PrngResetState(void);
................................................................................
  2881   2871   int sqlite3ExprIsInteger(Expr*, int*);
  2882   2872   int sqlite3ExprCanBeNull(const Expr*);
  2883   2873   void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
  2884   2874   int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
  2885   2875   int sqlite3IsRowid(const char*);
  2886   2876   void sqlite3GenerateRowDelete(Parse*, Table*, int, int, int, Trigger *, int);
  2887   2877   void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int*);
  2888         -int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int);
         2878  +int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*);
  2889   2879   void sqlite3GenerateConstraintChecks(Parse*,Table*,int,int,
  2890   2880                                        int*,int,int,int,int,int*);
  2891   2881   void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
  2892   2882   int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
  2893   2883   void sqlite3BeginWriteOperation(Parse*, int, int);
  2894   2884   void sqlite3MultiWrite(Parse*);
  2895   2885   void sqlite3MayAbort(Parse*);
................................................................................
  3084   3074   void sqlite3NestedParse(Parse*, const char*, ...);
  3085   3075   void sqlite3ExpirePreparedStatements(sqlite3*);
  3086   3076   int sqlite3CodeSubselect(Parse *, Expr *, int, int);
  3087   3077   void sqlite3SelectPrep(Parse*, Select*, NameContext*);
  3088   3078   int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
  3089   3079   int sqlite3ResolveExprNames(NameContext*, Expr*);
  3090   3080   void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
         3081  +void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
  3091   3082   int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
  3092   3083   void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
  3093   3084   void sqlite3AlterFinishAddColumn(Parse *, Token *);
  3094   3085   void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
  3095   3086   CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
  3096   3087   char sqlite3AffinityType(const char*);
  3097   3088   void sqlite3Analyze(Parse*, Token*, Token*);

Changes to src/test_autoext.c.

    93     93     int objc,
    94     94     Tcl_Obj *CONST objv[]
    95     95   ){
    96     96     int rc = sqlite3_auto_extension((void*)sqr_init);
    97     97     Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
    98     98     return SQLITE_OK;
    99     99   }
          100  +
          101  +/*
          102  +** tclcmd:   sqlite3_cancel_auto_extension_sqr
          103  +**
          104  +** Unregister the "sqr" extension.
          105  +*/
          106  +static int cancelAutoExtSqrObjCmd(
          107  +  void * clientData,
          108  +  Tcl_Interp *interp,
          109  +  int objc,
          110  +  Tcl_Obj *CONST objv[]
          111  +){
          112  +  int rc = sqlite3_cancel_auto_extension((void*)sqr_init);
          113  +  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
          114  +  return SQLITE_OK;
          115  +}
   100    116   
   101    117   /*
   102    118   ** tclcmd:   sqlite3_auto_extension_cube
   103    119   **
   104    120   ** Register the "cube" extension to be loaded automatically.
   105    121   */
   106    122   static int autoExtCubeObjCmd(
................................................................................
   109    125     int objc,
   110    126     Tcl_Obj *CONST objv[]
   111    127   ){
   112    128     int rc = sqlite3_auto_extension((void*)cube_init);
   113    129     Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
   114    130     return SQLITE_OK;
   115    131   }
          132  +
          133  +/*
          134  +** tclcmd:   sqlite3_cancel_auto_extension_cube
          135  +**
          136  +** Unregister the "cube" extension.
          137  +*/
          138  +static int cancelAutoExtCubeObjCmd(
          139  +  void * clientData,
          140  +  Tcl_Interp *interp,
          141  +  int objc,
          142  +  Tcl_Obj *CONST objv[]
          143  +){
          144  +  int rc = sqlite3_cancel_auto_extension((void*)cube_init);
          145  +  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
          146  +  return SQLITE_OK;
          147  +}
   116    148   
   117    149   /*
   118    150   ** tclcmd:   sqlite3_auto_extension_broken
   119    151   **
   120    152   ** Register the broken extension to be loaded automatically.
   121    153   */
   122    154   static int autoExtBrokenObjCmd(
................................................................................
   125    157     int objc,
   126    158     Tcl_Obj *CONST objv[]
   127    159   ){
   128    160     int rc = sqlite3_auto_extension((void*)broken_init);
   129    161     Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
   130    162     return SQLITE_OK;
   131    163   }
          164  +
          165  +/*
          166  +** tclcmd:   sqlite3_cancel_auto_extension_broken
          167  +**
          168  +** Unregister the broken extension.
          169  +*/
          170  +static int cancelAutoExtBrokenObjCmd(
          171  +  void * clientData,
          172  +  Tcl_Interp *interp,
          173  +  int objc,
          174  +  Tcl_Obj *CONST objv[]
          175  +){
          176  +  int rc = sqlite3_cancel_auto_extension((void*)broken_init);
          177  +  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
          178  +  return SQLITE_OK;
          179  +}
   132    180   
   133    181   #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   134    182   
   135    183   
   136    184   /*
   137    185   ** tclcmd:   sqlite3_reset_auto_extension
   138    186   **
................................................................................
   157    205     Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr",
   158    206             autoExtSqrObjCmd, 0, 0);
   159    207     Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube",
   160    208             autoExtCubeObjCmd, 0, 0);
   161    209     Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken",
   162    210             autoExtBrokenObjCmd, 0, 0);
   163    211   #endif
          212  +  Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_sqr",
          213  +          cancelAutoExtSqrObjCmd, 0, 0);
          214  +  Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_cube",
          215  +          cancelAutoExtCubeObjCmd, 0, 0);
          216  +  Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_broken",
          217  +          cancelAutoExtBrokenObjCmd, 0, 0);
   164    218     Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
   165    219             resetAutoExtObjCmd, 0, 0);
   166    220     return TCL_OK;
   167    221   }

Changes to src/test_vfs.c.

    24     24   **   -mxpathname INTEGER        (Value for sqlite3_vfs.mxPathname)
    25     25   **   -iversion   INTEGER        (Value for sqlite3_vfs.iVersion)
    26     26   */
    27     27   #if SQLITE_TEST          /* This file is used for testing only */
    28     28   
    29     29   #include "sqlite3.h"
    30     30   #include "sqliteInt.h"
           31  +#include <tcl.h>
    31     32   
    32     33   typedef struct Testvfs Testvfs;
    33     34   typedef struct TestvfsShm TestvfsShm;
    34     35   typedef struct TestvfsBuffer TestvfsBuffer;
    35     36   typedef struct TestvfsFile TestvfsFile;
    36     37   typedef struct TestvfsFd TestvfsFd;
    37     38   

Changes to src/update.c.

   242    242     for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){}
   243    243     if( nIdx>0 ){
   244    244       aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx );
   245    245       if( aRegIdx==0 ) goto update_cleanup;
   246    246     }
   247    247     for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
   248    248       int reg;
   249         -    if( hasFK || chngRowid ){
          249  +    if( hasFK || chngRowid || pIdx->pPartIdxWhere ){
   250    250         reg = ++pParse->nMem;
   251    251       }else{
   252    252         reg = 0;
   253    253         for(i=0; i<pIdx->nColumn; i++){
   254    254           if( aXRef[pIdx->aiColumn[i]]>=0 ){
   255    255             reg = ++pParse->nMem;
   256    256             break;

Changes to src/vdbe.c.

   561    561     int rc = SQLITE_OK;        /* Value to return */
   562    562     sqlite3 *db = p->db;       /* The database */
   563    563     u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
   564    564     u8 encoding = ENC(db);     /* The database encoding */
   565    565     int iCompare = 0;          /* Result of last OP_Compare operation */
   566    566     unsigned nVmStep = 0;      /* Number of virtual machine steps */
   567    567   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   568         -  unsigned nProgressOps = 0; /* nVmStep at last progress callback. */
          568  +  unsigned nProgressLimit;   /* Invoke xProgress() when nVmStep reaches this */
   569    569   #endif
   570    570     Mem *aMem = p->aMem;       /* Copy of p->aMem */
   571    571     Mem *pIn1 = 0;             /* 1st input operand */
   572    572     Mem *pIn2 = 0;             /* 2nd input operand */
   573    573     Mem *pIn3 = 0;             /* 3rd input operand */
   574    574     Mem *pOut = 0;             /* Output operand */
   575    575     int *aPermute = 0;         /* Permutation of columns for OP_Compare */
................................................................................
   591    591     assert( p->bIsReader || p->readOnly!=0 );
   592    592     p->rc = SQLITE_OK;
   593    593     assert( p->explain==0 );
   594    594     p->pResultSet = 0;
   595    595     db->busyHandler.nBusy = 0;
   596    596     CHECK_FOR_INTERRUPT;
   597    597     sqlite3VdbeIOTraceSql(p);
          598  +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
          599  +  if( db->xProgress ){
          600  +    assert( 0 < db->nProgressOps );
          601  +    nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP-1];
          602  +    if( nProgressLimit==0 ){
          603  +      nProgressLimit = db->nProgressOps;
          604  +    }else{
          605  +      nProgressLimit %= (unsigned)db->nProgressOps;
          606  +    }
          607  +  }
          608  +#endif
   598    609   #ifdef SQLITE_DEBUG
   599    610     sqlite3BeginBenignMalloc();
   600    611     if( p->pc==0  && (p->db->flags & SQLITE_VdbeListing)!=0 ){
   601    612       int i;
   602    613       printf("VDBE Program Listing:\n");
   603    614       sqlite3VdbePrintSql(p);
   604    615       for(i=0; i<p->nOp; i++){
................................................................................
   751    762   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
   752    763     /* Call the progress callback if it is configured and the required number
   753    764     ** of VDBE ops have been executed (either since this invocation of
   754    765     ** sqlite3VdbeExec() or since last time the progress callback was called).
   755    766     ** If the progress callback returns non-zero, exit the virtual machine with
   756    767     ** a return code SQLITE_ABORT.
   757    768     */
   758         -  if( db->xProgress!=0 && (nVmStep - nProgressOps)>=db->nProgressOps ){
          769  +  if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
   759    770       int prc;
   760    771       prc = db->xProgress(db->pProgressArg);
   761    772       if( prc!=0 ){
   762    773         rc = SQLITE_INTERRUPT;
   763    774         goto vdbe_error_halt;
   764    775       }
   765         -    nProgressOps = nVmStep;
          776  +    if( db->xProgress!=0 ){
          777  +      nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
          778  +    }
   766    779     }
   767    780   #endif
   768    781     
   769    782     break;
   770    783   }
   771    784   
   772    785   /* Opcode:  Gosub P1 P2 * * *
................................................................................
  1426   1439       assert( memIsValid(pArg) );
  1427   1440       apVal[i] = pArg;
  1428   1441       Deephemeralize(pArg);
  1429   1442       sqlite3VdbeMemStoreType(pArg);
  1430   1443       REGISTER_TRACE(pOp->p2+i, pArg);
  1431   1444     }
  1432   1445   
  1433         -  assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
  1434         -  if( pOp->p4type==P4_FUNCDEF ){
  1435         -    ctx.pFunc = pOp->p4.pFunc;
  1436         -    ctx.pVdbeFunc = 0;
  1437         -  }else{
  1438         -    ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
  1439         -    ctx.pFunc = ctx.pVdbeFunc->pFunc;
  1440         -  }
  1441         -
         1446  +  assert( pOp->p4type==P4_FUNCDEF );
         1447  +  ctx.pFunc = pOp->p4.pFunc;
  1442   1448     ctx.s.flags = MEM_Null;
  1443   1449     ctx.s.db = db;
  1444   1450     ctx.s.xDel = 0;
  1445   1451     ctx.s.zMalloc = 0;
         1452  +  ctx.iOp = pc;
         1453  +  ctx.pVdbe = p;
  1446   1454   
  1447   1455     /* The output cell may already have a buffer allocated. Move
  1448   1456     ** the pointer to ctx.s so in case the user-function can use
  1449   1457     ** the already allocated buffer instead of allocating a new one.
  1450   1458     */
  1451   1459     sqlite3VdbeMemMove(&ctx.s, pOut);
  1452   1460     MemSetTypeFlag(&ctx.s, MEM_Null);
................................................................................
  1461   1469     db->lastRowid = lastRowid;
  1462   1470     (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  1463   1471     lastRowid = db->lastRowid;
  1464   1472   
  1465   1473     /* If any auxiliary data functions have been called by this user function,
  1466   1474     ** immediately call the destructor for any non-static values.
  1467   1475     */
  1468         -  if( ctx.pVdbeFunc ){
  1469         -    sqlite3VdbeDeleteAuxData(ctx.pVdbeFunc, pOp->p1);
  1470         -    pOp->p4.pVdbeFunc = ctx.pVdbeFunc;
  1471         -    pOp->p4type = P4_VDBEFUNC;
  1472         -  }
         1476  +  sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
  1473   1477   
  1474   1478     if( db->mallocFailed ){
  1475   1479       /* Even though a malloc() has failed, the implementation of the
  1476   1480       ** user function may have called an sqlite3_result_XXX() function
  1477   1481       ** to return a value. The following call releases any resources
  1478   1482       ** associated with such a value.
  1479   1483       */

Changes to src/vdbe.h.

    26     26   */
    27     27   typedef struct Vdbe Vdbe;
    28     28   
    29     29   /*
    30     30   ** The names of the following types declared in vdbeInt.h are required
    31     31   ** for the VdbeOp definition.
    32     32   */
    33         -typedef struct VdbeFunc VdbeFunc;
    34     33   typedef struct Mem Mem;
    35     34   typedef struct SubProgram SubProgram;
    36     35   
    37     36   /*
    38     37   ** A single instruction of the virtual machine has an opcode
    39     38   ** and as many as three operands.  The instruction is recorded
    40     39   ** as an instance of the following structure:
................................................................................
    50     49     union {             /* fourth parameter */
    51     50       int i;                 /* Integer value if p4type==P4_INT32 */
    52     51       void *p;               /* Generic pointer */
    53     52       char *z;               /* Pointer to data for string (char array) types */
    54     53       i64 *pI64;             /* Used when p4type is P4_INT64 */
    55     54       double *pReal;         /* Used when p4type is P4_REAL */
    56     55       FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
    57         -    VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
    58     56       CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
    59     57       Mem *pMem;             /* Used when p4type is P4_MEM */
    60     58       VTable *pVtab;         /* Used when p4type is P4_VTAB */
    61     59       KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
    62     60       int *ai;               /* Used when p4type is P4_INTARRAY */
    63     61       SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
    64     62       Table *pTab;           /* Used when p4type is P4_TABLE */
................................................................................
   105    103   */
   106    104   #define P4_NOTUSED    0   /* The P4 parameter is not used */
   107    105   #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
   108    106   #define P4_STATIC   (-2)  /* Pointer to a static string */
   109    107   #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
   110    108   #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
   111    109   #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
   112         -#define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
   113    110   #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
   114    111   #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
   115    112   #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
   116    113   #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
   117    114   #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
   118    115   #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
   119    116   #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
................................................................................
   205    202   void sqlite3VdbeSetNumCols(Vdbe*,int);
   206    203   int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
   207    204   void sqlite3VdbeCountChanges(Vdbe*);
   208    205   sqlite3 *sqlite3VdbeDb(Vdbe*);
   209    206   void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
   210    207   void sqlite3VdbeSwap(Vdbe*,Vdbe*);
   211    208   VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
   212         -sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8);
          209  +sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
   213    210   void sqlite3VdbeSetVarmask(Vdbe*, int);
   214    211   #ifndef SQLITE_OMIT_TRACE
   215    212     char *sqlite3VdbeExpandSql(Vdbe*, const char*);
   216    213   #endif
   217    214   
   218    215   void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
   219    216   int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);

Changes to src/vdbeInt.h.

    40     40   
    41     41   /* Opaque type used by code in vdbesort.c */
    42     42   typedef struct VdbeSorter VdbeSorter;
    43     43   
    44     44   /* Opaque type used by the explainer */
    45     45   typedef struct Explain Explain;
    46     46   
           47  +/* Elements of the linked list at Vdbe.pAuxData */
           48  +typedef struct AuxData AuxData;
           49  +
    47     50   /*
    48     51   ** A cursor is a pointer into a single BTree within a database file.
    49     52   ** The cursor can seek to a BTree entry with a particular key, or
    50     53   ** loop over all entries of the Btree.  You can also insert new BTree
    51     54   ** entries or retrieve the key or data from the entry that the cursor
    52     55   ** is currently pointing to.
    53     56   ** 
................................................................................
   226    229   ** Return true if a memory cell is not marked as invalid.  This macro
   227    230   ** is for use inside assert() statements only.
   228    231   */
   229    232   #ifdef SQLITE_DEBUG
   230    233   #define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
   231    234   #endif
   232    235   
   233         -
   234         -/* A VdbeFunc is just a FuncDef (defined in sqliteInt.h) that contains
   235         -** additional information about auxiliary information bound to arguments
   236         -** of the function.  This is used to implement the sqlite3_get_auxdata()
   237         -** and sqlite3_set_auxdata() APIs.  The "auxdata" is some auxiliary data
   238         -** that can be associated with a constant argument to a function.  This
   239         -** allows functions such as "regexp" to compile their constant regular
   240         -** expression argument once and reused the compiled code for multiple
   241         -** invocations.
          236  +/*
          237  +** Each auxilliary data pointer stored by a user defined function 
          238  +** implementation calling sqlite3_set_auxdata() is stored in an instance
          239  +** of this structure. All such structures associated with a single VM
          240  +** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
          241  +** when the VM is halted (if not before).
   242    242   */
   243         -struct VdbeFunc {
   244         -  FuncDef *pFunc;               /* The definition of the function */
   245         -  int nAux;                     /* Number of entries allocated for apAux[] */
   246         -  struct AuxData {
   247         -    void *pAux;                   /* Aux data for the i-th argument */
   248         -    void (*xDelete)(void *);      /* Destructor for the aux data */
   249         -  } apAux[1];                   /* One slot for each function argument */
          243  +struct AuxData {
          244  +  int iOp;                        /* Instruction number of OP_Function opcode */
          245  +  int iArg;                       /* Index of function argument. */
          246  +  void *pAux;                     /* Aux data pointer */
          247  +  void (*xDelete)(void *);        /* Destructor for the aux data */
          248  +  AuxData *pNext;                 /* Next element in list */
   250    249   };
   251    250   
   252    251   /*
   253    252   ** The "context" argument for a installable function.  A pointer to an
   254    253   ** instance of this structure is the first argument to the routines used
   255    254   ** implement the SQL functions.
   256    255   **
................................................................................
   260    259   ** structure are known.
   261    260   **
   262    261   ** This structure is defined inside of vdbeInt.h because it uses substructures
   263    262   ** (Mem) which are only defined there.
   264    263   */
   265    264   struct sqlite3_context {
   266    265     FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
   267         -  VdbeFunc *pVdbeFunc;  /* Auxilary data, if created. */
   268    266     Mem s;                /* The return value is stored here */
   269    267     Mem *pMem;            /* Memory cell used to store aggregate context */
   270    268     CollSeq *pColl;       /* Collating sequence */
   271    269     int isError;          /* Error code returned by the function. */
   272    270     int skipFlag;         /* Skip skip accumulator loading if true */
          271  +  int iOp;              /* Instruction number of OP_Function */
          272  +  Vdbe *pVdbe;          /* The VM that owns this context */
   273    273   };
   274    274   
   275    275   /*
   276    276   ** An Explain object accumulates indented output which is helpful
   277    277   ** in describing recursive data structures.
   278    278   */
   279    279   struct Explain {
................................................................................
   364    364     VdbeFrame *pFrame;      /* Parent frame */
   365    365     VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
   366    366     int nFrame;             /* Number of frames in pFrame list */
   367    367     u32 expmask;            /* Binding to these vars invalidates VM */
   368    368     SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
   369    369     int nOnceFlag;          /* Size of array aOnceFlag[] */
   370    370     u8 *aOnceFlag;          /* Flags for OP_Once */
          371  +  AuxData *pAuxData;      /* Linked list of auxdata allocations */
   371    372   };
   372    373   
   373    374   /*
   374    375   ** The following are allowed values for Vdbe.magic
   375    376   */
   376    377   #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
   377    378   #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
................................................................................
   406    407   #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
   407    408   void sqlite3VdbePrintOp(FILE*, int, Op*);
   408    409   #endif
   409    410   u32 sqlite3VdbeSerialTypeLen(u32);
   410    411   u32 sqlite3VdbeSerialType(Mem*, int);
   411    412   u32 sqlite3VdbeSerialPut(unsigned char*, int, Mem*, int);
   412    413   u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   413         -void sqlite3VdbeDeleteAuxData(VdbeFunc*, int);
          414  +void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
   414    415   
   415    416   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   416    417   int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
   417    418   int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
   418    419   int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
   419    420   int sqlite3VdbeExec(Vdbe*);
   420    421   int sqlite3VdbeList(Vdbe*);

Changes to src/vdbeapi.c.

   580    580   }
   581    581   
   582    582   /*
   583    583   ** Return the auxilary data pointer, if any, for the iArg'th argument to
   584    584   ** the user-function defined by pCtx.
   585    585   */
   586    586   void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
   587         -  VdbeFunc *pVdbeFunc;
          587  +  AuxData *pAuxData;
   588    588   
   589    589     assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   590         -  pVdbeFunc = pCtx->pVdbeFunc;
   591         -  if( !pVdbeFunc || iArg>=pVdbeFunc->nAux || iArg<0 ){
   592         -    return 0;
          590  +  for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
          591  +    if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
   593    592     }
   594         -  return pVdbeFunc->apAux[iArg].pAux;
          593  +
          594  +  return (pAuxData ? pAuxData->pAux : 0);
   595    595   }
   596    596   
   597    597   /*
   598    598   ** Set the auxilary data pointer and delete function, for the iArg'th
   599    599   ** argument to the user-function defined by pCtx. Any previous value is
   600    600   ** deleted by calling the delete function specified when it was set.
   601    601   */
   602    602   void sqlite3_set_auxdata(
   603    603     sqlite3_context *pCtx, 
   604    604     int iArg, 
   605    605     void *pAux, 
   606    606     void (*xDelete)(void*)
   607    607   ){
   608         -  struct AuxData *pAuxData;
   609         -  VdbeFunc *pVdbeFunc;
   610         -  if( iArg<0 ) goto failed;
          608  +  AuxData *pAuxData;
          609  +  Vdbe *pVdbe = pCtx->pVdbe;
   611    610   
   612    611     assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
   613         -  pVdbeFunc = pCtx->pVdbeFunc;
   614         -  if( !pVdbeFunc || pVdbeFunc->nAux<=iArg ){
   615         -    int nAux = (pVdbeFunc ? pVdbeFunc->nAux : 0);
   616         -    int nMalloc = sizeof(VdbeFunc) + sizeof(struct AuxData)*iArg;
   617         -    pVdbeFunc = sqlite3DbRealloc(pCtx->s.db, pVdbeFunc, nMalloc);
   618         -    if( !pVdbeFunc ){
   619         -      goto failed;
   620         -    }
   621         -    pCtx->pVdbeFunc = pVdbeFunc;
   622         -    memset(&pVdbeFunc->apAux[nAux], 0, sizeof(struct AuxData)*(iArg+1-nAux));
   623         -    pVdbeFunc->nAux = iArg+1;
   624         -    pVdbeFunc->pFunc = pCtx->pFunc;
          612  +  if( iArg<0 ) goto failed;
          613  +
          614  +  for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
          615  +    if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
   625    616     }
   626         -
   627         -  pAuxData = &pVdbeFunc->apAux[iArg];
   628         -  if( pAuxData->pAux && pAuxData->xDelete ){
          617  +  if( pAuxData==0 ){
          618  +    pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
          619  +    if( !pAuxData ) goto failed;
          620  +    pAuxData->iOp = pCtx->iOp;
          621  +    pAuxData->iArg = iArg;
          622  +    pAuxData->pNext = pVdbe->pAuxData;
          623  +    pVdbe->pAuxData = pAuxData;
          624  +  }else if( pAuxData->xDelete ){
   629    625       pAuxData->xDelete(pAuxData->pAux);
   630    626     }
          627  +
   631    628     pAuxData->pAux = pAux;
   632    629     pAuxData->xDelete = xDelete;
   633    630     return;
   634    631   
   635    632   failed:
   636    633     if( xDelete ){
   637    634       xDelete(pAux);

Changes to src/vdbeaux.c.

   247    247   ** Resolve label "x" to be the address of the next instruction to
   248    248   ** be inserted.  The parameter "x" must have been obtained from
   249    249   ** a prior call to sqlite3VdbeMakeLabel().
   250    250   */
   251    251   void sqlite3VdbeResolveLabel(Vdbe *p, int x){
   252    252     int j = -1-x;
   253    253     assert( p->magic==VDBE_MAGIC_INIT );
   254         -  assert( j>=0 && j<p->nLabel );
   255         -  if( p->aLabel ){
          254  +  assert( j<p->nLabel );
          255  +  if( j>=0 && p->aLabel ){
   256    256       p->aLabel[j] = p->nOp;
   257    257     }
   258    258   }
   259    259   
   260    260   /*
   261    261   ** Mark the VDBE as one that can only be run one time.
   262    262   */
................................................................................
   572    572   }
   573    573   
   574    574   /*
   575    575   ** Change the P2 operand of instruction addr so that it points to
   576    576   ** the address of the next instruction to be coded.
   577    577   */
   578    578   void sqlite3VdbeJumpHere(Vdbe *p, int addr){
   579         -  assert( addr>=0 || p->db->mallocFailed );
   580         -  if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
          579  +  if( ALWAYS(addr>=0) ) sqlite3VdbeChangeP2(p, addr, p->nOp);
   581    580   }
   582    581   
   583    582   
   584    583   /*
   585    584   ** If the input FuncDef structure is ephemeral, then free it.  If
   586    585   ** the FuncDef is not ephermal, then do nothing.
   587    586   */
................................................................................
   609    608           sqlite3DbFree(db, p4);
   610    609           break;
   611    610         }
   612    611         case P4_MPRINTF: {
   613    612           if( db->pnBytesFreed==0 ) sqlite3_free(p4);
   614    613           break;
   615    614         }
   616         -      case P4_VDBEFUNC: {
   617         -        VdbeFunc *pVdbeFunc = (VdbeFunc *)p4;
   618         -        freeEphemeralFunction(db, pVdbeFunc->pFunc);
   619         -        if( db->pnBytesFreed==0 ) sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
   620         -        sqlite3DbFree(db, pVdbeFunc);
   621         -        break;
   622         -      }
   623    615         case P4_FUNCDEF: {
   624    616           freeEphemeralFunction(db, (FuncDef*)p4);
   625    617           break;
   626    618         }
   627    619         case P4_MEM: {
   628    620           if( db->pnBytesFreed==0 ){
   629    621             sqlite3ValueFree((sqlite3_value*)p4);
................................................................................
  1645   1637       releaseMemArray(&p->aMem[1], p->nMem);
  1646   1638     }
  1647   1639     while( p->pDelFrame ){
  1648   1640       VdbeFrame *pDel = p->pDelFrame;
  1649   1641       p->pDelFrame = pDel->pParent;
  1650   1642       sqlite3VdbeFrameDelete(pDel);
  1651   1643     }
         1644  +
         1645  +  /* Delete any auxdata allocations made by the VM */
         1646  +  sqlite3VdbeDeleteAuxData(p, -1, 0);
         1647  +  assert( p->pAuxData==0 );
  1652   1648   }
  1653   1649   
  1654   1650   /*
  1655   1651   ** Clean up the VM after execution.
  1656   1652   **
  1657   1653   ** This routine will automatically close any cursors, lists, and/or
  1658   1654   ** sorters that were left open.  It also deletes the values of
................................................................................
  2443   2439       assert( (rc & p->db->errMask)==rc );
  2444   2440     }
  2445   2441     sqlite3VdbeDelete(p);
  2446   2442     return rc;
  2447   2443   }
  2448   2444   
  2449   2445   /*
  2450         -** Call the destructor for each auxdata entry in pVdbeFunc for which
  2451         -** the corresponding bit in mask is clear.  Auxdata entries beyond 31
  2452         -** are always destroyed.  To destroy all auxdata entries, call this
  2453         -** routine with mask==0.
         2446  +** If parameter iOp is less than zero, then invoke the destructor for
         2447  +** all auxiliary data pointers currently cached by the VM passed as
         2448  +** the first argument.
         2449  +**
         2450  +** Or, if iOp is greater than or equal to zero, then the destructor is
         2451  +** only invoked for those auxiliary data pointers created by the user 
         2452  +** function invoked by the OP_Function opcode at instruction iOp of 
         2453  +** VM pVdbe, and only then if:
         2454  +**
         2455  +**    * the associated function parameter is the 32nd or later (counting
         2456  +**      from left to right), or
         2457  +**
         2458  +**    * the corresponding bit in argument mask is clear (where the first
         2459  +**      function parameter corrsponds to bit 0 etc.).
  2454   2460   */
  2455         -void sqlite3VdbeDeleteAuxData(VdbeFunc *pVdbeFunc, int mask){
  2456         -  int i;
  2457         -  for(i=0; i<pVdbeFunc->nAux; i++){
  2458         -    struct AuxData *pAux = &pVdbeFunc->apAux[i];
  2459         -    if( (i>31 || !(mask&(((u32)1)<<i))) && pAux->pAux ){
         2461  +void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
         2462  +  AuxData **pp = &pVdbe->pAuxData;
         2463  +  while( *pp ){
         2464  +    AuxData *pAux = *pp;
         2465  +    if( (iOp<0)
         2466  +     || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & ((u32)1<<pAux->iArg))))
         2467  +    ){
  2460   2468         if( pAux->xDelete ){
  2461   2469           pAux->xDelete(pAux->pAux);
  2462   2470         }
  2463         -      pAux->pAux = 0;
         2471  +      *pp = pAux->pNext;
         2472  +      sqlite3DbFree(pVdbe->db, pAux);
         2473  +    }else{
         2474  +      pp= &pAux->pNext;
  2464   2475       }
  2465   2476     }
  2466   2477   }
  2467   2478   
  2468   2479   /*
  2469   2480   ** Free all memory associated with the Vdbe passed as the second argument,
  2470   2481   ** except for object itself, which is preserved.
................................................................................
  2976   2987   ** equal, then the keys are considered to be equal and
  2977   2988   ** the parts beyond the common prefix are ignored.
  2978   2989   */
  2979   2990   int sqlite3VdbeRecordCompare(
  2980   2991     int nKey1, const void *pKey1, /* Left key */
  2981   2992     UnpackedRecord *pPKey2        /* Right key */
  2982   2993   ){
  2983         -  int d1;            /* Offset into aKey[] of next data element */
         2994  +  u32 d1;            /* Offset into aKey[] of next data element */
  2984   2995     u32 idx1;          /* Offset into aKey[] of next header element */
  2985   2996     u32 szHdr1;        /* Number of bytes in header */
  2986   2997     int i = 0;
  2987   2998     int nField;
  2988   2999     int rc = 0;
  2989   3000     const unsigned char *aKey1 = (const unsigned char *)pKey1;
  2990   3001     KeyInfo *pKeyInfo;
................................................................................
  3010   3021     nField = pKeyInfo->nField;
  3011   3022     assert( pKeyInfo->aSortOrder!=0 );
  3012   3023     while( idx1<szHdr1 && i<pPKey2->nField ){
  3013   3024       u32 serial_type1;
  3014   3025   
  3015   3026       /* Read the serial types for the next element in each key. */
  3016   3027       idx1 += getVarint32( aKey1+idx1, serial_type1 );
  3017         -    if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
         3028  +    if( d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1 ) break;
  3018   3029   
  3019   3030       /* Extract the values to be compared.
  3020   3031       */
  3021   3032       d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
  3022   3033   
  3023   3034       /* Do the comparison
  3024   3035       */
................................................................................
  3239   3250   ** Return a pointer to an sqlite3_value structure containing the value bound
  3240   3251   ** parameter iVar of VM v. Except, if the value is an SQL NULL, return 
  3241   3252   ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
  3242   3253   ** constants) to the value before returning it.
  3243   3254   **
  3244   3255   ** The returned value must be freed by the caller using sqlite3ValueFree().
  3245   3256   */
  3246         -sqlite3_value *sqlite3VdbeGetValue(Vdbe *v, int iVar, u8 aff){
         3257  +sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
  3247   3258     assert( iVar>0 );
  3248   3259     if( v ){
  3249   3260       Mem *pMem = &v->aVar[iVar-1];
  3250   3261       if( 0==(pMem->flags & MEM_Null) ){
  3251   3262         sqlite3_value *pRet = sqlite3ValueNew(v->db);
  3252   3263         if( pRet ){
  3253   3264           sqlite3VdbeMemCopy((Mem *)pRet, pMem);

Changes to src/vtab.c.

   821    821     db->aVTrans = 0;
   822    822     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
   823    823       int (*x)(sqlite3_vtab *);
   824    824       sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
   825    825       if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
   826    826         rc = x(pVtab);
   827    827         sqlite3DbFree(db, *pzErrmsg);
   828         -      *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
   829         -      sqlite3_free(pVtab->zErrMsg);
          828  +      *pzErrmsg = pVtab->zErrMsg;
          829  +      pVtab->zErrMsg = 0;
   830    830       }
   831    831     }
   832    832     db->aVTrans = aVTrans;
   833    833     return rc;
   834    834   }
   835    835   
   836    836   /*

Changes to src/where.c.

    41     41   typedef struct WhereAndInfo WhereAndInfo;
    42     42   typedef struct WhereLevel WhereLevel;
    43     43   typedef struct WhereLoop WhereLoop;
    44     44   typedef struct WherePath WherePath;
    45     45   typedef struct WhereTerm WhereTerm;
    46     46   typedef struct WhereLoopBuilder WhereLoopBuilder;
    47     47   typedef struct WhereScan WhereScan;
           48  +typedef struct WhereOrCost WhereOrCost;
           49  +typedef struct WhereOrSet WhereOrSet;
    48     50   
    49     51   /*
    50     52   ** Cost X is tracked as 10*log2(X) stored in a 16-bit integer.  The
    51     53   ** maximum cost for ordinary tables is 64*(2**63) which becomes 6900.
    52     54   ** (Virtual tables can return a larger cost, but let's assume they do not.)
    53     55   ** So all costs can be stored in a 16-bit unsigned integer without risk
    54     56   ** of overflow.
................................................................................
   147    149     /**** whereLoopXfer() copies fields above ***********************/
   148    150   # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
   149    151     u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
   150    152     WhereTerm **aLTerm;   /* WhereTerms used */
   151    153     WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
   152    154     WhereTerm *aLTermSpace[4];  /* Initial aLTerm[] space */
   153    155   };
          156  +
          157  +/* This object holds the prerequisites and the cost of running a
          158  +** subquery on one operand of an OR operator in the WHERE clause.
          159  +** See WhereOrSet for additional information 
          160  +*/
          161  +struct WhereOrCost {
          162  +  Bitmask prereq;     /* Prerequisites */
          163  +  WhereCost rRun;     /* Cost of running this subquery */
          164  +  WhereCost nOut;     /* Number of outputs for this subquery */
          165  +};
          166  +
          167  +/* The WhereOrSet object holds a set of possible WhereOrCosts that
          168  +** correspond to the subquery(s) of OR-clause processing.  At most
          169  +** favorable N_OR_COST elements are retained.
          170  +*/
          171  +#define N_OR_COST 3
          172  +struct WhereOrSet {
          173  +  u16 n;                      /* Number of valid a[] entries */
          174  +  WhereOrCost a[N_OR_COST];   /* Set of best costs */
          175  +};
          176  +
   154    177   
   155    178   /* Forward declaration of methods */
   156    179   static int whereLoopResize(sqlite3*, WhereLoop*, int);
   157    180   
   158    181   /*
   159    182   ** Each instance of this object holds a sequence of WhereLoop objects
   160    183   ** that implement some or all of a query plan.
................................................................................
   362    385   ** to construct WhereLoop objects for a particular query.
   363    386   */
   364    387   struct WhereLoopBuilder {
   365    388     WhereInfo *pWInfo;        /* Information about this WHERE */
   366    389     WhereClause *pWC;         /* WHERE clause terms */
   367    390     ExprList *pOrderBy;       /* ORDER BY clause */
   368    391     WhereLoop *pNew;          /* Template WhereLoop */
   369         -  WhereLoop *pBest;         /* If non-NULL, store single best loop here */
          392  +  WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
   370    393   };
   371    394   
   372    395   /*
   373    396   ** The WHERE clause processing routine has two halves.  The
   374    397   ** first part does the start of the WHERE loop and the second
   375    398   ** half does the tail of the WHERE loop.  An instance of
   376    399   ** this structure is returned by the first half and passed
................................................................................
   504    527   ** Return TRUE if an UPDATE or DELETE statement can operate directly on
   505    528   ** the rowids returned by a WHERE clause.  Return FALSE if doing an
   506    529   ** UPDATE or DELETE might change subsequent WHERE clause results.
   507    530   */
   508    531   int sqlite3WhereOkOnePass(WhereInfo *pWInfo){
   509    532     return pWInfo->okOnePass;
   510    533   }
          534  +
          535  +/*
          536  +** Move the content of pSrc into pDest
          537  +*/
          538  +static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
          539  +  pDest->n = pSrc->n;
          540  +  memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
          541  +}
          542  +
          543  +/*
          544  +** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
          545  +**
          546  +** The new entry might overwrite an existing entry, or it might be
          547  +** appended, or it might be discarded.  Do whatever is the right thing
          548  +** so that pSet keeps the N_OR_COST best entries seen so far.
          549  +*/
          550  +static int whereOrInsert(
          551  +  WhereOrSet *pSet,      /* The WhereOrSet to be updated */
          552  +  Bitmask prereq,        /* Prerequisites of the new entry */
          553  +  WhereCost rRun,        /* Run-cost of the new entry */
          554  +  WhereCost nOut         /* Number of outputs for the new entry */
          555  +){
          556  +  u16 i;
          557  +  WhereOrCost *p;
          558  +  for(i=pSet->n, p=pSet->a; i>0; i--, p++){
          559  +    if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
          560  +      goto whereOrInsert_done;
          561  +    }
          562  +    if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
          563  +      return 0;
          564  +    }
          565  +  }
          566  +  if( pSet->n<N_OR_COST ){
          567  +    p = &pSet->a[pSet->n++];
          568  +    p->nOut = nOut;
          569  +  }else{
          570  +    p = pSet->a;
          571  +    for(i=1; i<pSet->n; i++){
          572  +      if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
          573  +    }
          574  +    if( p->rRun<=rRun ) return 0;
          575  +  }
          576  +whereOrInsert_done:
          577  +  p->prereq = prereq;
          578  +  p->rRun = rRun;
          579  +  if( p->nOut>nOut ) p->nOut = nOut;
          580  +  return 1;
          581  +}
   511    582   
   512    583   /*
   513    584   ** Initialize a preallocated WhereClause structure.
   514    585   */
   515    586   static void whereClauseInit(
   516    587     WhereClause *pWC,        /* The WhereClause to be initialized */
   517    588     WhereInfo *pWInfo        /* The WHERE processing context */
................................................................................
  1053   1124     op = pRight->op;
  1054   1125     if( op==TK_REGISTER ){
  1055   1126       op = pRight->op2;
  1056   1127     }
  1057   1128     if( op==TK_VARIABLE ){
  1058   1129       Vdbe *pReprepare = pParse->pReprepare;
  1059   1130       int iCol = pRight->iColumn;
  1060         -    pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
         1131  +    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
  1061   1132       if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
  1062   1133         z = (char *)sqlite3_value_text(pVal);
  1063   1134       }
  1064   1135       sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
  1065   1136       assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
  1066   1137     }else if( op==TK_STRING ){
  1067   1138       z = pRight->u.zToken;
................................................................................
  2160   2231     sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
  2161   2232                       (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
  2162   2233     VdbeComment((v, "for %s", pTable->zName));
  2163   2234   
  2164   2235     /* Fill the automatic index with content */
  2165   2236     addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
  2166   2237     regRecord = sqlite3GetTempReg(pParse);
  2167         -  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1);
         2238  +  sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1, 0);
  2168   2239     sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
  2169   2240     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  2170   2241     sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
  2171   2242     sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
  2172   2243     sqlite3VdbeJumpHere(v, addrTop);
  2173   2244     sqlite3ReleaseTempReg(pParse, regRecord);
  2174   2245     
................................................................................
  2517   2588     sqlite3_value **pp
  2518   2589   ){
  2519   2590     if( pExpr->op==TK_VARIABLE
  2520   2591      || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
  2521   2592     ){
  2522   2593       int iVar = pExpr->iColumn;
  2523   2594       sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
  2524         -    *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
         2595  +    *pp = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, aff);
  2525   2596       return SQLITE_OK;
  2526   2597     }
  2527   2598     return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
  2528   2599   }
  2529   2600   #endif
  2530   2601   
  2531   2602   /*
................................................................................
  3175   3246     int omitTable;       /* True if we use the index only */
  3176   3247     int bRev;            /* True if we need to scan in reverse order */
  3177   3248     WhereLevel *pLevel;  /* The where level to be coded */
  3178   3249     WhereLoop *pLoop;    /* The WhereLoop object being coded */
  3179   3250     WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
  3180   3251     WhereTerm *pTerm;               /* A WHERE clause term */
  3181   3252     Parse *pParse;                  /* Parsing context */
         3253  +  sqlite3 *db;                    /* Database connection */
  3182   3254     Vdbe *v;                        /* The prepared stmt under constructions */
  3183   3255     struct SrcList_item *pTabItem;  /* FROM clause term being coded */
  3184   3256     int addrBrk;                    /* Jump here to break out of the loop */
  3185   3257     int addrCont;                   /* Jump here to continue with next cycle */
  3186   3258     int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
  3187   3259     int iReleaseReg = 0;      /* Temp register to free before returning */
  3188   3260     Bitmask newNotReady;      /* Return value */
  3189   3261   
  3190   3262     pParse = pWInfo->pParse;
  3191   3263     v = pParse->pVdbe;
  3192   3264     pWC = &pWInfo->sWC;
         3265  +  db = pParse->db;
  3193   3266     pLevel = &pWInfo->a[iLevel];
  3194   3267     pLoop = pLevel->pWLoop;
  3195   3268     pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
  3196   3269     iCur = pTabItem->iCursor;
  3197   3270     bRev = (pWInfo->revMask>>iLevel)&1;
  3198   3271     omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
  3199   3272              && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
................................................................................
  3476   3549       }
  3477   3550   
  3478   3551       /* Generate code to evaluate all constraint terms using == or IN
  3479   3552       ** and store the values of those terms in an array of registers
  3480   3553       ** starting at regBase.
  3481   3554       */
  3482   3555       regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
  3483         -    zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
         3556  +    zEndAff = sqlite3DbStrDup(db, zStartAff);
  3484   3557       addrNxt = pLevel->addrNxt;
  3485   3558   
  3486   3559       /* If we are doing a reverse order scan on an ascending index, or
  3487   3560       ** a forward order scan on a descending index, interchange the 
  3488   3561       ** start and end terms (pRangeStart and pRangeEnd).
  3489   3562       */
  3490   3563       if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
................................................................................
  3561   3634             zEndAff[nEq] = SQLITE_AFF_NONE;
  3562   3635           }
  3563   3636         }  
  3564   3637         codeApplyAffinity(pParse, regBase, nEq+1, zEndAff);
  3565   3638         nConstraint++;
  3566   3639         testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3567   3640       }
  3568         -    sqlite3DbFree(pParse->db, zStartAff);
  3569         -    sqlite3DbFree(pParse->db, zEndAff);
         3641  +    sqlite3DbFree(db, zStartAff);
         3642  +    sqlite3DbFree(db, zEndAff);
  3570   3643   
  3571   3644       /* Top of the loop body */
  3572   3645       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3573   3646   
  3574   3647       /* Check if the index cursor is past the end of the range. */
  3575   3648       op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
  3576   3649       testcase( op==OP_Noop );
................................................................................
  3689   3762       ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
  3690   3763       ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
  3691   3764       */
  3692   3765       if( pWInfo->nLevel>1 ){
  3693   3766         int nNotReady;                 /* The number of notReady tables */
  3694   3767         struct SrcList_item *origSrc;     /* Original list of tables */
  3695   3768         nNotReady = pWInfo->nLevel - iLevel - 1;
  3696         -      pOrTab = sqlite3StackAllocRaw(pParse->db,
         3769  +      pOrTab = sqlite3StackAllocRaw(db,
  3697   3770                               sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
  3698   3771         if( pOrTab==0 ) return notReady;
  3699   3772         pOrTab->nAlloc = (u8)(nNotReady + 1);
  3700   3773         pOrTab->nSrc = pOrTab->nAlloc;
  3701   3774         memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
  3702   3775         origSrc = pWInfo->pTabList->a;
  3703   3776         for(k=1; k<=nNotReady; k++){
................................................................................
  3739   3812       ** is not contained in the ON clause of a LEFT JOIN.
  3740   3813       ** See ticket http://www.sqlite.org/src/info/f2369304e4
  3741   3814       */
  3742   3815       if( pWC->nTerm>1 ){
  3743   3816         int iTerm;
  3744   3817         for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
  3745   3818           Expr *pExpr = pWC->a[iTerm].pExpr;
         3819  +        if( &pWC->a[iTerm] == pTerm ) continue;
  3746   3820           if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
  3747         -        if( pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_ORINFO) ) continue;
         3821  +        if( pWC->a[iTerm].wtFlags & (TERM_ORINFO) ) continue;
  3748   3822           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
  3749         -        pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
  3750         -        pAndExpr = sqlite3ExprAnd(pParse->db, pAndExpr, pExpr);
         3823  +        pExpr = sqlite3ExprDup(db, pExpr, 0);
         3824  +        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
  3751   3825         }
  3752   3826         if( pAndExpr ){
  3753   3827           pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
  3754   3828         }
  3755   3829       }
  3756   3830   
  3757   3831       for(ii=0; ii<pOrWc->nTerm; ii++){
................................................................................
  3763   3837             pAndExpr->pLeft = pOrExpr;
  3764   3838             pOrExpr = pAndExpr;
  3765   3839           }
  3766   3840           /* Loop through table entries that match term pOrTerm. */
  3767   3841           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  3768   3842                           WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
  3769   3843                           WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
  3770         -        assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
         3844  +        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
  3771   3845           if( pSubWInfo ){
  3772   3846             WhereLoop *pSubLoop;
  3773   3847             explainOneScan(
  3774   3848                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  3775   3849             );
  3776   3850             if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  3777   3851               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
................................................................................
  3818   3892           }
  3819   3893         }
  3820   3894       }
  3821   3895       pLevel->u.pCovidx = pCov;
  3822   3896       if( pCov ) pLevel->iIdxCur = iCovCur;
  3823   3897       if( pAndExpr ){
  3824   3898         pAndExpr->pLeft = 0;
  3825         -      sqlite3ExprDelete(pParse->db, pAndExpr);
         3899  +      sqlite3ExprDelete(db, pAndExpr);
  3826   3900       }
  3827   3901       sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
  3828   3902       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
  3829   3903       sqlite3VdbeResolveLabel(v, iLoopBody);
  3830   3904   
  3831         -    if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
         3905  +    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
  3832   3906       if( !untestedTerms ) disableTerm(pLevel, pTerm);
  3833   3907     }else
  3834   3908   #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  3835   3909   
  3836   3910     {
  3837   3911       /* Case 6:  There is no usable index.  We must do a complete
  3838   3912       **          scan of the entire table.
................................................................................
  3879   3953     **
  3880   3954     ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
  3881   3955     ** and we are coding the t1 loop and the t2 loop has not yet coded,
  3882   3956     ** then we cannot use the "t1.a=t2.b" constraint, but we can code
  3883   3957     ** the implied "t1.a=123" constraint.
  3884   3958     */
  3885   3959     for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  3886         -    Expr *pE;
         3960  +    Expr *pE, *pEAlt;
  3887   3961       WhereTerm *pAlt;
  3888         -    Expr sEq;
  3889   3962       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  3890   3963       if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
  3891   3964       if( pTerm->leftCursor!=iCur ) continue;
  3892   3965       if( pLevel->iLeftJoin ) continue;
  3893   3966       pE = pTerm->pExpr;
  3894   3967       assert( !ExprHasProperty(pE, EP_FromJoin) );
  3895   3968       assert( (pTerm->prereqRight & newNotReady)!=0 );
  3896   3969       pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
  3897   3970       if( pAlt==0 ) continue;
  3898   3971       if( pAlt->wtFlags & (TERM_CODED) ) continue;
  3899   3972       testcase( pAlt->eOperator & WO_EQ );
  3900   3973       testcase( pAlt->eOperator & WO_IN );
  3901   3974       VdbeNoopComment((v, "begin transitive constraint"));
  3902         -    sEq = *pAlt->pExpr;
  3903         -    sEq.pLeft = pE->pLeft;
  3904         -    sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL);
         3975  +    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
         3976  +    if( pEAlt ){
         3977  +      *pEAlt = *pAlt->pExpr;
         3978  +      pEAlt->pLeft = pE->pLeft;
         3979  +      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
         3980  +      sqlite3StackFree(db, pEAlt);
         3981  +    }
  3905   3982     }
  3906   3983   
  3907   3984     /* For a LEFT OUTER JOIN, generate code that will record the fact that
  3908   3985     ** at least one row of the right table has matched the left table.  
  3909   3986     */
  3910   3987     if( pLevel->iLeftJoin ){
  3911   3988       pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
................................................................................
  4068   4145   **
  4069   4146   ** An existing WhereLoop entry might be overwritten if the new template
  4070   4147   ** is better and has fewer dependencies.  Or the template will be ignored
  4071   4148   ** and no insert will occur if an existing WhereLoop is faster and has
  4072   4149   ** fewer dependencies than the template.  Otherwise a new WhereLoop is
  4073   4150   ** added based on the template.
  4074   4151   **
  4075         -** If pBuilder->pBest is not NULL then we only care about the very
  4076         -** best template and that template should be stored in pBuilder->pBest.
  4077         -** If pBuilder->pBest is NULL then a list of the best templates are stored
  4078         -** in pBuilder->pWInfo->pLoops.
         4152  +** If pBuilder->pOrSet is not NULL then we only care about only the
         4153  +** prerequisites and rRun and nOut costs of the N best loops.  That
         4154  +** information is gathered in the pBuilder->pOrSet object.  This special
         4155  +** processing mode is used only for OR clause processing.
  4079   4156   **
  4080         -** When accumulating multiple loops (when pBuilder->pBest is NULL) we
         4157  +** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
  4081   4158   ** still might overwrite similar loops with the new template if the
  4082   4159   ** template is better.  Loops may be overwritten if the following 
  4083   4160   ** conditions are met:
  4084   4161   **
  4085   4162   **    (1)  They have the same iTab.
  4086   4163   **    (2)  They have the same iSortIdx.
  4087   4164   **    (3)  The template has same or fewer dependencies than the current loop
................................................................................
  4090   4167   **         dependencies          
  4091   4168   */
  4092   4169   static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
  4093   4170     WhereLoop **ppPrev, *p, *pNext = 0;
  4094   4171     WhereInfo *pWInfo = pBuilder->pWInfo;
  4095   4172     sqlite3 *db = pWInfo->pParse->db;
  4096   4173   
  4097         -  /* If pBuilder->pBest is defined, then only keep track of the single
  4098         -  ** best WhereLoop.  pBuilder->pBest->maskSelf==0 indicates that no
  4099         -  ** prior WhereLoops have been evaluated and that the current pTemplate
  4100         -  ** is therefore the first and hence the best and should be retained.
         4174  +  /* If pBuilder->pOrSet is defined, then only keep track of the costs
         4175  +  ** and prereqs.
  4101   4176     */
  4102         -  if( (p = pBuilder->pBest)!=0 ){
  4103         -    if( p->maskSelf!=0 ){
  4104         -      WhereCost rCost = whereCostAdd(p->rRun,p->rSetup);
  4105         -      WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup);
  4106         -      if( rCost < rTemplate ){
  4107         -        testcase( rCost==rTemplate-1 );
  4108         -        goto whereLoopInsert_noop;
  4109         -      }
  4110         -      if( rCost==rTemplate && (p->prereq & pTemplate->prereq)==p->prereq ){
  4111         -        goto whereLoopInsert_noop;
  4112         -      }
  4113         -    }
         4177  +  if( pBuilder->pOrSet!=0 ){
         4178  +#if WHERETRACE_ENABLED
         4179  +    u16 n = pBuilder->pOrSet->n;
         4180  +    int x =
         4181  +#endif
         4182  +    whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
         4183  +                                    pTemplate->nOut);
  4114   4184   #if WHERETRACE_ENABLED
  4115   4185       if( sqlite3WhereTrace & 0x8 ){
  4116         -      sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: ");
         4186  +      sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
  4117   4187         whereLoopPrint(pTemplate, pWInfo->pTabList);
  4118   4188       }
  4119   4189   #endif
  4120         -    whereLoopXfer(db, p, pTemplate);
  4121   4190       return SQLITE_OK;
  4122   4191     }
  4123   4192   
  4124   4193     /* Search for an existing WhereLoop to overwrite, or which takes
  4125   4194     ** priority over pTemplate.
  4126   4195     */
  4127   4196     for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
................................................................................
  4207   4276     }
  4208   4277     return SQLITE_OK;
  4209   4278   
  4210   4279     /* Jump here if the insert is a no-op */
  4211   4280   whereLoopInsert_noop:
  4212   4281   #if WHERETRACE_ENABLED
  4213   4282     if( sqlite3WhereTrace & 0x8 ){
  4214         -    sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: ");
         4283  +    sqlite3DebugPrintf("ins-noop: ");
  4215   4284       whereLoopPrint(pTemplate, pWInfo->pTabList);
  4216   4285     }
  4217   4286   #endif
  4218   4287     return SQLITE_OK;  
  4219   4288   }
  4220   4289   
  4221   4290   /*
................................................................................
  4358   4427           testcase( pTerm->eOperator & WO_EQ );
  4359   4428           testcase( pTerm->eOperator & WO_ISNULL );
  4360   4429           rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
  4361   4430         }else if( (pTerm->eOperator & WO_IN)
  4362   4431                &&  !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)  ){
  4363   4432           rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
  4364   4433         }
  4365         -      if( rc==SQLITE_OK ) pNew->nOut = whereCost(nOut);
         4434  +      assert( nOut==0 || rc==SQLITE_OK );
         4435  +      if( nOut ) pNew->nOut = whereCost(nOut);
  4366   4436       }
  4367   4437   #endif
  4368   4438       if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
  4369   4439         /* Each row involves a step of the index, then a binary search of
  4370   4440         ** the main table */
  4371   4441         pNew->rRun =  whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10);
  4372   4442       }
................................................................................
  4430   4500       testcase( x==BMS-1 );
  4431   4501       testcase( x==BMS-2 );
  4432   4502       if( x<BMS-1 ) m |= MASKBIT(x);
  4433   4503     }
  4434   4504     return m;
  4435   4505   }
  4436   4506   
         4507  +/* Check to see if a partial index with pPartIndexWhere can be used
         4508  +** in the current query.  Return true if it can be and false if not.
         4509  +*/
         4510  +static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
         4511  +  int i;
         4512  +  WhereTerm *pTerm;
         4513  +  for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
         4514  +    if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1;
         4515  +  }
         4516  +  return 0;
         4517  +}
  4437   4518   
  4438   4519   /*
  4439   4520   ** Add all WhereLoop objects for a single table of the join where the table
  4440   4521   ** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
  4441   4522   ** a b-tree table, not a virtual table.
  4442   4523   */
  4443   4524   static int whereLoopAddBtree(
................................................................................
  4453   4534     struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
  4454   4535     WhereLoop *pNew;            /* Template WhereLoop object */
  4455   4536     int rc = SQLITE_OK;         /* Return code */
  4456   4537     int iSortIdx = 1;           /* Index number */
  4457   4538     int b;                      /* A boolean value */
  4458   4539     WhereCost rSize;            /* number of rows in the table */
  4459   4540     WhereCost rLogSize;         /* Logarithm of the number of rows in the table */
         4541  +  WhereClause *pWC;           /* The parsed WHERE clause */
  4460   4542     
  4461   4543     pNew = pBuilder->pNew;
  4462   4544     pWInfo = pBuilder->pWInfo;
  4463   4545     pTabList = pWInfo->pTabList;
  4464   4546     pSrc = pTabList->a + pNew->iTab;
         4547  +  pWC = pBuilder->pWC;
  4465   4548     assert( !IsVirtual(pSrc->pTab) );
  4466   4549   
  4467   4550     if( pSrc->pIndex ){
  4468   4551       /* An INDEXED BY clause specifies a particular index to use */
  4469   4552       pProbe = pSrc->pIndex;
  4470   4553     }else{
  4471   4554       /* There is no INDEXED BY clause.  Create a fake Index object in local
................................................................................
  4489   4572       }
  4490   4573       pProbe = &sPk;
  4491   4574     }
  4492   4575     rSize = whereCost(pSrc->pTab->nRowEst);
  4493   4576     rLogSize = estLog(rSize);
  4494   4577   
  4495   4578     /* Automatic indexes */
  4496         -  if( !pBuilder->pBest
         4579  +  if( !pBuilder->pOrSet
  4497   4580      && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
  4498   4581      && pSrc->pIndex==0
  4499   4582      && !pSrc->viaCoroutine
  4500   4583      && !pSrc->notIndexed
  4501   4584      && !pSrc->isCorrelated
  4502   4585     ){
  4503   4586       /* Generate auto-index WhereLoops */
  4504         -    WhereClause *pWC = pBuilder->pWC;
  4505   4587       WhereTerm *pTerm;
  4506   4588       WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
  4507   4589       for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
  4508   4590         if( pTerm->prereqRight & pNew->maskSelf ) continue;
  4509   4591         if( termCanDriveIndex(pTerm, pSrc, 0) ){
  4510   4592           pNew->u.btree.nEq = 1;
  4511   4593           pNew->u.btree.pIndex = 0;
................................................................................
  4527   4609         }
  4528   4610       }
  4529   4611     }
  4530   4612   
  4531   4613     /* Loop over all indices
  4532   4614     */
  4533   4615     for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
         4616  +    if( pProbe->pPartIdxWhere!=0
         4617  +     && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
         4618  +      continue;  /* Partial index inappropriate for this query */
         4619  +    }
  4534   4620       pNew->u.btree.nEq = 0;
  4535   4621       pNew->nLTerm = 0;
  4536   4622       pNew->iSortIdx = 0;
  4537   4623       pNew->rSetup = 0;
  4538   4624       pNew->prereq = mExtra;
  4539   4625       pNew->nOut = rSize;
  4540   4626       pNew->u.btree.pIndex = pProbe;
................................................................................
  4775   4861     WhereClause *pWC;
  4776   4862     WhereLoop *pNew;
  4777   4863     WhereTerm *pTerm, *pWCEnd;
  4778   4864     int rc = SQLITE_OK;
  4779   4865     int iCur;
  4780   4866     WhereClause tempWC;
  4781   4867     WhereLoopBuilder sSubBuild;
  4782         -  WhereLoop sBest;
         4868  +  WhereOrSet sSum, sCur, sPrev;
  4783   4869     struct SrcList_item *pItem;
  4784   4870     
  4785   4871     pWC = pBuilder->pWC;
  4786   4872     if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
  4787   4873     pWCEnd = pWC->a + pWC->nTerm;
  4788   4874     pNew = pBuilder->pNew;
  4789   4875   
................................................................................
  4790   4876     for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
  4791   4877       if( (pTerm->eOperator & WO_OR)!=0
  4792   4878        && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 
  4793   4879       ){
  4794   4880         WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
  4795   4881         WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
  4796   4882         WhereTerm *pOrTerm;
  4797         -      WhereCost rTotal = 0;
  4798         -      WhereCost nRow = 0;
  4799         -      Bitmask prereq = mExtra;
         4883  +      int once = 1;
         4884  +      int i, j;
  4800   4885       
  4801         -      whereLoopInit(&sBest);
  4802   4886         pItem = pWInfo->pTabList->a + pNew->iTab;
  4803   4887         iCur = pItem->iCursor;
  4804   4888         sSubBuild = *pBuilder;
  4805   4889         sSubBuild.pOrderBy = 0;
  4806         -      sSubBuild.pBest = &sBest;
         4890  +      sSubBuild.pOrSet = &sCur;
  4807   4891   
  4808   4892         for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
  4809   4893           if( (pOrTerm->eOperator & WO_AND)!=0 ){
  4810   4894             sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
  4811   4895           }else if( pOrTerm->leftCursor==iCur ){
  4812   4896             tempWC.pWInfo = pWC->pWInfo;
  4813   4897             tempWC.pOuter = pWC;
................................................................................
  4814   4898             tempWC.op = TK_AND;
  4815   4899             tempWC.nTerm = 1;
  4816   4900             tempWC.a = pOrTerm;
  4817   4901             sSubBuild.pWC = &tempWC;
  4818   4902           }else{
  4819   4903             continue;
  4820   4904           }
  4821         -        sBest.maskSelf = 0;
  4822         -        sBest.rSetup = 0;
  4823         -        sBest.rRun = 0;
         4905  +        sCur.n = 0;
  4824   4906   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4825   4907           if( IsVirtual(pItem->pTab) ){
  4826   4908             rc = whereLoopAddVirtual(&sSubBuild);
         4909  +          for(i=0; i<sCur.n; i++) sCur.a[i].prereq |= mExtra;
  4827   4910           }else
  4828   4911   #endif
  4829   4912           {
  4830   4913             rc = whereLoopAddBtree(&sSubBuild, mExtra);
  4831   4914           }
  4832         -        /* sBest.maskSelf is always zero if an error occurs */
  4833         -        assert( rc==SQLITE_OK || sBest.maskSelf==0 );
  4834         -        if( sBest.maskSelf==0 ) break;
  4835         -        assert( sBest.rSetup==0 );
  4836         -        rTotal = whereCostAdd(rTotal, sBest.rRun);
  4837         -        nRow = whereCostAdd(nRow, sBest.nOut);
  4838         -        prereq |= sBest.prereq;
         4915  +        assert( rc==SQLITE_OK || sCur.n==0 );
         4916  +        if( sCur.n==0 ){
         4917  +          sSum.n = 0;
         4918  +          break;
         4919  +        }else if( once ){
         4920  +          whereOrMove(&sSum, &sCur);
         4921  +          once = 0;
         4922  +        }else{
         4923  +          whereOrMove(&sPrev, &sSum);
         4924  +          sSum.n = 0;
         4925  +          for(i=0; i<sPrev.n; i++){
         4926  +            for(j=0; j<sCur.n; j++){
         4927  +              whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
         4928  +                            whereCostAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
         4929  +                            whereCostAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
         4930  +            }
         4931  +          }
         4932  +        }
  4839   4933         }
  4840         -      assert( pNew->nLSlot>=1 );
  4841         -      if( sBest.maskSelf ){
  4842         -        pNew->nLTerm = 1;
  4843         -        pNew->aLTerm[0] = pTerm;
  4844         -        pNew->wsFlags = WHERE_MULTI_OR;
  4845         -        pNew->rSetup = 0;
         4934  +      pNew->nLTerm = 1;
         4935  +      pNew->aLTerm[0] = pTerm;
         4936  +      pNew->wsFlags = WHERE_MULTI_OR;
         4937  +      pNew->rSetup = 0;
         4938  +      pNew->iSortIdx = 0;
         4939  +      memset(&pNew->u, 0, sizeof(pNew->u));
         4940  +      for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
  4846   4941           /* TUNING: Multiple by 3.5 for the secondary table lookup */
  4847         -        pNew->rRun = rTotal + 18; assert( 18==whereCost(7)-whereCost(2) );
  4848         -        pNew->nOut = nRow;
  4849         -        pNew->prereq = prereq;
  4850         -        memset(&pNew->u, 0, sizeof(pNew->u));
         4942  +        pNew->rRun = sSum.a[i].rRun + 18;
         4943  +        pNew->nOut = sSum.a[i].nOut;
         4944  +        pNew->prereq = sSum.a[i].prereq;
  4851   4945           rc = whereLoopInsert(pBuilder, pNew);
  4852   4946         }
  4853         -      whereLoopClear(pWInfo->pParse->db, &sBest);
  4854   4947       }
  4855   4948     }
  4856   4949     return rc;
  4857   4950   }
  4858   4951   
  4859   4952   /*
  4860   4953   ** Add all WhereLoop objects for all tables 
................................................................................
  5460   5553       pLoop->aLTerm[0] = pTerm;
  5461   5554       pLoop->nLTerm = 1;
  5462   5555       pLoop->u.btree.nEq = 1;
  5463   5556       /* TUNING: Cost of a rowid lookup is 10 */
  5464   5557       pLoop->rRun = 33;  /* 33==whereCost(10) */
  5465   5558     }else{
  5466   5559       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  5467         -      if( pIdx->onError==OE_None ) continue;
         5560  +      if( pIdx->onError==OE_None || pIdx->pPartIdxWhere!=0 ) continue;
  5468   5561         for(j=0; j<pIdx->nColumn; j++){
  5469   5562           pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
  5470   5563           if( pTerm==0 ) break;
  5471   5564           whereLoopResize(pWInfo->pParse->db, pLoop, j);
  5472   5565           pLoop->aLTerm[j] = pTerm;
  5473   5566         }
  5474   5567         if( j!=pIdx->nColumn ) continue;
................................................................................
  5653   5746     pWInfo->pResultSet = pResultSet;
  5654   5747     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
  5655   5748     pWInfo->wctrlFlags = wctrlFlags;
  5656   5749     pWInfo->savedNQueryLoop = pParse->nQueryLoop;
  5657   5750     pMaskSet = &pWInfo->sMaskSet;
  5658   5751     sWLB.pWInfo = pWInfo;
  5659   5752     sWLB.pWC = &pWInfo->sWC;
  5660         -  sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList];
         5753  +  sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
         5754  +  assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
  5661   5755     whereLoopInit(sWLB.pNew);
  5662   5756   #ifdef SQLITE_DEBUG
  5663   5757     sWLB.pNew->cId = '*';
  5664   5758   #endif
  5665   5759   
  5666   5760     /* Split the WHERE clause into separate subexpressions where each
  5667   5761     ** subexpression is separated by an AND operator.

Changes to test/analyze3.test.

    39     39   #
    40     40   # analyze3-4.*: Test that SQL or authorization callback errors occuring
    41     41   #               within sqlite3Reprepare() are handled correctly.
    42     42   #
    43     43   # analyze3-5.*: Check that the query plans of applicable statements are
    44     44   #               invalidated if the values of SQL parameter are modified
    45     45   #               using the clear_bindings() or transfer_bindings() APIs.
           46  +# 
           47  +# analyze3-6.*: Test that the problem fixed by commit [127a5b776d] is fixed.
    46     48   #
    47     49   
    48     50   proc getvar {varname} { uplevel #0 set $varname }
    49     51   db function var getvar
    50     52   
    51     53   proc eqp {sql {db db}} {
    52     54     uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
................................................................................
   607    609     }
   608    610     concat [sqlite3_reset $S1] $R
   609    611   } {SQLITE_OK aaa abb acc}
   610    612   do_test analyze3-5.1.3 {
   611    613     sqlite3_finalize $S2
   612    614     sqlite3_finalize $S1
   613    615   } {SQLITE_OK}
          616  +
          617  +#-------------------------------------------------------------------------
          618  +
          619  +do_test analyze3-6.1 {
          620  +  execsql { DROP TABLE IF EXISTS t1 }
          621  +  execsql BEGIN
          622  +  execsql { CREATE TABLE t1(a, b, c) }
          623  +  for {set i 0} {$i < 1000} {incr i} {
          624  +    execsql "INSERT INTO t1 VALUES([expr $i/100], 'x', [expr $i/10])"
          625  +  }
          626  +  execsql {
          627  +    CREATE INDEX i1 ON t1(a, b);
          628  +    CREATE INDEX i2 ON t1(c);
          629  +  }
          630  +  execsql COMMIT
          631  +  execsql ANALYZE
          632  +} {}
          633  +
          634  +do_eqp_test analyze3-6-3 {
          635  +  SELECT * FROM t1 WHERE a = 5 AND c = 13;
          636  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}}
          637  +
          638  +do_eqp_test analyze3-6-2 {
          639  +  SELECT * FROM t1 WHERE a = 5 AND b > 'w' AND c = 13;
          640  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (c=?)}}
   614    641   
   615    642   finish_test

Changes to test/check.test.

   447    447   } {}
   448    448   
   449    449   do_test 7.8 {
   450    450     db2 func myfunc myfunc
   451    451     catchsql { INSERT INTO t6 VALUES(12) } db2
   452    452   } {1 {constraint failed}}
   453    453   
          454  +# 2013-08-02:  Silently ignore database name qualifiers in CHECK constraints.
          455  +#
          456  +do_execsql_test 8.1 {
          457  +  CREATE TABLE t810(a, CHECK( main.t810.a>0 ));
          458  +  CREATE TABLE t811(b, CHECK( xyzzy.t811.b BETWEEN 5 AND 10 ));
          459  +} {}
   454    460   
   455    461   finish_test

Added test/corruptG.test.

            1  +# 2013-08-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  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +set testprefix corruptG
           16  +
           17  +# Do not use a codec for tests in this file, as the database file is
           18  +# manipulated directly using tcl scripts (using the [hexio_write] command).
           19  +#
           20  +do_not_use_codec
           21  +
           22  +# Create a simple database with a single entry.  Then corrupt the
           23  +# header-size varint on the index payload so that it maps into a
           24  +# negative number.  Try to use the database.
           25  +#
           26  +
           27  +do_execsql_test 1.1 {
           28  +  PRAGMA page_size=512;
           29  +  CREATE TABLE t1(a,b,c);
           30  +  INSERT INTO t1(rowid,a,b,c) VALUES(52,'abc','xyz','123');
           31  +  CREATE INDEX t1abc ON t1(a,b,c);
           32  +}
           33  +
           34  +# Corrupt the file
           35  +db close
           36  +hexio_write test.db [expr {3*512 - 15}] 888080807f
           37  +sqlite3 db test.db
           38  +
           39  +# Try to use the file.
           40  +do_test 1.2 {
           41  +  catchsql {
           42  +    SELECT c FROM t1 WHERE a>'abc';
           43  +  }
           44  +} {0 {}}
           45  +do_test 1.3 {
           46  +  catchsql {
           47  +     PRAGMA integrity_check
           48  +  }
           49  +} {0 ok}
           50  +do_test 1.4 {
           51  +  catchsql {
           52  +    SELECT c FROM t1 ORDER BY a;
           53  +  }
           54  +} {1 {database disk image is malformed}}
           55  +
           56  +# Corrupt the same file in a slightly different way.  Make the record header
           57  +# sane, but corrupt one of the serial_type value to indicate a huge payload
           58  +# such that the payload begins in allocated space but overflows the buffer.
           59  +#
           60  +db close
           61  +hexio_write test.db [expr {3*512-15}] 0513ff7f01
           62  +sqlite3 db test.db
           63  +
           64  +do_test 2.1 {
           65  +  catchsql {
           66  +    SELECT rowid FROM t1 WHERE a='abc' and b='xyz123456789XYZ';
           67  +  }
           68  +  # The following test result is brittle.  The point above is to try to
           69  +  # force a buffer overread by a corrupt database file.  If we get an
           70  +  # incorrect answer from a corrupt database file, that is OK.  If the
           71  +  # result below changes, that just means that "undefined behavior" has
           72  +  # changed.
           73  +} {0 52}
           74  +
           75  +finish_test

Changes to test/func.test.

    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this file is testing built-in functions.
    13     13   #
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
           17  +set testprefix func
    17     18   
    18     19   # Create a table to work with.
    19     20   #
    20     21   do_test func-0.0 {
    21     22     execsql {CREATE TABLE tbl1(t1 text)}
    22     23     foreach word {this program is free software} {
    23     24       execsql "INSERT INTO tbl1 VALUES('$word')"
................................................................................
   677    678     sqlite3_bind_text $STMT 1 hello\000 -1
   678    679     set res [list]
   679    680     while { "SQLITE_ROW"==[sqlite3_step $STMT] } {
   680    681       lappend res [sqlite3_column_text $STMT 0]
   681    682     }
   682    683     lappend res [sqlite3_finalize $STMT]
   683    684   } {{0 0} {1 0} SQLITE_OK}
          685  +
          686  +# Test that auxiliary data is discarded when a statement is reset.
          687  +do_execsql_test 13.8.1 {
          688  +  SELECT test_auxdata('constant') FROM t4;
          689  +} {0 1}
          690  +do_execsql_test 13.8.2 {
          691  +  SELECT test_auxdata('constant') FROM t4;
          692  +} {0 1}
          693  +db cache flush
          694  +do_execsql_test 13.8.3 {
          695  +  SELECT test_auxdata('constant') FROM t4;
          696  +} {0 1}
          697  +set V "one"
          698  +do_execsql_test 13.8.4 {
          699  +  SELECT test_auxdata($V), $V FROM t4;
          700  +} {0 one 1 one}
          701  +set V "two"
          702  +do_execsql_test 13.8.5 {
          703  +  SELECT test_auxdata($V), $V FROM t4;
          704  +} {0 two 1 two}
          705  +db cache flush
          706  +set V "three"
          707  +do_execsql_test 13.8.6 {
          708  +  SELECT test_auxdata($V), $V FROM t4;
          709  +} {0 three 1 three}
          710  +
   684    711   
   685    712   # Make sure that a function with a very long name is rejected
   686    713   do_test func-14.1 {
   687    714     catch {
   688    715       db function [string repeat X 254] {return "hello"}
   689    716     } 
   690    717   } {0}

Changes to test/index.test.

   711    711     }
   712    712   } {}
   713    713   do_test index-20.2 {
   714    714     execsql {
   715    715       DROP INDEX "t6i1";
   716    716     }
   717    717   } {}
          718  +
          719  +# Try to create a TEMP index on a non-TEMP table. */
          720  +#
          721  +do_test index-21.1 {
          722  +  catchsql {
          723  +     CREATE INDEX temp.i21 ON t6(c);
          724  +  }
          725  +} {1 {cannot create a TEMP index on non-TEMP table "t6"}}
          726  +do_test index-21.2 {
          727  +  catchsql {
          728  +     CREATE TEMP TABLE t6(x);
          729  +     INSERT INTO temp.t6 values(1),(5),(9);
          730  +     CREATE INDEX temp.i21 ON t6(x);
          731  +     SELECT x FROM t6 ORDER BY x DESC;
          732  +  }
          733  +} {0 {9 5 1}}
          734  +
   718    735      
   719    736   
   720    737   finish_test

Added test/index6.test.

            1  +# 2013-07-31
            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  +# Test cases for partial indices
           13  +#
           14  +
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +
           19  +load_static_extension db wholenumber;
           20  +do_test index6-1.1 {
           21  +  # Able to parse and manage partial indices
           22  +  execsql {
           23  +    CREATE TABLE t1(a,b,c);
           24  +    CREATE INDEX t1a ON t1(a) WHERE a IS NOT NULL;
           25  +    CREATE INDEX t1b ON t1(b) WHERE b>10;
           26  +    CREATE VIRTUAL TABLE nums USING wholenumber;
           27  +    INSERT INTO t1(a,b,c)
           28  +       SELECT CASE WHEN value%3!=0 THEN value END, value, value
           29  +         FROM nums WHERE value<=20;
           30  +    SELECT count(a), count(b) FROM t1;
           31  +    PRAGMA integrity_check;
           32  +  }
           33  +} {14 20 ok}
           34  +
           35  +# Error conditions during parsing...
           36  +#
           37  +do_test index6-1.2 {
           38  +  catchsql {
           39  +    CREATE INDEX bad1 ON t1(a,b) WHERE x IS NOT NULL;
           40  +  }
           41  +} {1 {no such column: x}}
           42  +do_test index6-1.3 {
           43  +  catchsql {
           44  +    CREATE INDEX bad1 ON t1(a,b) WHERE EXISTS(SELECT * FROM t1);
           45  +  }
           46  +} {1 {subqueries prohibited in partial index WHERE clauses}}
           47  +do_test index6-1.4 {
           48  +  catchsql {
           49  +    CREATE INDEX bad1 ON t1(a,b) WHERE a!=?1;
           50  +  }
           51  +} {1 {parameters prohibited in partial index WHERE clauses}}
           52  +do_test index6-1.5 {
           53  +  catchsql {
           54  +    CREATE INDEX bad1 ON t1(a,b) WHERE a!=random();
           55  +  }
           56  +} {1 {functions prohibited in partial index WHERE clauses}}
           57  +do_test index6-1.6 {
           58  +  catchsql {
           59  +    CREATE INDEX bad1 ON t1(a,b) WHERE a NOT LIKE 'abc%';
           60  +  }
           61  +} {1 {functions prohibited in partial index WHERE clauses}}
           62  +
           63  +do_test index6-1.10 {
           64  +  execsql {
           65  +    ANALYZE;
           66  +    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
           67  +    PRAGMA integrity_check;
           68  +  }
           69  +} {{} 20 t1a {14 1} t1b {10 1} ok}
           70  +
           71  +# STAT1 shows the partial indices have a reduced number of
           72  +# rows.
           73  +#
           74  +do_test index6-1.11 {
           75  +  execsql {
           76  +    UPDATE t1 SET a=b;
           77  +    ANALYZE;
           78  +    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
           79  +    PRAGMA integrity_check;
           80  +  }
           81  +} {{} 20 t1a {20 1} t1b {10 1} ok}
           82  +
           83  +do_test index6-1.11 {
           84  +  execsql {
           85  +    UPDATE t1 SET a=NULL WHERE b%3!=0;
           86  +    UPDATE t1 SET b=b+100;
           87  +    ANALYZE;
           88  +    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
           89  +    PRAGMA integrity_check;
           90  +  }
           91  +} {{} 20 t1a {6 1} t1b {20 1} ok}
           92  +
           93  +do_test index6-1.12 {
           94  +  execsql {
           95  +    UPDATE t1 SET a=CASE WHEN b%3!=0 THEN b END;
           96  +    UPDATE t1 SET b=b-100;
           97  +    ANALYZE;
           98  +    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
           99  +    PRAGMA integrity_check;
          100  +  }
          101  +} {{} 20 t1a {13 1} t1b {10 1} ok}
          102  +
          103  +do_test index6-1.13 {
          104  +  execsql {
          105  +    DELETE FROM t1 WHERE b BETWEEN 8 AND 12;
          106  +    ANALYZE;
          107  +    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
          108  +    PRAGMA integrity_check;
          109  +  }
          110  +} {{} 15 t1a {10 1} t1b {8 1} ok}
          111  +
          112  +do_test index6-1.14 {
          113  +  execsql {
          114  +    REINDEX;
          115  +    ANALYZE;
          116  +    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
          117  +    PRAGMA integrity_check;
          118  +  }
          119  +} {{} 15 t1a {10 1} t1b {8 1} ok}
          120  +
          121  +do_test index6-1.15 {
          122  +  execsql {
          123  +    CREATE INDEX t1c ON t1(c);
          124  +    ANALYZE;
          125  +    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
          126  +    PRAGMA integrity_check;
          127  +  }
          128  +} {t1a {10 1} t1b {8 1} t1c {15 1} ok}
          129  +
          130  +# Queries use partial indices as appropriate times.
          131  +#
          132  +do_test index6-2.1 {
          133  +  execsql {
          134  +    CREATE TABLE t2(a,b);
          135  +    INSERT INTO t2(a,b) SELECT value, value FROM nums WHERE value<1000;
          136  +    UPDATE t2 SET a=NULL WHERE b%5==0;
          137  +    CREATE INDEX t2a1 ON t2(a) WHERE a IS NOT NULL;
          138  +    SELECT count(*) FROM t2 WHERE a IS NOT NULL;
          139  +  }
          140  +} {800}
          141  +do_test index6-2.2 {
          142  +  execsql {
          143  +    EXPLAIN QUERY PLAN
          144  +    SELECT * FROM t2 WHERE a=5;
          145  +  }
          146  +} {/.* TABLE t2 USING INDEX t2a1 .*/}
          147  +do_test index6-2.3 {
          148  +  execsql {
          149  +    EXPLAIN QUERY PLAN
          150  +    SELECT * FROM t2 WHERE a IS NOT NULL;
          151  +  }
          152  +} {/.* TABLE t2 USING INDEX t2a1 .*/}
          153  +do_test index6-2.4 {
          154  +  execsql {
          155  +    EXPLAIN QUERY PLAN
          156  +    SELECT * FROM t2 WHERE a IS NULL;
          157  +  }
          158  +} {~/.*INDEX t2a1.*/}
          159  +
          160  +do_execsql_test index6-2.101 {
          161  +  DROP INDEX t2a1;
          162  +  UPDATE t2 SET a=b, b=b+10000;
          163  +  SELECT b FROM t2 WHERE a=15;
          164  +} {10015}
          165  +do_execsql_test index6-2.102 {
          166  +  CREATE INDEX t2a2 ON t2(a) WHERE a<100 OR a>200;
          167  +  SELECT b FROM t2 WHERE a=15;
          168  +  PRAGMA integrity_check;
          169  +} {10015 ok}
          170  +do_execsql_test index6-2.102eqp {
          171  +  EXPLAIN QUERY PLAN
          172  +  SELECT b FROM t2 WHERE a=15;
          173  +} {~/.*INDEX t2a2.*/}
          174  +do_execsql_test index6-2.103 {
          175  +  SELECT b FROM t2 WHERE a=15 AND a<100;
          176  +} {10015}
          177  +do_execsql_test index6-2.103eqp {
          178  +  EXPLAIN QUERY PLAN
          179  +  SELECT b FROM t2 WHERE a=15 AND a<100;
          180  +} {/.*INDEX t2a2.*/}
          181  +do_execsql_test index6-2.104 {
          182  +  SELECT b FROM t2 WHERE a=515 AND a>200;
          183  +} {10515}
          184  +do_execsql_test index6-2.104eqp {
          185  +  EXPLAIN QUERY PLAN
          186  +  SELECT b FROM t2 WHERE a=515 AND a>200;
          187  +} {/.*INDEX t2a2.*/}
          188  +
          189  +# Partial UNIQUE indices
          190  +#
          191  +do_execsql_test index6-3.1 {
          192  +  CREATE TABLE t3(a,b);
          193  +  INSERT INTO t3 SELECT value, value FROM nums WHERE value<200;
          194  +  UPDATE t3 SET a=999 WHERE b%5!=0;
          195  +  CREATE UNIQUE INDEX t3a ON t3(a) WHERE a<>999;
          196  +} {}
          197  +do_test index6-3.2 {
          198  +  # unable to insert a duplicate row a-value that is not 999.
          199  +  catchsql {
          200  +    INSERT INTO t3(a,b) VALUES(150, 'test1');
          201  +  }
          202  +} {1 {column a is not unique}}
          203  +do_test index6-3.3 {
          204  +  # can insert multiple rows with a==999 because such rows are not
          205  +  # part of the unique index.
          206  +  catchsql {
          207  +    INSERT INTO t3(a,b) VALUES(999, 'test1'), (999, 'test2');
          208  +  }
          209  +} {0 {}}
          210  +do_execsql_test index6-3.4 {
          211  +  SELECT count(*) FROM t3 WHERE a=999;
          212  +} {162}
          213  +integrity_check index6-3.5
          214  +
          215  +do_execsql_test index6-4.0 {
          216  +  VACUUM;
          217  +  PRAGMA integrity_check;
          218  +} {ok}
          219  +
          220  +# Silently ignore database name qualifiers in partial indices.
          221  +#
          222  +do_execsql_test index6-5.0 {
          223  +  CREATE INDEX t3b ON t3(b) WHERE xyzzy.t3.b BETWEEN 5 AND 10;
          224  +                               /* ^^^^^-- ignored */
          225  +  ANALYZE;
          226  +  SELECT count(*) FROM t3 WHERE t3.b BETWEEN 5 AND 10;
          227  +  SELECT stat+0 FROM sqlite_stat1 WHERE idx='t3b';
          228  +} {6 6}
          229  +
          230  +finish_test

Changes to test/loadext2.test.

    38     38     }
    39     39   } {1 {no such function: sqr}}
    40     40   do_test loadext2-1.2 {
    41     41     catchsql {
    42     42       SELECT cube(2)
    43     43     }
    44     44   } {1 {no such function: cube}}
           45  +
           46  +# Extensions loaders not currently registered
           47  +#
           48  +do_test loadext2-1.2.1 {
           49  +  sqlite3_cancel_auto_extension_sqr
           50  +} {0}
           51  +do_test loadext2-1.2.2 {
           52  +  sqlite3_cancel_auto_extension_sqr
           53  +} {0}
           54  +do_test loadext2-1.2.3 {
           55  +  sqlite3_cancel_auto_extension_sqr
           56  +} {0}
           57  +
    45     58   
    46     59   # Register auto-loaders.  Still functions do not exist.
    47     60   #
    48     61   do_test loadext2-1.3 {
    49     62     sqlite3_auto_extension_sqr
    50     63     sqlite3_auto_extension_cube
    51     64     catchsql {
................................................................................
    72     85       SELECT cube(2)
    73     86     }
    74     87   } {0 8.0}
    75     88   
    76     89   
    77     90   # Reset extension auto loading.  Existing extensions still exist.
    78     91   #
    79         -do_test loadext2-1.7 {
    80         -  sqlite3_reset_auto_extension
           92  +do_test loadext2-1.7.1 {
           93  +  sqlite3_cancel_auto_extension_sqr
           94  +} {1}
           95  +do_test loadext2-1.7.2 {
           96  +  sqlite3_cancel_auto_extension_sqr
           97  +} {0}
           98  +do_test loadext2-1.7.3 {
           99  +  sqlite3_cancel_auto_extension_cube
          100  +} {1}
          101  +do_test loadext2-1.7.4 {
          102  +  sqlite3_cancel_auto_extension_cube
          103  +} {0}
          104  +do_test loadext2-1.7.5 {
    81    105     catchsql {
    82    106       SELECT sqr(2)
    83    107     }
    84    108   } {0 4.0}
    85    109   do_test loadext2-1.8 {
    86    110     catchsql {
    87    111       SELECT cube(2)

Changes to test/where2.test.

   694    694     }
   695    695   } {4}
   696    696   do_test where2-11.4 {
   697    697     execsql {
   698    698       SELECT d FROM t11 WHERE c=7 OR (a=1 AND b=2) ORDER BY d;
   699    699     }
   700    700   } {4 8 10}
          701  +
          702  +# Verify that the OR clause is used in an outer loop even when
          703  +# the OR clause scores slightly better on an inner loop.
          704  +do_execsql_test where2-12.1 {
          705  +  CREATE TABLE t12(x INTEGER PRIMARY KEY, y);
          706  +  CREATE INDEX t12y ON t12(y);
          707  +  EXPLAIN QUERY PLAN
          708  +    SELECT a.x, b.x
          709  +      FROM t12 AS a JOIN t12 AS b ON a.y=b.x
          710  +     WHERE (b.x=$abc OR b.y=$abc);
          711  +} {/.*SEARCH TABLE t12 AS b .*SEARCH TABLE t12 AS b .*/}
   701    712   
   702    713   
   703    714   finish_test

Changes to test/where8.test.

   208    208       SELECT a, d FROM t1, t2 WHERE (a = 2 OR a = 3) AND d = a
   209    209     }
   210    210   } {2 2 3 3 0 0}
   211    211   
   212    212   do_test where8-3.5 {
   213    213     execsql_status {
   214    214       SELECT a, d FROM t1, t2 WHERE (a = 2 OR a = 3) AND (d = a OR e = 'sixteen')
          215  +     ORDER BY +a, +d;
   215    216     }
   216         -} {2 2 2 4 3 3 3 4 0 0}
          217  +} {2 2 2 4 3 3 3 4 0 1}
   217    218   
   218    219   do_test where8-3.6 {
   219    220     # The first part of the WHERE clause in this query, (a=2 OR a=3) is
   220    221     # transformed into "a IN (2, 3)". This is why the sort is required.
   221    222     #
   222    223     execsql_status {
   223    224       SELECT a, d 
................................................................................
   229    230   do_test where8-3.7 {
   230    231     execsql_status {
   231    232       SELECT a, d 
   232    233       FROM t1, t2 
   233    234       WHERE a = 2 AND (d = a OR e = 'sixteen')
   234    235       ORDER BY t1.rowid
   235    236     }
   236         -} {2 2 2 4 0 0}
          237  +} {/2 2 2 4 0 [01]/}
   237    238   do_test where8-3.8 {
   238    239     execsql_status {
   239    240       SELECT a, d 
   240    241       FROM t1, t2 
   241    242       WHERE (a = 2 OR b = 'three') AND (d = a OR e = 'sixteen')
   242    243       ORDER BY t1.rowid
   243    244     }

Changes to tool/spaceanal.tcl.

   242    242   
   243    243   # Quote a string for use in an SQL query. Examples:
   244    244   #
   245    245   # [quote {hello world}]   == {'hello world'}
   246    246   # [quote {hello world's}] == {'hello world''s'}
   247    247   #
   248    248   proc quote {txt} {
   249         -  regsub -all ' $txt '' q
   250         -  return '$q'
          249  +  return [string map {' ''} $txt]
          250  +}
          251  +
          252  +# Output a title line
          253  +#
          254  +proc titleline {title} {
          255  +  if {$title==""} {
          256  +    puts [string repeat * 79]
          257  +  } else {
          258  +    set len [string length $title]
          259  +    set stars [string repeat * [expr 79-$len-5]]
          260  +    puts "*** $title $stars"
          261  +  }
   251    262   }
   252    263   
   253    264   # Generate a single line of output in the statistics section of the
   254    265   # report.
   255    266   #
   256    267   proc statline {title value {extra {}}} {
   257    268     set len [string length $title]
   258         -  set dots [string range {......................................} $len end]
          269  +  set dots [string repeat . [expr 50-$len]]
   259    270     set len [string length $value]
   260    271     set sp2 [string range {          } $len end]
   261    272     if {$extra ne ""} {
   262    273       set extra " $extra"
   263    274     }
   264    275     puts "$title$dots $value$sp2$extra"
   265    276   }
................................................................................
   315    326         int(sum(gap_cnt)) AS gap_cnt,
   316    327         int(sum(compressed_size)) AS compressed_size
   317    328       FROM space_used WHERE $where" {} {}
   318    329   
   319    330     # Output the sub-report title, nicely decorated with * characters.
   320    331     #
   321    332     puts ""
   322         -  set len [string length $title]
   323         -  set stars [string repeat * [expr 65-$len]]
   324         -  puts "*** $title $stars"
          333  +  titleline $title
   325    334     puts ""
   326    335   
   327    336     # Calculate statistics and store the results in TCL variables, as follows:
   328    337     #
   329    338     # total_pages: Database pages consumed.
   330    339     # total_pages_percent: Pages consumed as a percentage of the file.
   331    340     # storage: Bytes consumed.
................................................................................
   486    495   set user_payload [mem one {SELECT int(sum(payload)) FROM space_used
   487    496        WHERE NOT is_index AND name NOT LIKE 'sqlite_master'}]
   488    497   set user_percent [percent $user_payload $file_bytes]
   489    498   
   490    499   # Output the summary statistics calculated above.
   491    500   #
   492    501   puts "/** Disk-Space Utilization Report For $root_filename"
   493         -catch {
   494         -  puts "*** As of [clock format [clock seconds] -format {%Y-%b-%d %H:%M:%S}]"
   495         -}
   496    502   puts ""
   497    503   statline {Page size in bytes} $pageSize
   498    504   statline {Pages in the whole file (measured)} $file_pgcnt
   499    505   statline {Pages in the whole file (calculated)} $file_pgcnt2
   500    506   statline {Pages that store data} $inuse_pgcnt $inuse_percent
   501    507   statline {Pages on the freelist (per header)} $free_pgcnt2 $free_percent2
   502    508   statline {Pages on the freelist (calculated)} $free_pgcnt $free_percent
................................................................................
   513    519     statline {Size of the file in bytes} $file_bytes
   514    520   }
   515    521   statline {Bytes of user payload stored} $user_payload $user_percent
   516    522   
   517    523   # Output table rankings
   518    524   #
   519    525   puts ""
   520         -puts "*** Page counts for all tables with their indices ********************"
          526  +titleline "Page counts for all tables with their indices"
   521    527   puts ""
   522    528   mem eval {SELECT tblname, count(*) AS cnt, 
   523    529                 int(sum(int_pages+leaf_pages+ovfl_pages)) AS size
   524    530             FROM space_used GROUP BY tblname ORDER BY size+0 DESC, tblname} {} {
   525    531     statline [string toupper $tblname] $size [percent $size $file_pgcnt]
   526    532   }
          533  +puts ""
          534  +titleline "Page counts for all tables and indices separately"
          535  +puts ""
          536  +mem eval {
          537  +  SELECT
          538  +       upper(name) AS nm,
          539  +       int(int_pages+leaf_pages+ovfl_pages) AS size
          540  +    FROM space_used
          541  +   ORDER BY size+0 DESC, name} {} {
          542  +  statline $nm $size [percent $size $file_pgcnt]
          543  +}
   527    544   if {$isCompressed} {
   528    545     puts ""
   529         -  puts "*** Bytes of disk space used after compression ***********************"
          546  +  titleline "Bytes of disk space used after compression"
   530    547     puts ""
   531    548     set csum 0
   532    549     mem eval {SELECT tblname,
   533    550                     int(sum(compressed_size)) +
   534    551                            $compressOverhead*sum(int_pages+leaf_pages+ovfl_pages)
   535    552                           AS csize
   536    553             FROM space_used GROUP BY tblname ORDER BY csize+0 DESC, tblname} {} {
................................................................................
   550    567   }
   551    568   subreport {All tables} {NOT is_index}
   552    569   if {$nindex>0} {
   553    570     subreport {All indices} {is_index}
   554    571   }
   555    572   foreach tbl [mem eval {SELECT name FROM space_used WHERE NOT is_index
   556    573                          ORDER BY name}] {
   557         -  regsub ' $tbl '' qn
          574  +  set qn [quote $tbl]
   558    575     set name [string toupper $tbl]
   559         -  set n [mem eval "SELECT count(*) FROM space_used WHERE tblname='$qn'"]
          576  +  set n [mem eval {SELECT count(*) FROM space_used WHERE tblname=$tbl}]
   560    577     if {$n>1} {
          578  +    set idxlist [mem eval "SELECT name FROM space_used
          579  +                            WHERE tblname='$qn' AND is_index
          580  +                            ORDER BY 1"]
   561    581       subreport "Table $name and all its indices" "tblname='$qn'"
   562    582       subreport "Table $name w/o any indices" "name='$qn'"
   563         -    subreport "Indices of table $name" "tblname='$qn' AND is_index"
          583  +    if {[llength $idxlist]>1} {
          584  +      subreport "Indices of table $name" "tblname='$qn' AND is_index"
          585  +    }
          586  +    foreach idx $idxlist {
          587  +      set qidx [quote $idx]
          588  +      subreport "Index [string toupper $idx] of table $name" "name='$qidx'"
          589  +    }
   564    590     } else {
   565    591       subreport "Table $name" "name='$qn'"
   566    592     }
   567    593   }
   568    594   
   569    595   # Output instructions on what the numbers above mean.
   570    596   #
          597  +puts ""
          598  +titleline Definitions
   571    599   puts {
   572         -*** Definitions ******************************************************
   573         -
   574    600   Page size in bytes
   575    601   
   576    602       The number of bytes in a single page of the database file.  
   577    603       Usually 1024.
   578    604   
   579    605   Number of pages in the whole file
   580    606   }
................................................................................
   718    744       pages.  The percentage at the right is the number of unused bytes 
   719    745       divided by the total number of bytes.
   720    746   }
   721    747   
   722    748   # Output a dump of the in-memory database. This can be used for more
   723    749   # complex offline analysis.
   724    750   #
   725         -puts "**********************************************************************"
          751  +titleline {}
   726    752   puts "The entire text of this report can be sourced into any SQL database"
   727    753   puts "engine for further analysis.  All of the text above is an SQL comment."
   728    754   puts "The data used to generate this report follows:"
   729    755   puts "*/"
   730    756   puts "BEGIN;"
   731    757   puts $tabledef
   732    758   unset -nocomplain x