/ Changes On Branch nextgen-query-plan-exp
Login

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

Changes In Branch nextgen-query-plan-exp Excluding Merge-Ins

This is equivalent to a diff from b6744622 to 19ab4811

2013-06-26
11:43
Cut over the next generation query planner. Increase the version number to 3.8.0. (check-in: 0fe31f60 user: drh tags: trunk)
00:34
Fix an uninitialized variable detected by valgrind. Unclear whether or not this should apply to trunk. (Closed-Leaf check-in: 19ab4811 user: drh tags: nextgen-query-plan-exp)
2013-06-21
18:32
Merge updates from trunk. (check-in: fa2a91e6 user: mistachkin tags: toTypeFuncs)
18:29
Merge the fts4-notindexed branch with the trunk. (check-in: 361084e1 user: dan tags: trunk)
17:30
Add the "notindexed" option to fts4. (check-in: 8ff2b8f5 user: dan tags: fts4-notindexed)
2013-06-20
18:53
VSIX tooling changes to support Visual Studio 2013. (check-in: c5954c58 user: mistachkin tags: vsix2013)
17:32
Add a NEVER() macro and an explanation comment around an unreachable branch in the STAT3 logic. (check-in: 604c3c5d user: drh tags: nextgen-query-plan-exp)
14:17
Pull in the posix_fallocate() change from trunk. (check-in: d94db3fd user: drh tags: nextgen-query-plan-exp)
14:07
Disable posix_fallocate() for all systems, all the time, unless the HAVE_POSIX_FALLOCATE compile-time macro is supplied. (check-in: b6744622 user: drh tags: trunk)
2013-06-19
14:49
Only default HAVE_POSIX_FALLOCATE on for linux, and then only if it is not previously defined. (check-in: 2b2ade92 user: drh tags: trunk)

Changes to ext/fts3/fts3.c.

  1419   1419     int iLangidCons = -1;           /* Index of langid=x constraint, if present */
  1420   1420   
  1421   1421     /* By default use a full table scan. This is an expensive option,
  1422   1422     ** so search through the constraints to see if a more efficient 
  1423   1423     ** strategy is possible.
  1424   1424     */
  1425   1425     pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
  1426         -  pInfo->estimatedCost = 500000;
         1426  +  pInfo->estimatedCost = 5000000;
  1427   1427     for(i=0; i<pInfo->nConstraint; i++){
  1428   1428       struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
  1429   1429       if( pCons->usable==0 ) continue;
  1430   1430   
  1431   1431       /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
  1432   1432       if( iCons<0 
  1433   1433        && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ 

Changes to ext/misc/closure.c.

   822    822   static int closureBestIndex(
   823    823     sqlite3_vtab *pTab,             /* The virtual table */
   824    824     sqlite3_index_info *pIdxInfo    /* Information about the query */
   825    825   ){
   826    826     int iPlan = 0;
   827    827     int i;
   828    828     int idx = 1;
          829  +  int seenMatch = 0;
   829    830     const struct sqlite3_index_constraint *pConstraint;
   830    831     closure_vtab *pVtab = (closure_vtab*)pTab;
          832  +  double rCost = 10000000.0;
   831    833   
   832    834     pConstraint = pIdxInfo->aConstraint;
   833    835     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
          836  +    if( pConstraint->iColumn==CLOSURE_COL_ROOT
          837  +     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
          838  +      seenMatch = 1;
          839  +    }
   834    840       if( pConstraint->usable==0 ) continue;
   835    841       if( (iPlan & 1)==0 
   836    842        && pConstraint->iColumn==CLOSURE_COL_ROOT
   837    843        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
   838    844       ){
   839    845         iPlan |= 1;
   840    846         pIdxInfo->aConstraintUsage[i].argvIndex = 1;
   841    847         pIdxInfo->aConstraintUsage[i].omit = 1;
          848  +      rCost /= 100.0;
   842    849       }
   843    850       if( (iPlan & 0x0000f0)==0
   844    851        && pConstraint->iColumn==CLOSURE_COL_DEPTH
   845    852        && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
   846    853              || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE
   847    854              || pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ)
   848    855       ){
   849    856         iPlan |= idx<<4;
   850    857         pIdxInfo->aConstraintUsage[i].argvIndex = ++idx;
   851    858         if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ) iPlan |= 0x000002;
          859  +      rCost /= 5.0;
   852    860       }
   853    861       if( (iPlan & 0x000f00)==0
   854    862        && pConstraint->iColumn==CLOSURE_COL_TABLENAME
   855    863        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
   856    864       ){
   857    865         iPlan |= idx<<8;
   858    866         pIdxInfo->aConstraintUsage[i].argvIndex = ++idx;
   859    867         pIdxInfo->aConstraintUsage[i].omit = 1;
          868  +      rCost /= 5.0;
   860    869       }
   861    870       if( (iPlan & 0x00f000)==0
   862    871        && pConstraint->iColumn==CLOSURE_COL_IDCOLUMN
   863    872        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
   864    873       ){
   865    874         iPlan |= idx<<12;
   866    875         pIdxInfo->aConstraintUsage[i].argvIndex = ++idx;
................................................................................
   887    896     pIdxInfo->idxNum = iPlan;
   888    897     if( pIdxInfo->nOrderBy==1
   889    898      && pIdxInfo->aOrderBy[0].iColumn==CLOSURE_COL_ID
   890    899      && pIdxInfo->aOrderBy[0].desc==0
   891    900     ){
   892    901       pIdxInfo->orderByConsumed = 1;
   893    902     }
   894         -  pIdxInfo->estimatedCost = (double)10000;
          903  +  if( seenMatch && (iPlan&1)==0 ) rCost *= 1e30;
          904  +  pIdxInfo->estimatedCost = rCost;
   895    905      
   896    906     return SQLITE_OK;
   897    907   }
   898    908   
   899    909   /*
   900    910   ** A virtual table module that implements the "approximate_match".
   901    911   */

Changes to ext/misc/fuzzer.c.

  1073   1073   ** filter.argv[2] if both bit-1 and bit-2 are set.
  1074   1074   */
  1075   1075   static int fuzzerBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  1076   1076     int iPlan = 0;
  1077   1077     int iDistTerm = -1;
  1078   1078     int iRulesetTerm = -1;
  1079   1079     int i;
         1080  +  int seenMatch = 0;
  1080   1081     const struct sqlite3_index_constraint *pConstraint;
         1082  +  double rCost = 1e12;
         1083  +
  1081   1084     pConstraint = pIdxInfo->aConstraint;
  1082   1085     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
         1086  +    if( pConstraint->iColumn==0
         1087  +     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
         1088  +      seenMatch = 1;
         1089  +    }
  1083   1090       if( pConstraint->usable==0 ) continue;
  1084   1091       if( (iPlan & 1)==0 
  1085   1092        && pConstraint->iColumn==0
  1086   1093        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH
  1087   1094       ){
  1088   1095         iPlan |= 1;
  1089   1096         pIdxInfo->aConstraintUsage[i].argvIndex = 1;
  1090   1097         pIdxInfo->aConstraintUsage[i].omit = 1;
         1098  +      rCost /= 1e6;
  1091   1099       }
  1092   1100       if( (iPlan & 2)==0
  1093   1101        && pConstraint->iColumn==1
  1094   1102        && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
  1095   1103              || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE)
  1096   1104       ){
  1097   1105         iPlan |= 2;
  1098   1106         iDistTerm = i;
         1107  +      rCost /= 10.0;
  1099   1108       }
  1100   1109       if( (iPlan & 4)==0
  1101   1110        && pConstraint->iColumn==2
  1102   1111        && pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ
  1103   1112       ){
  1104   1113         iPlan |= 4;
  1105   1114         pIdxInfo->aConstraintUsage[i].omit = 1;
  1106   1115         iRulesetTerm = i;
         1116  +      rCost /= 10.0;
  1107   1117       }
  1108   1118     }
  1109   1119     if( iPlan & 2 ){
  1110   1120       pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = 1+((iPlan&1)!=0);
  1111   1121     }
  1112   1122     if( iPlan & 4 ){
  1113   1123       int idx = 1;
................................................................................
  1118   1128     pIdxInfo->idxNum = iPlan;
  1119   1129     if( pIdxInfo->nOrderBy==1
  1120   1130      && pIdxInfo->aOrderBy[0].iColumn==1
  1121   1131      && pIdxInfo->aOrderBy[0].desc==0
  1122   1132     ){
  1123   1133       pIdxInfo->orderByConsumed = 1;
  1124   1134     }
  1125         -  pIdxInfo->estimatedCost = (double)10000;
         1135  +  if( seenMatch && (iPlan&1)==0 ) rCost = 1e99;
         1136  +  pIdxInfo->estimatedCost = rCost;
  1126   1137      
  1127   1138     return SQLITE_OK;
  1128   1139   }
  1129   1140   
  1130   1141   /*
  1131   1142   ** A virtual table module that implements the "fuzzer".
  1132   1143   */

Changes to ext/rtree/rtree6.test.

    70     70   do_test rtree6-1.5 {
    71     71     rtree_strategy {SELECT * FROM t1,t2 WHERE k=+ii AND x1<10}
    72     72   } {Ca}
    73     73   
    74     74   do_eqp_test rtree6.2.1 {
    75     75     SELECT * FROM t1,t2 WHERE k=+ii AND x1<10
    76     76   } {
    77         -  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:Ca (~0 rows)} 
    78         -  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
           77  +  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:Ca} 
           78  +  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)}
    79     79   }
    80     80   
    81     81   do_eqp_test rtree6.2.2 {
    82     82     SELECT * FROM t1,t2 WHERE k=ii AND x1<10
    83     83   } {
    84         -  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:Ca (~0 rows)} 
    85         -  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
           84  +  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:Ca} 
           85  +  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)}
    86     86   }
    87     87   
    88     88   do_eqp_test rtree6.2.3 {
    89     89     SELECT * FROM t1,t2 WHERE k=ii
    90     90   } {
    91         -  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2: (~0 rows)} 
    92         -  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
           91  +  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:} 
           92  +  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)}
    93     93   }
    94     94   
    95     95   do_eqp_test rtree6.2.4 {
    96     96     SELECT * FROM t1,t2 WHERE v=10 and x1<10 and x2>10
    97     97   } {
    98         -  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:CaEb (~0 rows)} 
    99         -  0 1 1 {SCAN TABLE t2 (~100000 rows)}
           98  +  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:CaEb} 
           99  +  0 1 1 {SCAN TABLE t2}
   100    100   }
   101    101   
   102    102   do_eqp_test rtree6.2.5 {
   103    103     SELECT * FROM t1,t2 WHERE k=ii AND x1<v
   104    104   } {
   105         -  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2: (~0 rows)} 
   106         -  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
          105  +  0 0 0 {SCAN TABLE t1 VIRTUAL TABLE INDEX 2:} 
          106  +  0 1 1 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)}
   107    107   }
   108    108   
   109    109   do_execsql_test rtree6-3.1 {
   110    110     CREATE VIRTUAL TABLE t3 USING rtree(id, x1, x2, y1, y2);
   111    111     INSERT INTO t3 VALUES(NULL, 1, 1, 2, 2);
   112    112     SELECT * FROM t3 WHERE 
   113    113       x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND x1>0.5 AND 

Changes to ext/rtree/rtree8.test.

   164    164       execsql { DELETE FROM t2 WHERE id = $i }
   165    165     }
   166    166     execsql COMMIT
   167    167   } {}
   168    168   
   169    169   
   170    170   finish_test
   171         -

Changes to main.mk.

   589    589   
   590    590   soaktest:	testfixture$(EXE) sqlite3$(EXE)
   591    591   	./testfixture$(EXE) $(TOP)/test/all.test -soak=1
   592    592   
   593    593   fulltestonly:	testfixture$(EXE) sqlite3$(EXE)
   594    594   	./testfixture$(EXE) $(TOP)/test/full.test
   595    595   
          596  +queryplantest:	testfixture$(EXE) sqlite3$(EXE)
          597  +	./testfixture$(EXE) $(TOP)/test/permutations.test queryplanner
          598  +
   596    599   test:	testfixture$(EXE) sqlite3$(EXE)
   597    600   	./testfixture$(EXE) $(TOP)/test/veryquick.test
   598    601   
   599    602   # The next two rules are used to support the "threadtest" target. Building
   600    603   # threadtest runs a few thread-safety tests that are implemented in C. This
   601    604   # target is invoked by the releasetest.tcl script.
   602    605   # 

Changes to src/backup.c.

    11     11   *************************************************************************
    12     12   ** This file contains the implementation of the sqlite3_backup_XXX() 
    13     13   ** API functions and the related features.
    14     14   */
    15     15   #include "sqliteInt.h"
    16     16   #include "btreeInt.h"
    17     17   
    18         -/* Macro to find the minimum of two numeric values.
    19         -*/
    20         -#ifndef MIN
    21         -# define MIN(x,y) ((x)<(y)?(x):(y))
    22         -#endif
    23         -
    24     18   /*
    25     19   ** Structure allocated for each backup operation.
    26     20   */
    27     21   struct sqlite3_backup {
    28     22     sqlite3* pDestDb;        /* Destination database handle */
    29     23     Btree *pDest;            /* Destination b-tree file */
    30     24     u32 iDestSchema;         /* Original schema cookie in destination */

Changes to src/build.c.

  2692   2692     pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
  2693   2693     pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
  2694   2694     zExtra = (char *)(&pIndex->zName[nName+1]);
  2695   2695     memcpy(pIndex->zName, zName, nName+1);
  2696   2696     pIndex->pTable = pTab;
  2697   2697     pIndex->nColumn = pList->nExpr;
  2698   2698     pIndex->onError = (u8)onError;
         2699  +  pIndex->uniqNotNull = onError==OE_Abort;
  2699   2700     pIndex->autoIndex = (u8)(pName==0);
  2700   2701     pIndex->pSchema = db->aDb[iDb].pSchema;
  2701   2702     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  2702   2703   
  2703   2704     /* Check to see if we should honor DESC requests on index columns
  2704   2705     */
  2705   2706     if( pDb->pSchema->file_format>=4 ){
................................................................................
  2750   2751       }
  2751   2752       if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
  2752   2753         goto exit_create_index;
  2753   2754       }
  2754   2755       pIndex->azColl[i] = zColl;
  2755   2756       requestedSortOrder = pListItem->sortOrder & sortOrderMask;
  2756   2757       pIndex->aSortOrder[i] = (u8)requestedSortOrder;
         2758  +    if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
  2757   2759     }
  2758   2760     sqlite3DefaultRowEst(pIndex);
  2759   2761   
  2760   2762     if( pTab==pParse->pNewTable ){
  2761   2763       /* This routine has been called to create an automatic index as a
  2762   2764       ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
  2763   2765       ** a PRIMARY KEY or UNIQUE clause following the column definitions.
................................................................................
  3181   3183                  sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
  3182   3184       if( pNew==0 ){
  3183   3185         assert( db->mallocFailed );
  3184   3186         return pSrc;
  3185   3187       }
  3186   3188       pSrc = pNew;
  3187   3189       nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
  3188         -    pSrc->nAlloc = (u16)nGot;
         3190  +    pSrc->nAlloc = (u8)nGot;
  3189   3191     }
  3190   3192   
  3191   3193     /* Move existing slots that come after the newly inserted slots
  3192   3194     ** out of the way */
  3193   3195     for(i=pSrc->nSrc-1; i>=iStart; i--){
  3194   3196       pSrc->a[i+nExtra] = pSrc->a[i];
  3195   3197     }
  3196         -  pSrc->nSrc += (i16)nExtra;
         3198  +  pSrc->nSrc += (i8)nExtra;
  3197   3199   
  3198   3200     /* Zero the newly allocated slots */
  3199   3201     memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
  3200   3202     for(i=iStart; i<iStart+nExtra; i++){
  3201   3203       pSrc->a[i].iCursor = -1;
  3202   3204     }
  3203   3205   

Changes to src/expr.c.

   916    916     for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
   917    917       Expr *pOldExpr = pOldItem->pExpr;
   918    918       pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
   919    919       pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
   920    920       pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
   921    921       pItem->sortOrder = pOldItem->sortOrder;
   922    922       pItem->done = 0;
          923  +    pItem->bSpanIsTab = pOldItem->bSpanIsTab;
   923    924       pItem->iOrderByCol = pOldItem->iOrderByCol;
   924    925       pItem->iAlias = pOldItem->iAlias;
   925    926     }
   926    927     return pNew;
   927    928   }
   928    929   
   929    930   /*
................................................................................
  1592   1593       }
  1593   1594     }
  1594   1595   
  1595   1596     if( eType==0 ){
  1596   1597       /* Could not found an existing table or index to use as the RHS b-tree.
  1597   1598       ** We will have to generate an ephemeral table to do the job.
  1598   1599       */
  1599         -    double savedNQueryLoop = pParse->nQueryLoop;
         1600  +    u32 savedNQueryLoop = pParse->nQueryLoop;
  1600   1601       int rMayHaveNull = 0;
  1601   1602       eType = IN_INDEX_EPH;
  1602   1603       if( prNotFound ){
  1603   1604         *prNotFound = rMayHaveNull = ++pParse->nMem;
  1604   1605         sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
  1605   1606       }else{
  1606         -      testcase( pParse->nQueryLoop>(double)1 );
  1607         -      pParse->nQueryLoop = (double)1;
         1607  +      testcase( pParse->nQueryLoop>0 );
         1608  +      pParse->nQueryLoop = 0;
  1608   1609         if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
  1609   1610           eType = IN_INDEX_ROWID;
  1610   1611         }
  1611   1612       }
  1612   1613       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
  1613   1614       pParse->nQueryLoop = savedNQueryLoop;
  1614   1615     }else{

Changes to src/memjournal.c.

    27     27   ** The size chosen is a little less than a power of two.  That way,
    28     28   ** the FileChunk object will have a size that almost exactly fills
    29     29   ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
    30     30   ** memory allocators.
    31     31   */
    32     32   #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
    33     33   
    34         -/* Macro to find the minimum of two numeric values.
    35         -*/
    36         -#ifndef MIN
    37         -# define MIN(x,y) ((x)<(y)?(x):(y))
    38         -#endif
    39         -
    40     34   /*
    41     35   ** The rollback journal is composed of a linked list of these structures.
    42     36   */
    43     37   struct FileChunk {
    44     38     FileChunk *pNext;               /* Next chunk in the journal */
    45     39     u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
    46     40   };

Changes to src/os_win.c.

    80     80   
    81     81   /*
    82     82   ** This file mapping API is common to both Win32 and WinRT.
    83     83   */
    84     84   WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
    85     85   #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
    86     86   
    87         -/*
    88         -** Macro to find the minimum of two numeric values.
    89         -*/
    90         -#ifndef MIN
    91         -# define MIN(x,y) ((x)<(y)?(x):(y))
    92         -#endif
    93         -
    94     87   /*
    95     88   ** Some Microsoft compilers lack this definition.
    96     89   */
    97     90   #ifndef INVALID_FILE_ATTRIBUTES
    98     91   # define INVALID_FILE_ATTRIBUTES ((DWORD)-1) 
    99     92   #endif
   100     93   

Changes to src/prepare.c.

   588    588         }
   589    589       }
   590    590     }
   591    591   
   592    592     sqlite3VtabUnlockList(db);
   593    593   
   594    594     pParse->db = db;
   595         -  pParse->nQueryLoop = (double)1;
          595  +  pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
   596    596     if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
   597    597       char *zSqlCopy;
   598    598       int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
   599    599       testcase( nBytes==mxLen );
   600    600       testcase( nBytes==mxLen+1 );
   601    601       if( nBytes>mxLen ){
   602    602         sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
................................................................................
   610    610         pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
   611    611       }else{
   612    612         pParse->zTail = &zSql[nBytes];
   613    613       }
   614    614     }else{
   615    615       sqlite3RunParser(pParse, zSql, &zErrMsg);
   616    616     }
   617         -  assert( 1==(int)pParse->nQueryLoop );
          617  +  assert( 0==pParse->nQueryLoop );
   618    618   
   619    619     if( db->mallocFailed ){
   620    620       pParse->rc = SQLITE_NOMEM;
   621    621     }
   622    622     if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
   623    623     if( pParse->checkSchema ){
   624    624       schemaIsValid(pParse);

Changes to src/select.c.

  1534   1534       v = sqlite3GetVdbe(pParse);
  1535   1535       if( NEVER(v==0) ) return;  /* VDBE should have already been allocated */
  1536   1536       if( sqlite3ExprIsInteger(p->pLimit, &n) ){
  1537   1537         sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
  1538   1538         VdbeComment((v, "LIMIT counter"));
  1539   1539         if( n==0 ){
  1540   1540           sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
  1541         -      }else{
  1542         -        if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n;
         1541  +      }else if( n>=0 && p->nSelectRow>(u64)n ){
         1542  +        p->nSelectRow = n;
  1543   1543         }
  1544   1544       }else{
  1545   1545         sqlite3ExprCode(pParse, p->pLimit, iLimit);
  1546   1546         sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
  1547   1547         VdbeComment((v, "LIMIT counter"));
  1548   1548         sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
  1549   1549       }
................................................................................
  1729   1729         rc = sqlite3Select(pParse, p, &dest);
  1730   1730         testcase( rc!=SQLITE_OK );
  1731   1731         pDelete = p->pPrior;
  1732   1732         p->pPrior = pPrior;
  1733   1733         p->nSelectRow += pPrior->nSelectRow;
  1734   1734         if( pPrior->pLimit
  1735   1735          && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
  1736         -       && p->nSelectRow > (double)nLimit 
         1736  +       && nLimit>0 && p->nSelectRow > (u64)nLimit 
  1737   1737         ){
  1738         -        p->nSelectRow = (double)nLimit;
         1738  +        p->nSelectRow = nLimit;
  1739   1739         }
  1740   1740         if( addr ){
  1741   1741           sqlite3VdbeJumpHere(v, addr);
  1742   1742         }
  1743   1743         break;
  1744   1744       }
  1745   1745       case TK_EXCEPT:
................................................................................
  3880   3880   #ifndef SQLITE_OMIT_EXPLAIN
  3881   3881   static void explainSimpleCount(
  3882   3882     Parse *pParse,                  /* Parse context */
  3883   3883     Table *pTab,                    /* Table being queried */
  3884   3884     Index *pIdx                     /* Index used to optimize scan, or NULL */
  3885   3885   ){
  3886   3886     if( pParse->explain==2 ){
  3887         -    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)",
         3887  +    char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
  3888   3888           pTab->zName, 
  3889         -        pIdx ? "USING COVERING INDEX " : "",
  3890         -        pIdx ? pIdx->zName : "",
  3891         -        pTab->nRowEst
         3889  +        pIdx ? " USING COVERING INDEX " : "",
         3890  +        pIdx ? pIdx->zName : ""
  3892   3891       );
  3893   3892       sqlite3VdbeAddOp4(
  3894   3893           pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
  3895   3894       );
  3896   3895     }
  3897   3896   }
  3898   3897   #else
................................................................................
  4235   4234     if( pDest->eDest==SRT_EphemTab ){
  4236   4235       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
  4237   4236     }
  4238   4237   
  4239   4238     /* Set the limiter.
  4240   4239     */
  4241   4240     iEnd = sqlite3VdbeMakeLabel(v);
  4242         -  p->nSelectRow = (double)LARGEST_INT64;
         4241  +  p->nSelectRow = LARGEST_INT64;
  4243   4242     computeLimitRegisters(pParse, p, iEnd);
  4244   4243     if( p->iLimit==0 && addrSortIndex>=0 ){
  4245   4244       sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
  4246   4245       p->selFlags |= SF_UseSorter;
  4247   4246     }
  4248   4247   
  4249   4248     /* Open a virtual index to use for the distinct set.
................................................................................
  4263   4262     if( !isAgg && pGroupBy==0 ){
  4264   4263       /* No aggregate functions and no GROUP BY clause */
  4265   4264       ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
  4266   4265   
  4267   4266       /* Begin the database scan. */
  4268   4267       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
  4269   4268       if( pWInfo==0 ) goto select_end;
  4270         -    if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
  4271         -    if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
  4272         -    if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
         4269  +    if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
         4270  +      p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
         4271  +    }
         4272  +    if( sqlite3WhereIsDistinct(pWInfo) ){
         4273  +      sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
         4274  +    }
         4275  +    if( pOrderBy && sqlite3WhereIsOrdered(pWInfo) ) pOrderBy = 0;
  4273   4276   
  4274   4277       /* If sorting index that was created by a prior OP_OpenEphemeral 
  4275   4278       ** instruction ended up not being needed, then change the OP_OpenEphemeral
  4276   4279       ** into an OP_Noop.
  4277   4280       */
  4278   4281       if( addrSortIndex>=0 && pOrderBy==0 ){
  4279   4282         sqlite3VdbeChangeToNoop(v, addrSortIndex);
  4280   4283         p->addrOpenEphm[2] = -1;
  4281   4284       }
  4282   4285   
  4283   4286       /* Use the standard inner loop. */
  4284   4287       selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
  4285         -                    pWInfo->iContinue, pWInfo->iBreak);
         4288  +                    sqlite3WhereContinueLabel(pWInfo),
         4289  +                    sqlite3WhereBreakLabel(pWInfo));
  4286   4290   
  4287   4291       /* End the database scan loop.
  4288   4292       */
  4289   4293       sqlite3WhereEnd(pWInfo);
  4290   4294     }else{
  4291   4295       /* This case when there exist aggregate functions or a GROUP BY clause
  4292   4296       ** or both */
................................................................................
  4311   4315   
  4312   4316         for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
  4313   4317           pItem->iAlias = 0;
  4314   4318         }
  4315   4319         for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
  4316   4320           pItem->iAlias = 0;
  4317   4321         }
  4318         -      if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100;
         4322  +      if( p->nSelectRow>100 ) p->nSelectRow = 100;
  4319   4323       }else{
  4320         -      p->nSelectRow = (double)1;
         4324  +      p->nSelectRow = 1;
  4321   4325       }
  4322   4326   
  4323   4327    
  4324   4328       /* Create a label to jump to when we want to abort the query */
  4325   4329       addrEnd = sqlite3VdbeMakeLabel(v);
  4326   4330   
  4327   4331       /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
................................................................................
  4393   4397   
  4394   4398         /* Begin a loop that will extract all source rows in GROUP BY order.
  4395   4399         ** This might involve two separate loops with an OP_Sort in between, or
  4396   4400         ** it might be a single loop that uses an index to extract information
  4397   4401         ** in the right order to begin with.
  4398   4402         */
  4399   4403         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
  4400         -      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
         4404  +      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 
         4405  +                                 WHERE_GROUPBY, 0);
  4401   4406         if( pWInfo==0 ) goto select_end;
  4402         -      if( pWInfo->nOBSat==pGroupBy->nExpr ){
         4407  +      if( sqlite3WhereIsOrdered(pWInfo) ){
  4403   4408           /* The optimizer is able to deliver rows in group by order so
  4404   4409           ** we do not have to sort.  The OP_OpenEphemeral table will be
  4405   4410           ** cancelled later because we still need to use the pKeyInfo
  4406   4411           */
  4407   4412           groupBySort = 0;
  4408   4413         }else{
  4409   4414           /* Rows are coming out in undetermined order.  We have to push
................................................................................
  4676   4681           pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
  4677   4682           if( pWInfo==0 ){
  4678   4683             sqlite3ExprListDelete(db, pDel);
  4679   4684             goto select_end;
  4680   4685           }
  4681   4686           updateAccumulator(pParse, &sAggInfo);
  4682   4687           assert( pMinMax==0 || pMinMax->nExpr==1 );
  4683         -        if( pWInfo->nOBSat>0 ){
  4684         -          sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
         4688  +        if( sqlite3WhereIsOrdered(pWInfo) ){
         4689  +          sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
  4685   4690             VdbeComment((v, "%s() by index",
  4686   4691                   (flag==WHERE_ORDERBY_MIN?"min":"max")));
  4687   4692           }
  4688   4693           sqlite3WhereEnd(pWInfo);
  4689   4694           finalizeAggFunctions(pParse, &sAggInfo);
  4690   4695         }
  4691   4696   

Changes to src/sqliteInt.h.

   391    391   ** GCC does not define the offsetof() macro so we'll have to do it
   392    392   ** ourselves.
   393    393   */
   394    394   #ifndef offsetof
   395    395   #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
   396    396   #endif
   397    397   
          398  +/*
          399  +** Macros to compute minimum and maximum of two numbers.
          400  +*/
          401  +#define MIN(A,B) ((A)<(B)?(A):(B))
          402  +#define MAX(A,B) ((A)>(B)?(A):(B))
          403  +
   398    404   /*
   399    405   ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
   400    406   ** not, there are still machines out there that use EBCDIC.)
   401    407   */
   402    408   #if 'A' == '\301'
   403    409   # define SQLITE_EBCDIC 1
   404    410   #else
................................................................................
   716    722   typedef struct Trigger Trigger;
   717    723   typedef struct TriggerPrg TriggerPrg;
   718    724   typedef struct TriggerStep TriggerStep;
   719    725   typedef struct UnpackedRecord UnpackedRecord;
   720    726   typedef struct VTable VTable;
   721    727   typedef struct VtabCtx VtabCtx;
   722    728   typedef struct Walker Walker;
   723         -typedef struct WherePlan WherePlan;
   724    729   typedef struct WhereInfo WhereInfo;
   725         -typedef struct WhereLevel WhereLevel;
   726    730   
   727    731   /*
   728    732   ** Defer sourcing vdbe.h and btree.h until after the "u8" and 
   729    733   ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
   730    734   ** pointer types (i.e. FuncDef) defined above.
   731    735   */
   732    736   #include "btree.h"
................................................................................
  1541   1545     u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
  1542   1546     char **azColl;           /* Array of collation sequence names for index */
  1543   1547     int tnum;                /* DB Page containing root of this index */
  1544   1548     u16 nColumn;             /* Number of columns in table used by this index */
  1545   1549     u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  1546   1550     unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  1547   1551     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
         1552  +  unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  1548   1553   #ifdef SQLITE_ENABLE_STAT3
  1549   1554     int nSample;             /* Number of elements in aSample[] */
  1550   1555     tRowcnt avgEq;           /* Average nEq value for key values not in aSample */
  1551   1556     IndexSample *aSample;    /* Samples of the left-most key */
  1552   1557   #endif
  1553   1558   };
  1554   1559   
................................................................................
  1886   1891   typedef u64 Bitmask;
  1887   1892   
  1888   1893   /*
  1889   1894   ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
  1890   1895   */
  1891   1896   #define BMS  ((int)(sizeof(Bitmask)*8))
  1892   1897   
         1898  +/*
         1899  +** A bit in a Bitmask
         1900  +*/
         1901  +#define MASKBIT(n)   (((Bitmask)1)<<(n))
         1902  +
  1893   1903   /*
  1894   1904   ** The following structure describes the FROM clause of a SELECT statement.
  1895   1905   ** Each table or subquery in the FROM clause is a separate element of
  1896   1906   ** the SrcList.a[] array.
  1897   1907   **
  1898   1908   ** With the addition of multiple database support, the following structure
  1899   1909   ** can also be used to describe a particular table such as the table that
................................................................................
  1906   1916   ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
  1907   1917   ** jointype expresses the join between the table and the previous table.
  1908   1918   **
  1909   1919   ** In the colUsed field, the high-order bit (bit 63) is set if the table
  1910   1920   ** contains more than 63 columns and the 64-th or later column is used.
  1911   1921   */
  1912   1922   struct SrcList {
  1913         -  i16 nSrc;        /* Number of tables or subqueries in the FROM clause */
  1914         -  i16 nAlloc;      /* Number of entries allocated in a[] below */
         1923  +  u8 nSrc;        /* Number of tables or subqueries in the FROM clause */
         1924  +  u8 nAlloc;      /* Number of entries allocated in a[] below */
  1915   1925     struct SrcList_item {
  1916   1926       Schema *pSchema;  /* Schema to which this item is fixed */
  1917   1927       char *zDatabase;  /* Name of database holding this table */
  1918   1928       char *zName;      /* Name of the table */
  1919   1929       char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
  1920   1930       Table *pTab;      /* An SQL table corresponding to zName */
  1921   1931       Select *pSelect;  /* A SELECT statement used in place of a table name */
................................................................................
  1945   1955   #define JT_NATURAL   0x0004    /* True for a "natural" join */
  1946   1956   #define JT_LEFT      0x0008    /* Left outer join */
  1947   1957   #define JT_RIGHT     0x0010    /* Right outer join */
  1948   1958   #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
  1949   1959   #define JT_ERROR     0x0040    /* unknown or unsupported join type */
  1950   1960   
  1951   1961   
  1952         -/*
  1953         -** A WherePlan object holds information that describes a lookup
  1954         -** strategy.
  1955         -**
  1956         -** This object is intended to be opaque outside of the where.c module.
  1957         -** It is included here only so that that compiler will know how big it
  1958         -** is.  None of the fields in this object should be used outside of
  1959         -** the where.c module.
  1960         -**
  1961         -** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
  1962         -** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
  1963         -** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
  1964         -** case that more than one of these conditions is true.
  1965         -*/
  1966         -struct WherePlan {
  1967         -  u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
  1968         -  u16 nEq;                       /* Number of == constraints */
  1969         -  u16 nOBSat;                    /* Number of ORDER BY terms satisfied */
  1970         -  double nRow;                   /* Estimated number of rows (for EQP) */
  1971         -  union {
  1972         -    Index *pIdx;                   /* Index when WHERE_INDEXED is true */
  1973         -    struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
  1974         -    sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
  1975         -  } u;
  1976         -};
  1977         -
  1978         -/*
  1979         -** For each nested loop in a WHERE clause implementation, the WhereInfo
  1980         -** structure contains a single instance of this structure.  This structure
  1981         -** is intended to be private to the where.c module and should not be
  1982         -** access or modified by other modules.
  1983         -**
  1984         -** The pIdxInfo field is used to help pick the best index on a
  1985         -** virtual table.  The pIdxInfo pointer contains indexing
  1986         -** information for the i-th table in the FROM clause before reordering.
  1987         -** All the pIdxInfo pointers are freed by whereInfoFree() in where.c.
  1988         -** All other information in the i-th WhereLevel object for the i-th table
  1989         -** after FROM clause ordering.
  1990         -*/
  1991         -struct WhereLevel {
  1992         -  WherePlan plan;       /* query plan for this element of the FROM clause */
  1993         -  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
  1994         -  int iTabCur;          /* The VDBE cursor used to access the table */
  1995         -  int iIdxCur;          /* The VDBE cursor used to access pIdx */
  1996         -  int addrBrk;          /* Jump here to break out of the loop */
  1997         -  int addrNxt;          /* Jump here to start the next IN combination */
  1998         -  int addrCont;         /* Jump here to continue with the next loop cycle */
  1999         -  int addrFirst;        /* First instruction of interior of the loop */
  2000         -  u8 iFrom;             /* Which entry in the FROM clause */
  2001         -  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
  2002         -  int p1, p2;           /* Operands of the opcode used to ends the loop */
  2003         -  union {               /* Information that depends on plan.wsFlags */
  2004         -    struct {
  2005         -      int nIn;              /* Number of entries in aInLoop[] */
  2006         -      struct InLoop {
  2007         -        int iCur;              /* The VDBE cursor used by this IN operator */
  2008         -        int addrInTop;         /* Top of the IN loop */
  2009         -        u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
  2010         -      } *aInLoop;           /* Information about each nested IN operator */
  2011         -    } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
  2012         -    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
  2013         -  } u;
  2014         -  double rOptCost;      /* "Optimal" cost for this level */
  2015         -
  2016         -  /* The following field is really not part of the current level.  But
  2017         -  ** we need a place to cache virtual table index information for each
  2018         -  ** virtual table in the FROM clause and the WhereLevel structure is
  2019         -  ** a convenient place since there is one WhereLevel for each FROM clause
  2020         -  ** element.
  2021         -  */
  2022         -  sqlite3_index_info *pIdxInfo;  /* Index info for n-th source table */
  2023         -};
  2024         -
  2025   1962   /*
  2026   1963   ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
  2027   1964   ** and the WhereInfo.wctrlFlags member.
  2028   1965   */
  2029   1966   #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
  2030   1967   #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
  2031   1968   #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
  2032   1969   #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
  2033   1970   #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
  2034   1971   #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
  2035   1972   #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
  2036   1973   #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
  2037   1974   #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
         1975  +#define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
         1976  +#define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
  2038   1977   
  2039         -/*
  2040         -** The WHERE clause processing routine has two halves.  The
  2041         -** first part does the start of the WHERE loop and the second
  2042         -** half does the tail of the WHERE loop.  An instance of
  2043         -** this structure is returned by the first half and passed
  2044         -** into the second half to give some continuity.
         1978  +/* Allowed return values from sqlite3WhereIsDistinct()
  2045   1979   */
  2046         -struct WhereInfo {
  2047         -  Parse *pParse;            /* Parsing and code generating context */
  2048         -  SrcList *pTabList;        /* List of tables in the join */
  2049         -  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
  2050         -  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
  2051         -  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
  2052         -  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
  2053         -  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
  2054         -  int iTop;                 /* The very beginning of the WHERE loop */
  2055         -  int iContinue;            /* Jump here to continue with next record */
  2056         -  int iBreak;               /* Jump here to break out of the loop */
  2057         -  int nLevel;               /* Number of nested loop */
  2058         -  struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
  2059         -  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
  2060         -  double nRowOut;           /* Estimated number of output rows */
  2061         -  WhereLevel a[1];          /* Information about each nest loop in WHERE */
  2062         -};
  2063         -
  2064         -/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
  2065   1980   #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
  2066   1981   #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
  2067   1982   #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
  2068   1983   #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
  2069   1984   
  2070   1985   /*
  2071   1986   ** A NameContext defines a context in which to resolve table and column
................................................................................
  2131   2046   */
  2132   2047   struct Select {
  2133   2048     ExprList *pEList;      /* The fields of the result */
  2134   2049     u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  2135   2050     u16 selFlags;          /* Various SF_* values */
  2136   2051     int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  2137   2052     int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
  2138         -  double nSelectRow;     /* Estimated number of result rows */
         2053  +  u64 nSelectRow;        /* Estimated number of result rows */
  2139   2054     SrcList *pSrc;         /* The FROM clause */
  2140   2055     Expr *pWhere;          /* The WHERE clause */
  2141   2056     ExprList *pGroupBy;    /* The GROUP BY clause */
  2142   2057     Expr *pHaving;         /* The HAVING clause */
  2143   2058     ExprList *pOrderBy;    /* The ORDER BY clause */
  2144   2059     Select *pPrior;        /* Prior select in a compound select statement */
  2145   2060     Select *pNext;         /* Next select to the left in a compound */
................................................................................
  2315   2230     TableLock *aTableLock; /* Required table locks for shared-cache mode */
  2316   2231   #endif
  2317   2232     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
  2318   2233   
  2319   2234     /* Information used while coding trigger programs. */
  2320   2235     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
  2321   2236     Table *pTriggerTab;  /* Table triggers are being coded for */
  2322         -  double nQueryLoop;   /* Estimated number of iterations of a query */
         2237  +  u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
  2323   2238     u32 oldmask;         /* Mask of old.* columns referenced */
  2324   2239     u32 newmask;         /* Mask of new.* columns referenced */
  2325   2240     u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
  2326   2241     u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
  2327   2242     u8 disableTriggers;  /* True to disable triggers */
  2328   2243   
  2329   2244     /* Above is constant between recursions.  Below is reset before and after
................................................................................
  2885   2800   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
  2886   2801   Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
  2887   2802   #endif
  2888   2803   void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  2889   2804   void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  2890   2805   WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  2891   2806   void sqlite3WhereEnd(WhereInfo*);
         2807  +u64 sqlite3WhereOutputRowCount(WhereInfo*);
         2808  +int sqlite3WhereIsDistinct(WhereInfo*);
         2809  +int sqlite3WhereIsOrdered(WhereInfo*);
         2810  +int sqlite3WhereContinueLabel(WhereInfo*);
         2811  +int sqlite3WhereBreakLabel(WhereInfo*);
         2812  +int sqlite3WhereOkOnePass(WhereInfo*);
  2892   2813   int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  2893   2814   void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  2894   2815   void sqlite3ExprCodeMove(Parse*, int, int, int);
  2895   2816   void sqlite3ExprCacheStore(Parse*, int, int, int);
  2896   2817   void sqlite3ExprCachePush(Parse*);
  2897   2818   void sqlite3ExprCachePop(Parse*, int);
  2898   2819   void sqlite3ExprCacheRemove(Parse*, int, int);

Changes to src/test1.c.

  6298   6298   #endif
  6299   6299   #ifdef SQLITE_DEBUG
  6300   6300     extern int sqlite3WhereTrace;
  6301   6301     extern int sqlite3OSTrace;
  6302   6302     extern int sqlite3WalTrace;
  6303   6303   #endif
  6304   6304   #ifdef SQLITE_TEST
  6305         -  extern char sqlite3_query_plan[];
  6306         -  static char *query_plan = sqlite3_query_plan;
  6307   6305   #ifdef SQLITE_ENABLE_FTS3
  6308   6306     extern int sqlite3_fts3_enable_parentheses;
  6309   6307   #endif
  6310   6308   #endif
  6311   6309   
  6312   6310     for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
  6313   6311       Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
................................................................................
  6353   6351         (char*)&pzNeededCollation, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
  6354   6352   #endif
  6355   6353   #if SQLITE_OS_WIN
  6356   6354     Tcl_LinkVar(interp, "sqlite_os_type",
  6357   6355         (char*)&sqlite3_os_type, TCL_LINK_INT);
  6358   6356   #endif
  6359   6357   #ifdef SQLITE_TEST
  6360         -  Tcl_LinkVar(interp, "sqlite_query_plan",
  6361         -      (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
         6358  +  {
         6359  +    static const char *query_plan = "*** OBSOLETE VARIABLE ***";
         6360  +    Tcl_LinkVar(interp, "sqlite_query_plan",
         6361  +       (char*)&query_plan, TCL_LINK_STRING|TCL_LINK_READ_ONLY);
         6362  +  }
  6362   6363   #endif
  6363   6364   #ifdef SQLITE_DEBUG
  6364   6365     Tcl_LinkVar(interp, "sqlite_where_trace",
  6365   6366         (char*)&sqlite3WhereTrace, TCL_LINK_INT);
  6366   6367     Tcl_LinkVar(interp, "sqlite_os_trace",
  6367   6368         (char*)&sqlite3OSTrace, TCL_LINK_INT);
  6368   6369   #ifndef SQLITE_OMIT_WAL

Changes to src/update.c.

   314    314     /* Begin the database scan
   315    315     */
   316    316     sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
   317    317     pWInfo = sqlite3WhereBegin(
   318    318         pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, 0
   319    319     );
   320    320     if( pWInfo==0 ) goto update_cleanup;
   321         -  okOnePass = pWInfo->okOnePass;
          321  +  okOnePass = sqlite3WhereOkOnePass(pWInfo);
   322    322   
   323    323     /* Remember the rowid of every item to be updated.
   324    324     */
   325    325     sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
   326    326     if( !okOnePass ){
   327    327       sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
   328    328     }

Changes to src/where.c.

    23     23   ** Trace output macros
    24     24   */
    25     25   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    26     26   /***/ int sqlite3WhereTrace = 0;
    27     27   #endif
    28     28   #if defined(SQLITE_DEBUG) \
    29     29       && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
    30         -# define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
           30  +# define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
           31  +# define WHERETRACE_ENABLED 1
    31     32   #else
    32         -# define WHERETRACE(X)
           33  +# define WHERETRACE(K,X)
    33     34   #endif
    34     35   
    35     36   /* Forward reference
    36     37   */
    37     38   typedef struct WhereClause WhereClause;
    38     39   typedef struct WhereMaskSet WhereMaskSet;
    39     40   typedef struct WhereOrInfo WhereOrInfo;
    40     41   typedef struct WhereAndInfo WhereAndInfo;
    41         -typedef struct WhereCost WhereCost;
           42  +typedef struct WhereLevel WhereLevel;
           43  +typedef struct WhereLoop WhereLoop;
           44  +typedef struct WherePath WherePath;
           45  +typedef struct WhereTerm WhereTerm;
           46  +typedef struct WhereLoopBuilder WhereLoopBuilder;
           47  +typedef struct WhereScan WhereScan;
           48  +
           49  +/*
           50  +** Cost X is tracked as 10*log2(X) stored in a 16-bit integer.  The
           51  +** maximum cost for ordinary tables is 64*(2**63) which becomes 6900.
           52  +** (Virtual tables can return a larger cost, but let's assume they do not.)
           53  +** So all costs can be stored in a 16-bit unsigned integer without risk
           54  +** of overflow.
           55  +**
           56  +** Costs are estimates, so don't go to the computational trouble to compute
           57  +** 10*log2(X) exactly.  Instead, a close estimate is used.  Any value of
           58  +** X<=1 is stored as 0.  X=2 is 10.  X=3 is 16.  X=1000 is 99. etc.
           59  +**
           60  +** The tool/wherecosttest.c source file implements a command-line program
           61  +** that will convert between WhereCost to integers and do addition and
           62  +** multiplication on WhereCost values.  That command-line program is a
           63  +** useful utility to have around when working with this module.
           64  +*/
           65  +typedef unsigned short int WhereCost;
           66  +
           67  +/*
           68  +** This object contains information needed to implement a single nested
           69  +** loop in WHERE clause.
           70  +**
           71  +** Contrast this object with WhereLoop.  This object describes the
           72  +** implementation of the loop.  WhereLoop describes the algorithm.
           73  +** This object contains a pointer to the WhereLoop algorithm as one of
           74  +** its elements.
           75  +**
           76  +** The WhereInfo object contains a single instance of this object for
           77  +** each term in the FROM clause (which is to say, for each of the
           78  +** nested loops as implemented).  The order of WhereLevel objects determines
           79  +** the loop nested order, with WhereInfo.a[0] being the outer loop and
           80  +** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
           81  +*/
           82  +struct WhereLevel {
           83  +  int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
           84  +  int iTabCur;          /* The VDBE cursor used to access the table */
           85  +  int iIdxCur;          /* The VDBE cursor used to access pIdx */
           86  +  int addrBrk;          /* Jump here to break out of the loop */
           87  +  int addrNxt;          /* Jump here to start the next IN combination */
           88  +  int addrCont;         /* Jump here to continue with the next loop cycle */
           89  +  int addrFirst;        /* First instruction of interior of the loop */
           90  +  u8 iFrom;             /* Which entry in the FROM clause */
           91  +  u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
           92  +  int p1, p2;           /* Operands of the opcode used to ends the loop */
           93  +  union {               /* Information that depends on pWLoop->wsFlags */
           94  +    struct {
           95  +      int nIn;              /* Number of entries in aInLoop[] */
           96  +      struct InLoop {
           97  +        int iCur;              /* The VDBE cursor used by this IN operator */
           98  +        int addrInTop;         /* Top of the IN loop */
           99  +        u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
          100  +      } *aInLoop;           /* Information about each nested IN operator */
          101  +    } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
          102  +    Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
          103  +  } u;
          104  +  struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
          105  +};
          106  +
          107  +/*
          108  +** Each instance of this object represents an algorithm for evaluating one
          109  +** term of a join.  Every term of the FROM clause will have at least
          110  +** one corresponding WhereLoop object (unless INDEXED BY constraints
          111  +** prevent a query solution - which is an error) and many terms of the
          112  +** FROM clause will have multiple WhereLoop objects, each describing a
          113  +** potential way of implementing that FROM-clause term, together with
          114  +** dependencies and cost estimates for using the chosen algorithm.
          115  +**
          116  +** Query planning consists of building up a collection of these WhereLoop
          117  +** objects, then computing a particular sequence of WhereLoop objects, with
          118  +** one WhereLoop object per FROM clause term, that satisfy all dependencies
          119  +** and that minimize the overall cost.
          120  +*/
          121  +struct WhereLoop {
          122  +  Bitmask prereq;       /* Bitmask of other loops that must run first */
          123  +  Bitmask maskSelf;     /* Bitmask identifying table iTab */
          124  +#ifdef SQLITE_DEBUG
          125  +  char cId;             /* Symbolic ID of this loop for debugging use */
          126  +#endif
          127  +  u8 iTab;              /* Position in FROM clause of table for this loop */
          128  +  u8 iSortIdx;          /* Sorting index number.  0==None */
          129  +  WhereCost rSetup;     /* One-time setup cost (ex: create transient index) */
          130  +  WhereCost rRun;       /* Cost of running each loop */
          131  +  WhereCost nOut;       /* Estimated number of output rows */
          132  +  union {
          133  +    struct {               /* Information for internal btree tables */
          134  +      int nEq;               /* Number of equality constraints */
          135  +      Index *pIndex;         /* Index used, or NULL */
          136  +    } btree;
          137  +    struct {               /* Information for virtual tables */
          138  +      int idxNum;            /* Index number */
          139  +      u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
          140  +      u8 isOrdered;          /* True if satisfies ORDER BY */
          141  +      u16 omitMask;          /* Terms that may be omitted */
          142  +      char *idxStr;          /* Index identifier string */
          143  +    } vtab;
          144  +  } u;
          145  +  u32 wsFlags;          /* WHERE_* flags describing the plan */
          146  +  u16 nLTerm;           /* Number of entries in aLTerm[] */
          147  +  /**** whereLoopXfer() copies fields above ***********************/
          148  +# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
          149  +  u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
          150  +  WhereTerm **aLTerm;   /* WhereTerms used */
          151  +  WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
          152  +  WhereTerm *aLTermSpace[4];  /* Initial aLTerm[] space */
          153  +};
          154  +
          155  +/* Forward declaration of methods */
          156  +static int whereLoopResize(sqlite3*, WhereLoop*, int);
          157  +
          158  +/*
          159  +** Each instance of this object holds a sequence of WhereLoop objects
          160  +** that implement some or all of a query plan.
          161  +**
          162  +** Think of each WhereLoop objects as a node in a graph, which arcs
          163  +** showing dependences and costs for travelling between nodes.  (That is
          164  +** not a completely accurate description because WhereLoop costs are a
          165  +** vector, not a scalar, and because dependences are many-to-one, not
          166  +** one-to-one as are graph nodes.  But it is a useful visualization aid.)
          167  +** Then a WherePath object is a path through the graph that visits some
          168  +** or all of the WhereLoop objects once.
          169  +**
          170  +** The "solver" works by creating the N best WherePath objects of length
          171  +** 1.  Then using those as a basis to compute the N best WherePath objects
          172  +** of length 2.  And so forth until the length of WherePaths equals the
          173  +** number of nodes in the FROM clause.  The best (lowest cost) WherePath
          174  +** at the end is the choosen query plan.
          175  +*/
          176  +struct WherePath {
          177  +  Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
          178  +  Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
          179  +  WhereCost nRow;       /* Estimated number of rows generated by this path */
          180  +  WhereCost rCost;      /* Total cost of this path */
          181  +  u8 isOrdered;         /* True if this path satisfies ORDER BY */
          182  +  u8 isOrderedValid;    /* True if the isOrdered field is valid */
          183  +  WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
          184  +};
    42    185   
    43    186   /*
    44    187   ** The query generator uses an array of instances of this structure to
    45    188   ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
    46    189   ** clause subexpression is separated from the others by AND operators,
    47    190   ** usually, or sometimes subexpressions separated by OR.
    48    191   **
................................................................................
    87    230   ** bits in the Bitmask.  So, in the example above, the cursor numbers
    88    231   ** would be mapped into integers 0 through 7.
    89    232   **
    90    233   ** The number of terms in a join is limited by the number of bits
    91    234   ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
    92    235   ** is only able to process joins with 64 or fewer tables.
    93    236   */
    94         -typedef struct WhereTerm WhereTerm;
    95    237   struct WhereTerm {
    96    238     Expr *pExpr;            /* Pointer to the subexpression that is this term */
    97    239     int iParent;            /* Disable pWC->a[iParent] when this term disabled */
    98    240     int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
    99    241     union {
   100    242       int leftColumn;         /* Column number of X in "X <op> <expr>" */
   101    243       WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
................................................................................
   121    263   #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
   122    264   #ifdef SQLITE_ENABLE_STAT3
   123    265   #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
   124    266   #else
   125    267   #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
   126    268   #endif
   127    269   
          270  +/*
          271  +** An instance of the WhereScan object is used as an iterator for locating
          272  +** terms in the WHERE clause that are useful to the query planner.
          273  +*/
          274  +struct WhereScan {
          275  +  WhereClause *pOrigWC;      /* Original, innermost WhereClause */
          276  +  WhereClause *pWC;          /* WhereClause currently being scanned */
          277  +  char *zCollName;           /* Required collating sequence, if not NULL */
          278  +  char idxaff;               /* Must match this affinity, if zCollName!=NULL */
          279  +  unsigned char nEquiv;      /* Number of entries in aEquiv[] */
          280  +  unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
          281  +  u32 opMask;                /* Acceptable operators */
          282  +  int k;                     /* Resume scanning at this->pWC->a[this->k] */
          283  +  int aEquiv[22];            /* Cursor,Column pairs for equivalence classes */
          284  +};
          285  +
   128    286   /*
   129    287   ** An instance of the following structure holds all information about a
   130    288   ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
   131    289   **
   132    290   ** Explanation of pOuter:  For a WHERE clause of the form
   133    291   **
   134    292   **           a AND ((b AND c) OR (d AND e)) AND f
   135    293   **
   136    294   ** There are separate WhereClause objects for the whole clause and for
   137    295   ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
   138    296   ** subclauses points to the WhereClause object for the whole clause.
   139    297   */
   140    298   struct WhereClause {
   141         -  Parse *pParse;           /* The parser context */
   142         -  WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
          299  +  WhereInfo *pWInfo;       /* WHERE clause processing context */
   143    300     WhereClause *pOuter;     /* Outer conjunction */
   144    301     u8 op;                   /* Split operator.  TK_AND or TK_OR */
   145         -  u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
   146    302     int nTerm;               /* Number of terms */
   147    303     int nSlot;               /* Number of entries in a[] */
   148    304     WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
   149    305   #if defined(SQLITE_SMALL_STACK)
   150    306     WhereTerm aStatic[1];    /* Initial static space for a[] */
   151    307   #else
   152    308     WhereTerm aStatic[8];    /* Initial static space for a[] */
................................................................................
   198    354   */
   199    355   struct WhereMaskSet {
   200    356     int n;                        /* Number of assigned cursor values */
   201    357     int ix[BMS];                  /* Cursor assigned to each bit */
   202    358   };
   203    359   
   204    360   /*
   205         -** A WhereCost object records a lookup strategy and the estimated
   206         -** cost of pursuing that strategy.
          361  +** This object is a convenience wrapper holding all information needed
          362  +** to construct WhereLoop objects for a particular query.
   207    363   */
   208         -struct WhereCost {
   209         -  WherePlan plan;    /* The lookup strategy */
   210         -  double rCost;      /* Overall cost of pursuing this search strategy */
   211         -  Bitmask used;      /* Bitmask of cursors used by this plan */
          364  +struct WhereLoopBuilder {
          365  +  WhereInfo *pWInfo;        /* Information about this WHERE */
          366  +  WhereClause *pWC;         /* WHERE clause terms */
          367  +  ExprList *pOrderBy;       /* ORDER BY clause */
          368  +  WhereLoop *pNew;          /* Template WhereLoop */
          369  +  WhereLoop *pBest;         /* If non-NULL, store single best loop here */
          370  +};
          371  +
          372  +/*
          373  +** The WHERE clause processing routine has two halves.  The
          374  +** first part does the start of the WHERE loop and the second
          375  +** half does the tail of the WHERE loop.  An instance of
          376  +** this structure is returned by the first half and passed
          377  +** into the second half to give some continuity.
          378  +**
          379  +** An instance of this object holds the complete state of the query
          380  +** planner.
          381  +*/
          382  +struct WhereInfo {
          383  +  Parse *pParse;            /* Parsing and code generating context */
          384  +  SrcList *pTabList;        /* List of tables in the join */
          385  +  ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
          386  +  ExprList *pDistinct;      /* DISTINCT ON values, or NULL */
          387  +  WhereLoop *pLoops;        /* List of all WhereLoop objects */
          388  +  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
          389  +  WhereCost nRowOut;        /* Estimated number of output rows */
          390  +  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
          391  +  u8 bOBSat;                /* ORDER BY satisfied by indices */
          392  +  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
          393  +  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
          394  +  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
          395  +  int iTop;                 /* The very beginning of the WHERE loop */
          396  +  int iContinue;            /* Jump here to continue with next record */
          397  +  int iBreak;               /* Jump here to break out of the loop */
          398  +  int nLevel;               /* Number of nested loop */
          399  +  int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
          400  +  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
          401  +  WhereClause sWC;          /* Decomposition of the WHERE clause */
          402  +  WhereLevel a[1];          /* Information about each nest loop in WHERE */
   212    403   };
   213    404   
   214    405   /*
   215         -** Bitmasks for the operators that indices are able to exploit.  An
          406  +** Bitmasks for the operators on WhereTerm objects.  These are all
          407  +** operators that are of interest to the query planner.  An
   216    408   ** OR-ed combination of these values can be used when searching for
   217         -** terms in the where clause.
          409  +** particular WhereTerms within a WhereClause.
   218    410   */
   219    411   #define WO_IN     0x001
   220    412   #define WO_EQ     0x002
   221    413   #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
   222    414   #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
   223    415   #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
   224    416   #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
................................................................................
   229    421   #define WO_EQUIV  0x400       /* Of the form A==B, both columns */
   230    422   #define WO_NOOP   0x800       /* This term does not restrict search space */
   231    423   
   232    424   #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
   233    425   #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
   234    426   
   235    427   /*
   236         -** Value for wsFlags returned by bestIndex() and stored in
   237         -** WhereLevel.wsFlags.  These flags determine which search
   238         -** strategies are appropriate.
   239         -**
   240         -** The least significant 12 bits is reserved as a mask for WO_ values above.
   241         -** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
   242         -** But if the table is the right table of a left join, WhereLevel.wsFlags
   243         -** is set to WO_IN|WO_EQ.  The WhereLevel.wsFlags field can then be used as
   244         -** the "op" parameter to findTerm when we are resolving equality constraints.
   245         -** ISNULL constraints will then not be used on the right table of a left
   246         -** join.  Tickets #2177 and #2189.
   247         -*/
   248         -#define WHERE_ROWID_EQ     0x00001000  /* rowid=EXPR or rowid IN (...) */
   249         -#define WHERE_ROWID_RANGE  0x00002000  /* rowid<EXPR and/or rowid>EXPR */
   250         -#define WHERE_COLUMN_EQ    0x00010000  /* x=EXPR or x IN (...) or x IS NULL */
   251         -#define WHERE_COLUMN_RANGE 0x00020000  /* x<EXPR and/or x>EXPR */
   252         -#define WHERE_COLUMN_IN    0x00040000  /* x IN (...) */
   253         -#define WHERE_COLUMN_NULL  0x00080000  /* x IS NULL */
   254         -#define WHERE_INDEXED      0x000f0000  /* Anything that uses an index */
   255         -#define WHERE_NOT_FULLSCAN 0x100f3000  /* Does not do a full table scan */
   256         -#define WHERE_IN_ABLE      0x080f1000  /* Able to support an IN operator */
   257         -#define WHERE_TOP_LIMIT    0x00100000  /* x<EXPR or x<=EXPR constraint */
   258         -#define WHERE_BTM_LIMIT    0x00200000  /* x>EXPR or x>=EXPR constraint */
   259         -#define WHERE_BOTH_LIMIT   0x00300000  /* Both x>EXPR and x<EXPR */
   260         -#define WHERE_IDX_ONLY     0x00400000  /* Use index only - omit table */
   261         -#define WHERE_ORDERED      0x00800000  /* Output will appear in correct order */
   262         -#define WHERE_REVERSE      0x01000000  /* Scan in reverse order */
   263         -#define WHERE_UNIQUE       0x02000000  /* Selects no more than one row */
   264         -#define WHERE_ALL_UNIQUE   0x04000000  /* This and all prior have one row */
   265         -#define WHERE_OB_UNIQUE    0x00004000  /* Values in ORDER BY columns are 
   266         -                                       ** different for every output row */
   267         -#define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
   268         -#define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
   269         -#define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
   270         -#define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
   271         -#define WHERE_COVER_SCAN   0x80000000  /* Full scan of a covering index */
   272         -
   273         -/*
   274         -** This module contains many separate subroutines that work together to
   275         -** find the best indices to use for accessing a particular table in a query.
   276         -** An instance of the following structure holds context information about the
   277         -** index search so that it can be more easily passed between the various
   278         -** routines.
   279         -*/
   280         -typedef struct WhereBestIdx WhereBestIdx;
   281         -struct WhereBestIdx {
   282         -  Parse *pParse;                  /* Parser context */
   283         -  WhereClause *pWC;               /* The WHERE clause */
   284         -  struct SrcList_item *pSrc;      /* The FROM clause term to search */
   285         -  Bitmask notReady;               /* Mask of cursors not available */
   286         -  Bitmask notValid;               /* Cursors not available for any purpose */
   287         -  ExprList *pOrderBy;             /* The ORDER BY clause */
   288         -  ExprList *pDistinct;            /* The select-list if query is DISTINCT */
   289         -  sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
   290         -  int i, n;                       /* Which loop is being coded; # of loops */
   291         -  WhereLevel *aLevel;             /* Info about outer loops */
   292         -  WhereCost cost;                 /* Lowest cost query plan */
   293         -};
   294         -
   295         -/*
   296         -** Return TRUE if the probe cost is less than the baseline cost
   297         -*/
   298         -static int compareCost(const WhereCost *pProbe, const WhereCost *pBaseline){
   299         -  if( pProbe->rCost<pBaseline->rCost ) return 1;
   300         -  if( pProbe->rCost>pBaseline->rCost ) return 0;
   301         -  if( pProbe->plan.nOBSat>pBaseline->plan.nOBSat ) return 1;
   302         -  if( pProbe->plan.nRow<pBaseline->plan.nRow ) return 1;
   303         -  return 0;
          428  +** These are definitions of bits in the WhereLoop.wsFlags field.
          429  +** The particular combination of bits in each WhereLoop help to
          430  +** determine the algorithm that WhereLoop represents.
          431  +*/
          432  +#define WHERE_COLUMN_EQ    0x00000001  /* x=EXPR or x IN (...) or x IS NULL */
          433  +#define WHERE_COLUMN_RANGE 0x00000002  /* x<EXPR and/or x>EXPR */
          434  +#define WHERE_COLUMN_IN    0x00000004  /* x IN (...) */
          435  +#define WHERE_COLUMN_NULL  0x00000008  /* x IS NULL */
          436  +#define WHERE_CONSTRAINT   0x0000000f  /* Any of the WHERE_COLUMN_xxx values */
          437  +#define WHERE_TOP_LIMIT    0x00000010  /* x<EXPR or x<=EXPR constraint */
          438  +#define WHERE_BTM_LIMIT    0x00000020  /* x>EXPR or x>=EXPR constraint */
          439  +#define WHERE_BOTH_LIMIT   0x00000030  /* Both x>EXPR and x<EXPR */
          440  +#define WHERE_IDX_ONLY     0x00000040  /* Use index only - omit table */
          441  +#define WHERE_IPK          0x00000100  /* x is the INTEGER PRIMARY KEY */
          442  +#define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
          443  +#define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
          444  +#define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
          445  +#define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
          446  +#define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
          447  +#define WHERE_TEMP_INDEX   0x00004000  /* Uses an ephemeral index */
          448  +
          449  +
          450  +/* Convert a WhereCost value (10 times log2(X)) into its integer value X.
          451  +** A rough approximation is used.  The value returned is not exact.
          452  +*/
          453  +static u64 whereCostToInt(WhereCost x){
          454  +  u64 n;
          455  +  if( x<10 ) return 1;
          456  +  n = x%10;
          457  +  x /= 10;
          458  +  if( n>=5 ) n -= 2;
          459  +  else if( n>=1 ) n -= 1;
          460  +  if( x>=3 ) return (n+8)<<(x-3);
          461  +  return (n+8)>>(3-x);
          462  +}
          463  +
          464  +/*
          465  +** Return the estimated number of output rows from a WHERE clause
          466  +*/
          467  +u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
          468  +  return whereCostToInt(pWInfo->nRowOut);
          469  +}
          470  +
          471  +/*
          472  +** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
          473  +** WHERE clause returns outputs for DISTINCT processing.
          474  +*/
          475  +int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
          476  +  return pWInfo->eDistinct;
          477  +}
          478  +
          479  +/*
          480  +** Return TRUE if the WHERE clause returns rows in ORDER BY order.
          481  +** Return FALSE if the output needs to be sorted.
          482  +*/
          483  +int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
          484  +  return pWInfo->bOBSat!=0;
          485  +}
          486  +
          487  +/*
          488  +** Return the VDBE address or label to jump to in order to continue
          489  +** immediately with the next row of a WHERE clause.
          490  +*/
          491  +int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
          492  +  return pWInfo->iContinue;
          493  +}
          494  +
          495  +/*
          496  +** Return the VDBE address or label to jump to in order to break
          497  +** out of a WHERE loop.
          498  +*/
          499  +int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
          500  +  return pWInfo->iBreak;
          501  +}
          502  +
          503  +/*
          504  +** Return TRUE if an UPDATE or DELETE statement can operate directly on
          505  +** the rowids returned by a WHERE clause.  Return FALSE if doing an
          506  +** UPDATE or DELETE might change subsequent WHERE clause results.
          507  +*/
          508  +int sqlite3WhereOkOnePass(WhereInfo *pWInfo){
          509  +  return pWInfo->okOnePass;
   304    510   }
   305    511   
   306    512   /*
   307    513   ** Initialize a preallocated WhereClause structure.
   308    514   */
   309    515   static void whereClauseInit(
   310    516     WhereClause *pWC,        /* The WhereClause to be initialized */
   311         -  Parse *pParse,           /* The parsing context */
   312         -  WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
   313         -  u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
          517  +  WhereInfo *pWInfo        /* The WHERE processing context */
   314    518   ){
   315         -  pWC->pParse = pParse;
   316         -  pWC->pMaskSet = pMaskSet;
          519  +  pWC->pWInfo = pWInfo;
   317    520     pWC->pOuter = 0;
   318    521     pWC->nTerm = 0;
   319    522     pWC->nSlot = ArraySize(pWC->aStatic);
   320    523     pWC->a = pWC->aStatic;
   321         -  pWC->wctrlFlags = wctrlFlags;
   322    524   }
   323    525   
   324    526   /* Forward reference */
   325    527   static void whereClauseClear(WhereClause*);
   326    528   
   327    529   /*
   328    530   ** Deallocate all memory associated with a WhereOrInfo object.
................................................................................
   343    545   /*
   344    546   ** Deallocate a WhereClause structure.  The WhereClause structure
   345    547   ** itself is not freed.  This routine is the inverse of whereClauseInit().
   346    548   */
   347    549   static void whereClauseClear(WhereClause *pWC){
   348    550     int i;
   349    551     WhereTerm *a;
   350         -  sqlite3 *db = pWC->pParse->db;
          552  +  sqlite3 *db = pWC->pWInfo->pParse->db;
   351    553     for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
   352    554       if( a->wtFlags & TERM_DYNAMIC ){
   353    555         sqlite3ExprDelete(db, a->pExpr);
   354    556       }
   355    557       if( a->wtFlags & TERM_ORINFO ){
   356    558         whereOrInfoDelete(db, a->u.pOrInfo);
   357    559       }else if( a->wtFlags & TERM_ANDINFO ){
................................................................................
   384    586   */
   385    587   static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
   386    588     WhereTerm *pTerm;
   387    589     int idx;
   388    590     testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
   389    591     if( pWC->nTerm>=pWC->nSlot ){
   390    592       WhereTerm *pOld = pWC->a;
   391         -    sqlite3 *db = pWC->pParse->db;
          593  +    sqlite3 *db = pWC->pWInfo->pParse->db;
   392    594       pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
   393    595       if( pWC->a==0 ){
   394    596         if( wtFlags & TERM_DYNAMIC ){
   395    597           sqlite3ExprDelete(db, p);
   396    598         }
   397    599         pWC->a = pOld;
   398    600         return 0;
................................................................................
   424    626   ** The original WHERE clause in pExpr is unaltered.  All this routine
   425    627   ** does is make slot[] entries point to substructure within pExpr.
   426    628   **
   427    629   ** In the previous sentence and in the diagram, "slot[]" refers to
   428    630   ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
   429    631   ** all terms of the WHERE clause.
   430    632   */
   431         -static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){
   432         -  pWC->op = (u8)op;
          633  +static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
          634  +  pWC->op = op;
   433    635     if( pExpr==0 ) return;
   434    636     if( pExpr->op!=op ){
   435    637       whereClauseInsert(pWC, pExpr, 0);
   436    638     }else{
   437    639       whereSplit(pWC, pExpr->pLeft, op);
   438    640       whereSplit(pWC, pExpr->pRight, op);
   439    641     }
   440    642   }
   441    643   
   442    644   /*
   443         -** Initialize an expression mask set (a WhereMaskSet object)
          645  +** Initialize a WhereMaskSet object
   444    646   */
   445         -#define initMaskSet(P)  memset(P, 0, sizeof(*P))
          647  +#define initMaskSet(P)  (P)->n=0
   446    648   
   447    649   /*
   448    650   ** Return the bitmask for the given cursor number.  Return 0 if
   449    651   ** iCursor is not in the set.
   450    652   */
   451    653   static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
   452    654     int i;
   453    655     assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
   454    656     for(i=0; i<pMaskSet->n; i++){
   455    657       if( pMaskSet->ix[i]==iCursor ){
   456         -      return ((Bitmask)1)<<i;
          658  +      return MASKBIT(i);
   457    659       }
   458    660     }
   459    661     return 0;
   460    662   }
   461    663   
   462    664   /*
   463    665   ** Create a new mask for cursor iCursor.
................................................................................
   469    671   */
   470    672   static void createMask(WhereMaskSet *pMaskSet, int iCursor){
   471    673     assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
   472    674     pMaskSet->ix[pMaskSet->n++] = iCursor;
   473    675   }
   474    676   
   475    677   /*
   476         -** This routine walks (recursively) an expression tree and generates
          678  +** These routine walk (recursively) an expression tree and generates
   477    679   ** a bitmask indicating which tables are used in that expression
   478    680   ** tree.
   479         -**
   480         -** In order for this routine to work, the calling function must have
   481         -** previously invoked sqlite3ResolveExprNames() on the expression.  See
   482         -** the header comment on that routine for additional information.
   483         -** The sqlite3ResolveExprNames() routines looks for column names and
   484         -** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
   485         -** the VDBE cursor number of the table.  This routine just has to
   486         -** translate the cursor numbers into bitmask values and OR all
   487         -** the bitmasks together.
   488    681   */
   489    682   static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
   490    683   static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
   491    684   static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
   492    685     Bitmask mask = 0;
   493    686     if( p==0 ) return 0;
   494    687     if( p->op==TK_COLUMN ){
................................................................................
   534    727     }
   535    728     return mask;
   536    729   }
   537    730   
   538    731   /*
   539    732   ** Return TRUE if the given operator is one of the operators that is
   540    733   ** allowed for an indexable WHERE clause term.  The allowed operators are
   541         -** "=", "<", ">", "<=", ">=", and "IN".
          734  +** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
   542    735   **
   543    736   ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
   544    737   ** of one of the following forms: column = expression column > expression
   545    738   ** column >= expression column < expression column <= expression
   546    739   ** expression = column expression > column expression >= column
   547    740   ** expression < column expression <= column column IN
   548    741   ** (expression-list) column IN (subquery) column IS NULL
................................................................................
   561    754   #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
   562    755   
   563    756   /*
   564    757   ** Commute a comparison operator.  Expressions of the form "X op Y"
   565    758   ** are converted into "Y op X".
   566    759   **
   567    760   ** If left/right precedence rules come into play when determining the
   568         -** collating
   569         -** side of the comparison, it remains associated with the same side after
   570         -** the commutation. So "Y collate NOCASE op X" becomes 
   571         -** "X op Y". This is because any collation sequence on
          761  +** collating sequence, then COLLATE operators are adjusted to ensure
          762  +** that the collating sequence does not change.  For example:
          763  +** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
   572    764   ** the left hand side of a comparison overrides any collation sequence 
   573    765   ** attached to the right. For the same reason the EP_Collate flag
   574    766   ** is not commuted.
   575    767   */
   576    768   static void exprCommute(Parse *pParse, Expr *pExpr){
   577    769     u16 expRight = (pExpr->pRight->flags & EP_Collate);
   578    770     u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
................................................................................
   620    812     assert( op!=TK_EQ || c==WO_EQ );
   621    813     assert( op!=TK_LT || c==WO_LT );
   622    814     assert( op!=TK_LE || c==WO_LE );
   623    815     assert( op!=TK_GT || c==WO_GT );
   624    816     assert( op!=TK_GE || c==WO_GE );
   625    817     return c;
   626    818   }
          819  +
          820  +/*
          821  +** Advance to the next WhereTerm that matches according to the criteria
          822  +** established when the pScan object was initialized by whereScanInit().
          823  +** Return NULL if there are no more matching WhereTerms.
          824  +*/
          825  +WhereTerm *whereScanNext(WhereScan *pScan){
          826  +  int iCur;            /* The cursor on the LHS of the term */
          827  +  int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
          828  +  Expr *pX;            /* An expression being tested */
          829  +  WhereClause *pWC;    /* Shorthand for pScan->pWC */
          830  +  WhereTerm *pTerm;    /* The term being tested */
          831  +  int k = pScan->k;    /* Where to start scanning */
          832  +
          833  +  while( pScan->iEquiv<=pScan->nEquiv ){
          834  +    iCur = pScan->aEquiv[pScan->iEquiv-2];
          835  +    iColumn = pScan->aEquiv[pScan->iEquiv-1];
          836  +    while( (pWC = pScan->pWC)!=0 ){
          837  +      for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
          838  +        if( pTerm->leftCursor==iCur && pTerm->u.leftColumn==iColumn ){
          839  +          if( (pTerm->eOperator & WO_EQUIV)!=0
          840  +           && pScan->nEquiv<ArraySize(pScan->aEquiv)
          841  +          ){
          842  +            int j;
          843  +            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
          844  +            assert( pX->op==TK_COLUMN );
          845  +            for(j=0; j<pScan->nEquiv; j+=2){
          846  +              if( pScan->aEquiv[j]==pX->iTable
          847  +               && pScan->aEquiv[j+1]==pX->iColumn ){
          848  +                  break;
          849  +              }
          850  +            }
          851  +            if( j==pScan->nEquiv ){
          852  +              pScan->aEquiv[j] = pX->iTable;
          853  +              pScan->aEquiv[j+1] = pX->iColumn;
          854  +              pScan->nEquiv += 2;
          855  +            }
          856  +          }
          857  +          if( (pTerm->eOperator & pScan->opMask)!=0 ){
          858  +            /* Verify the affinity and collating sequence match */
          859  +            if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
          860  +              CollSeq *pColl;
          861  +              Parse *pParse = pWC->pWInfo->pParse;
          862  +              pX = pTerm->pExpr;
          863  +              if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
          864  +                continue;
          865  +              }
          866  +              assert(pX->pLeft);
          867  +              pColl = sqlite3BinaryCompareCollSeq(pParse,
          868  +                                                  pX->pLeft, pX->pRight);
          869  +              if( pColl==0 ) pColl = pParse->db->pDfltColl;
          870  +              if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
          871  +                continue;
          872  +              }
          873  +            }
          874  +            if( (pTerm->eOperator & WO_EQ)!=0
          875  +             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
          876  +             && pX->iTable==pScan->aEquiv[0]
          877  +             && pX->iColumn==pScan->aEquiv[1]
          878  +            ){
          879  +              continue;
          880  +            }
          881  +            pScan->k = k+1;
          882  +            return pTerm;
          883  +          }
          884  +        }
          885  +      }
          886  +      pScan->pWC = pScan->pWC->pOuter;
          887  +      k = 0;
          888  +    }
          889  +    pScan->pWC = pScan->pOrigWC;
          890  +    k = 0;
          891  +    pScan->iEquiv += 2;
          892  +  }
          893  +  return 0;
          894  +}
          895  +
          896  +/*
          897  +** Initialize a WHERE clause scanner object.  Return a pointer to the
          898  +** first match.  Return NULL if there are no matches.
          899  +**
          900  +** The scanner will be searching the WHERE clause pWC.  It will look
          901  +** for terms of the form "X <op> <expr>" where X is column iColumn of table
          902  +** iCur.  The <op> must be one of the operators described by opMask.
          903  +**
          904  +** If the search is for X and the WHERE clause contains terms of the
          905  +** form X=Y then this routine might also return terms of the form
          906  +** "Y <op> <expr>".  The number of levels of transitivity is limited,
          907  +** but is enough to handle most commonly occurring SQL statements.
          908  +**
          909  +** If X is not the INTEGER PRIMARY KEY then X must be compatible with
          910  +** index pIdx.
          911  +*/
          912  +WhereTerm *whereScanInit(
          913  +  WhereScan *pScan,       /* The WhereScan object being initialized */
          914  +  WhereClause *pWC,       /* The WHERE clause to be scanned */
          915  +  int iCur,               /* Cursor to scan for */
          916  +  int iColumn,            /* Column to scan for */
          917  +  u32 opMask,             /* Operator(s) to scan for */
          918  +  Index *pIdx             /* Must be compatible with this index */
          919  +){
          920  +  int j;
          921  +
          922  +  /* memset(pScan, 0, sizeof(*pScan)); */
          923  +  pScan->pOrigWC = pWC;
          924  +  pScan->pWC = pWC;
          925  +  if( pIdx && iColumn>=0 ){
          926  +    pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
          927  +    for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
          928  +      if( NEVER(j>=pIdx->nColumn) ) return 0;
          929  +    }
          930  +    pScan->zCollName = pIdx->azColl[j];
          931  +  }else{
          932  +    pScan->idxaff = 0;
          933  +    pScan->zCollName = 0;
          934  +  }
          935  +  pScan->opMask = opMask;
          936  +  pScan->k = 0;
          937  +  pScan->aEquiv[0] = iCur;
          938  +  pScan->aEquiv[1] = iColumn;
          939  +  pScan->nEquiv = 2;
          940  +  pScan->iEquiv = 2;
          941  +  return whereScanNext(pScan);
          942  +}
   627    943   
   628    944   /*
   629    945   ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
   630    946   ** where X is a reference to the iColumn of table iCur and <op> is one of
   631    947   ** the WO_xx operator codes specified by the op parameter.
   632    948   ** Return a pointer to the term.  Return 0 if not found.
   633    949   **
................................................................................
   652    968     WhereClause *pWC,     /* The WHERE clause to be searched */
   653    969     int iCur,             /* Cursor number of LHS */
   654    970     int iColumn,          /* Column number of LHS */
   655    971     Bitmask notReady,     /* RHS must not overlap with this mask */
   656    972     u32 op,               /* Mask of WO_xx values describing operator */
   657    973     Index *pIdx           /* Must be compatible with this index, if not NULL */
   658    974   ){
   659         -  WhereTerm *pTerm;            /* Term being examined as possible result */
   660         -  WhereTerm *pResult = 0;      /* The answer to return */
   661         -  WhereClause *pWCOrig = pWC;  /* Original pWC value */
   662         -  int j, k;                    /* Loop counters */
   663         -  Expr *pX;                /* Pointer to an expression */
   664         -  Parse *pParse;           /* Parsing context */
   665         -  int iOrigCol = iColumn;  /* Original value of iColumn */
   666         -  int nEquiv = 2;          /* Number of entires in aEquiv[] */
   667         -  int iEquiv = 2;          /* Number of entries of aEquiv[] processed so far */
   668         -  int aEquiv[22];          /* iCur,iColumn and up to 10 other equivalents */
   669         -
   670         -  assert( iCur>=0 );
   671         -  aEquiv[0] = iCur;
   672         -  aEquiv[1] = iColumn;
   673         -  for(;;){
   674         -    for(pWC=pWCOrig; pWC; pWC=pWC->pOuter){
   675         -      for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
   676         -        if( pTerm->leftCursor==iCur
   677         -          && pTerm->u.leftColumn==iColumn
   678         -        ){
   679         -          if( (pTerm->prereqRight & notReady)==0
   680         -           && (pTerm->eOperator & op & WO_ALL)!=0
   681         -          ){
   682         -            if( iOrigCol>=0 && pIdx && (pTerm->eOperator & WO_ISNULL)==0 ){
   683         -              CollSeq *pColl;
   684         -              char idxaff;
   685         -      
   686         -              pX = pTerm->pExpr;
   687         -              pParse = pWC->pParse;
   688         -              idxaff = pIdx->pTable->aCol[iOrigCol].affinity;
   689         -              if( !sqlite3IndexAffinityOk(pX, idxaff) ){
   690         -                continue;
   691         -              }
   692         -      
   693         -              /* Figure out the collation sequence required from an index for
   694         -              ** it to be useful for optimising expression pX. Store this
   695         -              ** value in variable pColl.
   696         -              */
   697         -              assert(pX->pLeft);
   698         -              pColl = sqlite3BinaryCompareCollSeq(pParse,pX->pLeft,pX->pRight);
   699         -              if( pColl==0 ) pColl = pParse->db->pDfltColl;
   700         -      
   701         -              for(j=0; pIdx->aiColumn[j]!=iOrigCol; j++){
   702         -                if( NEVER(j>=pIdx->nColumn) ) return 0;
   703         -              }
   704         -              if( sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ){
   705         -                continue;
   706         -              }
   707         -            }
   708         -            if( pTerm->prereqRight==0 && (pTerm->eOperator&WO_EQ)!=0 ){
   709         -              pResult = pTerm;
   710         -              goto findTerm_success;
   711         -            }else if( pResult==0 ){
   712         -              pResult = pTerm;
   713         -            }
   714         -          }
   715         -          if( (pTerm->eOperator & WO_EQUIV)!=0
   716         -           && nEquiv<ArraySize(aEquiv)
   717         -          ){
   718         -            pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
   719         -            assert( pX->op==TK_COLUMN );
   720         -            for(j=0; j<nEquiv; j+=2){
   721         -              if( aEquiv[j]==pX->iTable && aEquiv[j+1]==pX->iColumn ) break;
   722         -            }
   723         -            if( j==nEquiv ){
   724         -              aEquiv[j] = pX->iTable;
   725         -              aEquiv[j+1] = pX->iColumn;
   726         -              nEquiv += 2;
   727         -            }
   728         -          }
   729         -        }
   730         -      }
   731         -    }
   732         -    if( iEquiv>=nEquiv ) break;
   733         -    iCur = aEquiv[iEquiv++];
   734         -    iColumn = aEquiv[iEquiv++];
   735         -  }
   736         -findTerm_success:
          975  +  WhereTerm *pResult = 0;
          976  +  WhereTerm *p;
          977  +  WhereScan scan;
          978  +
          979  +  p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
          980  +  while( p ){
          981  +    if( (p->prereqRight & notReady)==0 ){
          982  +      if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
          983  +        return p;
          984  +      }
          985  +      if( pResult==0 ) pResult = p;
          986  +    }
          987  +    p = whereScanNext(&scan);
          988  +  }
   737    989     return pResult;
   738    990   }
   739    991   
   740    992   /* Forward reference */
   741    993   static void exprAnalyze(SrcList*, WhereClause*, int);
   742    994   
   743    995   /*
   744    996   ** Call exprAnalyze on all terms in a WHERE clause.  
   745         -**
   746         -**
   747    997   */
   748    998   static void exprAnalyzeAll(
   749    999     SrcList *pTabList,       /* the FROM clause */
   750   1000     WhereClause *pWC         /* the WHERE clause to be analyzed */
   751   1001   ){
   752   1002     int i;
   753   1003     for(i=pWC->nTerm-1; i>=0; i--){
................................................................................
   971   1221   ** zero.  This term is not useful for search.
   972   1222   */
   973   1223   static void exprAnalyzeOrTerm(
   974   1224     SrcList *pSrc,            /* the FROM clause */
   975   1225     WhereClause *pWC,         /* the complete WHERE clause */
   976   1226     int idxTerm               /* Index of the OR-term to be analyzed */
   977   1227   ){
   978         -  Parse *pParse = pWC->pParse;            /* Parser context */
         1228  +  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
         1229  +  Parse *pParse = pWInfo->pParse;         /* Parser context */
   979   1230     sqlite3 *db = pParse->db;               /* Database connection */
   980   1231     WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
   981   1232     Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
   982         -  WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
   983   1233     int i;                                  /* Loop counters */
   984   1234     WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
   985   1235     WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
   986   1236     WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
   987   1237     Bitmask chngToIN;         /* Tables that might satisfy case 1 */
   988   1238     Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
   989   1239   
................................................................................
   994   1244     */
   995   1245     assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
   996   1246     assert( pExpr->op==TK_OR );
   997   1247     pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
   998   1248     if( pOrInfo==0 ) return;
   999   1249     pTerm->wtFlags |= TERM_ORINFO;
  1000   1250     pOrWc = &pOrInfo->wc;
  1001         -  whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
         1251  +  whereClauseInit(pOrWc, pWInfo);
  1002   1252     whereSplit(pOrWc, pExpr, TK_OR);
  1003   1253     exprAnalyzeAll(pSrc, pOrWc);
  1004   1254     if( db->mallocFailed ) return;
  1005   1255     assert( pOrWc->nTerm>=2 );
  1006   1256   
  1007   1257     /*
  1008   1258     ** Compute the set of tables that might satisfy cases 1 or 2.
................................................................................
  1020   1270           WhereTerm *pAndTerm;
  1021   1271           int j;
  1022   1272           Bitmask b = 0;
  1023   1273           pOrTerm->u.pAndInfo = pAndInfo;
  1024   1274           pOrTerm->wtFlags |= TERM_ANDINFO;
  1025   1275           pOrTerm->eOperator = WO_AND;
  1026   1276           pAndWC = &pAndInfo->wc;
  1027         -        whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
         1277  +        whereClauseInit(pAndWC, pWC->pWInfo);
  1028   1278           whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
  1029   1279           exprAnalyzeAll(pSrc, pAndWC);
  1030   1280           pAndWC->pOuter = pWC;
  1031   1281           testcase( db->mallocFailed );
  1032   1282           if( !db->mallocFailed ){
  1033   1283             for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
  1034   1284               assert( pAndTerm->pExpr );
  1035   1285               if( allowedOp(pAndTerm->pExpr->op) ){
  1036         -              b |= getMask(pMaskSet, pAndTerm->leftCursor);
         1286  +              b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
  1037   1287               }
  1038   1288             }
  1039   1289           }
  1040   1290           indexable &= b;
  1041   1291         }
  1042   1292       }else if( pOrTerm->wtFlags & TERM_COPIED ){
  1043   1293         /* Skip this term for now.  We revisit it when we process the
  1044   1294         ** corresponding TERM_VIRTUAL term */
  1045   1295       }else{
  1046   1296         Bitmask b;
  1047         -      b = getMask(pMaskSet, pOrTerm->leftCursor);
         1297  +      b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
  1048   1298         if( pOrTerm->wtFlags & TERM_VIRTUAL ){
  1049   1299           WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
  1050         -        b |= getMask(pMaskSet, pOther->leftCursor);
         1300  +        b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
  1051   1301         }
  1052   1302         indexable &= b;
  1053   1303         if( (pOrTerm->eOperator & WO_EQ)==0 ){
  1054   1304           chngToIN = 0;
  1055   1305         }else{
  1056   1306           chngToIN &= b;
  1057   1307         }
................................................................................
  1105   1355           pOrTerm->wtFlags &= ~TERM_OR_OK;
  1106   1356           if( pOrTerm->leftCursor==iCursor ){
  1107   1357             /* This is the 2-bit case and we are on the second iteration and
  1108   1358             ** current term is from the first iteration.  So skip this term. */
  1109   1359             assert( j==1 );
  1110   1360             continue;
  1111   1361           }
  1112         -        if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
         1362  +        if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
  1113   1363             /* This term must be of the form t1.a==t2.b where t2 is in the
  1114   1364             ** chngToIN set but t1 is not.  This term will be either preceeded
  1115   1365             ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
  1116   1366             ** and use its inversion. */
  1117   1367             testcase( pOrTerm->wtFlags & TERM_COPIED );
  1118   1368             testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
  1119   1369             assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
................................................................................
  1124   1374           break;
  1125   1375         }
  1126   1376         if( i<0 ){
  1127   1377           /* No candidate table+column was found.  This can only occur
  1128   1378           ** on the second iteration */
  1129   1379           assert( j==1 );
  1130   1380           assert( IsPowerOfTwo(chngToIN) );
  1131         -        assert( chngToIN==getMask(pMaskSet, iCursor) );
         1381  +        assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
  1132   1382           break;
  1133   1383         }
  1134   1384         testcase( j==1 );
  1135   1385   
  1136   1386         /* We have found a candidate table and column.  Check to see if that
  1137   1387         ** table and column is common to every term in the OR clause */
  1138   1388         okToChngToIN = 1;
................................................................................
  1173   1423   
  1174   1424         for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
  1175   1425           if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
  1176   1426           assert( pOrTerm->eOperator & WO_EQ );
  1177   1427           assert( pOrTerm->leftCursor==iCursor );
  1178   1428           assert( pOrTerm->u.leftColumn==iColumn );
  1179   1429           pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
  1180         -        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
         1430  +        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
  1181   1431           pLeft = pOrTerm->pExpr->pLeft;
  1182   1432         }
  1183   1433         assert( pLeft!=0 );
  1184   1434         pDup = sqlite3ExprDup(db, pLeft, 0);
  1185   1435         pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
  1186   1436         if( pNew ){
  1187   1437           int idxNew;
................................................................................
  1222   1472   ** and the copy has idxParent set to the index of the original term.
  1223   1473   */
  1224   1474   static void exprAnalyze(
  1225   1475     SrcList *pSrc,            /* the FROM clause */
  1226   1476     WhereClause *pWC,         /* the WHERE clause */
  1227   1477     int idxTerm               /* Index of the term to be analyzed */
  1228   1478   ){
         1479  +  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
  1229   1480     WhereTerm *pTerm;                /* The term to be analyzed */
  1230   1481     WhereMaskSet *pMaskSet;          /* Set of table index masks */
  1231   1482     Expr *pExpr;                     /* The expression to be analyzed */
  1232   1483     Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
  1233   1484     Bitmask prereqAll;               /* Prerequesites of pExpr */
  1234   1485     Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
  1235   1486     Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
  1236   1487     int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
  1237   1488     int noCase = 0;                  /* LIKE/GLOB distinguishes case */
  1238   1489     int op;                          /* Top-level operator.  pExpr->op */
  1239         -  Parse *pParse = pWC->pParse;     /* Parsing context */
         1490  +  Parse *pParse = pWInfo->pParse;  /* Parsing context */
  1240   1491     sqlite3 *db = pParse->db;        /* Database connection */
  1241   1492   
  1242   1493     if( db->mallocFailed ){
  1243   1494       return;
  1244   1495     }
  1245   1496     pTerm = &pWC->a[idxTerm];
  1246         -  pMaskSet = pWC->pMaskSet;
         1497  +  pMaskSet = &pWInfo->sMaskSet;
  1247   1498     pExpr = pTerm->pExpr;
  1248   1499     assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
  1249   1500     prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
  1250   1501     op = pExpr->op;
  1251   1502     if( op==TK_IN ){
  1252   1503       assert( pExpr->pRight==0 );
  1253   1504       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
  1517   1768     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1518   1769     ** an index for tables to the left of the join.
  1519   1770     */
  1520   1771     pTerm->prereqRight |= extraRight;
  1521   1772   }
  1522   1773   
  1523   1774   /*
  1524         -** This function searches the expression list passed as the second argument
  1525         -** for an expression of type TK_COLUMN that refers to the same column and
  1526         -** uses the same collation sequence as the iCol'th column of index pIdx.
  1527         -** Argument iBase is the cursor number used for the table that pIdx refers
  1528         -** to.
         1775  +** This function searches pList for a entry that matches the iCol-th column
         1776  +** of index pIdx.
  1529   1777   **
  1530   1778   ** If such an expression is found, its index in pList->a[] is returned. If
  1531   1779   ** no expression is found, -1 is returned.
  1532   1780   */
  1533   1781   static int findIndexCol(
  1534   1782     Parse *pParse,                  /* Parse context */
  1535   1783     ExprList *pList,                /* Expression list to search */
................................................................................
  1551   1799           return i;
  1552   1800         }
  1553   1801       }
  1554   1802     }
  1555   1803   
  1556   1804     return -1;
  1557   1805   }
  1558         -
  1559         -/*
  1560         -** This routine determines if pIdx can be used to assist in processing a
  1561         -** DISTINCT qualifier. In other words, it tests whether or not using this
  1562         -** index for the outer loop guarantees that rows with equal values for
  1563         -** all expressions in the pDistinct list are delivered grouped together.
  1564         -**
  1565         -** For example, the query 
  1566         -**
  1567         -**   SELECT DISTINCT a, b, c FROM tbl WHERE a = ?
  1568         -**
  1569         -** can benefit from any index on columns "b" and "c".
  1570         -*/
  1571         -static int isDistinctIndex(
  1572         -  Parse *pParse,                  /* Parsing context */
  1573         -  WhereClause *pWC,               /* The WHERE clause */
  1574         -  Index *pIdx,                    /* The index being considered */
  1575         -  int base,                       /* Cursor number for the table pIdx is on */
  1576         -  ExprList *pDistinct,            /* The DISTINCT expressions */
  1577         -  int nEqCol                      /* Number of index columns with == */
  1578         -){
  1579         -  Bitmask mask = 0;               /* Mask of unaccounted for pDistinct exprs */
  1580         -  int i;                          /* Iterator variable */
  1581         -
  1582         -  assert( pDistinct!=0 );
  1583         -  if( pIdx->zName==0 || pDistinct->nExpr>=BMS ) return 0;
  1584         -  testcase( pDistinct->nExpr==BMS-1 );
  1585         -
  1586         -  /* Loop through all the expressions in the distinct list. If any of them
  1587         -  ** are not simple column references, return early. Otherwise, test if the
  1588         -  ** WHERE clause contains a "col=X" clause. If it does, the expression
  1589         -  ** can be ignored. If it does not, and the column does not belong to the
  1590         -  ** same table as index pIdx, return early. Finally, if there is no
  1591         -  ** matching "col=X" expression and the column is on the same table as pIdx,
  1592         -  ** set the corresponding bit in variable mask.
  1593         -  */
  1594         -  for(i=0; i<pDistinct->nExpr; i++){
  1595         -    WhereTerm *pTerm;
  1596         -    Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
  1597         -    if( p->op!=TK_COLUMN ) return 0;
  1598         -    pTerm = findTerm(pWC, p->iTable, p->iColumn, ~(Bitmask)0, WO_EQ, 0);
  1599         -    if( pTerm ){
  1600         -      Expr *pX = pTerm->pExpr;
  1601         -      CollSeq *p1 = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
  1602         -      CollSeq *p2 = sqlite3ExprCollSeq(pParse, p);
  1603         -      if( p1==p2 ) continue;
  1604         -    }
  1605         -    if( p->iTable!=base ) return 0;
  1606         -    mask |= (((Bitmask)1) << i);
  1607         -  }
  1608         -
  1609         -  for(i=nEqCol; mask && i<pIdx->nColumn; i++){
  1610         -    int iExpr = findIndexCol(pParse, pDistinct, base, pIdx, i);
  1611         -    if( iExpr<0 ) break;
  1612         -    mask &= ~(((Bitmask)1) << iExpr);
  1613         -  }
  1614         -
  1615         -  return (mask==0);
  1616         -}
  1617         -
  1618   1806   
  1619   1807   /*
  1620   1808   ** Return true if the DISTINCT expression-list passed as the third argument
  1621         -** is redundant. A DISTINCT list is redundant if the database contains a
  1622         -** UNIQUE index that guarantees that the result of the query will be distinct
  1623         -** anyway.
         1809  +** is redundant.
         1810  +**
         1811  +** A DISTINCT list is redundant if the database contains some subset of
         1812  +** columns that are unique and non-null.
  1624   1813   */
  1625   1814   static int isDistinctRedundant(
  1626         -  Parse *pParse,
  1627         -  SrcList *pTabList,
  1628         -  WhereClause *pWC,
  1629         -  ExprList *pDistinct
         1815  +  Parse *pParse,            /* Parsing context */
         1816  +  SrcList *pTabList,        /* The FROM clause */
         1817  +  WhereClause *pWC,         /* The WHERE clause */
         1818  +  ExprList *pDistinct       /* The result set that needs to be DISTINCT */
  1630   1819   ){
  1631   1820     Table *pTab;
  1632   1821     Index *pIdx;
  1633   1822     int i;                          
  1634   1823     int iBase;
  1635   1824   
  1636   1825     /* If there is more than one table or sub-select in the FROM clause of
................................................................................
  1678   1867         return 1;
  1679   1868       }
  1680   1869     }
  1681   1870   
  1682   1871     return 0;
  1683   1872   }
  1684   1873   
  1685         -/*
  1686         -** Prepare a crude estimate of the logarithm of the input value.
  1687         -** The results need not be exact.  This is only used for estimating
  1688         -** the total cost of performing operations with O(logN) or O(NlogN)
  1689         -** complexity.  Because N is just a guess, it is no great tragedy if
  1690         -** logN is a little off.
  1691         -*/
  1692         -static double estLog(double N){
  1693         -  double logN = 1;
  1694         -  double x = 10;
  1695         -  while( N>x ){
  1696         -    logN += 1;
  1697         -    x *= 10;
  1698         -  }
  1699         -  return logN;
         1874  +/* 
         1875  +** The (an approximate) sum of two WhereCosts.  This computation is
         1876  +** not a simple "+" operator because WhereCost is stored as a logarithmic
         1877  +** value.
         1878  +** 
         1879  +*/
         1880  +static WhereCost whereCostAdd(WhereCost a, WhereCost b){
         1881  +  static const unsigned char x[] = {
         1882  +     10, 10,                         /* 0,1 */
         1883  +      9, 9,                          /* 2,3 */
         1884  +      8, 8,                          /* 4,5 */
         1885  +      7, 7, 7,                       /* 6,7,8 */
         1886  +      6, 6, 6,                       /* 9,10,11 */
         1887  +      5, 5, 5,                       /* 12-14 */
         1888  +      4, 4, 4, 4,                    /* 15-18 */
         1889  +      3, 3, 3, 3, 3, 3,              /* 19-24 */
         1890  +      2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
         1891  +  };
         1892  +  if( a>=b ){
         1893  +    if( a>b+49 ) return a;
         1894  +    if( a>b+31 ) return a+1;
         1895  +    return a+x[a-b];
         1896  +  }else{
         1897  +    if( b>a+49 ) return b;
         1898  +    if( b>a+31 ) return b+1;
         1899  +    return b+x[b-a];
         1900  +  }
         1901  +}
         1902  +
         1903  +/*
         1904  +** Convert an integer into a WhereCost.  In other words, compute a
         1905  +** good approximatation for 10*log2(x).
         1906  +*/
         1907  +static WhereCost whereCost(tRowcnt x){
         1908  +  static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
         1909  +  WhereCost y = 40;
         1910  +  if( x<8 ){
         1911  +    if( x<2 ) return 0;
         1912  +    while( x<8 ){  y -= 10; x <<= 1; }
         1913  +  }else{
         1914  +    while( x>255 ){ y += 40; x >>= 4; }
         1915  +    while( x>15 ){  y += 10; x >>= 1; }
         1916  +  }
         1917  +  return a[x&7] + y - 10;
         1918  +}
         1919  +
         1920  +#ifndef SQLITE_OMIT_VIRTUALTABLE
         1921  +/*
         1922  +** Convert a double (as received from xBestIndex of a virtual table)
         1923  +** into a WhereCost.  In other words, compute an approximation for
         1924  +** 10*log2(x).
         1925  +*/
         1926  +static WhereCost whereCostFromDouble(double x){
         1927  +  u64 a;
         1928  +  WhereCost e;
         1929  +  assert( sizeof(x)==8 && sizeof(a)==8 );
         1930  +  if( x<=1 ) return 0;
         1931  +  if( x<=2000000000 ) return whereCost((tRowcnt)x);
         1932  +  memcpy(&a, &x, 8);
         1933  +  e = (a>>52) - 1022;
         1934  +  return e*10;
         1935  +}
         1936  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
         1937  +
         1938  +/*
         1939  +** Estimate the logarithm of the input value to base 2.
         1940  +*/
         1941  +static WhereCost estLog(WhereCost N){
         1942  +  WhereCost x = whereCost(N);
         1943  +  return x>33 ? x - 33 : 0;
  1700   1944   }
  1701   1945   
  1702   1946   /*
  1703   1947   ** Two routines for printing the content of an sqlite3_index_info
  1704   1948   ** structure.  Used for testing and debugging only.  If neither
  1705   1949   ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
  1706   1950   ** are no-ops.
  1707   1951   */
  1708         -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG)
         1952  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
  1709   1953   static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
  1710   1954     int i;
  1711   1955     if( !sqlite3WhereTrace ) return;
  1712   1956     for(i=0; i<p->nConstraint; i++){
  1713   1957       sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
  1714   1958          i,
  1715   1959          p->aConstraint[i].iColumn,
................................................................................
  1739   1983     sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
  1740   1984   }
  1741   1985   #else
  1742   1986   #define TRACE_IDX_INPUTS(A)
  1743   1987   #define TRACE_IDX_OUTPUTS(A)
  1744   1988   #endif
  1745   1989   
  1746         -/* 
  1747         -** Required because bestIndex() is called by bestOrClauseIndex() 
  1748         -*/
  1749         -static void bestIndex(WhereBestIdx*);
  1750         -
  1751         -/*
  1752         -** This routine attempts to find an scanning strategy that can be used 
  1753         -** to optimize an 'OR' expression that is part of a WHERE clause. 
  1754         -**
  1755         -** The table associated with FROM clause term pSrc may be either a
  1756         -** regular B-Tree table or a virtual table.
  1757         -*/
  1758         -static void bestOrClauseIndex(WhereBestIdx *p){
  1759         -#ifndef SQLITE_OMIT_OR_OPTIMIZATION
  1760         -  WhereClause *pWC = p->pWC;           /* The WHERE clause */
  1761         -  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
  1762         -  const int iCur = pSrc->iCursor;      /* The cursor of the table  */
  1763         -  const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
  1764         -  WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
  1765         -  WhereTerm *pTerm;                    /* A single term of the WHERE clause */
  1766         -
  1767         -  /* The OR-clause optimization is disallowed if the INDEXED BY or
  1768         -  ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
  1769         -  if( pSrc->notIndexed || pSrc->pIndex!=0 ){
  1770         -    return;
  1771         -  }
  1772         -  if( pWC->wctrlFlags & WHERE_AND_ONLY ){
  1773         -    return;
  1774         -  }
  1775         -
  1776         -  /* Search the WHERE clause terms for a usable WO_OR term. */
  1777         -  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  1778         -    if( (pTerm->eOperator & WO_OR)!=0
  1779         -     && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
  1780         -     && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
  1781         -    ){
  1782         -      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
  1783         -      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
  1784         -      WhereTerm *pOrTerm;
  1785         -      int flags = WHERE_MULTI_OR;
  1786         -      double rTotal = 0;
  1787         -      double nRow = 0;
  1788         -      Bitmask used = 0;
  1789         -      WhereBestIdx sBOI;
  1790         -
  1791         -      sBOI = *p;
  1792         -      sBOI.pOrderBy = 0;
  1793         -      sBOI.pDistinct = 0;
  1794         -      sBOI.ppIdxInfo = 0;
  1795         -      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
  1796         -        WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
  1797         -          (pOrTerm - pOrWC->a), (pTerm - pWC->a)
  1798         -        ));
  1799         -        if( (pOrTerm->eOperator& WO_AND)!=0 ){
  1800         -          sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
  1801         -          bestIndex(&sBOI);
  1802         -        }else if( pOrTerm->leftCursor==iCur ){
  1803         -          WhereClause tempWC;
  1804         -          tempWC.pParse = pWC->pParse;
  1805         -          tempWC.pMaskSet = pWC->pMaskSet;
  1806         -          tempWC.pOuter = pWC;
  1807         -          tempWC.op = TK_AND;
  1808         -          tempWC.a = pOrTerm;
  1809         -          tempWC.wctrlFlags = 0;
  1810         -          tempWC.nTerm = 1;
  1811         -          sBOI.pWC = &tempWC;
  1812         -          bestIndex(&sBOI);
  1813         -        }else{
  1814         -          continue;
  1815         -        }
  1816         -        rTotal += sBOI.cost.rCost;
  1817         -        nRow += sBOI.cost.plan.nRow;
  1818         -        used |= sBOI.cost.used;
  1819         -        if( rTotal>=p->cost.rCost ) break;
  1820         -      }
  1821         -
  1822         -      /* If there is an ORDER BY clause, increase the scan cost to account 
  1823         -      ** for the cost of the sort. */
  1824         -      if( p->pOrderBy!=0 ){
  1825         -        WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
  1826         -                    rTotal, rTotal+nRow*estLog(nRow)));
  1827         -        rTotal += nRow*estLog(nRow);
  1828         -      }
  1829         -
  1830         -      /* If the cost of scanning using this OR term for optimization is
  1831         -      ** less than the current cost stored in pCost, replace the contents
  1832         -      ** of pCost. */
  1833         -      WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
  1834         -      if( rTotal<p->cost.rCost ){
  1835         -        p->cost.rCost = rTotal;
  1836         -        p->cost.used = used;
  1837         -        p->cost.plan.nRow = nRow;
  1838         -        p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
  1839         -        p->cost.plan.wsFlags = flags;
  1840         -        p->cost.plan.u.pTerm = pTerm;
  1841         -      }
  1842         -    }
  1843         -  }
  1844         -#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  1845         -}
  1846         -
  1847   1990   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  1848   1991   /*
  1849   1992   ** Return TRUE if the WHERE clause term pTerm is of a form where it
  1850   1993   ** could be used with an index to access pSrc, assuming an appropriate
  1851   1994   ** index existed.
  1852   1995   */
  1853   1996   static int termCanDriveIndex(
................................................................................
  1855   1998     struct SrcList_item *pSrc,     /* Table we are trying to access */
  1856   1999     Bitmask notReady               /* Tables in outer loops of the join */
  1857   2000   ){
  1858   2001     char aff;
  1859   2002     if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
  1860   2003     if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
  1861   2004     if( (pTerm->prereqRight & notReady)!=0 ) return 0;
         2005  +  if( pTerm->u.leftColumn<0 ) return 0;
  1862   2006     aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
  1863   2007     if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
  1864   2008     return 1;
  1865   2009   }
  1866   2010   #endif
  1867   2011   
  1868         -#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  1869         -/*
  1870         -** If the query plan for pSrc specified in pCost is a full table scan
  1871         -** and indexing is allows (if there is no NOT INDEXED clause) and it
  1872         -** possible to construct a transient index that would perform better
  1873         -** than a full table scan even when the cost of constructing the index
  1874         -** is taken into account, then alter the query plan to use the
  1875         -** transient index.
  1876         -*/
  1877         -static void bestAutomaticIndex(WhereBestIdx *p){
  1878         -  Parse *pParse = p->pParse;            /* The parsing context */
  1879         -  WhereClause *pWC = p->pWC;            /* The WHERE clause */
  1880         -  struct SrcList_item *pSrc = p->pSrc;  /* The FROM clause term to search */
  1881         -  double nTableRow;                     /* Rows in the input table */
  1882         -  double logN;                          /* log(nTableRow) */
  1883         -  double costTempIdx;         /* per-query cost of the transient index */
  1884         -  WhereTerm *pTerm;           /* A single term of the WHERE clause */
  1885         -  WhereTerm *pWCEnd;          /* End of pWC->a[] */
  1886         -  Table *pTable;              /* Table tht might be indexed */
  1887         -
  1888         -  if( pParse->nQueryLoop<=(double)1 ){
  1889         -    /* There is no point in building an automatic index for a single scan */
  1890         -    return;
  1891         -  }
  1892         -  if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
  1893         -    /* Automatic indices are disabled at run-time */
  1894         -    return;
  1895         -  }
  1896         -  if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0
  1897         -   && (p->cost.plan.wsFlags & WHERE_COVER_SCAN)==0
  1898         -  ){
  1899         -    /* We already have some kind of index in use for this query. */
  1900         -    return;
  1901         -  }
  1902         -  if( pSrc->viaCoroutine ){
  1903         -    /* Cannot index a co-routine */
  1904         -    return;
  1905         -  }
  1906         -  if( pSrc->notIndexed ){
  1907         -    /* The NOT INDEXED clause appears in the SQL. */
  1908         -    return;
  1909         -  }
  1910         -  if( pSrc->isCorrelated ){
  1911         -    /* The source is a correlated sub-query. No point in indexing it. */
  1912         -    return;
  1913         -  }
  1914         -
  1915         -  assert( pParse->nQueryLoop >= (double)1 );
  1916         -  pTable = pSrc->pTab;
  1917         -  nTableRow = pTable->nRowEst;
  1918         -  logN = estLog(nTableRow);
  1919         -  costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
  1920         -  if( costTempIdx>=p->cost.rCost ){
  1921         -    /* The cost of creating the transient table would be greater than
  1922         -    ** doing the full table scan */
  1923         -    return;
  1924         -  }
  1925         -
  1926         -  /* Search for any equality comparison term */
  1927         -  pWCEnd = &pWC->a[pWC->nTerm];
  1928         -  for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  1929         -    if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
  1930         -      WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
  1931         -                    p->cost.rCost, costTempIdx));
  1932         -      p->cost.rCost = costTempIdx;
  1933         -      p->cost.plan.nRow = logN + 1;
  1934         -      p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
  1935         -      p->cost.used = pTerm->prereqRight;
  1936         -      break;
  1937         -    }
  1938         -  }
  1939         -}
  1940         -#else
  1941         -# define bestAutomaticIndex(A)  /* no-op */
  1942         -#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
  1943         -
  1944   2012   
  1945   2013   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  1946   2014   /*
  1947   2015   ** Generate code to construct the Index object for an automatic index
  1948   2016   ** and to set up the WhereLevel object pLevel so that the code generator
  1949   2017   ** makes use of the automatic index.
  1950   2018   */
................................................................................
  1966   2034     KeyInfo *pKeyinfo;          /* Key information for the index */   
  1967   2035     int addrTop;                /* Top of the index fill loop */
  1968   2036     int regRecord;              /* Register holding an index record */
  1969   2037     int n;                      /* Column counter */
  1970   2038     int i;                      /* Loop counter */
  1971   2039     int mxBitCol;               /* Maximum column in pSrc->colUsed */
  1972   2040     CollSeq *pColl;             /* Collating sequence to on a column */
         2041  +  WhereLoop *pLoop;           /* The Loop object */
  1973   2042     Bitmask idxCols;            /* Bitmap of columns used for indexing */
  1974   2043     Bitmask extraCols;          /* Bitmap of additional columns */
  1975   2044   
  1976   2045     /* Generate code to skip over the creation and initialization of the
  1977   2046     ** transient index on 2nd and subsequent iterations of the loop. */
  1978   2047     v = pParse->pVdbe;
  1979   2048     assert( v!=0 );
................................................................................
  1980   2049     addrInit = sqlite3CodeOnce(pParse);
  1981   2050   
  1982   2051     /* Count the number of columns that will be added to the index
  1983   2052     ** and used to match WHERE clause constraints */
  1984   2053     nColumn = 0;
  1985   2054     pTable = pSrc->pTab;
  1986   2055     pWCEnd = &pWC->a[pWC->nTerm];
         2056  +  pLoop = pLevel->pWLoop;
  1987   2057     idxCols = 0;
  1988   2058     for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  1989   2059       if( termCanDriveIndex(pTerm, pSrc, notReady) ){
  1990   2060         int iCol = pTerm->u.leftColumn;
  1991         -      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
         2061  +      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
  1992   2062         testcase( iCol==BMS );
  1993   2063         testcase( iCol==BMS-1 );
  1994   2064         if( (idxCols & cMask)==0 ){
  1995         -        nColumn++;
         2065  +        if( whereLoopResize(pParse->db, pLoop, nColumn+1) ) return;
         2066  +        pLoop->aLTerm[nColumn++] = pTerm;
  1996   2067           idxCols |= cMask;
  1997   2068         }
  1998   2069       }
  1999   2070     }
  2000   2071     assert( nColumn>0 );
  2001         -  pLevel->plan.nEq = nColumn;
         2072  +  pLoop->u.btree.nEq = pLoop->nLTerm = nColumn;
         2073  +  pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
         2074  +                     | WHERE_TEMP_INDEX;
  2002   2075   
  2003   2076     /* Count the number of additional columns needed to create a
  2004   2077     ** covering index.  A "covering index" is an index that contains all
  2005   2078     ** columns that are needed by the query.  With a covering index, the
  2006   2079     ** original table never needs to be accessed.  Automatic indices must
  2007   2080     ** be a covering index because the index will not be updated if the
  2008   2081     ** original table changes and the index and table cannot both be used
  2009   2082     ** if they go out of sync.
  2010   2083     */
  2011         -  extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1)));
         2084  +  extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
  2012   2085     mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
  2013   2086     testcase( pTable->nCol==BMS-1 );
  2014   2087     testcase( pTable->nCol==BMS-2 );
  2015   2088     for(i=0; i<mxBitCol; i++){
  2016         -    if( extraCols & (((Bitmask)1)<<i) ) nColumn++;
         2089  +    if( extraCols & MASKBIT(i) ) nColumn++;
  2017   2090     }
  2018         -  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
         2091  +  if( pSrc->colUsed & MASKBIT(BMS-1) ){
  2019   2092       nColumn += pTable->nCol - BMS + 1;
  2020   2093     }
  2021         -  pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
         2094  +  pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
  2022   2095   
  2023   2096     /* Construct the Index object to describe this index */
  2024   2097     nByte = sizeof(Index);
  2025   2098     nByte += nColumn*sizeof(int);     /* Index.aiColumn */
  2026   2099     nByte += nColumn*sizeof(char*);   /* Index.azColl */
  2027   2100     nByte += nColumn;                 /* Index.aSortOrder */
  2028   2101     pIdx = sqlite3DbMallocZero(pParse->db, nByte);
  2029   2102     if( pIdx==0 ) return;
  2030         -  pLevel->plan.u.pIdx = pIdx;
         2103  +  pLoop->u.btree.pIndex = pIdx;
  2031   2104     pIdx->azColl = (char**)&pIdx[1];
  2032   2105     pIdx->aiColumn = (int*)&pIdx->azColl[nColumn];
  2033   2106     pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn];
  2034   2107     pIdx->zName = "auto-index";
  2035   2108     pIdx->nColumn = nColumn;
  2036   2109     pIdx->pTable = pTable;
  2037   2110     n = 0;
  2038   2111     idxCols = 0;
  2039   2112     for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  2040   2113       if( termCanDriveIndex(pTerm, pSrc, notReady) ){
  2041   2114         int iCol = pTerm->u.leftColumn;
  2042         -      Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<<iCol;
         2115  +      Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
         2116  +      testcase( iCol==BMS-1 );
         2117  +      testcase( iCol==BMS );
  2043   2118         if( (idxCols & cMask)==0 ){
  2044   2119           Expr *pX = pTerm->pExpr;
  2045   2120           idxCols |= cMask;
  2046   2121           pIdx->aiColumn[n] = pTerm->u.leftColumn;
  2047   2122           pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
  2048   2123           pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
  2049   2124           n++;
  2050   2125         }
  2051   2126       }
  2052   2127     }
  2053         -  assert( (u32)n==pLevel->plan.nEq );
         2128  +  assert( (u32)n==pLoop->u.btree.nEq );
  2054   2129   
  2055   2130     /* Add additional columns needed to make the automatic index into
  2056   2131     ** a covering index */
  2057   2132     for(i=0; i<mxBitCol; i++){
  2058         -    if( extraCols & (((Bitmask)1)<<i) ){
         2133  +    if( extraCols & MASKBIT(i) ){
  2059   2134         pIdx->aiColumn[n] = i;
  2060   2135         pIdx->azColl[n] = "BINARY";
  2061   2136         n++;
  2062   2137       }
  2063   2138     }
  2064         -  if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){
         2139  +  if( pSrc->colUsed & MASKBIT(BMS-1) ){
  2065   2140       for(i=BMS-1; i<pTable->nCol; i++){
  2066   2141         pIdx->aiColumn[n] = i;
  2067   2142         pIdx->azColl[n] = "BINARY";
  2068   2143         n++;
  2069   2144       }
  2070   2145     }
  2071   2146     assert( n==nColumn );
  2072   2147   
  2073   2148     /* Create the automatic index */
  2074   2149     pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx);
  2075   2150     assert( pLevel->iIdxCur>=0 );
         2151  +  pLevel->iIdxCur = pParse->nTab++;
  2076   2152     sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0,
  2077   2153                       (char*)pKeyinfo, P4_KEYINFO_HANDOFF);
  2078   2154     VdbeComment((v, "for %s", pTable->zName));
  2079   2155   
  2080   2156     /* Fill the automatic index with content */
  2081   2157     addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
  2082   2158     regRecord = sqlite3GetTempReg(pParse);
................................................................................
  2095   2171   
  2096   2172   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2097   2173   /*
  2098   2174   ** Allocate and populate an sqlite3_index_info structure. It is the 
  2099   2175   ** responsibility of the caller to eventually release the structure
  2100   2176   ** by passing the pointer returned by this function to sqlite3_free().
  2101   2177   */
  2102         -static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
  2103         -  Parse *pParse = p->pParse; 
  2104         -  WhereClause *pWC = p->pWC;
  2105         -  struct SrcList_item *pSrc = p->pSrc;
  2106         -  ExprList *pOrderBy = p->pOrderBy;
         2178  +static sqlite3_index_info *allocateIndexInfo(
         2179  +  Parse *pParse,
         2180  +  WhereClause *pWC,
         2181  +  struct SrcList_item *pSrc,
         2182  +  ExprList *pOrderBy
         2183  +){
  2107   2184     int i, j;
  2108   2185     int nTerm;
  2109   2186     struct sqlite3_index_constraint *pIdxCons;
  2110   2187     struct sqlite3_index_orderby *pIdxOrderBy;
  2111   2188     struct sqlite3_index_constraint_usage *pUsage;
  2112   2189     WhereTerm *pTerm;
  2113   2190     int nOrderBy;
  2114   2191     sqlite3_index_info *pIdxInfo;
  2115   2192   
  2116         -  WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName));
  2117         -
  2118   2193     /* Count the number of possible WHERE clause constraints referring
  2119   2194     ** to this virtual table */
  2120   2195     for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  2121   2196       if( pTerm->leftCursor != pSrc->iCursor ) continue;
  2122   2197       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
  2123   2198       testcase( pTerm->eOperator & WO_IN );
  2124   2199       testcase( pTerm->eOperator & WO_ISNULL );
................................................................................
  2146   2221     /* Allocate the sqlite3_index_info structure
  2147   2222     */
  2148   2223     pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
  2149   2224                              + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
  2150   2225                              + sizeof(*pIdxOrderBy)*nOrderBy );
  2151   2226     if( pIdxInfo==0 ){
  2152   2227       sqlite3ErrorMsg(pParse, "out of memory");
  2153         -    /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  2154   2228       return 0;
  2155   2229     }
  2156   2230   
  2157   2231     /* Initialize the structure.  The sqlite3_index_info structure contains
  2158   2232     ** many fields that are declared "const" to prevent xBestIndex from
  2159   2233     ** changing them.  We have to do some funky casting in order to
  2160   2234     ** initialize those fields.
................................................................................
  2202   2276   
  2203   2277     return pIdxInfo;
  2204   2278   }
  2205   2279   
  2206   2280   /*
  2207   2281   ** The table object reference passed as the second argument to this function
  2208   2282   ** must represent a virtual table. This function invokes the xBestIndex()
  2209         -** method of the virtual table with the sqlite3_index_info pointer passed
  2210         -** as the argument.
         2283  +** method of the virtual table with the sqlite3_index_info object that
         2284  +** comes in as the 3rd argument to this function.
  2211   2285   **
  2212   2286   ** If an error occurs, pParse is populated with an error message and a
  2213   2287   ** non-zero value is returned. Otherwise, 0 is returned and the output
  2214   2288   ** part of the sqlite3_index_info structure is left populated.
  2215   2289   **
  2216   2290   ** Whether or not an error is returned, it is the responsibility of the
  2217   2291   ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
................................................................................
  2218   2292   ** that this is required.
  2219   2293   */
  2220   2294   static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
  2221   2295     sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
  2222   2296     int i;
  2223   2297     int rc;
  2224   2298   
  2225         -  WHERETRACE(("xBestIndex for %s\n", pTab->zName));
  2226   2299     TRACE_IDX_INPUTS(p);
  2227   2300     rc = pVtab->pModule->xBestIndex(pVtab, p);
  2228   2301     TRACE_IDX_OUTPUTS(p);
  2229   2302   
  2230   2303     if( rc!=SQLITE_OK ){
  2231   2304       if( rc==SQLITE_NOMEM ){
  2232   2305         pParse->db->mallocFailed = 1;
................................................................................
  2244   2317         sqlite3ErrorMsg(pParse, 
  2245   2318             "table %s: xBestIndex returned an invalid plan", pTab->zName);
  2246   2319       }
  2247   2320     }
  2248   2321   
  2249   2322     return pParse->nErr;
  2250   2323   }
  2251         -
  2252         -
  2253         -/*
  2254         -** Compute the best index for a virtual table.
  2255         -**
  2256         -** The best index is computed by the xBestIndex method of the virtual
  2257         -** table module.  This routine is really just a wrapper that sets up
  2258         -** the sqlite3_index_info structure that is used to communicate with
  2259         -** xBestIndex.
  2260         -**
  2261         -** In a join, this routine might be called multiple times for the
  2262         -** same virtual table.  The sqlite3_index_info structure is created
  2263         -** and initialized on the first invocation and reused on all subsequent
  2264         -** invocations.  The sqlite3_index_info structure is also used when
  2265         -** code is generated to access the virtual table.  The whereInfoDelete() 
  2266         -** routine takes care of freeing the sqlite3_index_info structure after
  2267         -** everybody has finished with it.
  2268         -*/
  2269         -static void bestVirtualIndex(WhereBestIdx *p){
  2270         -  Parse *pParse = p->pParse;      /* The parsing context */
  2271         -  WhereClause *pWC = p->pWC;      /* The WHERE clause */
  2272         -  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
  2273         -  Table *pTab = pSrc->pTab;
  2274         -  sqlite3_index_info *pIdxInfo;
  2275         -  struct sqlite3_index_constraint *pIdxCons;
  2276         -  struct sqlite3_index_constraint_usage *pUsage;
  2277         -  WhereTerm *pTerm;
  2278         -  int i, j;
  2279         -  int nOrderBy;
  2280         -  int bAllowIN;                   /* Allow IN optimizations */
  2281         -  double rCost;
  2282         -
  2283         -  /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
  2284         -  ** malloc in allocateIndexInfo() fails and this function returns leaving
  2285         -  ** wsFlags in an uninitialized state, the caller may behave unpredictably.
  2286         -  */
  2287         -  memset(&p->cost, 0, sizeof(p->cost));
  2288         -  p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
  2289         -
  2290         -  /* If the sqlite3_index_info structure has not been previously
  2291         -  ** allocated and initialized, then allocate and initialize it now.
  2292         -  */
  2293         -  pIdxInfo = *p->ppIdxInfo;
  2294         -  if( pIdxInfo==0 ){
  2295         -    *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
  2296         -  }
  2297         -  if( pIdxInfo==0 ){
  2298         -    return;
  2299         -  }
  2300         -
  2301         -  /* At this point, the sqlite3_index_info structure that pIdxInfo points
  2302         -  ** to will have been initialized, either during the current invocation or
  2303         -  ** during some prior invocation.  Now we just have to customize the
  2304         -  ** details of pIdxInfo for the current invocation and pass it to
  2305         -  ** xBestIndex.
  2306         -  */
  2307         -
  2308         -  /* The module name must be defined. Also, by this point there must
  2309         -  ** be a pointer to an sqlite3_vtab structure. Otherwise
  2310         -  ** sqlite3ViewGetColumnNames() would have picked up the error. 
  2311         -  */
  2312         -  assert( pTab->azModuleArg && pTab->azModuleArg[0] );
  2313         -  assert( sqlite3GetVTable(pParse->db, pTab) );
  2314         -
  2315         -  /* Try once or twice.  On the first attempt, allow IN optimizations.
  2316         -  ** If an IN optimization is accepted by the virtual table xBestIndex
  2317         -  ** method, but the  pInfo->aConstrainUsage.omit flag is not set, then
  2318         -  ** the query will not work because it might allow duplicate rows in
  2319         -  ** output.  In that case, run the xBestIndex method a second time
  2320         -  ** without the IN constraints.  Usually this loop only runs once.
  2321         -  ** The loop will exit using a "break" statement.
  2322         -  */
  2323         -  for(bAllowIN=1; 1; bAllowIN--){
  2324         -    assert( bAllowIN==0 || bAllowIN==1 );
  2325         -
  2326         -    /* Set the aConstraint[].usable fields and initialize all 
  2327         -    ** output variables to zero.
  2328         -    **
  2329         -    ** aConstraint[].usable is true for constraints where the right-hand
  2330         -    ** side contains only references to tables to the left of the current
  2331         -    ** table.  In other words, if the constraint is of the form:
  2332         -    **
  2333         -    **           column = expr
  2334         -    **
  2335         -    ** and we are evaluating a join, then the constraint on column is 
  2336         -    ** only valid if all tables referenced in expr occur to the left
  2337         -    ** of the table containing column.
  2338         -    **
  2339         -    ** The aConstraints[] array contains entries for all constraints
  2340         -    ** on the current table.  That way we only have to compute it once
  2341         -    ** even though we might try to pick the best index multiple times.
  2342         -    ** For each attempt at picking an index, the order of tables in the
  2343         -    ** join might be different so we have to recompute the usable flag
  2344         -    ** each time.
  2345         -    */
  2346         -    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
  2347         -    pUsage = pIdxInfo->aConstraintUsage;
  2348         -    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
  2349         -      j = pIdxCons->iTermOffset;
  2350         -      pTerm = &pWC->a[j];
  2351         -      if( (pTerm->prereqRight&p->notReady)==0
  2352         -       && (bAllowIN || (pTerm->eOperator & WO_IN)==0)
  2353         -      ){
  2354         -        pIdxCons->usable = 1;
  2355         -      }else{
  2356         -        pIdxCons->usable = 0;
  2357         -      }
  2358         -    }
  2359         -    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
  2360         -    if( pIdxInfo->needToFreeIdxStr ){
  2361         -      sqlite3_free(pIdxInfo->idxStr);
  2362         -    }
  2363         -    pIdxInfo->idxStr = 0;
  2364         -    pIdxInfo->idxNum = 0;
  2365         -    pIdxInfo->needToFreeIdxStr = 0;
  2366         -    pIdxInfo->orderByConsumed = 0;
  2367         -    /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
  2368         -    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
  2369         -    nOrderBy = pIdxInfo->nOrderBy;
  2370         -    if( !p->pOrderBy ){
  2371         -      pIdxInfo->nOrderBy = 0;
  2372         -    }
  2373         -  
  2374         -    if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
  2375         -      return;
  2376         -    }
  2377         -  
  2378         -    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
  2379         -    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
  2380         -      if( pUsage[i].argvIndex>0 ){
  2381         -        j = pIdxCons->iTermOffset;
  2382         -        pTerm = &pWC->a[j];
  2383         -        p->cost.used |= pTerm->prereqRight;
  2384         -        if( (pTerm->eOperator & WO_IN)!=0 ){
  2385         -          if( pUsage[i].omit==0 ){
  2386         -            /* Do not attempt to use an IN constraint if the virtual table
  2387         -            ** says that the equivalent EQ constraint cannot be safely omitted.
  2388         -            ** If we do attempt to use such a constraint, some rows might be
  2389         -            ** repeated in the output. */
  2390         -            break;
  2391         -          }
  2392         -          /* A virtual table that is constrained by an IN clause may not
  2393         -          ** consume the ORDER BY clause because (1) the order of IN terms
  2394         -          ** is not necessarily related to the order of output terms and
  2395         -          ** (2) Multiple outputs from a single IN value will not merge
  2396         -          ** together.  */
  2397         -          pIdxInfo->orderByConsumed = 0;
  2398         -        }
  2399         -      }
  2400         -    }
  2401         -    if( i>=pIdxInfo->nConstraint ) break;
  2402         -  }
  2403         -
  2404         -  /* The orderByConsumed signal is only valid if all outer loops collectively
  2405         -  ** generate just a single row of output.
  2406         -  */
  2407         -  if( pIdxInfo->orderByConsumed ){
  2408         -    for(i=0; i<p->i; i++){
  2409         -      if( (p->aLevel[i].plan.wsFlags & WHERE_UNIQUE)==0 ){
  2410         -        pIdxInfo->orderByConsumed = 0;
  2411         -      }
  2412         -    }
  2413         -  }
  2414         -  
  2415         -  /* If there is an ORDER BY clause, and the selected virtual table index
  2416         -  ** does not satisfy it, increase the cost of the scan accordingly. This
  2417         -  ** matches the processing for non-virtual tables in bestBtreeIndex().
  2418         -  */
  2419         -  rCost = pIdxInfo->estimatedCost;
  2420         -  if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
  2421         -    rCost += estLog(rCost)*rCost;
  2422         -  }
  2423         -
  2424         -  /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
  2425         -  ** inital value of lowestCost in this loop. If it is, then the
  2426         -  ** (cost<lowestCost) test below will never be true.
  2427         -  ** 
  2428         -  ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
  2429         -  ** is defined.
  2430         -  */
  2431         -  if( (SQLITE_BIG_DBL/((double)2))<rCost ){
  2432         -    p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
  2433         -  }else{
  2434         -    p->cost.rCost = rCost;
  2435         -  }
  2436         -  p->cost.plan.u.pVtabIdx = pIdxInfo;
  2437         -  if( pIdxInfo->orderByConsumed ){
  2438         -    p->cost.plan.wsFlags |= WHERE_ORDERED;
  2439         -    p->cost.plan.nOBSat = nOrderBy;
  2440         -  }else{
  2441         -    p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
  2442         -  }
  2443         -  p->cost.plan.nEq = 0;
  2444         -  pIdxInfo->nOrderBy = nOrderBy;
  2445         -
  2446         -  /* Try to find a more efficient access pattern by using multiple indexes
  2447         -  ** to optimize an OR expression within the WHERE clause. 
  2448         -  */
  2449         -  bestOrClauseIndex(p);
  2450         -}
  2451         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
         2324  +#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
         2325  +
  2452   2326   
  2453   2327   #ifdef SQLITE_ENABLE_STAT3
  2454   2328   /*
  2455   2329   ** Estimate the location of a particular key among all keys in an
  2456   2330   ** index.  Store the results in aStat as follows:
  2457   2331   **
  2458   2332   **    aStat[0]      Est. number of rows less than pVal
................................................................................
  2531   2405         const u8 *z;
  2532   2406         if( eType==SQLITE_BLOB ){
  2533   2407           z = (const u8 *)sqlite3_value_blob(pVal);
  2534   2408           pColl = db->pDfltColl;
  2535   2409           assert( pColl->enc==SQLITE_UTF8 );
  2536   2410         }else{
  2537   2411           pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
  2538         -        if( pColl==0 ){
  2539         -          return SQLITE_ERROR;
  2540         -        }
         2412  +        /* If the collating sequence was unavailable, we should have failed
         2413  +        ** long ago and never reached this point.  But we'll check just to
         2414  +        ** be doubly sure. */
         2415  +        if( NEVER(pColl==0) ) return SQLITE_ERROR;
  2541   2416           z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
  2542   2417           if( !z ){
  2543   2418             return SQLITE_NOMEM;
  2544   2419           }
  2545   2420           assert( z && pColl && pColl->xCmp );
  2546   2421         }
  2547   2422         n = sqlite3ValueBytes(pVal, pColl->enc);
................................................................................
  2686   2561   */
  2687   2562   static int whereRangeScanEst(
  2688   2563     Parse *pParse,       /* Parsing & code generating context */
  2689   2564     Index *p,            /* The index containing the range-compared column; "x" */
  2690   2565     int nEq,             /* index into p->aCol[] of the range-compared column */
  2691   2566     WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
  2692   2567     WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
  2693         -  double *pRangeDiv   /* OUT: Reduce search space by this divisor */
         2568  +  WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */
  2694   2569   ){
  2695   2570     int rc = SQLITE_OK;
  2696   2571   
  2697   2572   #ifdef SQLITE_ENABLE_STAT3
  2698   2573   
  2699   2574     if( nEq==0 && p->nSample ){
  2700   2575       sqlite3_value *pRangeVal;
................................................................................
  2724   2599         ){
  2725   2600           iUpper = a[0];
  2726   2601           if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1];
  2727   2602         }
  2728   2603         sqlite3ValueFree(pRangeVal);
  2729   2604       }
  2730   2605       if( rc==SQLITE_OK ){
  2731         -      if( iUpper<=iLower ){
  2732         -        *pRangeDiv = (double)p->aiRowEst[0];
  2733         -      }else{
  2734         -        *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
         2606  +      WhereCost iBase = whereCost(p->aiRowEst[0]);
         2607  +      if( iUpper>iLower ){
         2608  +        iBase -= whereCost(iUpper - iLower);
  2735   2609         }
  2736         -      WHERETRACE(("range scan regions: %u..%u  div=%g\n",
  2737         -                  (u32)iLower, (u32)iUpper, *pRangeDiv));
         2610  +      *pRangeDiv = iBase;
         2611  +      WHERETRACE(0x100, ("range scan regions: %u..%u  div=%d\n",
         2612  +                         (u32)iLower, (u32)iUpper, *pRangeDiv));
  2738   2613         return SQLITE_OK;
  2739   2614       }
  2740   2615     }
  2741   2616   #else
  2742   2617     UNUSED_PARAMETER(pParse);
  2743   2618     UNUSED_PARAMETER(p);
  2744   2619     UNUSED_PARAMETER(nEq);
  2745   2620   #endif
  2746   2621     assert( pLower || pUpper );
  2747         -  *pRangeDiv = (double)1;
  2748         -  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
  2749         -  if( pUpper ) *pRangeDiv *= (double)4;
         2622  +  *pRangeDiv = 0;
         2623  +  /* TUNING:  Each inequality constraint reduces the search space 4-fold.
         2624  +  ** A BETWEEN operator, therefore, reduces the search space 16-fold */
         2625  +  if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
         2626  +    *pRangeDiv += 20;  assert( 20==whereCost(4) );
         2627  +  }
         2628  +  if( pUpper ){
         2629  +    *pRangeDiv += 20;  assert( 20==whereCost(4) );
         2630  +  }
  2750   2631     return rc;
  2751   2632   }
  2752   2633   
  2753   2634   #ifdef SQLITE_ENABLE_STAT3
  2754   2635   /*
  2755   2636   ** Estimate the number of rows that will be returned based on
  2756   2637   ** an equality constraint x=VALUE and where that VALUE occurs in
................................................................................
  2768   2649   ** for a UTF conversion required for comparison.  The error is stored
  2769   2650   ** in the pParse structure.
  2770   2651   */
  2771   2652   static int whereEqualScanEst(
  2772   2653     Parse *pParse,       /* Parsing & code generating context */
  2773   2654     Index *p,            /* The index whose left-most column is pTerm */
  2774   2655     Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
  2775         -  double *pnRow        /* Write the revised row estimate here */
         2656  +  tRowcnt *pnRow       /* Write the revised row estimate here */
  2776   2657   ){
  2777   2658     sqlite3_value *pRhs = 0;  /* VALUE on right-hand side of pTerm */
  2778   2659     u8 aff;                   /* Column affinity */
  2779   2660     int rc;                   /* Subfunction return code */
  2780   2661     tRowcnt a[2];             /* Statistics */
  2781   2662   
  2782   2663     assert( p->aSample!=0 );
................................................................................
  2787   2668       if( rc ) goto whereEqualScanEst_cancel;
  2788   2669     }else{
  2789   2670       pRhs = sqlite3ValueNew(pParse->db);
  2790   2671     }
  2791   2672     if( pRhs==0 ) return SQLITE_NOTFOUND;
  2792   2673     rc = whereKeyStats(pParse, p, pRhs, 0, a);
  2793   2674     if( rc==SQLITE_OK ){
  2794         -    WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
         2675  +    WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
  2795   2676       *pnRow = a[1];
  2796   2677     }
  2797   2678   whereEqualScanEst_cancel:
  2798   2679     sqlite3ValueFree(pRhs);
  2799   2680     return rc;
  2800   2681   }
  2801   2682   #endif /* defined(SQLITE_ENABLE_STAT3) */
................................................................................
  2817   2698   ** for a UTF conversion required for comparison.  The error is stored
  2818   2699   ** in the pParse structure.
  2819   2700   */
  2820   2701   static int whereInScanEst(
  2821   2702     Parse *pParse,       /* Parsing & code generating context */
  2822   2703     Index *p,            /* The index whose left-most column is pTerm */
  2823   2704     ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
  2824         -  double *pnRow        /* Write the revised row estimate here */
         2705  +  tRowcnt *pnRow       /* Write the revised row estimate here */
  2825   2706   ){
  2826         -  int rc = SQLITE_OK;         /* Subfunction return code */
  2827         -  double nEst;                /* Number of rows for a single term */
  2828         -  double nRowEst = (double)0; /* New estimate of the number of rows */
  2829         -  int i;                      /* Loop counter */
         2707  +  int rc = SQLITE_OK;     /* Subfunction return code */
         2708  +  tRowcnt nEst;           /* Number of rows for a single term */
         2709  +  tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
         2710  +  int i;                  /* Loop counter */
  2830   2711   
  2831   2712     assert( p->aSample!=0 );
  2832   2713     for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
  2833   2714       nEst = p->aiRowEst[0];
  2834   2715       rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
  2835   2716       nRowEst += nEst;
  2836   2717     }
  2837   2718     if( rc==SQLITE_OK ){
  2838   2719       if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
  2839   2720       *pnRow = nRowEst;
  2840         -    WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
         2721  +    WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
  2841   2722     }
  2842   2723     return rc;
  2843   2724   }
  2844   2725   #endif /* defined(SQLITE_ENABLE_STAT3) */
  2845   2726   
  2846         -/*
  2847         -** Check to see if column iCol of the table with cursor iTab will appear
  2848         -** in sorted order according to the current query plan.
  2849         -**
  2850         -** Return values:
  2851         -**
  2852         -**    0   iCol is not ordered
  2853         -**    1   iCol has only a single value
  2854         -**    2   iCol is in ASC order
  2855         -**    3   iCol is in DESC order
  2856         -*/
  2857         -static int isOrderedColumn(
  2858         -  WhereBestIdx *p,
  2859         -  int iTab,
  2860         -  int iCol
  2861         -){
  2862         -  int i, j;
  2863         -  WhereLevel *pLevel = &p->aLevel[p->i-1];
  2864         -  Index *pIdx;
  2865         -  u8 sortOrder;
  2866         -  for(i=p->i-1; i>=0; i--, pLevel--){
  2867         -    if( pLevel->iTabCur!=iTab ) continue;
  2868         -    if( (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
  2869         -      return 1;
  2870         -    }
  2871         -    assert( (pLevel->plan.wsFlags & WHERE_ORDERED)!=0 );
  2872         -    if( (pIdx = pLevel->plan.u.pIdx)!=0 ){
  2873         -      if( iCol<0 ){
  2874         -        sortOrder = 0;
  2875         -        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
  2876         -      }else{
  2877         -        int n = pIdx->nColumn;
  2878         -        for(j=0; j<n; j++){
  2879         -          if( iCol==pIdx->aiColumn[j] ) break;
  2880         -        }
  2881         -        if( j>=n ) return 0;
  2882         -        sortOrder = pIdx->aSortOrder[j];
  2883         -        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
  2884         -      }
  2885         -    }else{
  2886         -      if( iCol!=(-1) ) return 0;
  2887         -      sortOrder = 0;
  2888         -      testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
  2889         -    }
  2890         -    if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
  2891         -      assert( sortOrder==0 || sortOrder==1 );
  2892         -      testcase( sortOrder==1 );
  2893         -      sortOrder = 1 - sortOrder;
  2894         -    }
  2895         -    return sortOrder+2;
  2896         -  }
  2897         -  return 0;
  2898         -}
  2899         -
  2900         -/*
  2901         -** This routine decides if pIdx can be used to satisfy the ORDER BY
  2902         -** clause, either in whole or in part.  The return value is the 
  2903         -** cumulative number of terms in the ORDER BY clause that are satisfied
  2904         -** by the index pIdx and other indices in outer loops.
  2905         -**
  2906         -** The table being queried has a cursor number of "base".  pIdx is the
  2907         -** index that is postulated for use to access the table.
  2908         -**
  2909         -** The *pbRev value is set to 0 order 1 depending on whether or not
  2910         -** pIdx should be run in the forward order or in reverse order.
  2911         -*/
  2912         -static int isSortingIndex(
  2913         -  WhereBestIdx *p,    /* Best index search context */
  2914         -  Index *pIdx,        /* The index we are testing */
  2915         -  int base,           /* Cursor number for the table to be sorted */
  2916         -  int *pbRev,         /* Set to 1 for reverse-order scan of pIdx */
  2917         -  int *pbObUnique     /* ORDER BY column values will different in every row */
  2918         -){
  2919         -  int i;                        /* Number of pIdx terms used */
  2920         -  int j;                        /* Number of ORDER BY terms satisfied */
  2921         -  int sortOrder = 2;            /* 0: forward.  1: backward.  2: unknown */
  2922         -  int nTerm;                    /* Number of ORDER BY terms */
  2923         -  struct ExprList_item *pOBItem;/* A term of the ORDER BY clause */
  2924         -  Table *pTab = pIdx->pTable;   /* Table that owns index pIdx */
  2925         -  ExprList *pOrderBy;           /* The ORDER BY clause */
  2926         -  Parse *pParse = p->pParse;    /* Parser context */
  2927         -  sqlite3 *db = pParse->db;     /* Database connection */
  2928         -  int nPriorSat;                /* ORDER BY terms satisfied by outer loops */
  2929         -  int seenRowid = 0;            /* True if an ORDER BY rowid term is seen */
  2930         -  int uniqueNotNull;            /* pIdx is UNIQUE with all terms are NOT NULL */
  2931         -  int outerObUnique;            /* Outer loops generate different values in
  2932         -                                ** every row for the ORDER BY columns */
  2933         -
  2934         -  if( p->i==0 ){
  2935         -    nPriorSat = 0;
  2936         -    outerObUnique = 1;
  2937         -  }else{
  2938         -    u32 wsFlags = p->aLevel[p->i-1].plan.wsFlags;
  2939         -    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
  2940         -    if( (wsFlags & WHERE_ORDERED)==0 ){
  2941         -      /* This loop cannot be ordered unless the next outer loop is
  2942         -      ** also ordered */
  2943         -      return nPriorSat;
  2944         -    }
  2945         -    if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){
  2946         -      /* Only look at the outer-most loop if the OrderByIdxJoin
  2947         -      ** optimization is disabled */
  2948         -      return nPriorSat;
  2949         -    }
  2950         -    testcase( wsFlags & WHERE_OB_UNIQUE );
  2951         -    testcase( wsFlags & WHERE_ALL_UNIQUE );
  2952         -    outerObUnique = (wsFlags & (WHERE_OB_UNIQUE|WHERE_ALL_UNIQUE))!=0;
  2953         -  }
  2954         -  pOrderBy = p->pOrderBy;
  2955         -  assert( pOrderBy!=0 );
  2956         -  if( pIdx->bUnordered ){
  2957         -    /* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot
  2958         -    ** be used for sorting */
  2959         -    return nPriorSat;
  2960         -  }
  2961         -  nTerm = pOrderBy->nExpr;
  2962         -  uniqueNotNull = pIdx->onError!=OE_None;
  2963         -  assert( nTerm>0 );
  2964         -
  2965         -  /* Argument pIdx must either point to a 'real' named index structure, 
  2966         -  ** or an index structure allocated on the stack by bestBtreeIndex() to
  2967         -  ** represent the rowid index that is part of every table.  */
  2968         -  assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
  2969         -
  2970         -  /* Match terms of the ORDER BY clause against columns of
  2971         -  ** the index.
  2972         -  **
  2973         -  ** Note that indices have pIdx->nColumn regular columns plus
  2974         -  ** one additional column containing the rowid.  The rowid column
  2975         -  ** of the index is also allowed to match against the ORDER BY
  2976         -  ** clause.
  2977         -  */
  2978         -  j = nPriorSat;
  2979         -  for(i=0,pOBItem=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
  2980         -    Expr *pOBExpr;          /* The expression of the ORDER BY pOBItem */
  2981         -    CollSeq *pColl;         /* The collating sequence of pOBExpr */
  2982         -    int termSortOrder;      /* Sort order for this term */
  2983         -    int iColumn;            /* The i-th column of the index.  -1 for rowid */
  2984         -    int iSortOrder;         /* 1 for DESC, 0 for ASC on the i-th index term */
  2985         -    int isEq;               /* Subject to an == or IS NULL constraint */
  2986         -    int isMatch;            /* ORDER BY term matches the index term */
  2987         -    const char *zColl;      /* Name of collating sequence for i-th index term */
  2988         -    WhereTerm *pConstraint; /* A constraint in the WHERE clause */
  2989         -
  2990         -    /* If the next term of the ORDER BY clause refers to anything other than
  2991         -    ** a column in the "base" table, then this index will not be of any
  2992         -    ** further use in handling the ORDER BY. */
  2993         -    pOBExpr = sqlite3ExprSkipCollate(pOBItem->pExpr);
  2994         -    if( pOBExpr->op!=TK_COLUMN || pOBExpr->iTable!=base ){
  2995         -      break;
  2996         -    }
  2997         -
  2998         -    /* Find column number and collating sequence for the next entry
  2999         -    ** in the index */
  3000         -    if( pIdx->zName && i<pIdx->nColumn ){
  3001         -      iColumn = pIdx->aiColumn[i];
  3002         -      if( iColumn==pIdx->pTable->iPKey ){
  3003         -        iColumn = -1;
  3004         -      }
  3005         -      iSortOrder = pIdx->aSortOrder[i];
  3006         -      zColl = pIdx->azColl[i];
  3007         -      assert( zColl!=0 );
  3008         -    }else{
  3009         -      iColumn = -1;
  3010         -      iSortOrder = 0;
  3011         -      zColl = 0;
  3012         -    }
  3013         -
  3014         -    /* Check to see if the column number and collating sequence of the
  3015         -    ** index match the column number and collating sequence of the ORDER BY
  3016         -    ** clause entry.  Set isMatch to 1 if they both match. */
  3017         -    if( pOBExpr->iColumn==iColumn ){
  3018         -      if( zColl ){
  3019         -        pColl = sqlite3ExprCollSeq(pParse, pOBItem->pExpr);
  3020         -        if( !pColl ) pColl = db->pDfltColl;
  3021         -        isMatch = sqlite3StrICmp(pColl->zName, zColl)==0;
  3022         -      }else{
  3023         -        isMatch = 1;
  3024         -      }
  3025         -    }else{
  3026         -      isMatch = 0;
  3027         -    }
  3028         -
  3029         -    /* termSortOrder is 0 or 1 for whether or not the access loop should
  3030         -    ** run forward or backwards (respectively) in order to satisfy this 
  3031         -    ** term of the ORDER BY clause. */
  3032         -    assert( pOBItem->sortOrder==0 || pOBItem->sortOrder==1 );
  3033         -    assert( iSortOrder==0 || iSortOrder==1 );
  3034         -    termSortOrder = iSortOrder ^ pOBItem->sortOrder;
  3035         -
  3036         -    /* If X is the column in the index and ORDER BY clause, check to see
  3037         -    ** if there are any X= or X IS NULL constraints in the WHERE clause. */
  3038         -    pConstraint = findTerm(p->pWC, base, iColumn, p->notReady,
  3039         -                           WO_EQ|WO_ISNULL|WO_IN, pIdx);
  3040         -    if( pConstraint==0 ){
  3041         -      isEq = 0;
  3042         -    }else if( (pConstraint->eOperator & WO_IN)!=0 ){
  3043         -      isEq = 0;
  3044         -    }else if( (pConstraint->eOperator & WO_ISNULL)!=0 ){
  3045         -      uniqueNotNull = 0;
  3046         -      isEq = 1;  /* "X IS NULL" means X has only a single value */
  3047         -    }else if( pConstraint->prereqRight==0 ){
  3048         -      isEq = 1;  /* Constraint "X=constant" means X has only a single value */
  3049         -    }else{
  3050         -      Expr *pRight = pConstraint->pExpr->pRight;
  3051         -      if( pRight->op==TK_COLUMN ){
  3052         -        WHERETRACE(("       .. isOrderedColumn(tab=%d,col=%d)",
  3053         -                    pRight->iTable, pRight->iColumn));
  3054         -        isEq = isOrderedColumn(p, pRight->iTable, pRight->iColumn);
  3055         -        WHERETRACE((" -> isEq=%d\n", isEq));
  3056         -
  3057         -        /* If the constraint is of the form X=Y where Y is an ordered value
  3058         -        ** in an outer loop, then make sure the sort order of Y matches the
  3059         -        ** sort order required for X. */
  3060         -        if( isMatch && isEq>=2 && isEq!=pOBItem->sortOrder+2 ){
  3061         -          testcase( isEq==2 );
  3062         -          testcase( isEq==3 );
  3063         -          break;
  3064         -        }
  3065         -      }else{
  3066         -        isEq = 0;  /* "X=expr" places no ordering constraints on X */
  3067         -      }
  3068         -    }
  3069         -    if( !isMatch ){
  3070         -      if( isEq==0 ){
  3071         -        break;
  3072         -      }else{
  3073         -        continue;
  3074         -      }
  3075         -    }else if( isEq!=1 ){
  3076         -      if( sortOrder==2 ){
  3077         -        sortOrder = termSortOrder;
  3078         -      }else if( termSortOrder!=sortOrder ){
  3079         -        break;
  3080         -      }
  3081         -    }
  3082         -    j++;
  3083         -    pOBItem++;
  3084         -    if( iColumn<0 ){
  3085         -      seenRowid = 1;
  3086         -      break;
  3087         -    }else if( pTab->aCol[iColumn].notNull==0 && isEq!=1 ){
  3088         -      testcase( isEq==0 );
  3089         -      testcase( isEq==2 );
  3090         -      testcase( isEq==3 );
  3091         -      uniqueNotNull = 0;
  3092         -    }
  3093         -  }
  3094         -  if( seenRowid ){
  3095         -    uniqueNotNull = 1;
  3096         -  }else if( uniqueNotNull==0 || i<pIdx->nColumn ){
  3097         -    uniqueNotNull = 0;
  3098         -  }
  3099         -
  3100         -  /* If we have not found at least one ORDER BY term that matches the
  3101         -  ** index, then show no progress. */
  3102         -  if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat;
  3103         -
  3104         -  /* Either the outer queries must generate rows where there are no two
  3105         -  ** rows with the same values in all ORDER BY columns, or else this
  3106         -  ** loop must generate just a single row of output.  Example:  Suppose
  3107         -  ** the outer loops generate A=1 and A=1, and this loop generates B=3
  3108         -  ** and B=4.  Then without the following test, ORDER BY A,B would 
  3109         -  ** generate the wrong order output: 1,3 1,4 1,3 1,4
  3110         -  */
  3111         -  if( outerObUnique==0 && uniqueNotNull==0 ) return nPriorSat;
  3112         -  *pbObUnique = uniqueNotNull;
  3113         -
  3114         -  /* Return the necessary scan order back to the caller */
  3115         -  *pbRev = sortOrder & 1;
  3116         -
  3117         -  /* If there was an "ORDER BY rowid" term that matched, or it is only
  3118         -  ** possible for a single row from this table to match, then skip over
  3119         -  ** any additional ORDER BY terms dealing with this table.
  3120         -  */
  3121         -  if( uniqueNotNull ){
  3122         -    /* Advance j over additional ORDER BY terms associated with base */
  3123         -    WhereMaskSet *pMS = p->pWC->pMaskSet;
  3124         -    Bitmask m = ~getMask(pMS, base);
  3125         -    while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
  3126         -      j++;
  3127         -    }
  3128         -  }
  3129         -  return j;
  3130         -}
  3131         -
  3132         -/*
  3133         -** Find the best query plan for accessing a particular table.  Write the
  3134         -** best query plan and its cost into the p->cost.
  3135         -**
  3136         -** The lowest cost plan wins.  The cost is an estimate of the amount of
  3137         -** CPU and disk I/O needed to process the requested result.
  3138         -** Factors that influence cost include:
  3139         -**
  3140         -**    *  The estimated number of rows that will be retrieved.  (The
  3141         -**       fewer the better.)
  3142         -**
  3143         -**    *  Whether or not sorting must occur.
  3144         -**
  3145         -**    *  Whether or not there must be separate lookups in the
  3146         -**       index and in the main table.
  3147         -**
  3148         -** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in
  3149         -** the SQL statement, then this function only considers plans using the 
  3150         -** named index. If no such plan is found, then the returned cost is
  3151         -** SQLITE_BIG_DBL. If a plan is found that uses the named index, 
  3152         -** then the cost is calculated in the usual way.
  3153         -**
  3154         -** If a NOT INDEXED clause was attached to the table 
  3155         -** in the SELECT statement, then no indexes are considered. However, the 
  3156         -** selected plan may still take advantage of the built-in rowid primary key
  3157         -** index.
  3158         -*/
  3159         -static void bestBtreeIndex(WhereBestIdx *p){
  3160         -  Parse *pParse = p->pParse;  /* The parsing context */
  3161         -  WhereClause *pWC = p->pWC;  /* The WHERE clause */
  3162         -  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
  3163         -  int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
  3164         -  Index *pProbe;              /* An index we are evaluating */
  3165         -  Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
  3166         -  int eqTermMask;             /* Current mask of valid equality operators */
  3167         -  int idxEqTermMask;          /* Index mask of valid equality operators */
  3168         -  Index sPk;                  /* A fake index object for the primary key */
  3169         -  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
  3170         -  int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
  3171         -  int wsFlagMask;             /* Allowed flags in p->cost.plan.wsFlag */
  3172         -  int nPriorSat;              /* ORDER BY terms satisfied by outer loops */
  3173         -  int nOrderBy;               /* Number of ORDER BY terms */
  3174         -  char bSortInit;             /* Initializer for bSort in inner loop */
  3175         -  char bDistInit;             /* Initializer for bDist in inner loop */
  3176         -
  3177         -
  3178         -  /* Initialize the cost to a worst-case value */
  3179         -  memset(&p->cost, 0, sizeof(p->cost));
  3180         -  p->cost.rCost = SQLITE_BIG_DBL;
  3181         -
  3182         -  /* If the pSrc table is the right table of a LEFT JOIN then we may not
  3183         -  ** use an index to satisfy IS NULL constraints on that table.  This is
  3184         -  ** because columns might end up being NULL if the table does not match -
  3185         -  ** a circumstance which the index cannot help us discover.  Ticket #2177.
  3186         -  */
  3187         -  if( pSrc->jointype & JT_LEFT ){
  3188         -    idxEqTermMask = WO_EQ|WO_IN;
  3189         -  }else{
  3190         -    idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL;
  3191         -  }
  3192         -
  3193         -  if( pSrc->pIndex ){
  3194         -    /* An INDEXED BY clause specifies a particular index to use */
  3195         -    pIdx = pProbe = pSrc->pIndex;
  3196         -    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
  3197         -    eqTermMask = idxEqTermMask;
  3198         -  }else{
  3199         -    /* There is no INDEXED BY clause.  Create a fake Index object in local
  3200         -    ** variable sPk to represent the rowid primary key index.  Make this
  3201         -    ** fake index the first in a chain of Index objects with all of the real
  3202         -    ** indices to follow */
  3203         -    Index *pFirst;                  /* First of real indices on the table */
  3204         -    memset(&sPk, 0, sizeof(Index));
  3205         -    sPk.nColumn = 1;
  3206         -    sPk.aiColumn = &aiColumnPk;
  3207         -    sPk.aiRowEst = aiRowEstPk;
  3208         -    sPk.onError = OE_Replace;
  3209         -    sPk.pTable = pSrc->pTab;
  3210         -    aiRowEstPk[0] = pSrc->pTab->nRowEst;
  3211         -    aiRowEstPk[1] = 1;
  3212         -    pFirst = pSrc->pTab->pIndex;
  3213         -    if( pSrc->notIndexed==0 ){
  3214         -      /* The real indices of the table are only considered if the
  3215         -      ** NOT INDEXED qualifier is omitted from the FROM clause */
  3216         -      sPk.pNext = pFirst;
  3217         -    }
  3218         -    pProbe = &sPk;
  3219         -    wsFlagMask = ~(
  3220         -        WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE
  3221         -    );
  3222         -    eqTermMask = WO_EQ|WO_IN;
  3223         -    pIdx = 0;
  3224         -  }
  3225         -
  3226         -  nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
  3227         -  if( p->i ){
  3228         -    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
  3229         -    bSortInit = nPriorSat<nOrderBy;
  3230         -    bDistInit = 0;
  3231         -  }else{
  3232         -    nPriorSat = 0;
  3233         -    bSortInit = nOrderBy>0;
  3234         -    bDistInit = p->pDistinct!=0;
  3235         -  }
  3236         -
  3237         -  /* Loop over all indices looking for the best one to use
  3238         -  */
  3239         -  for(; pProbe; pIdx=pProbe=pProbe->pNext){
  3240         -    const tRowcnt * const aiRowEst = pProbe->aiRowEst;
  3241         -    WhereCost pc;               /* Cost of using pProbe */
  3242         -    double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
  3243         -
  3244         -    /* The following variables are populated based on the properties of
  3245         -    ** index being evaluated. They are then used to determine the expected
  3246         -    ** cost and number of rows returned.
  3247         -    **
  3248         -    **  pc.plan.nEq: 
  3249         -    **    Number of equality terms that can be implemented using the index.
  3250         -    **    In other words, the number of initial fields in the index that
  3251         -    **    are used in == or IN or NOT NULL constraints of the WHERE clause.
  3252         -    **
  3253         -    **  nInMul:  
  3254         -    **    The "in-multiplier". This is an estimate of how many seek operations 
  3255         -    **    SQLite must perform on the index in question. For example, if the 
  3256         -    **    WHERE clause is:
  3257         -    **
  3258         -    **      WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
  3259         -    **
  3260         -    **    SQLite must perform 9 lookups on an index on (a, b), so nInMul is 
  3261         -    **    set to 9. Given the same schema and either of the following WHERE 
  3262         -    **    clauses:
  3263         -    **
  3264         -    **      WHERE a =  1
  3265         -    **      WHERE a >= 2
  3266         -    **
  3267         -    **    nInMul is set to 1.
  3268         -    **
  3269         -    **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
  3270         -    **    the sub-select is assumed to return 25 rows for the purposes of 
  3271         -    **    determining nInMul.
  3272         -    **
  3273         -    **  bInEst:  
  3274         -    **    Set to true if there was at least one "x IN (SELECT ...)" term used 
  3275         -    **    in determining the value of nInMul.  Note that the RHS of the
  3276         -    **    IN operator must be a SELECT, not a value list, for this variable
  3277         -    **    to be true.
  3278         -    **
  3279         -    **  rangeDiv:
  3280         -    **    An estimate of a divisor by which to reduce the search space due
  3281         -    **    to inequality constraints.  In the absence of sqlite_stat3 ANALYZE
  3282         -    **    data, a single inequality reduces the search space to 1/4rd its
  3283         -    **    original size (rangeDiv==4).  Two inequalities reduce the search
  3284         -    **    space to 1/16th of its original size (rangeDiv==16).
  3285         -    **
  3286         -    **  bSort:   
  3287         -    **    Boolean. True if there is an ORDER BY clause that will require an 
  3288         -    **    external sort (i.e. scanning the index being evaluated will not 
  3289         -    **    correctly order records).
  3290         -    **
  3291         -    **  bDist:
  3292         -    **    Boolean. True if there is a DISTINCT clause that will require an 
  3293         -    **    external btree.
  3294         -    **
  3295         -    **  bLookup: 
  3296         -    **    Boolean. True if a table lookup is required for each index entry
  3297         -    **    visited.  In other words, true if this is not a covering index.
  3298         -    **    This is always false for the rowid primary key index of a table.
  3299         -    **    For other indexes, it is true unless all the columns of the table
  3300         -    **    used by the SELECT statement are present in the index (such an
  3301         -    **    index is sometimes described as a covering index).
  3302         -    **    For example, given the index on (a, b), the second of the following 
  3303         -    **    two queries requires table b-tree lookups in order to find the value
  3304         -    **    of column c, but the first does not because columns a and b are
  3305         -    **    both available in the index.
  3306         -    **
  3307         -    **             SELECT a, b    FROM tbl WHERE a = 1;
  3308         -    **             SELECT a, b, c FROM tbl WHERE a = 1;
  3309         -    */
  3310         -    int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
  3311         -    int nInMul = 1;               /* Number of distinct equalities to lookup */
  3312         -    double rangeDiv = (double)1;  /* Estimated reduction in search space */
  3313         -    int nBound = 0;               /* Number of range constraints seen */
  3314         -    char bSort = bSortInit;       /* True if external sort required */
  3315         -    char bDist = bDistInit;       /* True if index cannot help with DISTINCT */
  3316         -    char bLookup = 0;             /* True if not a covering index */
  3317         -    WhereTerm *pTerm;             /* A single term of the WHERE clause */
  3318         -#ifdef SQLITE_ENABLE_STAT3
  3319         -    WhereTerm *pFirstTerm = 0;    /* First term matching the index */
  3320         -#endif
  3321         -
  3322         -    WHERETRACE((
  3323         -      "   %s(%s):\n",
  3324         -      pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk")
  3325         -    ));
  3326         -    memset(&pc, 0, sizeof(pc));
  3327         -    pc.plan.nOBSat = nPriorSat;
  3328         -
  3329         -    /* Determine the values of pc.plan.nEq and nInMul */
  3330         -    for(pc.plan.nEq=0; pc.plan.nEq<pProbe->nColumn; pc.plan.nEq++){
  3331         -      int j = pProbe->aiColumn[pc.plan.nEq];
  3332         -      pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
  3333         -      if( pTerm==0 ) break;
  3334         -      pc.plan.wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
  3335         -      testcase( pTerm->pWC!=pWC );
  3336         -      if( pTerm->eOperator & WO_IN ){
  3337         -        Expr *pExpr = pTerm->pExpr;
  3338         -        pc.plan.wsFlags |= WHERE_COLUMN_IN;
  3339         -        if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3340         -          /* "x IN (SELECT ...)":  Assume the SELECT returns 25 rows */
  3341         -          nInMul *= 25;
  3342         -          bInEst = 1;
  3343         -        }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
  3344         -          /* "x IN (value, value, ...)" */
  3345         -          nInMul *= pExpr->x.pList->nExpr;
  3346         -        }
  3347         -      }else if( pTerm->eOperator & WO_ISNULL ){
  3348         -        pc.plan.wsFlags |= WHERE_COLUMN_NULL;
  3349         -      }
  3350         -#ifdef SQLITE_ENABLE_STAT3
  3351         -      if( pc.plan.nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
  3352         -#endif
  3353         -      pc.used |= pTerm->prereqRight;
  3354         -    }
  3355         - 
  3356         -    /* If the index being considered is UNIQUE, and there is an equality 
  3357         -    ** constraint for all columns in the index, then this search will find
  3358         -    ** at most a single row. In this case set the WHERE_UNIQUE flag to 
  3359         -    ** indicate this to the caller.
  3360         -    **
  3361         -    ** Otherwise, if the search may find more than one row, test to see if
  3362         -    ** there is a range constraint on indexed column (pc.plan.nEq+1) that
  3363         -    ** can be optimized using the index. 
  3364         -    */
  3365         -    if( pc.plan.nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
  3366         -      testcase( pc.plan.wsFlags & WHERE_COLUMN_IN );
  3367         -      testcase( pc.plan.wsFlags & WHERE_COLUMN_NULL );
  3368         -      if( (pc.plan.wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
  3369         -        pc.plan.wsFlags |= WHERE_UNIQUE;
  3370         -        if( p->i==0 || (p->aLevel[p->i-1].plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
  3371         -          pc.plan.wsFlags |= WHERE_ALL_UNIQUE;
  3372         -        }
  3373         -      }
  3374         -    }else if( pProbe->bUnordered==0 ){
  3375         -      int j;
  3376         -      j = (pc.plan.nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[pc.plan.nEq]);
  3377         -      if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
  3378         -        WhereTerm *pTop, *pBtm;
  3379         -        pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
  3380         -        pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
  3381         -        whereRangeScanEst(pParse, pProbe, pc.plan.nEq, pBtm, pTop, &rangeDiv);
  3382         -        if( pTop ){
  3383         -          nBound = 1;
  3384         -          pc.plan.wsFlags |= WHERE_TOP_LIMIT;
  3385         -          pc.used |= pTop->prereqRight;
  3386         -          testcase( pTop->pWC!=pWC );
  3387         -        }
  3388         -        if( pBtm ){
  3389         -          nBound++;
  3390         -          pc.plan.wsFlags |= WHERE_BTM_LIMIT;
  3391         -          pc.used |= pBtm->prereqRight;
  3392         -          testcase( pBtm->pWC!=pWC );
  3393         -        }
  3394         -        pc.plan.wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
  3395         -      }
  3396         -    }
  3397         -
  3398         -    /* If there is an ORDER BY clause and the index being considered will
  3399         -    ** naturally scan rows in the required order, set the appropriate flags
  3400         -    ** in pc.plan.wsFlags. Otherwise, if there is an ORDER BY clause but
  3401         -    ** the index will scan rows in a different order, set the bSort
  3402         -    ** variable.  */
  3403         -    if( bSort && (pSrc->jointype & JT_LEFT)==0 ){
  3404         -      int bRev = 2;
  3405         -      int bObUnique = 0;
  3406         -      WHERETRACE(("      --> before isSortIndex: nPriorSat=%d\n",nPriorSat));
  3407         -      pc.plan.nOBSat = isSortingIndex(p, pProbe, iCur, &bRev, &bObUnique);
  3408         -      WHERETRACE(("      --> after  isSortIndex: bRev=%d bObU=%d nOBSat=%d\n",
  3409         -                  bRev, bObUnique, pc.plan.nOBSat));
  3410         -      if( nPriorSat<pc.plan.nOBSat || (pc.plan.wsFlags & WHERE_ALL_UNIQUE)!=0 ){
  3411         -        pc.plan.wsFlags |= WHERE_ORDERED;
  3412         -        if( bObUnique ) pc.plan.wsFlags |= WHERE_OB_UNIQUE;
  3413         -      }
  3414         -      if( nOrderBy==pc.plan.nOBSat ){
  3415         -        bSort = 0;
  3416         -        pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE;
  3417         -      }
  3418         -      if( bRev & 1 ) pc.plan.wsFlags |= WHERE_REVERSE;
  3419         -    }
  3420         -
  3421         -    /* If there is a DISTINCT qualifier and this index will scan rows in
  3422         -    ** order of the DISTINCT expressions, clear bDist and set the appropriate
  3423         -    ** flags in pc.plan.wsFlags. */
  3424         -    if( bDist
  3425         -     && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, pc.plan.nEq)
  3426         -     && (pc.plan.wsFlags & WHERE_COLUMN_IN)==0
  3427         -    ){
  3428         -      bDist = 0;
  3429         -      pc.plan.wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
  3430         -    }
  3431         -
  3432         -    /* If currently calculating the cost of using an index (not the IPK
  3433         -    ** index), determine if all required column data may be obtained without 
  3434         -    ** using the main table (i.e. if the index is a covering
  3435         -    ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
  3436         -    ** pc.plan.wsFlags. Otherwise, set the bLookup variable to true.  */
  3437         -    if( pIdx ){
  3438         -      Bitmask m = pSrc->colUsed;
  3439         -      int j;
  3440         -      for(j=0; j<pIdx->nColumn; j++){
  3441         -        int x = pIdx->aiColumn[j];
  3442         -        if( x<BMS-1 ){
  3443         -          m &= ~(((Bitmask)1)<<x);
  3444         -        }
  3445         -      }
  3446         -      if( m==0 ){
  3447         -        pc.plan.wsFlags |= WHERE_IDX_ONLY;
  3448         -      }else{
  3449         -        bLookup = 1;
  3450         -      }
  3451         -    }
  3452         -
  3453         -    /*
  3454         -    ** Estimate the number of rows of output.  For an "x IN (SELECT...)"
  3455         -    ** constraint, do not let the estimate exceed half the rows in the table.
  3456         -    */
  3457         -    pc.plan.nRow = (double)(aiRowEst[pc.plan.nEq] * nInMul);
  3458         -    if( bInEst && pc.plan.nRow*2>aiRowEst[0] ){
  3459         -      pc.plan.nRow = aiRowEst[0]/2;
  3460         -      nInMul = (int)(pc.plan.nRow / aiRowEst[pc.plan.nEq]);
  3461         -    }
  3462         -
  3463         -#ifdef SQLITE_ENABLE_STAT3
  3464         -    /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
  3465         -    ** and we do not think that values of x are unique and if histogram
  3466         -    ** data is available for column x, then it might be possible
  3467         -    ** to get a better estimate on the number of rows based on
  3468         -    ** VALUE and how common that value is according to the histogram.
  3469         -    */
  3470         -    if( pc.plan.nRow>(double)1 && pc.plan.nEq==1
  3471         -     && pFirstTerm!=0 && aiRowEst[1]>1 ){
  3472         -      assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
  3473         -      if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
  3474         -        testcase( pFirstTerm->eOperator & WO_EQ );
  3475         -        testcase( pFirstTerm->eOperator & WO_EQUIV );
  3476         -        testcase( pFirstTerm->eOperator & WO_ISNULL );
  3477         -        whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight,
  3478         -                          &pc.plan.nRow);
  3479         -      }else if( bInEst==0 ){
  3480         -        assert( pFirstTerm->eOperator & WO_IN );
  3481         -        whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList,
  3482         -                       &pc.plan.nRow);
  3483         -      }
  3484         -    }
  3485         -#endif /* SQLITE_ENABLE_STAT3 */
  3486         -
  3487         -    /* Adjust the number of output rows and downward to reflect rows
  3488         -    ** that are excluded by range constraints.
  3489         -    */
  3490         -    pc.plan.nRow = pc.plan.nRow/rangeDiv;
  3491         -    if( pc.plan.nRow<1 ) pc.plan.nRow = 1;
  3492         -
  3493         -    /* Experiments run on real SQLite databases show that the time needed
  3494         -    ** to do a binary search to locate a row in a table or index is roughly
  3495         -    ** log10(N) times the time to move from one row to the next row within
  3496         -    ** a table or index.  The actual times can vary, with the size of
  3497         -    ** records being an important factor.  Both moves and searches are
  3498         -    ** slower with larger records, presumably because fewer records fit
  3499         -    ** on one page and hence more pages have to be fetched.
  3500         -    **
  3501         -    ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
  3502         -    ** not give us data on the relative sizes of table and index records.
  3503         -    ** So this computation assumes table records are about twice as big
  3504         -    ** as index records
  3505         -    */
  3506         -    if( (pc.plan.wsFlags&~(WHERE_REVERSE|WHERE_ORDERED|WHERE_OB_UNIQUE))
  3507         -                                                              ==WHERE_IDX_ONLY
  3508         -     && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
  3509         -     && sqlite3GlobalConfig.bUseCis
  3510         -     && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
  3511         -    ){
  3512         -      /* This index is not useful for indexing, but it is a covering index.
  3513         -      ** A full-scan of the index might be a little faster than a full-scan
  3514         -      ** of the table, so give this case a cost slightly less than a table
  3515         -      ** scan. */
  3516         -      pc.rCost = aiRowEst[0]*3 + pProbe->nColumn;
  3517         -      pc.plan.wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
  3518         -    }else if( (pc.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
  3519         -      /* The cost of a full table scan is a number of move operations equal
  3520         -      ** to the number of rows in the table.
  3521         -      **
  3522         -      ** We add an additional 4x penalty to full table scans.  This causes
  3523         -      ** the cost function to err on the side of choosing an index over
  3524         -      ** choosing a full scan.  This 4x full-scan penalty is an arguable
  3525         -      ** decision and one which we expect to revisit in the future.  But
  3526         -      ** it seems to be working well enough at the moment.
  3527         -      */
  3528         -      pc.rCost = aiRowEst[0]*4;
  3529         -      pc.plan.wsFlags &= ~WHERE_IDX_ONLY;
  3530         -      if( pIdx ){
  3531         -        pc.plan.wsFlags &= ~WHERE_ORDERED;
  3532         -        pc.plan.nOBSat = nPriorSat;
  3533         -      }
  3534         -    }else{
  3535         -      log10N = estLog(aiRowEst[0]);
  3536         -      pc.rCost = pc.plan.nRow;
  3537         -      if( pIdx ){
  3538         -        if( bLookup ){
  3539         -          /* For an index lookup followed by a table lookup:
  3540         -          **    nInMul index searches to find the start of each index range
  3541         -          **  + nRow steps through the index
  3542         -          **  + nRow table searches to lookup the table entry using the rowid
  3543         -          */
  3544         -          pc.rCost += (nInMul + pc.plan.nRow)*log10N;
  3545         -        }else{
  3546         -          /* For a covering index:
  3547         -          **     nInMul index searches to find the initial entry 
  3548         -          **   + nRow steps through the index
  3549         -          */
  3550         -          pc.rCost += nInMul*log10N;
  3551         -        }
  3552         -      }else{
  3553         -        /* For a rowid primary key lookup:
  3554         -        **    nInMult table searches to find the initial entry for each range
  3555         -        **  + nRow steps through the table
  3556         -        */
  3557         -        pc.rCost += nInMul*log10N;
  3558         -      }
  3559         -    }
  3560         -
  3561         -    /* Add in the estimated cost of sorting the result.  Actual experimental
  3562         -    ** measurements of sorting performance in SQLite show that sorting time
  3563         -    ** adds C*N*log10(N) to the cost, where N is the number of rows to be 
  3564         -    ** sorted and C is a factor between 1.95 and 4.3.  We will split the
  3565         -    ** difference and select C of 3.0.
  3566         -    */
  3567         -    if( bSort ){
  3568         -      double m = estLog(pc.plan.nRow*(nOrderBy - pc.plan.nOBSat)/nOrderBy);
  3569         -      m *= (double)(pc.plan.nOBSat ? 2 : 3);
  3570         -      pc.rCost += pc.plan.nRow*m;
  3571         -    }
  3572         -    if( bDist ){
  3573         -      pc.rCost += pc.plan.nRow*estLog(pc.plan.nRow)*3;
  3574         -    }
  3575         -
  3576         -    /**** Cost of using this index has now been computed ****/
  3577         -
  3578         -    /* If there are additional constraints on this table that cannot
  3579         -    ** be used with the current index, but which might lower the number
  3580         -    ** of output rows, adjust the nRow value accordingly.  This only 
  3581         -    ** matters if the current index is the least costly, so do not bother
  3582         -    ** with this step if we already know this index will not be chosen.
  3583         -    ** Also, never reduce the output row count below 2 using this step.
  3584         -    **
  3585         -    ** It is critical that the notValid mask be used here instead of
  3586         -    ** the notReady mask.  When computing an "optimal" index, the notReady
  3587         -    ** mask will only have one bit set - the bit for the current table.
  3588         -    ** The notValid mask, on the other hand, always has all bits set for
  3589         -    ** tables that are not in outer loops.  If notReady is used here instead
  3590         -    ** of notValid, then a optimal index that depends on inner joins loops
  3591         -    ** might be selected even when there exists an optimal index that has
  3592         -    ** no such dependency.
  3593         -    */
  3594         -    if( pc.plan.nRow>2 && pc.rCost<=p->cost.rCost ){
  3595         -      int k;                       /* Loop counter */
  3596         -      int nSkipEq = pc.plan.nEq;   /* Number of == constraints to skip */
  3597         -      int nSkipRange = nBound;     /* Number of < constraints to skip */
  3598         -      Bitmask thisTab;             /* Bitmap for pSrc */
  3599         -
  3600         -      thisTab = getMask(pWC->pMaskSet, iCur);
  3601         -      for(pTerm=pWC->a, k=pWC->nTerm; pc.plan.nRow>2 && k; k--, pTerm++){
  3602         -        if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
  3603         -        if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
  3604         -        if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
  3605         -          if( nSkipEq ){
  3606         -            /* Ignore the first pc.plan.nEq equality matches since the index
  3607         -            ** has already accounted for these */
  3608         -            nSkipEq--;
  3609         -          }else{
  3610         -            /* Assume each additional equality match reduces the result
  3611         -            ** set size by a factor of 10 */
  3612         -            pc.plan.nRow /= 10;
  3613         -          }
  3614         -        }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){
  3615         -          if( nSkipRange ){
  3616         -            /* Ignore the first nSkipRange range constraints since the index
  3617         -            ** has already accounted for these */
  3618         -            nSkipRange--;
  3619         -          }else{
  3620         -            /* Assume each additional range constraint reduces the result
  3621         -            ** set size by a factor of 3.  Indexed range constraints reduce
  3622         -            ** the search space by a larger factor: 4.  We make indexed range
  3623         -            ** more selective intentionally because of the subjective 
  3624         -            ** observation that indexed range constraints really are more
  3625         -            ** selective in practice, on average. */
  3626         -            pc.plan.nRow /= 3;
  3627         -          }
  3628         -        }else if( (pTerm->eOperator & WO_NOOP)==0 ){
  3629         -          /* Any other expression lowers the output row count by half */
  3630         -          pc.plan.nRow /= 2;
  3631         -        }
  3632         -      }
  3633         -      if( pc.plan.nRow<2 ) pc.plan.nRow = 2;
  3634         -    }
  3635         -
  3636         -
  3637         -    WHERETRACE((
  3638         -      "      nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
  3639         -      "      notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
  3640         -      "      used=0x%llx nOBSat=%d\n",
  3641         -      pc.plan.nEq, nInMul, (int)rangeDiv, bSort, bLookup, pc.plan.wsFlags,
  3642         -      p->notReady, log10N, pc.plan.nRow, pc.rCost, pc.used,
  3643         -      pc.plan.nOBSat
  3644         -    ));
  3645         -
  3646         -    /* If this index is the best we have seen so far, then record this
  3647         -    ** index and its cost in the p->cost structure.
  3648         -    */
  3649         -    if( (!pIdx || pc.plan.wsFlags) && compareCost(&pc, &p->cost) ){
  3650         -      p->cost = pc;
  3651         -      p->cost.plan.wsFlags &= wsFlagMask;
  3652         -      p->cost.plan.u.pIdx = pIdx;
  3653         -    }
  3654         -
  3655         -    /* If there was an INDEXED BY clause, then only that one index is
  3656         -    ** considered. */
  3657         -    if( pSrc->pIndex ) break;
  3658         -
  3659         -    /* Reset masks for the next index in the loop */
  3660         -    wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE);
  3661         -    eqTermMask = idxEqTermMask;
  3662         -  }
  3663         -
  3664         -  /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
  3665         -  ** is set, then reverse the order that the index will be scanned
  3666         -  ** in. This is used for application testing, to help find cases
  3667         -  ** where application behavior depends on the (undefined) order that
  3668         -  ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
  3669         -  if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
  3670         -    p->cost.plan.wsFlags |= WHERE_REVERSE;
  3671         -  }
  3672         -
  3673         -  assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERED)==0 );
  3674         -  assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
  3675         -  assert( pSrc->pIndex==0 
  3676         -       || p->cost.plan.u.pIdx==0 
  3677         -       || p->cost.plan.u.pIdx==pSrc->pIndex 
  3678         -  );
  3679         -
  3680         -  WHERETRACE(("   best index is %s cost=%.1f\n",
  3681         -         p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk",
  3682         -         p->cost.rCost));
  3683         -  
  3684         -  bestOrClauseIndex(p);
  3685         -  bestAutomaticIndex(p);
  3686         -  p->cost.plan.wsFlags |= eqTermMask;
  3687         -}
  3688         -
  3689         -/*
  3690         -** Find the query plan for accessing table pSrc->pTab. Write the
  3691         -** best query plan and its cost into the WhereCost object supplied 
  3692         -** as the last parameter. This function may calculate the cost of
  3693         -** both real and virtual table scans.
  3694         -**
  3695         -** This function does not take ORDER BY or DISTINCT into account.  Nor
  3696         -** does it remember the virtual table query plan.  All it does is compute
  3697         -** the cost while determining if an OR optimization is applicable.  The
  3698         -** details will be reconsidered later if the optimization is found to be
  3699         -** applicable.
  3700         -*/
  3701         -static void bestIndex(WhereBestIdx *p){
  3702         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  3703         -  if( IsVirtual(p->pSrc->pTab) ){
  3704         -    sqlite3_index_info *pIdxInfo = 0;
  3705         -    p->ppIdxInfo = &pIdxInfo;
  3706         -    bestVirtualIndex(p);
  3707         -    assert( pIdxInfo!=0 || p->pParse->db->mallocFailed );
  3708         -    if( pIdxInfo && pIdxInfo->needToFreeIdxStr ){
  3709         -      sqlite3_free(pIdxInfo->idxStr);
  3710         -    }
  3711         -    sqlite3DbFree(p->pParse->db, pIdxInfo);
  3712         -  }else
  3713         -#endif
  3714         -  {
  3715         -    bestBtreeIndex(p);
  3716         -  }
  3717         -}
  3718         -
  3719   2727   /*
  3720   2728   ** Disable a term in the WHERE clause.  Except, do not disable the term
  3721   2729   ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  3722   2730   ** or USING clause of that join.
  3723   2731   **
  3724   2732   ** Consider the term t2.z='ok' in the following queries:
  3725   2733   **
................................................................................
  3809   2817   ** this routine sets up a loop that will iterate over all values of X.
  3810   2818   */
  3811   2819   static int codeEqualityTerm(
  3812   2820     Parse *pParse,      /* The parsing context */
  3813   2821     WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
  3814   2822     WhereLevel *pLevel, /* The level of the FROM clause we are working on */
  3815   2823     int iEq,            /* Index of the equality term within this level */
         2824  +  int bRev,           /* True for reverse-order IN operations */
  3816   2825     int iTarget         /* Attempt to leave results in this register */
  3817   2826   ){
  3818   2827     Expr *pX = pTerm->pExpr;
  3819   2828     Vdbe *v = pParse->pVdbe;
  3820   2829     int iReg;                  /* Register holding results */
  3821   2830   
  3822   2831     assert( iTarget>0 );
................................................................................
  3826   2835       iReg = iTarget;
  3827   2836       sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
  3828   2837   #ifndef SQLITE_OMIT_SUBQUERY
  3829   2838     }else{
  3830   2839       int eType;
  3831   2840       int iTab;
  3832   2841       struct InLoop *pIn;
  3833         -    u8 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
         2842  +    WhereLoop *pLoop = pLevel->pWLoop;
  3834   2843   
  3835         -    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 
  3836         -      && pLevel->plan.u.pIdx->aSortOrder[iEq]
         2844  +    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
         2845  +      && pLoop->u.btree.pIndex!=0
         2846  +      && pLoop->u.btree.pIndex->aSortOrder[iEq]
  3837   2847       ){
  3838   2848         testcase( iEq==0 );
  3839         -      testcase( iEq==pLevel->plan.u.pIdx->nColumn-1 );
  3840         -      testcase( iEq>0 && iEq+1<pLevel->plan.u.pIdx->nColumn );
  3841   2849         testcase( bRev );
  3842   2850         bRev = !bRev;
  3843   2851       }
  3844   2852       assert( pX->op==TK_IN );
  3845   2853       iReg = iTarget;
  3846   2854       eType = sqlite3FindInIndex(pParse, pX, 0);
  3847   2855       if( eType==IN_INDEX_INDEX_DESC ){
  3848   2856         testcase( bRev );
  3849   2857         bRev = !bRev;
  3850   2858       }
  3851   2859       iTab = pX->iTable;
  3852   2860       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
  3853         -    assert( pLevel->plan.wsFlags & WHERE_IN_ABLE );
         2861  +    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
         2862  +    pLoop->wsFlags |= WHERE_IN_ABLE;
  3854   2863       if( pLevel->u.in.nIn==0 ){
  3855   2864         pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  3856   2865       }
  3857   2866       pLevel->u.in.nIn++;
  3858   2867       pLevel->u.in.aInLoop =
  3859   2868          sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
  3860   2869                                 sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
................................................................................
  3916   2925   ** no conversion should be attempted before using a t2.b value as part of
  3917   2926   ** a key to search the index. Hence the first byte in the returned affinity
  3918   2927   ** string in this example would be set to SQLITE_AFF_NONE.
  3919   2928   */
  3920   2929   static int codeAllEqualityTerms(
  3921   2930     Parse *pParse,        /* Parsing context */
  3922   2931     WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
  3923         -  WhereClause *pWC,     /* The WHERE clause */
  3924         -  Bitmask notReady,     /* Which parts of FROM have not yet been coded */
         2932  +  int bRev,             /* Reverse the order of IN operators */
  3925   2933     int nExtraReg,        /* Number of extra registers to allocate */
  3926   2934     char **pzAff          /* OUT: Set to point to affinity string */
  3927   2935   ){
  3928         -  int nEq = pLevel->plan.nEq;   /* The number of == or IN constraints to code */
         2936  +  int nEq;                      /* The number of == or IN constraints to code */
  3929   2937     Vdbe *v = pParse->pVdbe;      /* The vm under construction */
  3930   2938     Index *pIdx;                  /* The index being used for this loop */
  3931         -  int iCur = pLevel->iTabCur;   /* The cursor of the table */
  3932   2939     WhereTerm *pTerm;             /* A single constraint term */
         2940  +  WhereLoop *pLoop;             /* The WhereLoop object */
  3933   2941     int j;                        /* Loop counter */
  3934   2942     int regBase;                  /* Base register */
  3935   2943     int nReg;                     /* Number of registers to allocate */
  3936   2944     char *zAff;                   /* Affinity string to return */
  3937   2945   
  3938   2946     /* This module is only called on query plans that use an index. */
  3939         -  assert( pLevel->plan.wsFlags & WHERE_INDEXED );
  3940         -  pIdx = pLevel->plan.u.pIdx;
         2947  +  pLoop = pLevel->pWLoop;
         2948  +  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
         2949  +  nEq = pLoop->u.btree.nEq;
         2950  +  pIdx = pLoop->u.btree.pIndex;
         2951  +  assert( pIdx!=0 );
  3941   2952   
  3942   2953     /* Figure out how many memory cells we will need then allocate them.
  3943   2954     */
  3944   2955     regBase = pParse->nMem + 1;
  3945         -  nReg = pLevel->plan.nEq + nExtraReg;
         2956  +  nReg = pLoop->u.btree.nEq + nExtraReg;
  3946   2957     pParse->nMem += nReg;
  3947   2958   
  3948   2959     zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
  3949   2960     if( !zAff ){
  3950   2961       pParse->db->mallocFailed = 1;
  3951   2962     }
  3952   2963   
  3953   2964     /* Evaluate the equality constraints
  3954   2965     */
  3955   2966     assert( pIdx->nColumn>=nEq );
  3956   2967     for(j=0; j<nEq; j++){
  3957   2968       int r1;
  3958         -    int k = pIdx->aiColumn[j];
  3959         -    pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx);
  3960         -    if( pTerm==0 ) break;
         2969  +    pTerm = pLoop->aLTerm[j];
         2970  +    assert( pTerm!=0 );
  3961   2971       /* The following true for indices with redundant columns. 
  3962   2972       ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  3963   2973       testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
  3964   2974       testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3965         -    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, regBase+j);
         2975  +    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
  3966   2976       if( r1!=regBase+j ){
  3967   2977         if( nReg==1 ){
  3968   2978           sqlite3ReleaseTempReg(pParse, regBase);
  3969   2979           regBase = r1;
  3970   2980         }else{
  3971   2981           sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
  3972   2982         }
................................................................................
  4026   3036   **
  4027   3037   **   "a=? AND b>?"
  4028   3038   **
  4029   3039   ** The returned pointer points to memory obtained from sqlite3DbMalloc().
  4030   3040   ** It is the responsibility of the caller to free the buffer when it is
  4031   3041   ** no longer required.
  4032   3042   */
  4033         -static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){
  4034         -  WherePlan *pPlan = &pLevel->plan;
  4035         -  Index *pIndex = pPlan->u.pIdx;
  4036         -  int nEq = pPlan->nEq;
         3043  +static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){
         3044  +  Index *pIndex = pLoop->u.btree.pIndex;
         3045  +  int nEq = pLoop->u.btree.nEq;
  4037   3046     int i, j;
  4038   3047     Column *aCol = pTab->aCol;
  4039   3048     int *aiColumn = pIndex->aiColumn;
  4040   3049     StrAccum txt;
  4041   3050   
  4042         -  if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
         3051  +  if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
  4043   3052       return 0;
  4044   3053     }
  4045   3054     sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
  4046   3055     txt.db = db;
  4047   3056     sqlite3StrAccumAppend(&txt, " (", 2);
  4048   3057     for(i=0; i<nEq; i++){
  4049   3058       explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
  4050   3059     }
  4051   3060   
  4052   3061     j = i;
  4053         -  if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
         3062  +  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
  4054   3063       char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
  4055   3064       explainAppendTerm(&txt, i++, z, ">");
  4056   3065     }
  4057         -  if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
         3066  +  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
  4058   3067       char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
  4059   3068       explainAppendTerm(&txt, i, z, "<");
  4060   3069     }
  4061   3070     sqlite3StrAccumAppend(&txt, ")", 1);
  4062   3071     return sqlite3StrAccumFinish(&txt);
  4063   3072   }
  4064   3073   
................................................................................
  4073   3082     SrcList *pTabList,              /* Table list this loop refers to */
  4074   3083     WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
  4075   3084     int iLevel,                     /* Value for "level" column of output */
  4076   3085     int iFrom,                      /* Value for "from" column of output */
  4077   3086     u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
  4078   3087   ){
  4079   3088     if( pParse->explain==2 ){
  4080         -    u32 flags = pLevel->plan.wsFlags;
  4081   3089       struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
  4082   3090       Vdbe *v = pParse->pVdbe;      /* VM being constructed */
  4083   3091       sqlite3 *db = pParse->db;     /* Database handle */
  4084   3092       char *zMsg;                   /* Text to add to EQP output */
  4085         -    sqlite3_int64 nRow;           /* Expected number of rows visited by scan */
  4086   3093       int iId = pParse->iSelectId;  /* Select id (left-most output column) */
  4087   3094       int isSearch;                 /* True for a SEARCH. False for SCAN. */
         3095  +    WhereLoop *pLoop;             /* The controlling WhereLoop object */
         3096  +    u32 flags;                    /* Flags that describe this loop */
  4088   3097   
         3098  +    pLoop = pLevel->pWLoop;
         3099  +    flags = pLoop->wsFlags;
  4089   3100       if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
  4090   3101   
  4091         -    isSearch = (pLevel->plan.nEq>0)
  4092         -             || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
  4093         -             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
         3102  +    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
         3103  +            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
         3104  +            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
  4094   3105   
  4095   3106       zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
  4096   3107       if( pItem->pSelect ){
  4097   3108         zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
  4098   3109       }else{
  4099   3110         zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
  4100   3111       }
  4101   3112   
  4102   3113       if( pItem->zAlias ){
  4103   3114         zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
  4104   3115       }
  4105         -    if( (flags & WHERE_INDEXED)!=0 ){
  4106         -      char *zWhere = explainIndexRange(db, pLevel, pItem->pTab);
         3116  +    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
         3117  +     && ALWAYS(pLoop->u.btree.pIndex!=0)
         3118  +    ){
         3119  +      char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
  4107   3120         zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, 
  4108   3121             ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""),
  4109   3122             ((flags & WHERE_IDX_ONLY)?"COVERING ":""),
  4110   3123             ((flags & WHERE_TEMP_INDEX)?"":" "),
  4111         -          ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName),
         3124  +          ((flags & WHERE_TEMP_INDEX)?"": pLoop->u.btree.pIndex->zName),
  4112   3125             zWhere
  4113   3126         );
  4114   3127         sqlite3DbFree(db, zWhere);
  4115         -    }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
         3128  +    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
  4116   3129         zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
  4117   3130   
  4118         -      if( flags&WHERE_ROWID_EQ ){
         3131  +      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
  4119   3132           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
  4120   3133         }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
  4121   3134           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
  4122   3135         }else if( flags&WHERE_BTM_LIMIT ){
  4123   3136           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
  4124         -      }else if( flags&WHERE_TOP_LIMIT ){
         3137  +      }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){
  4125   3138           zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
  4126   3139         }
  4127   3140       }
  4128   3141   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4129   3142       else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
  4130         -      sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
  4131   3143         zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
  4132         -                  pVtabIdx->idxNum, pVtabIdx->idxStr);
         3144  +                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
  4133   3145       }
  4134   3146   #endif
  4135         -    if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){
  4136         -      testcase( wctrlFlags & WHERE_ORDERBY_MIN );
  4137         -      nRow = 1;
  4138         -    }else{
  4139         -      nRow = (sqlite3_int64)pLevel->plan.nRow;
  4140         -    }
  4141         -    zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow);
         3147  +    zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg);
  4142   3148       sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
  4143   3149     }
  4144   3150   }
  4145   3151   #else
  4146   3152   # define explainOneScan(u,v,w,x,y,z)
  4147   3153   #endif /* SQLITE_OMIT_EXPLAIN */
  4148   3154   
................................................................................
  4150   3156   /*
  4151   3157   ** Generate code for the start of the iLevel-th loop in the WHERE clause
  4152   3158   ** implementation described by pWInfo.
  4153   3159   */
  4154   3160   static Bitmask codeOneLoopStart(
  4155   3161     WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
  4156   3162     int iLevel,          /* Which level of pWInfo->a[] should be coded */
  4157         -  u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
  4158   3163     Bitmask notReady     /* Which tables are currently available */
  4159   3164   ){
  4160   3165     int j, k;            /* Loop counters */
  4161   3166     int iCur;            /* The VDBE cursor for the table */
  4162   3167     int addrNxt;         /* Where to jump to continue with the next IN case */
  4163   3168     int omitTable;       /* True if we use the index only */
  4164   3169     int bRev;            /* True if we need to scan in reverse order */
  4165   3170     WhereLevel *pLevel;  /* The where level to be coded */
         3171  +  WhereLoop *pLoop;    /* The WhereLoop object being coded */
  4166   3172     WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
  4167   3173     WhereTerm *pTerm;               /* A WHERE clause term */
  4168   3174     Parse *pParse;                  /* Parsing context */
  4169   3175     Vdbe *v;                        /* The prepared stmt under constructions */
  4170   3176     struct SrcList_item *pTabItem;  /* FROM clause term being coded */
  4171   3177     int addrBrk;                    /* Jump here to break out of the loop */
  4172   3178     int addrCont;                   /* Jump here to continue with next cycle */
  4173   3179     int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
  4174   3180     int iReleaseReg = 0;      /* Temp register to free before returning */
  4175   3181     Bitmask newNotReady;      /* Return value */
  4176   3182   
  4177   3183     pParse = pWInfo->pParse;
  4178   3184     v = pParse->pVdbe;
  4179         -  pWC = pWInfo->pWC;
         3185  +  pWC = &pWInfo->sWC;
  4180   3186     pLevel = &pWInfo->a[iLevel];
         3187  +  pLoop = pLevel->pWLoop;
  4181   3188     pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
  4182   3189     iCur = pTabItem->iCursor;
  4183         -  bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
  4184         -  omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 
  4185         -           && (wctrlFlags & WHERE_FORCE_TABLE)==0;
         3190  +  bRev = (pWInfo->revMask>>iLevel)&1;
         3191  +  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
         3192  +           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
  4186   3193     VdbeNoopComment((v, "Begin Join Loop %d", iLevel));
  4187   3194   
  4188   3195     /* Create labels for the "break" and "continue" instructions
  4189   3196     ** for the current loop.  Jump to addrBrk to break out of a loop.
  4190   3197     ** Jump to cont to go immediately to the next iteration of the
  4191   3198     ** loop.
  4192   3199     **
................................................................................
  4215   3222       pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
  4216   3223       VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
  4217   3224       sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
  4218   3225       pLevel->op = OP_Goto;
  4219   3226     }else
  4220   3227   
  4221   3228   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4222         -  if(  (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  4223         -    /* Case 0:  The table is a virtual-table.  Use the VFilter and VNext
         3229  +  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
         3230  +    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
  4224   3231       **          to access the data.
  4225   3232       */
  4226   3233       int iReg;   /* P3 Value for OP_VFilter */
  4227   3234       int addrNotFound;
  4228         -    sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx;
  4229         -    int nConstraint = pVtabIdx->nConstraint;
  4230         -    struct sqlite3_index_constraint_usage *aUsage =
  4231         -                                                pVtabIdx->aConstraintUsage;
  4232         -    const struct sqlite3_index_constraint *aConstraint =
  4233         -                                                pVtabIdx->aConstraint;
         3235  +    int nConstraint = pLoop->nLTerm;
  4234   3236   
  4235   3237       sqlite3ExprCachePush(pParse);
  4236   3238       iReg = sqlite3GetTempRange(pParse, nConstraint+2);
  4237   3239       addrNotFound = pLevel->addrBrk;
  4238         -    for(j=1; j<=nConstraint; j++){
  4239         -      for(k=0; k<nConstraint; k++){
  4240         -        if( aUsage[k].argvIndex==j ){
  4241         -          int iTarget = iReg+j+1;
  4242         -          pTerm = &pWC->a[aConstraint[k].iTermOffset];
  4243         -          if( pTerm->eOperator & WO_IN ){
  4244         -            codeEqualityTerm(pParse, pTerm, pLevel, k, iTarget);
  4245         -            addrNotFound = pLevel->addrNxt;
  4246         -          }else{
  4247         -            sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
  4248         -          }
  4249         -          break;
  4250         -        }
  4251         -      }
  4252         -      if( k==nConstraint ) break;
  4253         -    }
  4254         -    sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg);
  4255         -    sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1);
  4256         -    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, pVtabIdx->idxStr,
  4257         -                      pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC);
  4258         -    pVtabIdx->needToFreeIdxStr = 0;
  4259   3240       for(j=0; j<nConstraint; j++){
  4260         -      if( aUsage[j].omit ){
  4261         -        int iTerm = aConstraint[j].iTermOffset;
  4262         -        disableTerm(pLevel, &pWC->a[iTerm]);
         3241  +      int iTarget = iReg+j+2;
         3242  +      pTerm = pLoop->aLTerm[j];
         3243  +      if( pTerm==0 ) continue;
         3244  +      if( pTerm->eOperator & WO_IN ){
         3245  +        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
         3246  +        addrNotFound = pLevel->addrNxt;
         3247  +      }else{
         3248  +        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
         3249  +      }
         3250  +    }
         3251  +    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
         3252  +    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
         3253  +    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
         3254  +                      pLoop->u.vtab.idxStr,
         3255  +                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
         3256  +    pLoop->u.vtab.needFree = 0;
         3257  +    for(j=0; j<nConstraint && j<16; j++){
         3258  +      if( (pLoop->u.vtab.omitMask>>j)&1 ){
         3259  +        disableTerm(pLevel, pLoop->aLTerm[j]);
  4263   3260         }
  4264   3261       }
  4265   3262       pLevel->op = OP_VNext;
  4266   3263       pLevel->p1 = iCur;
  4267   3264       pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  4268   3265       sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
  4269   3266       sqlite3ExprCachePop(pParse, 1);
  4270   3267     }else
  4271   3268   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  4272   3269   
  4273         -  if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){
  4274         -    /* Case 1:  We can directly reference a single row using an
         3270  +  if( (pLoop->wsFlags & WHERE_IPK)!=0
         3271  +   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
         3272  +  ){
         3273  +    /* Case 2:  We can directly reference a single row using an
  4275   3274       **          equality comparison against the ROWID field.  Or
  4276   3275       **          we reference multiple rows using a "rowid IN (...)"
  4277   3276       **          construct.
  4278   3277       */
         3278  +    assert( pLoop->u.btree.nEq==1 );
  4279   3279       iReleaseReg = sqlite3GetTempReg(pParse);
  4280         -    pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0);
         3280  +    pTerm = pLoop->aLTerm[0];
  4281   3281       assert( pTerm!=0 );
  4282   3282       assert( pTerm->pExpr!=0 );
  4283   3283       assert( omitTable==0 );
  4284   3284       testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  4285         -    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, iReleaseReg);
         3285  +    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
  4286   3286       addrNxt = pLevel->addrNxt;
  4287   3287       sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
  4288   3288       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
  4289   3289       sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
  4290   3290       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  4291   3291       VdbeComment((v, "pk"));
  4292   3292       pLevel->op = OP_Noop;
  4293         -  }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){
  4294         -    /* Case 2:  We have an inequality comparison against the ROWID field.
         3293  +  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
         3294  +         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
         3295  +  ){
         3296  +    /* Case 3:  We have an inequality comparison against the ROWID field.
  4295   3297       */
  4296   3298       int testOp = OP_Noop;
  4297   3299       int start;
  4298   3300       int memEndValue = 0;
  4299   3301       WhereTerm *pStart, *pEnd;
  4300   3302   
  4301   3303       assert( omitTable==0 );
  4302         -    pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0);
  4303         -    pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0);
         3304  +    j = 0;
         3305  +    pStart = pEnd = 0;
         3306  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
         3307  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
         3308  +    assert( pStart!=0 || pEnd!=0 );
  4304   3309       if( bRev ){
  4305   3310         pTerm = pStart;
  4306   3311         pStart = pEnd;
  4307   3312         pEnd = pTerm;
  4308   3313       }
  4309   3314       if( pStart ){
  4310   3315         Expr *pX;             /* The expression that defines the start bound */
................................................................................
  4351   3356         }
  4352   3357         disableTerm(pLevel, pEnd);
  4353   3358       }
  4354   3359       start = sqlite3VdbeCurrentAddr(v);
  4355   3360       pLevel->op = bRev ? OP_Prev : OP_Next;
  4356   3361       pLevel->p1 = iCur;
  4357   3362       pLevel->p2 = start;
  4358         -    if( pStart==0 && pEnd==0 ){
  4359         -      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  4360         -    }else{
  4361         -      assert( pLevel->p5==0 );
  4362         -    }
         3363  +    assert( pLevel->p5==0 );
  4363   3364       if( testOp!=OP_Noop ){
  4364   3365         iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
  4365   3366         sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
  4366   3367         sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  4367   3368         sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
  4368   3369         sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
  4369   3370       }
  4370         -  }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
  4371         -    /* Case 3: A scan using an index.
         3371  +  }else if( pLoop->wsFlags & WHERE_INDEXED ){
         3372  +    /* Case 4: A scan using an index.
  4372   3373       **
  4373   3374       **         The WHERE clause may contain zero or more equality 
  4374   3375       **         terms ("==" or "IN" operators) that refer to the N
  4375   3376       **         left-most columns of the index. It may also contain
  4376   3377       **         inequality constraints (>, <, >= or <=) on the indexed
  4377   3378       **         column that immediately follows the N equalities. Only 
  4378   3379       **         the right-most column can be an inequality - the rest must
................................................................................
  4410   3411         OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
  4411   3412       };
  4412   3413       static const u8 aEndOp[] = {
  4413   3414         OP_Noop,             /* 0: (!end_constraints) */
  4414   3415         OP_IdxGE,            /* 1: (end_constraints && !bRev) */
  4415   3416         OP_IdxLT             /* 2: (end_constraints && bRev) */
  4416   3417       };
  4417         -    int nEq = pLevel->plan.nEq;  /* Number of == or IN terms */
  4418         -    int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
         3418  +    int nEq = pLoop->u.btree.nEq;  /* Number of == or IN terms */
         3419  +    int isMinQuery = 0;            /* If this is an optimized SELECT min(x).. */
  4419   3420       int regBase;                 /* Base register holding constraint values */
  4420   3421       int r1;                      /* Temp register */
  4421   3422       WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
  4422   3423       WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
  4423   3424       int startEq;                 /* True if range start uses ==, >= or <= */
  4424   3425       int endEq;                   /* True if range end uses ==, >= or <= */
  4425   3426       int start_constraints;       /* Start of range is constrained */
................................................................................
  4427   3428       Index *pIdx;                 /* The index we will be using */
  4428   3429       int iIdxCur;                 /* The VDBE cursor for the index */
  4429   3430       int nExtraReg = 0;           /* Number of extra registers needed */
  4430   3431       int op;                      /* Instruction opcode */
  4431   3432       char *zStartAff;             /* Affinity for start of range constraint */
  4432   3433       char *zEndAff;               /* Affinity for end of range constraint */
  4433   3434   
  4434         -    pIdx = pLevel->plan.u.pIdx;
         3435  +    pIdx = pLoop->u.btree.pIndex;
  4435   3436       iIdxCur = pLevel->iIdxCur;
  4436         -    k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
  4437   3437   
  4438   3438       /* If this loop satisfies a sort order (pOrderBy) request that 
  4439   3439       ** was passed to this function to implement a "SELECT min(x) ..." 
  4440   3440       ** query, then the caller will only allow the loop to run for
  4441   3441       ** a single iteration. This means that the first row returned
  4442   3442       ** should not have a NULL value stored in 'x'. If column 'x' is
  4443   3443       ** the first one after the nEq equality constraints in the index,
  4444   3444       ** this requires some special handling.
  4445   3445       */
  4446         -    if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
  4447         -     && (pLevel->plan.wsFlags&WHERE_ORDERED)
         3446  +    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
         3447  +     && (pWInfo->bOBSat!=0)
  4448   3448        && (pIdx->nColumn>nEq)
  4449   3449       ){
  4450   3450         /* assert( pOrderBy->nExpr==1 ); */
  4451   3451         /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
  4452   3452         isMinQuery = 1;
  4453   3453         nExtraReg = 1;
  4454   3454       }
  4455   3455   
  4456   3456       /* Find any inequality constraint terms for the start and end 
  4457   3457       ** of the range. 
  4458   3458       */
  4459         -    if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){
  4460         -      pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx);
         3459  +    j = nEq;
         3460  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
         3461  +      pRangeStart = pLoop->aLTerm[j++];
  4461   3462         nExtraReg = 1;
  4462   3463       }
  4463         -    if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){
  4464         -      pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx);
         3464  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
         3465  +      pRangeEnd = pLoop->aLTerm[j++];
  4465   3466         nExtraReg = 1;
  4466   3467       }
  4467   3468   
  4468   3469       /* Generate code to evaluate all constraint terms using == or IN
  4469   3470       ** and store the values of those terms in an array of registers
  4470   3471       ** starting at regBase.
  4471   3472       */
  4472         -    regBase = codeAllEqualityTerms(
  4473         -        pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff
  4474         -    );
         3473  +    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
  4475   3474       zEndAff = sqlite3DbStrDup(pParse->db, zStartAff);
  4476   3475       addrNxt = pLevel->addrNxt;
  4477   3476   
  4478   3477       /* If we are doing a reverse order scan on an ascending index, or
  4479   3478       ** a forward order scan on a descending index, interchange the 
  4480   3479       ** start and end terms (pRangeStart and pRangeEnd).
  4481   3480       */
  4482   3481       if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
  4483   3482        || (bRev && pIdx->nColumn==nEq)
  4484   3483       ){
  4485   3484         SWAP(WhereTerm *, pRangeEnd, pRangeStart);
  4486   3485       }
  4487   3486   
  4488         -    testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
  4489         -    testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
  4490         -    testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE );
  4491         -    testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE );
         3487  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
         3488  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
         3489  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
         3490  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
  4492   3491       startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
  4493   3492       endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
  4494   3493       start_constraints = pRangeStart || nEq>0;
  4495   3494   
  4496   3495       /* Seek the index cursor to the start of the range. */
  4497   3496       nConstraint = nEq;
  4498   3497       if( pRangeStart ){
................................................................................
  4574   3573       }
  4575   3574   
  4576   3575       /* If there are inequality constraints, check that the value
  4577   3576       ** of the table column that the inequality contrains is not NULL.
  4578   3577       ** If it is, jump to the next iteration of the loop.
  4579   3578       */
  4580   3579       r1 = sqlite3GetTempReg(pParse);
  4581         -    testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT );
  4582         -    testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT );
  4583         -    if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
         3580  +    testcase( pLoop->wsFlags & WHERE_BTM_LIMIT );
         3581  +    testcase( pLoop->wsFlags & WHERE_TOP_LIMIT );
         3582  +    if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){
  4584   3583         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
  4585   3584         sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
  4586   3585       }
  4587   3586       sqlite3ReleaseTempReg(pParse, r1);
  4588   3587   
  4589   3588       /* Seek the table cursor, if required */
  4590   3589       disableTerm(pLevel, pRangeStart);
................................................................................
  4595   3594         sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  4596   3595         sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
  4597   3596       }
  4598   3597   
  4599   3598       /* Record the instruction used to terminate the loop. Disable 
  4600   3599       ** WHERE clause terms made redundant by the index range scan.
  4601   3600       */
  4602         -    if( pLevel->plan.wsFlags & WHERE_UNIQUE ){
         3601  +    if( pLoop->wsFlags & WHERE_ONEROW ){
  4603   3602         pLevel->op = OP_Noop;
  4604   3603       }else if( bRev ){
  4605   3604         pLevel->op = OP_Prev;
  4606   3605       }else{
  4607   3606         pLevel->op = OP_Next;
  4608   3607       }
  4609   3608       pLevel->p1 = iIdxCur;
  4610         -    if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
         3609  +    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
  4611   3610         pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  4612   3611       }else{
  4613   3612         assert( pLevel->p5==0 );
  4614   3613       }
  4615   3614     }else
  4616   3615   
  4617   3616   #ifndef SQLITE_OMIT_OR_OPTIMIZATION
  4618         -  if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
  4619         -    /* Case 4:  Two or more separately indexed terms connected by OR
         3617  +  if( pLoop->wsFlags & WHERE_MULTI_OR ){
         3618  +    /* Case 5:  Two or more separately indexed terms connected by OR
  4620   3619       **
  4621   3620       ** Example:
  4622   3621       **
  4623   3622       **   CREATE TABLE t1(a,b,c,d);
  4624   3623       **   CREATE INDEX i1 ON t1(a);
  4625   3624       **   CREATE INDEX i2 ON t1(b);
  4626   3625       **   CREATE INDEX i3 ON t1(c);
................................................................................
  4665   3664       int regRowid = 0;                         /* Register holding rowid */
  4666   3665       int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
  4667   3666       int iRetInit;                             /* Address of regReturn init */
  4668   3667       int untestedTerms = 0;             /* Some terms not completely tested */
  4669   3668       int ii;                            /* Loop counter */
  4670   3669       Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
  4671   3670      
  4672         -    pTerm = pLevel->plan.u.pTerm;
         3671  +    pTerm = pLoop->aLTerm[0];
  4673   3672       assert( pTerm!=0 );
  4674   3673       assert( pTerm->eOperator & WO_OR );
  4675   3674       assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
  4676   3675       pOrWc = &pTerm->u.pOrInfo->wc;
  4677   3676       pLevel->op = OP_Return;
  4678   3677       pLevel->p1 = regReturn;
  4679   3678   
................................................................................
  4684   3683       if( pWInfo->nLevel>1 ){
  4685   3684         int nNotReady;                 /* The number of notReady tables */
  4686   3685         struct SrcList_item *origSrc;     /* Original list of tables */
  4687   3686         nNotReady = pWInfo->nLevel - iLevel - 1;
  4688   3687         pOrTab = sqlite3StackAllocRaw(pParse->db,
  4689   3688                               sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
  4690   3689         if( pOrTab==0 ) return notReady;
  4691         -      pOrTab->nAlloc = (i16)(nNotReady + 1);
         3690  +      pOrTab->nAlloc = (u8)(nNotReady + 1);
  4692   3691         pOrTab->nSrc = pOrTab->nAlloc;
  4693   3692         memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
  4694   3693         origSrc = pWInfo->pTabList->a;
  4695   3694         for(k=1; k<=nNotReady; k++){
  4696   3695           memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
  4697   3696         }
  4698   3697       }else{
................................................................................
  4706   3705       ** immediately following the OP_Return at the bottom of the loop. This
  4707   3706       ** is required in a few obscure LEFT JOIN cases where control jumps
  4708   3707       ** over the top of the loop into the body of it. In this case the 
  4709   3708       ** correct response for the end-of-loop code (the OP_Return) is to 
  4710   3709       ** fall through to the next instruction, just as an OP_Next does if
  4711   3710       ** called on an uninitialized cursor.
  4712   3711       */
  4713         -    if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         3712  +    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  4714   3713         regRowset = ++pParse->nMem;
  4715   3714         regRowid = ++pParse->nMem;
  4716   3715         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
  4717   3716       }
  4718   3717       iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
  4719   3718   
  4720   3719       /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
................................................................................
  4757   3756           }
  4758   3757           /* Loop through table entries that match term pOrTerm. */
  4759   3758           pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  4760   3759                           WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
  4761   3760                           WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
  4762   3761           assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
  4763   3762           if( pSubWInfo ){
  4764         -          WhereLevel *pLvl;
         3763  +          WhereLoop *pSubLoop;
  4765   3764             explainOneScan(
  4766   3765                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  4767   3766             );
  4768         -          if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         3767  +          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  4769   3768               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  4770   3769               int r;
  4771   3770               r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
  4772   3771                                            regRowid, 0);
  4773   3772               sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
  4774   3773                                    sqlite3VdbeCurrentAddr(v)+2, r, iSet);
  4775   3774             }
................................................................................
  4790   3789             ** If the call to sqlite3WhereBegin() above resulted in a scan that
  4791   3790             ** uses an index, and this is either the first OR-connected term
  4792   3791             ** processed or the index is the same as that used by all previous
  4793   3792             ** terms, set pCov to the candidate covering index. Otherwise, set 
  4794   3793             ** pCov to NULL to indicate that no candidate covering index will 
  4795   3794             ** be available.
  4796   3795             */
  4797         -          pLvl = &pSubWInfo->a[0];
  4798         -          if( (pLvl->plan.wsFlags & WHERE_INDEXED)!=0
  4799         -           && (pLvl->plan.wsFlags & WHERE_TEMP_INDEX)==0
  4800         -           && (ii==0 || pLvl->plan.u.pIdx==pCov)
         3796  +          pSubLoop = pSubWInfo->a[0].pWLoop;
         3797  +          assert( (pSubLoop->wsFlags & WHERE_TEMP_INDEX)==0 );
         3798  +          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
         3799  +           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
  4801   3800             ){
  4802         -            assert( pLvl->iIdxCur==iCovCur );
  4803         -            pCov = pLvl->plan.u.pIdx;
         3801  +            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
         3802  +            pCov = pSubLoop->u.btree.pIndex;
  4804   3803             }else{
  4805   3804               pCov = 0;
  4806   3805             }
  4807   3806   
  4808   3807             /* Finish the loop through table entries that match term pOrTerm. */
  4809   3808             sqlite3WhereEnd(pSubWInfo);
  4810   3809           }
................................................................................
  4822   3821   
  4823   3822       if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
  4824   3823       if( !untestedTerms ) disableTerm(pLevel, pTerm);
  4825   3824     }else
  4826   3825   #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  4827   3826   
  4828   3827     {
  4829         -    /* Case 5:  There is no usable index.  We must do a complete
         3828  +    /* Case 6:  There is no usable index.  We must do a complete
  4830   3829       **          scan of the entire table.
  4831   3830       */
  4832   3831       static const u8 aStep[] = { OP_Next, OP_Prev };
  4833   3832       static const u8 aStart[] = { OP_Rewind, OP_Last };
  4834   3833       assert( bRev==0 || bRev==1 );
  4835         -    assert( omitTable==0 );
  4836   3834       pLevel->op = aStep[bRev];
  4837   3835       pLevel->p1 = iCur;
  4838   3836       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
  4839   3837       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  4840   3838     }
  4841         -  newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur);
         3839  +  newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
  4842   3840   
  4843   3841     /* Insert code to test every subexpression that can be completely
  4844   3842     ** computed using the current set of tables.
  4845   3843     **
  4846   3844     ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
  4847   3845     ** the use of indices become tests that are evaluated against each row of
  4848   3846     ** the relevant input tables.
................................................................................
  4884   3882       if( pTerm->leftCursor!=iCur ) continue;
  4885   3883       pE = pTerm->pExpr;
  4886   3884       assert( !ExprHasProperty(pE, EP_FromJoin) );
  4887   3885       assert( (pTerm->prereqRight & newNotReady)!=0 );
  4888   3886       pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
  4889   3887       if( pAlt==0 ) continue;
  4890   3888       if( pAlt->wtFlags & (TERM_CODED) ) continue;
         3889  +    testcase( pAlt->eOperator & WO_EQ );
         3890  +    testcase( pAlt->eOperator & WO_IN );
  4891   3891       VdbeNoopComment((v, "begin transitive constraint"));
  4892   3892       sEq = *pAlt->pExpr;
  4893   3893       sEq.pLeft = pE->pLeft;
  4894   3894       sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL);
  4895   3895     }
  4896   3896   
  4897   3897     /* For a LEFT OUTER JOIN, generate code that will record the fact that
................................................................................
  4916   3916       }
  4917   3917     }
  4918   3918     sqlite3ReleaseTempReg(pParse, iReleaseReg);
  4919   3919   
  4920   3920     return newNotReady;
  4921   3921   }
  4922   3922   
  4923         -#if defined(SQLITE_TEST)
  4924         -/*
  4925         -** The following variable holds a text description of query plan generated
  4926         -** by the most recent call to sqlite3WhereBegin().  Each call to WhereBegin
  4927         -** overwrites the previous.  This information is used for testing and
  4928         -** analysis only.
  4929         -*/
  4930         -char sqlite3_query_plan[BMS*2*40];  /* Text of the join */
  4931         -static int nQPlan = 0;              /* Next free slow in _query_plan[] */
  4932         -
  4933         -#endif /* SQLITE_TEST */
  4934         -
         3923  +#ifdef WHERETRACE_ENABLED
         3924  +/*
         3925  +** Print a WhereLoop object for debugging purposes
         3926  +*/
         3927  +static void whereLoopPrint(WhereLoop *p, SrcList *pTabList){
         3928  +  int nb = 1+(pTabList->nSrc+7)/8;
         3929  +  struct SrcList_item *pItem = pTabList->a + p->iTab;
         3930  +  Table *pTab = pItem->pTab;
         3931  +  sqlite3DebugPrintf("%c %2d.%0*llx.%0*llx", p->cId,
         3932  +                     p->iTab, nb, p->maskSelf, nb, p->prereq);
         3933  +  sqlite3DebugPrintf(" %8s",
         3934  +                     pItem->zAlias ? pItem->zAlias : pTab->zName);
         3935  +  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
         3936  +    if( p->u.btree.pIndex ){
         3937  +      const char *zName = p->u.btree.pIndex->zName;
         3938  +      if( zName==0 ) zName = "ipk";
         3939  +      if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
         3940  +        int i = sqlite3Strlen30(zName) - 1;
         3941  +        while( zName[i]!='_' ) i--;
         3942  +        zName += i;
         3943  +      }
         3944  +      sqlite3DebugPrintf(".%-12s %2d", zName, p->u.btree.nEq);
         3945  +    }else{
         3946  +      sqlite3DebugPrintf("%16s","");
         3947  +    }
         3948  +  }else{
         3949  +    char *z;
         3950  +    if( p->u.vtab.idxStr ){
         3951  +      z = sqlite3_mprintf("(%d,\"%s\",%x)",
         3952  +                p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
         3953  +    }else{
         3954  +      z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
         3955  +    }
         3956  +    sqlite3DebugPrintf(" %-15s", z);
         3957  +    sqlite3_free(z);
         3958  +  }
         3959  +  sqlite3DebugPrintf(" fg %05x N %d", p->wsFlags, p->nLTerm);
         3960  +  sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
         3961  +}
         3962  +#endif
         3963  +
         3964  +/*
         3965  +** Convert bulk memory into a valid WhereLoop that can be passed
         3966  +** to whereLoopClear harmlessly.
         3967  +*/
         3968  +static void whereLoopInit(WhereLoop *p){
         3969  +  p->aLTerm = p->aLTermSpace;
         3970  +  p->nLTerm = 0;
         3971  +  p->nLSlot = ArraySize(p->aLTermSpace);
         3972  +  p->wsFlags = 0;
         3973  +}
         3974  +
         3975  +/*
         3976  +** Clear the WhereLoop.u union.  Leave WhereLoop.pLTerm intact.
         3977  +*/
         3978  +static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
         3979  +  if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_TEMP_INDEX) ){
         3980  +    if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
         3981  +      sqlite3_free(p->u.vtab.idxStr);
         3982  +      p->u.vtab.needFree = 0;
         3983  +      p->u.vtab.idxStr = 0;
         3984  +    }else if( (p->wsFlags & WHERE_TEMP_INDEX)!=0 && p->u.btree.pIndex!=0 ){
         3985  +      sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
         3986  +      sqlite3DbFree(db, p->u.btree.pIndex);
         3987  +      p->u.btree.pIndex = 0;
         3988  +    }
         3989  +  }
         3990  +}
         3991  +
         3992  +/*
         3993  +** Deallocate internal memory used by a WhereLoop object
         3994  +*/
         3995  +static void whereLoopClear(sqlite3 *db, WhereLoop *p){
         3996  +  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
         3997  +  whereLoopClearUnion(db, p);
         3998  +  whereLoopInit(p);
         3999  +}
         4000  +
         4001  +/*
         4002  +** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
         4003  +*/
         4004  +static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
         4005  +  WhereTerm **paNew;
         4006  +  if( p->nLSlot>=n ) return SQLITE_OK;
         4007  +  n = (n+7)&~7;
         4008  +  paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
         4009  +  if( paNew==0 ) return SQLITE_NOMEM;
         4010  +  memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
         4011  +  if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
         4012  +  p->aLTerm = paNew;
         4013  +  p->nLSlot = n;
         4014  +  return SQLITE_OK;
         4015  +}
         4016  +
         4017  +/*
         4018  +** Transfer content from the second pLoop into the first.
         4019  +*/
         4020  +static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
         4021  +  if( whereLoopResize(db, pTo, pFrom->nLTerm) ) return SQLITE_NOMEM;
         4022  +  whereLoopClearUnion(db, pTo);
         4023  +  memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
         4024  +  memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
         4025  +  if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
         4026  +    pFrom->u.vtab.needFree = 0;
         4027  +  }else if( (pFrom->wsFlags & WHERE_TEMP_INDEX)!=0 ){
         4028  +    pFrom->u.btree.pIndex = 0;
         4029  +  }
         4030  +  return SQLITE_OK;
         4031  +}
         4032  +
         4033  +/*
         4034  +** Delete a WhereLoop object
         4035  +*/
         4036  +static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
         4037  +  whereLoopClear(db, p);
         4038  +  sqlite3DbFree(db, p);
         4039  +}
  4935   4040   
  4936   4041   /*
  4937   4042   ** Free a WhereInfo structure
  4938   4043   */
  4939   4044   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
  4940   4045     if( ALWAYS(pWInfo) ){
  4941         -    int i;
  4942         -    for(i=0; i<pWInfo->nLevel; i++){
  4943         -      sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo;
  4944         -      if( pInfo ){
  4945         -        /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */
  4946         -        if( pInfo->needToFreeIdxStr ){
  4947         -          sqlite3_free(pInfo->idxStr);
  4948         -        }
  4949         -        sqlite3DbFree(db, pInfo);
  4950         -      }
  4951         -      if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){
  4952         -        Index *pIdx = pWInfo->a[i].plan.u.pIdx;
  4953         -        if( pIdx ){
  4954         -          sqlite3DbFree(db, pIdx->zColAff);
  4955         -          sqlite3DbFree(db, pIdx);
  4956         -        }
  4957         -      }
         4046  +    whereClauseClear(&pWInfo->sWC);
         4047  +    while( pWInfo->pLoops ){
         4048  +      WhereLoop *p = pWInfo->pLoops;
         4049  +      pWInfo->pLoops = p->pNextLoop;
         4050  +      whereLoopDelete(db, p);
  4958   4051       }
  4959         -    whereClauseClear(pWInfo->pWC);
  4960   4052       sqlite3DbFree(db, pWInfo);
  4961   4053     }
  4962   4054   }
  4963   4055   
         4056  +/*
         4057  +** Insert or replace a WhereLoop entry using the template supplied.
         4058  +**
         4059  +** An existing WhereLoop entry might be overwritten if the new template
         4060  +** is better and has fewer dependencies.  Or the template will be ignored
         4061  +** and no insert will occur if an existing WhereLoop is faster and has
         4062  +** fewer dependencies than the template.  Otherwise a new WhereLoop is
         4063  +** added based on the template.
         4064  +**
         4065  +** If pBuilder->pBest is not NULL then we only care about the very
         4066  +** best template and that template should be stored in pBuilder->pBest.
         4067  +** If pBuilder->pBest is NULL then a list of the best templates are stored
         4068  +** in pBuilder->pWInfo->pLoops.
         4069  +**
         4070  +** When accumulating multiple loops (when pBuilder->pBest is NULL) we
         4071  +** still might overwrite similar loops with the new template if the
         4072  +** template is better.  Loops may be overwritten if the following 
         4073  +** conditions are met:
         4074  +**
         4075  +**    (1)  They have the same iTab.
         4076  +**    (2)  They have the same iSortIdx.
         4077  +**    (3)  The template has same or fewer dependencies than the current loop
         4078  +**    (4)  The template has the same or lower cost than the current loop
         4079  +**    (5)  The template uses more terms of the same index but has no additional
         4080  +**         dependencies          
         4081  +*/
         4082  +static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
         4083  +  WhereLoop **ppPrev, *p, *pNext = 0;
         4084  +  WhereInfo *pWInfo = pBuilder->pWInfo;
         4085  +  sqlite3 *db = pWInfo->pParse->db;
         4086  +
         4087  +  /* If pBuilder->pBest is defined, then only keep track of the single
         4088  +  ** best WhereLoop.  pBuilder->pBest->maskSelf==0 indicates that no
         4089  +  ** prior WhereLoops have been evaluated and that the current pTemplate
         4090  +  ** is therefore the first and hence the best and should be retained.
         4091  +  */
         4092  +  if( (p = pBuilder->pBest)!=0 ){
         4093  +    if( p->maskSelf!=0 ){
         4094  +      WhereCost rCost = whereCostAdd(p->rRun,p->rSetup);
         4095  +      WhereCost rTemplate = whereCostAdd(pTemplate->rRun,pTemplate->rSetup);
         4096  +      if( rCost < rTemplate ){
         4097  +        testcase( rCost==rTemplate-1 );
         4098  +        goto whereLoopInsert_noop;
         4099  +      }
         4100  +      if( rCost==rTemplate && (p->prereq & pTemplate->prereq)==p->prereq ){
         4101  +        goto whereLoopInsert_noop;
         4102  +      }
         4103  +    }
         4104  +#if WHERETRACE_ENABLED
         4105  +    if( sqlite3WhereTrace & 0x8 ){
         4106  +      sqlite3DebugPrintf(p->maskSelf==0 ? "ins-init: " : "ins-best: ");
         4107  +      whereLoopPrint(pTemplate, pWInfo->pTabList);
         4108  +    }
         4109  +#endif
         4110  +    whereLoopXfer(db, p, pTemplate);
         4111  +    return SQLITE_OK;
         4112  +  }
         4113  +
         4114  +  /* Search for an existing WhereLoop to overwrite, or which takes
         4115  +  ** priority over pTemplate.
         4116  +  */
         4117  +  for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
         4118  +    if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
         4119  +      /* If either the iTab or iSortIdx values for two WhereLoop are different
         4120  +      ** then those WhereLoops need to be considered separately.  Neither is
         4121  +      ** a candidate to replace the other. */
         4122  +      continue;
         4123  +    }
         4124  +    /* In the current implementation, the rSetup value is either zero
         4125  +    ** or the cost of building an automatic index (NlogN) and the NlogN
         4126  +    ** is the same for compatible WhereLoops. */
         4127  +    assert( p->rSetup==0 || pTemplate->rSetup==0 
         4128  +                 || p->rSetup==pTemplate->rSetup );
         4129  +
         4130  +    /* whereLoopAddBtree() always generates and inserts the automatic index
         4131  +    ** case first.  Hence compatible candidate WhereLoops never have a larger
         4132  +    ** rSetup. Call this SETUP-INVARIANT */
         4133  +    assert( p->rSetup>=pTemplate->rSetup );
         4134  +
         4135  +    if( (p->prereq & pTemplate->prereq)==p->prereq
         4136  +     && p->rSetup<=pTemplate->rSetup
         4137  +     && p->rRun<=pTemplate->rRun
         4138  +    ){
         4139  +      /* This branch taken when p is equal or better than pTemplate in 
         4140  +      ** all of (1) dependences (2) setup-cost, and (3) run-cost. */
         4141  +      assert( p->rSetup==pTemplate->rSetup );
         4142  +      if( p->nLTerm<pTemplate->nLTerm
         4143  +       && (p->wsFlags & WHERE_INDEXED)!=0
         4144  +       && (pTemplate->wsFlags & WHERE_INDEXED)!=0
         4145  +       && p->u.btree.pIndex==pTemplate->u.btree.pIndex
         4146  +       && p->prereq==pTemplate->prereq
         4147  +      ){
         4148  +        /* Overwrite an existing WhereLoop with an similar one that uses
         4149  +        ** more terms of the index */
         4150  +        pNext = p->pNextLoop;
         4151  +        break;
         4152  +      }else{
         4153  +        /* pTemplate is not helpful.
         4154  +        ** Return without changing or adding anything */
         4155  +        goto whereLoopInsert_noop;
         4156  +      }
         4157  +    }
         4158  +    if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
         4159  +     && p->rRun>=pTemplate->rRun
         4160  +     && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */
         4161  +    ){
         4162  +      /* Overwrite an existing WhereLoop with a better one: one that is
         4163  +      ** better at one of (1) dependences, (2) setup-cost, or (3) run-cost
         4164  +      ** and is no worse in any of those categories. */
         4165  +      pNext = p->pNextLoop;
         4166  +      break;
         4167  +    }
         4168  +  }
         4169  +
         4170  +  /* If we reach this point it means that either p[] should be overwritten
         4171  +  ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
         4172  +  ** WhereLoop and insert it.
         4173  +  */
         4174  +#if WHERETRACE_ENABLED
         4175  +  if( sqlite3WhereTrace & 0x8 ){
         4176  +    if( p!=0 ){
         4177  +      sqlite3DebugPrintf("ins-del:  ");
         4178  +      whereLoopPrint(p, pWInfo->pTabList);
         4179  +    }
         4180  +    sqlite3DebugPrintf("ins-new:  ");
         4181  +    whereLoopPrint(pTemplate, pWInfo->pTabList);
         4182  +  }
         4183  +#endif
         4184  +  if( p==0 ){
         4185  +    p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
         4186  +    if( p==0 ) return SQLITE_NOMEM;
         4187  +    whereLoopInit(p);
         4188  +  }
         4189  +  whereLoopXfer(db, p, pTemplate);
         4190  +  p->pNextLoop = pNext;
         4191  +  *ppPrev = p;
         4192  +  if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
         4193  +    Index *pIndex = p->u.btree.pIndex;
         4194  +    if( pIndex && pIndex->tnum==0 ){
         4195  +      p->u.btree.pIndex = 0;
         4196  +    }
         4197  +  }
         4198  +  return SQLITE_OK;
         4199  +
         4200  +  /* Jump here if the insert is a no-op */
         4201  +whereLoopInsert_noop:
         4202  +#if WHERETRACE_ENABLED
         4203  +  if( sqlite3WhereTrace & 0x8 ){
         4204  +    sqlite3DebugPrintf(pBuilder->pBest ? "ins-skip: " : "ins-noop: ");
         4205  +    whereLoopPrint(pTemplate, pWInfo->pTabList);
         4206  +  }
         4207  +#endif
         4208  +  return SQLITE_OK;  
         4209  +}
         4210  +
         4211  +/*
         4212  +** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
         4213  +** Try to match one more.
         4214  +**
         4215  +** If pProbe->tnum==0, that means pIndex is a fake index used for the
         4216  +** INTEGER PRIMARY KEY.
         4217  +*/
         4218  +static int whereLoopAddBtreeIndex(
         4219  +  WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
         4220  +  struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
         4221  +  Index *pProbe,                  /* An index on pSrc */
         4222  +  WhereCost nInMul                /* log(Number of iterations due to IN) */
         4223  +){
         4224  +  WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
         4225  +  Parse *pParse = pWInfo->pParse;        /* Parsing context */
         4226  +  sqlite3 *db = pParse->db;       /* Database connection malloc context */
         4227  +  WhereLoop *pNew;                /* Template WhereLoop under construction */
         4228  +  WhereTerm *pTerm;               /* A WhereTerm under consideration */
         4229  +  int opMask;                     /* Valid operators for constraints */
         4230  +  WhereScan scan;                 /* Iterator for WHERE terms */
         4231  +  Bitmask saved_prereq;           /* Original value of pNew->prereq */
         4232  +  u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
         4233  +  int saved_nEq;                  /* Original value of pNew->u.btree.nEq */
         4234  +  u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
         4235  +  WhereCost saved_nOut;           /* Original value of pNew->nOut */
         4236  +  int iCol;                       /* Index of the column in the table */
         4237  +  int rc = SQLITE_OK;             /* Return code */
         4238  +  WhereCost nRowEst;              /* Estimated index selectivity */
         4239  +  WhereCost rLogSize;             /* Logarithm of table size */
         4240  +  WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
         4241  +
         4242  +  pNew = pBuilder->pNew;
         4243  +  if( db->mallocFailed ) return SQLITE_NOMEM;
         4244  +
         4245  +  assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
         4246  +  assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
         4247  +  if( pNew->wsFlags & WHERE_BTM_LIMIT ){
         4248  +    opMask = WO_LT|WO_LE;
         4249  +  }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
         4250  +    opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
         4251  +  }else{
         4252  +    opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
         4253  +  }
         4254  +  if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
         4255  +
         4256  +  assert( pNew->u.btree.nEq<=pProbe->nColumn );
         4257  +  if( pNew->u.btree.nEq < pProbe->nColumn ){
         4258  +    iCol = pProbe->aiColumn[pNew->u.btree.nEq];
         4259  +    nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
         4260  +    if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
         4261  +  }else{
         4262  +    iCol = -1;
         4263  +    nRowEst = 0;
         4264  +  }
         4265  +  pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
         4266  +                        opMask, pProbe);
         4267  +  saved_nEq = pNew->u.btree.nEq;
         4268  +  saved_nLTerm = pNew->nLTerm;
         4269  +  saved_wsFlags = pNew->wsFlags;
         4270  +  saved_prereq = pNew->prereq;
         4271  +  saved_nOut = pNew->nOut;
         4272  +  pNew->rSetup = 0;
         4273  +  rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
         4274  +  for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
         4275  +    int nIn = 0;
         4276  +    if( pTerm->prereqRight & pNew->maskSelf ) continue;
         4277  +    pNew->wsFlags = saved_wsFlags;
         4278  +    pNew->u.btree.nEq = saved_nEq;
         4279  +    pNew->nLTerm = saved_nLTerm;
         4280  +    if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
         4281  +    pNew->aLTerm[pNew->nLTerm++] = pTerm;
         4282  +    pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
         4283  +    pNew->rRun = rLogSize; /* Baseline cost is log2(N).  Adjustments below */
         4284  +    if( pTerm->eOperator & WO_IN ){
         4285  +      Expr *pExpr = pTerm->pExpr;
         4286  +      pNew->wsFlags |= WHERE_COLUMN_IN;
         4287  +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
         4288  +        /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
         4289  +        nIn = 46;  assert( 46==whereCost(25) );
         4290  +      }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
         4291  +        /* "x IN (value, value, ...)" */
         4292  +        nIn = whereCost(pExpr->x.pList->nExpr);
         4293  +      }
         4294  +      pNew->rRun += nIn;
         4295  +      pNew->u.btree.nEq++;
         4296  +      pNew->nOut = nRowEst + nInMul + nIn;
         4297  +    }else if( pTerm->eOperator & (WO_EQ) ){
         4298  +      assert( (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN))!=0
         4299  +                  || nInMul==0 );
         4300  +      pNew->wsFlags |= WHERE_COLUMN_EQ;
         4301  +      if( iCol<0  
         4302  +       || (pProbe->onError!=OE_None && nInMul==0
         4303  +           && pNew->u.btree.nEq==pProbe->nColumn-1)
         4304  +      ){
         4305  +        assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
         4306  +        pNew->wsFlags |= WHERE_ONEROW;
         4307  +      }
         4308  +      pNew->u.btree.nEq++;
         4309  +      pNew->nOut = nRowEst + nInMul;
         4310  +    }else if( pTerm->eOperator & (WO_ISNULL) ){
         4311  +      pNew->wsFlags |= WHERE_COLUMN_NULL;
         4312  +      pNew->u.btree.nEq++;
         4313  +      /* TUNING: IS NULL selects 2 rows */
         4314  +      nIn = 10;  assert( 10==whereCost(2) );
         4315  +      pNew->nOut = nRowEst + nInMul + nIn;
         4316  +    }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
         4317  +      testcase( pTerm->eOperator & WO_GT );
         4318  +      testcase( pTerm->eOperator & WO_GE );
         4319  +      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
         4320  +      pBtm = pTerm;
         4321  +      pTop = 0;
         4322  +    }else{
         4323  +      assert( pTerm->eOperator & (WO_LT|WO_LE) );
         4324  +      testcase( pTerm->eOperator & WO_LT );
         4325  +      testcase( pTerm->eOperator & WO_LE );
         4326  +      pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
         4327  +      pTop = pTerm;
         4328  +      pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
         4329  +                     pNew->aLTerm[pNew->nLTerm-2] : 0;
         4330  +    }
         4331  +    if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
         4332  +      /* Adjust nOut and rRun for STAT3 range values */
         4333  +      WhereCost rDiv;
         4334  +      whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
         4335  +                        pBtm, pTop, &rDiv);
         4336  +      pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10;
         4337  +    }
         4338  +#ifdef SQLITE_ENABLE_STAT3
         4339  +    if( pNew->u.btree.nEq==1 && pProbe->nSample ){
         4340  +      tRowcnt nOut = 0;
         4341  +      if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
         4342  +        testcase( pTerm->eOperator & WO_EQ );
         4343  +        testcase( pTerm->eOperator & WO_ISNULL );
         4344  +        rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
         4345  +      }else if( (pTerm->eOperator & WO_IN)
         4346  +             &&  !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)  ){
         4347  +        rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
         4348  +      }
         4349  +      if( rc==SQLITE_OK ) pNew->nOut = whereCost(nOut);
         4350  +    }
         4351  +#endif
         4352  +    if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
         4353  +      /* Each row involves a step of the index, then a binary search of
         4354  +      ** the main table */
         4355  +      pNew->rRun =  whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10);
         4356  +    }
         4357  +    /* Step cost for each output row */
         4358  +    pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut);
         4359  +    /* TBD: Adjust nOut for additional constraints */
         4360  +    rc = whereLoopInsert(pBuilder, pNew);
         4361  +    if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
         4362  +     && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
         4363  +    ){
         4364  +      whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
         4365  +    }
         4366  +  }
         4367  +  pNew->prereq = saved_prereq;
         4368  +  pNew->u.btree.nEq = saved_nEq;
         4369  +  pNew->wsFlags = saved_wsFlags;
         4370  +  pNew->nOut = saved_nOut;
         4371  +  pNew->nLTerm = saved_nLTerm;
         4372  +  return rc;
         4373  +}
         4374  +
         4375  +/*
         4376  +** Return True if it is possible that pIndex might be useful in
         4377  +** implementing the ORDER BY clause in pBuilder.
         4378  +**
         4379  +** Return False if pBuilder does not contain an ORDER BY clause or
         4380  +** if there is no way for pIndex to be useful in implementing that
         4381  +** ORDER BY clause.
         4382  +*/
         4383  +static int indexMightHelpWithOrderBy(
         4384  +  WhereLoopBuilder *pBuilder,
         4385  +  Index *pIndex,
         4386  +  int iCursor
         4387  +){
         4388  +  ExprList *pOB;
         4389  +  int ii, jj;
         4390  +
         4391  +  if( pIndex->bUnordered ) return 0;
         4392  +  if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
         4393  +  for(ii=0; ii<pOB->nExpr; ii++){
         4394  +    Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
         4395  +    if( pExpr->op!=TK_COLUMN ) return 0;
         4396  +    if( pExpr->iTable==iCursor ){
         4397  +      for(jj=0; jj<pIndex->nColumn; jj++){
         4398  +        if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
         4399  +      }
         4400  +    }
         4401  +  }
         4402  +  return 0;
         4403  +}
         4404  +
         4405  +/*
         4406  +** Return a bitmask where 1s indicate that the corresponding column of
         4407  +** the table is used by an index.  Only the first 63 columns are considered.
         4408  +*/
         4409  +static Bitmask columnsInIndex(Index *pIdx){
         4410  +  Bitmask m = 0;
         4411  +  int j;
         4412  +  for(j=pIdx->nColumn-1; j>=0; j--){
         4413  +    int x = pIdx->aiColumn[j];
         4414  +    testcase( x==BMS-1 );
         4415  +    testcase( x==BMS-2 );
         4416  +    if( x<BMS-1 ) m |= MASKBIT(x);
         4417  +  }
         4418  +  return m;
         4419  +}
         4420  +
         4421  +
         4422  +/*
         4423  +** Add all WhereLoop objects a single table of the join were the table
         4424  +** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
         4425  +** a b-tree table, not a virtual table.
         4426  +*/
         4427  +static int whereLoopAddBtree(
         4428  +  WhereLoopBuilder *pBuilder, /* WHERE clause information */
         4429  +  Bitmask mExtra              /* Extra prerequesites for using this table */
         4430  +){
         4431  +  WhereInfo *pWInfo;          /* WHERE analysis context */
         4432  +  Index *pProbe;              /* An index we are evaluating */
         4433  +  Index sPk;                  /* A fake index object for the primary key */
         4434  +  tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
         4435  +  int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
         4436  +  SrcList *pTabList;          /* The FROM clause */
         4437  +  struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
         4438  +  WhereLoop *pNew;            /* Template WhereLoop object */
         4439  +  int rc = SQLITE_OK;         /* Return code */
         4440  +  int iSortIdx = 1;           /* Index number */
         4441  +  int b;                      /* A boolean value */
         4442  +  WhereCost rSize;            /* number of rows in the table */
         4443  +  WhereCost rLogSize;         /* Logarithm of the number of rows in the table */
         4444  +  
         4445  +  pNew = pBuilder->pNew;
         4446  +  pWInfo = pBuilder->pWInfo;
         4447  +  pTabList = pWInfo->pTabList;
         4448  +  pSrc = pTabList->a + pNew->iTab;
         4449  +  assert( !IsVirtual(pSrc->pTab) );
         4450  +
         4451  +  if( pSrc->pIndex ){
         4452  +    /* An INDEXED BY clause specifies a particular index to use */
         4453  +    pProbe = pSrc->pIndex;
         4454  +  }else{
         4455  +    /* There is no INDEXED BY clause.  Create a fake Index object in local
         4456  +    ** variable sPk to represent the rowid primary key index.  Make this
         4457  +    ** fake index the first in a chain of Index objects with all of the real
         4458  +    ** indices to follow */
         4459  +    Index *pFirst;                  /* First of real indices on the table */
         4460  +    memset(&sPk, 0, sizeof(Index));
         4461  +    sPk.nColumn = 1;
         4462  +    sPk.aiColumn = &aiColumnPk;
         4463  +    sPk.aiRowEst = aiRowEstPk;
         4464  +    sPk.onError = OE_Replace;
         4465  +    sPk.pTable = pSrc->pTab;
         4466  +    aiRowEstPk[0] = pSrc->pTab->nRowEst;
         4467  +    aiRowEstPk[1] = 1;
         4468  +    pFirst = pSrc->pTab->pIndex;
         4469  +    if( pSrc->notIndexed==0 ){
         4470  +      /* The real indices of the table are only considered if the
         4471  +      ** NOT INDEXED qualifier is omitted from the FROM clause */
         4472  +      sPk.pNext = pFirst;
         4473  +    }
         4474  +    pProbe = &sPk;
         4475  +  }
         4476  +  rSize = whereCost(pSrc->pTab->nRowEst);
         4477  +  rLogSize = estLog(rSize);
         4478  +
         4479  +  /* Automatic indexes */
         4480  +  if( !pBuilder->pBest
         4481  +   && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
         4482  +   && pSrc->pIndex==0
         4483  +   && !pSrc->viaCoroutine
         4484  +   && !pSrc->notIndexed
         4485  +   && !pSrc->isCorrelated
         4486  +  ){
         4487  +    /* Generate auto-index WhereLoops */
         4488  +    WhereClause *pWC = pBuilder->pWC;
         4489  +    WhereTerm *pTerm;
         4490  +    WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
         4491  +    for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
         4492  +      if( pTerm->prereqRight & pNew->maskSelf ) continue;
         4493  +      if( termCanDriveIndex(pTerm, pSrc, 0) ){
         4494  +        pNew->u.btree.nEq = 1;
         4495  +        pNew->u.btree.pIndex = 0;
         4496  +        pNew->nLTerm = 1;
         4497  +        pNew->aLTerm[0] = pTerm;
         4498  +        /* TUNING: One-time cost for computing the automatic index is
         4499  +        ** approximately 6*N*log2(N) where N is the number of rows in
         4500  +        ** the table being indexed. */
         4501  +        pNew->rSetup = rLogSize + rSize + 26;  assert( 26==whereCost(6) );
         4502  +        /* TUNING: Each index lookup yields 10 rows in the table */
         4503  +        pNew->nOut = 33;  assert( 33==whereCost(10) );
         4504  +        pNew->rRun = whereCostAdd(rLogSize,pNew->nOut);
         4505  +        pNew->wsFlags = WHERE_TEMP_INDEX;
         4506  +        pNew->prereq = mExtra | pTerm->prereqRight;
         4507  +        rc = whereLoopInsert(pBuilder, pNew);
         4508  +      }
         4509  +    }
         4510  +  }
         4511  +
         4512  +  /* Loop over all indices
         4513  +  */
         4514  +  for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
         4515  +    pNew->u.btree.nEq = 0;
         4516  +    pNew->nLTerm = 0;
         4517  +    pNew->iSortIdx = 0;
         4518  +    pNew->rSetup = 0;
         4519  +    pNew->prereq = mExtra;
         4520  +    pNew->nOut = rSize;
         4521  +    pNew->u.btree.pIndex = pProbe;
         4522  +    b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
         4523  +    /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
         4524  +    assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
         4525  +    if( pProbe->tnum<=0 ){
         4526  +      /* Integer primary key index */
         4527  +      pNew->wsFlags = WHERE_IPK;
         4528  +
         4529  +      /* Full table scan */
         4530  +      pNew->iSortIdx = b ? iSortIdx : 0;
         4531  +      /* TUNING: Cost of full table scan is 3*(N + log2(N)).
         4532  +      **  +  The extra 3 factor is to encourage the use of indexed lookups
         4533  +      **     over full scans.  A smaller constant 2 is used for covering
         4534  +      **     index scans so that a covering index scan will be favored over
         4535  +      **     a table scan. */
         4536  +      pNew->rRun = whereCostAdd(rSize,rLogSize) + 16;
         4537  +      rc = whereLoopInsert(pBuilder, pNew);
         4538  +      if( rc ) break;
         4539  +    }else{
         4540  +      Bitmask m = pSrc->colUsed & ~columnsInIndex(pProbe);
         4541  +      pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
         4542  +
         4543  +      /* Full scan via index */
         4544  +      if( b
         4545  +       || ( m==0
         4546  +         && pProbe->bUnordered==0
         4547  +         && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
         4548  +         && sqlite3GlobalConfig.bUseCis
         4549  +         && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
         4550  +          )
         4551  +      ){
         4552  +        pNew->iSortIdx = b ? iSortIdx : 0;
         4553  +        if( m==0 ){
         4554  +          /* TUNING: Cost of a covering index scan is 2*(N + log2(N)).
         4555  +          **  +  The extra 2 factor is to encourage the use of indexed lookups
         4556  +          **     over index scans.  A table scan uses a factor of 3 so that
         4557  +          **     index scans are favored over table scans.
         4558  +          **  +  If this covering index might also help satisfy the ORDER BY
         4559  +          **     clause, then the cost is fudged down slightly so that this
         4560  +          **     index is favored above other indices that have no hope of
         4561  +          **     helping with the ORDER BY. */
         4562  +          pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b;
         4563  +        }else{
         4564  +          assert( b!=0 ); 
         4565  +          /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
         4566  +          ** which we will simplify to just N*log2(N) */
         4567  +          pNew->rRun = rSize + rLogSize;
         4568  +        }
         4569  +        rc = whereLoopInsert(pBuilder, pNew);
         4570  +        if( rc ) break;
         4571  +      }
         4572  +    }
         4573  +    rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
         4574  +
         4575  +    /* If there was an INDEXED BY clause, then only that one index is
         4576  +    ** considered. */
         4577  +    if( pSrc->pIndex ) break;
         4578  +  }
         4579  +  return rc;
         4580  +}
         4581  +
         4582  +#ifndef SQLITE_OMIT_VIRTUALTABLE
         4583  +/*
         4584  +** Add all WhereLoop objects for a table of the join identified by
         4585  +** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
         4586  +*/
         4587  +static int whereLoopAddVirtual(
         4588  +  WhereLoopBuilder *pBuilder   /* WHERE clause information */
         4589  +){
         4590  +  WhereInfo *pWInfo;           /* WHERE analysis context */
         4591  +  Parse *pParse;               /* The parsing context */
         4592  +  WhereClause *pWC;            /* The WHERE clause */
         4593  +  struct SrcList_item *pSrc;   /* The FROM clause term to search */
         4594  +  Table *pTab;
         4595  +  sqlite3 *db;
         4596  +  sqlite3_index_info *pIdxInfo;
         4597  +  struct sqlite3_index_constraint *pIdxCons;
         4598  +  struct sqlite3_index_constraint_usage *pUsage;
         4599  +  WhereTerm *pTerm;
         4600  +  int i, j;
         4601  +  int iTerm, mxTerm;
         4602  +  int nConstraint;
         4603  +  int seenIn = 0;              /* True if an IN operator is seen */
         4604  +  int seenVar = 0;             /* True if a non-constant constraint is seen */
         4605  +  int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
         4606  +  WhereLoop *pNew;
         4607  +  int rc = SQLITE_OK;
         4608  +
         4609  +  pWInfo = pBuilder->pWInfo;
         4610  +  pParse = pWInfo->pParse;
         4611  +  db = pParse->db;
         4612  +  pWC = pBuilder->pWC;
         4613  +  pNew = pBuilder->pNew;
         4614  +  pSrc = &pWInfo->pTabList->a[pNew->iTab];
         4615  +  pTab = pSrc->pTab;
         4616  +  assert( IsVirtual(pTab) );
         4617  +  pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
         4618  +  if( pIdxInfo==0 ) return SQLITE_NOMEM;
         4619  +  pNew->prereq = 0;
         4620  +  pNew->rSetup = 0;
         4621  +  pNew->wsFlags = WHERE_VIRTUALTABLE;
         4622  +  pNew->nLTerm = 0;
         4623  +  pNew->u.vtab.needFree = 0;
         4624  +  pUsage = pIdxInfo->aConstraintUsage;
         4625  +  nConstraint = pIdxInfo->nConstraint;
         4626  +  if( whereLoopResize(db, pNew, nConstraint) ){
         4627  +    sqlite3DbFree(db, pIdxInfo);
         4628  +    return SQLITE_NOMEM;
         4629  +  }
         4630  +
         4631  +  for(iPhase=0; iPhase<=3; iPhase++){
         4632  +    if( !seenIn && (iPhase&1)!=0 ){
         4633  +      iPhase++;
         4634  +      if( iPhase>3 ) break;
         4635  +    }
         4636  +    if( !seenVar && iPhase>1 ) break;
         4637  +    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
         4638  +    for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
         4639  +      j = pIdxCons->iTermOffset;
         4640  +      pTerm = &pWC->a[j];
         4641  +      switch( iPhase ){
         4642  +        case 0:    /* Constants without IN operator */
         4643  +          pIdxCons->usable = 0;
         4644  +          if( (pTerm->eOperator & WO_IN)!=0 ){
         4645  +            seenIn = 1;
         4646  +          }
         4647  +          if( pTerm->prereqRight!=0 ){
         4648  +            seenVar = 1;
         4649  +          }else if( (pTerm->eOperator & WO_IN)==0 ){
         4650  +            pIdxCons->usable = 1;
         4651  +          }
         4652  +          break;
         4653  +        case 1:    /* Constants with IN operators */
         4654  +          assert( seenIn );
         4655  +          pIdxCons->usable = (pTerm->prereqRight==0);
         4656  +          break;
         4657  +        case 2:    /* Variables without IN */
         4658  +          assert( seenVar );
         4659  +          pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
         4660  +          break;
         4661  +        default:   /* Variables with IN */
         4662  +          assert( seenVar && seenIn );
         4663  +          pIdxCons->usable = 1;
         4664  +          break;
         4665  +      }
         4666  +    }
         4667  +    memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
         4668  +    if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
         4669  +    pIdxInfo->idxStr = 0;
         4670  +    pIdxInfo->idxNum = 0;
         4671  +    pIdxInfo->needToFreeIdxStr = 0;
         4672  +    pIdxInfo->orderByConsumed = 0;
         4673  +    pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
         4674  +    rc = vtabBestIndex(pParse, pTab, pIdxInfo);
         4675  +    if( rc ) goto whereLoopAddVtab_exit;
         4676  +    pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
         4677  +    pNew->prereq = 0;
         4678  +    mxTerm = -1;
         4679  +    assert( pNew->nLSlot>=nConstraint );
         4680  +    for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
         4681  +    pNew->u.vtab.omitMask = 0;
         4682  +    for(i=0; i<nConstraint; i++, pIdxCons++){
         4683  +      if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
         4684  +        j = pIdxCons->iTermOffset;
         4685  +        if( iTerm>=nConstraint
         4686  +         || j<0
         4687  +         || j>=pWC->nTerm
         4688  +         || pNew->aLTerm[iTerm]!=0
         4689  +        ){
         4690  +          rc = SQLITE_ERROR;
         4691  +          sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
         4692  +          goto whereLoopAddVtab_exit;
         4693  +        }
         4694  +        testcase( iTerm==nConstraint-1 );
         4695  +        testcase( j==0 );
         4696  +        testcase( j==pWC->nTerm-1 );
         4697  +        pTerm = &pWC->a[j];
         4698  +        pNew->prereq |= pTerm->prereqRight;
         4699  +        assert( iTerm<pNew->nLSlot );
         4700  +        pNew->aLTerm[iTerm] = pTerm;
         4701  +        if( iTerm>mxTerm ) mxTerm = iTerm;
         4702  +        testcase( iTerm==15 );
         4703  +        testcase( iTerm==16 );
         4704  +        if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
         4705  +        if( (pTerm->eOperator & WO_IN)!=0 ){
         4706  +          if( pUsage[i].omit==0 ){
         4707  +            /* Do not attempt to use an IN constraint if the virtual table
         4708  +            ** says that the equivalent EQ constraint cannot be safely omitted.
         4709  +            ** If we do attempt to use such a constraint, some rows might be
         4710  +            ** repeated in the output. */
         4711  +            break;
         4712  +          }
         4713  +          /* A virtual table that is constrained by an IN clause may not
         4714  +          ** consume the ORDER BY clause because (1) the order of IN terms
         4715  +          ** is not necessarily related to the order of output terms and
         4716  +          ** (2) Multiple outputs from a single IN value will not merge
         4717  +          ** together.  */
         4718  +          pIdxInfo->orderByConsumed = 0;
         4719  +        }
         4720  +      }
         4721  +    }
         4722  +    if( i>=nConstraint ){
         4723  +      pNew->nLTerm = mxTerm+1;
         4724  +      assert( pNew->nLTerm<=pNew->nLSlot );
         4725  +      pNew->u.vtab.idxNum = pIdxInfo->idxNum;
         4726  +      pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
         4727  +      pIdxInfo->needToFreeIdxStr = 0;
         4728  +      pNew->u.vtab.idxStr = pIdxInfo->idxStr;
         4729  +      pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
         4730  +                                     && pIdxInfo->orderByConsumed);
         4731  +      pNew->rSetup = 0;
         4732  +      pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost);
         4733  +      /* TUNING: Every virtual table query returns 25 rows */
         4734  +      pNew->nOut = 46;  assert( 46==whereCost(25) );
         4735  +      whereLoopInsert(pBuilder, pNew);
         4736  +      if( pNew->u.vtab.needFree ){
         4737  +        sqlite3_free(pNew->u.vtab.idxStr);
         4738  +        pNew->u.vtab.needFree = 0;
         4739  +      }
         4740  +    }
         4741  +  }  
         4742  +
         4743  +whereLoopAddVtab_exit:
         4744  +  if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
         4745  +  sqlite3DbFree(db, pIdxInfo);
         4746  +  return rc;
         4747  +}
         4748  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
         4749  +
         4750  +/*
         4751  +** Add WhereLoop entries to handle OR terms.  This works for either
         4752  +** btrees or virtual tables.
         4753  +*/
         4754  +static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
         4755  +  WhereInfo *pWInfo = pBuilder->pWInfo;
         4756  +  WhereClause *pWC;
         4757  +  WhereLoop *pNew;
         4758  +  WhereTerm *pTerm, *pWCEnd;
         4759  +  int rc = SQLITE_OK;
         4760  +  int iCur;
         4761  +  WhereClause tempWC;
         4762  +  WhereLoopBuilder sSubBuild;
         4763  +  WhereLoop sBest;
         4764  +  struct SrcList_item *pItem;
         4765  +  
         4766  +  pWC = pBuilder->pWC;
         4767  +  if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
         4768  +  pWCEnd = pWC->a + pWC->nTerm;
         4769  +  pNew = pBuilder->pNew;
         4770  +
         4771  +  for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
         4772  +    if( (pTerm->eOperator & WO_OR)!=0
         4773  +     && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 
         4774  +    ){
         4775  +      WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
         4776  +      WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
         4777  +      WhereTerm *pOrTerm;
         4778  +      WhereCost rTotal = 0;
         4779  +      WhereCost nRow = 0;
         4780  +      Bitmask prereq = mExtra;
         4781  +    
         4782  +      whereLoopInit(&sBest);
         4783  +      pItem = pWInfo->pTabList->a + pNew->iTab;
         4784  +      iCur = pItem->iCursor;
         4785  +      sSubBuild = *pBuilder;
         4786  +      sSubBuild.pOrderBy = 0;
         4787  +      sSubBuild.pBest = &sBest;
         4788  +
         4789  +      for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
         4790  +        if( (pOrTerm->eOperator & WO_AND)!=0 ){
         4791  +          sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
         4792  +        }else if( pOrTerm->leftCursor==iCur ){
         4793  +          tempWC.pWInfo = pWC->pWInfo;
         4794  +          tempWC.pOuter = pWC;
         4795  +          tempWC.op = TK_AND;
         4796  +          tempWC.nTerm = 1;
         4797  +          tempWC.a = pOrTerm;
         4798  +          sSubBuild.pWC = &tempWC;
         4799  +        }else{
         4800  +          continue;
         4801  +        }
         4802  +        sBest.maskSelf = 0;
         4803  +        sBest.rSetup = 0;
         4804  +        sBest.rRun = 0;
         4805  +#ifndef SQLITE_OMIT_VIRTUALTABLE
         4806  +        if( IsVirtual(pItem->pTab) ){
         4807  +          rc = whereLoopAddVirtual(&sSubBuild);
         4808  +        }else
         4809  +#endif
         4810  +        {
         4811  +          rc = whereLoopAddBtree(&sSubBuild, mExtra);
         4812  +        }
         4813  +        /* sBest.maskSelf is always zero if an error occurs */
         4814  +        assert( rc==SQLITE_OK || sBest.maskSelf==0 );
         4815  +        if( sBest.maskSelf==0 ) break;
         4816  +        assert( sBest.rSetup==0 );
         4817  +        rTotal = whereCostAdd(rTotal, sBest.rRun);
         4818  +        nRow = whereCostAdd(nRow, sBest.nOut);
         4819  +        prereq |= sBest.prereq;
         4820  +      }
         4821  +      assert( pNew->nLSlot>=1 );
         4822  +      if( sBest.maskSelf ){
         4823  +        pNew->nLTerm = 1;
         4824  +        pNew->aLTerm[0] = pTerm;
         4825  +        pNew->wsFlags = WHERE_MULTI_OR;
         4826  +        pNew->rSetup = 0;
         4827  +        /* TUNING: Multiple by 3.5 for the secondary table lookup */
         4828  +        pNew->rRun = rTotal + 18; assert( 18==whereCost(7)-whereCost(2) );
         4829  +        pNew->nOut = nRow;
         4830  +        pNew->prereq = prereq;
         4831  +        memset(&pNew->u, 0, sizeof(pNew->u));
         4832  +        rc = whereLoopInsert(pBuilder, pNew);
         4833  +      }
         4834  +      whereLoopClear(pWInfo->pParse->db, &sBest);
         4835  +    }
         4836  +  }
         4837  +  return rc;
         4838  +}
         4839  +
         4840  +/*
         4841  +** Add all WhereLoop objects for all tables 
         4842  +*/
         4843  +static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
         4844  +  WhereInfo *pWInfo = pBuilder->pWInfo;
         4845  +  Bitmask mExtra = 0;
         4846  +  Bitmask mPrior = 0;
         4847  +  int iTab;
         4848  +  SrcList *pTabList = pWInfo->pTabList;
         4849  +  struct SrcList_item *pItem;
         4850  +  sqlite3 *db = pWInfo->pParse->db;
         4851  +  int nTabList = pWInfo->nLevel;
         4852  +  int rc = SQLITE_OK;
         4853  +  u8 priorJoinType = 0;
         4854  +  WhereLoop *pNew;
         4855  +
         4856  +  /* Loop over the tables in the join, from left to right */
         4857  +  pNew = pBuilder->pNew;
         4858  +  whereLoopInit(pNew);
         4859  +  for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
         4860  +    pNew->iTab = iTab;
         4861  +    pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
         4862  +    if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
         4863  +      mExtra = mPrior;
         4864  +    }
         4865  +    priorJoinType = pItem->jointype;
         4866  +    if( IsVirtual(pItem->pTab) ){
         4867  +      rc = whereLoopAddVirtual(pBuilder);
         4868  +    }else{
         4869  +      rc = whereLoopAddBtree(pBuilder, mExtra);
         4870  +    }
         4871  +    if( rc==SQLITE_OK ){
         4872  +      rc = whereLoopAddOr(pBuilder, mExtra);
         4873  +    }
         4874  +    mPrior |= pNew->maskSelf;
         4875  +    if( rc || db->mallocFailed ) break;
         4876  +  }
         4877  +  whereLoopClear(db, pNew);
         4878  +  return rc;
         4879  +}
         4880  +
         4881  +/*
         4882  +** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
         4883  +** parameters) to see if it outputs rows in the requested ORDER BY
         4884  +** (or GROUP BY) without requiring a separate source operation.  Return:
         4885  +** 
         4886  +**    0:  ORDER BY is not satisfied.  Sorting required
         4887  +**    1:  ORDER BY is satisfied.      Omit sorting
         4888  +**   -1:  Unknown at this time
         4889  +**
         4890  +*/
         4891  +static int wherePathSatisfiesOrderBy(
         4892  +  WhereInfo *pWInfo,    /* The WHERE clause */
         4893  +  ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
         4894  +  WherePath *pPath,     /* The WherePath to check */
         4895  +  u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
         4896  +  u16 nLoop,            /* Number of entries in pPath->aLoop[] */
         4897  +  WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
         4898  +  Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
         4899  +){
         4900  +  u8 revSet;            /* True if rev is known */
         4901  +  u8 rev;               /* Composite sort order */
         4902  +  u8 revIdx;            /* Index sort order */
         4903  +  u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
         4904  +  u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
         4905  +  u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
         4906  +  u16 nColumn;          /* Number of columns in pIndex */
         4907  +  u16 nOrderBy;         /* Number terms in the ORDER BY clause */
         4908  +  int iLoop;            /* Index of WhereLoop in pPath being processed */
         4909  +  int i, j;             /* Loop counters */
         4910  +  int iCur;             /* Cursor number for current WhereLoop */
         4911  +  int iColumn;          /* A column number within table iCur */
         4912  +  WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
         4913  +  WhereTerm *pTerm;     /* A single term of the WHERE clause */
         4914  +  Expr *pOBExpr;        /* An expression from the ORDER BY clause */
         4915  +  CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
         4916  +  Index *pIndex;        /* The index associated with pLoop */
         4917  +  sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
         4918  +  Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
         4919  +  Bitmask obDone;       /* Mask of all ORDER BY terms */
         4920  +  Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
         4921  +  Bitmask ready;              /* Mask of inner loops */
         4922  +
         4923  +  /*
         4924  +  ** We say the WhereLoop is "one-row" if it generates no more than one
         4925  +  ** row of output.  A WhereLoop is one-row if all of the following are true:
         4926  +  **  (a) All index columns match with WHERE_COLUMN_EQ.
         4927  +  **  (b) The index is unique
         4928  +  ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
         4929  +  ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
         4930  +  **
         4931  +  ** We say the WhereLoop is "order-distinct" if the set of columns from
         4932  +  ** that WhereLoop that are in the ORDER BY clause are different for every
         4933  +  ** row of the WhereLoop.  Every one-row WhereLoop is automatically
         4934  +  ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
         4935  +  ** is not order-distinct. To be order-distinct is not quite the same as being
         4936  +  ** UNIQUE since a UNIQUE column or index can have multiple rows that 
         4937  +  ** are NULL and NULL values are equivalent for the purpose of order-distinct.
         4938  +  ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
         4939  +  **
         4940  +  ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
         4941  +  ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
         4942  +  ** automatically order-distinct.
         4943  +  */
         4944  +
         4945  +  assert( pOrderBy!=0 );
         4946  +
         4947  +  /* Sortability of virtual tables is determined by the xBestIndex method
         4948  +  ** of the virtual table itself */
         4949  +  if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
         4950  +    testcase( nLoop>0 );  /* True when outer loops are one-row and match 
         4951  +                          ** no ORDER BY terms */
         4952  +    return pLast->u.vtab.isOrdered;
         4953  +  }
         4954  +  if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
         4955  +
         4956  +  nOrderBy = pOrderBy->nExpr;
         4957  +  testcase( nOrderBy==BMS-1 );
         4958  +  if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
         4959  +  isOrderDistinct = 1;
         4960  +  obDone = MASKBIT(nOrderBy)-1;
         4961  +  orderDistinctMask = 0;
         4962  +  ready = 0;
         4963  +  for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
         4964  +    if( iLoop>0 ) ready |= pLoop->maskSelf;
         4965  +    pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
         4966  +    assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
         4967  +    iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
         4968  +
         4969  +    /* Mark off any ORDER BY term X that is a column in the table of
         4970  +    ** the current loop for which there is term in the WHERE
         4971  +    ** clause of the form X IS NULL or X=? that reference only outer
         4972  +    ** loops.
         4973  +    */
         4974  +    for(i=0; i<nOrderBy; i++){
         4975  +      if( MASKBIT(i) & obSat ) continue;
         4976  +      pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
         4977  +      if( pOBExpr->op!=TK_COLUMN ) continue;
         4978  +      if( pOBExpr->iTable!=iCur ) continue;
         4979  +      pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
         4980  +                       ~ready, WO_EQ|WO_ISNULL, 0);
         4981  +      if( pTerm==0 ) continue;
         4982  +      if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
         4983  +        const char *z1, *z2;
         4984  +        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
         4985  +        if( !pColl ) pColl = db->pDfltColl;
         4986  +        z1 = pColl->zName;
         4987  +        pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
         4988  +        if( !pColl ) pColl = db->pDfltColl;
         4989  +        z2 = pColl->zName;
         4990  +        if( sqlite3StrICmp(z1, z2)!=0 ) continue;
         4991  +      }
         4992  +      obSat |= MASKBIT(i);
         4993  +    }
         4994  +
         4995  +    if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
         4996  +      if( pLoop->wsFlags & WHERE_IPK ){
         4997  +        pIndex = 0;
         4998  +        nColumn = 0;
         4999  +      }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
         5000  +        return 0;
         5001  +      }else{
         5002  +        nColumn = pIndex->nColumn;
         5003  +        isOrderDistinct = pIndex->onError!=OE_None;
         5004  +      }
         5005  +
         5006  +      /* Loop through all columns of the index and deal with the ones
         5007  +      ** that are not constrained by == or IN.
         5008  +      */
         5009  +      rev = revSet = 0;
         5010  +      distinctColumns = 0;
         5011  +      for(j=0; j<=nColumn; j++){
         5012  +        u8 bOnce;   /* True to run the ORDER BY search loop */
         5013  +
         5014  +        /* Skip over == and IS NULL terms */
         5015  +        if( j<pLoop->u.btree.nEq
         5016  +         && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
         5017  +        ){
         5018  +          if( i & WO_ISNULL ){
         5019  +            testcase( isOrderDistinct );
         5020  +            isOrderDistinct = 0;
         5021  +          }
         5022  +          continue;  
         5023  +        }
         5024  +
         5025  +        /* Get the column number in the table (iColumn) and sort order
         5026  +        ** (revIdx) for the j-th column of the index.
         5027  +        */
         5028  +        if( j<nColumn ){
         5029  +          /* Normal index columns */
         5030  +          iColumn = pIndex->aiColumn[j];
         5031  +          revIdx = pIndex->aSortOrder[j];
         5032  +          if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
         5033  +        }else{
         5034  +          /* The ROWID column at the end */
         5035  +          assert( j==nColumn );
         5036  +          iColumn = -1;
         5037  +          revIdx = 0;
         5038  +        }
         5039  +
         5040  +        /* An unconstrained column that might be NULL means that this
         5041  +        ** WhereLoop is not well-ordered 
         5042  +        */
         5043  +        if( isOrderDistinct
         5044  +         && iColumn>=0
         5045  +         && j>=pLoop->u.btree.nEq
         5046  +         && pIndex->pTable->aCol[iColumn].notNull==0
         5047  +        ){
         5048  +          isOrderDistinct = 0;
         5049  +        }
         5050  +
         5051  +        /* Find the ORDER BY term that corresponds to the j-th column
         5052  +        ** of the index and and mark that ORDER BY term off 
         5053  +        */
         5054  +        bOnce = 1;
         5055  +        isMatch = 0;
         5056  +        for(i=0; bOnce && i<nOrderBy; i++){
         5057  +          if( MASKBIT(i) & obSat ) continue;
         5058  +          pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
         5059  +          testcase( wctrlFlags & WHERE_GROUPBY );
         5060  +          testcase( wctrlFlags & WHERE_DISTINCTBY );
         5061  +          if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
         5062  +          if( pOBExpr->op!=TK_COLUMN ) continue;
         5063  +          if( pOBExpr->iTable!=iCur ) continue;
         5064  +          if( pOBExpr->iColumn!=iColumn ) continue;
         5065  +          if( iColumn>=0 ){
         5066  +            pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
         5067  +            if( !pColl ) pColl = db->pDfltColl;
         5068  +            if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
         5069  +          }
         5070  +          isMatch = 1;
         5071  +          break;
         5072  +        }
         5073  +        if( isMatch ){
         5074  +          if( iColumn<0 ){
         5075  +            testcase( distinctColumns==0 );
         5076  +            distinctColumns = 1;
         5077  +          }
         5078  +          obSat |= MASKBIT(i);
         5079  +          if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
         5080  +            /* Make sure the sort order is compatible in an ORDER BY clause.
         5081  +            ** Sort order is irrelevant for a GROUP BY clause. */
         5082  +            if( revSet ){
         5083  +              if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0;
         5084  +            }else{
         5085  +              rev = revIdx ^ pOrderBy->a[i].sortOrder;
         5086  +              if( rev ) *pRevMask |= MASKBIT(iLoop);
         5087  +              revSet = 1;
         5088  +            }
         5089  +          }
         5090  +        }else{
         5091  +          /* No match found */
         5092  +          if( j==0 || j<nColumn ){
         5093  +            testcase( isOrderDistinct!=0 );
         5094  +            isOrderDistinct = 0;
         5095  +          }
         5096  +          break;
         5097  +        }
         5098  +      } /* end Loop over all index columns */
         5099  +      if( distinctColumns ){
         5100  +        testcase( isOrderDistinct==0 );
         5101  +        isOrderDistinct = 1;
         5102  +      }
         5103  +    } /* end-if not one-row */
         5104  +
         5105  +    /* Mark off any other ORDER BY terms that reference pLoop */
         5106  +    if( isOrderDistinct ){
         5107  +      orderDistinctMask |= pLoop->maskSelf;
         5108  +      for(i=0; i<nOrderBy; i++){
         5109  +        Expr *p;
         5110  +        if( MASKBIT(i) & obSat ) continue;
         5111  +        p = pOrderBy->a[i].pExpr;
         5112  +        if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){
         5113  +          obSat |= MASKBIT(i);
         5114  +        }
         5115  +      }
         5116  +    }
         5117  +  } /* End the loop over all WhereLoops from outer-most down to inner-most */
         5118  +  if( obSat==obDone ) return 1;
         5119  +  if( !isOrderDistinct ) return 0;
         5120  +  return -1;
         5121  +}
         5122  +
         5123  +#ifdef WHERETRACE_ENABLED
         5124  +/* For debugging use only: */
         5125  +static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
         5126  +  static char zName[65];
         5127  +  int i;
         5128  +  for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
         5129  +  if( pLast ) zName[i++] = pLast->cId;
         5130  +  zName[i] = 0;
         5131  +  return zName;
         5132  +}
         5133  +#endif
         5134  +
         5135  +
         5136  +/*
         5137  +** Given the list of WhereLoop objects on pWInfo->pLoops, this routine
         5138  +** attempts to find the lowest cost path that visits each WhereLoop
         5139  +** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
         5140  +**
         5141  +** Assume that the total number of output rows that will need to be sorted
         5142  +** will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
         5143  +** costs if nRowEst==0.
         5144  +**
         5145  +** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
         5146  +** error occurs.
         5147  +*/
         5148  +static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){
         5149  +  int mxChoice;             /* Maximum number of simultaneous paths tracked */
         5150  +  int nLoop;                /* Number of terms in the join */
         5151  +  Parse *pParse;            /* Parsing context */
         5152  +  sqlite3 *db;              /* The database connection */
         5153  +  int iLoop;                /* Loop counter over the terms of the join */
         5154  +  int ii, jj;               /* Loop counters */
         5155  +  WhereCost rCost;             /* Cost of a path */
         5156  +  WhereCost mxCost = 0;        /* Maximum cost of a set of paths */
         5157  +  WhereCost rSortCost;         /* Cost to do a sort */
         5158  +  int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
         5159  +  WherePath *aFrom;         /* All nFrom paths at the previous level */
         5160  +  WherePath *aTo;           /* The nTo best paths at the current level */
         5161  +  WherePath *pFrom;         /* An element of aFrom[] that we are working on */
         5162  +  WherePath *pTo;           /* An element of aTo[] that we are working on */
         5163  +  WhereLoop *pWLoop;        /* One of the WhereLoop objects */
         5164  +  WhereLoop **pX;           /* Used to divy up the pSpace memory */
         5165  +  char *pSpace;             /* Temporary memory used by this routine */
         5166  +
         5167  +  pParse = pWInfo->pParse;
         5168  +  db = pParse->db;
         5169  +  nLoop = pWInfo->nLevel;
         5170  +  /* TUNING: For simple queries, only the best path is tracked.
         5171  +  ** For 2-way joins, the 5 best paths are followed.
         5172  +  ** For joins of 3 or more tables, track the 10 best paths */
         5173  +  mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10);
         5174  +  assert( nLoop<=pWInfo->pTabList->nSrc );
         5175  +  WHERETRACE(0x002, ("---- begin solver\n"));
         5176  +
         5177  +  /* Allocate and initialize space for aTo and aFrom */
         5178  +  ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
         5179  +  pSpace = sqlite3DbMallocRaw(db, ii);
         5180  +  if( pSpace==0 ) return SQLITE_NOMEM;
         5181  +  aTo = (WherePath*)pSpace;
         5182  +  aFrom = aTo+mxChoice;
         5183  +  memset(aFrom, 0, sizeof(aFrom[0]));
         5184  +  pX = (WhereLoop**)(aFrom+mxChoice);
         5185  +  for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
         5186  +    pFrom->aLoop = pX;
         5187  +  }
         5188  +
         5189  +  /* Seed the search with a single WherePath containing zero WhereLoops.
         5190  +  **
         5191  +  ** TUNING: Do not let the number of iterations go above 25.  If the cost
         5192  +  ** of computing an automatic index is not paid back within the first 25
         5193  +  ** rows, then do not use the automatic index. */
         5194  +  aFrom[0].nRow = MIN(pParse->nQueryLoop, 46);  assert( 46==whereCost(25) );
         5195  +  nFrom = 1;
         5196  +
         5197  +  /* Precompute the cost of sorting the final result set, if the caller
         5198  +  ** to sqlite3WhereBegin() was concerned about sorting */
         5199  +  rSortCost = 0;
         5200  +  if( pWInfo->pOrderBy==0 || nRowEst==0 ){
         5201  +    aFrom[0].isOrderedValid = 1;
         5202  +  }else{
         5203  +    /* TUNING: Estimated cost of sorting is N*log2(N) where N is the
         5204  +    ** number of output rows. */
         5205  +    rSortCost = nRowEst + estLog(nRowEst);
         5206  +    WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
         5207  +  }
         5208  +
         5209  +  /* Compute successively longer WherePaths using the previous generation
         5210  +  ** of WherePaths as the basis for the next.  Keep track of the mxChoice
         5211  +  ** best paths at each generation */
         5212  +  for(iLoop=0; iLoop<nLoop; iLoop++){
         5213  +    nTo = 0;
         5214  +    for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
         5215  +      for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
         5216  +        Bitmask maskNew;
         5217  +        Bitmask revMask = 0;
         5218  +        u8 isOrderedValid = pFrom->isOrderedValid;
         5219  +        u8 isOrdered = pFrom->isOrdered;
         5220  +        if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
         5221  +        if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
         5222  +        /* At this point, pWLoop is a candidate to be the next loop. 
         5223  +        ** Compute its cost */
         5224  +        rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
         5225  +        rCost = whereCostAdd(rCost, pFrom->rCost);
         5226  +        maskNew = pFrom->maskLoop | pWLoop->maskSelf;
         5227  +        if( !isOrderedValid ){
         5228  +          switch( wherePathSatisfiesOrderBy(pWInfo,
         5229  +                       pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
         5230  +                       iLoop, pWLoop, &revMask) ){
         5231  +            case 1:  /* Yes.  pFrom+pWLoop does satisfy the ORDER BY clause */
         5232  +              isOrdered = 1;
         5233  +              isOrderedValid = 1;
         5234  +              break;
         5235  +            case 0:  /* No.  pFrom+pWLoop will require a separate sort */
         5236  +              isOrdered = 0;
         5237  +              isOrderedValid = 1;
         5238  +              rCost = whereCostAdd(rCost, rSortCost);
         5239  +              break;
         5240  +            default: /* Cannot tell yet.  Try again on the next iteration */
         5241  +              break;
         5242  +          }
         5243  +        }else{
         5244  +          revMask = pFrom->revLoop;
         5245  +        }
         5246  +        /* Check to see if pWLoop should be added to the mxChoice best so far */
         5247  +        for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
         5248  +          if( pTo->maskLoop==maskNew && pTo->isOrderedValid==isOrderedValid ){
         5249  +            testcase( jj==nTo-1 );
         5250  +            break;
         5251  +          }
         5252  +        }
         5253  +        if( jj>=nTo ){
         5254  +          if( nTo>=mxChoice && rCost>=mxCost ){
         5255  +#ifdef WHERETRACE_ENABLED
         5256  +            if( sqlite3WhereTrace&0x4 ){
         5257  +              sqlite3DebugPrintf("Skip   %s cost=%3d order=%c\n",
         5258  +                  wherePathName(pFrom, iLoop, pWLoop), rCost,
         5259  +                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
         5260  +            }
         5261  +#endif
         5262  +            continue;
         5263  +          }
         5264  +          /* Add a new Path to the aTo[] set */
         5265  +          if( nTo<mxChoice ){
         5266  +            /* Increase the size of the aTo set by one */
         5267  +            jj = nTo++;
         5268  +          }else{
         5269  +            /* New path replaces the prior worst to keep count below mxChoice */
         5270  +            for(jj=nTo-1; aTo[jj].rCost<mxCost; jj--){ assert(jj>0); }
         5271  +          }
         5272  +          pTo = &aTo[jj];
         5273  +#ifdef WHERETRACE_ENABLED
         5274  +          if( sqlite3WhereTrace&0x4 ){
         5275  +            sqlite3DebugPrintf("New    %s cost=%-3d order=%c\n",
         5276  +                wherePathName(pFrom, iLoop, pWLoop), rCost,
         5277  +                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
         5278  +          }
         5279  +#endif
         5280  +        }else{
         5281  +          if( pTo->rCost<=rCost ){
         5282  +#ifdef WHERETRACE_ENABLED
         5283  +            if( sqlite3WhereTrace&0x4 ){
         5284  +              sqlite3DebugPrintf(
         5285  +                  "Skip   %s cost=%-3d order=%c",
         5286  +                  wherePathName(pFrom, iLoop, pWLoop), rCost,
         5287  +                  isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
         5288  +              sqlite3DebugPrintf("   vs %s cost=%-3d order=%c\n",
         5289  +                  wherePathName(pTo, iLoop+1, 0), pTo->rCost,
         5290  +                  pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
         5291  +            }
         5292  +#endif
         5293  +            testcase( pTo->rCost==rCost );
         5294  +            continue;
         5295  +          }
         5296  +          testcase( pTo->rCost==rCost+1 );
         5297  +          /* A new and better score for a previously created equivalent path */
         5298  +#ifdef WHERETRACE_ENABLED
         5299  +          if( sqlite3WhereTrace&0x4 ){
         5300  +            sqlite3DebugPrintf(
         5301  +                "Update %s cost=%-3d order=%c",
         5302  +                wherePathName(pFrom, iLoop, pWLoop), rCost,
         5303  +                isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
         5304  +            sqlite3DebugPrintf("  was %s cost=%-3d order=%c\n",
         5305  +                wherePathName(pTo, iLoop+1, 0), pTo->rCost,
         5306  +                pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
         5307  +          }
         5308  +#endif
         5309  +        }
         5310  +        /* pWLoop is a winner.  Add it to the set of best so far */
         5311  +        pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
         5312  +        pTo->revLoop = revMask;
         5313  +        pTo->nRow = pFrom->nRow + pWLoop->nOut;
         5314  +        pTo->rCost = rCost;
         5315  +        pTo->isOrderedValid = isOrderedValid;
         5316  +        pTo->isOrdered = isOrdered;
         5317  +        memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
         5318  +        pTo->aLoop[iLoop] = pWLoop;
         5319  +        if( nTo>=mxChoice ){
         5320  +          mxCost = aTo[0].rCost;
         5321  +          for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
         5322  +            if( pTo->rCost>mxCost ) mxCost = pTo->rCost;
         5323  +          }
         5324  +        }
         5325  +      }
         5326  +    }
         5327  +
         5328  +#ifdef WHERETRACE_ENABLED
         5329  +    if( sqlite3WhereTrace>=2 ){
         5330  +      sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
         5331  +      for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
         5332  +        sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
         5333  +           wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
         5334  +           pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
         5335  +        if( pTo->isOrderedValid && pTo->isOrdered ){
         5336  +          sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
         5337  +        }else{
         5338  +          sqlite3DebugPrintf("\n");
         5339  +        }
         5340  +      }
         5341  +    }
         5342  +#endif
         5343  +
         5344  +    /* Swap the roles of aFrom and aTo for the next generation */
         5345  +    pFrom = aTo;
         5346  +    aTo = aFrom;
         5347  +    aFrom = pFrom;
         5348  +    nFrom = nTo;
         5349  +  }
         5350  +
         5351  +  if( nFrom==0 ){
         5352  +    sqlite3ErrorMsg(pParse, "no query solution");
         5353  +    sqlite3DbFree(db, pSpace);
         5354  +    return SQLITE_ERROR;
         5355  +  }
         5356  +  
         5357  +  /* Find the lowest cost path.  pFrom will be left pointing to that path */
         5358  +  pFrom = aFrom;
         5359  +  assert( nFrom==1 );
         5360  +#if 0 /* The following is needed if nFrom is ever more than 1 */
         5361  +  for(ii=1; ii<nFrom; ii++){
         5362  +    if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
         5363  +  }
         5364  +#endif
         5365  +  assert( pWInfo->nLevel==nLoop );
         5366  +  /* Load the lowest cost path into pWInfo */
         5367  +  for(iLoop=0; iLoop<nLoop; iLoop++){
         5368  +    WhereLevel *pLevel = pWInfo->a + iLoop;
         5369  +    pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
         5370  +    pLevel->iFrom = pWLoop->iTab;
         5371  +    pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
         5372  +  }
         5373  +  if( (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0 
         5374  +   && pWInfo->pDistinct
         5375  +   && nRowEst
         5376  +  ){
         5377  +    Bitmask notUsed;
         5378  +    int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pDistinct, pFrom,
         5379  +                 WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
         5380  +    if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
         5381  +  }
         5382  +  if( pFrom->isOrdered ){
         5383  +    if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
         5384  +      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
         5385  +    }else{
         5386  +      pWInfo->bOBSat = 1;
         5387  +      pWInfo->revMask = pFrom->revLoop;
         5388  +    }
         5389  +  }
         5390  +  pWInfo->nRowOut = pFrom->nRow;
         5391  +
         5392  +  /* Free temporary memory and return success */
         5393  +  sqlite3DbFree(db, pSpace);
         5394  +  return SQLITE_OK;
         5395  +}
         5396  +
         5397  +/*
         5398  +** Most queries use only a single table (they are not joins) and have
         5399  +** simple == constraints against indexed fields.  This routine attempts
         5400  +** to plan those simple cases using much less ceremony than the
         5401  +** general-purpose query planner, and thereby yield faster sqlite3_prepare()
         5402  +** times for the common case.
         5403  +**
         5404  +** Return non-zero on success, if this query can be handled by this
         5405  +** no-frills query planner.  Return zero if this query needs the 
         5406  +** general-purpose query planner.
         5407  +*/
         5408  +static int whereShortCut(WhereLoopBuilder *pBuilder){
         5409  +  WhereInfo *pWInfo;
         5410  +  struct SrcList_item *pItem;
         5411  +  WhereClause *pWC;
         5412  +  WhereTerm *pTerm;
         5413  +  WhereLoop *pLoop;
         5414  +  int iCur;
         5415  +  int j;
         5416  +  Table *pTab;
         5417  +  Index *pIdx;
         5418  +  
         5419  +  pWInfo = pBuilder->pWInfo;
         5420  +  if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
         5421  +  assert( pWInfo->pTabList->nSrc>=1 );
         5422  +  pItem = pWInfo->pTabList->a;
         5423  +  pTab = pItem->pTab;
         5424  +  if( IsVirtual(pTab) ) return 0;
         5425  +  if( pItem->zIndex ) return 0;
         5426  +  iCur = pItem->iCursor;
         5427  +  pWC = &pWInfo->sWC;
         5428  +  pLoop = pBuilder->pNew;
         5429  +  pLoop->wsFlags = 0;
         5430  +  pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
         5431  +  if( pTerm ){
         5432  +    pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
         5433  +    pLoop->aLTerm[0] = pTerm;
         5434  +    pLoop->nLTerm = 1;
         5435  +    pLoop->u.btree.nEq = 1;
         5436  +    /* TUNING: Cost of a rowid lookup is 10 */
         5437  +    pLoop->rRun = 33;  /* 33==whereCost(10) */
         5438  +  }else{
         5439  +    for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
         5440  +      if( pIdx->onError==OE_None ) continue;
         5441  +      for(j=0; j<pIdx->nColumn; j++){
         5442  +        pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
         5443  +        if( pTerm==0 ) break;
         5444  +        whereLoopResize(pWInfo->pParse->db, pLoop, j);
         5445  +        pLoop->aLTerm[j] = pTerm;
         5446  +      }
         5447  +      if( j!=pIdx->nColumn ) continue;
         5448  +      pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
         5449  +      if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
         5450  +        pLoop->wsFlags |= WHERE_IDX_ONLY;
         5451  +      }
         5452  +      pLoop->nLTerm = j;
         5453  +      pLoop->u.btree.nEq = j;
         5454  +      pLoop->u.btree.pIndex = pIdx;
         5455  +      /* TUNING: Cost of a unique index lookup is 15 */
         5456  +      pLoop->rRun = 39;  /* 39==whereCost(15) */
         5457  +      break;
         5458  +    }
         5459  +  }
         5460  +  if( pLoop->wsFlags ){
         5461  +    pLoop->nOut = (WhereCost)1;
         5462  +    pWInfo->a[0].pWLoop = pLoop;
         5463  +    pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
         5464  +    pWInfo->a[0].iTabCur = iCur;
         5465  +    pWInfo->nRowOut = 1;
         5466  +    if( pWInfo->pOrderBy ) pWInfo->bOBSat =  1;
         5467  +    if( pWInfo->pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
         5468  +#ifdef SQLITE_DEBUG
         5469  +    pLoop->cId = '0';
         5470  +#endif
         5471  +    return 1;
         5472  +  }
         5473  +  return 0;
         5474  +}
  4964   5475   
  4965   5476   /*
  4966   5477   ** Generate the beginning of the loop used for WHERE clause processing.
  4967   5478   ** The return value is a pointer to an opaque structure that contains
  4968   5479   ** information needed to terminate the loop.  Later, the calling routine
  4969   5480   ** should invoke sqlite3WhereEnd() with the return value of this function
  4970   5481   ** in order to complete the WHERE clause processing.
................................................................................
  5036   5547   **    end
  5037   5548   **
  5038   5549   ** ORDER BY CLAUSE PROCESSING
  5039   5550   **
  5040   5551   ** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
  5041   5552   ** if there is one.  If there is no ORDER BY clause or if this routine
  5042   5553   ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
  5043         -**
  5044         -** If an index can be used so that the natural output order of the table
  5045         -** scan is correct for the ORDER BY clause, then that index is used and
  5046         -** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr.  This
  5047         -** is an optimization that prevents an unnecessary sort of the result set
  5048         -** if an index appropriate for the ORDER BY clause already exists.
  5049         -**
  5050         -** If the where clause loops cannot be arranged to provide the correct
  5051         -** output order, then WhereInfo.nOBSat is 0.
  5052   5554   */
  5053   5555   WhereInfo *sqlite3WhereBegin(
  5054   5556     Parse *pParse,        /* The parser context */
  5055   5557     SrcList *pTabList,    /* A list of all tables to be scanned */
  5056   5558     Expr *pWhere,         /* The WHERE clause */
  5057   5559     ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
  5058   5560     ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
................................................................................
  5060   5562     int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
  5061   5563   ){
  5062   5564     int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
  5063   5565     int nTabList;              /* Number of elements in pTabList */
  5064   5566     WhereInfo *pWInfo;         /* Will become the return value of this function */
  5065   5567     Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
  5066   5568     Bitmask notReady;          /* Cursors that are not yet positioned */
  5067         -  WhereBestIdx sWBI;         /* Best index search context */
         5569  +  WhereLoopBuilder sWLB;     /* The WhereLoop builder */
  5068   5570     WhereMaskSet *pMaskSet;    /* The expression mask set */
  5069   5571     WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
  5070         -  int iFrom;                 /* First unused FROM clause element */
  5071         -  int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
  5072   5572     int ii;                    /* Loop counter */
  5073   5573     sqlite3 *db;               /* Database connection */
         5574  +  int rc;                    /* Return code */
  5074   5575   
  5075   5576   
  5076   5577     /* Variable initialization */
  5077         -  memset(&sWBI, 0, sizeof(sWBI));
  5078         -  sWBI.pParse = pParse;
         5578  +  memset(&sWLB, 0, sizeof(sWLB));
         5579  +  sWLB.pOrderBy = pOrderBy;
  5079   5580   
  5080   5581     /* The number of tables in the FROM clause is limited by the number of
  5081   5582     ** bits in a Bitmask 
  5082   5583     */
  5083   5584     testcase( pTabList->nSrc==BMS );
  5084   5585     if( pTabList->nSrc>BMS ){
  5085   5586       sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
................................................................................
  5098   5599     ** struct, the contents of WhereInfo.a[], the WhereClause structure
  5099   5600     ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
  5100   5601     ** field (type Bitmask) it must be aligned on an 8-byte boundary on
  5101   5602     ** some architectures. Hence the ROUND8() below.
  5102   5603     */
  5103   5604     db = pParse->db;
  5104   5605     nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
  5105         -  pWInfo = sqlite3DbMallocZero(db, 
  5106         -      nByteWInfo + 
  5107         -      sizeof(WhereClause) +
  5108         -      sizeof(WhereMaskSet)
  5109         -  );
         5606  +  pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
  5110   5607     if( db->mallocFailed ){
  5111   5608       sqlite3DbFree(db, pWInfo);
  5112   5609       pWInfo = 0;
  5113   5610       goto whereBeginError;
  5114   5611     }
  5115   5612     pWInfo->nLevel = nTabList;
  5116   5613     pWInfo->pParse = pParse;
  5117   5614     pWInfo->pTabList = pTabList;
         5615  +  pWInfo->pOrderBy = pOrderBy;
         5616  +  pWInfo->pDistinct = pDistinct;
  5118   5617     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
  5119         -  pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
  5120   5618     pWInfo->wctrlFlags = wctrlFlags;
  5121   5619     pWInfo->savedNQueryLoop = pParse->nQueryLoop;
  5122         -  pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
  5123         -  sWBI.aLevel = pWInfo->a;
         5620  +  pMaskSet = &pWInfo->sMaskSet;
         5621  +  sWLB.pWInfo = pWInfo;
         5622  +  sWLB.pWC = &pWInfo->sWC;
         5623  +  sWLB.pNew = (WhereLoop*)&pWInfo->a[nTabList];
         5624  +  whereLoopInit(sWLB.pNew);
         5625  +#ifdef SQLITE_DEBUG
         5626  +  sWLB.pNew->cId = '*';
         5627  +#endif
  5124   5628   
  5125   5629     /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
  5126   5630     ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
  5127   5631     if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
  5128   5632   
  5129   5633     /* Split the WHERE clause into separate subexpressions where each
  5130   5634     ** subexpression is separated by an AND operator.
  5131   5635     */
  5132   5636     initMaskSet(pMaskSet);
  5133         -  whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
         5637  +  whereClauseInit(&pWInfo->sWC, pWInfo);
  5134   5638     sqlite3ExprCodeConstants(pParse, pWhere);
  5135         -  whereSplit(sWBI.pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
         5639  +  whereSplit(&pWInfo->sWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
  5136   5640       
  5137   5641     /* Special case: a WHERE clause that is constant.  Evaluate the
  5138   5642     ** expression and either jump over all of the code or fall thru.
  5139   5643     */
  5140   5644     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
  5141   5645       sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
  5142   5646       pWhere = 0;
  5143   5647     }
         5648  +
         5649  +  /* Special case: No FROM clause
         5650  +  */
         5651  +  if( nTabList==0 ){
         5652  +    if( pOrderBy ) pWInfo->bOBSat = 1;
         5653  +    if( pDistinct ) pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
         5654  +  }
  5144   5655   
  5145   5656     /* Assign a bit from the bitmask to every term in the FROM clause.
  5146   5657     **
  5147   5658     ** When assigning bitmask values to FROM clause cursors, it must be
  5148   5659     ** the case that if X is the bitmask for the N-th FROM clause term then
  5149   5660     ** the bitmask for all FROM clause terms to the left of the N-th term
  5150   5661     ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
................................................................................
  5173   5684   #endif
  5174   5685   
  5175   5686     /* Analyze all of the subexpressions.  Note that exprAnalyze() might
  5176   5687     ** add new virtual terms onto the end of the WHERE clause.  We do not
  5177   5688     ** want to analyze these virtual terms, so start analyzing at the end
  5178   5689     ** and work forward so that the added virtual terms are never processed.
  5179   5690     */
  5180         -  exprAnalyzeAll(pTabList, sWBI.pWC);
         5691  +  exprAnalyzeAll(pTabList, &pWInfo->sWC);
  5181   5692     if( db->mallocFailed ){
  5182   5693       goto whereBeginError;
  5183   5694     }
         5695  +
         5696  +  /* If the ORDER BY (or GROUP BY) clause contains references to general
         5697  +  ** expressions, then we won't be able to satisfy it using indices, so
         5698  +  ** go ahead and disable it now.
         5699  +  */
         5700  +  if( pOrderBy && pDistinct ){
         5701  +    for(ii=0; ii<pOrderBy->nExpr; ii++){
         5702  +      Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr);
         5703  +      if( pExpr->op!=TK_COLUMN ){
         5704  +        pWInfo->pOrderBy = pOrderBy = 0;
         5705  +        break;
         5706  +      }else if( pExpr->iColumn<0 ){
         5707  +        break;
         5708  +      }
         5709  +    }
         5710  +  }
  5184   5711   
  5185   5712     /* Check if the DISTINCT qualifier, if there is one, is redundant. 
  5186   5713     ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
  5187   5714     ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
  5188   5715     */
  5189         -  if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
  5190         -    pDistinct = 0;
  5191         -    pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
         5716  +  if( pDistinct ){
         5717  +    if( isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){
         5718  +      pDistinct = 0;
         5719  +      pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
         5720  +    }else if( pOrderBy==0 ){
         5721  +      pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
         5722  +      pWInfo->pOrderBy = pDistinct;
         5723  +    }
         5724  +  }
         5725  +
         5726  +  /* Construct the WhereLoop objects */
         5727  +  WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
         5728  +  if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
         5729  +    rc = whereLoopAddAll(&sWLB);
         5730  +    if( rc ) goto whereBeginError;
         5731  +  
         5732  +    /* Display all of the WhereLoop objects if wheretrace is enabled */
         5733  +#ifdef WHERETRACE_ENABLED
         5734  +    if( sqlite3WhereTrace ){
         5735  +      WhereLoop *p;
         5736  +      int i = 0;
         5737  +      static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
         5738  +                                       "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
         5739  +      for(p=pWInfo->pLoops; p; p=p->pNextLoop){
         5740  +        p->cId = zLabel[(i++)%sizeof(zLabel)];
         5741  +        whereLoopPrint(p, pTabList);
         5742  +      }
         5743  +    }
         5744  +#endif
         5745  +  
         5746  +    wherePathSolver(pWInfo, 0);
         5747  +    if( db->mallocFailed ) goto whereBeginError;
         5748  +    if( pWInfo->pOrderBy ){
         5749  +       wherePathSolver(pWInfo, pWInfo->nRowOut+1);
         5750  +       if( db->mallocFailed ) goto whereBeginError;
         5751  +    }
         5752  +  }
         5753  +  if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
         5754  +     pWInfo->revMask = (Bitmask)(-1);
         5755  +  }
         5756  +  if( pParse->nErr || NEVER(db->mallocFailed) ){
         5757  +    goto whereBeginError;
  5192   5758     }
  5193         -
  5194         -  /* Chose the best index to use for each table in the FROM clause.
  5195         -  **
  5196         -  ** This loop fills in the following fields:
  5197         -  **
  5198         -  **   pWInfo->a[].pIdx      The index to use for this level of the loop.
  5199         -  **   pWInfo->a[].wsFlags   WHERE_xxx flags associated with pIdx
  5200         -  **   pWInfo->a[].nEq       The number of == and IN constraints
  5201         -  **   pWInfo->a[].iFrom     Which term of the FROM clause is being coded
  5202         -  **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
  5203         -  **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
  5204         -  **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
  5205         -  **
  5206         -  ** This loop also figures out the nesting order of tables in the FROM
  5207         -  ** clause.
  5208         -  */
  5209         -  sWBI.notValid = ~(Bitmask)0;
  5210         -  sWBI.pOrderBy = pOrderBy;
  5211         -  sWBI.n = nTabList;
  5212         -  sWBI.pDistinct = pDistinct;
  5213         -  andFlags = ~0;
  5214         -  WHERETRACE(("*** Optimizer Start ***\n"));
  5215         -  for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
  5216         -    WhereCost bestPlan;         /* Most efficient plan seen so far */
  5217         -    Index *pIdx;                /* Index for FROM table at pTabItem */
  5218         -    int j;                      /* For looping over FROM tables */
  5219         -    int bestJ = -1;             /* The value of j */
  5220         -    Bitmask m;                  /* Bitmask value for j or bestJ */
  5221         -    int isOptimal;              /* Iterator for optimal/non-optimal search */
  5222         -    int ckOptimal;              /* Do the optimal scan check */
  5223         -    int nUnconstrained;         /* Number tables without INDEXED BY */
  5224         -    Bitmask notIndexed;         /* Mask of tables that cannot use an index */
  5225         -
  5226         -    memset(&bestPlan, 0, sizeof(bestPlan));
  5227         -    bestPlan.rCost = SQLITE_BIG_DBL;
  5228         -    WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
  5229         -
  5230         -    /* Loop through the remaining entries in the FROM clause to find the
  5231         -    ** next nested loop. The loop tests all FROM clause entries
  5232         -    ** either once or twice. 
  5233         -    **
  5234         -    ** The first test is always performed if there are two or more entries
  5235         -    ** remaining and never performed if there is only one FROM clause entry
  5236         -    ** to choose from.  The first test looks for an "optimal" scan.  In
  5237         -    ** this context an optimal scan is one that uses the same strategy
  5238         -    ** for the given FROM clause entry as would be selected if the entry
  5239         -    ** were used as the innermost nested loop.  In other words, a table
  5240         -    ** is chosen such that the cost of running that table cannot be reduced
  5241         -    ** by waiting for other tables to run first.  This "optimal" test works
  5242         -    ** by first assuming that the FROM clause is on the inner loop and finding
  5243         -    ** its query plan, then checking to see if that query plan uses any
  5244         -    ** other FROM clause terms that are sWBI.notValid.  If no notValid terms
  5245         -    ** are used then the "optimal" query plan works.
  5246         -    **
  5247         -    ** Note that the WhereCost.nRow parameter for an optimal scan might
  5248         -    ** not be as small as it would be if the table really were the innermost
  5249         -    ** join.  The nRow value can be reduced by WHERE clause constraints
  5250         -    ** that do not use indices.  But this nRow reduction only happens if the
  5251         -    ** table really is the innermost join.  
  5252         -    **
  5253         -    ** The second loop iteration is only performed if no optimal scan
  5254         -    ** strategies were found by the first iteration. This second iteration
  5255         -    ** is used to search for the lowest cost scan overall.
  5256         -    **
  5257         -    ** Without the optimal scan step (the first iteration) a suboptimal
  5258         -    ** plan might be chosen for queries like this:
  5259         -    **   
  5260         -    **   CREATE TABLE t1(a, b); 
  5261         -    **   CREATE TABLE t2(c, d);
  5262         -    **   SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
  5263         -    **
  5264         -    ** The best strategy is to iterate through table t1 first. However it
  5265         -    ** is not possible to determine this with a simple greedy algorithm.
  5266         -    ** Since the cost of a linear scan through table t2 is the same 
  5267         -    ** as the cost of a linear scan through table t1, a simple greedy 
  5268         -    ** algorithm may choose to use t2 for the outer loop, which is a much
  5269         -    ** costlier approach.
  5270         -    */
  5271         -    nUnconstrained = 0;
  5272         -    notIndexed = 0;
  5273         -
  5274         -    /* The optimal scan check only occurs if there are two or more tables
  5275         -    ** available to be reordered */
  5276         -    if( iFrom==nTabList-1 ){
  5277         -      ckOptimal = 0;  /* Common case of just one table in the FROM clause */
  5278         -    }else{
  5279         -      ckOptimal = -1;
  5280         -      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
  5281         -        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
  5282         -        if( (m & sWBI.notValid)==0 ){
  5283         -          if( j==iFrom ) iFrom++;
  5284         -          continue;
  5285         -        }
  5286         -        if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ) break;
  5287         -        if( ++ckOptimal ) break;
  5288         -        if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
         5759  +#ifdef WHERETRACE_ENABLED
         5760  +  if( sqlite3WhereTrace ){
         5761  +    int ii;
         5762  +    sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
         5763  +    if( pWInfo->bOBSat ){
         5764  +      sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask);
         5765  +    }
         5766  +    switch( pWInfo->eDistinct ){
         5767  +      case WHERE_DISTINCT_UNIQUE: {
         5768  +        sqlite3DebugPrintf("  DISTINCT=unique");
         5769  +        break;
         5770  +      }
         5771  +      case WHERE_DISTINCT_ORDERED: {
         5772  +        sqlite3DebugPrintf("  DISTINCT=ordered");
         5773  +        break;
         5774  +      }
         5775  +      case WHERE_DISTINCT_UNORDERED: {
         5776  +        sqlite3DebugPrintf("  DISTINCT=unordered");
         5777  +        break;
  5289   5778         }
  5290   5779       }
  5291         -    assert( ckOptimal==0 || ckOptimal==1 );
  5292         -
  5293         -    for(isOptimal=ckOptimal; isOptimal>=0 && bestJ<0; isOptimal--){
  5294         -      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
  5295         -        if( j>iFrom && (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0 ){
  5296         -          /* This break and one like it in the ckOptimal computation loop
  5297         -          ** above prevent table reordering across LEFT and CROSS JOINs.
  5298         -          ** The LEFT JOIN case is necessary for correctness.  The prohibition
  5299         -          ** against reordering across a CROSS JOIN is an SQLite feature that
  5300         -          ** allows the developer to control table reordering */
  5301         -          break;
  5302         -        }
  5303         -        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
  5304         -        if( (m & sWBI.notValid)==0 ){
  5305         -          assert( j>iFrom );
  5306         -          continue;
  5307         -        }
  5308         -        sWBI.notReady = (isOptimal ? m : sWBI.notValid);
  5309         -        if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
  5310         -  
  5311         -        WHERETRACE(("   === trying table %d (%s) with isOptimal=%d ===\n",
  5312         -                    j, sWBI.pSrc->pTab->zName, isOptimal));
  5313         -        assert( sWBI.pSrc->pTab );
  5314         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  5315         -        if( IsVirtual(sWBI.pSrc->pTab) ){
  5316         -          sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
  5317         -          bestVirtualIndex(&sWBI);
  5318         -        }else 
  5319         -#endif
  5320         -        {
  5321         -          bestBtreeIndex(&sWBI);
  5322         -        }
  5323         -        assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
  5324         -
  5325         -        /* If an INDEXED BY clause is present, then the plan must use that
  5326         -        ** index if it uses any index at all */
  5327         -        assert( sWBI.pSrc->pIndex==0 
  5328         -                  || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
  5329         -                  || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
  5330         -
  5331         -        if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
  5332         -          notIndexed |= m;
  5333         -        }
  5334         -        if( isOptimal ){
  5335         -          pWInfo->a[j].rOptCost = sWBI.cost.rCost;
  5336         -        }else if( ckOptimal ){
  5337         -          /* If two or more tables have nearly the same outer loop cost, but
  5338         -          ** very different inner loop (optimal) cost, we want to choose
  5339         -          ** for the outer loop that table which benefits the least from
  5340         -          ** being in the inner loop.  The following code scales the 
  5341         -          ** outer loop cost estimate to accomplish that. */
  5342         -          WHERETRACE(("   scaling cost from %.1f to %.1f\n",
  5343         -                      sWBI.cost.rCost,
  5344         -                      sWBI.cost.rCost/pWInfo->a[j].rOptCost));
  5345         -          sWBI.cost.rCost /= pWInfo->a[j].rOptCost;
  5346         -        }
  5347         -
  5348         -        /* Conditions under which this table becomes the best so far:
  5349         -        **
  5350         -        **   (1) The table must not depend on other tables that have not
  5351         -        **       yet run.  (In other words, it must not depend on tables
  5352         -        **       in inner loops.)
  5353         -        **
  5354         -        **   (2) (This rule was removed on 2012-11-09.  The scaling of the
  5355         -        **       cost using the optimal scan cost made this rule obsolete.)
  5356         -        **
  5357         -        **   (3) All tables have an INDEXED BY clause or this table lacks an
  5358         -        **       INDEXED BY clause or this table uses the specific
  5359         -        **       index specified by its INDEXED BY clause.  This rule ensures
  5360         -        **       that a best-so-far is always selected even if an impossible
  5361         -        **       combination of INDEXED BY clauses are given.  The error
  5362         -        **       will be detected and relayed back to the application later.
  5363         -        **       The NEVER() comes about because rule (2) above prevents
  5364         -        **       An indexable full-table-scan from reaching rule (3).
  5365         -        **
  5366         -        **   (4) The plan cost must be lower than prior plans, where "cost"
  5367         -        **       is defined by the compareCost() function above. 
  5368         -        */
  5369         -        if( (sWBI.cost.used&sWBI.notValid)==0                    /* (1) */
  5370         -            && (nUnconstrained==0 || sWBI.pSrc->pIndex==0        /* (3) */
  5371         -                || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
  5372         -            && (bestJ<0 || compareCost(&sWBI.cost, &bestPlan))   /* (4) */
  5373         -        ){
  5374         -          WHERETRACE(("   === table %d (%s) is best so far\n"
  5375         -                      "       cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=%08x\n",
  5376         -                      j, sWBI.pSrc->pTab->zName,
  5377         -                      sWBI.cost.rCost, sWBI.cost.plan.nRow,
  5378         -                      sWBI.cost.plan.nOBSat, sWBI.cost.plan.wsFlags));
  5379         -          bestPlan = sWBI.cost;
  5380         -          bestJ = j;
  5381         -        }
  5382         -
  5383         -        /* In a join like "w JOIN x LEFT JOIN y JOIN z"  make sure that
  5384         -        ** table y (and not table z) is always the next inner loop inside
  5385         -        ** of table x. */
  5386         -        if( (sWBI.pSrc->jointype & JT_LEFT)!=0 ) break;
  5387         -      }
  5388         -    }
  5389         -    assert( bestJ>=0 );
  5390         -    assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
  5391         -    assert( bestJ==iFrom || (pTabList->a[iFrom].jointype & JT_LEFT)==0 );
  5392         -    testcase( bestJ>iFrom && (pTabList->a[iFrom].jointype & JT_CROSS)!=0 );
  5393         -    testcase( bestJ>iFrom && bestJ<nTabList-1
  5394         -                          && (pTabList->a[bestJ+1].jointype & JT_LEFT)!=0 );
  5395         -    WHERETRACE(("*** Optimizer selects table %d (%s) for loop %d with:\n"
  5396         -                "    cost=%.1f, nRow=%.1f, nOBSat=%d, wsFlags=0x%08x\n",
  5397         -                bestJ, pTabList->a[bestJ].pTab->zName,
  5398         -                pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
  5399         -                bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
  5400         -    if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
  5401         -      assert( pWInfo->eDistinct==0 );
  5402         -      pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
  5403         -    }
  5404         -    andFlags &= bestPlan.plan.wsFlags;
  5405         -    pLevel->plan = bestPlan.plan;
  5406         -    pLevel->iTabCur = pTabList->a[bestJ].iCursor;
  5407         -    testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
  5408         -    testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
  5409         -    if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){
  5410         -      if( (wctrlFlags & WHERE_ONETABLE_ONLY) 
  5411         -       && (bestPlan.plan.wsFlags & WHERE_TEMP_INDEX)==0 
  5412         -      ){
  5413         -        pLevel->iIdxCur = iIdxCur;
  5414         -      }else{
  5415         -        pLevel->iIdxCur = pParse->nTab++;
  5416         -      }
  5417         -    }else{
  5418         -      pLevel->iIdxCur = -1;
  5419         -    }
  5420         -    sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
  5421         -    pLevel->iFrom = (u8)bestJ;
  5422         -    if( bestPlan.plan.nRow>=(double)1 ){
  5423         -      pParse->nQueryLoop *= bestPlan.plan.nRow;
  5424         -    }
  5425         -
  5426         -    /* Check that if the table scanned by this loop iteration had an
  5427         -    ** INDEXED BY clause attached to it, that the named index is being
  5428         -    ** used for the scan. If not, then query compilation has failed.
  5429         -    ** Return an error.
  5430         -    */
  5431         -    pIdx = pTabList->a[bestJ].pIndex;
  5432         -    if( pIdx ){
  5433         -      if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){
  5434         -        sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName);
  5435         -        goto whereBeginError;
  5436         -      }else{
  5437         -        /* If an INDEXED BY clause is used, the bestIndex() function is
  5438         -        ** guaranteed to find the index specified in the INDEXED BY clause
  5439         -        ** if it find an index at all. */
  5440         -        assert( bestPlan.plan.u.pIdx==pIdx );
  5441         -      }
         5780  +    sqlite3DebugPrintf("\n");
         5781  +    for(ii=0; ii<nTabList; ii++){
         5782  +      whereLoopPrint(pWInfo->a[ii].pWLoop, pTabList);
  5442   5783       }
  5443   5784     }
  5444         -  WHERETRACE(("*** Optimizer Finished ***\n"));
  5445         -  if( pParse->nErr || db->mallocFailed ){
  5446         -    goto whereBeginError;
  5447         -  }
  5448         -  if( nTabList ){
  5449         -    pLevel--;
  5450         -    pWInfo->nOBSat = pLevel->plan.nOBSat;
  5451         -  }else{
  5452         -    pWInfo->nOBSat = 0;
  5453         -  }
  5454         -
  5455         -  /* If the total query only selects a single row, then the ORDER BY
  5456         -  ** clause is irrelevant.
  5457         -  */
  5458         -  if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
  5459         -    assert( nTabList==0 || (pLevel->plan.wsFlags & WHERE_ALL_UNIQUE)!=0 );
  5460         -    pWInfo->nOBSat = pOrderBy->nExpr;
  5461         -  }
         5785  +#endif
         5786  +  WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
         5787  +  pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
  5462   5788   
  5463   5789     /* If the caller is an UPDATE or DELETE statement that is requesting
  5464   5790     ** to use a one-pass algorithm, determine if this is appropriate.
  5465   5791     ** The one-pass algorithm only works if the WHERE clause constraints
  5466   5792     ** the statement to update a single row.
  5467   5793     */
  5468   5794     assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
  5469         -  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){
         5795  +  if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 
         5796  +   && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
  5470   5797       pWInfo->okOnePass = 1;
  5471         -    pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY;
         5798  +    pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
  5472   5799     }
  5473   5800   
  5474   5801     /* Open all tables in the pTabList and any indices selected for
  5475   5802     ** searching those tables.
  5476   5803     */
  5477   5804     sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
  5478   5805     notReady = ~(Bitmask)0;
  5479         -  pWInfo->nRowOut = (double)1;
  5480   5806     for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
  5481   5807       Table *pTab;     /* Table to open */
  5482   5808       int iDb;         /* Index of database containing table/index */
  5483   5809       struct SrcList_item *pTabItem;
         5810  +    WhereLoop *pLoop;
  5484   5811   
  5485   5812       pTabItem = &pTabList->a[pLevel->iFrom];
  5486   5813       pTab = pTabItem->pTab;
  5487         -    pWInfo->nRowOut *= pLevel->plan.nRow;
  5488   5814       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
         5815  +    pLoop = pLevel->pWLoop;
  5489   5816       if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
  5490   5817         /* Do nothing */
  5491   5818       }else
  5492   5819   #ifndef SQLITE_OMIT_VIRTUALTABLE
  5493         -    if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){
         5820  +    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  5494   5821         const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
  5495   5822         int iCur = pTabItem->iCursor;
  5496   5823         sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
  5497   5824       }else if( IsVirtual(pTab) ){
  5498   5825         /* noop */
  5499   5826       }else
  5500   5827   #endif
  5501         -    if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
         5828  +    if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
  5502   5829            && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
  5503   5830         int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
  5504   5831         sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
  5505         -      testcase( pTab->nCol==BMS-1 );
  5506         -      testcase( pTab->nCol==BMS );
         5832  +      testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
         5833  +      testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
  5507   5834         if( !pWInfo->okOnePass && pTab->nCol<BMS ){
  5508   5835           Bitmask b = pTabItem->colUsed;
  5509   5836           int n = 0;
  5510   5837           for(; b; b=b>>1, n++){}
  5511   5838           sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, 
  5512   5839                               SQLITE_INT_TO_PTR(n), P4_INT32);
  5513   5840           assert( n<=pTab->nCol );
  5514   5841         }
  5515   5842       }else{
  5516   5843         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  5517   5844       }
  5518   5845   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  5519         -    if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
  5520         -      constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
         5846  +    if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){
         5847  +      constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel);
  5521   5848       }else
  5522   5849   #endif
  5523         -    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
  5524         -      Index *pIx = pLevel->plan.u.pIdx;
         5850  +    if( pLoop->wsFlags & WHERE_INDEXED ){
         5851  +      Index *pIx = pLoop->u.btree.pIndex;
  5525   5852         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
  5526         -      int iIndexCur = pLevel->iIdxCur;
         5853  +      /* FIXME:  As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */
         5854  +      int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++;
  5527   5855         assert( pIx->pSchema==pTab->pSchema );
  5528   5856         assert( iIndexCur>=0 );
  5529   5857         sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
  5530   5858                           (char*)pKey, P4_KEYINFO_HANDOFF);
  5531   5859         VdbeComment((v, "%s", pIx->zName));
  5532   5860       }
  5533   5861       sqlite3CodeVerifySchema(pParse, iDb);
  5534         -    notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
         5862  +    notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
  5535   5863     }
  5536   5864     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
  5537   5865     if( db->mallocFailed ) goto whereBeginError;
  5538   5866   
  5539   5867     /* Generate the code to do the search.  Each iteration of the for
  5540   5868     ** loop below generates code for a single nested loop of the VM
  5541   5869     ** program.
  5542   5870     */
  5543   5871     notReady = ~(Bitmask)0;
  5544   5872     for(ii=0; ii<nTabList; ii++){
  5545   5873       pLevel = &pWInfo->a[ii];
  5546   5874       explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
  5547         -    notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
         5875  +    notReady = codeOneLoopStart(pWInfo, ii, notReady);
  5548   5876       pWInfo->iContinue = pLevel->addrCont;
  5549   5877     }
  5550   5878   
  5551         -#ifdef SQLITE_TEST  /* For testing and debugging use only */
  5552         -  /* Record in the query plan information about the current table
  5553         -  ** and the index used to access it (if any).  If the table itself
  5554         -  ** is not used, its name is just '{}'.  If no index is used
  5555         -  ** the index is listed as "{}".  If the primary key is used the
  5556         -  ** index name is '*'.
  5557         -  */
  5558         -  for(ii=0; ii<nTabList; ii++){
  5559         -    char *z;
  5560         -    int n;
  5561         -    int w;
  5562         -    struct SrcList_item *pTabItem;
  5563         -
  5564         -    pLevel = &pWInfo->a[ii];
  5565         -    w = pLevel->plan.wsFlags;
  5566         -    pTabItem = &pTabList->a[pLevel->iFrom];
  5567         -    z = pTabItem->zAlias;
  5568         -    if( z==0 ) z = pTabItem->pTab->zName;
  5569         -    n = sqlite3Strlen30(z);
  5570         -    if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
  5571         -      if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
  5572         -        memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
  5573         -        nQPlan += 2;
  5574         -      }else{
  5575         -        memcpy(&sqlite3_query_plan[nQPlan], z, n);
  5576         -        nQPlan += n;
  5577         -      }
  5578         -      sqlite3_query_plan[nQPlan++] = ' ';
  5579         -    }
  5580         -    testcase( w & WHERE_ROWID_EQ );
  5581         -    testcase( w & WHERE_ROWID_RANGE );
  5582         -    if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
  5583         -      memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
  5584         -      nQPlan += 2;
  5585         -    }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
  5586         -      n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
  5587         -      if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
  5588         -        memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
  5589         -        nQPlan += n;
  5590         -        sqlite3_query_plan[nQPlan++] = ' ';
  5591         -      }
  5592         -    }else{
  5593         -      memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3);
  5594         -      nQPlan += 3;
  5595         -    }
  5596         -  }
  5597         -  while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
  5598         -    sqlite3_query_plan[--nQPlan] = 0;
  5599         -  }
  5600         -  sqlite3_query_plan[nQPlan] = 0;
  5601         -  nQPlan = 0;
  5602         -#endif /* SQLITE_TEST // Testing and debugging use only */
  5603         -
  5604         -  /* Record the continuation address in the WhereInfo structure.  Then
  5605         -  ** clean up and return.
  5606         -  */
         5879  +  /* Done. */
  5607   5880     return pWInfo;
  5608   5881   
  5609   5882     /* Jump here if malloc fails */
  5610   5883   whereBeginError:
  5611   5884     if( pWInfo ){
  5612   5885       pParse->nQueryLoop = pWInfo->savedNQueryLoop;
  5613   5886       whereInfoFree(db, pWInfo);
................................................................................
  5620   5893   ** sqlite3WhereBegin() for additional information.
  5621   5894   */
  5622   5895   void sqlite3WhereEnd(WhereInfo *pWInfo){
  5623   5896     Parse *pParse = pWInfo->pParse;
  5624   5897     Vdbe *v = pParse->pVdbe;
  5625   5898     int i;
  5626   5899     WhereLevel *pLevel;
         5900  +  WhereLoop *pLoop;
  5627   5901     SrcList *pTabList = pWInfo->pTabList;
  5628   5902     sqlite3 *db = pParse->db;
  5629   5903   
  5630   5904     /* Generate loop termination code.
  5631   5905     */
  5632   5906     sqlite3ExprCacheClear(pParse);
  5633   5907     for(i=pWInfo->nLevel-1; i>=0; i--){
  5634   5908       pLevel = &pWInfo->a[i];
         5909  +    pLoop = pLevel->pWLoop;
  5635   5910       sqlite3VdbeResolveLabel(v, pLevel->addrCont);
  5636   5911       if( pLevel->op!=OP_Noop ){
  5637   5912         sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
  5638   5913         sqlite3VdbeChangeP5(v, pLevel->p5);
  5639   5914       }
  5640         -    if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
         5915  +    if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
  5641   5916         struct InLoop *pIn;
  5642   5917         int j;
  5643   5918         sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
  5644   5919         for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
  5645   5920           sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
  5646   5921           sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
  5647   5922           sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
................................................................................
  5648   5923         }
  5649   5924         sqlite3DbFree(db, pLevel->u.in.aInLoop);
  5650   5925       }
  5651   5926       sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
  5652   5927       if( pLevel->iLeftJoin ){
  5653   5928         int addr;
  5654   5929         addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
  5655         -      assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
  5656         -           || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 );
  5657         -      if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){
         5930  +      assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
         5931  +           || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
         5932  +      if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
  5658   5933           sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
  5659   5934         }
  5660         -      if( pLevel->iIdxCur>=0 ){
         5935  +      if( pLoop->wsFlags & WHERE_INDEXED ){
  5661   5936           sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
  5662   5937         }
  5663   5938         if( pLevel->op==OP_Return ){
  5664   5939           sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
  5665   5940         }else{
  5666   5941           sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
  5667   5942         }
................................................................................
  5678   5953     */
  5679   5954     assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc );
  5680   5955     for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
  5681   5956       Index *pIdx = 0;
  5682   5957       struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
  5683   5958       Table *pTab = pTabItem->pTab;
  5684   5959       assert( pTab!=0 );
         5960  +    pLoop = pLevel->pWLoop;
  5685   5961       if( (pTab->tabFlags & TF_Ephemeral)==0
  5686   5962        && pTab->pSelect==0
  5687   5963        && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
  5688   5964       ){
  5689         -      int ws = pLevel->plan.wsFlags;
         5965  +      int ws = pLoop->wsFlags;
  5690   5966         if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
  5691   5967           sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
  5692   5968         }
  5693         -      if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){
         5969  +      if( (ws & WHERE_INDEXED)!=0 && (ws & (WHERE_IPK|WHERE_TEMP_INDEX))==0 ){
  5694   5970           sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
  5695   5971         }
  5696   5972       }
  5697   5973   
  5698         -    /* If this scan uses an index, make code substitutions to read data
  5699         -    ** from the index in preference to the table. Sometimes, this means
  5700         -    ** the table need never be read from. This is a performance boost,
  5701         -    ** as the vdbe level waits until the table is read before actually
  5702         -    ** seeking the table cursor to the record corresponding to the current
  5703         -    ** position in the index.
         5974  +    /* If this scan uses an index, make VDBE code substitutions to read data
         5975  +    ** from the index instead of from the table where possible.  In some cases
         5976  +    ** this optimization prevents the table from ever being read, which can
         5977  +    ** yield a significant performance boost.
  5704   5978       ** 
  5705   5979       ** Calls to the code generator in between sqlite3WhereBegin and
  5706   5980       ** sqlite3WhereEnd will have created code that references the table
  5707   5981       ** directly.  This loop scans all that code looking for opcodes
  5708   5982       ** that reference the table and converts them into opcodes that
  5709   5983       ** reference the index.
  5710   5984       */
  5711         -    if( pLevel->plan.wsFlags & WHERE_INDEXED ){
  5712         -      pIdx = pLevel->plan.u.pIdx;
  5713         -    }else if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
         5985  +    if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
         5986  +      pIdx = pLoop->u.btree.pIndex;
         5987  +    }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
  5714   5988         pIdx = pLevel->u.pCovidx;
  5715   5989       }
  5716         -    if( pIdx && !db->mallocFailed){
         5990  +    if( pIdx && !db->mallocFailed ){
  5717   5991         int k, j, last;
  5718   5992         VdbeOp *pOp;
  5719   5993   
  5720   5994         pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
  5721   5995         last = sqlite3VdbeCurrentAddr(v);
  5722   5996         for(k=pWInfo->iTop; k<last; k++, pOp++){
  5723   5997           if( pOp->p1!=pLevel->iTabCur ) continue;
................................................................................
  5725   5999             for(j=0; j<pIdx->nColumn; j++){
  5726   6000               if( pOp->p2==pIdx->aiColumn[j] ){
  5727   6001                 pOp->p2 = j;
  5728   6002                 pOp->p1 = pLevel->iIdxCur;
  5729   6003                 break;
  5730   6004               }
  5731   6005             }
  5732         -          assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
  5733         -               || j<pIdx->nColumn );
         6006  +          assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || j<pIdx->nColumn );
  5734   6007           }else if( pOp->opcode==OP_Rowid ){
  5735   6008             pOp->p1 = pLevel->iIdxCur;
  5736   6009             pOp->opcode = OP_IdxRowid;
  5737   6010           }
  5738   6011         }
  5739   6012       }
  5740   6013     }

Changes to test/all.test.

    44     44   if {$::tcl_platform(platform)=="unix"} {
    45     45     ifcapable !default_autovacuum {
    46     46       run_test_suite autovacuum_crash
    47     47     }
    48     48   }
    49     49   
    50     50   finish_test
    51         -
    52         -

Changes to test/analyze3.test.

    93     93       COMMIT;
    94     94       ANALYZE;
    95     95     }
    96     96   } {}
    97     97   
    98     98   do_eqp_test analyze3-1.1.2 {
    99     99     SELECT sum(y) FROM t1 WHERE x>200 AND x<300
   100         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~179 rows)}}
          100  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
   101    101   do_eqp_test analyze3-1.1.3 {
   102    102     SELECT sum(y) FROM t1 WHERE x>0 AND x<1100 
   103         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?) (~959 rows)}}
          103  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (x>? AND x<?)}}
   104    104   
   105    105   do_test analyze3-1.1.4 {
   106    106     sf_execsql { SELECT sum(y) FROM t1 WHERE x>200 AND x<300 }
   107    107   } {199 0 14850}
   108    108   do_test analyze3-1.1.5 {
   109    109     set l [string range "200" 0 end]
   110    110     set u [string range "300" 0 end]
................................................................................
   142    142         CREATE INDEX i2 ON t2(x);
   143    143       COMMIT;
   144    144       ANALYZE;
   145    145     }
   146    146   } {}
   147    147   do_eqp_test analyze3-1.2.2 {
   148    148     SELECT sum(y) FROM t2 WHERE x>1 AND x<2
   149         -} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~196 rows)}}
          149  +} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?)}}
   150    150   do_eqp_test analyze3-1.2.3 {
   151    151     SELECT sum(y) FROM t2 WHERE x>0 AND x<99
   152         -} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?) (~968 rows)}}
          152  +} {0 0 0 {SEARCH TABLE t2 USING INDEX i2 (x>? AND x<?)}}
   153    153   do_test analyze3-1.2.4 {
   154    154     sf_execsql { SELECT sum(y) FROM t2 WHERE x>12 AND x<20 }
   155    155   } {161 0 4760}
   156    156   do_test analyze3-1.2.5 {
   157    157     set l [string range "12" 0 end]
   158    158     set u [string range "20" 0 end]
   159    159     sf_execsql {SELECT typeof($l), typeof($u), sum(y) FROM t2 WHERE x>$l AND x<$u}
................................................................................
   189    189         CREATE INDEX i3 ON t3(x);
   190    190       COMMIT;
   191    191       ANALYZE;
   192    192     }
   193    193   } {}
   194    194   do_eqp_test analyze3-1.3.2 {
   195    195     SELECT sum(y) FROM t3 WHERE x>200 AND x<300
   196         -} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~156 rows)}}
          196  +} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?)}}
   197    197   do_eqp_test analyze3-1.3.3 {
   198    198     SELECT sum(y) FROM t3 WHERE x>0 AND x<1100
   199         -} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?) (~989 rows)}}
          199  +} {0 0 0 {SEARCH TABLE t3 USING INDEX i3 (x>? AND x<?)}}
   200    200   
   201    201   do_test analyze3-1.3.4 {
   202    202     sf_execsql { SELECT sum(y) FROM t3 WHERE x>200 AND x<300 }
   203    203   } {199 0 14850}
   204    204   do_test analyze3-1.3.5 {
   205    205     set l [string range "200" 0 end]
   206    206     set u [string range "300" 0 end]
................................................................................
   244    244       append t [lindex {a b c d e f g h i j} [expr ($i%10)]]
   245    245       execsql { INSERT INTO t1 VALUES($i, $t) }
   246    246     }
   247    247     execsql COMMIT
   248    248   } {}
   249    249   do_eqp_test analyze3-2.2 {
   250    250     SELECT count(a) FROM t1 WHERE b LIKE 'a%'
   251         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?) (~31250 rows)}}
          251  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (b>? AND b<?)}}
   252    252   do_eqp_test analyze3-2.3 {
   253    253     SELECT count(a) FROM t1 WHERE b LIKE '%a'
   254         -} {0 0 0 {SCAN TABLE t1 (~500000 rows)}}
          254  +} {0 0 0 {SCAN TABLE t1}}
   255    255   
   256    256   do_test analyze3-2.4 {
   257    257     sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE 'a%' }
   258    258   } {101 0 100}
   259    259   do_test analyze3-2.5 {
   260    260     sf_execsql { SELECT count(*) FROM t1 WHERE b LIKE '%a' }
   261    261   } {999 999 100}
................................................................................
   326    326   do_test analyze3-3.2.5 {
   327    327     set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE b=?" -1 dummy]
   328    328     sqlite3_expired $S
   329    329   } {0}
   330    330   do_test analyze3-3.2.6 {
   331    331     sqlite3_bind_text $S 1 "abc" 3
   332    332     sqlite3_expired $S
   333         -} {0}
          333  +} {1}
   334    334   do_test analyze3-3.2.7 {
   335    335     sqlite3_finalize $S
   336    336   } {SQLITE_OK}
   337    337   
   338    338   do_test analyze3-3.4.1 {
   339    339     set S [sqlite3_prepare_v2 db "SELECT * FROM t1 WHERE a=? AND b>?" -1 dummy]
   340    340     sqlite3_expired $S

Changes to test/analyze4.test.

    34     34       INSERT INTO t1 SELECT a+32, b FROM t1;
    35     35       INSERT INTO t1 SELECT a+64, b FROM t1;
    36     36       ANALYZE;
    37     37     }
    38     38   
    39     39     # Should choose the t1a index since it is more specific than t1b.
    40     40     db eval {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=5 AND b IS NULL}
    41         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           41  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    42     42   
    43     43   # Verify that the t1b index shows that it does not narrow down the
    44     44   # search any at all.
    45     45   #
    46     46   do_test analyze4-1.1 {
    47     47     db eval {
    48     48       SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t1' ORDER BY idx;

Changes to test/analyze5.test.

   152    152     301  {y=1}                 t1y   26
   153    153     302  {y=0.1}               t1y    1
   154    154   
   155    155     400  {x IS NULL}           t1x  400
   156    156   
   157    157   } {
   158    158     # Verify that the expected index is used with the expected row count
   159         -  do_test analyze5-1.${testid}a {
   160         -    set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
   161         -    set idx {}
   162         -    regexp {INDEX (t1.) } $x all idx
   163         -    regexp {~([0-9]+) rows} $x all nrow
   164         -    list $idx $nrow
   165         -  } [list $index $rows]
          159  +  # No longer valid due to an EXPLAIN QUERY PLAN output format change
          160  +  # do_test analyze5-1.${testid}a {
          161  +  #   set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
          162  +  #   set idx {}
          163  +  #   regexp {INDEX (t1.) } $x all idx
          164  +  #   regexp {~([0-9]+) rows} $x all nrow
          165  +  #   list $idx $nrow
          166  +  # } [list $index $rows]
   166    167   
   167    168     # Verify that the same result is achieved regardless of whether or not
   168    169     # the index is used
   169    170     do_test analyze5-1.${testid}b {
   170    171       set w2 [string map {y +y z +z} $where]
   171    172       set a1 [db eval "SELECT rowid FROM t1 NOT INDEXED WHERE $w2\
   172    173                        ORDER BY +rowid"]
................................................................................
   198    199     503  {x=1}                               t1x   1
   199    200     504  {x IS NOT NULL}                     t1x   2
   200    201     505  {+x IS NOT NULL}                     {} 500
   201    202     506  {upper(x) IS NOT NULL}               {} 500
   202    203   
   203    204   } {
   204    205     # Verify that the expected index is used with the expected row count
   205         -if {$testid==50299} {breakpoint; set sqlite_where_trace 1}
   206         -  do_test analyze5-1.${testid}a {
   207         -    set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
   208         -    set idx {}
   209         -    regexp {INDEX (t1.) } $x all idx
   210         -    regexp {~([0-9]+) rows} $x all nrow
   211         -    list $idx $nrow
   212         -  } [list $index $rows]
   213         -if {$testid==50299} exit
          206  +  # No longer valid due to an EXPLAIN QUERY PLAN format change
          207  +  # do_test analyze5-1.${testid}a {
          208  +  #   set x [lindex [eqp "SELECT * FROM t1 WHERE $where"] 3]
          209  +  #   set idx {}
          210  +  #   regexp {INDEX (t1.) } $x all idx
          211  +  #   regexp {~([0-9]+) rows} $x all nrow
          212  +  #   list $idx $nrow
          213  +  # } [list $index $rows]
   214    214   
   215    215     # Verify that the same result is achieved regardless of whether or not
   216    216     # the index is used
   217    217     do_test analyze5-1.${testid}b {
   218    218       set w2 [string map {y +y z +z} $where]
   219    219       set a1 [db eval "SELECT rowid FROM t1 NOT INDEXED WHERE $w2\
   220    220                        ORDER BY +rowid"]

Changes to test/analyze6.test.

    57     57   # The lowest cost plan is to scan CAT and for each integer there, do a single
    58     58   # lookup of the first corresponding entry in EV then read off the equal values
    59     59   # in EV.  (Prior to the 2011-03-04 enhancement to where.c, this query would
    60     60   # have used EV for the outer loop instead of CAT - which was about 3x slower.)
    61     61   #
    62     62   do_test analyze6-1.1 {
    63     63     eqp {SELECT count(*) FROM ev, cat WHERE x=y}
    64         -} {0 0 1 {SCAN TABLE cat USING COVERING INDEX catx (~16 rows)} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
           64  +} {0 0 1 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}}
    65     65   
    66     66   # The same plan is chosen regardless of the order of the tables in the
    67     67   # FROM clause.
    68     68   #
    69     69   do_test analyze6-1.2 {
    70     70     eqp {SELECT count(*) FROM cat, ev WHERE x=y}
    71         -} {0 0 0 {SCAN TABLE cat USING COVERING INDEX catx (~16 rows)} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
           71  +} {0 0 0 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}}
    72     72   
    73     73   
    74     74   # Ticket [83ea97620bd3101645138b7b0e71c12c5498fe3d] 2011-03-30
    75     75   # If ANALYZE is run on an empty table, make sure indices are used
    76     76   # on the table.
    77     77   #
    78     78   do_test analyze6-2.1 {
    79     79     execsql {
    80     80       CREATE TABLE t201(x INTEGER PRIMARY KEY, y UNIQUE, z);
    81     81       CREATE INDEX t201z ON t201(z);
    82     82       ANALYZE;
    83     83     }
    84     84     eqp {SELECT * FROM t201 WHERE z=5}
    85         -} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?) (~10 rows)}}
           85  +} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
    86     86   do_test analyze6-2.2 {
    87     87     eqp {SELECT * FROM t201 WHERE y=5}
    88         -} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?) (~1 rows)}}
           88  +} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
    89     89   do_test analyze6-2.3 {
    90     90     eqp {SELECT * FROM t201 WHERE x=5}
    91         -} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
           91  +} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
    92     92   do_test analyze6-2.4 {
    93     93     execsql {
    94     94       INSERT INTO t201 VALUES(1,2,3);
    95     95       ANALYZE t201;
    96     96     }
    97     97     eqp {SELECT * FROM t201 WHERE z=5}
    98         -} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?) (~10 rows)}}
           98  +} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
    99     99   do_test analyze6-2.5 {
   100    100     eqp {SELECT * FROM t201 WHERE y=5}
   101         -} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?) (~1 rows)}}
          101  +} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
   102    102   do_test analyze6-2.6 {
   103    103     eqp {SELECT * FROM t201 WHERE x=5}
   104         -} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
          104  +} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
   105    105   do_test analyze6-2.7 {
   106    106     execsql {
   107    107       INSERT INTO t201 VALUES(4,5,7);
   108    108       INSERT INTO t201 SELECT x+100, y+100, z+100 FROM t201;
   109    109       INSERT INTO t201 SELECT x+200, y+200, z+200 FROM t201;
   110    110       INSERT INTO t201 SELECT x+400, y+400, z+400 FROM t201;
   111    111       ANALYZE t201;
   112    112     }
   113    113     eqp {SELECT * FROM t201 WHERE z=5}
   114         -} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?) (~10 rows)}}
          114  +} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
   115    115   do_test analyze6-2.8 {
   116    116     eqp {SELECT * FROM t201 WHERE y=5}
   117         -} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?) (~1 rows)}}
          117  +} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
   118    118   do_test analyze6-2.9 {
   119    119     eqp {SELECT * FROM t201 WHERE x=5}
   120         -} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}}
          120  +} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
   121    121   
   122    122   finish_test

Changes to test/analyze7.test.

    33     33       CREATE INDEX t1b ON t1(b);
    34     34       CREATE INDEX t1cd ON t1(c,d);
    35     35       CREATE VIRTUAL TABLE nums USING wholenumber;
    36     36       INSERT INTO t1 SELECT value, value, value/100, value FROM nums
    37     37                       WHERE value BETWEEN 1 AND 256;
    38     38       EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;
    39     39     }
    40         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~10 rows)}}
           40  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    41     41   do_test analyze7-1.1 {
    42     42     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
    43         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
           43  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
    44     44   do_test analyze7-1.2 {
    45     45     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    46         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~10 rows)}}
           46  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
    47     47   
    48     48   # Run an analyze on one of the three indices.  Verify that this
    49     49   # effects the row-count estimate on the one query that uses that
    50     50   # one index.
    51     51   #
    52     52   do_test analyze7-2.0 {
    53     53     execsql {ANALYZE t1a;}
    54     54     db cache flush
    55     55     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
    56         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           56  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    57     57   do_test analyze7-2.1 {
    58     58     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
    59         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
           59  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
    60     60   do_test analyze7-2.2 {
    61     61     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    62         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~10 rows)}}
           62  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
    63     63   
    64     64   # Verify that since the query planner now things that t1a is more
    65     65   # selective than t1b, it prefers to use t1a.
    66     66   #
    67     67   do_test analyze7-2.3 {
    68     68     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
    69         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           69  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    70     70   
    71     71   # Run an analysis on another of the three indices.  Verify  that this
    72     72   # new analysis works and does not disrupt the previous analysis.
    73     73   #
    74     74   do_test analyze7-3.0 {
    75     75     execsql {ANALYZE t1cd;}
    76     76     db cache flush;
    77     77     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
    78         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           78  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    79     79   do_test analyze7-3.1 {
    80     80     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
    81         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
           81  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
    82     82   do_test analyze7-3.2.1 {
    83     83     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;}
    84         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~86 rows)}}
           84  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
    85     85   ifcapable stat3 {
    86     86     # If ENABLE_STAT3 is defined, SQLite comes up with a different estimated
    87     87     # row count for (c=2) than it does for (c=?).
    88     88     do_test analyze7-3.2.2 {
    89     89       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    90         -  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~57 rows)}}
           90  +  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
    91     91   } else {
    92     92     # If ENABLE_STAT3 is not defined, the expected row count for (c=2) is the
    93     93     # same as that for (c=?).
    94     94     do_test analyze7-3.2.3 {
    95     95       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
    96         -  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~86 rows)}}
           96  +  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}}
    97     97   }
    98     98   do_test analyze7-3.3 {
    99     99     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
   100         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
          100  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
   101    101   ifcapable {!stat3} {
   102    102     do_test analyze7-3.4 {
   103    103       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
   104         -  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
          104  +  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
   105    105     do_test analyze7-3.5 {
   106    106       execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123}
   107         -  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
          107  +  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
   108    108   }
   109    109   do_test analyze7-3.6 {
   110    110     execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123}
   111         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?) (~1 rows)}}
          111  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?)}}
   112    112   
   113    113   finish_test

Changes to test/analyze8.test.

    57     57   # with a==100.  And so for those cases, choose the t1b index.
    58     58   #
    59     59   # Buf ro a==99 and a==101, there are far fewer rows so choose
    60     60   # the t1a index.
    61     61   #
    62     62   do_test 1.1 {
    63     63     eqp {SELECT * FROM t1 WHERE a=100 AND b=55}
    64         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
           64  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
    65     65   do_test 1.2 {
    66     66     eqp {SELECT * FROM t1 WHERE a=99 AND b=55}
    67         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           67  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    68     68   do_test 1.3 {
    69     69     eqp {SELECT * FROM t1 WHERE a=101 AND b=55}
    70         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           70  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    71     71   do_test 1.4 {
    72     72     eqp {SELECT * FROM t1 WHERE a=100 AND b=56}
    73         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
           73  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}}
    74     74   do_test 1.5 {
    75     75     eqp {SELECT * FROM t1 WHERE a=99 AND b=56}
    76         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           76  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    77     77   do_test 1.6 {
    78     78     eqp {SELECT * FROM t1 WHERE a=101 AND b=56}
    79         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
           79  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    80     80   do_test 2.1 {
    81     81     eqp {SELECT * FROM t1 WHERE a=100 AND b BETWEEN 50 AND 54}
    82         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?) (~2 rows)}}
           82  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}}
    83     83   
    84     84   # There are many more values of c between 0 and 100000 than there are
    85     85   # between 800000 and 900000.  So t1c is more selective for the latter
    86     86   # range.
    87     87   #
    88     88   do_test 3.1 {
    89     89     eqp {SELECT * FROM t1 WHERE b BETWEEN 50 AND 54 AND c BETWEEN 0 AND 100000}
    90         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?) (~6 rows)}}
           90  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b>? AND b<?)}}
    91     91   do_test 3.2 {
    92     92     eqp {SELECT * FROM t1
    93     93          WHERE b BETWEEN 50 AND 54 AND c BETWEEN 800000 AND 900000}
    94         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?) (~4 rows)}}
           94  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}}
    95     95   do_test 3.3 {
    96     96     eqp {SELECT * FROM t1 WHERE a=100 AND c BETWEEN 0 AND 100000}
    97         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~63 rows)}}
           97  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}}
    98     98   do_test 3.4 {
    99     99     eqp {SELECT * FROM t1
   100    100          WHERE a=100 AND c BETWEEN 800000 AND 900000}
   101         -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?) (~2 rows)}}
          101  +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1c (c>? AND c<?)}}
   102    102   
   103    103   finish_test

Changes to test/async5.test.

    62     62   sqlite3async_control halt idle
    63     63   sqlite3async_start
    64     64   sqlite3async_wait
    65     65   sqlite3async_control halt never
    66     66   sqlite3async_shutdown
    67     67   set sqlite3async_trace 0
    68     68   finish_test
    69         -

Changes to test/autoindex1.test.

    74     74   } {35}
    75     75   do_test autoindex1-202 {
    76     76     db status autoindex
    77     77   } {0}
    78     78   do_test autoindex1-210 {
    79     79     db eval {
    80     80       PRAGMA automatic_index=ON;
           81  +    ANALYZE;
           82  +    UPDATE sqlite_stat1 SET stat='10000' WHERE tbl='t1';
           83  +    ANALYZE sqlite_master;
    81     84       SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1;
    82     85     }
    83     86   } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
    84     87   do_test autoindex1-211 {
    85     88     db status step
    86     89   } {7}
    87     90   do_test autoindex1-212 {
................................................................................
   139    142   # Ticket [8011086c85c6c404014c947fcf3eb9f42b184a0d] from 2010-07-08
   140    143   # Make sure automatic indices are not created for the RHS of an IN expression
   141    144   # that is not a correlated subquery.
   142    145   #
   143    146   do_execsql_test autoindex1-500 {
   144    147     CREATE TABLE t501(a INTEGER PRIMARY KEY, b);
   145    148     CREATE TABLE t502(x INTEGER PRIMARY KEY, y);
          149  +  INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t501',null,'1000000');
          150  +  INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t502',null,'1000');
          151  +  ANALYZE sqlite_master;
   146    152     EXPLAIN QUERY PLAN
   147    153     SELECT b FROM t501
   148    154      WHERE t501.a IN (SELECT x FROM t502 WHERE y=?);
   149    155   } {
   150         -  0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?) (~25 rows)} 
          156  +  0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?)} 
   151    157     0 0 0 {EXECUTE LIST SUBQUERY 1} 
   152         -  1 0 0 {SCAN TABLE t502 (~100000 rows)}
          158  +  1 0 0 {SCAN TABLE t502}
   153    159   }
   154    160   do_execsql_test autoindex1-501 {
   155    161     EXPLAIN QUERY PLAN
   156    162     SELECT b FROM t501
   157    163      WHERE t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
   158    164   } {
   159         -  0 0 0 {SCAN TABLE t501 (~500000 rows)} 
          165  +  0 0 0 {SCAN TABLE t501} 
   160    166     0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1} 
   161         -  1 0 0 {SEARCH TABLE t502 USING AUTOMATIC COVERING INDEX (y=?) (~7 rows)}
          167  +  1 0 0 {SEARCH TABLE t502 USING AUTOMATIC COVERING INDEX (y=?)}
   162    168   }
   163    169   do_execsql_test autoindex1-502 {
   164    170     EXPLAIN QUERY PLAN
   165    171     SELECT b FROM t501
   166    172      WHERE t501.a=123
   167    173        AND t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
   168    174   } {
   169         -  0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)} 
          175  +  0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?)} 
   170    176     0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1} 
   171         -  1 0 0 {SCAN TABLE t502 (~100000 rows)}
          177  +  1 0 0 {SCAN TABLE t502}
   172    178   }
   173    179   
   174    180   
   175    181   # The following code checks a performance regression reported on the
   176    182   # mailing list on 2010-10-19.  The problem is that the nRowEst field
   177    183   # of ephermeral tables was not being initialized correctly and so no
   178    184   # automatic index was being created for the emphemeral table when it was
................................................................................
   236    242              WHERE prev.flock_no = later.flock_no
   237    243              AND later.owner_change_date > prev.owner_change_date
   238    244              AND later.owner_change_date <= s.date_of_registration||' 00:00:00')
   239    245          ) y ON x.sheep_no = y.sheep_no
   240    246      WHERE y.sheep_no IS NULL
   241    247      ORDER BY x.registering_flock;
   242    248   } {
   243         -  1 0 0 {SCAN TABLE sheep AS s (~1000000 rows)} 
   244         -  1 1 1 {SEARCH TABLE flock_owner AS prev USING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date<?) (~2 rows)} 
          249  +  1 0 0 {SCAN TABLE sheep AS s} 
          250  +  1 1 1 {SEARCH TABLE flock_owner AS prev USING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date<?)} 
   245    251     1 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2} 
   246         -  2 0 0 {SEARCH TABLE flock_owner AS later USING COVERING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date>? AND owner_change_date<?) (~1 rows)} 
   247         -  0 0 0 {SCAN TABLE sheep AS x USING INDEX sheep_reg_flock_index (~1000000 rows)} 
   248         -  0 1 1 {SEARCH SUBQUERY 1 AS y USING AUTOMATIC COVERING INDEX (sheep_no=?) (~8 rows)}
          252  +  2 0 0 {SEARCH TABLE flock_owner AS later USING COVERING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date>? AND owner_change_date<?)} 
          253  +  0 0 0 {SCAN TABLE sheep AS x USING INDEX sheep_reg_flock_index} 
          254  +  0 1 1 {SEARCH SUBQUERY 1 AS y USING AUTOMATIC COVERING INDEX (sheep_no=?)}
   249    255   }
   250    256   
   251    257   
   252    258   do_execsql_test autoindex1-700 {
   253    259     CREATE TABLE t5(a, b, c);
   254    260     EXPLAIN QUERY PLAN SELECT a FROM t5 WHERE b=10 ORDER BY c;
   255    261   } {
   256         -  0 0 0 {SCAN TABLE t5 (~100000 rows)} 
          262  +  0 0 0 {SCAN TABLE t5} 
   257    263     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   258    264   }
   259    265   
   260    266   # The following checks a performance issue reported on the sqlite-dev
   261    267   # mailing list on 2013-01-10
   262    268   #
   263    269   do_execsql_test autoindex1-800 {

Changes to test/backup4.test.

    97     97     db1 close
    98     98     file size test.db
    99     99   } {1024}
   100    100   
   101    101   do_test 3.4 { file size test.db2 } 0
   102    102   
   103    103   finish_test
   104         -

Changes to test/between.test.

    44     44       CREATE INDEX i1zyx ON t1(z,y,x);
    45     45       COMMIT;
    46     46     }
    47     47   } {}
    48     48   
    49     49   # This procedure executes the SQL.  Then it appends to the result the
    50     50   # "sort" or "nosort" keyword depending on whether or not any sorting
    51         -# is done.  Then it appends the ::sqlite_query_plan variable.
           51  +# is done.  Then it appends the names of the table and index used.
    52     52   #
    53     53   proc queryplan {sql} {
    54     54     set ::sqlite_sort_count 0
    55     55     set data [execsql $sql]
    56     56     if {$::sqlite_sort_count} {set x sort} {set x nosort}
    57     57     lappend data $x
    58         -  return [concat $data $::sqlite_query_plan]
           58  +  set eqp [execsql "EXPLAIN QUERY PLAN $sql"]
           59  +  # puts eqp=$eqp
           60  +  foreach {a b c x} $eqp {
           61  +    if {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \
           62  +        $x all as tab idx]} {
           63  +      lappend data $tab $idx
           64  +    } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} {
           65  +      lappend data $tab *
           66  +    }
           67  +  }
           68  +  return $data   
    59     69   }
    60     70   
    61     71   do_test between-1.1.1 {
    62     72     queryplan {
    63     73       SELECT * FROM t1 WHERE w BETWEEN 5 AND 6 ORDER BY +w
    64     74     }
    65     75   } {5 2 36 38 6 2 49 51 sort t1 i1w}
    66     76   do_test between-1.1.2 {
    67     77     queryplan {
    68     78       SELECT * FROM t1 WHERE +w BETWEEN 5 AND 6 ORDER BY +w
    69     79     }
    70         -} {5 2 36 38 6 2 49 51 sort t1 {}}
           80  +} {5 2 36 38 6 2 49 51 sort t1 *}
    71     81   do_test between-1.2.1 {
    72     82     queryplan {
    73     83       SELECT * FROM t1 WHERE w BETWEEN 5 AND 65-y ORDER BY +w
    74     84     }
    75     85   } {5 2 36 38 6 2 49 51 sort t1 i1w}
    76     86   do_test between-1.2.2 {
    77     87     queryplan {
    78     88       SELECT * FROM t1 WHERE +w BETWEEN 5 AND 65-y ORDER BY +w
    79     89     }
    80         -} {5 2 36 38 6 2 49 51 sort t1 {}}
           90  +} {5 2 36 38 6 2 49 51 sort t1 *}
    81     91   do_test between-1.3.1 {
    82     92     queryplan {
    83     93       SELECT * FROM t1 WHERE w BETWEEN 41-y AND 6 ORDER BY +w
    84     94     }
    85     95   } {5 2 36 38 6 2 49 51 sort t1 i1w}
    86     96   do_test between-1.3.2 {
    87     97     queryplan {
    88     98       SELECT * FROM t1 WHERE +w BETWEEN 41-y AND 6 ORDER BY +w
    89     99     }
    90         -} {5 2 36 38 6 2 49 51 sort t1 {}}
          100  +} {5 2 36 38 6 2 49 51 sort t1 *}
    91    101   do_test between-1.4 {
    92    102     queryplan {
    93    103       SELECT * FROM t1 WHERE w BETWEEN 41-y AND 65-y ORDER BY +w
    94    104     }
    95         -} {5 2 36 38 6 2 49 51 sort t1 {}}
          105  +} {5 2 36 38 6 2 49 51 sort t1 *}
    96    106   do_test between-1.5.1 {
    97    107     queryplan {
    98    108       SELECT * FROM t1 WHERE 26 BETWEEN y AND z ORDER BY +w
    99    109     }
   100    110   } {4 2 25 27 sort t1 i1zyx}
   101    111   do_test between-1.5.2 {
   102    112     queryplan {
................................................................................
   103    113       SELECT * FROM t1 WHERE 26 BETWEEN +y AND z ORDER BY +w
   104    114     }
   105    115   } {4 2 25 27 sort t1 i1zyx}
   106    116   do_test between-1.5.3 {
   107    117     queryplan {
   108    118       SELECT * FROM t1 WHERE 26 BETWEEN y AND +z ORDER BY +w
   109    119     }
   110         -} {4 2 25 27 sort t1 {}}
          120  +} {4 2 25 27 sort t1 *}
   111    121   
   112    122   
   113    123   finish_test

Changes to test/btreefault.test.

    51     51   } -test {
    52     52     sqlite3_finalize $::STMT
    53     53     faultsim_test_result {0 {}} 
    54     54     faultsim_integrity_check
    55     55   }
    56     56   
    57     57   finish_test
    58         -

Changes to test/capi3e.test.

    56     56   # capi3e-1.*: Test sqlite3_open with various UTF8 filenames
    57     57   # capi3e-2.*: Test sqlite3_open16 with various UTF8 filenames
    58     58   # capi3e-3.*: Test ATTACH with various UTF8 filenames
    59     59   
    60     60   db close
    61     61   
    62     62   # here's the list of file names we're testing
    63         -set names {t 1 t. 1. t.d 1.d t-1 1-1 t.db .db .db .db .db .db}
           63  +set names {t 1 t. 1. t.d 1.d t-1 1-1 t.db ä.db ë.db ö.db ü.db ÿ.db}
    64     64   
    65     65   set i 0
    66     66   foreach name $names {
    67     67     incr i
    68     68     do_test capi3e-1.1.$i {
    69     69       set db2 [sqlite3_open $name {}]
    70     70       sqlite3_errcode $db2

Changes to test/close.test.

    72     72   } {1 {(21) library routine called out of sequence}}
    73     73   
    74     74   do_test 1.4.4 {
    75     75     sqlite3_finalize $STMT
    76     76   } {SQLITE_OK}
    77     77   
    78     78   finish_test
    79         -

Changes to test/collate2.test.

   632    632   } {aa}
   633    633   
   634    634   # Test that when one side has a default collation type and the other
   635    635   # does not, the collation type is used.
   636    636   do_test collate2-4.3 {
   637    637     execsql {
   638    638       SELECT collate2t1.a FROM collate2t1, collate2t3 
   639         -      WHERE collate2t1.b = collate2t3.b||'';
          639  +      WHERE collate2t1.b = collate2t3.b||''
          640  +      ORDER BY +collate2t1.a DESC;
   640    641     }
   641    642   } {aa aA Aa AA}
   642    643   do_test collate2-4.4 {
   643    644     execsql {
   644    645       SELECT collate2t1.a FROM collate2t1, collate2t3 
   645         -      WHERE collate2t3.b||'' = collate2t1.b;
          646  +      WHERE collate2t3.b||'' = collate2t1.b
          647  +      ORDER BY +collate2t1.a DESC;
   646    648     }
   647    649   } {aa aA Aa AA}
   648    650   
   649    651   do_test collate2-4.5 {
   650    652     execsql {
   651    653       DROP TABLE collate2t3;
   652    654     }

Added test/contrib01.test.

            1  +# 2013-06-05
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# This file contains test cases that were contributed on the sqlite-users
           14  +# mailing list on 2013-06-05 by Mi Chen at mi.chen@echostar.com.
           15  +#
           16  +# At the time it was contributed, this test failed on trunk, but 
           17  +# worked on the NGQP.
           18  +
           19  +set testdir [file dirname $argv0]
           20  +source $testdir/tester.tcl
           21  +
           22  +# Build some test data
           23  +#
           24  +do_test contrib01-1.0 {
           25  +  db eval {
           26  +    CREATE TABLE T1 (B INTEGER NOT NULL,
           27  +                     C INTEGER NOT NULL,
           28  +                     D INTEGER NOT NULL,
           29  +                     E INTEGER NOT NULL,
           30  +                     F INTEGER NOT NULL,
           31  +                     G INTEGER NOT NULL,
           32  +                     H INTEGER NOT NULL,
           33  +                     PRIMARY KEY (B, C, D));
           34  +    
           35  +    CREATE TABLE T2 (A INTEGER NOT NULL,
           36  +                     B INTEGER NOT NULL,
           37  +                     C INTEGER NOT NULL,
           38  +                     PRIMARY KEY (A, B, C));
           39  +    
           40  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15527);
           41  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15560);
           42  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15561);
           43  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15563);
           44  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15564);
           45  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15566);
           46  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15567);
           47  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15569);
           48  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15612);
           49  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15613);
           50  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15638);
           51  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15681);
           52  +    INSERT INTO T2(A, B, C) VALUES(702118,16183,15682);
           53  +    
           54  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15527,6,0,5,5,0);
           55  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15560,6,0,5,2,0);
           56  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15561,6,0,5,2,0);
           57  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15563,6,0,5,2,0);
           58  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15564,6,0,5,2,0);
           59  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15566,6,0,5,2,0);
           60  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15567,6,0,5,2,0);
           61  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15569,6,0,5,2,0);
           62  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15612,6,0,5,5,0);
           63  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15613,6,0,5,2,0);
           64  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15638,6,0,5,2,0);
           65  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15681,6,0,5,5,0);
           66  +    INSERT INTO T1(B, C, D, E, F, G, H) VALUES(16183,15682,6,0,5,2,0);
           67  +  }
           68  +} {}
           69  +do_test contrib01-1.1 {
           70  +  db eval {
           71  +    SELECT T2.A, T2.B, T1.D, T1.E, T1.F, T1.G, T1.H, MAX(T1.C), '^'
           72  +      FROM T1, T2
           73  +     WHERE T1.B = T2.B
           74  +       AND T1.C = T2.C
           75  +     GROUP BY T2.A, T2.B, T1.D, T1.E, T1.F, T1.G, T1.H
           76  +     ORDER BY +max(t1.c);
           77  +  }
           78  +} {702118 16183 6 0 5 5 0 15681 ^ 702118 16183 6 0 5 2 0 15682 ^}
           79  +do_test contrib01-1.2 {
           80  +  db eval {
           81  +   SELECT T2.A, T2.B, T1.D, T1.E, T1.F, T1.G, T1.H, MAX(T1.C), '^'
           82  +     FROM T1, T2
           83  +    WHERE T1.B = T2.B
           84  +      AND T1.C = T2.C
           85  +    GROUP BY T2.A, T2.B, T1.F, T1.D, T1.E, T1.G, T1.H
           86  +    ORDER BY +max(t1.c);
           87  +  }
           88  +} {702118 16183 6 0 5 5 0 15681 ^ 702118 16183 6 0 5 2 0 15682 ^}
           89  +
           90  +finish_test

Changes to test/corruptF.test.

   143    143         set res ""
   144    144       }
   145    145       set res
   146    146     } {}
   147    147   }
   148    148   
   149    149   finish_test
   150         -

Changes to test/descidx1.test.

   193    193     do_test descidx1-4.2 {
   194    194       execsql {
   195    195         SELECT d FROM t2 ORDER BY a;
   196    196       }
   197    197     } {1.0 2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
   198    198     do_test descidx1-4.3 {
   199    199       execsql {
   200         -      SELECT d FROM t2 WHERE a>=2;
          200  +      SELECT d FROM t2 WHERE a>=2 ORDER BY a;
   201    201       }
   202    202     } {2.2 2.0 2.1 2.3 3.0 4.0 5.0 6.0}
   203    203     do_test descidx1-4.4 {
   204    204       execsql {
   205         -      SELECT d FROM t2 WHERE a>2;
          205  +      SELECT d FROM t2 WHERE a>2 ORDER BY a;
   206    206       }
   207    207     } {3.0 4.0 5.0 6.0}
   208    208     do_test descidx1-4.5 {
   209    209       execsql {
   210    210         SELECT d FROM t2 WHERE a=2 AND b>'two';
   211    211       }
   212    212     } {2.2}

Changes to test/distinct.test.

   161    161   
   162    162   foreach {tn sql temptables res} {
   163    163     1   "a, b FROM t1"                                       {}      {A B a b}
   164    164     2   "b, a FROM t1"                                       {}      {B A b a}
   165    165     3   "a, b, c FROM t1"                                    {hash}  {a b c A B C}
   166    166     4   "a, b, c FROM t1 ORDER BY a, b, c"                   {btree} {A B C a b c}
   167    167     5   "b FROM t1 WHERE a = 'a'"                            {}      {b}
   168         -  6   "b FROM t1"                                          {hash}  {b B}
          168  +  6   "b FROM t1 ORDER BY +b COLLATE binary"          {btree hash} {B b}
   169    169     7   "a FROM t1"                                          {}      {A a}
   170    170     8   "b COLLATE nocase FROM t1"                           {}      {b}
   171    171     9   "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {}      {b}
   172    172   } {
   173    173     do_execsql_test    2.$tn.1 "SELECT DISTINCT $sql" $res
   174    174     do_temptables_test 2.$tn.2 "SELECT DISTINCT $sql" $temptables
   175    175   }

Changes to test/e_createtable.test.

  1364   1364   #
  1365   1365   do_execsql_test 4.10.0 {
  1366   1366     CREATE TABLE t1(a, b PRIMARY KEY);
  1367   1367     CREATE TABLE t2(a, b, c, UNIQUE(b, c));
  1368   1368   }
  1369   1369   do_createtable_tests 4.10 {
  1370   1370     1    "EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 5" 
  1371         -       {0 0 0 {SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (b=?) (~1 rows)}}
         1371  +       {0 0 0 {SEARCH TABLE t1 USING INDEX sqlite_autoindex_t1_1 (b=?)}}
  1372   1372   
  1373   1373     2    "EXPLAIN QUERY PLAN SELECT * FROM t2 ORDER BY b, c"
  1374         -       {0 0 0 {SCAN TABLE t2 USING INDEX sqlite_autoindex_t2_1 (~1000000 rows)}}
         1374  +       {0 0 0 {SCAN TABLE t2 USING INDEX sqlite_autoindex_t2_1}}
  1375   1375   
  1376   1376     3    "EXPLAIN QUERY PLAN SELECT * FROM t2 WHERE b=10 AND c>10"
  1377         -       {0 0 0 {SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?) (~2 rows)}}
         1377  +       {0 0 0 {SEARCH TABLE t2 USING INDEX sqlite_autoindex_t2_1 (b=? AND c>?)}}
  1378   1378   }
  1379   1379   
  1380   1380   # EVIDENCE-OF: R-45493-35653 A CHECK constraint may be attached to a
  1381   1381   # column definition or specified as a table constraint. In practice it
  1382   1382   # makes no difference.
  1383   1383   #
  1384   1384   #   All the tests that deal with CHECK constraints below (4.11.* and 

Changes to test/e_fkey.test.

   970    970     }
   971    971   } {}
   972    972   do_execsql_test e_fkey-25.2 {
   973    973     PRAGMA foreign_keys = OFF;
   974    974     EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
   975    975     EXPLAIN QUERY PLAN SELECT rowid FROM track WHERE trackartist = ?;
   976    976   } {
   977         -  0 0 0 {SCAN TABLE artist (~1000000 rows)} 
   978         -  0 0 0 {SCAN TABLE track (~100000 rows)}
          977  +  0 0 0 {SCAN TABLE artist} 
          978  +  0 0 0 {SCAN TABLE track}
   979    979   }
   980    980   do_execsql_test e_fkey-25.3 {
   981    981     PRAGMA foreign_keys = ON;
   982    982     EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
   983    983   } {
   984         -  0 0 0 {SCAN TABLE artist (~1000000 rows)} 
   985         -  0 0 0 {SCAN TABLE track (~100000 rows)}
          984  +  0 0 0 {SCAN TABLE artist} 
          985  +  0 0 0 {SCAN TABLE track}
   986    986   }
   987    987   do_test e_fkey-25.4 {
   988    988     execsql {
   989    989       INSERT INTO artist VALUES(5, 'artist 5');
   990    990       INSERT INTO artist VALUES(6, 'artist 6');
   991    991       INSERT INTO artist VALUES(7, 'artist 7');
   992    992       INSERT INTO track VALUES(1, 'track 1', 5);
................................................................................
  1095   1095   } {}
  1096   1096   do_test e_fkey-27.2 {
  1097   1097     eqp { INSERT INTO artist VALUES(?, ?) }
  1098   1098   } {}
  1099   1099   do_execsql_test e_fkey-27.3 {
  1100   1100     EXPLAIN QUERY PLAN UPDATE artist SET artistid = ?, artistname = ?
  1101   1101   } {
  1102         -  0 0 0 {SCAN TABLE artist (~1000000 rows)} 
  1103         -  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?) (~10 rows)} 
  1104         -  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?) (~10 rows)}
         1102  +  0 0 0 {SCAN TABLE artist} 
         1103  +  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)} 
         1104  +  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
  1105   1105   }
  1106   1106   do_execsql_test e_fkey-27.4 {
  1107   1107     EXPLAIN QUERY PLAN DELETE FROM artist
  1108   1108   } {
  1109         -  0 0 0 {SCAN TABLE artist (~1000000 rows)} 
  1110         -  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?) (~10 rows)}
         1109  +  0 0 0 {SCAN TABLE artist} 
         1110  +  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
  1111   1111   }
  1112   1112   
  1113   1113   
  1114   1114   ###########################################################################
  1115   1115   ### SECTION 4.1: Composite Foreign Key Constraints
  1116   1116   ###########################################################################
  1117   1117   

Changes to test/eqp.test.

    39     39     CREATE TABLE t2(a, b);
    40     40     CREATE TABLE t3(a, b);
    41     41   }
    42     42   
    43     43   do_eqp_test 1.2 {
    44     44     SELECT * FROM t2, t1 WHERE t1.a=1 OR t1.b=2;
    45     45   } {
    46         -  0 0 1 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~10 rows)} 
    47         -  0 0 1 {SEARCH TABLE t1 USING INDEX i2 (b=?) (~10 rows)} 
    48         -  0 1 0 {SCAN TABLE t2 (~1000000 rows)}
           46  +  0 0 1 {SEARCH TABLE t1 USING INDEX i1 (a=?)} 
           47  +  0 0 1 {SEARCH TABLE t1 USING INDEX i2 (b=?)} 
           48  +  0 1 0 {SCAN TABLE t2}
    49     49   }
    50     50   do_eqp_test 1.3 {
    51     51     SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a=1 OR t1.b=2;
    52     52   } {
    53         -  0 0 0 {SCAN TABLE t2 (~1000000 rows)}
    54         -  0 1 1 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~10 rows)} 
    55         -  0 1 1 {SEARCH TABLE t1 USING INDEX i2 (b=?) (~10 rows)} 
           53  +  0 0 0 {SCAN TABLE t2}
           54  +  0 1 1 {SEARCH TABLE t1 USING INDEX i1 (a=?)} 
           55  +  0 1 1 {SEARCH TABLE t1 USING INDEX i2 (b=?)} 
    56     56   }
    57     57   do_eqp_test 1.3 {
    58     58     SELECT a FROM t1 ORDER BY a
    59     59   } {
    60         -  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
           60  +  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1}
    61     61   }
    62     62   do_eqp_test 1.4 {
    63     63     SELECT a FROM t1 ORDER BY +a
    64     64   } {
    65         -  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
           65  +  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1}
    66     66     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
    67     67   }
    68     68   do_eqp_test 1.5 {
    69     69     SELECT a FROM t1 WHERE a=4
    70     70   } {
    71         -  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?) (~10 rows)}
           71  +  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}
    72     72   }
    73     73   do_eqp_test 1.6 {
    74     74     SELECT DISTINCT count(*) FROM t3 GROUP BY a;
    75     75   } {
    76         -  0 0 0 {SCAN TABLE t3 (~1000000 rows)}
           76  +  0 0 0 {SCAN TABLE t3}
    77     77     0 0 0 {USE TEMP B-TREE FOR GROUP BY}
    78     78     0 0 0 {USE TEMP B-TREE FOR DISTINCT}
    79     79   }
    80     80   
    81     81   do_eqp_test 1.7 {
    82     82     SELECT * FROM t3 JOIN (SELECT 1)
    83     83   } {
    84         -  0 0 1 {SCAN SUBQUERY 1 (~1 rows)}
    85         -  0 1 0 {SCAN TABLE t3 (~1000000 rows)}
           84  +  0 0 1 {SCAN SUBQUERY 1}
           85  +  0 1 0 {SCAN TABLE t3}
    86     86   }
    87     87   do_eqp_test 1.8 {
    88     88     SELECT * FROM t3 JOIN (SELECT 1 UNION SELECT 2)
    89     89   } {
    90     90     1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)}
    91         -  0 0 1 {SCAN SUBQUERY 1 (~2 rows)}
    92         -  0 1 0 {SCAN TABLE t3 (~1000000 rows)}
           91  +  0 0 1 {SCAN SUBQUERY 1}
           92  +  0 1 0 {SCAN TABLE t3}
    93     93   }
    94     94   do_eqp_test 1.9 {
    95     95     SELECT * FROM t3 JOIN (SELECT 1 EXCEPT SELECT a FROM t3 LIMIT 17)
    96     96   } {
    97         -  3 0 0 {SCAN TABLE t3 (~1000000 rows)}
           97  +  3 0 0 {SCAN TABLE t3}
    98     98     1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (EXCEPT)}
    99         -  0 0 1 {SCAN SUBQUERY 1 (~17 rows)}
   100         -  0 1 0 {SCAN TABLE t3 (~1000000 rows)}
           99  +  0 0 1 {SCAN SUBQUERY 1}
          100  +  0 1 0 {SCAN TABLE t3}
   101    101   }
   102    102   do_eqp_test 1.10 {
   103    103     SELECT * FROM t3 JOIN (SELECT 1 INTERSECT SELECT a FROM t3 LIMIT 17)
   104    104   } {
   105         -  3 0 0 {SCAN TABLE t3 (~1000000 rows)}
          105  +  3 0 0 {SCAN TABLE t3}
   106    106     1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (INTERSECT)}
   107         -  0 0 1 {SCAN SUBQUERY 1 (~1 rows)}
   108         -  0 1 0 {SCAN TABLE t3 (~1000000 rows)}
          107  +  0 0 1 {SCAN SUBQUERY 1}
          108  +  0 1 0 {SCAN TABLE t3}
   109    109   }
   110    110   
   111    111   do_eqp_test 1.11 {
   112    112     SELECT * FROM t3 JOIN (SELECT 1 UNION ALL SELECT a FROM t3 LIMIT 17)
   113    113   } {
   114         -  3 0 0 {SCAN TABLE t3 (~1000000 rows)}
          114  +  3 0 0 {SCAN TABLE t3}
   115    115     1 0 0 {COMPOUND SUBQUERIES 2 AND 3 (UNION ALL)}
   116         -  0 0 1 {SCAN SUBQUERY 1 (~17 rows)}
   117         -  0 1 0 {SCAN TABLE t3 (~1000000 rows)}
          116  +  0 0 1 {SCAN SUBQUERY 1}
          117  +  0 1 0 {SCAN TABLE t3}
   118    118   }
   119    119   
   120    120   #-------------------------------------------------------------------------
   121    121   # Test cases eqp-2.* - tests for single select statements.
   122    122   #
   123    123   drop_all_tables
   124    124   do_execsql_test 2.1 {
................................................................................
   125    125     CREATE TABLE t1(x, y);
   126    126   
   127    127     CREATE TABLE t2(x, y);
   128    128     CREATE INDEX t2i1 ON t2(x);
   129    129   }
   130    130   
   131    131   det 2.2.1 "SELECT DISTINCT min(x), max(x) FROM t1 GROUP BY x ORDER BY 1" {
   132         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
          132  +  0 0 0 {SCAN TABLE t1}
   133    133     0 0 0 {USE TEMP B-TREE FOR GROUP BY}
   134    134     0 0 0 {USE TEMP B-TREE FOR DISTINCT}
   135    135     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   136    136   }
   137    137   det 2.2.2 "SELECT DISTINCT min(x), max(x) FROM t2 GROUP BY x ORDER BY 1" {
   138         -  0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
          138  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
   139    139     0 0 0 {USE TEMP B-TREE FOR DISTINCT}
   140    140     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   141    141   }
   142    142   det 2.2.3 "SELECT DISTINCT * FROM t1" {
   143         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
          143  +  0 0 0 {SCAN TABLE t1}
   144    144     0 0 0 {USE TEMP B-TREE FOR DISTINCT}
   145    145   }
   146    146   det 2.2.4 "SELECT DISTINCT * FROM t1, t2" {
   147         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
   148         -  0 1 1 {SCAN TABLE t2 (~1000000 rows)}
          147  +  0 0 0 {SCAN TABLE t1}
          148  +  0 1 1 {SCAN TABLE t2}
   149    149     0 0 0 {USE TEMP B-TREE FOR DISTINCT}
   150    150   }
   151    151   det 2.2.5 "SELECT DISTINCT * FROM t1, t2 ORDER BY t1.x" {
   152         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
   153         -  0 1 1 {SCAN TABLE t2 (~1000000 rows)}
          152  +  0 0 0 {SCAN TABLE t1}
          153  +  0 1 1 {SCAN TABLE t2}
   154    154     0 0 0 {USE TEMP B-TREE FOR DISTINCT}
   155    155     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   156    156   }
   157    157   det 2.2.6 "SELECT DISTINCT t2.x FROM t1, t2 ORDER BY t2.x" {
   158         -  0 0 1 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
   159         -  0 1 0 {SCAN TABLE t1 (~1000000 rows)}
          158  +  0 0 1 {SCAN TABLE t2 USING COVERING INDEX t2i1}
          159  +  0 1 0 {SCAN TABLE t1}
   160    160   }
   161    161   
   162    162   det 2.3.1 "SELECT max(x) FROM t2" {
   163         -  0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1 (~1 rows)}
          163  +  0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1}
   164    164   }
   165    165   det 2.3.2 "SELECT min(x) FROM t2" {
   166         -  0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1 (~1 rows)}
          166  +  0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1}
   167    167   }
   168    168   det 2.3.3 "SELECT min(x), max(x) FROM t2" {
   169         -  0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
          169  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
   170    170   }
   171    171   
   172    172   det 2.4.1 "SELECT * FROM t1 WHERE rowid=?" {
   173         -  0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
          173  +  0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?)}
   174    174   }
   175    175   
   176    176   
   177    177   
   178    178   #-------------------------------------------------------------------------
   179    179   # Test cases eqp-3.* - tests for select statements that use sub-selects.
   180    180   #
   181    181   do_eqp_test 3.1.1 {
   182    182     SELECT (SELECT x FROM t1 AS sub) FROM t1;
   183    183   } {
   184         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
          184  +  0 0 0 {SCAN TABLE t1}
   185    185     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   186         -  1 0 0 {SCAN TABLE t1 AS sub (~1000000 rows)}
          186  +  1 0 0 {SCAN TABLE t1 AS sub}
   187    187   }
   188    188   do_eqp_test 3.1.2 {
   189    189     SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub);
   190    190   } {
   191         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
          191  +  0 0 0 {SCAN TABLE t1}
   192    192     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   193         -  1 0 0 {SCAN TABLE t1 AS sub (~1000000 rows)}
          193  +  1 0 0 {SCAN TABLE t1 AS sub}
   194    194   }
   195    195   do_eqp_test 3.1.3 {
   196    196     SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub ORDER BY y);
   197    197   } {
   198         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
          198  +  0 0 0 {SCAN TABLE t1}
   199    199     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   200         -  1 0 0 {SCAN TABLE t1 AS sub (~1000000 rows)}
          200  +  1 0 0 {SCAN TABLE t1 AS sub}
   201    201     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   202    202   }
   203    203   do_eqp_test 3.1.4 {
   204    204     SELECT * FROM t1 WHERE (SELECT x FROM t2 ORDER BY x);
   205    205   } {
   206         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
          206  +  0 0 0 {SCAN TABLE t1}
   207    207     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   208         -  1 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
          208  +  1 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
   209    209   }
   210    210   
   211    211   det 3.2.1 {
   212    212     SELECT * FROM (SELECT * FROM t1 ORDER BY x LIMIT 10) ORDER BY y LIMIT 5
   213    213   } {
   214         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          214  +  1 0 0 {SCAN TABLE t1} 
   215    215     1 0 0 {USE TEMP B-TREE FOR ORDER BY} 
   216         -  0 0 0 {SCAN SUBQUERY 1 (~10 rows)} 
          216  +  0 0 0 {SCAN SUBQUERY 1} 
   217    217     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   218    218   }
   219    219   det 3.2.2 {
   220    220     SELECT * FROM 
   221    221       (SELECT * FROM t1 ORDER BY x LIMIT 10) AS x1,
   222    222       (SELECT * FROM t2 ORDER BY x LIMIT 10) AS x2
   223    223     ORDER BY x2.y LIMIT 5
   224    224   } {
   225         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          225  +  1 0 0 {SCAN TABLE t1} 
   226    226     1 0 0 {USE TEMP B-TREE FOR ORDER BY} 
   227         -  2 0 0 {SCAN TABLE t2 USING INDEX t2i1 (~1000000 rows)} 
   228         -  0 0 0 {SCAN SUBQUERY 1 AS x1 (~10 rows)} 
   229         -  0 1 1 {SCAN SUBQUERY 2 AS x2 (~10 rows)} 
          227  +  2 0 0 {SCAN TABLE t2 USING INDEX t2i1} 
          228  +  0 0 0 {SCAN SUBQUERY 1 AS x1} 
          229  +  0 1 1 {SCAN SUBQUERY 2 AS x2} 
   230    230     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   231    231   }
   232    232   
   233    233   det 3.3.1 {
   234    234     SELECT * FROM t1 WHERE y IN (SELECT y FROM t2)
   235    235   } {
   236         -  0 0 0 {SCAN TABLE t1 (~100000 rows)} 
          236  +  0 0 0 {SCAN TABLE t1} 
   237    237     0 0 0 {EXECUTE LIST SUBQUERY 1} 
   238         -  1 0 0 {SCAN TABLE t2 (~1000000 rows)}
          238  +  1 0 0 {SCAN TABLE t2}
   239    239   }
   240    240   det 3.3.2 {
   241    241     SELECT * FROM t1 WHERE y IN (SELECT y FROM t2 WHERE t1.x!=t2.x)
   242    242   } {
   243         -  0 0 0 {SCAN TABLE t1 (~500000 rows)} 
          243  +  0 0 0 {SCAN TABLE t1} 
   244    244     0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1} 
   245         -  1 0 0 {SCAN TABLE t2 (~500000 rows)}
          245  +  1 0 0 {SCAN TABLE t2}
   246    246   }
   247    247   det 3.3.3 {
   248    248     SELECT * FROM t1 WHERE EXISTS (SELECT y FROM t2 WHERE t1.x!=t2.x)
   249    249   } {
   250         -  0 0 0 {SCAN TABLE t1 (~500000 rows)} 
          250  +  0 0 0 {SCAN TABLE t1} 
   251    251     0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 1} 
   252         -  1 0 0 {SCAN TABLE t2 (~500000 rows)}
          252  +  1 0 0 {SCAN TABLE t2}
   253    253   }
   254    254   
   255    255   #-------------------------------------------------------------------------
   256    256   # Test cases eqp-4.* - tests for composite select statements.
   257    257   #
   258    258   do_eqp_test 4.1.1 {
   259    259     SELECT * FROM t1 UNION ALL SELECT * FROM t2
   260    260   } {
   261         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
   262         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          261  +  1 0 0 {SCAN TABLE t1} 
          262  +  2 0 0 {SCAN TABLE t2} 
   263    263     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)} 
   264    264   }
   265    265   do_eqp_test 4.1.2 {
   266    266     SELECT * FROM t1 UNION ALL SELECT * FROM t2 ORDER BY 2
   267    267   } {
   268         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          268  +  1 0 0 {SCAN TABLE t1} 
   269    269     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   270         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          270  +  2 0 0 {SCAN TABLE t2} 
   271    271     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   272    272     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)} 
   273    273   }
   274    274   do_eqp_test 4.1.3 {
   275    275     SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY 2
   276    276   } {
   277         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          277  +  1 0 0 {SCAN TABLE t1} 
   278    278     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   279         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          279  +  2 0 0 {SCAN TABLE t2} 
   280    280     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   281    281     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION)} 
   282    282   }
   283    283   do_eqp_test 4.1.4 {
   284    284     SELECT * FROM t1 INTERSECT SELECT * FROM t2 ORDER BY 2
   285    285   } {
   286         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          286  +  1 0 0 {SCAN TABLE t1} 
   287    287     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   288         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          288  +  2 0 0 {SCAN TABLE t2} 
   289    289     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   290    290     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (INTERSECT)} 
   291    291   }
   292    292   do_eqp_test 4.1.5 {
   293    293     SELECT * FROM t1 EXCEPT SELECT * FROM t2 ORDER BY 2
   294    294   } {
   295         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          295  +  1 0 0 {SCAN TABLE t1} 
   296    296     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   297         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          297  +  2 0 0 {SCAN TABLE t2} 
   298    298     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   299    299     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)} 
   300    300   }
   301    301   
   302    302   do_eqp_test 4.2.2 {
   303    303     SELECT * FROM t1 UNION ALL SELECT * FROM t2 ORDER BY 1
   304    304   } {
   305         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          305  +  1 0 0 {SCAN TABLE t1} 
   306    306     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   307         -  2 0 0 {SCAN TABLE t2 USING INDEX t2i1 (~1000000 rows)} 
          307  +  2 0 0 {SCAN TABLE t2 USING INDEX t2i1} 
   308    308     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)} 
   309    309   }
   310    310   do_eqp_test 4.2.3 {
   311    311     SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY 1
   312    312   } {
   313         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          313  +  1 0 0 {SCAN TABLE t1} 
   314    314     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   315         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          315  +  2 0 0 {SCAN TABLE t2} 
   316    316     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   317    317     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION)} 
   318    318   }
   319    319   do_eqp_test 4.2.4 {
   320    320     SELECT * FROM t1 INTERSECT SELECT * FROM t2 ORDER BY 1
   321    321   } {
   322         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          322  +  1 0 0 {SCAN TABLE t1} 
   323    323     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   324         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          324  +  2 0 0 {SCAN TABLE t2} 
   325    325     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   326    326     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (INTERSECT)} 
   327    327   }
   328    328   do_eqp_test 4.2.5 {
   329    329     SELECT * FROM t1 EXCEPT SELECT * FROM t2 ORDER BY 1
   330    330   } {
   331         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          331  +  1 0 0 {SCAN TABLE t1} 
   332    332     1 0 0 {USE TEMP B-TREE FOR ORDER BY}
   333         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          333  +  2 0 0 {SCAN TABLE t2} 
   334    334     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   335    335     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)} 
   336    336   }
   337    337   
   338    338   do_eqp_test 4.3.1 {
   339    339     SELECT x FROM t1 UNION SELECT x FROM t2
   340    340   } {
   341         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
   342         -  2 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)} 
          341  +  1 0 0 {SCAN TABLE t1} 
          342  +  2 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} 
   343    343     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)} 
   344    344   }
   345    345   
   346    346   do_eqp_test 4.3.2 {
   347    347     SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1
   348    348   } {
   349         -  2 0 0 {SCAN TABLE t1 (~1000000 rows)} 
   350         -  3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)} 
          349  +  2 0 0 {SCAN TABLE t1} 
          350  +  3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} 
   351    351     1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)}
   352         -  4 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          352  +  4 0 0 {SCAN TABLE t1} 
   353    353     0 0 0 {COMPOUND SUBQUERIES 1 AND 4 USING TEMP B-TREE (UNION)}
   354    354   }
   355    355   do_eqp_test 4.3.3 {
   356    356     SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1 ORDER BY 1
   357    357   } {
   358         -  2 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          358  +  2 0 0 {SCAN TABLE t1} 
   359    359     2 0 0 {USE TEMP B-TREE FOR ORDER BY} 
   360         -  3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)} 
          360  +  3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1} 
   361    361     1 0 0 {COMPOUND SUBQUERIES 2 AND 3 (UNION)} 
   362         -  4 0 0 {SCAN TABLE t1 (~1000000 rows)} 
          362  +  4 0 0 {SCAN TABLE t1} 
   363    363     4 0 0 {USE TEMP B-TREE FOR ORDER BY} 
   364    364     0 0 0 {COMPOUND SUBQUERIES 1 AND 4 (UNION)}
   365    365   }
   366    366   
   367    367   #-------------------------------------------------------------------------
   368    368   # This next block of tests verifies that the examples on the 
   369    369   # lang_explain.html page are correct.
   370    370   #
   371    371   drop_all_tables
   372    372   
   373    373   # EVIDENCE-OF: R-64208-08323 sqlite> EXPLAIN QUERY PLAN SELECT a, b
   374         -# FROM t1 WHERE a=1; 0|0|0|SCAN TABLE t1 (~100000 rows)
          374  +# FROM t1 WHERE a=1; 0|0|0|SCAN TABLE t1
   375    375   do_execsql_test 5.1.0 { CREATE TABLE t1(a, b) }
   376    376   det 5.1.1 "SELECT a, b FROM t1 WHERE a=1" {
   377         -  0 0 0 {SCAN TABLE t1 (~100000 rows)}
          377  +  0 0 0 {SCAN TABLE t1}
   378    378   }
   379    379   
   380    380   # EVIDENCE-OF: R-09022-44606 sqlite> CREATE INDEX i1 ON t1(a);
   381    381   # sqlite> EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
   382         -# 0|0|0|SEARCH TABLE t1 USING INDEX i1 (a=?) (~10 rows)
          382  +# 0|0|0|SEARCH TABLE t1 USING INDEX i1 (a=?)
   383    383   do_execsql_test 5.2.0 { CREATE INDEX i1 ON t1(a) }
   384    384   det 5.2.1 "SELECT a, b FROM t1 WHERE a=1" {
   385         -  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~10 rows)}
          385  +  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}
   386    386   }
   387    387   
   388    388   # EVIDENCE-OF: R-62228-34103 sqlite> CREATE INDEX i2 ON t1(a, b);
   389    389   # sqlite> EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
   390         -# 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)
          390  +# 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)
   391    391   do_execsql_test 5.3.0 { CREATE INDEX i2 ON t1(a, b) }
   392    392   det 5.3.1 "SELECT a, b FROM t1 WHERE a=1" {
   393         -  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)}
          393  +  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)}
   394    394   }
   395    395   
   396    396   # EVIDENCE-OF: R-22253-05302 sqlite> EXPLAIN QUERY PLAN SELECT t1.*,
   397    397   # t2.* FROM t1, t2 WHERE t1.a=1 AND t1.b>2; 0|0|0|SEARCH TABLE t1
   398         -# USING COVERING INDEX i2 (a=? AND b>?) (~3 rows) 0|1|1|SCAN TABLE t2
   399         -# (~1000000 rows)
          398  +# USING COVERING INDEX i2 (a=? AND b>?) 0|1|1|SCAN TABLE t2
          399  +#
   400    400   do_execsql_test 5.4.0 {CREATE TABLE t2(c, d)}
   401    401   det 5.4.1 "SELECT t1.*, t2.* FROM t1, t2 WHERE t1.a=1 AND t1.b>2" {
   402         -  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?) (~2 rows)}
   403         -  0 1 1 {SCAN TABLE t2 (~1000000 rows)}
          402  +  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)}
          403  +  0 1 1 {SCAN TABLE t2}
   404    404   }
   405    405   
   406    406   # EVIDENCE-OF: R-21040-07025 sqlite> EXPLAIN QUERY PLAN SELECT t1.*,
   407    407   # t2.* FROM t2, t1 WHERE t1.a=1 AND t1.b>2; 0|0|1|SEARCH TABLE t1
   408         -# USING COVERING INDEX i2 (a=? AND b>?) (~3 rows) 0|1|0|SCAN TABLE t2
   409         -# (~1000000 rows)
          408  +# USING COVERING INDEX i2 (a=? AND b>?) 0|1|0|SCAN TABLE t2
          409  +#
   410    410   det 5.5 "SELECT t1.*, t2.* FROM t2, t1 WHERE t1.a=1 AND t1.b>2" {
   411         -  0 0 1 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?) (~2 rows)}
   412         -  0 1 0 {SCAN TABLE t2 (~1000000 rows)}
          411  +  0 0 1 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)}
          412  +  0 1 0 {SCAN TABLE t2}
   413    413   }
   414    414   
   415    415   # EVIDENCE-OF: R-39007-61103 sqlite> CREATE INDEX i3 ON t1(b);
   416    416   # sqlite> EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=1 OR b=2;
   417         -# 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)
   418         -# 0|0|0|SEARCH TABLE t1 USING INDEX i3 (b=?) (~10 rows)
          417  +# 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)
          418  +# 0|0|0|SEARCH TABLE t1 USING INDEX i3 (b=?)
   419    419   do_execsql_test 5.5.0 {CREATE INDEX i3 ON t1(b)}
   420    420   det 5.6.1 "SELECT * FROM t1 WHERE a=1 OR b=2" {
   421         -  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)}
   422         -  0 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?) (~10 rows)}
          421  +  0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)}
          422  +  0 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?)}
   423    423   }
   424    424   
   425    425   # EVIDENCE-OF: R-33025-54904 sqlite> EXPLAIN QUERY PLAN SELECT c, d
   426         -# FROM t2 ORDER BY c; 0|0|0|SCAN TABLE t2 (~1000000 rows) 0|0|0|USE TEMP
          426  +# FROM t2 ORDER BY c; 0|0|0|SCAN TABLE t2 0|0|0|USE TEMP
   427    427   # B-TREE FOR ORDER BY
   428    428   det 5.7 "SELECT c, d FROM t2 ORDER BY c" {
   429         -  0 0 0 {SCAN TABLE t2 (~1000000 rows)}
          429  +  0 0 0 {SCAN TABLE t2}
   430    430     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
   431    431   }
   432    432   
   433    433   # EVIDENCE-OF: R-38854-22809 sqlite> CREATE INDEX i4 ON t2(c);
   434    434   # sqlite> EXPLAIN QUERY PLAN SELECT c, d FROM t2 ORDER BY c;
   435         -# 0|0|0|SCAN TABLE t2 USING INDEX i4 (~1000000 rows)
          435  +# 0|0|0|SCAN TABLE t2 USING INDEX i4
   436    436   do_execsql_test 5.8.0 {CREATE INDEX i4 ON t2(c)}
   437    437   det 5.8.1 "SELECT c, d FROM t2 ORDER BY c" {
   438         -  0 0 0 {SCAN TABLE t2 USING INDEX i4 (~1000000 rows)}
          438  +  0 0 0 {SCAN TABLE t2 USING INDEX i4}
   439    439   }
   440    440   
   441    441   # EVIDENCE-OF: R-29884-43993 sqlite> EXPLAIN QUERY PLAN SELECT
   442    442   # (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2;
   443         -# 0|0|0|SCAN TABLE t2 (~1000000 rows) 0|0|0|EXECUTE SCALAR SUBQUERY 1
   444         -# 1|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)
          443  +# 0|0|0|SCAN TABLE t2 0|0|0|EXECUTE SCALAR SUBQUERY 1
          444  +# 1|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)
   445    445   # 0|0|0|EXECUTE CORRELATED SCALAR SUBQUERY 2 2|0|0|SEARCH TABLE t1 USING
   446         -# INDEX i3 (b=?) (~10 rows)
          446  +# INDEX i3 (b=?)
   447    447   det 5.9 {
   448    448     SELECT (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2
   449    449   } {
   450         -  0 0 0 {SCAN TABLE t2 USING COVERING INDEX i4 (~1000000 rows)}
          450  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX i4}
   451    451     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   452         -  1 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)}
          452  +  1 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)}
   453    453     0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2}
   454         -  2 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?) (~10 rows)}
          454  +  2 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?)}
   455    455   }
   456    456   
   457    457   # EVIDENCE-OF: R-17911-16445 sqlite> EXPLAIN QUERY PLAN SELECT
   458    458   # count(*) FROM (SELECT max(b) AS x FROM t1 GROUP BY a) GROUP BY x;
   459         -# 1|0|0|SCAN TABLE t1 USING COVERING INDEX i2 (~1000000 rows) 0|0|0|SCAN
   460         -# SUBQUERY 1 (~1000000 rows) 0|0|0|USE TEMP B-TREE FOR GROUP BY
          459  +# 1|0|0|SCAN TABLE t1 USING COVERING INDEX i2 0|0|0|SCAN
          460  +# SUBQUERY 1 0|0|0|USE TEMP B-TREE FOR GROUP BY
   461    461   det 5.10 {
   462    462     SELECT count(*) FROM (SELECT max(b) AS x FROM t1 GROUP BY a) GROUP BY x
   463    463   } {
   464         -  1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2 (~1000000 rows)}
   465         -  0 0 0 {SCAN SUBQUERY 1 (~100 rows)}
          464  +  1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2}
          465  +  0 0 0 {SCAN SUBQUERY 1}
   466    466     0 0 0 {USE TEMP B-TREE FOR GROUP BY}
   467    467   }
   468    468   
   469    469   # EVIDENCE-OF: R-18544-33103 sqlite> EXPLAIN QUERY PLAN SELECT * FROM
   470    470   # (SELECT * FROM t2 WHERE c=1), t1; 0|0|0|SEARCH TABLE t2 USING INDEX i4
   471         -# (c=?) (~10 rows) 0|1|1|SCAN TABLE t1 (~1000000 rows)
          471  +# (c=?) 0|1|1|SCAN TABLE t1
   472    472   det 5.11 "SELECT * FROM (SELECT * FROM t2 WHERE c=1), t1" {
   473         -  0 0 0 {SEARCH TABLE t2 USING INDEX i4 (c=?) (~10 rows)}
   474         -  0 1 1 {SCAN TABLE t1 USING COVERING INDEX i2 (~1000000 rows)}
          473  +  0 0 0 {SEARCH TABLE t2 USING INDEX i4 (c=?)}
          474  +  0 1 1 {SCAN TABLE t1 USING COVERING INDEX i2}
   475    475   }
   476    476   
   477    477   # EVIDENCE-OF: R-40701-42164 sqlite> EXPLAIN QUERY PLAN SELECT a FROM
   478         -# t1 UNION SELECT c FROM t2; 1|0|0|SCAN TABLE t1 (~1000000 rows)
   479         -# 2|0|0|SCAN TABLE t2 (~1000000 rows) 0|0|0|COMPOUND SUBQUERIES 1 AND 2
          478  +# t1 UNION SELECT c FROM t2; 1|0|0|SCAN TABLE t1
          479  +# 2|0|0|SCAN TABLE t2 0|0|0|COMPOUND SUBQUERIES 1 AND 2
   480    480   # USING TEMP B-TREE (UNION)
   481    481   det 5.12 "SELECT a FROM t1 UNION SELECT c FROM t2" {
   482         -  1 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
   483         -  2 0 0 {SCAN TABLE t2 USING COVERING INDEX i4 (~1000000 rows)}
          482  +  1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2}
          483  +  2 0 0 {SCAN TABLE t2 USING COVERING INDEX i4}
   484    484     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)}
   485    485   }
   486    486   
   487    487   # EVIDENCE-OF: R-61538-24748 sqlite> EXPLAIN QUERY PLAN SELECT a FROM
   488    488   # t1 EXCEPT SELECT d FROM t2 ORDER BY 1; 1|0|0|SCAN TABLE t1 USING
   489         -# COVERING INDEX i2 (~1000000 rows) 2|0|0|SCAN TABLE t2 (~1000000 rows)
          489  +# COVERING INDEX i2 2|0|0|SCAN TABLE t2
   490    490   # 2|0|0|USE TEMP B-TREE FOR ORDER BY 0|0|0|COMPOUND SUBQUERIES 1 AND 2
   491    491   # (EXCEPT)
   492    492   det 5.13 "SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1" {
   493         -  1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2 (~1000000 rows)}
   494         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)}
          493  +  1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2}
          494  +  2 0 0 {SCAN TABLE t2}
   495    495     2 0 0 {USE TEMP B-TREE FOR ORDER BY}
   496    496     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)}
   497    497   }
   498    498   
   499    499   
   500    500   #-------------------------------------------------------------------------
   501    501   # The following tests - eqp-6.* - test that the example C code on 
................................................................................
   527    527       set data
   528    528     }] [list $res]
   529    529   }
   530    530   
   531    531   do_peqp_test 6.1 {
   532    532     SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1
   533    533   } [string trimleft {
   534         -1 0 0 SCAN TABLE t1 USING COVERING INDEX i2 (~1000000 rows)
   535         -2 0 0 SCAN TABLE t2 (~1000000 rows)
          534  +1 0 0 SCAN TABLE t1 USING COVERING INDEX i2
          535  +2 0 0 SCAN TABLE t2
   536    536   2 0 0 USE TEMP B-TREE FOR ORDER BY
   537    537   0 0 0 COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)
   538    538   }]
   539    539   
   540    540   #-------------------------------------------------------------------------
   541    541   # The following tests - eqp-7.* - test that queries that use the OP_Count
   542    542   # optimization return something sensible with EQP.
................................................................................
   546    546   do_execsql_test 7.0 {
   547    547     CREATE TABLE t1(a, b);
   548    548     CREATE TABLE t2(a, b);
   549    549     CREATE INDEX i1 ON t2(a);
   550    550   }
   551    551   
   552    552   det 7.1 "SELECT count(*) FROM t1" {
   553         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
          553  +  0 0 0 {SCAN TABLE t1}
   554    554   }
   555    555   
   556    556   det 7.2 "SELECT count(*) FROM t2" {
   557         -  0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1(~1000000 rows)}
          557  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1}
   558    558   }
   559    559   
   560    560   do_execsql_test 7.3 {
   561    561     INSERT INTO t1 VALUES(1, 2);
   562    562     INSERT INTO t1 VALUES(3, 4);
   563    563   
   564    564     INSERT INTO t2 VALUES(1, 2);
................................................................................
   568    568     ANALYZE;
   569    569   }
   570    570   
   571    571   db close
   572    572   sqlite3 db test.db
   573    573   
   574    574   det 7.4 "SELECT count(*) FROM t1" {
   575         -  0 0 0 {SCAN TABLE t1 (~2 rows)}
          575  +  0 0 0 {SCAN TABLE t1}
   576    576   }
   577    577   
   578    578   det 7.5 "SELECT count(*) FROM t2" {
   579         -  0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1(~3 rows)}
          579  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1}
   580    580   }
   581    581   
   582    582   
   583    583   finish_test

Changes to test/exclusive.test.

   502    502   
   503    503   do_execsql_test exclusive-6.5 {
   504    504     PRAGMA locking_mode = EXCLUSIVE;
   505    505     SELECT * FROM sqlite_master;
   506    506   } {exclusive}
   507    507   
   508    508   finish_test
   509         -

Changes to test/fallocate.test.

   139    139       execsql { PRAGMA wal_checkpoint }
   140    140       file size test.db
   141    141     } [expr 32*1024]
   142    142   }
   143    143   
   144    144   
   145    145   finish_test
   146         -

Changes to test/filefmt.test.

   244    244   do_test filefmt-4.4 { 
   245    245     sqlite3 db2 bak.db
   246    246     db2 eval { PRAGMA integrity_check }
   247    247   } {ok}
   248    248   db2 close
   249    249   
   250    250   finish_test
   251         -

Changes to test/fts3aa.test.

   220    220   } {}
   221    221   do_catchsql_test fts3aa-7.5 {
   222    222     CREATE VIRTUAL TABLE t4 USING fts4(tokenize=simple, tokenize=simple);
   223    223   } {1 {unrecognized parameter: tokenize=simple}}
   224    224   
   225    225   
   226    226   finish_test
   227         -

Changes to test/fts3ao.test.

   216    216   do_execsql_test 5.2 {
   217    217     ALTER TABLE t7 RENAME TO t8;
   218    218     SELECT count(*) FROM sqlite_master WHERE name LIKE 't7%';
   219    219     SELECT count(*) FROM sqlite_master WHERE name LIKE 't8%';
   220    220   } {0 6}
   221    221   
   222    222   finish_test
   223         -

Changes to test/fts3atoken.test.

   189    189   
   190    190   do_test fts3token-internal {
   191    191     execsql { SELECT fts3_tokenizer_internal_test() }
   192    192   } {ok}
   193    193   
   194    194   
   195    195   finish_test
   196         -
   197         -

Changes to test/fts3auto.test.

   703    703     do_fts3query_test 7.$tn.1             t1 {"M B"}
   704    704     do_fts3query_test 7.$tn.2             t1 {"B D"}
   705    705     do_fts3query_test 7.$tn.3 -deferred B t1 {"M B D"}
   706    706   }
   707    707   
   708    708   set sqlite_fts3_enable_parentheses $sfep
   709    709   finish_test
   710         -

Changes to test/fts3aux1.test.

   101    101   db func rec rec
   102    102   
   103    103   # Use EQP to show that the WHERE expression "term='braid'" uses a different
   104    104   # index number (1) than "+term='braid'" (0).
   105    105   #
   106    106   do_execsql_test 2.1.1.1 {
   107    107     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term='braid'
   108         -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 1: (~0 rows)} }
          108  +} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 1:} }
   109    109   do_execsql_test 2.1.1.2 {
   110    110     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term='braid'
   111         -} {0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0: (~0 rows)}}
          111  +} {0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:}}
   112    112   
   113    113   # Now show that using "term='braid'" means the virtual table returns
   114    114   # only 1 row to SQLite, but "+term='braid'" means all 19 are returned.
   115    115   #
   116    116   do_test 2.1.2.1 {
   117    117     set cnt 0
   118    118     execsql { SELECT * FROM terms_v WHERE rec('cnt', term) AND term='braid' }
................................................................................
   150    150   
   151    151   # Special case: term=NULL
   152    152   #
   153    153   do_execsql_test 2.1.5 { SELECT * FROM terms WHERE term=NULL } {}
   154    154   
   155    155   do_execsql_test 2.2.1.1 {
   156    156     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term>'brain'
   157         -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 2: (~0 rows)} }
          157  +} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 2:} }
   158    158   do_execsql_test 2.2.1.2 {
   159    159     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term>'brain'
   160         -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0: (~0 rows)} }
          160  +} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} }
   161    161   
   162    162   do_execsql_test 2.2.1.3 {
   163    163     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term<'brain'
   164         -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 4: (~0 rows)} }
          164  +} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 4:} }
   165    165   do_execsql_test 2.2.1.4 {
   166    166     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term<'brain'
   167         -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0: (~0 rows)} }
          167  +} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} }
   168    168   
   169    169   do_execsql_test 2.2.1.5 {
   170    170     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE term BETWEEN 'brags' AND 'brain'
   171         -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 6: (~0 rows)} }
          171  +} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 6:} }
   172    172   do_execsql_test 2.2.1.6 {
   173    173     EXPLAIN QUERY PLAN SELECT * FROM terms WHERE +term BETWEEN 'brags' AND 'brain'
   174         -} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0: (~0 rows)} }
          174  +} { 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} }
   175    175   
   176    176   do_test 2.2.2.1 {
   177    177     set cnt 0
   178    178     execsql { SELECT * FROM terms WHERE rec('cnt', term) AND term>'brain' }
   179    179     set cnt
   180    180   } {18}
   181    181   do_test 2.2.2.2 {
................................................................................
   331    331     5    1    "ORDER BY documents"
   332    332     6    1    "ORDER BY documents DESC"
   333    333     7    1    "ORDER BY occurrences ASC"
   334    334     8    1    "ORDER BY occurrences"
   335    335     9    1    "ORDER BY occurrences DESC"
   336    336   } {
   337    337   
   338         -  set res [list 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0: (~0 rows)}]
          338  +  set res [list 0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:}]
   339    339     if {$sort} { lappend res 0 0 0 {USE TEMP B-TREE FOR ORDER BY} }
   340    340   
   341    341     set sql "SELECT * FROM terms $orderby"
   342    342     do_execsql_test 2.3.1.$tn "EXPLAIN QUERY PLAN $sql" $res
   343    343   }
   344    344   
   345    345   #-------------------------------------------------------------------------
................................................................................
   406    406   proc do_plansql_test {tn sql r} {
   407    407     uplevel do_execsql_test $tn [list "EXPLAIN QUERY PLAN $sql ; $sql"] [list $r]
   408    408   }
   409    409   
   410    410   do_plansql_test 4.2 {
   411    411     SELECT y FROM x2, terms WHERE y = term AND col = '*'
   412    412   } {
   413         -  0 0 0 {SCAN TABLE x2 (~1000000 rows)} 
   414         -  0 1 1 {SCAN TABLE terms VIRTUAL TABLE INDEX 1: (~0 rows)} 
          413  +  0 0 0 {SCAN TABLE x2} 
          414  +  0 1 1 {SCAN TABLE terms VIRTUAL TABLE INDEX 1:} 
   415    415     a b c d e f g h i j k l
   416    416   }
   417    417   
   418    418   do_plansql_test 4.3 {
   419    419     SELECT y FROM terms, x2 WHERE y = term AND col = '*'
   420    420   } {
   421         -  0 0 1 {SCAN TABLE x2 (~1000000 rows)} 
   422         -  0 1 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 1: (~0 rows)} 
          421  +  0 0 1 {SCAN TABLE x2} 
          422  +  0 1 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 1:} 
   423    423     a b c d e f g h i j k l
   424    424   }
   425    425   
   426    426   do_plansql_test 4.4 {
   427    427     SELECT y FROM x3, terms WHERE y = term AND col = '*'
   428    428   } {
   429         -  0 0 1 {SCAN TABLE terms VIRTUAL TABLE INDEX 0: (~0 rows)} 
   430         -  0 1 0 {SEARCH TABLE x3 USING COVERING INDEX i1 (y=?) (~10 rows)}
          429  +  0 0 1 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} 
          430  +  0 1 0 {SEARCH TABLE x3 USING COVERING INDEX i1 (y=?)}
   431    431     a b c d e f g h i j k l
   432    432   }
   433    433   
   434    434   do_plansql_test 4.5 {
   435    435     SELECT y FROM terms, x3 WHERE y = term AND occurrences>1 AND col = '*'
   436    436   } {
   437         -  0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0: (~0 rows)} 
   438         -  0 1 1 {SEARCH TABLE x3 USING COVERING INDEX i1 (y=?) (~10 rows)}
          437  +  0 0 0 {SCAN TABLE terms VIRTUAL TABLE INDEX 0:} 
          438  +  0 1 1 {SEARCH TABLE x3 USING COVERING INDEX i1 (y=?)}
   439    439     a k l
   440    440   }
   441    441   
   442    442   #-------------------------------------------------------------------------
   443    443   # The following tests check that fts4aux can handle an fts table with an
   444    444   # odd name (one that requires quoting for use in SQL statements). And that
   445    445   # the argument to the fts4aux constructor is properly dequoted before use.
................................................................................
   515    515   
   516    516   do_test 8.2 {
   517    517     execsql {DETACH att}
   518    518     catchsql { SELECT * FROM aux2 }
   519    519   } {1 {SQL logic error or missing database}}
   520    520   
   521    521   finish_test
   522         -

Changes to test/fts3corrupt.test.

   162    162     UPDATE t1_stat SET value = NULL;
   163    163     SELECT matchinfo(t1, 'nxa') FROM t1 WHERE t1 MATCH 't*';
   164    164   } {1 {database disk image is malformed}}
   165    165   do_test 5.3.1 { sqlite3_extended_errcode db } SQLITE_CORRUPT_VTAB
   166    166   
   167    167   
   168    168   finish_test
   169         -

Changes to test/fts3defer2.test.

   149    149     do_execsql_test 2.4.$tn {
   150    150       SELECT docid, mit(matchinfo(t3, 'pcxnal')) FROM t3 WHERE t3 MATCH '"a b c"';
   151    151     } {1 {1 1 1 4 4 11 912 6} 3 {1 1 1 4 4 11 912 6}}
   152    152   }
   153    153   
   154    154   
   155    155   finish_test
   156         -

Changes to test/fts3expr3.test.

   200    200     test_fts3expr2 $::query
   201    201   } -test {
   202    202     faultsim_test_result [list 0 $::result]
   203    203   }
   204    204   
   205    205   set sqlite_fts3_enable_parentheses 0
   206    206   finish_test
   207         -
   208         -
   209         -
   210         -

Changes to test/fts3malloc.test.

   297    297   
   298    298   do_write_test fts3_malloc-5.3 ft_content {
   299    299     INSERT INTO ft8 VALUES('short alongertoken reallyquitealotlongerimeanit andthistokenisjustsolongthatonemightbeforgivenforimaginingthatitwasmerelyacontrivedexampleandnotarealtoken')
   300    300   }
   301    301   
   302    302   
   303    303   finish_test
   304         -

Changes to test/fts3matchinfo.test.

   423    423     INSERT INTO t12 VALUES('a d d a');
   424    424     SELECT mit(matchinfo(t12, 'x')) FROM t12 WHERE t12 MATCH 'a NEAR/1 d OR a';
   425    425   } {
   426    426     {0 3 2 0 3 2 1 4 3} {1 3 2 1 3 2 1 4 3} {2 3 2 2 3 2 2 4 3}
   427    427   }
   428    428   
   429    429   finish_test
   430         -

Changes to test/fts3prefix2.test.

    55     55     {T TX T TX T TX T TX T TX}
    56     56     {T TX T TX T TX T TX T TX}
    57     57     {T TX T TX T TX T TX T TX}
    58     58     {T TX T TX T TX T TX T TX}
    59     59   }
    60     60   
    61     61   finish_test
    62         -

Changes to test/fts3query.test.

   114    114       CREATE VIRTUAL TABLE ft USING fts3(title);
   115    115       CREATE TABLE bt(title);
   116    116     }
   117    117   } {}
   118    118   do_eqp_test fts3query-4.2 {
   119    119     SELECT t1.number FROM t1, ft WHERE t1.number=ft.rowid ORDER BY t1.date
   120    120   } {
   121         -  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)} 
   122         -  0 1 1 {SCAN TABLE ft VIRTUAL TABLE INDEX 1: (~0 rows)}
          121  +  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1} 
          122  +  0 1 1 {SCAN TABLE ft VIRTUAL TABLE INDEX 1:}
   123    123   }
   124    124   do_eqp_test fts3query-4.3 {
   125    125     SELECT t1.number FROM ft, t1 WHERE t1.number=ft.rowid ORDER BY t1.date
   126    126   } {
   127         -  0 0 1 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)} 
   128         -  0 1 0 {SCAN TABLE ft VIRTUAL TABLE INDEX 1: (~0 rows)}
          127  +  0 0 1 {SCAN TABLE t1 USING COVERING INDEX i1} 
          128  +  0 1 0 {SCAN TABLE ft VIRTUAL TABLE INDEX 1:}
   129    129   }
   130    130   do_eqp_test fts3query-4.4 {
   131    131     SELECT t1.number FROM t1, bt WHERE t1.number=bt.rowid ORDER BY t1.date
   132    132   } {
   133         -  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)} 
   134         -  0 1 1 {SEARCH TABLE bt USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
          133  +  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1} 
          134  +  0 1 1 {SEARCH TABLE bt USING INTEGER PRIMARY KEY (rowid=?)}
   135    135   }
   136    136   do_eqp_test fts3query-4.5 {
   137    137     SELECT t1.number FROM bt, t1 WHERE t1.number=bt.rowid ORDER BY t1.date
   138    138   } {
   139         -  0 0 1 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)} 
   140         -  0 1 0 {SEARCH TABLE bt USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
          139  +  0 0 1 {SCAN TABLE t1 USING COVERING INDEX i1} 
          140  +  0 1 0 {SEARCH TABLE bt USING INTEGER PRIMARY KEY (rowid=?)}
   141    141   }
   142    142   
   143    143   
   144    144   # Test that calling matchinfo() with the wrong number of arguments, or with
   145    145   # an invalid argument returns an error.
   146    146   #
   147    147   do_execsql_test 5.1 {
................................................................................
   206    206   
   207    207     7 "SELECT snippet(t3, 'XXX', 'YYY', 'ZZZ', 1, 5) FROM t3 WHERE t3 MATCH 'gestures'" 
   208    208     {{ZZZthe hand XXXgesturesYYY (called beatsZZZ}}
   209    209   }
   210    210   
   211    211   
   212    212   finish_test
   213         -

Changes to test/fts3shared.test.

   170    170     execsql ROLLBACK dbW 
   171    171   }
   172    172   
   173    173   dbW close
   174    174   dbR close
   175    175   sqlite3_enable_shared_cache $::enable_shared_cache
   176    176   finish_test
   177         -

Changes to test/fts3snippet.test.


Changes to test/fts3sort.test.

   178    178     INSERT INTO t4(docid, x) VALUES(1, 'ab');
   179    179     SELECT rowid FROM t4 WHERE x MATCH 'a*';
   180    180   } {-113382409004785664 1}
   181    181   
   182    182   
   183    183   
   184    184   finish_test
   185         -

Changes to test/fts3tok1.test.

   109    109   do_catchsql_test 2.1 {
   110    110     CREATE VIRTUAL TABLE t4 USING fts3tokenize;
   111    111     SELECT * FROM t4;
   112    112   } {1 {SQL logic error or missing database}}
   113    113   
   114    114   
   115    115   finish_test
   116         -
   117         -

Changes to test/fts3tok_err.test.

    41     41     execsql { SELECT token FROM t1 WHERE input = 'A galaxy far, far away' } 
    42     42   } -test {
    43     43     faultsim_test_result {0 {a galaxy far far away}} 
    44     44   }
    45     45   
    46     46   
    47     47   finish_test
    48         -
    49         -

Changes to test/fts4content.test.

   619    619   do_execsql_test 10.7 {
   620    620     SELECT snippet(ft, '[', ']', '...', -1, 5) FROM ft WHERE ft MATCH 'e'
   621    621   } {
   622    622     {...c d [e] f g...}
   623    623   }
   624    624   
   625    625   finish_test
   626         -

Changes to test/fuzzer1.test.

  1724   1724     INSERT INTO x3_rules VALUES(2, 'a', 'z',  8);
  1725   1725     CREATE VIRTUAL TABLE x3 USING fuzzer(x3_rules);
  1726   1726   }
  1727   1727   
  1728   1728   do_execsql_test 8.2.1 {
  1729   1729     SELECT cFrom, cTo, word 
  1730   1730       FROM x3_rules CROSS JOIN x3 
  1731         -    WHERE word MATCH 'a' AND cost=distance AND ruleset=2;
         1731  +    WHERE word MATCH 'a' AND cost=distance AND ruleset=2
         1732  +    ORDER BY +cTo;
  1732   1733   } {a x x a y y a z z}
  1733   1734   
  1734   1735   do_execsql_test 8.2.2 {
  1735   1736     SELECT cFrom, cTo, word 
  1736   1737       FROM x3 CROSS JOIN x3_rules
  1737         -    WHERE word MATCH 'a' AND cost=distance AND ruleset=2;
         1738  +    WHERE word MATCH 'a' AND cost=distance AND ruleset=2
         1739  +    ORDER BY +cTo DESC
  1738   1740   } {a z z a y y a x x}
  1739   1741   
  1740   1742   do_execsql_test 8.2.3 {
  1741   1743     SELECT cFrom, cTo, word 
  1742   1744       FROM x3_rules, x3 
  1743         -    WHERE word MATCH 'a' AND cost=distance AND ruleset=2;
         1745  +    WHERE word MATCH 'a' AND cost=distance AND ruleset=2
         1746  +    ORDER BY +cTo DESC;
  1744   1747   } {a z z a y y a x x}
  1745   1748   
  1746   1749   do_execsql_test 8.2.4 {
  1747   1750     SELECT cFrom, cTo, word 
  1748   1751       FROM x3, x3_rules
  1749         -    WHERE word MATCH 'a' AND cost=distance AND ruleset=2;
         1752  +    WHERE word MATCH 'a' AND cost=distance AND ruleset=2
         1753  +    ORDER BY +cTo DESC;
  1750   1754   } {a z z a y y a x x}
  1751   1755   
  1752   1756   do_execsql_test 8.2.5 {
  1753   1757     CREATE INDEX i1 ON x3_rules(cost);
  1754   1758     SELECT cFrom, cTo, word 
  1755   1759       FROM x3_rules, x3 
  1756         -    WHERE word MATCH 'a' AND cost=distance AND ruleset=2;
         1760  +    WHERE word MATCH 'a' AND cost=distance AND ruleset=2
         1761  +    ORDER BY +cTo DESC;
  1757   1762   } {a z z a y y a x x}
  1758   1763   
  1759   1764   do_execsql_test 8.2.5 {
  1760         -  SELECT word FROM x3_rules, x3 WHERE word MATCH x3_rules.cFrom AND ruleset=2;
         1765  +  SELECT word FROM x3_rules, x3 WHERE word MATCH x3_rules.cFrom AND ruleset=2
  1761   1766   } {a z y x a z y x a z y x}
  1762   1767   
  1763   1768   do_execsql_test 8.2.6 {
  1764   1769     SELECT word FROM x3_rules, x3 
  1765   1770     WHERE word MATCH x3_rules.cFrom 
  1766   1771       AND ruleset=2 
  1767   1772       AND x3_rules.cost=8;

Changes to test/incrblob3.test.

   265    265     sqlite3_db_config_lookaside db 0 0 0
   266    266     list [catch {db incrblob blobs v 1} msg] $msg
   267    267   } {1 {database schema has changed}}
   268    268   db close
   269    269   tvfs delete
   270    270   
   271    271   finish_test
   272         -

Changes to test/incrblob4.test.

    83     83     set new [string repeat % 900]
    84     84     execsql { UPDATE t1 SET v = $new WHERE k = 20 }
    85     85     execsql { DELETE FROM t1 WHERE k=19 }
    86     86     execsql { INSERT INTO t1(v) VALUES($new) }
    87     87   } {}
    88     88   
    89     89   finish_test
    90         -

Changes to test/incrblobfault.test.

    63     63     gets $::blob
    64     64   } -test {
    65     65     faultsim_test_result {0 {hello world}}
    66     66     catch { close $::blob }
    67     67   }
    68     68   
    69     69   finish_test
    70         -

Changes to test/incrvacuum3.test.

   147    147     }
   148    148   
   149    149     do_execsql_test $T.1.x.1 { PRAGMA freelist_count   } 0
   150    150     do_execsql_test $T.1.x.2 { SELECT count(*) FROM t1 } 128
   151    151   }
   152    152   
   153    153   finish_test
   154         -

Changes to test/indexedby.test.

    38     38     uplevel "execsql {EXPLAIN QUERY PLAN $sql}"
    39     39   }
    40     40   
    41     41   # These tests are to check that "EXPLAIN QUERY PLAN" is working as expected.
    42     42   #
    43     43   do_execsql_test indexedby-1.2 {
    44     44     EXPLAIN QUERY PLAN select * from t1 WHERE a = 10; 
    45         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~10 rows)}}
           45  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}}
    46     46   do_execsql_test indexedby-1.3 {
    47     47     EXPLAIN QUERY PLAN select * from t1 ; 
    48         -} {0 0 0 {SCAN TABLE t1 (~1000000 rows)}}
           48  +} {0 0 0 {SCAN TABLE t1}}
    49     49   do_execsql_test indexedby-1.4 {
    50     50     EXPLAIN QUERY PLAN select * from t1, t2 WHERE c = 10; 
    51     51   } {
    52         -  0 0 1 {SEARCH TABLE t2 USING INDEX i3 (c=?) (~10 rows)} 
    53         -  0 1 0 {SCAN TABLE t1 (~1000000 rows)}
           52  +  0 0 1 {SEARCH TABLE t2 USING INDEX i3 (c=?)} 
           53  +  0 1 0 {SCAN TABLE t1}
    54     54   }
    55     55   
    56     56   # Parser tests. Test that an INDEXED BY or NOT INDEX clause can be 
    57     57   # attached to a table in the FROM clause, but not to a sub-select or
    58     58   # SQL view. Also test that specifying an index that does not exist or
    59     59   # is attached to a different table is detected as an error.
    60     60   # 
................................................................................
    81     81     catchsql { SELECT * FROM v1 INDEXED BY i1 WHERE a = 'one' }
    82     82   } {1 {no such index: i1}}
    83     83   
    84     84   # Tests for single table cases.
    85     85   #
    86     86   do_execsql_test indexedby-3.1 {
    87     87     EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE a = 'one' AND b = 'two'
    88         -} {0 0 0 {SCAN TABLE t1 (~10000 rows)}}
           88  +} {0 0 0 {SCAN TABLE t1}}
    89     89   do_execsql_test indexedby-3.2 {
    90     90     EXPLAIN QUERY PLAN 
    91     91     SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' AND b = 'two'
    92         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~2 rows)}}
           92  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}}
    93     93   do_execsql_test indexedby-3.3 {
    94     94     EXPLAIN QUERY PLAN 
    95     95     SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' AND b = 'two'
    96         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?) (~2 rows)}}
           96  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}}
    97     97   do_test indexedby-3.4 {
    98     98     catchsql { SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' }
    99         -} {1 {cannot use index: i2}}
           99  +} {1 {no query solution}}
   100    100   do_test indexedby-3.5 {
   101    101     catchsql { SELECT * FROM t1 INDEXED BY i2 ORDER BY a }
   102         -} {1 {cannot use index: i2}}
          102  +} {1 {no query solution}}
   103    103   do_test indexedby-3.6 {
   104    104     catchsql { SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' }
   105    105   } {0 {}}
   106    106   do_test indexedby-3.7 {
   107    107     catchsql { SELECT * FROM t1 INDEXED BY i1 ORDER BY a }
   108    108   } {0 {}}
   109    109   
   110    110   do_execsql_test indexedby-3.8 {
   111    111     EXPLAIN QUERY PLAN 
   112    112     SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_1 ORDER BY e 
   113         -} {0 0 0 {SCAN TABLE t3 USING INDEX sqlite_autoindex_t3_1 (~1000000 rows)}}
          113  +} {0 0 0 {SCAN TABLE t3 USING INDEX sqlite_autoindex_t3_1}}
   114    114   do_execsql_test indexedby-3.9 {
   115    115     EXPLAIN QUERY PLAN 
   116    116     SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_1 WHERE e = 10 
   117         -} {0 0 0 {SEARCH TABLE t3 USING INDEX sqlite_autoindex_t3_1 (e=?) (~1 rows)}}
          117  +} {0 0 0 {SEARCH TABLE t3 USING INDEX sqlite_autoindex_t3_1 (e=?)}}
   118    118   do_test indexedby-3.10 {
   119    119     catchsql { SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_1 WHERE f = 10 }
   120         -} {1 {cannot use index: sqlite_autoindex_t3_1}}
          120  +} {1 {no query solution}}
   121    121   do_test indexedby-3.11 {
   122    122     catchsql { SELECT * FROM t3 INDEXED BY sqlite_autoindex_t3_2 WHERE f = 10 }
   123    123   } {1 {no such index: sqlite_autoindex_t3_2}}
   124    124   
   125    125   # Tests for multiple table cases.
   126    126   #
   127    127   do_execsql_test indexedby-4.1 {
   128    128     EXPLAIN QUERY PLAN SELECT * FROM t1, t2 WHERE a = c 
   129    129   } {
   130         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)} 
   131         -  0 1 1 {SEARCH TABLE t2 USING INDEX i3 (c=?) (~10 rows)}
          130  +  0 0 0 {SCAN TABLE t1} 
          131  +  0 1 1 {SEARCH TABLE t2 USING INDEX i3 (c=?)}
   132    132   }
   133    133   do_execsql_test indexedby-4.2 {
   134    134     EXPLAIN QUERY PLAN SELECT * FROM t1 INDEXED BY i1, t2 WHERE a = c 
   135    135   } {
   136         -  0 0 1 {SCAN TABLE t2 (~1000000 rows)} 
   137         -  0 1 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~10 rows)}
          136  +  0 0 1 {SCAN TABLE t2} 
          137  +  0 1 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}
   138    138   }
   139    139   do_test indexedby-4.3 {
   140    140     catchsql {
   141    141       SELECT * FROM t1 INDEXED BY i1, t2 INDEXED BY i3 WHERE a=c
   142    142     }
   143         -} {1 {cannot use index: i1}}
          143  +} {1 {no query solution}}
   144    144   do_test indexedby-4.4 {
   145    145     catchsql {
   146    146       SELECT * FROM t2 INDEXED BY i3, t1 INDEXED BY i1 WHERE a=c
   147    147     }
   148         -} {1 {cannot use index: i3}}
          148  +} {1 {no query solution}}
   149    149   
   150    150   # Test embedding an INDEXED BY in a CREATE VIEW statement. This block
   151    151   # also tests that nothing bad happens if an index refered to by
   152    152   # a CREATE VIEW statement is dropped and recreated.
   153    153   #
   154    154   do_execsql_test indexedby-5.1 {
   155    155     CREATE VIEW v2 AS SELECT * FROM t1 INDEXED BY i1 WHERE a > 5;
   156    156     EXPLAIN QUERY PLAN SELECT * FROM v2 
   157         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?) (~250000 rows)}}
          157  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?)}}
   158    158   do_execsql_test indexedby-5.2 {
   159    159     EXPLAIN QUERY PLAN SELECT * FROM v2 WHERE b = 10 
   160         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?) (~25000 rows)}}
          160  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a>?)}}
   161    161   do_test indexedby-5.3 {
   162    162     execsql { DROP INDEX i1 }
   163    163     catchsql { SELECT * FROM v2 }
   164    164   } {1 {no such index: i1}}
   165    165   do_test indexedby-5.4 {
   166    166     # Recreate index i1 in such a way as it cannot be used by the view query.
   167    167     execsql { CREATE INDEX i1 ON t1(b) }
   168    168     catchsql { SELECT * FROM v2 }
   169         -} {1 {cannot use index: i1}}
          169  +} {1 {no query solution}}
   170    170   do_test indexedby-5.5 {
   171    171     # Drop and recreate index i1 again. This time, create it so that it can
   172    172     # be used by the query.
   173    173     execsql { DROP INDEX i1 ; CREATE INDEX i1 ON t1(a) }
   174    174     catchsql { SELECT * FROM v2 }
   175    175   } {0 {}}
   176    176   
   177    177   # Test that "NOT INDEXED" may use the rowid index, but not others.
   178    178   # 
   179    179   do_execsql_test indexedby-6.1 {
   180    180     EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 10 ORDER BY rowid 
   181         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?) (~10 rows)}}
          181  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}}
   182    182   do_execsql_test indexedby-6.2 {
   183    183     EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE b = 10 ORDER BY rowid 
   184         -} {0 0 0 {SCAN TABLE t1 USING INTEGER PRIMARY KEY (~100000 rows)}}
          184  +} {0 0 0 {SCAN TABLE t1}}
   185    185   
   186    186   # Test that "INDEXED BY" can be used in a DELETE statement.
   187    187   # 
   188    188   do_execsql_test indexedby-7.1 {
   189    189     EXPLAIN QUERY PLAN DELETE FROM t1 WHERE a = 5 
   190         -} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?) (~10 rows)}}
          190  +} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}}
   191    191   do_execsql_test indexedby-7.2 {
   192    192     EXPLAIN QUERY PLAN DELETE FROM t1 NOT INDEXED WHERE a = 5 
   193         -} {0 0 0 {SCAN TABLE t1 (~100000 rows)}}
          193  +} {0 0 0 {SCAN TABLE t1}}
   194    194   do_execsql_test indexedby-7.3 {
   195    195     EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 
   196         -} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?) (~10 rows)}}
          196  +} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}}
   197    197   do_execsql_test indexedby-7.4 {
   198    198     EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 AND b = 10
   199         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~2 rows)}}
          199  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}}
   200    200   do_execsql_test indexedby-7.5 {
   201    201     EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i2 WHERE a = 5 AND b = 10
   202         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?) (~2 rows)}}
          202  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}}
   203    203   do_test indexedby-7.6 {
   204    204     catchsql { DELETE FROM t1 INDEXED BY i2 WHERE a = 5}
   205         -} {1 {cannot use index: i2}}
          205  +} {1 {no query solution}}
   206    206   
   207    207   # Test that "INDEXED BY" can be used in an UPDATE statement.
   208    208   # 
   209    209   do_execsql_test indexedby-8.1 {
   210    210     EXPLAIN QUERY PLAN UPDATE t1 SET rowid=rowid+1 WHERE a = 5 
   211         -} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?) (~10 rows)}}
          211  +} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}}
   212    212   do_execsql_test indexedby-8.2 {
   213    213     EXPLAIN QUERY PLAN UPDATE t1 NOT INDEXED SET rowid=rowid+1 WHERE a = 5 
   214         -} {0 0 0 {SCAN TABLE t1 (~100000 rows)}}
          214  +} {0 0 0 {SCAN TABLE t1}}
   215    215   do_execsql_test indexedby-8.3 {
   216    216     EXPLAIN QUERY PLAN UPDATE t1 INDEXED BY i1 SET rowid=rowid+1 WHERE a = 5 
   217         -} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?) (~10 rows)}}
          217  +} {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}}
   218    218   do_execsql_test indexedby-8.4 {
   219    219     EXPLAIN QUERY PLAN 
   220    220     UPDATE t1 INDEXED BY i1 SET rowid=rowid+1 WHERE a = 5 AND b = 10
   221         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~2 rows)}}
          221  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}}
   222    222   do_execsql_test indexedby-8.5 {
   223    223     EXPLAIN QUERY PLAN 
   224    224     UPDATE t1 INDEXED BY i2 SET rowid=rowid+1 WHERE a = 5 AND b = 10
   225         -} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?) (~2 rows)}}
          225  +} {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}}
   226    226   do_test indexedby-8.6 {
   227    227     catchsql { UPDATE t1 INDEXED BY i2 SET rowid=rowid+1 WHERE a = 5}
   228         -} {1 {cannot use index: i2}}
          228  +} {1 {no query solution}}
   229    229   
   230    230   # Test that bug #3560 is fixed.
   231    231   #
   232    232   do_test indexedby-9.1 {
   233    233     execsql {
   234    234       CREATE TABLE maintable( id integer);
   235    235       CREATE TABLE joinme(id_int integer, id_text text);
................................................................................
   239    239   } {}
   240    240   do_test indexedby-9.2 {
   241    241     catchsql {
   242    242       select * from maintable as m inner join
   243    243       joinme as j indexed by joinme_id_text_idx
   244    244       on ( m.id  = j.id_int)
   245    245     }
   246         -} {1 {cannot use index: joinme_id_text_idx}}
          246  +} {1 {no query solution}}
   247    247   do_test indexedby-9.3 {
   248    248     catchsql { select * from maintable, joinme INDEXED by joinme_id_text_idx }
   249         -} {1 {cannot use index: joinme_id_text_idx}}
          249  +} {1 {no query solution}}
   250    250   
   251    251   # Make sure we can still create tables, indices, and columns whose name
   252    252   # is "indexed".
   253    253   #
   254    254   do_test indexedby-10.1 {
   255    255     execsql {
   256    256       CREATE TABLE indexed(x,y);

Changes to test/intpkey.test.

   121    121   #
   122    122   do_test intpkey-1.12.1 {
   123    123     execsql {
   124    124       SELECT * FROM t1 WHERE a==4;
   125    125     }
   126    126   } {4 one two}
   127    127   do_test intpkey-1.12.2 {
   128         -  set sqlite_query_plan
   129         -} {t1 *}
          128  +  execsql {
          129  +    EXPLAIN QUERY PLAN
          130  +    SELECT * FROM t1 WHERE a==4;
          131  +  }
          132  +} {/SEARCH TABLE t1 /}
   130    133   
   131    134   # Try to insert a non-integer value into the primary key field.  This
   132    135   # should result in a data type mismatch.
   133    136   #
   134    137   do_test intpkey-1.13.1 {
   135    138     set r [catch {execsql {
   136    139       INSERT INTO t1 VALUES('x','y','z');

Changes to test/io.test.

   637    637     hexio_write test.db [expr 1024 * 5] [string repeat 00 2048]
   638    638     do_execsql_test 6.2.$tn.3 { PRAGMA integrity_check } {ok}
   639    639     db close
   640    640   }
   641    641   
   642    642   sqlite3_simulate_device -char {} -sectorsize 0
   643    643   finish_test
   644         -

Changes to test/ioerr6.test.

    85     85     db eval { CREATE TABLE t3(x) }
    86     86     if {[db one { PRAGMA integrity_check }] != "ok"} {
    87     87       error "integrity check failed"
    88     88     }
    89     89   }
    90     90   
    91     91   finish_test
    92         -

Changes to test/like.test.

   152    152   ifcapable !like_opt {
   153    153     finish_test
   154    154     return
   155    155   } 
   156    156   
   157    157   # This procedure executes the SQL.  Then it appends to the result the
   158    158   # "sort" or "nosort" keyword (as in the cksort procedure above) then
   159         -# it appends the ::sqlite_query_plan variable.
          159  +# it appends the names of the table and index used.
   160    160   #
   161    161   proc queryplan {sql} {
   162    162     set ::sqlite_sort_count 0
   163    163     set data [execsql $sql]
   164    164     if {$::sqlite_sort_count} {set x sort} {set x nosort}
   165    165     lappend data $x
   166         -  return [concat $data $::sqlite_query_plan]
          166  +  set eqp [execsql "EXPLAIN QUERY PLAN $sql"]
          167  +  # puts eqp=$eqp
          168  +  foreach {a b c x} $eqp {
          169  +    if {[regexp { TABLE (\w+ AS )?(\w+) USING COVERING INDEX (\w+)\y} \
          170  +        $x all as tab idx]} {
          171  +      lappend data {} $idx
          172  +    } elseif {[regexp { TABLE (\w+ AS )?(\w+) USING.* INDEX (\w+)\y} \
          173  +        $x all as tab idx]} {
          174  +      lappend data $tab $idx
          175  +    } elseif {[regexp { TABLE (\w+ AS )?(\w+)\y} $x all as tab]} {
          176  +      lappend data $tab *
          177  +    }
          178  +  }
          179  +  return $data   
   167    180   }
   168    181   
   169    182   # Perform tests on the like optimization.
   170    183   #
   171    184   # With no index on t1.x and with case sensitivity turned off, no optimization
   172    185   # is performed.
   173    186   #
   174    187   do_test like-3.1 {
   175    188     set sqlite_like_count 0
   176    189     queryplan {
   177    190       SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
   178    191     }
   179         -} {ABC {ABC abc xyz} abc abcd sort t1 {}}
          192  +} {ABC {ABC abc xyz} abc abcd sort t1 *}
   180    193   do_test like-3.2 {
   181    194     set sqlite_like_count
   182    195   } {12}
   183    196   
   184    197   # With an index on t1.x and case sensitivity on, optimize completely.
   185    198   #
   186    199   do_test like-3.3 {
................................................................................
   265    278     set sqlite_like_count
   266    279   } 12
   267    280   
   268    281   # No optimization for case insensitive LIKE
   269    282   #
   270    283   do_test like-3.13 {
   271    284     set sqlite_like_count 0
          285  +  db eval {PRAGMA case_sensitive_like=off;}
   272    286     queryplan {
   273         -    PRAGMA case_sensitive_like=off;
   274    287       SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
   275    288     }
   276    289   } {ABC {ABC abc xyz} abc abcd nosort {} i1}
   277    290   do_test like-3.14 {
   278    291     set sqlite_like_count
   279    292   } 12
   280    293   
   281    294   # No optimization without an index.
   282    295   #
   283    296   do_test like-3.15 {
   284    297     set sqlite_like_count 0
   285         -  queryplan {
          298  +  db eval {
   286    299       PRAGMA case_sensitive_like=on;
   287    300       DROP INDEX i1;
          301  +  }
          302  +  queryplan {
   288    303       SELECT x FROM t1 WHERE x LIKE 'abc%' ORDER BY 1;
   289    304     }
   290         -} {abc abcd sort t1 {}}
          305  +} {abc abcd sort t1 *}
   291    306   do_test like-3.16 {
   292    307     set sqlite_like_count
   293    308   } 12
   294    309   
   295    310   # No GLOB optimization without an index.
   296    311   #
   297    312   do_test like-3.17 {
   298    313     set sqlite_like_count 0
   299    314     queryplan {
   300    315       SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
   301    316     }
   302         -} {abc abcd sort t1 {}}
          317  +} {abc abcd sort t1 *}
   303    318   do_test like-3.18 {
   304    319     set sqlite_like_count
   305    320   } 12
   306    321   
   307    322   # GLOB is optimized regardless of the case_sensitive_like setting.
   308    323   #
   309    324   do_test like-3.19 {
................................................................................
   314    329     }
   315    330   } {abc abcd nosort {} i1}
   316    331   do_test like-3.20 {
   317    332     set sqlite_like_count
   318    333   } 0
   319    334   do_test like-3.21 {
   320    335     set sqlite_like_count 0
          336  +  db eval {PRAGMA case_sensitive_like=on;}
   321    337     queryplan {
   322         -    PRAGMA case_sensitive_like=on;
   323    338       SELECT x FROM t1 WHERE x GLOB 'abc*' ORDER BY 1;
   324    339     }
   325    340   } {abc abcd nosort {} i1}
   326    341   do_test like-3.22 {
   327    342     set sqlite_like_count
   328    343   } 0
   329    344   do_test like-3.23 {
   330    345     set sqlite_like_count 0
          346  +  db eval {PRAGMA case_sensitive_like=off;}
   331    347     queryplan {
   332         -    PRAGMA case_sensitive_like=off;
   333    348       SELECT x FROM t1 WHERE x GLOB 'a[bc]d' ORDER BY 1;
   334    349     }
   335    350   } {abd acd nosort {} i1}
   336    351   do_test like-3.24 {
   337    352     set sqlite_like_count
   338    353   } 6
   339    354   
................................................................................
   805    820       INSERT INTO t11 VALUES(10, 'yz','yz');
   806    821       INSERT INTO t11 VALUES(11, 'X','X');
   807    822       INSERT INTO t11 VALUES(12, 'YZ','YZ');
   808    823       SELECT count(*) FROM t11;
   809    824     }
   810    825   } {12}
   811    826   do_test like-11.1 {
          827  +  db eval {PRAGMA case_sensitive_like=OFF;}
   812    828     queryplan {
   813         -    PRAGMA case_sensitive_like=OFF;
   814    829       SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY a;
   815    830     }
   816    831   } {abc abcd ABC ABCD nosort t11 *}
   817    832   do_test like-11.2 {
          833  +  db eval {PRAGMA case_sensitive_like=ON;}
   818    834     queryplan {
   819         -    PRAGMA case_sensitive_like=ON;
   820    835       SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY a;
   821    836     }
   822    837   } {abc abcd nosort t11 *}
   823    838   do_test like-11.3 {
   824         -  queryplan {
          839  +  db eval {
   825    840       PRAGMA case_sensitive_like=OFF;
   826    841       CREATE INDEX t11b ON t11(b);
          842  +  }
          843  +  queryplan {
   827    844       SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
   828    845     }
   829    846   } {abc abcd ABC ABCD sort {} t11b}
   830    847   do_test like-11.4 {
          848  +  db eval {PRAGMA case_sensitive_like=ON;}
   831    849     queryplan {
   832         -    PRAGMA case_sensitive_like=ON;
   833    850       SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY a;
   834    851     }
   835    852   } {abc abcd nosort t11 *}
   836    853   do_test like-11.5 {
   837         -  queryplan {
          854  +  db eval {
   838    855       PRAGMA case_sensitive_like=OFF;
   839    856       DROP INDEX t11b;
   840    857       CREATE INDEX t11bnc ON t11(b COLLATE nocase);
          858  +  }
          859  +  queryplan {
   841    860       SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
   842    861     }
   843    862   } {abc abcd ABC ABCD sort {} t11bnc}
   844    863   do_test like-11.6 {
          864  +  db eval {CREATE INDEX t11bb ON t11(b COLLATE binary);}
   845    865     queryplan {
   846         -    CREATE INDEX t11bb ON t11(b COLLATE binary);
   847    866       SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
   848    867     }
   849    868   } {abc abcd ABC ABCD sort {} t11bnc}
   850    869   do_test like-11.7 {
          870  +  db eval {PRAGMA case_sensitive_like=ON;}
   851    871     queryplan {
   852         -    PRAGMA case_sensitive_like=ON;
   853    872       SELECT b FROM t11 WHERE b LIKE 'abc%' ORDER BY +a;
   854    873     }
   855    874   } {abc abcd sort {} t11bb}
   856    875   do_test like-11.8 {
          876  +  db eval {PRAGMA case_sensitive_like=OFF;}
   857    877     queryplan {
   858         -    PRAGMA case_sensitive_like=OFF;
   859    878       SELECT b FROM t11 WHERE b GLOB 'abc*' ORDER BY +a;
   860    879     }
   861    880   } {abc abcd sort {} t11bb}
   862    881   do_test like-11.9 {
   863         -  queryplan {
          882  +  db eval {
   864    883       CREATE INDEX t11cnc ON t11(c COLLATE nocase);
   865    884       CREATE INDEX t11cb ON t11(c COLLATE binary);
          885  +  }
          886  +  queryplan {
   866    887       SELECT c FROM t11 WHERE c LIKE 'abc%' ORDER BY +a;
   867    888     }
   868    889   } {abc abcd ABC ABCD sort {} t11cnc}
   869    890   do_test like-11.10 {
   870    891     queryplan {
   871    892       SELECT c FROM t11 WHERE c GLOB 'abc*' ORDER BY +a;
   872    893     }
   873    894   } {abc abcd sort {} t11cb}
   874    895   
   875    896   
   876    897   finish_test

Changes to test/lock7.test.

    54     54     execsql { COMMIT } db1
    55     55   } {}
    56     56   
    57     57   db1 close
    58     58   db2 close
    59     59   
    60     60   finish_test
    61         -

Changes to test/misc7.test.

   265    265   sqlite3 db test.db
   266    266   
   267    267   ifcapable explain {
   268    268     do_execsql_test misc7-14.1 {
   269    269       CREATE TABLE abc(a PRIMARY KEY, b, c);
   270    270       EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE rowid = 1;
   271    271     } {
   272         -    0 0 0 {SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
          272  +    0 0 0 {SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?)}
   273    273     }
   274    274     do_execsql_test misc7-14.2 {
   275    275       EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE a = 1;
   276    276     } {0 0 0 
   277         -     {SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?) (~1 rows)}
          277  +     {SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?)}
   278    278     }
   279    279     do_execsql_test misc7-14.3 {
   280    280       EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 ORDER BY a;
   281    281     } {0 0 0 
   282         -     {SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (~1000000 rows)}
          282  +     {SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1}
   283    283     }
   284    284   }
   285    285   
   286    286   db close
   287    287   forcedelete test.db
   288    288   forcedelete test.db-journal
   289    289   sqlite3 db test.db

Changes to test/notify3.test.

   146    146   }
   147    147   catch { db1 close }
   148    148   catch { db2 close }
   149    149   
   150    150   
   151    151   sqlite3_enable_shared_cache $esc
   152    152   finish_test
   153         -

Changes to test/orderby1.test.

    44     44           (NULL, 2, 1, 'two-a'),
    45     45           (NULL, 3, 1, 'three-a');
    46     46       COMMIT;
    47     47     }
    48     48   } {}
    49     49   do_test 1.1a {
    50     50     db eval {
    51         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
           51  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
    52     52     }
    53     53   } {one-a one-c two-a two-b three-a three-c}
    54     54   
    55     55   # Verify that the ORDER BY clause is optimized out
    56     56   #
    57     57   do_test 1.1b {
    58     58     db eval {
................................................................................
    62     62   } {~/ORDER BY/}  ;# ORDER BY optimized out
    63     63   
    64     64   # The same query with ORDER BY clause optimization disabled via + operators
    65     65   # should give exactly the same answer.
    66     66   #
    67     67   do_test 1.2a {
    68     68     db eval {
    69         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
           69  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
    70     70     }
    71     71   } {one-a one-c two-a two-b three-a three-c}
    72     72   
    73     73   # The output is sorted manually in this case.
    74     74   #
    75     75   do_test 1.2b {
    76     76     db eval {
    77     77       EXPLAIN QUERY PLAN
    78         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
           78  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
    79     79     }
    80     80   } {/ORDER BY/}   ;# separate sorting pass due to "+" on ORDER BY terms
    81     81   
    82     82   # The same query with ORDER BY optimizations turned off via built-in test.
    83     83   #
    84     84   do_test 1.3a {
    85     85     optimization_control db order-by-idx-join 0
    86     86     db cache flush
    87     87     db eval {
    88         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
           88  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
    89     89     }
    90     90   } {one-a one-c two-a two-b three-a three-c}
    91     91   do_test 1.3b {
    92     92     db eval {
    93     93       EXPLAIN QUERY PLAN
    94         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
           94  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
    95     95     }
    96     96   } {/ORDER BY/}   ;# separate sorting pass due to disabled optimization
    97     97   optimization_control db all 1
    98     98   db cache flush
    99     99   
   100    100   # Reverse order sorts
   101    101   #
   102    102   do_test 1.4a {
   103    103     db eval {
   104         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
          104  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
   105    105     }
   106    106   } {three-a three-c two-a two-b one-a one-c}
   107    107   do_test 1.4b {
   108    108     db eval {
   109         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn
          109  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn
   110    110     }
   111    111   } {three-a three-c two-a two-b one-a one-c}  ;# verify same order after sorting
   112    112   do_test 1.4c {
   113    113     db eval {
   114    114       EXPLAIN QUERY PLAN
   115         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
          115  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
   116    116     }
   117         -} {~/ORDER BY/}  ;# optimized out
   118         -
          117  +} {~/ORDER BY/}  ;# ORDER BY suppressed due to uniqueness constraints
   119    118   
   120    119   do_test 1.5a {
   121    120     db eval {
   122         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
          121  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
   123    122     }
   124    123   } {one-c one-a two-b two-a three-c three-a}
   125    124   do_test 1.5b {
   126    125     db eval {
   127         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
          126  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
   128    127     }
   129    128   } {one-c one-a two-b two-a three-c three-a}  ;# verify same order after sorting
   130    129   do_test 1.5c {
   131    130     db eval {
   132    131       EXPLAIN QUERY PLAN
   133         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
          132  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
   134    133     }
   135         -} {~/ORDER BY/}  ;# optimized out
          134  +} {~/ORDER BY/}  ;# ORDER BY suppressed due to uniqueness constraints
   136    135   
   137    136   do_test 1.6a {
   138    137     db eval {
   139         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
          138  +    SELECT name FROM album CROSS JOIN track USING (aid)
          139  +     ORDER BY title DESC, tn DESC
   140    140     }
   141    141   } {three-c three-a two-b two-a one-c one-a}
   142    142   do_test 1.6b {
   143    143     db eval {
   144         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
          144  +    SELECT name FROM album CROSS JOIN track USING (aid)
          145  +     ORDER BY +title DESC, +tn DESC
   145    146     }
   146    147   } {three-c three-a two-b two-a one-c one-a}  ;# verify same order after sorting
   147    148   do_test 1.6c {
   148    149     db eval {
   149    150       EXPLAIN QUERY PLAN
   150         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
          151  +    SELECT name FROM album CROSS JOIN track USING (aid)
          152  +     ORDER BY title DESC, tn DESC
   151    153     }
   152         -} {~/ORDER BY/}  ;# ORDER BY optimized-out
          154  +} {~/ORDER BY/}  ;# ORDER BY 
   153    155   
   154    156   
   155    157   # Reconstruct the test data to use indices rather than integer primary keys.
   156    158   #
   157    159   do_test 2.0 {
   158    160     db eval {
   159    161       BEGIN;
................................................................................
   179    181           (20, 1, 'two-a'),
   180    182           (3,  1, 'three-a');
   181    183       COMMIT;
   182    184     }
   183    185   } {}
   184    186   do_test 2.1a {
   185    187     db eval {
   186         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
          188  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
   187    189     }
   188    190   } {one-a one-c two-a two-b three-a three-c}
   189    191   
   190    192   # Verify that the ORDER BY clause is optimized out
   191    193   #
   192    194   do_test 2.1b {
   193    195     db eval {
   194    196       EXPLAIN QUERY PLAN
   195         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
          197  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
   196    198     }
   197         -} {~/ORDER BY/}  ;# ORDER BY optimized out
          199  +} {/ORDER BY/}  ;# ORDER BY required because of missing aid term in ORDER BY
   198    200   
   199    201   do_test 2.1c {
   200    202     db eval {
   201         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, aid, tn
          203  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, aid, tn
   202    204     }
   203    205   } {one-a one-c two-a two-b three-a three-c}
   204    206   do_test 2.1d {
   205    207     db eval {
   206    208       EXPLAIN QUERY PLAN
   207         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, aid, tn
          209  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, aid, tn
   208    210     }
   209         -} {~/ORDER BY/}  ;# ORDER BY optimized out
          211  +} {/ORDER BY/}  ;# ORDER BY required in this case
   210    212   
   211    213   # The same query with ORDER BY clause optimization disabled via + operators
   212    214   # should give exactly the same answer.
   213    215   #
   214    216   do_test 2.2a {
   215    217     db eval {
   216         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
          218  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
   217    219     }
   218    220   } {one-a one-c two-a two-b three-a three-c}
   219    221   
   220    222   # The output is sorted manually in this case.
   221    223   #
   222    224   do_test 2.2b {
   223    225     db eval {
   224    226       EXPLAIN QUERY PLAN
   225         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
          227  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
   226    228     }
   227    229   } {/ORDER BY/}   ;# separate sorting pass due to "+" on ORDER BY terms
   228    230   
   229    231   # The same query with ORDER BY optimizations turned off via built-in test.
   230    232   #
   231    233   do_test 2.3a {
   232    234     optimization_control db order-by-idx-join 0
   233    235     db cache flush
   234    236     db eval {
   235         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
          237  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
   236    238     }
   237    239   } {one-a one-c two-a two-b three-a three-c}
   238    240   do_test 2.3b {
   239    241     db eval {
   240    242       EXPLAIN QUERY PLAN
   241         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
          243  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
   242    244     }
   243    245   } {/ORDER BY/}   ;# separate sorting pass due to disabled optimization
   244    246   optimization_control db all 1
   245    247   db cache flush
   246    248   
   247    249   # Reverse order sorts
   248    250   #
   249    251   do_test 2.4a {
   250    252     db eval {
   251         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
          253  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
   252    254     }
   253    255   } {three-a three-c two-a two-b one-a one-c}
   254    256   do_test 2.4b {
   255    257     db eval {
   256         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn
          258  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn
   257    259     }
   258    260   } {three-a three-c two-a two-b one-a one-c}  ;# verify same order after sorting
   259    261   do_test 2.4c {
   260    262     db eval {
   261    263       EXPLAIN QUERY PLAN
   262         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
          264  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
   263    265     }
   264         -} {~/ORDER BY/}  ;# optimized out
          266  +} {/ORDER BY/}  ;# separate sorting pass due to mixed DESC/ASC
   265    267   
   266    268   
   267    269   do_test 2.5a {
   268    270     db eval {
   269         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
          271  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
   270    272     }
   271    273   } {one-c one-a two-b two-a three-c three-a}
   272    274   do_test 2.5b {
   273    275     db eval {
   274         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
          276  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
   275    277     }
   276    278   } {one-c one-a two-b two-a three-c three-a}  ;# verify same order after sorting
   277    279   do_test 2.5c {
   278    280     db eval {
   279    281       EXPLAIN QUERY PLAN
   280         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
          282  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
   281    283     }
   282         -} {~/ORDER BY/}  ;# optimized out
          284  +} {/ORDER BY/}  ;# separate sorting pass due to mixed ASC/DESC
   283    285   
   284    286   do_test 2.6a {
   285    287     db eval {
   286         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
          288  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
   287    289     }
   288    290   } {three-c three-a two-b two-a one-c one-a}
   289    291   do_test 2.6b {
   290    292     db eval {
   291         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
          293  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
   292    294     }
   293    295   } {three-c three-a two-b two-a one-c one-a}  ;# verify same order after sorting
   294    296   do_test 2.6c {
   295    297     db eval {
   296    298       EXPLAIN QUERY PLAN
   297         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
          299  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
   298    300     }
   299         -} {~/ORDER BY/}  ;# ORDER BY optimized out
          301  +} {/ORDER BY/}  ;# ORDER BY required
   300    302   
   301    303   
   302    304   # Generate another test dataset, but this time using mixed ASC/DESC indices.
   303    305   #
   304    306   do_test 3.0 {
   305    307     db eval {
   306    308       BEGIN;
................................................................................
   344    346   } {~/ORDER BY/}  ;# ORDER BY optimized out
   345    347   
   346    348   # The same query with ORDER BY clause optimization disabled via + operators
   347    349   # should give exactly the same answer.
   348    350   #
   349    351   do_test 3.2a {
   350    352     db eval {
   351         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
          353  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
   352    354     }
   353    355   } {one-c one-a two-b two-a three-c three-a}
   354    356   
   355    357   # The output is sorted manually in this case.
   356    358   #
   357    359   do_test 3.2b {
   358    360     db eval {
   359    361       EXPLAIN QUERY PLAN
   360         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn DESC
          362  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
   361    363     }
   362    364   } {/ORDER BY/}   ;# separate sorting pass due to "+" on ORDER BY terms
   363    365   
   364    366   # The same query with ORDER BY optimizations turned off via built-in test.
   365    367   #
   366    368   do_test 3.3a {
   367    369     optimization_control db order-by-idx-join 0
   368    370     db cache flush
   369    371     db eval {
   370         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
          372  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
   371    373     }
   372    374   } {one-c one-a two-b two-a three-c three-a}
   373    375   do_test 3.3b {
   374    376     db eval {
   375    377       EXPLAIN QUERY PLAN
   376         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn DESC
          378  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
   377    379     }
   378    380   } {/ORDER BY/}   ;# separate sorting pass due to disabled optimization
   379    381   optimization_control db all 1
   380    382   db cache flush
   381    383   
   382    384   # Without the mixed ASC/DESC on ORDER BY
   383    385   #
   384    386   do_test 3.4a {
   385    387     db eval {
   386         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
          388  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
   387    389     }
   388    390   } {one-a one-c two-a two-b three-a three-c}
   389    391   do_test 3.4b {
   390    392     db eval {
   391         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title, +tn
          393  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
   392    394     }
   393    395   } {one-a one-c two-a two-b three-a three-c}  ;# verify same order after sorting
   394    396   do_test 3.4c {
   395    397     db eval {
   396    398       EXPLAIN QUERY PLAN
   397         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title, tn
          399  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
   398    400     }
   399         -} {~/ORDER BY/}  ;# optimized out
   400         -
          401  +} {~/ORDER BY/}  ;# ORDER BY suppressed by uniqueness constraints
   401    402   
   402    403   do_test 3.5a {
   403    404     db eval {
   404         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
          405  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
   405    406     }
   406    407   } {three-c three-a two-b two-a one-c one-a}
   407    408   do_test 3.5b {
   408    409     db eval {
   409         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
          410  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
   410    411     }
   411    412   } {three-c three-a two-b two-a one-c one-a}  ;# verify same order after sorting
   412    413   do_test 3.5c {
   413    414     db eval {
   414    415       EXPLAIN QUERY PLAN
   415         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn DESC
          416  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
   416    417     }
   417         -} {~/ORDER BY/}  ;# optimzed out
          418  +} {~/ORDER BY/}  ;# ORDER BY suppressed by uniqueness constraints
   418    419   
   419    420   
   420    421   do_test 3.6a {
   421    422     db eval {
   422    423       SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
   423    424     }
   424    425   } {three-a three-c two-a two-b one-a one-c}
   425    426   do_test 3.6b {
   426    427     db eval {
   427         -    SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY +title DESC, +tn
          428  +    SELECT name FROM album CROSS JOIN track USING (aid)
          429  +     ORDER BY +title DESC, +tn
   428    430     }
   429    431   } {three-a three-c two-a two-b one-a one-c}  ;# verify same order after sorting
   430    432   do_test 3.6c {
   431    433     db eval {
   432    434       EXPLAIN QUERY PLAN
   433    435       SELECT name FROM album CROSS JOIN track USING (aid) ORDER BY title DESC, tn
   434    436     }
   435    437   } {~/ORDER BY/}  ;# inverted ASC/DESC is optimized out
          438  +
          439  +# Ticket 5ed1772895bf3deeab78c5e3519b1da9165c541b (2013-06-04)
          440  +# Incorrect ORDER BY on an indexed JOIN
          441  +#
          442  +do_test 4.0 {
          443  +  db eval {
          444  +    CREATE TABLE t41(a INT UNIQUE NOT NULL, b INT NOT NULL);
          445  +    CREATE INDEX t41ba ON t41(b,a);
          446  +    CREATE TABLE t42(x INT NOT NULL REFERENCES t41(a), y INT NOT NULL);
          447  +    CREATE UNIQUE INDEX t42xy ON t42(x,y);
          448  +    INSERT INTO t41 VALUES(1,1),(3,1);
          449  +    INSERT INTO t42 VALUES(1,13),(1,15),(3,14),(3,16);
          450  +    
          451  +    SELECT b, y FROM t41 CROSS JOIN t42 ON x=a ORDER BY b, y;
          452  +  }
          453  +} {1 13 1 14 1 15 1 16}
          454  +
   436    455   
   437    456   
   438    457   finish_test

Added test/orderby5.test.

            1  +# 2013-06-14
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this file is testing that the optimizations that disable
           13  +# ORDER BY clauses work correctly
           14  +#
           15  +
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +set ::testprefix orderby5
           20  +
           21  +# Generate test data for a join.  Verify that the join gets the
           22  +# correct answer.
           23  +#
           24  +do_execsql_test 1.1 {
           25  +  CREATE TABLE t1(a,b,c);
           26  +  CREATE INDEX t1bc ON t1(b,c);
           27  +
           28  +  EXPLAIN QUERY PLAN
           29  +  SELECT DISTINCT a, b, c FROM t1 WHERE a=0;
           30  +} {~/B-TREE/}
           31  +do_execsql_test 1.2.1 {
           32  +  EXPLAIN QUERY PLAN
           33  +  SELECT DISTINCT a, c, b FROM t1 WHERE a=0;
           34  +} {~/B-TREE/}
           35  +do_execsql_test 1.2.2 {
           36  +  EXPLAIN QUERY PLAN
           37  +  SELECT DISTINCT a, c, b FROM t1 WHERE a='xyz' COLLATE nocase;
           38  +} {/B-TREE/}
           39  +do_execsql_test 1.2.3 {
           40  +  EXPLAIN QUERY PLAN
           41  +  SELECT DISTINCT a COLLATE nocase, c, b FROM t1 WHERE a='xyz';
           42  +} {/B-TREE/}
           43  +do_execsql_test 1.2.4 {
           44  +  EXPLAIN QUERY PLAN
           45  +  SELECT DISTINCT a COLLATE nocase, c, b FROM t1 WHERE a='xyz' COLLATE nocase;
           46  +} {~/B-TREE/}
           47  +do_execsql_test 1.3 {
           48  +  EXPLAIN QUERY PLAN
           49  +  SELECT DISTINCT b, a, c FROM t1 WHERE a=0;
           50  +} {~/B-TREE/}
           51  +do_execsql_test 1.4 {
           52  +  EXPLAIN QUERY PLAN
           53  +  SELECT DISTINCT b, c, a FROM t1 WHERE a=0;
           54  +} {~/B-TREE/}
           55  +do_execsql_test 1.5 {
           56  +  EXPLAIN QUERY PLAN
           57  +  SELECT DISTINCT c, a, b FROM t1 WHERE a=0;
           58  +} {~/B-TREE/}
           59  +do_execsql_test 1.6 {
           60  +  EXPLAIN QUERY PLAN
           61  +  SELECT DISTINCT c, b, a FROM t1 WHERE a=0;
           62  +} {~/B-TREE/}
           63  +do_execsql_test 1.7 {
           64  +  EXPLAIN QUERY PLAN
           65  +  SELECT DISTINCT c, b, a FROM t1 WHERE +a=0;
           66  +} {/B-TREE/}
           67  +do_execsql_test 2.1 {
           68  +  EXPLAIN QUERY PLAN
           69  +  SELECT * FROM t1 WHERE a=0 ORDER BY a, b, c;
           70  +} {~/B-TREE/}
           71  +do_execsql_test 2.2 {
           72  +  EXPLAIN QUERY PLAN
           73  +  SELECT * FROM t1 WHERE +a=0 ORDER BY a, b, c;
           74  +} {/B-TREE/}
           75  +do_execsql_test 2.3 {
           76  +  EXPLAIN QUERY PLAN
           77  +  SELECT * FROM t1 WHERE a=0 ORDER BY b, a, c;
           78  +} {~/B-TREE/}
           79  +do_execsql_test 2.4 {
           80  +  EXPLAIN QUERY PLAN
           81  +  SELECT * FROM t1 WHERE a=0 ORDER BY b, c, a;
           82  +} {~/B-TREE/}
           83  +do_execsql_test 2.5 {
           84  +  EXPLAIN QUERY PLAN
           85  +  SELECT * FROM t1 WHERE a=0 ORDER BY a, c, b;
           86  +} {/B-TREE/}
           87  +do_execsql_test 2.6 {
           88  +  EXPLAIN QUERY PLAN
           89  +  SELECT * FROM t1 WHERE a=0 ORDER BY c, a, b;
           90  +} {/B-TREE/}
           91  +do_execsql_test 2.7 {
           92  +  EXPLAIN QUERY PLAN
           93  +  SELECT * FROM t1 WHERE a=0 ORDER BY c, b, a;
           94  +} {/B-TREE/}
           95  +
           96  +
           97  +finish_test

Changes to test/pager1.test.

  2811   2811   
  2812   2812   do_test 43.3 {
  2813   2813     db eval { SELECT * FROM t3 }
  2814   2814     sqlite3_db_status db CACHE_MISS 0
  2815   2815   } {0 1 0}
  2816   2816   
  2817   2817   finish_test
  2818         -

Changes to test/pagerfault.test.

  1542   1542     catch { db2 close }
  1543   1543   }
  1544   1544   
  1545   1545   sqlite3_shutdown
  1546   1546   sqlite3_config_uri 0
  1547   1547   
  1548   1548   finish_test
  1549         -

Changes to test/pagerfault2.test.

    92     92     execsql { INSERT INTO t1 VALUES (a_string(2000000), a_string(2500000)) }
    93     93   } -test {
    94     94     faultsim_test_result {0 {}}
    95     95   }
    96     96   
    97     97   sqlite3_memdebug_vfs_oom_test 1
    98     98   finish_test
    99         -

Changes to test/pagerfault3.test.

    57     57     }
    58     58   } -test {
    59     59     faultsim_test_result {0 {}} 
    60     60     faultsim_integrity_check
    61     61   }
    62     62   
    63     63   finish_test
    64         -

Changes to test/permutations.test.

   208    208     sqlite3_shutdown
   209    209     install_malloc_faultsim 0
   210    210     sqlite3_initialize
   211    211     autoinstall_test_functions
   212    212   } -shutdown {
   213    213     unset -nocomplain ::G(valgrind)
   214    214   }
          215  +
          216  +test_suite "queryplanner" -prefix "" -description {
          217  +  Tests of the query planner and query optimizer
          218  +} -files {
          219  +  alter2.test alter3.test alter4.test alter.test analyze3.test
          220  +  analyze4.test analyze5.test analyze6.test analyze7.test analyze8.test
          221  +  analyze.test attach2.test attach3.test attach4.test
          222  +  attach.test autoinc.test autoindex1.test between.test cast.test
          223  +  check.test closure01.test coalesce.test collate1.test collate2.test
          224  +  collate3.test collate4.test collate5.test collate6.test collate7.test
          225  +  collate8.test collate9.test collateA.test colmeta.test colname.test
          226  +  conflict.test count.test coveridxscan.test createtab.test cse.test
          227  +  date.test dbstatus2.test dbstatus.test default.test delete2.test
          228  +  delete3.test delete.test descidx1.test descidx2.test descidx3.test
          229  +  distinctagg.test distinct.test e_createtable.test e_delete.test
          230  +  e_droptrigger.test e_dropview.test e_expr.test e_insert.test
          231  +  eqp.test e_reindex.test e_resolve.test e_select2.test e_select.test
          232  +  e_update.test exists.test expr.test fkey1.test fkey2.test fkey3.test
          233  +  fkey4.test fkey5.test func2.test func3.test func.test 
          234  +  in3.test in4.test in5.test index2.test index3.test
          235  +  index4.test index5.test indexedby.test index.test
          236  +  insert2.test insert3.test insert4.test insert5.test insert.test
          237  +  instr.test in.test intpkey.test join2.test join3.test join4.test
          238  +  join5.test join6.test join.test like2.test like.test limit.test
          239  +  minmax2.test minmax3.test minmax4.test minmax.test misc1.test misc2.test
          240  +  misc3.test misc4.test misc5.test misc6.test misc7.test orderby1.test
          241  +  orderby2.test orderby3.test orderby4.test randexpr1.test regexp1.test
          242  +  reindex.test rowhash.test rowid.test schema2.test schema3.test
          243  +  schema4.test schema5.test schema.test
          244  +  select1.test select2.test select3.test select4.test select5.test
          245  +  select6.test select7.test select8.test select9.test selectA.test
          246  +  selectB.test selectC.test selectD.test selectE.test sidedelete.test
          247  +  sort.test spellfix.test subquery2.test subquery.test subselect.test
          248  +  substr.test tkt-02a8e81d44.test tkt1435.test tkt1443.test tkt1444.test
          249  +  tkt1449.test tkt1473.test tkt1501.test tkt1512.test tkt1514.test
          250  +  tkt1536.test tkt1537.test tkt1567.test tkt1644.test tkt1667.test
          251  +  tkt1873.test tkt2141.test tkt2192.test tkt2213.test tkt2251.test
          252  +  tkt2285.test tkt2332.test tkt2339.test tkt2391.test tkt2409.test
          253  +  tkt2450.test tkt2565.test tkt2640.test tkt2643.test tkt2686.test
          254  +  tkt-26ff0c2d1e.test tkt2767.test tkt2817.test tkt2820.test tkt2822.test
          255  +  tkt2832.test tkt2854.test tkt2920.test tkt2927.test tkt2942.test
          256  +  tkt-2a5629202f.test tkt-2d1a5c67d.test tkt-2ea2425d34.test tkt3080.test
          257  +  tkt3093.test tkt3121.test tkt-31338dca7e.test tkt-313723c356.test
          258  +  tkt3201.test tkt3292.test tkt3298.test tkt3334.test tkt3346.test
          259  +  tkt3357.test tkt3419.test tkt3424.test tkt3442.test tkt3457.test
          260  +  tkt3461.test tkt3493.test tkt3508.test tkt3522.test tkt3527.test
          261  +  tkt3541.test tkt3554.test tkt3581.test tkt35xx.test tkt3630.test
          262  +  tkt3718.test tkt3731.test tkt3757.test tkt3761.test tkt3762.test
          263  +  tkt3773.test tkt3791.test tkt3793.test tkt3810.test tkt3824.test
          264  +  tkt3832.test tkt3838.test tkt3841.test tkt-385a5b56b9.test tkt3871.test
          265  +  tkt3879.test tkt-38cb5df375.test tkt3911.test tkt3918.test tkt3922.test
          266  +  tkt3929.test tkt3935.test tkt3992.test tkt3997.test tkt-3998683a16.test
          267  +  tkt-3a77c9714e.test tkt-3fe897352e.test tkt4018.test tkt-4a03edc4c8.test
          268  +  tkt-4dd95f6943.test tkt-54844eea3f.test tkt-5d863f876e.test
          269  +  tkt-5e10420e8d.test tkt-5ee23731f.test tkt-6bfb98dfc0.test
          270  +  tkt-752e1646fc.test tkt-78e04e52ea.test tkt-7a31705a7e6.test
          271  +  tkt-7bbfb7d442.test tkt-80ba201079.test tkt-80e031a00f.test
          272  +  tkt-8454a207b9.test tkt-91e2e8ba6f.test tkt-94c04eaadb.test
          273  +  tkt-9d68c883.test tkt-a7b7803e.test tkt-b1d3a2e531.test
          274  +  tkt-b351d95f9.test tkt-b72787b1.test tkt-bd484a090c.test
          275  +  tkt-bdc6bbbb38.test tkt-c48d99d690.test tkt-cbd054fa6b.test
          276  +  tkt-d11f09d36e.test tkt-d635236375.test tkt-d82e3f3721.test
          277  +  tkt-f3e5abed55.test tkt-f777251dc7a.test tkt-f7b4edec.test
          278  +  tkt-f973c7ac31.test tkt-fa7bf5ec.test tkt-fc62af4523.test
          279  +  tkt-fc7bd6358f.test trigger1.test trigger2.test trigger3.test
          280  +  trigger4.test trigger5.test trigger6.test trigger7.test trigger8.test
          281  +  trigger9.test triggerA.test triggerB.test triggerC.test triggerD.test
          282  +  types2.test types3.test types.test unique.test unordered.test
          283  +  update.test view.test vtab1.test vtab2.test vtab3.test vtab4.test
          284  +  vtab5.test vtab6.test vtab7.test vtab8.test vtab9.test vtab_alter.test
          285  +  vtabA.test vtabB.test vtabC.test vtabD.test vtabE.test
          286  +  vtabF.test where2.test where3.test where4.test where5.test where6.test
          287  +  where7.test where8m.test where8.test where9.test whereA.test whereB.test
          288  +  whereC.test whereD.test whereE.test whereF.test wherelimit.test
          289  +  where.test
          290  +}
   215    291   
   216    292   lappend ::testsuitelist xxx
   217    293   #-------------------------------------------------------------------------
   218    294   # Define the coverage related test suites:
   219    295   #
   220    296   #   coverage-wal
   221    297   #

Changes to test/securedel2.test.

    88     88     for {set i 2} {$i <= 850} {incr i 5} {
    89     89       incr n [detect_blob {} $i]
    90     90     }
    91     91     set n
    92     92   } {0}
    93     93   
    94     94   finish_test
    95         -

Changes to test/shared8.test.

   106    106     catchsql { SELECT * FROM v1 } db4
   107    107   } {1 {no such table: v1}}
   108    108   
   109    109   
   110    110   foreach db {db1 db2 db3 db4} { catch { $db close } }
   111    111   sqlite3_enable_shared_cache $::enable_shared_cache
   112    112   finish_test
   113         -

Changes to test/sharedlock.test.

    48     48   } {1 one 2 two 3 three}
    49     49   
    50     50   db close
    51     51   db2 close
    52     52   
    53     53   sqlite3_enable_shared_cache $::enable_shared_cache
    54     54   finish_test
    55         -

Changes to test/subquery.test.

   237    237     execsql {
   238    238       CREATE INDEX t4i ON t4(x);
   239    239       SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
   240    240     }
   241    241   } {10.0}
   242    242   do_test subquery-2.5.3.2 {
   243    243     # Verify that the t4i index was not used in the previous query
   244         -  set ::sqlite_query_plan
   245         -} {t4 {}}
          244  +  execsql {
          245  +    EXPLAIN QUERY PLAN
          246  +    SELECT * FROM t4 WHERE x IN (SELECT a FROM t3);
          247  +  }
          248  +} {/SCAN TABLE t4 /}
   246    249   do_test subquery-2.5.4 {
   247    250     execsql {
   248    251       DROP TABLE t3;
   249    252       DROP TABLE t4;
   250    253     }
   251    254   } {}
   252    255   

Changes to test/tester.tcl.

   547    547   
   548    548     if {![info exists ::G(match)] || [string match $::G(match) $name]} {
   549    549       if {[catch {uplevel #0 "$cmd;\n"} result]} {
   550    550         puts "\nError: $result"
   551    551         fail_test $name
   552    552       } else {
   553    553         if {[regexp {^~?/.*/$} $expected]} {
          554  +        # "expected" is of the form "/PATTERN/" then the result if correct if
          555  +        # regular expression PATTERN matches the result.  "~/PATTERN/" means
          556  +        # the regular expression must not match.
   554    557           if {[string index $expected 0]=="~"} {
   555    558             set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
   556    559             set ok [expr {![regexp $re $result]}]
   557    560           } else {
   558    561             set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
   559    562             set ok [regexp $re $result]
          563  +        }
          564  +      } elseif {[regexp {^~?\*.*\*$} $expected]} {
          565  +        # "expected" is of the form "*GLOB*" then the result if correct if
          566  +        # glob pattern GLOB matches the result.  "~/GLOB/" means
          567  +        # the glob must not match.
          568  +        if {[string index $expected 0]=="~"} {
          569  +          set e [string range $expected 1 end]
          570  +          set ok [expr {![string match $e $result]}]
          571  +        } else {
          572  +          set ok [string match $expected $result]
   560    573           }
   561    574         } else {
   562    575           set ok [expr {[string compare $result $expected]==0}]
   563    576         }
   564    577         if {!$ok} {
   565    578           # if {![info exists ::testprefix] || $::testprefix eq ""} {
   566    579           #   error "no test prefix"
................................................................................
   789    802     db close
   790    803     sqlite3_reset_auto_extension
   791    804