/ Check-in [ec8bfa38]
Login

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

Overview
Comment:Simplify the where.c logic by flipping expression over so that the controlling variable is always on the left. (CVS 1838)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:ec8bfa3891dbf0f3172e31cf322974c03f9af59a
User & Date: drh 2004-07-20 18:23:15
Context
2004-07-21
02:53
Minor coding enhancements. (CVS 1839) check-in: 65c3af74 user: drh tags: trunk
2004-07-20
18:23
Simplify the where.c logic by flipping expression over so that the controlling variable is always on the left. (CVS 1838) check-in: ec8bfa38 user: drh tags: trunk
14:06
Lemon collapses common destructors and reduce actions into a single case. (CVS 1837) check-in: 3c5aa850 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/where.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This module contains C code that generates VDBE code used to process
    13     13   ** the WHERE clause of SQL statements.
    14     14   **
    15         -** $Id: where.c,v 1.110 2004/07/19 19:14:01 drh Exp $
           15  +** $Id: where.c,v 1.111 2004/07/20 18:23:15 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   /*
    20     20   ** The query generator uses an array of instances of this structure to
    21     21   ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
    22     22   ** clause subexpression is separated from the others by an AND operator.
................................................................................
   156    156       case TK_EQ:
   157    157       case TK_IN:
   158    158         return 1;
   159    159       default:
   160    160         return 0;
   161    161     }
   162    162   }
          163  +
          164  +/*
          165  +** Swap two integers.
          166  +*/
          167  +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
          168  +
          169  +/*
          170  +** Return the index in the SrcList that uses cursor iCur.  If iCur is
          171  +** used by the first entry in SrcList return 0.  If iCur is used by
          172  +** the second entry return 1.  And so forth.
          173  +**
          174  +** SrcList is the set of tables in the FROM clause in the order that
          175  +** they will be processed.  The value returned here gives us an index
          176  +** of which tables will be processed first.
          177  +*/
          178  +static int tableOrder(SrcList *pList, int iCur){
          179  +  int i;
          180  +  for(i=0; i<pList->nSrc; i++){
          181  +    if( pList->a[i].iCursor==iCur ) return i;
          182  +  }
          183  +  return -1;
          184  +}
   163    185   
   164    186   /*
   165    187   ** The input to this routine is an ExprInfo structure with only the
   166    188   ** "p" field filled in.  The job of this routine is to analyze the
   167    189   ** subexpression and populate all the other fields of the ExprInfo
   168    190   ** structure.
   169    191   */
   170         -static void exprAnalyze(ExprMaskSet *pMaskSet, ExprInfo *pInfo){
          192  +static void exprAnalyze(SrcList *pSrc, ExprMaskSet *pMaskSet, ExprInfo *pInfo){
   171    193     Expr *pExpr = pInfo->p;
   172    194     pInfo->prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
   173    195     pInfo->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
   174    196     pInfo->prereqAll = exprTableUsage(pMaskSet, pExpr);
   175    197     pInfo->indexable = 0;
   176    198     pInfo->idxLeft = -1;
   177    199     pInfo->idxRight = -1;
................................................................................
   181    203         pInfo->indexable = 1;
   182    204       }
   183    205       if( pExpr->pLeft->op==TK_COLUMN ){
   184    206         pInfo->idxLeft = pExpr->pLeft->iTable;
   185    207         pInfo->indexable = 1;
   186    208       }
   187    209     }
          210  +  if( pInfo->indexable ){
          211  +    assert( pInfo->idxLeft!=pInfo->idxRight );
          212  +
          213  +    /* We want the expression to be of the form "X = expr", not "expr = X".
          214  +    ** So flip it over if necessary.  If the expression is "X = Y", then
          215  +    ** we want Y to come from an earlier table than X.
          216  +    **
          217  +    ** The collating sequence rule is to always choose the left expression.
          218  +    ** So if we do a flip, we also have to move the collating sequence.
          219  +    */
          220  +    if( tableOrder(pSrc,pInfo->idxLeft)<tableOrder(pSrc,pInfo->idxRight) ){
          221  +      assert( pExpr->op!=TK_IN );
          222  +      SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl);
          223  +      SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
          224  +      switch( pExpr->op ){
          225  +        case TK_LT:   pExpr->op = TK_GT;  break;
          226  +        case TK_LE:   pExpr->op = TK_GE;  break;
          227  +        case TK_GT:   pExpr->op = TK_LT;  break;
          228  +        case TK_GE:   pExpr->op = TK_LE;  break;
          229  +        default:      break;
          230  +      }
          231  +      SWAP(unsigned, pInfo->prereqLeft, pInfo->prereqRight);
          232  +      SWAP(short int, pInfo->idxLeft, pInfo->idxRight);
          233  +    }
          234  +  }      
          235  +
   188    236   }
   189    237   
   190    238   /*
   191    239   ** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
   192    240   ** left-most table in the FROM clause of that same SELECT statement and
   193    241   ** the table has a cursor number of "base".
   194    242   **
................................................................................
   396    444     int i;                     /* Loop counter */
   397    445     WhereInfo *pWInfo;         /* Will become the return value of this function */
   398    446     Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
   399    447     int brk, cont = 0;         /* Addresses used during code generation */
   400    448     int nExpr;           /* Number of subexpressions in the WHERE clause */
   401    449     int loopMask;        /* One bit set for each outer loop */
   402    450     int haveKey = 0;     /* True if KEY is on the stack */
          451  +  ExprInfo *pTerm;     /* A single term in the WHERE clause; ptr to aExpr[] */
   403    452     ExprMaskSet maskSet; /* The expression mask set */
   404    453     int iDirectEq[32];   /* Term of the form ROWID==X for the N-th table */
   405    454     int iDirectLt[32];   /* Term of the form ROWID<X or ROWID<=X */
   406    455     int iDirectGt[32];   /* Term of the form ROWID>X or ROWID>=X */
   407         -  ExprInfo aExpr[101]; /* The WHERE clause is divided into these expressions */
          456  +  ExprInfo aExpr[101]; /* The WHERE clause is divided into these terms */
   408    457   
   409    458     /* pushKey is only allowed if there is a single table (as in an INSERT or
   410    459     ** UPDATE statement)
   411    460     */
   412    461     assert( pushKey==0 || pTabList->nSrc==1 );
   413    462   
   414    463     /* Split the WHERE clause into separate subexpressions where each
................................................................................
   426    475     }
   427    476     
   428    477     /* Allocate and initialize the WhereInfo structure that will become the
   429    478     ** return value.
   430    479     */
   431    480     pWInfo = sqliteMalloc( sizeof(WhereInfo) + pTabList->nSrc*sizeof(WhereLevel));
   432    481     if( sqlite3_malloc_failed ){
   433         -    sqliteFree(pWInfo);
          482  +    /* sqliteFree(pWInfo); // Leak memory when malloc fails */
   434    483       return 0;
   435    484     }
   436    485     pWInfo->pParse = pParse;
   437    486     pWInfo->pTabList = pTabList;
   438    487     pWInfo->peakNTab = pWInfo->savedNTab = pParse->nTab;
   439    488     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
   440    489   
................................................................................
   444    493     if( pWhere && (pTabList->nSrc==0 || sqlite3ExprIsConstant(pWhere)) ){
   445    494       sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, 1);
   446    495       pWhere = 0;
   447    496     }
   448    497   
   449    498     /* Analyze all of the subexpressions.
   450    499     */
   451         -  for(i=0; i<nExpr; i++){
   452         -    exprAnalyze(&maskSet, &aExpr[i]);
          500  +  for(pTerm=aExpr, i=0; i<nExpr; i++, pTerm++){
          501  +    TriggerStack *pStack;
          502  +    exprAnalyze(pTabList, &maskSet, pTerm);
   453    503   
   454    504       /* If we are executing a trigger body, remove all references to
   455    505       ** new.* and old.* tables from the prerequisite masks.
   456    506       */
   457         -    if( pParse->trigStack ){
          507  +    if( (pStack = pParse->trigStack)!=0 ){
   458    508         int x;
   459         -      if( (x = pParse->trigStack->newIdx) >= 0 ){
          509  +      if( (x=pStack->newIdx) >= 0 ){
   460    510           int mask = ~getMask(&maskSet, x);
   461         -        aExpr[i].prereqRight &= mask;
   462         -        aExpr[i].prereqLeft &= mask;
   463         -        aExpr[i].prereqAll &= mask;
          511  +        pTerm->prereqRight &= mask;
          512  +        pTerm->prereqLeft &= mask;
          513  +        pTerm->prereqAll &= mask;
   464    514         }
   465         -      if( (x = pParse->trigStack->oldIdx) >= 0 ){
          515  +      if( (x=pStack->oldIdx) >= 0 ){
   466    516           int mask = ~getMask(&maskSet, x);
   467         -        aExpr[i].prereqRight &= mask;
   468         -        aExpr[i].prereqLeft &= mask;
   469         -        aExpr[i].prereqAll &= mask;
          517  +        pTerm->prereqRight &= mask;
          518  +        pTerm->prereqLeft &= mask;
          519  +        pTerm->prereqAll &= mask;
   470    520         }
   471    521       }
   472    522     }
   473    523   
   474    524     /* Figure out what index to use (if any) for each nested loop.
   475    525     ** Make pWInfo->a[i].pIdx point to the index to use for the i-th nested
   476    526     ** loop where i==0 is the outer loop and i==pTabList->nSrc-1 is the inner
................................................................................
   505    555       **
   506    556       ** (Added:) Treat ROWID IN expr like ROWID=expr.
   507    557       */
   508    558       pWInfo->a[i].iCur = -1;
   509    559       iDirectEq[i] = -1;
   510    560       iDirectLt[i] = -1;
   511    561       iDirectGt[i] = -1;
   512         -    for(j=0; j<nExpr; j++){
   513         -      if( aExpr[j].idxLeft==iCur && aExpr[j].p->pLeft->iColumn<0
   514         -            && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
   515         -        switch( aExpr[j].p->op ){
          562  +    for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
          563  +      Expr *pX = pTerm->p;
          564  +      if( pTerm->idxLeft==iCur && pX->pLeft->iColumn<0
          565  +            && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){
          566  +        switch( pX->op ){
   516    567             case TK_IN:
   517    568             case TK_EQ: iDirectEq[i] = j; break;
   518    569             case TK_LE:
   519    570             case TK_LT: iDirectLt[i] = j; break;
   520    571             case TK_GE:
   521    572             case TK_GT: iDirectGt[i] = j;  break;
   522    573           }
   523    574         }
   524         -      if( aExpr[j].idxRight==iCur && aExpr[j].p->pRight->iColumn<0
   525         -            && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
   526         -        switch( aExpr[j].p->op ){
   527         -          case TK_EQ: iDirectEq[i] = j;  break;
   528         -          case TK_LE:
   529         -          case TK_LT: iDirectGt[i] = j;  break;
   530         -          case TK_GE:
   531         -          case TK_GT: iDirectLt[i] = j;  break;
   532         -        }
   533         -      }
   534    575       }
   535    576       if( iDirectEq[i]>=0 ){
   536    577         loopMask |= mask;
   537    578         pWInfo->a[i].pIdx = 0;
   538    579         continue;
   539    580       }
   540    581   
................................................................................
   567    608         int eqMask = 0;  /* Index columns covered by an x=... term */
   568    609         int ltMask = 0;  /* Index columns covered by an x<... term */
   569    610         int gtMask = 0;  /* Index columns covered by an x>... term */
   570    611         int inMask = 0;  /* Index columns covered by an x IN .. term */
   571    612         int nEq, m, score;
   572    613   
   573    614         if( pIdx->nColumn>32 ) continue;  /* Ignore indices too many columns */
   574         -      for(j=0; j<nExpr; j++){
   575         -        CollSeq *pColl = sqlite3ExprCollSeq(pParse, aExpr[j].p->pLeft);
   576         -        if( !pColl && aExpr[j].p->pRight ){
   577         -          pColl = sqlite3ExprCollSeq(pParse, aExpr[j].p->pRight);
          615  +      for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
          616  +        CollSeq *pColl = sqlite3ExprCollSeq(pParse, pTerm->p->pLeft);
          617  +        Expr *pX = pTerm->p;
          618  +        if( !pColl && pX->pRight ){
          619  +          pColl = sqlite3ExprCollSeq(pParse, pX->pRight);
   578    620           }
   579    621           if( !pColl ){
   580    622             pColl = pParse->db->pDfltColl;
   581    623           }
   582         -        if( aExpr[j].idxLeft==iCur 
   583         -             && (aExpr[j].prereqRight & loopMask)==aExpr[j].prereqRight ){
   584         -          int iColumn = aExpr[j].p->pLeft->iColumn;
          624  +        if( pTerm->idxLeft==iCur 
          625  +             && (pTerm->prereqRight & loopMask)==pTerm->prereqRight ){
          626  +          int iColumn = pX->pLeft->iColumn;
   585    627             int k;
   586    628             char idxaff = pIdx->pTable->aCol[iColumn].affinity; 
   587    629             for(k=0; k<pIdx->nColumn; k++){
   588    630               /* If the collating sequences or affinities don't match, 
   589    631               ** ignore this index.  */
   590    632               if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
   591         -            if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
          633  +            if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
   592    634               if( pIdx->aiColumn[k]==iColumn ){
   593         -              switch( aExpr[j].p->op ){
          635  +              switch( pX->op ){
   594    636                   case TK_IN: {
   595    637                     if( k==0 ) inMask |= 1;
   596    638                     break;
   597    639                   }
   598    640                   case TK_EQ: {
   599    641                     eqMask |= 1<<k;
   600    642                     break;
................................................................................
   605    647                     break;
   606    648                   }
   607    649                   case TK_GE:
   608    650                   case TK_GT: {
   609    651                     gtMask |= 1<<k;
   610    652                     break;
   611    653                   }
   612         -                default: {
   613         -                  /* CANT_HAPPEN */
   614         -                  assert( 0 );
   615         -                  break;
   616         -                }
   617         -              }
   618         -              break;
   619         -            }
   620         -          }
   621         -        }
   622         -        if( aExpr[j].idxRight==iCur 
   623         -             && (aExpr[j].prereqLeft & loopMask)==aExpr[j].prereqLeft ){
   624         -          int iColumn = aExpr[j].p->pRight->iColumn;
   625         -          int k;
   626         -          char idxaff = pIdx->pTable->aCol[iColumn].affinity; 
   627         -          for(k=0; k<pIdx->nColumn; k++){
   628         -            /* If the collating sequences or affinities don't match, 
   629         -            ** ignore this index.  */
   630         -            if( pColl!=pIdx->keyInfo.aColl[k] ) continue;
   631         -            if( !sqlite3IndexAffinityOk(aExpr[j].p, idxaff) ) continue;
   632         -            if( pIdx->aiColumn[k]==iColumn ){
   633         -              switch( aExpr[j].p->op ){
   634         -                case TK_EQ: {
   635         -                  eqMask |= 1<<k;
   636         -                  break;
   637         -                }
   638         -                case TK_LE:
   639         -                case TK_LT: {
   640         -                  gtMask |= 1<<k;
   641         -                  break;
   642         -                }
   643         -                case TK_GE:
   644         -                case TK_GT: {
   645         -                  ltMask |= 1<<k;
   646         -                  break;
   647         -                }
   648    654                   default: {
   649    655                     /* CANT_HAPPEN */
   650    656                     assert( 0 );
   651    657                     break;
   652    658                   }
   653    659                 }
   654    660                 break;
................................................................................
   766    772       pLevel->inOp = OP_Noop;
   767    773       if( i<ARRAYSIZE(iDirectEq) && iDirectEq[i]>=0 ){
   768    774         /* Case 1:  We can directly reference a single row using an
   769    775         **          equality comparison against the ROWID field.  Or
   770    776         **          we reference multiple rows using a "rowid IN (...)"
   771    777         **          construct.
   772    778         */
          779  +      Expr *pX;
   773    780         k = iDirectEq[i];
   774    781         assert( k<nExpr );
   775         -      assert( aExpr[k].p!=0 );
   776         -      assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
          782  +      pTerm = &aExpr[k];
          783  +      assert( pTerm->p!=0 );
          784  +      assert( pTerm->idxLeft==iCur || pTerm->idxRight==iCur );
   777    785         brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
   778         -      if( aExpr[k].idxLeft==iCur ){
   779         -        Expr *pX = aExpr[k].p;
   780         -        if( pX->op!=TK_IN ){
   781         -          sqlite3ExprCode(pParse, aExpr[k].p->pRight);
   782         -        }else{
   783         -          sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
   784         -          sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
   785         -          pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
   786         -          pLevel->inOp = OP_Next;
   787         -          pLevel->inP1 = pX->iTable;
   788         -        }
          786  +      pX = pTerm->p;
          787  +      assert( pTerm->idxLeft==iCur );
          788  +      if( pX->op!=TK_IN ){
          789  +        sqlite3ExprCode(pParse, pTerm->p->pRight);
   789    790         }else{
   790         -        sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
          791  +        sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
          792  +        sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
          793  +        pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
          794  +        pLevel->inOp = OP_Next;
          795  +        pLevel->inP1 = pX->iTable;
   791    796         }
   792         -      disableTerm(pLevel, &aExpr[k].p);
          797  +      disableTerm(pLevel, &pTerm->p);
   793    798         cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
   794    799         sqlite3VdbeAddOp(v, OP_MustBeInt, 1, brk);
   795    800         haveKey = 0;
   796    801         sqlite3VdbeAddOp(v, OP_NotExists, iCur, brk);
   797    802         pLevel->op = OP_Noop;
   798    803       }else if( pIdx!=0 && pLevel->score>0 && pLevel->score%4==0 ){
   799    804         /* Case 2:  There is an index and all terms of the WHERE clause that
................................................................................
   803    808         int nColumn = (pLevel->score+4)/8;
   804    809         brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
   805    810   
   806    811         /* For each column of the index, find the term of the WHERE clause that
   807    812         ** constraints that column.  If the WHERE clause term is X=expr, then
   808    813         ** evaluation expr and leave the result on the stack */
   809    814         for(j=0; j<nColumn; j++){
   810         -        for(k=0; k<nExpr; k++){
   811         -          Expr *pX = aExpr[k].p;
          815  +        for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
          816  +          Expr *pX = pTerm->p;
   812    817             if( pX==0 ) continue;
   813         -          if( aExpr[k].idxLeft==iCur
   814         -             && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight 
          818  +          if( pTerm->idxLeft==iCur
          819  +             && (pTerm->prereqRight & loopMask)==pTerm->prereqRight 
   815    820                && pX->pLeft->iColumn==pIdx->aiColumn[j]
   816    821             ){
   817    822               char idxaff = pIdx->pTable->aCol[pX->pLeft->iColumn].affinity;
   818         -            if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
          823  +            if( sqlite3IndexAffinityOk(pTerm->p, idxaff) ){
   819    824                 if( pX->op==TK_EQ ){
   820    825                   sqlite3ExprCode(pParse, pX->pRight);
   821         -                disableTerm(pLevel, &aExpr[k].p);
          826  +                disableTerm(pLevel, &pTerm->p);
   822    827                   break;
   823    828                 }
          829  +              assert( pX->op==TK_IN && nColumn==1 );
   824    830                 if( pX->op==TK_IN && nColumn==1 ){
   825    831                   sqlite3VdbeAddOp(v, OP_Rewind, pX->iTable, brk);
   826    832                   sqlite3VdbeAddOp(v, OP_KeyAsData, pX->iTable, 1);
   827    833                   pLevel->inP2 = sqlite3VdbeAddOp(v, OP_IdxColumn, pX->iTable, 0);
   828    834                   pLevel->inOp = OP_Next;
   829    835                   pLevel->inP1 = pX->iTable;
   830         -                disableTerm(pLevel, &aExpr[k].p);
          836  +                disableTerm(pLevel, &pTerm->p);
   831    837                   break;
   832    838                 }
   833    839               }
   834    840             }
   835         -          if( aExpr[k].idxRight==iCur
   836         -             && aExpr[k].p->op==TK_EQ
   837         -             && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
   838         -             && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
   839         -          ){
   840         -            char idxaff = pIdx->pTable->aCol[pX->pRight->iColumn].affinity;
   841         -            if( sqlite3IndexAffinityOk(aExpr[k].p, idxaff) ){
   842         -              sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
   843         -              disableTerm(pLevel, &aExpr[k].p);
   844         -              break;
   845         -            }
   846         -          }
   847    841           }
   848    842         }
   849    843         pLevel->iMem = pParse->nMem++;
   850    844         cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
   851    845   
   852    846         /* At this point, the top nColumn elements of the stack are the
   853    847         ** values of columns in the index we are using.  Check to see if
................................................................................
   898    892         int start;
   899    893   
   900    894         brk = pLevel->brk = sqlite3VdbeMakeLabel(v);
   901    895         cont = pLevel->cont = sqlite3VdbeMakeLabel(v);
   902    896         if( iDirectGt[i]>=0 ){
   903    897           k = iDirectGt[i];
   904    898           assert( k<nExpr );
   905         -        assert( aExpr[k].p!=0 );
   906         -        assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
   907         -        if( aExpr[k].idxLeft==iCur ){
   908         -          sqlite3ExprCode(pParse, aExpr[k].p->pRight);
   909         -        }else{
   910         -          sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
   911         -        }
          899  +        pTerm = &aExpr[k];
          900  +        assert( pTerm->p!=0 );
          901  +        assert( pTerm->idxLeft==iCur );
          902  +        sqlite3ExprCode(pParse, pTerm->p->pRight);
   912    903           sqlite3VdbeAddOp(v, OP_ForceInt,
   913         -          aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT, brk);
          904  +          pTerm->p->op==TK_LT || pTerm->p->op==TK_GT, brk);
   914    905           sqlite3VdbeAddOp(v, OP_MoveGe, iCur, brk);
   915         -        disableTerm(pLevel, &aExpr[k].p);
          906  +        disableTerm(pLevel, &pTerm->p);
   916    907         }else{
   917    908           sqlite3VdbeAddOp(v, OP_Rewind, iCur, brk);
   918    909         }
   919    910         if( iDirectLt[i]>=0 ){
   920    911           k = iDirectLt[i];
   921    912           assert( k<nExpr );
   922         -        assert( aExpr[k].p!=0 );
   923         -        assert( aExpr[k].idxLeft==iCur || aExpr[k].idxRight==iCur );
   924         -        if( aExpr[k].idxLeft==iCur ){
   925         -          sqlite3ExprCode(pParse, aExpr[k].p->pRight);
   926         -        }else{
   927         -          sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
   928         -        }
          913  +        pTerm = &aExpr[k];
          914  +        assert( pTerm->p!=0 );
          915  +        assert( pTerm->idxLeft==iCur );
          916  +        sqlite3ExprCode(pParse, pTerm->p->pRight);
   929    917           /* sqlite3VdbeAddOp(v, OP_MustBeInt, 0, sqlite3VdbeCurrentAddr(v)+1); */
   930    918           pLevel->iMem = pParse->nMem++;
   931    919           sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iMem, 1);
   932         -        if( aExpr[k].p->op==TK_LT || aExpr[k].p->op==TK_GT ){
          920  +        if( pTerm->p->op==TK_LT || pTerm->p->op==TK_GT ){
   933    921             testOp = OP_Ge;
   934    922           }else{
   935    923             testOp = OP_Gt;
   936    924           }
   937         -        disableTerm(pLevel, &aExpr[k].p);
          925  +        disableTerm(pLevel, &pTerm->p);
   938    926         }
   939    927         start = sqlite3VdbeCurrentAddr(v);
   940    928         pLevel->op = OP_Next;
   941    929         pLevel->p1 = iCur;
   942    930         pLevel->p2 = start;
   943    931         if( testOp!=OP_Noop ){
   944    932           sqlite3VdbeAddOp(v, OP_Recno, iCur, 0);
................................................................................
   977    965         int start;
   978    966         int leFlag=0, geFlag=0;
   979    967         int testOp;
   980    968   
   981    969         /* Evaluate the equality constraints
   982    970         */
   983    971         for(j=0; j<nEqColumn; j++){
   984         -        for(k=0; k<nExpr; k++){
   985         -          if( aExpr[k].p==0 ) continue;
   986         -          if( aExpr[k].idxLeft==iCur
   987         -             && aExpr[k].p->op==TK_EQ
   988         -             && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight 
   989         -             && aExpr[k].p->pLeft->iColumn==pIdx->aiColumn[j]
          972  +        for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
          973  +          if( pTerm->p==0 ) continue;
          974  +          if( pTerm->idxLeft==iCur
          975  +             && pTerm->p->op==TK_EQ
          976  +             && (pTerm->prereqRight & loopMask)==pTerm->prereqRight 
          977  +             && pTerm->p->pLeft->iColumn==pIdx->aiColumn[j]
   990    978             ){
   991         -            sqlite3ExprCode(pParse, aExpr[k].p->pRight);
   992         -            disableTerm(pLevel, &aExpr[k].p);
   993         -            break;
   994         -          }
   995         -          if( aExpr[k].idxRight==iCur
   996         -             && aExpr[k].p->op==TK_EQ
   997         -             && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
   998         -             && aExpr[k].p->pRight->iColumn==pIdx->aiColumn[j]
   999         -          ){
  1000         -            sqlite3ExprCode(pParse, aExpr[k].p->pLeft);
  1001         -            disableTerm(pLevel, &aExpr[k].p);
          979  +            sqlite3ExprCode(pParse, pTerm->p->pRight);
          980  +            disableTerm(pLevel, &pTerm->p);
  1002    981               break;
  1003    982             }
  1004    983           }
  1005    984         }
  1006    985   
  1007    986         /* Duplicate the equality term values because they will all be
  1008    987         ** used twice: once to make the termination key and once to make the
................................................................................
  1021   1000         ** will end the search.  There is no termination key if there
  1022   1001         ** are no equality terms and no "X<..." term.
  1023   1002         **
  1024   1003         ** 2002-Dec-04: On a reverse-order scan, the so-called "termination"
  1025   1004         ** key computed here really ends up being the start key.
  1026   1005         */
  1027   1006         if( (score & 1)!=0 ){
  1028         -        for(k=0; k<nExpr; k++){
  1029         -          Expr *pExpr = aExpr[k].p;
         1007  +        for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
         1008  +          Expr *pExpr = pTerm->p;
  1030   1009             if( pExpr==0 ) continue;
  1031         -          if( aExpr[k].idxLeft==iCur
         1010  +          if( pTerm->idxLeft==iCur
  1032   1011                && (pExpr->op==TK_LT || pExpr->op==TK_LE)
  1033         -             && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight 
         1012  +             && (pTerm->prereqRight & loopMask)==pTerm->prereqRight 
  1034   1013                && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
  1035   1014             ){
  1036   1015               sqlite3ExprCode(pParse, pExpr->pRight);
  1037   1016               leFlag = pExpr->op==TK_LE;
  1038         -            disableTerm(pLevel, &aExpr[k].p);
  1039         -            break;
  1040         -          }
  1041         -          if( aExpr[k].idxRight==iCur
  1042         -             && (pExpr->op==TK_GT || pExpr->op==TK_GE)
  1043         -             && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
  1044         -             && pExpr->pRight->iColumn==pIdx->aiColumn[j]
  1045         -          ){
  1046         -            sqlite3ExprCode(pParse, pExpr->pLeft);
  1047         -            leFlag = pExpr->op==TK_GE;
  1048         -            disableTerm(pLevel, &aExpr[k].p);
         1017  +            disableTerm(pLevel, &pTerm->p);
  1049   1018               break;
  1050   1019             }
  1051   1020           }
  1052   1021           testOp = OP_IdxGE;
  1053   1022         }else{
  1054   1023           testOp = nEqColumn>0 ? OP_IdxGE : OP_Noop;
  1055   1024           leFlag = 1;
................................................................................
  1078   1047         ** that case, generate a "Rewind" instruction in place of the
  1079   1048         ** start key search.
  1080   1049         **
  1081   1050         ** 2002-Dec-04: In the case of a reverse-order search, the so-called
  1082   1051         ** "start" key really ends up being used as the termination key.
  1083   1052         */
  1084   1053         if( (score & 2)!=0 ){
  1085         -        for(k=0; k<nExpr; k++){
  1086         -          Expr *pExpr = aExpr[k].p;
         1054  +        for(pTerm=aExpr, k=0; k<nExpr; k++, pTerm++){
         1055  +          Expr *pExpr = pTerm->p;
  1087   1056             if( pExpr==0 ) continue;
  1088         -          if( aExpr[k].idxLeft==iCur
         1057  +          if( pTerm->idxLeft==iCur
  1089   1058                && (pExpr->op==TK_GT || pExpr->op==TK_GE)
  1090         -             && (aExpr[k].prereqRight & loopMask)==aExpr[k].prereqRight 
         1059  +             && (pTerm->prereqRight & loopMask)==pTerm->prereqRight 
  1091   1060                && pExpr->pLeft->iColumn==pIdx->aiColumn[j]
  1092   1061             ){
  1093   1062               sqlite3ExprCode(pParse, pExpr->pRight);
  1094   1063               geFlag = pExpr->op==TK_GE;
  1095         -            disableTerm(pLevel, &aExpr[k].p);
  1096         -            break;
  1097         -          }
  1098         -          if( aExpr[k].idxRight==iCur
  1099         -             && (pExpr->op==TK_LT || pExpr->op==TK_LE)
  1100         -             && (aExpr[k].prereqLeft & loopMask)==aExpr[k].prereqLeft
  1101         -             && pExpr->pRight->iColumn==pIdx->aiColumn[j]
  1102         -          ){
  1103         -            sqlite3ExprCode(pParse, pExpr->pLeft);
  1104         -            geFlag = pExpr->op==TK_LE;
  1105         -            disableTerm(pLevel, &aExpr[k].p);
         1064  +            disableTerm(pLevel, &pTerm->p);
  1106   1065               break;
  1107   1066             }
  1108   1067           }
  1109   1068         }else{
  1110   1069           geFlag = 1;
  1111   1070         }
  1112   1071         if( nEqColumn>0 || (score&2)!=0 ){
................................................................................
  1159   1118         pLevel->p2 = start;
  1160   1119       }
  1161   1120       loopMask |= getMask(&maskSet, iCur);
  1162   1121   
  1163   1122       /* Insert code to test every subexpression that can be completely
  1164   1123       ** computed using the current set of tables.
  1165   1124       */
  1166         -    for(j=0; j<nExpr; j++){
  1167         -      if( aExpr[j].p==0 ) continue;
  1168         -      if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
  1169         -      if( pLevel->iLeftJoin && !ExprHasProperty(aExpr[j].p,EP_FromJoin) ){
         1125  +    for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
         1126  +      if( pTerm->p==0 ) continue;
         1127  +      if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue;
         1128  +      if( pLevel->iLeftJoin && !ExprHasProperty(pTerm->p,EP_FromJoin) ){
  1170   1129           continue;
  1171   1130         }
  1172   1131         if( haveKey ){
  1173   1132           haveKey = 0;
  1174   1133           sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
  1175   1134         }
  1176         -      sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
  1177         -      aExpr[j].p = 0;
         1135  +      sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1);
         1136  +      pTerm->p = 0;
  1178   1137       }
  1179   1138       brk = cont;
  1180   1139   
  1181   1140       /* For a LEFT OUTER JOIN, generate code that will record the fact that
  1182   1141       ** at least one row of the right table has matched the left table.  
  1183   1142       */
  1184   1143       if( pLevel->iLeftJoin ){
  1185   1144         pLevel->top = sqlite3VdbeCurrentAddr(v);
  1186   1145         sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
  1187   1146         sqlite3VdbeAddOp(v, OP_MemStore, pLevel->iLeftJoin, 1);
  1188         -      for(j=0; j<nExpr; j++){
  1189         -        if( aExpr[j].p==0 ) continue;
  1190         -        if( (aExpr[j].prereqAll & loopMask)!=aExpr[j].prereqAll ) continue;
         1147  +      for(pTerm=aExpr, j=0; j<nExpr; j++, pTerm++){
         1148  +        if( pTerm->p==0 ) continue;
         1149  +        if( (pTerm->prereqAll & loopMask)!=pTerm->prereqAll ) continue;
  1191   1150           if( haveKey ){
  1192   1151             /* Cannot happen.  "haveKey" can only be true if pushKey is true
  1193   1152             ** an pushKey can only be true for DELETE and UPDATE and there are
  1194   1153             ** no outer joins with DELETE and UPDATE.
  1195   1154             */
  1196   1155             haveKey = 0;
  1197   1156             sqlite3VdbeAddOp(v, OP_MoveGe, iCur, 0);
  1198   1157           }
  1199         -        sqlite3ExprIfFalse(pParse, aExpr[j].p, cont, 1);
  1200         -        aExpr[j].p = 0;
         1158  +        sqlite3ExprIfFalse(pParse, pTerm->p, cont, 1);
         1159  +        pTerm->p = 0;
  1201   1160         }
  1202   1161       }
  1203   1162     }
  1204   1163     pWInfo->iContinue = cont;
  1205   1164     if( pushKey && !haveKey ){
  1206   1165       sqlite3VdbeAddOp(v, OP_Recno, pTabList->a[0].iCursor, 0);
  1207   1166     }