/ Check-in [bfc76ae1]
Login

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

Overview
Comment:Clean up and reorganize the elements of the various objects in the analysis tree for the NGQP.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | nextgen-query-plan-exp
Files: files | file ages | folders
SHA1:bfc76ae1e94fc5b7fd2c9484a36a8dfe6655d165
User & Date: drh 2013-06-06 19:16:33
Context
2013-06-06
19:25
Remove some commented-out code that was mistakenly left in the previous check-in. check-in: b4a5dbad user: drh tags: nextgen-query-plan-exp
19:16
Clean up and reorganize the elements of the various objects in the analysis tree for the NGQP. check-in: bfc76ae1 user: drh tags: nextgen-query-plan-exp
2013-06-05
23:39
Move the definitions of the WhereLevel and WhereInfo objects out of sqliteInt.h and into where.c. This will facilitate future refactoring of the internal query planner data structures. check-in: 1574653b user: drh tags: nextgen-query-plan-exp
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/where.c.

    41     41   typedef struct WhereAndInfo WhereAndInfo;
    42     42   typedef struct WhereLevel WhereLevel;
    43     43   typedef struct WhereLoop WhereLoop;
    44     44   typedef struct WherePath WherePath;
    45     45   typedef struct WhereTerm WhereTerm;
    46     46   typedef struct WhereLoopBuilder WhereLoopBuilder;
    47     47   typedef struct WhereScan WhereScan;
    48         -typedef struct WhereVtabPlan WhereVtabPlan;
    49         -
    50     48   
    51     49   /*
    52     50   ** For each nested loop in a WHERE clause implementation, the WhereInfo
    53     51   ** structure contains a single instance of this structure.  This structure
    54     52   ** is intended to be private to the where.c module and should not be
    55     53   ** access or modified by other modules.
    56     54   **
................................................................................
    82     80         } *aInLoop;           /* Information about each nested IN operator */
    83     81       } in;                 /* Used when plan.wsFlags&WHERE_IN_ABLE */
    84     82       Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
    85     83     } u;
    86     84     struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
    87     85   };
    88     86   
    89         -/*
    90         -** The WHERE clause processing routine has two halves.  The
    91         -** first part does the start of the WHERE loop and the second
    92         -** half does the tail of the WHERE loop.  An instance of
    93         -** this structure is returned by the first half and passed
    94         -** into the second half to give some continuity.
    95         -*/
    96         -struct WhereInfo {
    97         -  Parse *pParse;            /* Parsing and code generating context */
    98         -  SrcList *pTabList;        /* List of tables in the join */
    99         -  ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
   100         -  ExprList *pDistinct;      /* DISTINCT ON values, or NULL */
   101         -  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
   102         -  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
   103         -  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
   104         -  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
   105         -  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
   106         -  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
   107         -  int iTop;                 /* The very beginning of the WHERE loop */
   108         -  int iContinue;            /* Jump here to continue with next record */
   109         -  int iBreak;               /* Jump here to break out of the loop */
   110         -  int nLevel;               /* Number of nested loop */
   111         -  struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
   112         -  struct WhereLoop *pLoops; /* List of all WhereLoop objects */
   113         -  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
   114         -  double nRowOut;           /* Estimated number of output rows */
   115         -  WhereLevel a[1];          /* Information about each nest loop in WHERE */
   116         -};
   117         -
   118     87   /*
   119     88   ** Each instance of this object represents a way of evaluating one
   120     89   ** term of a join.  The WhereClause object holds a table of these
   121     90   ** objects using (maskSelf,prereq,) as the primary key.  Note that the
   122     91   ** same join term might have multiple associated WhereLoop objects.
   123     92   */
   124     93   struct WhereLoop {
................................................................................
   275    244   **           a AND ((b AND c) OR (d AND e)) AND f
   276    245   **
   277    246   ** There are separate WhereClause objects for the whole clause and for
   278    247   ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
   279    248   ** subclauses points to the WhereClause object for the whole clause.
   280    249   */
   281    250   struct WhereClause {
   282         -  Parse *pParse;           /* The parser context */
   283         -  WhereMaskSet *pMaskSet;  /* Mapping of table cursor numbers to bitmasks */
          251  +  WhereInfo *pWInfo;       /* WHERE clause processing context */
   284    252     WhereClause *pOuter;     /* Outer conjunction */
   285    253     u8 op;                   /* Split operator.  TK_AND or TK_OR */
   286         -  u16 wctrlFlags;          /* Might include WHERE_AND_ONLY */
   287    254     int nTerm;               /* Number of terms */
   288    255     int nSlot;               /* Number of entries in a[] */
   289    256     WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
   290    257   #if defined(SQLITE_SMALL_STACK)
   291    258     WhereTerm aStatic[1];    /* Initial static space for a[] */
   292    259   #else
   293    260     WhereTerm aStatic[8];    /* Initial static space for a[] */
................................................................................
   343    310   };
   344    311   
   345    312   /*
   346    313   ** This object is a factory for WhereLoop objects for a particular query.
   347    314   */
   348    315   struct WhereLoopBuilder {
   349    316     WhereInfo *pWInfo;        /* Information about this WHERE */
   350         -  sqlite3 *db;              /* Database connection */
   351         -  Parse *pParse;            /* Parsing context */
          317  +//  sqlite3 *db;              /* Database connection */
          318  +//  Parse *pParse;            /* Parsing context */
          319  +//  SrcList *pTabList;        /* FROM clause */
   352    320     WhereClause *pWC;         /* WHERE clause terms */
   353         -  SrcList *pTabList;        /* FROM clause */
   354    321     ExprList *pOrderBy;       /* ORDER BY clause */
   355    322     WhereLoop *pNew;          /* Template WhereLoop */
   356    323     WhereLoop *pBest;         /* If non-NULL, store single best loop here */
   357    324     int mxTerm;               /* Maximum number of aTerm[] entries on pNew */
   358    325   };
          326  +
          327  +/*
          328  +** The WHERE clause processing routine has two halves.  The
          329  +** first part does the start of the WHERE loop and the second
          330  +** half does the tail of the WHERE loop.  An instance of
          331  +** this structure is returned by the first half and passed
          332  +** into the second half to give some continuity.
          333  +*/
          334  +struct WhereInfo {
          335  +  Parse *pParse;            /* Parsing and code generating context */
          336  +  SrcList *pTabList;        /* List of tables in the join */
          337  +  ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
          338  +  ExprList *pDistinct;      /* DISTINCT ON values, or NULL */
          339  +  Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
          340  +  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
          341  +  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
          342  +  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
          343  +  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
          344  +  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
          345  +  int iTop;                 /* The very beginning of the WHERE loop */
          346  +  int iContinue;            /* Jump here to continue with next record */
          347  +  int iBreak;               /* Jump here to break out of the loop */
          348  +  int nLevel;               /* Number of nested loop */
          349  +  WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
          350  +  WhereClause sWC;          /* Decomposition of the WHERE clause */
          351  +  WhereLoop *pLoops;        /* List of all WhereLoop objects */
          352  +  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
          353  +  double nRowOut;           /* Estimated number of output rows */
          354  +  WhereLevel a[1];          /* Information about each nest loop in WHERE */
          355  +};
   359    356   
   360    357   /*
   361    358   ** Bitmasks for the operators that indices are able to exploit.  An
   362    359   ** OR-ed combination of these values can be used when searching for
   363    360   ** terms in the where clause.
   364    361   */
   365    362   #define WO_IN     0x001
................................................................................
   450    447   }
   451    448   
   452    449   /*
   453    450   ** Initialize a preallocated WhereClause structure.
   454    451   */
   455    452   static void whereClauseInit(
   456    453     WhereClause *pWC,        /* The WhereClause to be initialized */
   457         -  Parse *pParse,           /* The parsing context */
   458         -  WhereMaskSet *pMaskSet,  /* Mapping from table cursor numbers to bitmasks */
   459         -  u16 wctrlFlags           /* Might include WHERE_AND_ONLY */
          454  +  WhereInfo *pWInfo        /* The WHERE processing context */
   460    455   ){
   461         -  pWC->pParse = pParse;
   462         -  pWC->pMaskSet = pMaskSet;
          456  +  pWC->pWInfo = pWInfo;
   463    457     pWC->pOuter = 0;
   464    458     pWC->nTerm = 0;
   465    459     pWC->nSlot = ArraySize(pWC->aStatic);
   466    460     pWC->a = pWC->aStatic;
   467         -  pWC->wctrlFlags = wctrlFlags;
   468    461   }
   469    462   
   470    463   /* Forward reference */
   471    464   static void whereClauseClear(WhereClause*);
   472    465   
   473    466   /*
   474    467   ** Deallocate all memory associated with a WhereOrInfo object.
................................................................................
   489    482   /*
   490    483   ** Deallocate a WhereClause structure.  The WhereClause structure
   491    484   ** itself is not freed.  This routine is the inverse of whereClauseInit().
   492    485   */
   493    486   static void whereClauseClear(WhereClause *pWC){
   494    487     int i;
   495    488     WhereTerm *a;
   496         -  sqlite3 *db = pWC->pParse->db;
          489  +  sqlite3 *db = pWC->pWInfo->pParse->db;
   497    490     for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
   498    491       if( a->wtFlags & TERM_DYNAMIC ){
   499    492         sqlite3ExprDelete(db, a->pExpr);
   500    493       }
   501    494       if( a->wtFlags & TERM_ORINFO ){
   502    495         whereOrInfoDelete(db, a->u.pOrInfo);
   503    496       }else if( a->wtFlags & TERM_ANDINFO ){
................................................................................
   530    523   */
   531    524   static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
   532    525     WhereTerm *pTerm;
   533    526     int idx;
   534    527     testcase( wtFlags & TERM_VIRTUAL );  /* EV: R-00211-15100 */
   535    528     if( pWC->nTerm>=pWC->nSlot ){
   536    529       WhereTerm *pOld = pWC->a;
   537         -    sqlite3 *db = pWC->pParse->db;
          530  +    sqlite3 *db = pWC->pWInfo->pParse->db;
   538    531       pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
   539    532       if( pWC->a==0 ){
   540    533         if( wtFlags & TERM_DYNAMIC ){
   541    534           sqlite3ExprDelete(db, p);
   542    535         }
   543    536         pWC->a = pOld;
   544    537         return 0;
................................................................................
   807    800                 pScan->nEquiv += 2;
   808    801               }
   809    802             }
   810    803             if( (pTerm->eOperator & pScan->opMask)!=0 ){
   811    804               /* Verify the affinity and collating sequence match */
   812    805               if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
   813    806                 CollSeq *pColl;
          807  +              Parse *pParse = pWC->pWInfo->pParse;
   814    808                 pX = pTerm->pExpr;
   815    809                 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
   816    810                   continue;
   817    811                 }
   818    812                 assert(pX->pLeft);
   819         -              pColl = sqlite3BinaryCompareCollSeq(pWC->pParse,
          813  +              pColl = sqlite3BinaryCompareCollSeq(pParse,
   820    814                                                     pX->pLeft, pX->pRight);
   821         -              if( pColl==0 ) pColl = pWC->pParse->db->pDfltColl;
          815  +              if( pColl==0 ) pColl = pParse->db->pDfltColl;
   822    816                 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
   823    817                   continue;
   824    818                 }
   825    819               }
   826    820               if( (pTerm->eOperator & WO_EQ)!=0
   827    821                && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
   828    822                && pX->iTable==pScan->aEquiv[0]
................................................................................
  1173   1167   ** zero.  This term is not useful for search.
  1174   1168   */
  1175   1169   static void exprAnalyzeOrTerm(
  1176   1170     SrcList *pSrc,            /* the FROM clause */
  1177   1171     WhereClause *pWC,         /* the complete WHERE clause */
  1178   1172     int idxTerm               /* Index of the OR-term to be analyzed */
  1179   1173   ){
  1180         -  Parse *pParse = pWC->pParse;            /* Parser context */
         1174  +  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
         1175  +  Parse *pParse = pWInfo->pParse;         /* Parser context */
  1181   1176     sqlite3 *db = pParse->db;               /* Database connection */
  1182   1177     WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
  1183   1178     Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
  1184         -  WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */
  1185   1179     int i;                                  /* Loop counters */
  1186   1180     WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
  1187   1181     WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
  1188   1182     WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
  1189   1183     Bitmask chngToIN;         /* Tables that might satisfy case 1 */
  1190   1184     Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
  1191   1185   
................................................................................
  1196   1190     */
  1197   1191     assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
  1198   1192     assert( pExpr->op==TK_OR );
  1199   1193     pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
  1200   1194     if( pOrInfo==0 ) return;
  1201   1195     pTerm->wtFlags |= TERM_ORINFO;
  1202   1196     pOrWc = &pOrInfo->wc;
  1203         -  whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
         1197  +  whereClauseInit(pOrWc, pWInfo);
  1204   1198     whereSplit(pOrWc, pExpr, TK_OR);
  1205   1199     exprAnalyzeAll(pSrc, pOrWc);
  1206   1200     if( db->mallocFailed ) return;
  1207   1201     assert( pOrWc->nTerm>=2 );
  1208   1202   
  1209   1203     /*
  1210   1204     ** Compute the set of tables that might satisfy cases 1 or 2.
................................................................................
  1222   1216           WhereTerm *pAndTerm;
  1223   1217           int j;
  1224   1218           Bitmask b = 0;
  1225   1219           pOrTerm->u.pAndInfo = pAndInfo;
  1226   1220           pOrTerm->wtFlags |= TERM_ANDINFO;
  1227   1221           pOrTerm->eOperator = WO_AND;
  1228   1222           pAndWC = &pAndInfo->wc;
  1229         -        whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
         1223  +        whereClauseInit(pAndWC, pWC->pWInfo);
  1230   1224           whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
  1231   1225           exprAnalyzeAll(pSrc, pAndWC);
  1232   1226           pAndWC->pOuter = pWC;
  1233   1227           testcase( db->mallocFailed );
  1234   1228           if( !db->mallocFailed ){
  1235   1229             for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
  1236   1230               assert( pAndTerm->pExpr );
  1237   1231               if( allowedOp(pAndTerm->pExpr->op) ){
  1238         -              b |= getMask(pMaskSet, pAndTerm->leftCursor);
         1232  +              b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
  1239   1233               }
  1240   1234             }
  1241   1235           }
  1242   1236           indexable &= b;
  1243   1237         }
  1244   1238       }else if( pOrTerm->wtFlags & TERM_COPIED ){
  1245   1239         /* Skip this term for now.  We revisit it when we process the
  1246   1240         ** corresponding TERM_VIRTUAL term */
  1247   1241       }else{
  1248   1242         Bitmask b;
  1249         -      b = getMask(pMaskSet, pOrTerm->leftCursor);
         1243  +      b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
  1250   1244         if( pOrTerm->wtFlags & TERM_VIRTUAL ){
  1251   1245           WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
  1252         -        b |= getMask(pMaskSet, pOther->leftCursor);
         1246  +        b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
  1253   1247         }
  1254   1248         indexable &= b;
  1255   1249         if( (pOrTerm->eOperator & WO_EQ)==0 ){
  1256   1250           chngToIN = 0;
  1257   1251         }else{
  1258   1252           chngToIN &= b;
  1259   1253         }
................................................................................
  1307   1301           pOrTerm->wtFlags &= ~TERM_OR_OK;
  1308   1302           if( pOrTerm->leftCursor==iCursor ){
  1309   1303             /* This is the 2-bit case and we are on the second iteration and
  1310   1304             ** current term is from the first iteration.  So skip this term. */
  1311   1305             assert( j==1 );
  1312   1306             continue;
  1313   1307           }
  1314         -        if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){
         1308  +        if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
  1315   1309             /* This term must be of the form t1.a==t2.b where t2 is in the
  1316   1310             ** chngToIN set but t1 is not.  This term will be either preceeded
  1317   1311             ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
  1318   1312             ** and use its inversion. */
  1319   1313             testcase( pOrTerm->wtFlags & TERM_COPIED );
  1320   1314             testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
  1321   1315             assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
................................................................................
  1326   1320           break;
  1327   1321         }
  1328   1322         if( i<0 ){
  1329   1323           /* No candidate table+column was found.  This can only occur
  1330   1324           ** on the second iteration */
  1331   1325           assert( j==1 );
  1332   1326           assert( IsPowerOfTwo(chngToIN) );
  1333         -        assert( chngToIN==getMask(pMaskSet, iCursor) );
         1327  +        assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
  1334   1328           break;
  1335   1329         }
  1336   1330         testcase( j==1 );
  1337   1331   
  1338   1332         /* We have found a candidate table and column.  Check to see if that
  1339   1333         ** table and column is common to every term in the OR clause */
  1340   1334         okToChngToIN = 1;
................................................................................
  1375   1369   
  1376   1370         for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
  1377   1371           if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
  1378   1372           assert( pOrTerm->eOperator & WO_EQ );
  1379   1373           assert( pOrTerm->leftCursor==iCursor );
  1380   1374           assert( pOrTerm->u.leftColumn==iColumn );
  1381   1375           pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
  1382         -        pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup);
         1376  +        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
  1383   1377           pLeft = pOrTerm->pExpr->pLeft;
  1384   1378         }
  1385   1379         assert( pLeft!=0 );
  1386   1380         pDup = sqlite3ExprDup(db, pLeft, 0);
  1387   1381         pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
  1388   1382         if( pNew ){
  1389   1383           int idxNew;
................................................................................
  1424   1418   ** and the copy has idxParent set to the index of the original term.
  1425   1419   */
  1426   1420   static void exprAnalyze(
  1427   1421     SrcList *pSrc,            /* the FROM clause */
  1428   1422     WhereClause *pWC,         /* the WHERE clause */
  1429   1423     int idxTerm               /* Index of the term to be analyzed */
  1430   1424   ){
         1425  +  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
  1431   1426     WhereTerm *pTerm;                /* The term to be analyzed */
  1432   1427     WhereMaskSet *pMaskSet;          /* Set of table index masks */
  1433   1428     Expr *pExpr;                     /* The expression to be analyzed */
  1434   1429     Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
  1435   1430     Bitmask prereqAll;               /* Prerequesites of pExpr */
  1436   1431     Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
  1437   1432     Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
  1438   1433     int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
  1439   1434     int noCase = 0;                  /* LIKE/GLOB distinguishes case */
  1440   1435     int op;                          /* Top-level operator.  pExpr->op */
  1441         -  Parse *pParse = pWC->pParse;     /* Parsing context */
         1436  +  Parse *pParse = pWInfo->pParse;  /* Parsing context */
  1442   1437     sqlite3 *db = pParse->db;        /* Database connection */
  1443   1438   
  1444   1439     if( db->mallocFailed ){
  1445   1440       return;
  1446   1441     }
  1447   1442     pTerm = &pWC->a[idxTerm];
  1448         -  pMaskSet = pWC->pMaskSet;
         1443  +  pMaskSet = &pWInfo->sMaskSet;
  1449   1444     pExpr = pTerm->pExpr;
  1450   1445     assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
  1451   1446     prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
  1452   1447     op = pExpr->op;
  1453   1448     if( op==TK_IN ){
  1454   1449       assert( pExpr->pRight==0 );
  1455   1450       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
  3064   3059   /*
  3065   3060   ** Generate code for the start of the iLevel-th loop in the WHERE clause
  3066   3061   ** implementation described by pWInfo.
  3067   3062   */
  3068   3063   static Bitmask codeOneLoopStart(
  3069   3064     WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
  3070   3065     int iLevel,          /* Which level of pWInfo->a[] should be coded */
  3071         -  u16 wctrlFlags,      /* One of the WHERE_* flags defined in sqliteInt.h */
  3072   3066     Bitmask notReady     /* Which tables are currently available */
  3073   3067   ){
  3074   3068     int j, k;            /* Loop counters */
  3075   3069     int iCur;            /* The VDBE cursor for the table */
  3076   3070     int addrNxt;         /* Where to jump to continue with the next IN case */
  3077   3071     int omitTable;       /* True if we use the index only */
  3078   3072     int bRev;            /* True if we need to scan in reverse order */
................................................................................
  3087   3081     int addrCont;                   /* Jump here to continue with next cycle */
  3088   3082     int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
  3089   3083     int iReleaseReg = 0;      /* Temp register to free before returning */
  3090   3084     Bitmask newNotReady;      /* Return value */
  3091   3085   
  3092   3086     pParse = pWInfo->pParse;
  3093   3087     v = pParse->pVdbe;
  3094         -  pWC = pWInfo->pWC;
         3088  +  pWC = &pWInfo->sWC;
  3095   3089     pLevel = &pWInfo->a[iLevel];
  3096   3090     pLoop = pLevel->pWLoop;
  3097   3091     pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
  3098   3092     iCur = pTabItem->iCursor;
  3099   3093     bRev = (pWInfo->revMask>>iLevel)&1;
  3100   3094     omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
  3101         -           && (wctrlFlags & WHERE_FORCE_TABLE)==0;
         3095  +           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
  3102   3096     VdbeNoopComment((v, "Begin Join Loop %d", iLevel));
  3103   3097   
  3104   3098     /* Create labels for the "break" and "continue" instructions
  3105   3099     ** for the current loop.  Jump to addrBrk to break out of a loop.
  3106   3100     ** Jump to cont to go immediately to the next iteration of the
  3107   3101     ** loop.
  3108   3102     **
................................................................................
  3351   3345       ** was passed to this function to implement a "SELECT min(x) ..." 
  3352   3346       ** query, then the caller will only allow the loop to run for
  3353   3347       ** a single iteration. This means that the first row returned
  3354   3348       ** should not have a NULL value stored in 'x'. If column 'x' is
  3355   3349       ** the first one after the nEq equality constraints in the index,
  3356   3350       ** this requires some special handling.
  3357   3351       */
  3358         -    if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0
         3352  +    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
  3359   3353        && (pWInfo->nOBSat>0)
  3360   3354        && (pIdx->nColumn>nEq)
  3361   3355       ){
  3362   3356         /* assert( pOrderBy->nExpr==1 ); */
  3363   3357         /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */
  3364   3358         isMinQuery = 1;
  3365   3359         nExtraReg = 1;
................................................................................
  3620   3614       ** immediately following the OP_Return at the bottom of the loop. This
  3621   3615       ** is required in a few obscure LEFT JOIN cases where control jumps
  3622   3616       ** over the top of the loop into the body of it. In this case the 
  3623   3617       ** correct response for the end-of-loop code (the OP_Return) is to 
  3624   3618       ** fall through to the next instruction, just as an OP_Next does if
  3625   3619       ** called on an uninitialized cursor.
  3626   3620       */
  3627         -    if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         3621  +    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  3628   3622         regRowset = ++pParse->nMem;
  3629   3623         regRowid = ++pParse->nMem;
  3630   3624         sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
  3631   3625       }
  3632   3626       iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
  3633   3627   
  3634   3628       /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
................................................................................
  3675   3669                           WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
  3676   3670           assert( pSubWInfo || pParse->nErr || pParse->db->mallocFailed );
  3677   3671           if( pSubWInfo ){
  3678   3672             WhereLoop *pSubLoop;
  3679   3673             explainOneScan(
  3680   3674                 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  3681   3675             );
  3682         -          if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         3676  +          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  3683   3677               int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  3684   3678               int r;
  3685   3679               r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, 
  3686   3680                                            regRowid, 0);
  3687   3681               sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
  3688   3682                                    sqlite3VdbeCurrentAddr(v)+2, r, iSet);
  3689   3683             }
................................................................................
  3747   3741       static const u8 aStart[] = { OP_Rewind, OP_Last };
  3748   3742       assert( bRev==0 || bRev==1 );
  3749   3743       pLevel->op = aStep[bRev];
  3750   3744       pLevel->p1 = iCur;
  3751   3745       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
  3752   3746       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  3753   3747     }
  3754         -  newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur);
         3748  +  newNotReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
  3755   3749   
  3756   3750     /* Insert code to test every subexpression that can be completely
  3757   3751     ** computed using the current set of tables.
  3758   3752     **
  3759   3753     ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
  3760   3754     ** the use of indices become tests that are evaluated against each row of
  3761   3755     ** the relevant input tables.
................................................................................
  3902   3896   }
  3903   3897   
  3904   3898   /*
  3905   3899   ** Free a WhereInfo structure
  3906   3900   */
  3907   3901   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
  3908   3902     if( ALWAYS(pWInfo) ){
  3909         -    whereClauseClear(pWInfo->pWC);
         3903  +    whereClauseClear(&pWInfo->sWC);
  3910   3904       while( pWInfo->pLoops ){
  3911   3905         WhereLoop *p = pWInfo->pLoops;
  3912   3906         pWInfo->pLoops = p->pNextLoop;
  3913   3907         whereLoopDelete(db, p);
  3914   3908       }
  3915   3909       sqlite3DbFree(db, pWInfo);
  3916   3910     }
................................................................................
  3941   3935   **    (4)  The template has the same or lower cost than the current loop
  3942   3936   **    (5)  The template uses more terms of the same index but has no additional
  3943   3937   **         dependencies          
  3944   3938   */
  3945   3939   static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
  3946   3940     WhereLoop **ppPrev, *p, *pNext = 0, *pToFree = 0;
  3947   3941     WhereTerm **paTerm = 0;
  3948         -  sqlite3 *db = pBuilder->db;
  3949   3942     WhereInfo *pWInfo = pBuilder->pWInfo;
         3943  +  sqlite3 *db = pWInfo->pParse->db;
  3950   3944   
  3951   3945     /* If pBuilder->pBest is defined, then only keep track of the single
  3952   3946     ** best WhereLoop.  pBuilder->pBest->maskSelf==0 indicates that no
  3953   3947     ** prior WhereLoops have been evaluated and that the current pTemplate
  3954   3948     ** is therefore the first and hence the best and should be retained.
  3955   3949     */
  3956   3950     if( (p = pBuilder->pBest)!=0 ){
................................................................................
  3965   3959       }
  3966   3960       *p = *pTemplate;
  3967   3961       p->aTerm = 0;
  3968   3962       p->u.vtab.needFree = 0;
  3969   3963   #if WHERETRACE_ENABLED
  3970   3964       if( sqlite3WhereTrace & 0x8 ){
  3971   3965         sqlite3DebugPrintf("ins-best: ");
  3972         -      whereLoopPrint(pTemplate, pBuilder->pTabList);
         3966  +      whereLoopPrint(pTemplate, pWInfo->pTabList);
  3973   3967       }
  3974   3968   #endif
  3975   3969       return SQLITE_OK;
  3976   3970     }
  3977   3971   
  3978   3972     /* Search for an existing WhereLoop to overwrite, or which takes
  3979   3973     ** priority over pTemplate.
................................................................................
  4017   4011     ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
  4018   4012     ** WhereLoop and insert it.
  4019   4013     */
  4020   4014   #if WHERETRACE_ENABLED
  4021   4015     if( sqlite3WhereTrace & 0x8 ){
  4022   4016       if( p!=0 ){
  4023   4017         sqlite3DebugPrintf("ins-del:  ");
  4024         -      whereLoopPrint(p, pBuilder->pTabList);
         4018  +      whereLoopPrint(p, pWInfo->pTabList);
  4025   4019       }
  4026   4020       sqlite3DebugPrintf("ins-new:  ");
  4027         -    whereLoopPrint(pTemplate, pBuilder->pTabList);
         4021  +    whereLoopPrint(pTemplate, pWInfo->pTabList);
  4028   4022     }
  4029   4023   #endif
  4030   4024     if( p==0 ){
  4031   4025       p = pToFree = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
  4032   4026       if( p==0 ) return SQLITE_NOMEM;
  4033   4027     }
  4034   4028     if( pTemplate->nTerm ){
................................................................................
  4056   4050     return SQLITE_OK;
  4057   4051   
  4058   4052     /* Jump here if the insert is a no-op */
  4059   4053   whereLoopInsert_noop:
  4060   4054   #if WHERETRACE_ENABLED
  4061   4055     if( sqlite3WhereTrace & 0x8 ){
  4062   4056       sqlite3DebugPrintf("ins-noop: ");
  4063         -    whereLoopPrint(pTemplate, pBuilder->pTabList);
         4057  +    whereLoopPrint(pTemplate, pWInfo->pTabList);
  4064   4058     }
  4065   4059   #endif
  4066   4060     return SQLITE_OK;  
  4067   4061   }
  4068   4062   
  4069   4063   /*
  4070   4064   ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
................................................................................
  4075   4069   */
  4076   4070   static int whereLoopAddBtreeIndex(
  4077   4071     WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
  4078   4072     struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
  4079   4073     Index *pProbe,                  /* An index on pSrc */
  4080   4074     int nInMul                      /* Number of iterations due to IN */
  4081   4075   ){
  4082         -  sqlite3 *db;                    /* Database connection malloc context */
         4076  +  WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
         4077  +  Parse *pParse = pWInfo->pParse;        /* Parsing context */
         4078  +  sqlite3 *db = pParse->db;       /* Database connection malloc context */
  4083   4079     WhereLoop *pNew;                /* Template WhereLoop under construction */
  4084   4080     WhereTerm *pTerm;               /* A WhereTerm under consideration */
  4085   4081     int opMask;                     /* Valid operators for constraints */
  4086   4082     WhereScan scan;                 /* Iterator for WHERE terms */
  4087   4083     WhereLoop savedLoop;            /* Saved original content of pNew[] */
  4088   4084     int iCol;                       /* Index of the column in the table */
  4089   4085     int rc = SQLITE_OK;             /* Return code */
  4090   4086     tRowcnt iRowEst;                /* Estimated index selectivity */
  4091   4087     double rLogSize;                /* Logarithm of table size */
  4092   4088     WhereTerm *pTop, *pBtm;         /* Top and bottom range constraints */
  4093   4089   
  4094         -  db = pBuilder->db;
  4095   4090     pNew = pBuilder->pNew;
  4096   4091     if( db->mallocFailed ) return SQLITE_NOMEM;
  4097   4092   
  4098   4093     assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
  4099   4094     assert( pNew->u.btree.nEq<=pProbe->nColumn );
  4100   4095     assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
  4101   4096     if( pNew->wsFlags & WHERE_BTM_LIMIT ){
................................................................................
  4169   4164         pTop = pTerm;
  4170   4165         pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
  4171   4166                        pNew->aTerm[pNew->nTerm-2] : 0;
  4172   4167       }
  4173   4168       if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
  4174   4169         /* Adjust nOut and rRun for STAT3 range values */
  4175   4170         double rDiv;
  4176         -      whereRangeScanEst(pBuilder->pParse, pProbe, pNew->u.btree.nEq,
         4171  +      whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
  4177   4172                           pBtm, pTop, &rDiv);
  4178   4173         pNew->nOut = savedLoop.nOut/rDiv;
  4179   4174       }
  4180   4175   #ifdef SQLITE_ENABLE_STAT3
  4181   4176       if( pNew->u.btree.nEq==1 && pProbe->nSample ){
  4182   4177         if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
  4183         -        rc = whereEqualScanEst(pBuilder->pParse, pProbe, pTerm->pExpr->pRight,
         4178  +        rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight,
  4184   4179                                  &pNew->nOut);
  4185   4180         }else if( (pTerm->eOperator & WO_IN)
  4186   4181                &&  !ExprHasProperty(pTerm->pExpr, EP_xIsSelect)  ){
  4187         -        rc = whereInScanEst(pBuilder->pParse, pProbe, pTerm->pExpr->x.pList,
         4182  +        rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList,
  4188   4183                                &pNew->nOut);
  4189   4184   
  4190   4185         }
  4191   4186       }
  4192   4187   #endif
  4193   4188       if( pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK) ){
  4194   4189         pNew->rRun += pNew->nOut;  /* Unit step cost to reach each row */
................................................................................
  4223   4218     Index *pIndex,
  4224   4219     int iCursor
  4225   4220   ){
  4226   4221     ExprList *pOB;
  4227   4222     int iCol;
  4228   4223     int ii;
  4229   4224   
  4230         -  if( (pOB = pBuilder->pOrderBy)==0 ) return 0;
         4225  +  if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
  4231   4226     iCol = pIndex->aiColumn[0];
  4232   4227     for(ii=0; ii<pOB->nExpr; ii++){
  4233   4228       Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
  4234   4229       if( pExpr->op!=TK_COLUMN ) return 0;
  4235   4230       if( pExpr->iTable==iCursor ){
  4236   4231         if( pExpr->iColumn==iCol ) return 1;
  4237   4232         return 0;
................................................................................
  4245   4240   ** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
  4246   4241   ** a b-tree table, not a virtual table.
  4247   4242   */
  4248   4243   static int whereLoopAddBtree(
  4249   4244     WhereLoopBuilder *pBuilder, /* WHERE clause information */
  4250   4245     Bitmask mExtra              /* Extra prerequesites for using this table */
  4251   4246   ){
         4247  +  WhereInfo *pWInfo;          /* WHERE analysis context */
  4252   4248     Index *pProbe;              /* An index we are evaluating */
  4253   4249     Index sPk;                  /* A fake index object for the primary key */
  4254   4250     tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
  4255   4251     int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
         4252  +  SrcList *pTabList;          /* The FROM clause */
  4256   4253     struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
  4257   4254     WhereLoop *pNew;            /* Template WhereLoop object */
  4258   4255     int rc = SQLITE_OK;         /* Return code */
  4259   4256     int iSortIdx = 1;           /* Index number */
  4260   4257     int b;                      /* A boolean value */
  4261   4258     double rSize;               /* number of rows in the table */
  4262   4259     double rLogSize;            /* Logarithm of the number of rows in the table */
  4263   4260     
  4264   4261     pNew = pBuilder->pNew;
  4265         -  pSrc = pBuilder->pTabList->a + pNew->iTab;
         4262  +  pWInfo = pBuilder->pWInfo;
         4263  +  pTabList = pWInfo->pTabList;
         4264  +  pSrc = pTabList->a + pNew->iTab;
  4266   4265     assert( !IsVirtual(pSrc->pTab) );
  4267   4266   
  4268   4267     if( pSrc->pIndex ){
  4269   4268       /* An INDEXED BY clause specifies a particular index to use */
  4270   4269       pProbe = pSrc->pIndex;
  4271   4270     }else{
  4272   4271       /* There is no INDEXED BY clause.  Create a fake Index object in local
................................................................................
  4291   4290       pProbe = &sPk;
  4292   4291     }
  4293   4292     rSize = (double)pSrc->pTab->nRowEst;
  4294   4293     rLogSize = estLog(rSize);
  4295   4294   
  4296   4295     /* Automatic indexes */
  4297   4296     if( !pBuilder->pBest
  4298         -   && pBuilder->pTabList->nSrc>1
  4299         -   && (pBuilder->pParse->db->flags & SQLITE_AutoIndex)!=0 
         4297  +   && pTabList->nSrc>1
         4298  +   && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 
  4300   4299      && !pSrc->viaCoroutine
  4301   4300      && !pSrc->notIndexed
  4302   4301      && !pSrc->isCorrelated
  4303   4302     ){
  4304   4303       /* Generate auto-index WhereLoops */
  4305   4304       WhereClause *pWC = pBuilder->pWC;
  4306   4305       WhereTerm *pTerm;
................................................................................
  4352   4351           }
  4353   4352         }
  4354   4353         pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
  4355   4354   
  4356   4355         /* Full scan via index */
  4357   4356         if( (m==0 || b)
  4358   4357          && pProbe->bUnordered==0
  4359         -       && (pBuilder->pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
         4358  +       && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
  4360   4359          && sqlite3GlobalConfig.bUseCis
  4361         -       && OptimizationEnabled(pBuilder->pParse->db, SQLITE_CoverIdxScan)
         4360  +       && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
  4362   4361         ){
  4363   4362           pNew->iSortIdx = b ? iSortIdx : 0;
  4364   4363           pNew->nOut = rSize;
  4365   4364           pNew->rRun = (m==0) ? (rSize + rLogSize)*(1+b) : (rSize*rLogSize);
  4366   4365           rc = whereLoopInsert(pBuilder, pNew);
  4367   4366           if( rc ) break;
  4368   4367         }
................................................................................
  4380   4379   ** Add all WhereLoop objects for a table of the join identified by
  4381   4380   ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
  4382   4381   */
  4383   4382   static int whereLoopAddVirtual(
  4384   4383     WhereLoopBuilder *pBuilder,  /* WHERE clause information */
  4385   4384     Bitmask mExtra               /* Extra prerequesites for using this table */
  4386   4385   ){
         4386  +  WhereInfo *pWInfo;           /* WHERE analysis context */
  4387   4387     Parse *pParse;               /* The parsing context */
  4388   4388     WhereClause *pWC;            /* The WHERE clause */
  4389   4389     struct SrcList_item *pSrc;   /* The FROM clause term to search */
  4390   4390     Table *pTab;
  4391   4391     sqlite3 *db;
  4392   4392     sqlite3_index_info *pIdxInfo;
  4393   4393     struct sqlite3_index_constraint *pIdxCons;
................................................................................
  4397   4397     int iTerm, mxTerm;
  4398   4398     int seenIn = 0;              /* True if an IN operator is seen */
  4399   4399     int seenVar = 0;             /* True if a non-constant constraint is seen */
  4400   4400     int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
  4401   4401     WhereLoop *pNew;
  4402   4402     int rc = SQLITE_OK;
  4403   4403   
  4404         -  pParse = pBuilder->pParse;
         4404  +  pWInfo = pBuilder->pWInfo;
         4405  +  pParse = pWInfo->pParse;
  4405   4406     db = pParse->db;
  4406   4407     pWC = pBuilder->pWC;
  4407   4408     pNew = pBuilder->pNew;
  4408         -  pSrc = &pBuilder->pTabList->a[pNew->iTab];
         4409  +  pSrc = &pWInfo->pTabList->a[pNew->iTab];
  4409   4410     pTab = pSrc->pTab;
  4410   4411     assert( IsVirtual(pTab) );
  4411   4412     pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
  4412   4413     if( pIdxInfo==0 ) return SQLITE_NOMEM;
  4413   4414     pNew->prereq = 0;
  4414   4415     pNew->rSetup = 0;
  4415   4416     pNew->wsFlags = WHERE_VIRTUALTABLE;
................................................................................
  4528   4529   }
  4529   4530   
  4530   4531   /*
  4531   4532   ** Add WhereLoop entries to handle OR terms.  This works for either
  4532   4533   ** btrees or virtual tables.
  4533   4534   */
  4534   4535   static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
         4536  +  WhereInfo *pWInfo = pBuilder->pWInfo;
  4535   4537     WhereClause *pWC;
  4536   4538     WhereLoop *pNew;
  4537   4539     WhereTerm *pTerm, *pWCEnd;
  4538   4540     int rc = SQLITE_OK;
  4539   4541     int iCur;
  4540   4542     WhereClause tempWC;
  4541   4543     WhereLoopBuilder sSubBuild;
  4542   4544     WhereLoop sBest;
  4543   4545     struct SrcList_item *pItem;
  4544   4546     
  4545         -
  4546   4547     pWC = pBuilder->pWC;
  4547         -  if( pWC->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
         4548  +  if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
  4548   4549     pWCEnd = pWC->a + pWC->nTerm;
  4549   4550     pNew = pBuilder->pNew;
  4550   4551   
  4551   4552     for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
  4552   4553       if( (pTerm->eOperator & WO_OR)!=0
  4553   4554        && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0 
  4554   4555       ){
................................................................................
  4555   4556         WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
  4556   4557         WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
  4557   4558         WhereTerm *pOrTerm;
  4558   4559         double rTotal = 0;
  4559   4560         double nRow = 0;
  4560   4561         Bitmask prereq = mExtra;
  4561   4562       
  4562         -      pItem = pBuilder->pTabList->a + pNew->iTab;
         4563  +      pItem = pWInfo->pTabList->a + pNew->iTab;
  4563   4564         iCur = pItem->iCursor;
  4564   4565         sSubBuild = *pBuilder;
  4565   4566         sSubBuild.pOrderBy = 0;
  4566   4567         sSubBuild.pBest = &sBest;
  4567   4568   
  4568   4569         for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
  4569   4570           if( (pOrTerm->eOperator & WO_AND)!=0 ){
  4570   4571             sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
  4571   4572           }else if( pOrTerm->leftCursor==iCur ){
  4572         -          tempWC.pParse = pWC->pParse;
  4573         -          tempWC.pMaskSet = pWC->pMaskSet;
         4573  +          tempWC.pWInfo = pWC->pWInfo;
  4574   4574             tempWC.pOuter = pWC;
  4575   4575             tempWC.op = TK_AND;
  4576         -          tempWC.wctrlFlags = 0;
  4577   4576             tempWC.nTerm = 1;
  4578   4577             tempWC.a = pOrTerm;
  4579   4578             sSubBuild.pWC = &tempWC;
  4580   4579           }else{
  4581   4580             continue;
  4582   4581           }
  4583   4582           sBest.maskSelf = 0;
................................................................................
  4606   4605     return rc;
  4607   4606   }
  4608   4607   
  4609   4608   /*
  4610   4609   ** Add all WhereLoop objects for all tables 
  4611   4610   */
  4612   4611   static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
         4612  +  WhereInfo *pWInfo = pBuilder->pWInfo;
  4613   4613     Bitmask mExtra = 0;
  4614   4614     Bitmask mPrior = 0;
  4615   4615     int iTab;
  4616         -  SrcList *pTabList = pBuilder->pTabList;
         4616  +  SrcList *pTabList = pWInfo->pTabList;
  4617   4617     struct SrcList_item *pItem;
  4618   4618     WhereClause *pWC = pBuilder->pWC;
  4619         -  sqlite3 *db = pBuilder->db;
  4620         -  int nTabList = pBuilder->pWInfo->nLevel;
         4619  +  sqlite3 *db = pWInfo->pParse->db;
         4620  +  int nTabList = pWInfo->nLevel;
  4621   4621     int rc = SQLITE_OK;
  4622   4622     WhereLoop *pNew;
  4623   4623   
  4624   4624     /* Loop over the tables in the join, from left to right */
  4625   4625     pBuilder->pNew = pNew = sqlite3DbMallocZero(db, sizeof(WhereLoop));
  4626   4626     if( pNew==0 ) return SQLITE_NOMEM;
  4627   4627     pBuilder->mxTerm = pWC->nTerm+1;
................................................................................
  4633   4633     pNew->aTerm = sqlite3DbMallocZero(db,pBuilder->mxTerm*sizeof(pNew->aTerm[0]));
  4634   4634     if( pNew->aTerm==0 ){
  4635   4635       rc = SQLITE_NOMEM;
  4636   4636       goto whereLoopAddAll_end;
  4637   4637     }
  4638   4638     for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
  4639   4639       pNew->iTab = iTab;
  4640         -    pNew->maskSelf = getMask(pWC->pMaskSet, pItem->iCursor);
         4640  +    pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
  4641   4641       if( (pItem->jointype & (JT_LEFT|JT_CROSS))!=0 ){
  4642   4642         mExtra = mPrior;
  4643   4643       }
  4644   4644       if( IsVirtual(pItem->pTab) ){
  4645   4645         rc = whereLoopAddVirtual(pBuilder, mExtra);
  4646   4646       }else{
  4647   4647         rc = whereLoopAddBtree(pBuilder, mExtra);
................................................................................
  4694   4694     Expr *pOBExpr;        /* An expression from the ORDER BY clause */
  4695   4695     CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
  4696   4696     Index *pIndex;        /* The index associated with pLoop */
  4697   4697     sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
  4698   4698     Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
  4699   4699     Bitmask obDone;       /* Mask of all ORDER BY terms */
  4700   4700     Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
  4701         -  WhereMaskSet *pMaskSet;     /* WhereMaskSet object for this where clause */
  4702   4701     
  4703   4702   
  4704   4703     /*
  4705   4704     ** We say the WhereLoop is "one-row" if it generates no more than one
  4706   4705     ** row of output.  A WhereLoop is one-row if all of the following are true:
  4707   4706     **  (a) All index columns match with WHERE_COLUMN_EQ.
  4708   4707     **  (b) The index is unique
................................................................................
  4735   4734     if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
  4736   4735   
  4737   4736     nOrderBy = pOrderBy->nExpr;
  4738   4737     if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
  4739   4738     isOrderDistinct = 1;
  4740   4739     obDone = MASKBIT(nOrderBy)-1;
  4741   4740     orderDistinctMask = 0;
  4742         -  pMaskSet = pWInfo->pWC->pMaskSet;
  4743   4741     for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
  4744   4742       pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
  4745   4743       assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
  4746   4744       iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
  4747   4745       if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
  4748   4746         if( pLoop->wsFlags & WHERE_IPK ){
  4749   4747           pIndex = 0;
................................................................................
  4866   4864       /* Mark off any other ORDER BY terms that reference pLoop */
  4867   4865       if( isOrderDistinct ){
  4868   4866         orderDistinctMask |= pLoop->maskSelf;
  4869   4867         for(i=0; i<nOrderBy; i++){
  4870   4868           Expr *p;
  4871   4869           if( MASKBIT(i) & obSat ) continue;
  4872   4870           p = pOrderBy->a[i].pExpr;
  4873         -        if( (exprTableUsage(pMaskSet, p)&~orderDistinctMask)==0 ){
         4871  +        if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){
  4874   4872             obSat |= MASKBIT(i);
  4875   4873           }
  4876   4874         }
  4877   4875       }
  4878   4876     }
  4879   4877     if( obSat==obDone ) return 1;
  4880   4878     if( !isOrderDistinct ) return 0;
................................................................................
  5235   5233     int ii;                    /* Loop counter */
  5236   5234     sqlite3 *db;               /* Database connection */
  5237   5235     int rc;                    /* Return code */
  5238   5236   
  5239   5237   
  5240   5238     /* Variable initialization */
  5241   5239     memset(&sWLB, 0, sizeof(sWLB));
  5242         -  sWLB.pParse = pParse;
  5243         -  sWLB.db = pParse->db;
  5244         -  sWLB.pTabList = pTabList;
  5245   5240     sWLB.pOrderBy = pOrderBy;
  5246   5241   
  5247   5242     /* The number of tables in the FROM clause is limited by the number of
  5248   5243     ** bits in a Bitmask 
  5249   5244     */
  5250   5245     testcase( pTabList->nSrc==BMS );
  5251   5246     if( pTabList->nSrc>BMS ){
................................................................................
  5265   5260     ** struct, the contents of WhereInfo.a[], the WhereClause structure
  5266   5261     ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
  5267   5262     ** field (type Bitmask) it must be aligned on an 8-byte boundary on
  5268   5263     ** some architectures. Hence the ROUND8() below.
  5269   5264     */
  5270   5265     db = pParse->db;
  5271   5266     nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
  5272         -  pWInfo = sqlite3DbMallocZero(db, 
  5273         -      nByteWInfo + 
  5274         -      sizeof(WhereClause) +
  5275         -      sizeof(WhereMaskSet)
  5276         -  );
         5267  +  pWInfo = sqlite3DbMallocZero(db, nByteWInfo);
  5277   5268     if( db->mallocFailed ){
  5278   5269       sqlite3DbFree(db, pWInfo);
  5279   5270       pWInfo = 0;
  5280   5271       goto whereBeginError;
  5281   5272     }
  5282   5273     pWInfo->nLevel = nTabList;
  5283   5274     pWInfo->pParse = pParse;
  5284   5275     pWInfo->pTabList = pTabList;
  5285   5276     pWInfo->pOrderBy = pOrderBy;
  5286   5277     pWInfo->pDistinct = pDistinct;
  5287   5278     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
  5288         -  pWInfo->pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
  5289   5279     pWInfo->wctrlFlags = wctrlFlags;
  5290   5280     pWInfo->savedNQueryLoop = pParse->nQueryLoop;
  5291         -  pMaskSet = (WhereMaskSet*)&pWInfo->pWC[1];
         5281  +  pMaskSet = &pWInfo->sMaskSet;
  5292   5282     sWLB.pWInfo = pWInfo;
  5293         -  sWLB.pWC = pWInfo->pWC;
         5283  +  sWLB.pWC = &pWInfo->sWC;
  5294   5284   
  5295   5285     /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
  5296   5286     ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
  5297   5287     if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
  5298   5288   
  5299   5289     /* Split the WHERE clause into separate subexpressions where each
  5300   5290     ** subexpression is separated by an AND operator.
  5301   5291     */
  5302   5292     initMaskSet(pMaskSet);
  5303         -  whereClauseInit(pWInfo->pWC, pParse, pMaskSet, wctrlFlags);
         5293  +  whereClauseInit(&pWInfo->sWC, pWInfo);
  5304   5294     sqlite3ExprCodeConstants(pParse, pWhere);
  5305         -  whereSplit(pWInfo->pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
         5295  +  whereSplit(&pWInfo->sWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
  5306   5296       
  5307   5297     /* Special case: a WHERE clause that is constant.  Evaluate the
  5308   5298     ** expression and either jump over all of the code or fall thru.
  5309   5299     */
  5310   5300     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
  5311   5301       sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
  5312   5302       pWhere = 0;
................................................................................
  5343   5333   #endif
  5344   5334   
  5345   5335     /* Analyze all of the subexpressions.  Note that exprAnalyze() might
  5346   5336     ** add new virtual terms onto the end of the WHERE clause.  We do not
  5347   5337     ** want to analyze these virtual terms, so start analyzing at the end
  5348   5338     ** and work forward so that the added virtual terms are never processed.
  5349   5339     */
  5350         -  exprAnalyzeAll(pTabList, pWInfo->pWC);
         5340  +  exprAnalyzeAll(pTabList, &pWInfo->sWC);
  5351   5341     if( db->mallocFailed ){
  5352   5342       goto whereBeginError;
  5353   5343     }
  5354   5344   
  5355   5345     /* Check if the DISTINCT qualifier, if there is one, is redundant. 
  5356   5346     ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
  5357   5347     ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
  5358   5348     */
  5359         -  if( pDistinct && isDistinctRedundant(pParse,pTabList,pWInfo->pWC,pDistinct) ){
         5349  +  if( pDistinct && isDistinctRedundant(pParse,pTabList,&pWInfo->sWC,pDistinct) ){
  5360   5350       pDistinct = 0;
  5361   5351       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  5362   5352     }
  5363   5353   
  5364   5354     /* Construct the WhereLoop objects */
  5365   5355     WHERETRACE(("*** Optimizer Start ***\n"));
  5366   5356     rc = whereLoopAddAll(&sWLB);
................................................................................
  5463   5453           assert( n<=pTab->nCol );
  5464   5454         }
  5465   5455       }else{
  5466   5456         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  5467   5457       }
  5468   5458   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  5469   5459       if( (pLoop->wsFlags & WHERE_TEMP_INDEX)!=0 ){
  5470         -      constructAutomaticIndex(pParse, pWInfo->pWC, pTabItem, notReady, pLevel);
         5460  +      constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel);
  5471   5461       }else
  5472   5462   #endif
  5473   5463       if( pLoop->wsFlags & WHERE_INDEXED ){
  5474   5464         Index *pIx = pLoop->u.btree.pIndex;
  5475   5465         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
  5476   5466         /* FIXME:  As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */
  5477   5467         int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++;
................................................................................
  5478   5468         assert( pIx->pSchema==pTab->pSchema );
  5479   5469         assert( iIndexCur>=0 );
  5480   5470         sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
  5481   5471                           (char*)pKey, P4_KEYINFO_HANDOFF);
  5482   5472         VdbeComment((v, "%s", pIx->zName));
  5483   5473       }
  5484   5474       sqlite3CodeVerifySchema(pParse, iDb);
  5485         -    notReady &= ~getMask(pWInfo->pWC->pMaskSet, pTabItem->iCursor);
         5475  +    notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
  5486   5476     }
  5487   5477     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
  5488   5478     if( db->mallocFailed ) goto whereBeginError;
  5489   5479   
  5490   5480     /* Generate the code to do the search.  Each iteration of the for
  5491   5481     ** loop below generates code for a single nested loop of the VM
  5492   5482     ** program.
  5493   5483     */
  5494   5484     notReady = ~(Bitmask)0;
  5495   5485     for(ii=0; ii<nTabList; ii++){
  5496   5486       pLevel = &pWInfo->a[ii];
  5497   5487       explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
  5498         -    notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
         5488  +    notReady = codeOneLoopStart(pWInfo, ii, notReady);
  5499   5489       pWInfo->iContinue = pLevel->addrCont;
  5500   5490     }
  5501   5491   
  5502   5492     /* Done. */
  5503   5493     return pWInfo;
  5504   5494   
  5505   5495     /* Jump here if malloc fails */