/ Changes On Branch expr-simplify
Login

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

Changes In Branch expr-simplify Excluding Merge-Ins

This is equivalent to a diff from e461cb2819 to 24b0f66ac6

2018-09-24
21:07
Avoid incrementing the SQLITE_LOOKASIDE_MISS_SIZE stat before sqlite3_open() returns. Fix test script problem in lookaside.test. (check-in: 3bd94e4317 user: drh tags: branch-3.25)
2018-09-19
20:14
Reduce the size of Expr to 64-bytes. This works somewhat, but there are test failures. More importantly, the size reduction from 80- to 64-bytes has not lowered the schema memory usage, but it has made the code a little bigger and a little slower. So the initial evidence is that this Expr refactoring experiment is not working... (Leaf check-in: 24b0f66ac6 user: drh tags: expr-simplify)
17:24
Fix an issue in virtual table handling associated with the new Expr.x.pRight field. (check-in: 8487f84af0 user: drh tags: expr-simplify)
2018-09-18
19:40
Fix a problem building on Android with SQLITE_ENABLE_BATCH_ATOMIC_WRITE set. (check-in: e41e50fe74 user: dan tags: trunk)
18:08
Merge all recent trunk enhancements. (check-in: 655f065404 user: drh tags: expr-simplify)
17:50
Avoid incrementing the SQLITE_LOOKASIDE_MISS_SIZE stat before sqlite3_open() returns. Fix test script problem in lookaside.test. (check-in: e461cb2819 user: dan tags: trunk)
17:00
Enhance tester.tcl so that when "--malloctrace=1" is specified, the test generates self-contained Tcl scripts that present GUIs instead of *.sql files that require a separate program to interpret. (check-in: de2e3cbd08 user: dan tags: trunk)

Changes to src/alter.c.

   799    799     if( pExpr->op==TK_TRIGGER 
   800    800      && pExpr->iColumn==p->iCol 
   801    801      && pWalker->pParse->pTriggerTab==p->pTab
   802    802     ){
   803    803       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
   804    804     }else if( pExpr->op==TK_COLUMN 
   805    805      && pExpr->iColumn==p->iCol 
   806         -   && p->pTab==pExpr->pTab
          806  +   && ALWAYS(pExpr->eX==EX_Tab)
          807  +   && p->pTab==pExpr->x.pTab
   807    808     ){
   808    809       renameTokenFind(pWalker->pParse, p, (void*)pExpr);
   809    810     }
   810    811     return WRC_Continue;
   811    812   }
   812    813   
   813    814   /*
................................................................................
  1337   1338   }
  1338   1339   
  1339   1340   /*
  1340   1341   ** Walker expression callback used by "RENAME TABLE". 
  1341   1342   */
  1342   1343   static int renameTableExprCb(Walker *pWalker, Expr *pExpr){
  1343   1344     RenameCtx *p = pWalker->u.pRename;
  1344         -  if( pExpr->op==TK_COLUMN && p->pTab==pExpr->pTab ){
  1345         -    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->pTab);
         1345  +  if( pExpr->op==TK_COLUMN 
         1346  +   && ALWAYS(pExpr->eX==EX_Tab)
         1347  +   && p->pTab==pExpr->x.pTab
         1348  +  ){
         1349  +    renameTokenFind(pWalker->pParse, p, (void*)&pExpr->x.pTab);
  1346   1350     }
  1347   1351     return WRC_Continue;
  1348   1352   }
  1349   1353   
  1350   1354   /*
  1351   1355   ** Walker select callback used by "RENAME TABLE". 
  1352   1356   */

Changes to src/attach.c.

   560    560           pExpr->op = TK_NULL;
   561    561         }else{
   562    562           sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
   563    563           return 1;
   564    564         }
   565    565       }
   566    566       if( ExprHasProperty(pExpr, EP_TokenOnly|EP_Leaf) ) break;
   567         -    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   568         -      if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
   569         -    }else{
   570         -      if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
   571         -    }
   572         -    if( sqlite3FixExpr(pFix, pExpr->pRight) ){
   573         -      return 1;
          567  +    switch( pExpr->eX ){
          568  +      case EX_Select: {
          569  +        if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
          570  +        break;
          571  +      }
          572  +      case EX_List: {
          573  +        if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
          574  +        break;
          575  +      }
          576  +      case EX_Right: {
          577  +        if( sqlite3FixExpr(pFix, pExpr->x.pRight) ) return 1;
          578  +        break;
          579  +      }
   574    580       }
   575    581       pExpr = pExpr->pLeft;
   576    582     }
   577    583     return 0;
   578    584   }
   579    585   int sqlite3FixExprList(
   580    586     DbFixer *pFix,     /* Context of the fixation */

Changes to src/delete.c.

   181    181       }else{
   182    182         int i;
   183    183         for(i=0; i<pPk->nKeyCol; i++){
   184    184           Expr *p = sqlite3Expr(db, TK_ID, pTab->aCol[pPk->aiColumn[i]].zName);
   185    185           pEList = sqlite3ExprListAppend(pParse, pEList, p);
   186    186         }
   187    187         pLhs = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
   188         -      if( pLhs ){
   189         -        pLhs->x.pList = sqlite3ExprListDup(db, pEList, 0);
   190         -      }
          188  +      sqlite3PExprAddExprList(pParse, pLhs, pEList);
   191    189       }
   192    190     }
   193    191   
   194    192     /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
   195    193     ** and the SELECT subtree. */
   196    194     pSrc->a[0].pTab = 0;
   197    195     pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);

Changes to src/expr.c.

    44     44   */
    45     45   char sqlite3ExprAffinity(Expr *pExpr){
    46     46     int op;
    47     47     pExpr = sqlite3ExprSkipCollate(pExpr);
    48     48     if( pExpr->flags & EP_Generic ) return 0;
    49     49     op = pExpr->op;
    50     50     if( op==TK_SELECT ){
    51         -    assert( pExpr->flags&EP_xIsSelect );
           51  +    assert( pExpr->eX==EX_Select );
    52     52       return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
    53     53     }
    54     54     if( op==TK_REGISTER ) op = pExpr->op2;
    55     55   #ifndef SQLITE_OMIT_CAST
    56     56     if( op==TK_CAST ){
    57     57       assert( !ExprHasProperty(pExpr, EP_IntValue) );
    58     58       return sqlite3AffinityType(pExpr->u.zToken, 0);
    59     59     }
    60     60   #endif
    61         -  if( (op==TK_AGG_COLUMN || op==TK_COLUMN) && pExpr->pTab ){
    62         -    return sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
           61  +  if( (op==TK_AGG_COLUMN || op==TK_COLUMN)
           62  +   && ALWAYS(pExpr->eX==EX_Tab) && pExpr->x.pTab ){
           63  +    return sqlite3TableColumnAffinity(pExpr->x.pTab, pExpr->iColumn);
    63     64     }
    64     65     if( op==TK_SELECT_COLUMN ){
    65         -    assert( pExpr->pLeft->flags&EP_xIsSelect );
           66  +    assert( pExpr->pLeft->eX==EX_Select );
    66     67       return sqlite3ExprAffinity(
    67     68           pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
    68     69       );
    69     70     }
    70     71     return pExpr->affinity;
    71     72   }
    72     73   
................................................................................
   104    105   /*
   105    106   ** Skip over any TK_COLLATE operators and any unlikely()
   106    107   ** or likelihood() function at the root of an expression.
   107    108   */
   108    109   Expr *sqlite3ExprSkipCollate(Expr *pExpr){
   109    110     while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
   110    111       if( ExprHasProperty(pExpr, EP_Unlikely) ){
   111         -      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
          112  +      assert( pExpr->eX==EX_List );
   112    113         assert( pExpr->x.pList->nExpr>0 );
   113    114         assert( pExpr->op==TK_FUNCTION );
   114    115         pExpr = pExpr->x.pList->a[0].pExpr;
   115    116       }else{
   116    117         assert( pExpr->op==TK_COLLATE );
   117    118         pExpr = pExpr->pLeft;
   118    119       }
................................................................................
   139    140     CollSeq *pColl = 0;
   140    141     Expr *p = pExpr;
   141    142     while( p ){
   142    143       int op = p->op;
   143    144       if( p->flags & EP_Generic ) break;
   144    145       if( (op==TK_AGG_COLUMN || op==TK_COLUMN
   145    146             || op==TK_REGISTER || op==TK_TRIGGER)
   146         -     && p->pTab!=0
          147  +     && p->eX==EX_Tab
          148  +     && p->x.pTab!=0
   147    149       ){
   148    150         /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
   149    151         ** a TK_COLUMN but was previously evaluated and cached in a register */
   150    152         int j = p->iColumn;
   151    153         if( j>=0 ){
   152         -        const char *zColl = p->pTab->aCol[j].zColl;
          154  +        const char *zColl = p->x.pTab->aCol[j].zColl;
   153    155           pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
   154    156         }
   155    157         break;
   156    158       }
   157    159       if( op==TK_CAST || op==TK_UPLUS ){
   158    160         p = p->pLeft;
   159    161         continue;
................................................................................
   162    164         pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
   163    165         break;
   164    166       }
   165    167       if( p->flags & EP_Collate ){
   166    168         if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){
   167    169           p = p->pLeft;
   168    170         }else{
   169         -        Expr *pNext  = p->pRight;
   170         -        /* The Expr.x union is never used at the same time as Expr.pRight */
   171         -        assert( p->x.pList==0 || p->pRight==0 );
          171  +        Expr *pNext = 0;
   172    172           /* p->flags holds EP_Collate and p->pLeft->flags does not.  And
   173    173           ** p->x.pSelect cannot.  So if p->x.pLeft exists, it must hold at
   174    174           ** least one EP_Collate. Thus the following two ALWAYS. */
   175         -        if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){
          175  +        if( p->eX==EX_List ){
   176    176             int i;
   177    177             for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){
   178    178               if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){
   179    179                 pNext = p->x.pList->a[i].pExpr;
   180    180                 break;
   181    181               }
   182    182             }
          183  +        }else if( p->eX==EX_Right ){
          184  +          pNext = p->x.pRight;
   183    185           }
   184    186           p = pNext;
   185    187         }
   186    188       }else{
   187    189         break;
   188    190       }
   189    191     }
................................................................................
   254    256   static char comparisonAffinity(Expr *pExpr){
   255    257     char aff;
   256    258     assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
   257    259             pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
   258    260             pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
   259    261     assert( pExpr->pLeft );
   260    262     aff = sqlite3ExprAffinity(pExpr->pLeft);
   261         -  if( pExpr->pRight ){
   262         -    aff = sqlite3CompareAffinity(pExpr->pRight, aff);
   263         -  }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
          263  +  if( pExpr->eX==EX_Right ){
          264  +    aff = sqlite3CompareAffinity(pExpr->x.pRight, aff);
          265  +  }else if( pExpr->eX==EX_Select ){
   264    266       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
   265    267     }else if( aff==0 ){
   266    268       aff = SQLITE_AFF_BLOB;
   267    269     }
   268    270     return aff;
   269    271   }
   270    272   
................................................................................
   303    305   ** If the left hand expression has a collating sequence type, then it is
   304    306   ** used. Otherwise the collation sequence for the right hand expression
   305    307   ** is used, or the default (BINARY) if neither expression has a collating
   306    308   ** type.
   307    309   **
   308    310   ** Argument pRight (but not pLeft) may be a null pointer. In this case,
   309    311   ** it is not considered.
          312  +**
          313  +** The second form (sqlite3BinaryExprCollSeq()) uses the Expr.pLeft
          314  +** and Expr.x.pRight pointer from the second argument instead of separate
          315  +** pointers.
   310    316   */
   311         -CollSeq *sqlite3BinaryCompareCollSeq(
          317  +CollSeq *sqlite3ComparisonCollSeq(
   312    318     Parse *pParse, 
   313    319     Expr *pLeft, 
   314    320     Expr *pRight
   315    321   ){
   316    322     CollSeq *pColl;
   317    323     assert( pLeft );
   318    324     if( pLeft->flags & EP_Collate ){
................................................................................
   323    329       pColl = sqlite3ExprCollSeq(pParse, pLeft);
   324    330       if( !pColl ){
   325    331         pColl = sqlite3ExprCollSeq(pParse, pRight);
   326    332       }
   327    333     }
   328    334     return pColl;
   329    335   }
          336  +CollSeq *sqlite3ComparisonExprCollSeq(Parse *pParse, Expr *pExpr){
          337  +  if( pExpr->eX==EX_Right ){
          338  +    return sqlite3ComparisonCollSeq(pParse, pExpr->pLeft, pExpr->x.pRight);
          339  +  }else{
          340  +    return sqlite3ComparisonCollSeq(pParse, pExpr->pLeft, 0);
          341  +  }
          342  +}
   330    343   
   331    344   /*
   332    345   ** Generate code for a comparison operator.
   333    346   */
   334    347   static int codeCompare(
   335    348     Parse *pParse,    /* The parsing (and code generating) context */
   336    349     Expr *pLeft,      /* The left operand */
................................................................................
   340    353     int dest,         /* Jump here if true.  */
   341    354     int jumpIfNull    /* If true, jump if either operand is NULL */
   342    355   ){
   343    356     int p5;
   344    357     int addr;
   345    358     CollSeq *p4;
   346    359   
   347         -  p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
          360  +  p4 = sqlite3ComparisonCollSeq(pParse, pLeft, pRight);
   348    361     p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
   349    362     addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
   350    363                              (void*)p4, P4_COLLSEQ);
   351    364     sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
   352    365     return addr;
   353    366   }
   354    367   
................................................................................
   435    448   Expr *sqlite3ExprForVectorField(
   436    449     Parse *pParse,       /* Parsing context */
   437    450     Expr *pVector,       /* The vector.  List of expressions or a sub-SELECT */
   438    451     int iField           /* Which column of the vector to return */
   439    452   ){
   440    453     Expr *pRet;
   441    454     if( pVector->op==TK_SELECT ){
   442         -    assert( pVector->flags & EP_xIsSelect );
          455  +    assert( pVector->eX==EX_Select );
   443    456       /* The TK_SELECT_COLUMN Expr node:
   444    457       **
   445    458       ** pLeft:           pVector containing TK_SELECT.  Not deleted.
   446    459       ** pRight:          not used.  But recursively deleted.
   447    460       ** iColumn:         Index of a column in pVector
   448    461       ** iTable:          0 or the number of columns on the LHS of an assignment
   449    462       ** pLeft->iTable:   First in an array of register holding result, or 0
................................................................................
   543    556     Expr *pExpr,          /* The comparison operation */
   544    557     int dest,             /* Write results into this register */
   545    558     u8 op,                /* Comparison operator */
   546    559     u8 p5                 /* SQLITE_NULLEQ or zero */
   547    560   ){
   548    561     Vdbe *v = pParse->pVdbe;
   549    562     Expr *pLeft = pExpr->pLeft;
   550         -  Expr *pRight = pExpr->pRight;
          563  +  Expr *pRight = pExpr->x.pRight;
   551    564     int nLeft = sqlite3ExprVectorSize(pLeft);
   552    565     int i;
   553    566     int regLeft = 0;
   554    567     int regRight = 0;
   555    568     u8 opx = op;
   556    569     int addrDone = sqlite3VdbeMakeLabel(v);
   557    570   
................................................................................
   677    690   **
   678    691   ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags,
   679    692   ** if appropriate.
   680    693   */
   681    694   static void exprSetHeight(Expr *p){
   682    695     int nHeight = 0;
   683    696     heightOfExpr(p->pLeft, &nHeight);
   684         -  heightOfExpr(p->pRight, &nHeight);
   685         -  if( ExprHasProperty(p, EP_xIsSelect) ){
   686         -    heightOfSelect(p->x.pSelect, &nHeight);
   687         -  }else if( p->x.pList ){
   688         -    heightOfExprList(p->x.pList, &nHeight);
   689         -    p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
          697  +  switch( p->eX ){
          698  +    case EX_Select: {
          699  +      heightOfSelect(p->x.pSelect, &nHeight);
          700  +      break;
          701  +    }
          702  +    case EX_List: {
          703  +      heightOfExprList(p->x.pList, &nHeight);
          704  +      p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
          705  +      break;
          706  +    }
          707  +    case EX_Right: {
          708  +      heightOfExpr(p->x.pRight, &nHeight);
          709  +      break;
          710  +    }
   690    711     }
   691    712     p->nHeight = nHeight + 1;
   692    713   }
   693    714   
   694    715   /*
   695    716   ** Set the Expr.nHeight variable using the exprSetHeight() function. If
   696    717   ** the height is greater than the maximum allowed expression depth,
   697    718   ** leave an error in pParse.
   698    719   **
   699    720   ** Also propagate all EP_Propagate flags from the Expr.x.pList into
   700    721   ** Expr.flags. 
   701    722   */
   702    723   void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
   703         -  if( pParse->nErr ) return;
          724  +  if( p==0 || pParse->nErr ) return;
   704    725     exprSetHeight(p);
   705    726     sqlite3ExprCheckHeight(pParse, p->nHeight);
   706    727   }
   707    728   
   708    729   /*
   709    730   ** Return the maximum height of any expression tree referenced
   710    731   ** by the select statement passed as an argument.
................................................................................
   716    737   }
   717    738   #else /* ABOVE:  Height enforcement enabled.  BELOW: Height enforcement off */
   718    739   /*
   719    740   ** Propagate all EP_Propagate flags from the Expr.x.pList into
   720    741   ** Expr.flags. 
   721    742   */
   722    743   void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){
   723         -  if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){
          744  +  if( p && p->eX==EX_List ){
   724    745       p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList);
   725    746     }
   726    747   }
   727    748   #define exprSetHeight(y)
   728    749   #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
   729    750   
   730    751   /*
................................................................................
   821    842   ){
   822    843     if( pRoot==0 ){
   823    844       assert( db->mallocFailed );
   824    845       sqlite3ExprDelete(db, pLeft);
   825    846       sqlite3ExprDelete(db, pRight);
   826    847     }else{
   827    848       if( pRight ){
   828         -      pRoot->pRight = pRight;
          849  +      assert( pRoot->eX==EX_None );
          850  +      pRoot->x.pRight = pRight;
          851  +      pRoot->eX = EX_Right;
   829    852         pRoot->flags |= EP_Propagate & pRight->flags;
   830    853       }
   831    854       if( pLeft ){
   832    855         pRoot->pLeft = pLeft;
   833    856         pRoot->flags |= EP_Propagate & pLeft->flags;
   834    857       }
   835    858       exprSetHeight(pRoot);
................................................................................
   869    892   }
   870    893   
   871    894   /*
   872    895   ** Add pSelect to the Expr.x.pSelect field.  Or, if pExpr is NULL (due
   873    896   ** do a memory allocation failure) then delete the pSelect object.
   874    897   */
   875    898   void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){
   876         -  if( pExpr ){
          899  +  if( pExpr && pSelect ){
          900  +    assert( pExpr->eX==EX_None );
          901  +    pExpr->eX = EX_Select;
   877    902       pExpr->x.pSelect = pSelect;
   878         -    ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery);
          903  +    ExprSetProperty(pExpr, EP_Subquery);
   879    904       sqlite3ExprSetHeightAndFlags(pParse, pExpr);
   880    905     }else{
   881    906       assert( pParse->db->mallocFailed );
   882    907       sqlite3SelectDelete(pParse->db, pSelect);
   883    908     }
   884    909   }
          910  +
          911  +/*
          912  +** Add ExprList to the Expr.x.pList field.  Or, if pExpr is NULL (due
          913  +** do a memory allocation failure) then delete the pSelect object.
          914  +*/
          915  +void sqlite3PExprAddExprList(Parse *pParse, Expr *pExpr, ExprList *pList){
          916  +  if( pExpr && pList ){
          917  +    assert( pExpr->eX==EX_None );
          918  +    pExpr->eX = EX_List;
          919  +    pExpr->x.pList = pList;
          920  +    /* sqlite3ExprSetHeightAndFlags(pParse, pExpr); // done by caller */
          921  +  }else{
          922  +    assert( pParse->db->mallocFailed );
          923  +    sqlite3ExprListDelete(pParse->db, pList);
          924  +  }
          925  +}
   885    926   
   886    927   
   887    928   /*
   888    929   ** If the expression is always either TRUE or FALSE (respectively),
   889    930   ** then return 1.  If one cannot determine the truth value of the
   890    931   ** expression at compile-time return 0.
   891    932   **
................................................................................
   952    993     if( pNew==0 ){
   953    994       sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
   954    995       return 0;
   955    996     }
   956    997     if( pList && pList->nExpr > pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
   957    998       sqlite3ErrorMsg(pParse, "too many arguments on function %T", pToken);
   958    999     }
   959         -  pNew->x.pList = pList;
         1000  +  assert( pNew->eX==EX_None );
         1001  +  if( pList ){
         1002  +    pNew->eX = EX_List;
         1003  +    pNew->x.pList = pList;
         1004  +  }
   960   1005     ExprSetProperty(pNew, EP_HasFunc);
   961         -  assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   962   1006     sqlite3ExprSetHeightAndFlags(pParse, pNew);
   963   1007     if( eDistinct==SF_Distinct ) ExprSetProperty(pNew, EP_Distinct);
   964   1008     return pNew;
   965   1009   }
   966   1010   
   967   1011   /*
   968   1012   ** Assign a variable number to an expression that encodes a wildcard
................................................................................
  1048   1092   /*
  1049   1093   ** Recursively delete an expression tree.
  1050   1094   */
  1051   1095   static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){
  1052   1096     assert( p!=0 );
  1053   1097     /* Sanity check: Assert that the IntValue is non-negative if it exists */
  1054   1098     assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
         1099  +  assert( p->eX!=EX_Select || p->x.pSelect!=0 );
         1100  +  assert( p->eX!=EX_List || p->x.pList!=0 );
  1055   1101   #ifdef SQLITE_DEBUG
  1056   1102     if( ExprHasProperty(p, EP_Leaf) && !ExprHasProperty(p, EP_TokenOnly) ){
  1057   1103       assert( p->pLeft==0 );
  1058         -    assert( p->pRight==0 );
  1059         -    assert( p->x.pSelect==0 );
         1104  +    assert( p->eX==EX_None || p->eX==EX_Tab );
         1105  +  }
         1106  +  if( !ExprHasProperty(p, EP_TokenOnly) ){
         1107  +    assert( p->op!=TK_FUNCTION || p->pLeft==0 );
         1108  +  }
         1109  +  if( !ExprHasProperty(p, (EP_TokenOnly|EP_Reduced)) ){
         1110  +    assert( p->pWin==0 || p->pLeft==0 );
         1111  +    assert( p->pWin==0 || p->op==TK_FUNCTION );
  1060   1112     }
  1061   1113   #endif
  1062   1114     if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
  1063         -    /* The Expr.x union is never used at the same time as Expr.pRight */
  1064         -    assert( p->x.pList==0 || p->pRight==0 );
  1065   1115       if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
  1066         -    if( p->pRight ){
  1067         -      sqlite3ExprDeleteNN(db, p->pRight);
  1068         -    }else if( ExprHasProperty(p, EP_xIsSelect) ){
  1069         -      sqlite3SelectDelete(db, p->x.pSelect);
  1070         -    }else{
  1071         -      sqlite3ExprListDelete(db, p->x.pList);
         1116  +    switch( p->eX ){
         1117  +      case EX_Select: {
         1118  +        sqlite3SelectDelete(db, p->x.pSelect);
         1119  +        break;
         1120  +      }
         1121  +      case EX_List: {
         1122  +        sqlite3ExprListDelete(db, p->x.pList);
         1123  +        break;
         1124  +      }
         1125  +      case EX_Right: {
         1126  +        sqlite3ExprDelete(db, p->x.pRight);
         1127  +        break;
         1128  +      }
  1072   1129       }
  1073   1130       if( !ExprHasProperty(p, EP_Reduced) ){
  1074   1131         sqlite3WindowDelete(db, p->pWin);
  1075   1132       }
  1076   1133     }
  1077   1134     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
  1078   1135     if( !ExprHasProperty(p, EP_Static) ){
  1079   1136       sqlite3DbFreeNN(db, p);
  1080   1137     }
  1081   1138   }
  1082   1139   void sqlite3ExprDelete(sqlite3 *db, Expr *p){
  1083   1140     if( p ) sqlite3ExprDeleteNN(db, p);
  1084   1141   }
         1142  +
         1143  +void sqlite3ExprClearXUnion(sqlite3 *db, Expr *p){
         1144  +  switch( p->eX ){
         1145  +    case EX_Select: {
         1146  +      sqlite3SelectDelete(db, p->x.pSelect);
         1147  +      break;
         1148  +    }
         1149  +    case EX_List: {
         1150  +      sqlite3ExprListDelete(db, p->x.pList);
         1151  +      break;
         1152  +    }
         1153  +    case EX_Right: {
         1154  +      sqlite3ExprDelete(db, p->x.pRight);
         1155  +      break;
         1156  +    }
         1157  +  }
         1158  +  p->eX = EX_None;
         1159  +}
         1160  +void sqlite3ExprAddTab(sqlite3 *db, Expr *pExpr, Table *pTab){
         1161  +  sqlite3ExprClearXUnion(db, pExpr);
         1162  +  pExpr->eX = EX_Tab;
         1163  +  pExpr->x.pTab = pTab;
         1164  +}
  1085   1165   
  1086   1166   /*
  1087   1167   ** Return the number of bytes allocated for the expression structure 
  1088   1168   ** passed as the first argument. This is always one of EXPR_FULLSIZE,
  1089   1169   ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
  1090   1170   */
  1091   1171   static int exprStructSize(Expr *p){
................................................................................
  1140   1220     ){
  1141   1221       nSize = EXPR_FULLSIZE;
  1142   1222     }else{
  1143   1223       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
  1144   1224       assert( !ExprHasProperty(p, EP_FromJoin) ); 
  1145   1225       assert( !ExprHasProperty(p, EP_MemToken) );
  1146   1226       assert( !ExprHasProperty(p, EP_NoReduce) );
  1147         -    if( p->pLeft || p->x.pList ){
         1227  +    if( p->pLeft || p->eX!=EX_None ){
  1148   1228         nSize = EXPR_REDUCEDSIZE | EP_Reduced;
  1149   1229       }else{
  1150         -      assert( p->pRight==0 );
  1151   1230         nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
  1152   1231       }
  1153   1232     }
  1154   1233     return nSize;
  1155   1234   }
  1156   1235   
  1157   1236   /*
................................................................................
  1181   1260   ** descended from the Expr.x.pList or Expr.x.pSelect variables).
  1182   1261   */
  1183   1262   static int dupedExprSize(Expr *p, int flags){
  1184   1263     int nByte = 0;
  1185   1264     if( p ){
  1186   1265       nByte = dupedExprNodeSize(p, flags);
  1187   1266       if( flags&EXPRDUP_REDUCE ){
  1188         -      nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
         1267  +      nByte += dupedExprSize(p->pLeft, flags);
         1268  +      if( p->eX==EX_Right ) nByte += dupedExprSize(p->x.pRight, flags);
  1189   1269       }
  1190   1270     }
  1191   1271     return nByte;
  1192   1272   }
  1193   1273   
  1194   1274   /*
  1195   1275   ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 
  1196   1276   ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 
  1197   1277   ** to store the copy of expression p, the copies of p->u.zToken
  1198         -** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
         1278  +** (if applicable), and the copies of the p->pLeft and p->x.pRight expressions,
  1199   1279   ** if any. Before returning, *pzBuffer is set to the first byte past the
  1200   1280   ** portion of the buffer copied into by this function.
  1201   1281   */
  1202   1282   static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){
  1203   1283     Expr *pNew;           /* Value to return */
  1204   1284     u8 *zAlloc;           /* Memory space from which to build Expr object */
  1205   1285     u32 staticFlag;       /* EP_Static if space not obtained from malloc */
................................................................................
  1244   1324         }
  1245   1325       }
  1246   1326   
  1247   1327       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
  1248   1328       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
  1249   1329       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
  1250   1330       pNew->flags |= staticFlag;
         1331  +    assert( pNew->eX==p->eX );
         1332  +    assert( pNew->eV==p->eV );
  1251   1333   
  1252   1334       /* Copy the p->u.zToken string, if any. */
  1253   1335       if( nToken ){
  1254   1336         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
  1255   1337         memcpy(zToken, p->u.zToken, nToken);
  1256   1338       }
  1257   1339   
  1258   1340       if( 0==((p->flags|pNew->flags) & (EP_TokenOnly|EP_Leaf)) ){
  1259   1341         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
  1260         -      if( ExprHasProperty(p, EP_xIsSelect) ){
  1261         -        pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
  1262         -      }else{
  1263         -        pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
         1342  +      switch( p->eX ){
         1343  +        case EX_Select: {
         1344  +          assert( pNew->eX==EX_Select );
         1345  +          pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags);
         1346  +          if( pNew->x.pSelect==0 ) pNew->eX = EX_None;
         1347  +          break;
         1348  +        }
         1349  +        case EX_List: {
         1350  +          assert( pNew->eX==EX_List );
         1351  +          pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags);
         1352  +          if( pNew->x.pList==0 ) pNew->eX = EX_None;
         1353  +          break;
         1354  +        }
  1264   1355         }
  1265   1356       }
  1266   1357   
  1267         -    /* Fill in pNew->pLeft and pNew->pRight. */
         1358  +    /* Fill in pNew->pLeft and pNew->x.pRight. */
  1268   1359       if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
  1269   1360         zAlloc += dupedExprNodeSize(p, dupFlags);
  1270   1361         if( !ExprHasProperty(pNew, EP_TokenOnly|EP_Leaf) ){
  1271   1362           pNew->pLeft = p->pLeft ?
  1272   1363                         exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0;
  1273         -        pNew->pRight = p->pRight ?
  1274         -                       exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0;
         1364  +        if( p->eX==EX_Right ){
         1365  +          pNew->x.pRight = exprDup(db, p->x.pRight, EXPRDUP_REDUCE, &zAlloc);
         1366  +        }
  1275   1367         }
  1276   1368         if( pzBuffer ){
  1277   1369           *pzBuffer = zAlloc;
  1278   1370         }
  1279   1371       }else{
  1280   1372   #ifndef SQLITE_OMIT_WINDOWFUNC
  1281   1373         if( ExprHasProperty(p, EP_Reduced|EP_TokenOnly) ){
................................................................................
  1283   1375         }else{
  1284   1376           pNew->pWin = sqlite3WindowDup(db, pNew, p->pWin);
  1285   1377         }
  1286   1378   #endif /* SQLITE_OMIT_WINDOWFUNC */
  1287   1379         if( !ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
  1288   1380           if( pNew->op==TK_SELECT_COLUMN ){
  1289   1381             pNew->pLeft = p->pLeft;
  1290         -          assert( p->iColumn==0 || p->pRight==0 );
  1291         -          assert( p->pRight==0  || p->pRight==p->pLeft );
         1382  +          assert( p->iColumn==0 || p->eX!=EX_Right );
         1383  +          /* OLD: assert( p->pRight==0  || p->x.pRight==p->pLeft ); */
  1292   1384           }else{
  1293   1385             pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
  1294   1386           }
  1295         -        pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
         1387  +        if( p->eX==EX_Right ){
         1388  +          pNew->x.pRight = sqlite3ExprDup(db, p->x.pRight, 0);
         1389  +          if( pNew->x.pRight==0 ) pNew->eX = EX_None;
         1390  +        }
  1296   1391         }
  1297   1392       }
  1298   1393     }
  1299   1394     return pNew;
  1300   1395   }
  1301   1396   
  1302   1397   /*
................................................................................
  1365   1460       pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
  1366   1461       if( pOldExpr 
  1367   1462        && pOldExpr->op==TK_SELECT_COLUMN
  1368   1463        && (pNewExpr = pItem->pExpr)!=0 
  1369   1464       ){
  1370   1465         assert( pNewExpr->iColumn==0 || i>0 );
  1371   1466         if( pNewExpr->iColumn==0 ){
  1372         -        assert( pOldExpr->pLeft==pOldExpr->pRight );
  1373         -        pPriorSelectCol = pNewExpr->pLeft = pNewExpr->pRight;
         1467  +        assert( pOldExpr->pLeft==pOldExpr->x.pRight );
         1468  +        pPriorSelectCol = pNewExpr->pLeft = pNewExpr->x.pRight;
  1374   1469         }else{
  1375   1470           assert( i>0 );
  1376   1471           assert( pItem[-1].pExpr!=0 );
  1377   1472           assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
  1378   1473           assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
  1379   1474           pNewExpr->pLeft = pPriorSelectCol;
  1380   1475         }
................................................................................
  1612   1707     if( !db->mallocFailed && pExpr->op==TK_SELECT && ALWAYS(pList!=0) ){
  1613   1708       Expr *pFirst = pList->a[iFirst].pExpr;
  1614   1709       assert( pFirst!=0 );
  1615   1710       assert( pFirst->op==TK_SELECT_COLUMN );
  1616   1711        
  1617   1712       /* Store the SELECT statement in pRight so it will be deleted when
  1618   1713       ** sqlite3ExprListDelete() is called */
  1619         -    pFirst->pRight = pExpr;
         1714  +    assert( pFirst->eX==EX_None );
         1715  +    pFirst->x.pRight = pExpr;
         1716  +    pFirst->eX = EX_Right;
  1620   1717       pExpr = 0;
  1621   1718   
  1622   1719       /* Remember the size of the LHS in iTable so that we can check that
  1623   1720       ** the RHS and LHS sizes match during code generation. */
  1624   1721       pFirst->iTable = pColumns->nId;
  1625   1722     }
  1626   1723   
................................................................................
  1950   2047         if( sqlite3IsBinary(pColl) ){
  1951   2048           return WRC_Prune;
  1952   2049         }
  1953   2050       }
  1954   2051     }
  1955   2052   
  1956   2053     /* Check if pExpr is a sub-select. If so, consider it variable. */
  1957         -  if( ExprHasProperty(pExpr, EP_xIsSelect) ){
         2054  +  if( pExpr->eX==EX_Select ){
  1958   2055       pWalker->eCode = 0;
  1959   2056       return WRC_Abort;
  1960   2057     }
  1961   2058   
  1962   2059     return exprNodeIsConstant(pWalker, pExpr);
  1963   2060   }
  1964   2061   
................................................................................
  2084   2181     switch( op ){
  2085   2182       case TK_INTEGER:
  2086   2183       case TK_STRING:
  2087   2184       case TK_FLOAT:
  2088   2185       case TK_BLOB:
  2089   2186         return 0;
  2090   2187       case TK_COLUMN:
         2188  +      assert( p->eX==EX_Tab );
  2091   2189         return ExprHasProperty(p, EP_CanBeNull) ||
  2092         -             p->pTab==0 ||  /* Reference to column of index on expression */
  2093         -             (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0);
         2190  +             p->x.pTab==0 ||  /* Reference to column of index on expression */
         2191  +             (p->iColumn>=0 && p->x.pTab->aCol[p->iColumn].notNull==0);
  2094   2192       default:
  2095   2193         return 1;
  2096   2194     }
  2097   2195   }
  2098   2196   
  2099   2197   /*
  2100   2198   ** Return TRUE if the given expression is a constant which would be
................................................................................
  2156   2254   #ifndef SQLITE_OMIT_SUBQUERY
  2157   2255   static Select *isCandidateForInOpt(Expr *pX){
  2158   2256     Select *p;
  2159   2257     SrcList *pSrc;
  2160   2258     ExprList *pEList;
  2161   2259     Table *pTab;
  2162   2260     int i;
  2163         -  if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0;  /* Not a subquery */
  2164         -  if( ExprHasProperty(pX, EP_VarSelect)  ) return 0;  /* Correlated subq */
         2261  +  if( pX->eX!=EX_Select ) return 0;      /* Not a subquery */
         2262  +  if( ExprHasProperty(pX, EP_VarSelect) ){
         2263  +    return 0;                            /* Correlated subq */
         2264  +  }
  2165   2265     p = pX->x.pSelect;
  2166   2266     if( p->pPrior ) return 0;              /* Not a compound SELECT */
  2167   2267     if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
  2168   2268       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
  2169   2269       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
  2170   2270       return 0; /* No DISTINCT keyword and no aggregate functions */
  2171   2271     }
................................................................................
  2215   2315   /*
  2216   2316   ** The argument is an IN operator with a list (not a subquery) on the 
  2217   2317   ** right-hand side.  Return TRUE if that list is constant.
  2218   2318   */
  2219   2319   static int sqlite3InRhsIsConstant(Expr *pIn){
  2220   2320     Expr *pLHS;
  2221   2321     int res;
  2222         -  assert( !ExprHasProperty(pIn, EP_xIsSelect) );
         2322  +  assert( pIn->eX!=EX_Select );
  2223   2323     pLHS = pIn->pLeft;
  2224   2324     pIn->pLeft = 0;
  2225   2325     res = sqlite3ExprIsConstant(pIn);
  2226   2326     pIn->pLeft = pLHS;
  2227   2327     return res;
  2228   2328   }
  2229   2329   #endif
................................................................................
  2326   2426     mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0;
  2327   2427   
  2328   2428     /* If the RHS of this IN(...) operator is a SELECT, and if it matters 
  2329   2429     ** whether or not the SELECT result contains NULL values, check whether
  2330   2430     ** or not NULL is actually possible (it may not be, for example, due 
  2331   2431     ** to NOT NULL constraints in the schema). If no NULL values are possible,
  2332   2432     ** set prRhsHasNull to 0 before continuing.  */
  2333         -  if( prRhsHasNull && (pX->flags & EP_xIsSelect) ){
         2433  +  if( prRhsHasNull && pX->eX==EX_Select ){
  2334   2434       int i;
  2335   2435       ExprList *pEList = pX->x.pSelect->pEList;
  2336   2436       for(i=0; i<pEList->nExpr; i++){
  2337   2437         if( sqlite3ExprCanBeNull(pEList->a[i].pExpr) ) break;
  2338   2438       }
  2339   2439       if( i==pEList->nExpr ){
  2340   2440         prRhsHasNull = 0;
................................................................................
  2422   2522               }
  2423   2523             }
  2424   2524     
  2425   2525             colUsed = 0;   /* Columns of index used so far */
  2426   2526             for(i=0; i<nExpr; i++){
  2427   2527               Expr *pLhs = sqlite3VectorFieldSubexpr(pX->pLeft, i);
  2428   2528               Expr *pRhs = pEList->a[i].pExpr;
  2429         -            CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
         2529  +            CollSeq *pReq = sqlite3ComparisonCollSeq(pParse, pLhs, pRhs);
  2430   2530               int j;
  2431   2531     
  2432   2532               assert( pReq!=0 || pRhs->iColumn==XN_ROWID || pParse->nErr );
  2433   2533               for(j=0; j<nExpr; j++){
  2434   2534                 if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue;
  2435   2535                 assert( pIdx->azColl[j] );
  2436   2536                 if( pReq!=0 && sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ){
................................................................................
  2479   2579     ** and IN_INDEX_NOOP is an allowed reply
  2480   2580     ** and the RHS of the IN operator is a list, not a subquery
  2481   2581     ** and the RHS is not constant or has two or fewer terms,
  2482   2582     ** then it is not worth creating an ephemeral table to evaluate
  2483   2583     ** the IN operator so return IN_INDEX_NOOP.
  2484   2584     */
  2485   2585     if( eType==0
  2486         -   && (inFlags & IN_INDEX_NOOP_OK)
  2487         -   && !ExprHasProperty(pX, EP_xIsSelect)
         2586  +   && (inFlags & IN_INDEX_NOOP_OK)!=0
         2587  +   && pX->eX==EX_List
  2488   2588      && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2)
  2489   2589     ){
  2490   2590       eType = IN_INDEX_NOOP;
  2491   2591     }
  2492   2592   
  2493   2593     if( eType==0 ){
  2494   2594       /* Could not find an existing table or index to use as the RHS b-tree.
................................................................................
  2495   2595       ** We will have to generate an ephemeral table to do the job.
  2496   2596       */
  2497   2597       u32 savedNQueryLoop = pParse->nQueryLoop;
  2498   2598       int rMayHaveNull = 0;
  2499   2599       eType = IN_INDEX_EPH;
  2500   2600       if( inFlags & IN_INDEX_LOOP ){
  2501   2601         pParse->nQueryLoop = 0;
  2502         -      if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
         2602  +      if( pX->pLeft->iColumn<0 && pX->eX==EX_List ){
  2503   2603           eType = IN_INDEX_ROWID;
  2504   2604         }
  2505   2605       }else if( prRhsHasNull ){
  2506   2606         *prRhsHasNull = rMayHaveNull = ++pParse->nMem;
  2507   2607       }
  2508   2608       sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
  2509   2609       pParse->nQueryLoop = savedNQueryLoop;
................................................................................
  2528   2628   **
  2529   2629   ** It is the responsibility of the caller to ensure that the returned
  2530   2630   ** string is eventually freed using sqlite3DbFree().
  2531   2631   */
  2532   2632   static char *exprINAffinity(Parse *pParse, Expr *pExpr){
  2533   2633     Expr *pLeft = pExpr->pLeft;
  2534   2634     int nVal = sqlite3ExprVectorSize(pLeft);
  2535         -  Select *pSelect = (pExpr->flags & EP_xIsSelect) ? pExpr->x.pSelect : 0;
         2635  +  Select *pSelect = (pExpr->eX==EX_Select) ? pExpr->x.pSelect : 0;
  2536   2636     char *zRet;
  2537   2637   
  2538   2638     assert( pExpr->op==TK_IN );
  2539   2639     zRet = sqlite3DbMallocRaw(pParse->db, nVal+1);
  2540   2640     if( zRet ){
  2541   2641       int i;
  2542   2642       for(i=0; i<nVal; i++){
................................................................................
  2576   2676   **
  2577   2677   ** Or, if it is a regular scalar vector:
  2578   2678   **
  2579   2679   **   "row value misused"
  2580   2680   */   
  2581   2681   void sqlite3VectorErrorMsg(Parse *pParse, Expr *pExpr){
  2582   2682   #ifndef SQLITE_OMIT_SUBQUERY
  2583         -  if( pExpr->flags & EP_xIsSelect ){
         2683  +  if( pExpr->eX==EX_Select ){
  2584   2684       sqlite3SubselectError(pParse, pExpr->x.pSelect->pEList->nExpr, 1);
  2585   2685     }else
  2586   2686   #endif
  2587   2687     {
  2588   2688       sqlite3ErrorMsg(pParse, "row value misused");
  2589   2689     }
  2590   2690   }
................................................................................
  2668   2768         ** is used.
  2669   2769         */
  2670   2770         pExpr->iTable = pParse->nTab++;
  2671   2771         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 
  2672   2772             pExpr->iTable, (isRowid?0:nVal));
  2673   2773         pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1);
  2674   2774   
  2675         -      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
         2775  +      assert( pExpr->eX==EX_Select || pExpr->eX==EX_List );
         2776  +      if( pExpr->eX==EX_Select ){
  2676   2777           /* Case 1:     expr IN (SELECT ...)
  2677   2778           **
  2678   2779           ** Generate code to write the results of the select into the temporary
  2679   2780           ** table allocated and opened above.
  2680   2781           */
  2681   2782           Select *pSelect = pExpr->x.pSelect;
  2682   2783           ExprList *pEList = pSelect->pEList;
................................................................................
  2703   2804             sqlite3DbFree(pParse->db, dest.zAffSdst);
  2704   2805             assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
  2705   2806             assert( pEList!=0 );
  2706   2807             assert( pEList->nExpr>0 );
  2707   2808             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  2708   2809             for(i=0; i<nVal; i++){
  2709   2810               Expr *p = sqlite3VectorFieldSubexpr(pLeft, i);
  2710         -            pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq(
         2811  +            pKeyInfo->aColl[i] = sqlite3ComparisonCollSeq(
  2711   2812                   pParse, p, pEList->a[i].pExpr
  2712   2813               );
  2713   2814             }
  2714   2815           }
  2715   2816         }else if( ALWAYS(pExpr->x.pList!=0) ){
  2716   2817           /* Case 2:     expr IN (exprlist)
  2717   2818           **
................................................................................
  2797   2898         SelectDest dest;                      /* How to deal with SELECT result */
  2798   2899         int nReg;                             /* Registers to allocate */
  2799   2900         Expr *pLimit;                         /* New limit expression */
  2800   2901   
  2801   2902         testcase( pExpr->op==TK_EXISTS );
  2802   2903         testcase( pExpr->op==TK_SELECT );
  2803   2904         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
  2804         -      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
         2905  +      assert( pExpr->eX==EX_Select );
  2805   2906   
  2806   2907         pSel = pExpr->x.pSelect;
  2807   2908         ExplainQueryPlan((pParse, 1, "%sSCALAR SUBQUERY",
  2808   2909               jmpIfDynamic>=0?"":"CORRELATED "));
  2809   2910         nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1;
  2810   2911         sqlite3SelectDestInit(&dest, 0, pParse->nMem+1);
  2811   2912         pParse->nMem += nReg;
................................................................................
  2854   2955   ** Expr pIn is an IN(...) expression. This function checks that the 
  2855   2956   ** sub-select on the RHS of the IN() operator has the same number of 
  2856   2957   ** columns as the vector on the LHS. Or, if the RHS of the IN() is not 
  2857   2958   ** a sub-query, that the LHS is a vector of size 1.
  2858   2959   */
  2859   2960   int sqlite3ExprCheckIN(Parse *pParse, Expr *pIn){
  2860   2961     int nVector = sqlite3ExprVectorSize(pIn->pLeft);
  2861         -  if( (pIn->flags & EP_xIsSelect) ){
         2962  +  if( pIn->eX==EX_Select ){
  2862   2963       if( nVector!=pIn->x.pSelect->pEList->nExpr ){
  2863   2964         sqlite3SubselectError(pParse, pIn->x.pSelect->pEList->nExpr, nVector);
  2864   2965         return 1;
  2865   2966       }
  2866   2967     }else if( nVector!=1 ){
  2867   2968       sqlite3VectorErrorMsg(pParse, pIn->pLeft);
  2868   2969       return 1;
................................................................................
  2984   3085     if( eType==IN_INDEX_NOOP ){
  2985   3086       ExprList *pList = pExpr->x.pList;
  2986   3087       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  2987   3088       int labelOk = sqlite3VdbeMakeLabel(v);
  2988   3089       int r2, regToFree;
  2989   3090       int regCkNull = 0;
  2990   3091       int ii;
  2991         -    assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
         3092  +    assert( pExpr->eX==EX_List );
  2992   3093       if( destIfNull!=destIfFalse ){
  2993   3094         regCkNull = sqlite3GetTempReg(pParse);
  2994   3095         sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
  2995   3096       }
  2996   3097       for(ii=0; ii<pList->nExpr; ii++){
  2997   3098         r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
  2998   3099         if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
................................................................................
  3373   3474         if( ExprHasProperty(pExpr, EP_FixedCol) ){
  3374   3475           /* This COLUMN expression is really a constant due to WHERE clause
  3375   3476           ** constraints, and that constant is coded by the pExpr->pLeft
  3376   3477           ** expresssion.  However, make sure the constant has the correct
  3377   3478           ** datatype by applying the Affinity of the table column to the
  3378   3479           ** constant.
  3379   3480           */
         3481  +        int aff;
  3380   3482           int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
  3381         -        int aff = sqlite3TableColumnAffinity(pExpr->pTab, pExpr->iColumn);
         3483  +        assert( pExpr->eX==EX_Tab );
         3484  +        aff = sqlite3TableColumnAffinity(pExpr->x.pTab, pExpr->iColumn);
  3382   3485           if( aff!=SQLITE_AFF_BLOB ){
  3383   3486             static const char zAff[] = "B\000C\000D\000E";
  3384   3487             assert( SQLITE_AFF_BLOB=='A' );
  3385   3488             assert( SQLITE_AFF_TEXT=='B' );
  3386   3489             if( iReg!=target ){
  3387   3490               sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
  3388   3491               iReg = target;
................................................................................
  3398   3501             return pExpr->iColumn - pParse->iSelfTab;
  3399   3502           }else{
  3400   3503             /* Coding an expression that is part of an index where column names
  3401   3504             ** in the index refer to the table to which the index belongs */
  3402   3505             iTab = pParse->iSelfTab - 1;
  3403   3506           }
  3404   3507         }
  3405         -      return sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
         3508  +      assert( pExpr->eX==EX_Tab );
         3509  +      return sqlite3ExprCodeGetColumn(pParse, pExpr->x.pTab,
  3406   3510                                  pExpr->iColumn, iTab, target,
  3407   3511                                  pExpr->op2);
  3408   3512       }
  3409   3513       case TK_INTEGER: {
  3410   3514         codeInteger(pParse, pExpr, 0, target);
  3411   3515         return target;
  3412   3516       }
................................................................................
  3487   3591       case TK_NE:
  3488   3592       case TK_EQ: {
  3489   3593         Expr *pLeft = pExpr->pLeft;
  3490   3594         if( sqlite3ExprIsVector(pLeft) ){
  3491   3595           codeVectorCompare(pParse, pExpr, target, op, p5);
  3492   3596         }else{
  3493   3597           r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
  3494         -        r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  3495         -        codeCompare(pParse, pLeft, pExpr->pRight, op,
         3598  +        assert( pExpr->eX==EX_Right );
         3599  +        r2 = sqlite3ExprCodeTemp(pParse, pExpr->x.pRight, &regFree2);
         3600  +        codeCompare(pParse, pLeft, pExpr->x.pRight, op,
  3496   3601               r1, r2, inReg, SQLITE_STOREP2 | p5);
  3497   3602           assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
  3498   3603           assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
  3499   3604           assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
  3500   3605           assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
  3501   3606           assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
  3502   3607           assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
................................................................................
  3525   3630         assert( TK_BITAND==OP_BitAnd );      testcase( op==TK_BITAND );
  3526   3631         assert( TK_BITOR==OP_BitOr );        testcase( op==TK_BITOR );
  3527   3632         assert( TK_SLASH==OP_Divide );       testcase( op==TK_SLASH );
  3528   3633         assert( TK_LSHIFT==OP_ShiftLeft );   testcase( op==TK_LSHIFT );
  3529   3634         assert( TK_RSHIFT==OP_ShiftRight );  testcase( op==TK_RSHIFT );
  3530   3635         assert( TK_CONCAT==OP_Concat );      testcase( op==TK_CONCAT );
  3531   3636         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3532         -      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
         3637  +      assert( pExpr->eX==EX_Right );
         3638  +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->x.pRight, &regFree2);
  3533   3639         sqlite3VdbeAddOp3(v, op, r2, r1, target);
  3534   3640         testcase( regFree1==0 );
  3535   3641         testcase( regFree2==0 );
  3536   3642         break;
  3537   3643       }
  3538   3644       case TK_UMINUS: {
  3539   3645         Expr *pLeft = pExpr->pLeft;
................................................................................
  3544   3650   #ifndef SQLITE_OMIT_FLOATING_POINT
  3545   3651         }else if( pLeft->op==TK_FLOAT ){
  3546   3652           assert( !ExprHasProperty(pExpr, EP_IntValue) );
  3547   3653           codeReal(v, pLeft->u.zToken, 1, target);
  3548   3654           return target;
  3549   3655   #endif
  3550   3656         }else{
         3657  +        memset(&tempX, 0, sizeof(tempX));
  3551   3658           tempX.op = TK_INTEGER;
  3552   3659           tempX.flags = EP_IntValue|EP_TokenOnly;
  3553   3660           tempX.u.iValue = 0;
  3554   3661           r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
  3555   3662           r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
  3556   3663           sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
  3557   3664           testcase( regFree2==0 );
................................................................................
  3568   3675         break;
  3569   3676       }
  3570   3677       case TK_TRUTH: {
  3571   3678         int isTrue;    /* IS TRUE or IS NOT TRUE */
  3572   3679         int bNormal;   /* IS TRUE or IS FALSE */
  3573   3680         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  3574   3681         testcase( regFree1==0 );
  3575         -      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
         3682  +      assert( pExpr->eX==EX_Right );
         3683  +      isTrue = sqlite3ExprTruthValue(pExpr->x.pRight);
  3576   3684         bNormal = pExpr->op2==TK_IS;
  3577   3685         testcase( isTrue && bNormal);
  3578   3686         testcase( !isTrue && bNormal);
  3579   3687         sqlite3VdbeAddOp4Int(v, OP_IsTrue, r1, inReg, !isTrue, isTrue ^ bNormal);
  3580   3688         break;
  3581   3689       }
  3582   3690       case TK_ISNULL:
................................................................................
  3622   3730   #endif
  3623   3731   
  3624   3732         if( ConstFactorOk(pParse) && sqlite3ExprIsConstantNotJoin(pExpr) ){
  3625   3733           /* SQL functions can be expensive. So try to move constant functions
  3626   3734           ** out of the inner loop, even if that means an extra OP_Copy. */
  3627   3735           return sqlite3ExprCodeAtInit(pParse, pExpr, -1);
  3628   3736         }
  3629         -      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
         3737  +      assert( pExpr->eX==EX_List || pExpr->eX==EX_None );
  3630   3738         if( ExprHasProperty(pExpr, EP_TokenOnly) ){
  3631   3739           pFarg = 0;
  3632   3740         }else{
  3633   3741           pFarg = pExpr->x.pList;
  3634   3742         }
  3635   3743         nFarg = pFarg ? pFarg->nExpr : 0;
  3636   3744         assert( !ExprHasProperty(pExpr, EP_IntValue) );
................................................................................
  3856   3964         **
  3857   3965         ** Then p1 is interpreted as follows:
  3858   3966         **
  3859   3967         **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
  3860   3968         **   p1==1   ->    old.a         p1==4   ->    new.a
  3861   3969         **   p1==2   ->    old.b         p1==5   ->    new.b       
  3862   3970         */
  3863         -      Table *pTab = pExpr->pTab;
         3971  +      Table *pTab;
         3972  +      assert( pExpr->eX==EX_Tab );
         3973  +      pTab = pExpr->x.pTab;
  3864   3974         int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
  3865   3975   
  3866   3976         assert( pExpr->iTable==0 || pExpr->iTable==1 );
  3867   3977         assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
  3868   3978         assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
  3869   3979         assert( p1>=0 && p1<(pTab->nCol*2+2) );
  3870   3980   
  3871   3981         sqlite3VdbeAddOp2(v, OP_Param, p1, target);
         3982  +      assert( pExpr->eX==EX_Tab );
  3872   3983         VdbeComment((v, "r[%d]=%s.%s", target,
  3873   3984           (pExpr->iTable ? "new" : "old"),
  3874         -        (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName)
         3985  +        (pExpr->iColumn<0 ? "rowid" : pExpr->x.pTab->aCol[pExpr->iColumn].zName)
  3875   3986         ));
  3876   3987   
  3877   3988   #ifndef SQLITE_OMIT_FLOATING_POINT
  3878   3989         /* If the column has REAL affinity, it may currently be stored as an
  3879   3990         ** integer. Use OP_RealAffinity to make sure it is really real.
  3880   3991         **
  3881   3992         ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to
................................................................................
  3931   4042         int i;                            /* Loop counter */
  3932   4043         ExprList *pEList;                 /* List of WHEN terms */
  3933   4044         struct ExprList_item *aListelem;  /* Array of WHEN terms */
  3934   4045         Expr opCompare;                   /* The X==Ei expression */
  3935   4046         Expr *pX;                         /* The X expression */
  3936   4047         Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
  3937   4048   
  3938         -      assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
  3939         -      assert(pExpr->x.pList->nExpr > 0);
         4049  +      assert( pExpr->eX==EX_List );
         4050  +      assert( pExpr->x.pList->nExpr > 0);
  3940   4051         pEList = pExpr->x.pList;
  3941   4052         aListelem = pEList->a;
  3942   4053         nExpr = pEList->nExpr;
  3943   4054         endLabel = sqlite3VdbeMakeLabel(v);
  3944   4055         if( (pX = pExpr->pLeft)!=0 ){
  3945   4056           tempX = *pX;
  3946   4057           testcase( pX->op==TK_COLUMN );
................................................................................
  3955   4066           ** So make sure that the regFree1 register is not reused for other
  3956   4067           ** purposes and possibly overwritten.  */
  3957   4068           regFree1 = 0;
  3958   4069         }
  3959   4070         for(i=0; i<nExpr-1; i=i+2){
  3960   4071           if( pX ){
  3961   4072             assert( pTest!=0 );
  3962         -          opCompare.pRight = aListelem[i].pExpr;
         4073  +          opCompare.x.pRight = aListelem[i].pExpr;
         4074  +          opCompare.eX = EX_Right;
  3963   4075           }else{
  3964   4076             pTest = aListelem[i].pExpr;
  3965   4077           }
  3966   4078           nextCase = sqlite3VdbeMakeLabel(v);
  3967   4079           testcase( pTest->op==TK_COLUMN );
  3968   4080           sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
  3969   4081           testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
................................................................................
  4264   4376     int regFree1 = 0; /* Temporary use register */
  4265   4377   
  4266   4378   
  4267   4379     memset(&compLeft, 0, sizeof(Expr));
  4268   4380     memset(&compRight, 0, sizeof(Expr));
  4269   4381     memset(&exprAnd, 0, sizeof(Expr));
  4270   4382   
  4271         -  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
         4383  +  assert( pExpr->eX==EX_List );
  4272   4384     exprX = *pExpr->pLeft;
  4273   4385     exprAnd.op = TK_AND;
  4274   4386     exprAnd.pLeft = &compLeft;
  4275         -  exprAnd.pRight = &compRight;
         4387  +  exprAnd.x.pRight = &compRight;
         4388  +  exprAnd.eX = EX_Right;
  4276   4389     compLeft.op = TK_GE;
  4277   4390     compLeft.pLeft = &exprX;
  4278         -  compLeft.pRight = pExpr->x.pList->a[0].pExpr;
         4391  +  compLeft.x.pRight = pExpr->x.pList->a[0].pExpr;
         4392  +  compLeft.eX = EX_Right;
  4279   4393     compRight.op = TK_LE;
  4280   4394     compRight.pLeft = &exprX;
  4281         -  compRight.pRight = pExpr->x.pList->a[1].pExpr;
         4395  +  compRight.x.pRight = pExpr->x.pList->a[1].pExpr;
         4396  +  compRight.eX = EX_Right;
  4282   4397     exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
  4283   4398     if( xJump ){
  4284   4399       xJump(pParse, &exprAnd, dest, jumpIfNull);
  4285   4400     }else{
  4286   4401       /* Mark the expression is being from the ON or USING clause of a join
  4287   4402       ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
  4288   4403       ** it into the Parse.pConstExpr list.  We should use a new bit for this,
................................................................................
  4331   4446     if( NEVER(pExpr==0) ) return;  /* No way this can happen */
  4332   4447     op = pExpr->op;
  4333   4448     switch( op ){
  4334   4449       case TK_AND: {
  4335   4450         int d2 = sqlite3VdbeMakeLabel(v);
  4336   4451         testcase( jumpIfNull==0 );
  4337   4452         sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
  4338         -      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
         4453  +      assert( pExpr->eX==EX_Right );
         4454  +      sqlite3ExprIfTrue(pParse, pExpr->x.pRight, dest, jumpIfNull);
  4339   4455         sqlite3VdbeResolveLabel(v, d2);
  4340   4456         break;
  4341   4457       }
  4342   4458       case TK_OR: {
  4343   4459         testcase( jumpIfNull==0 );
  4344   4460         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4345         -      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
         4461  +      assert( pExpr->eX==EX_Right );
         4462  +      sqlite3ExprIfTrue(pParse, pExpr->x.pRight, dest, jumpIfNull);
  4346   4463         break;
  4347   4464       }
  4348   4465       case TK_NOT: {
  4349   4466         testcase( jumpIfNull==0 );
  4350   4467         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4351   4468         break;
  4352   4469       }
  4353   4470       case TK_TRUTH: {
  4354   4471         int isNot;      /* IS NOT TRUE or IS NOT FALSE */
  4355   4472         int isTrue;     /* IS TRUE or IS NOT TRUE */
  4356   4473         testcase( jumpIfNull==0 );
  4357   4474         isNot = pExpr->op2==TK_ISNOT;
  4358         -      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
         4475  +      assert( pExpr->eX==EX_Right );
         4476  +      isTrue = sqlite3ExprTruthValue(pExpr->x.pRight);
  4359   4477         testcase( isTrue && isNot );
  4360   4478         testcase( !isTrue && isNot );
  4361   4479         if( isTrue ^ isNot ){
  4362   4480           sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest,
  4363   4481                             isNot ? SQLITE_JUMPIFNULL : 0);
  4364   4482         }else{
  4365   4483           sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
................................................................................
  4379   4497       case TK_GT:
  4380   4498       case TK_GE:
  4381   4499       case TK_NE:
  4382   4500       case TK_EQ: {
  4383   4501         if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
  4384   4502         testcase( jumpIfNull==0 );
  4385   4503         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  4386         -      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  4387         -      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
         4504  +      assert( pExpr->eX==EX_Right );
         4505  +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->x.pRight, &regFree2);
         4506  +      codeCompare(pParse, pExpr->pLeft, pExpr->x.pRight, op,
  4388   4507                     r1, r2, dest, jumpIfNull);
  4389   4508         assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
  4390   4509         assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
  4391   4510         assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
  4392   4511         assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
  4393   4512         assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
  4394   4513         VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ);
................................................................................
  4497   4616     assert( pExpr->op!=TK_GT || op==OP_Le );
  4498   4617     assert( pExpr->op!=TK_GE || op==OP_Lt );
  4499   4618   
  4500   4619     switch( pExpr->op ){
  4501   4620       case TK_AND: {
  4502   4621         testcase( jumpIfNull==0 );
  4503   4622         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4504         -      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
         4623  +      assert( pExpr->eX==EX_Right );
         4624  +      sqlite3ExprIfFalse(pParse, pExpr->x.pRight, dest, jumpIfNull);
  4505   4625         break;
  4506   4626       }
  4507   4627       case TK_OR: {
  4508   4628         int d2 = sqlite3VdbeMakeLabel(v);
  4509   4629         testcase( jumpIfNull==0 );
  4510   4630         sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
  4511         -      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
         4631  +      assert( pExpr->eX==EX_Right );
         4632  +      sqlite3ExprIfFalse(pParse, pExpr->x.pRight, dest, jumpIfNull);
  4512   4633         sqlite3VdbeResolveLabel(v, d2);
  4513   4634         break;
  4514   4635       }
  4515   4636       case TK_NOT: {
  4516   4637         testcase( jumpIfNull==0 );
  4517   4638         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4518   4639         break;
  4519   4640       }
  4520   4641       case TK_TRUTH: {
  4521   4642         int isNot;   /* IS NOT TRUE or IS NOT FALSE */
  4522   4643         int isTrue;  /* IS TRUE or IS NOT TRUE */
  4523   4644         testcase( jumpIfNull==0 );
  4524   4645         isNot = pExpr->op2==TK_ISNOT;
  4525         -      isTrue = sqlite3ExprTruthValue(pExpr->pRight);
         4646  +      assert( pExpr->eX==EX_Right );
         4647  +      isTrue = sqlite3ExprTruthValue(pExpr->x.pRight);
  4526   4648         testcase( isTrue && isNot );
  4527   4649         testcase( !isTrue && isNot );
  4528   4650         if( isTrue ^ isNot ){
  4529   4651           /* IS TRUE and IS NOT FALSE */
  4530   4652           sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest,
  4531   4653                              isNot ? 0 : SQLITE_JUMPIFNULL);
  4532   4654   
................................................................................
  4549   4671       case TK_GT:
  4550   4672       case TK_GE:
  4551   4673       case TK_NE:
  4552   4674       case TK_EQ: {
  4553   4675         if( sqlite3ExprIsVector(pExpr->pLeft) ) goto default_expr;
  4554   4676         testcase( jumpIfNull==0 );
  4555   4677         r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
  4556         -      r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
  4557         -      codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
         4678  +      assert( pExpr->eX==EX_Right );
         4679  +      r2 = sqlite3ExprCodeTemp(pParse, pExpr->x.pRight, &regFree2);
         4680  +      codeCompare(pParse, pExpr->pLeft, pExpr->x.pRight, op,
  4558   4681                     r1, r2, dest, jumpIfNull);
  4559   4682         assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
  4560   4683         assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
  4561   4684         assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
  4562   4685         assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
  4563   4686         assert(TK_EQ==OP_Eq); testcase(op==OP_Eq);
  4564   4687         VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ);
................................................................................
  4726   4849         if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
  4727   4850       }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
  4728   4851         return 2;
  4729   4852       }
  4730   4853     }
  4731   4854     if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
  4732   4855     if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
  4733         -    if( combinedFlags & EP_xIsSelect ) return 2;
         4856  +    if( pA->eX==EX_Select || pA->eX!=pB->eX ) return 2;
  4734   4857       if( (combinedFlags & EP_FixedCol)==0
  4735   4858        && sqlite3ExprCompare(pParse, pA->pLeft, pB->pLeft, iTab) ) return 2;
  4736         -    if( sqlite3ExprCompare(pParse, pA->pRight, pB->pRight, iTab) ) return 2;
  4737         -    if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
         4859  +    if( pA->eX==EX_Right ){
         4860  +      if( sqlite3ExprCompare(pParse,pA->x.pRight,pB->x.pRight,iTab) ) return 2;
         4861  +    }
         4862  +    if( pA->eX==EX_List ){
         4863  +      if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
         4864  +    }
  4738   4865       assert( (combinedFlags & EP_Reduced)==0 );
  4739   4866       if( pA->op!=TK_STRING && pA->op!=TK_TRUEFALSE ){
  4740   4867         if( pA->iColumn!=pB->iColumn ) return 2;
  4741   4868         if( pA->iTable!=pB->iTable 
  4742   4869          && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
  4743   4870       }
  4744   4871   #ifndef SQLITE_OMIT_WINDOWFUNC
................................................................................
  4825   4952   ** improvement.  Returning false might cause a performance reduction, but
  4826   4953   ** it will always give the correct answer and is hence always safe.
  4827   4954   */
  4828   4955   int sqlite3ExprImpliesExpr(Parse *pParse, Expr *pE1, Expr *pE2, int iTab){
  4829   4956     if( sqlite3ExprCompare(pParse, pE1, pE2, iTab)==0 ){
  4830   4957       return 1;
  4831   4958     }
         4959  +  assert( pE2->op!=TK_OR || pE2->eX==EX_Right );
  4832   4960     if( pE2->op==TK_OR
  4833   4961      && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
  4834         -             || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
         4962  +             || sqlite3ExprImpliesExpr(pParse, pE1, pE2->x.pRight, iTab) )
  4835   4963     ){
  4836   4964       return 1;
  4837   4965     }
  4838   4966     if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
  4839   4967       Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
  4840   4968       testcase( pX!=pE1->pLeft );
  4841   4969       if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
................................................................................
  4895   5023       case TK_GE:
  4896   5024         testcase( pExpr->op==TK_EQ );
  4897   5025         testcase( pExpr->op==TK_NE );
  4898   5026         testcase( pExpr->op==TK_LT );
  4899   5027         testcase( pExpr->op==TK_LE );
  4900   5028         testcase( pExpr->op==TK_GT );
  4901   5029         testcase( pExpr->op==TK_GE );
  4902         -      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->pTab))
  4903         -       || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->pTab))
         5030  +      assert( pExpr->eX==EX_Right );
         5031  +      if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->x.pTab))
         5032  +       || (pExpr->x.pRight->op==TK_COLUMN && IsVirtual(pExpr->x.pRight->x.pTab))
  4904   5033         ){
  4905   5034          return WRC_Prune;
  4906   5035         }
  4907   5036       default:
  4908   5037         return WRC_Continue;
  4909   5038     }
  4910   5039   }
................................................................................
  5127   5256                   break;
  5128   5257                 }
  5129   5258               }
  5130   5259               if( (k>=pAggInfo->nColumn)
  5131   5260                && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 
  5132   5261               ){
  5133   5262                 pCol = &pAggInfo->aCol[k];
  5134         -              pCol->pTab = pExpr->pTab;
         5263  +              assert( pExpr->eX==EX_Tab );
         5264  +              pCol->pTab = pExpr->x.pTab;
  5135   5265                 pCol->iTable = pExpr->iTable;
  5136   5266                 pCol->iColumn = pExpr->iColumn;
  5137   5267                 pCol->iMem = ++pParse->nMem;
  5138   5268                 pCol->iSorterColumn = -1;
  5139   5269                 pCol->pExpr = pExpr;
  5140   5270                 if( pAggInfo->pGroupBy ){
  5141   5271                   int j, n;
................................................................................
  5185   5315           }
  5186   5316           if( i>=pAggInfo->nFunc ){
  5187   5317             /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
  5188   5318             */
  5189   5319             u8 enc = ENC(pParse->db);
  5190   5320             i = addAggInfoFunc(pParse->db, pAggInfo);
  5191   5321             if( i>=0 ){
  5192         -            assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
         5322  +            assert( pExpr->eX==EX_List || pExpr->eX==EX_None );
  5193   5323               pItem = &pAggInfo->aFunc[i];
  5194   5324               pItem->pExpr = pExpr;
  5195   5325               pItem->iMem = ++pParse->nMem;
  5196   5326               assert( !ExprHasProperty(pExpr, EP_IntValue) );
  5197   5327               pItem->pFunc = sqlite3FindFunction(pParse->db,
  5198   5328                      pExpr->u.zToken, 
  5199         -                   pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
         5329  +                   pExpr->eX==EX_List ? pExpr->x.pList->nExpr : 0, enc, 0);
  5200   5330               if( pExpr->flags & EP_Distinct ){
  5201   5331                 pItem->iDistinct = pParse->nTab++;
  5202   5332               }else{
  5203   5333                 pItem->iDistinct = -1;
  5204   5334               }
  5205   5335             }
  5206   5336           }

Changes to src/fkey.c.

   498    498     sqlite3 *db,      /* The database connection */
   499    499     Table *pTab,      /* The table whose column is desired */
   500    500     int iCursor,      /* The open cursor on the table */
   501    501     i16 iCol          /* The column that is wanted */
   502    502   ){
   503    503     Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
   504    504     if( pExpr ){
   505         -    pExpr->pTab = pTab;
          505  +    assert( pExpr->eX==EX_None );
          506  +    if( pTab ){
          507  +      pExpr->x.pTab = pTab;
          508  +      pExpr->eX = EX_Tab;
          509  +    }
   506    510       pExpr->iTable = iCursor;
   507    511       pExpr->iColumn = iCol;
   508    512     }
   509    513     return pExpr;
   510    514   }
   511    515   
   512    516   /*

Changes to src/func.c.

  1844   1844   */
  1845   1845   int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
  1846   1846     FuncDef *pDef;
  1847   1847     int nExpr;
  1848   1848     if( pExpr->op!=TK_FUNCTION || !pExpr->x.pList ){
  1849   1849       return 0;
  1850   1850     }
  1851         -  assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
         1851  +  assert( pExpr->eX==EX_List );
  1852   1852     nExpr = pExpr->x.pList->nExpr;
  1853   1853     pDef = sqlite3FindFunction(db, pExpr->u.zToken, nExpr, SQLITE_UTF8, 0);
  1854   1854     if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
  1855   1855       return 0;
  1856   1856     }
  1857   1857     if( nExpr<3 ){
  1858   1858       aWc[3] = 0;

Changes to src/parse.y.

   932    932     */
   933    933     static Expr *tokenExpr(Parse *pParse, int op, Token t){
   934    934       Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
   935    935       if( p ){
   936    936         /* memset(p, 0, sizeof(Expr)); */
   937    937         p->op = (u8)op;
   938    938         p->affinity = 0;
          939  +      p->eX = EX_None;
   939    940         p->flags = EP_Leaf;
   940    941         p->iAgg = -1;
   941         -      p->pLeft = p->pRight = 0;
   942         -      p->x.pList = 0;
          942  +      p->pLeft = 0;
   943    943         p->pAggInfo = 0;
   944         -      p->pTab = 0;
   945    944         p->op2 = 0;
   946    945         p->iTable = 0;
   947    946         p->iColumn = 0;
   948    947   #ifndef SQLITE_OMIT_WINDOWFUNC
   949    948         p->pWin = 0;
   950    949   #endif
   951    950         p->u.zToken = (char*)&p[1];
................................................................................
  1048   1047   term(A) ::= CTIME_KW(OP). {
  1049   1048     A = sqlite3ExprFunction(pParse, 0, &OP, 0);
  1050   1049   }
  1051   1050   
  1052   1051   expr(A) ::= LP nexprlist(X) COMMA expr(Y) RP. {
  1053   1052     ExprList *pList = sqlite3ExprListAppend(pParse, X, Y);
  1054   1053     A = sqlite3PExpr(pParse, TK_VECTOR, 0, 0);
  1055         -  if( A ){
  1056         -    A->x.pList = pList;
  1057         -  }else{
  1058         -    sqlite3ExprListDelete(pParse->db, pList);
  1059         -  }
         1054  +  sqlite3PExprAddExprList(pParse, A, pList);
  1060   1055   }
  1061   1056   
  1062   1057   expr(A) ::= expr(A) AND(OP) expr(Y).    {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1063   1058   expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1064   1059   expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
  1065   1060                                           {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1066   1061   expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
................................................................................
  1100   1095   expr(A) ::= expr(A) NOT NULL.    {A = sqlite3PExpr(pParse,TK_NOTNULL,A,0);}
  1101   1096   
  1102   1097   %include {
  1103   1098     /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
  1104   1099     ** unary TK_ISNULL or TK_NOTNULL expression. */
  1105   1100     static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
  1106   1101       sqlite3 *db = pParse->db;
  1107         -    if( pA && pY && pY->op==TK_NULL && !IN_RENAME_OBJECT ){
         1102  +    if( pA && pY && pY->op==TK_NULL && pA->eX==EX_Right && !IN_RENAME_OBJECT ){
  1108   1103         pA->op = (u8)op;
  1109         -      sqlite3ExprDelete(db, pA->pRight);
  1110         -      pA->pRight = 0;
         1104  +      sqlite3ExprDelete(db, pA->x.pRight);
         1105  +      pA->x.pRight = 0;
         1106  +      pA->eX = EX_None;
  1111   1107       }
  1112   1108     }
  1113   1109   }
  1114   1110   
  1115   1111   //    expr1 IS expr2
  1116   1112   //    expr1 IS NOT expr2
  1117   1113   //
................................................................................
  1139   1135   %type between_op {int}
  1140   1136   between_op(A) ::= BETWEEN.     {A = 0;}
  1141   1137   between_op(A) ::= NOT BETWEEN. {A = 1;}
  1142   1138   expr(A) ::= expr(A) between_op(N) expr(X) AND expr(Y). [BETWEEN] {
  1143   1139     ExprList *pList = sqlite3ExprListAppend(pParse,0, X);
  1144   1140     pList = sqlite3ExprListAppend(pParse,pList, Y);
  1145   1141     A = sqlite3PExpr(pParse, TK_BETWEEN, A, 0);
  1146         -  if( A ){
  1147         -    A->x.pList = pList;
  1148         -  }else{
  1149         -    sqlite3ExprListDelete(pParse->db, pList);
  1150         -  } 
         1142  +  sqlite3PExprAddExprList(pParse, A, pList);
  1151   1143     if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
  1152   1144   }
  1153   1145   %ifndef SQLITE_OMIT_SUBQUERY
  1154   1146     %type in_op {int}
  1155   1147     in_op(A) ::= IN.      {A = 0;}
  1156   1148     in_op(A) ::= NOT IN.  {A = 1;}
  1157   1149     expr(A) ::= expr(A) in_op(N) LP exprlist(Y) RP. [IN] {
................................................................................
  1191   1183         if( ALWAYS(pRHS) ){
  1192   1184           pRHS->flags &= ~EP_Collate;
  1193   1185           pRHS->flags |= EP_Generic;
  1194   1186         }
  1195   1187         A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
  1196   1188       }else{
  1197   1189         A = sqlite3PExpr(pParse, TK_IN, A, 0);
  1198         -      if( A ){
  1199         -        A->x.pList = Y;
  1200         -        sqlite3ExprSetHeightAndFlags(pParse, A);
  1201         -      }else{
  1202         -        sqlite3ExprListDelete(pParse->db, Y);
  1203         -      }
         1190  +      sqlite3PExprAddExprList(pParse, A, Y);
         1191  +      sqlite3ExprSetHeightAndFlags(pParse, A);
  1204   1192         if( N ) A = sqlite3PExpr(pParse, TK_NOT, A, 0);
  1205   1193       }
  1206   1194     }
  1207   1195     expr(A) ::= LP select(X) RP. {
  1208   1196       A = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
  1209   1197       sqlite3PExprAddSelect(pParse, A, X);
  1210   1198     }
................................................................................
  1227   1215       sqlite3PExprAddSelect(pParse, p, Y);
  1228   1216     }
  1229   1217   %endif SQLITE_OMIT_SUBQUERY
  1230   1218   
  1231   1219   /* CASE expressions */
  1232   1220   expr(A) ::= CASE case_operand(X) case_exprlist(Y) case_else(Z) END. {
  1233   1221     A = sqlite3PExpr(pParse, TK_CASE, X, 0);
  1234         -  if( A ){
  1235         -    A->x.pList = Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y;
  1236         -    sqlite3ExprSetHeightAndFlags(pParse, A);
  1237         -  }else{
  1238         -    sqlite3ExprListDelete(pParse->db, Y);
  1239         -    sqlite3ExprDelete(pParse->db, Z);
  1240         -  }
         1222  +  sqlite3PExprAddExprList(pParse,A,Z ? sqlite3ExprListAppend(pParse,Y,Z) : Y);
         1223  +  sqlite3ExprSetHeightAndFlags(pParse, A);
  1241   1224   }
  1242   1225   %type case_exprlist {ExprList*}
  1243   1226   %destructor case_exprlist {sqlite3ExprListDelete(pParse->db, $$);}
  1244   1227   case_exprlist(A) ::= case_exprlist(A) WHEN expr(Y) THEN expr(Z). {
  1245   1228     A = sqlite3ExprListAppend(pParse,A, Y);
  1246   1229     A = sqlite3ExprListAppend(pParse,A, Z);
  1247   1230   }

Changes to src/resolve.c.

   160    160   **    pExpr->iTable        Set to the cursor number for the table obtained
   161    161   **                         from pSrcList.
   162    162   **    pExpr->pTab          Points to the Table structure of X.Y (even if
   163    163   **                         X and/or Y are implied.)
   164    164   **    pExpr->iColumn       Set to the column number within the table.
   165    165   **    pExpr->op            Set to TK_COLUMN.
   166    166   **    pExpr->pLeft         Any expression this points to is deleted
   167         -**    pExpr->pRight        Any expression this points to is deleted.
          167  +**    pExpr->x.pRight      Any expression this points to is deleted.
   168    168   **
   169    169   ** The zDb variable is the name of the database (the "X").  This value may be
   170    170   ** NULL meaning that name is of the form Y.Z or Z.  Any available database
   171    171   ** can be used.  The zTable variable is the name of the table (the "Y").  This
   172    172   ** value can be NULL if zDb is also NULL.  If zTable is NULL it
   173    173   ** means that the form of the name is Z and that columns from any table
   174    174   ** can be used.
................................................................................
   199    199   
   200    200     assert( pNC );     /* the name context cannot be NULL. */
   201    201     assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
   202    202     assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
   203    203   
   204    204     /* Initialize the node to no-match */
   205    205     pExpr->iTable = -1;
   206         -  pExpr->pTab = 0;
   207    206     ExprSetVVAProperty(pExpr, EP_NoReduce);
   208    207   
   209    208     /* Translate the schema name in zDb into a pointer to the corresponding
   210    209     ** schema.  If not found, pSchema will remain NULL and nothing will match
   211    210     ** resulting in an appropriate error message toward the end of this routine
   212    211     */
   213    212     if( zDb ){
................................................................................
   261    260           if( zTab ){
   262    261             const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
   263    262             assert( zTabName!=0 );
   264    263             if( sqlite3StrICmp(zTabName, zTab)!=0 ){
   265    264               continue;
   266    265             }
   267    266             if( IN_RENAME_OBJECT && pItem->zAlias ){
   268         -            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->pTab);
          267  +            sqlite3RenameTokenRemap(pParse, 0, (void*)&pExpr->x.pTab);
   269    268             }
   270    269           }
   271    270           if( 0==(cntTab++) ){
   272    271             pMatch = pItem;
   273    272           }
   274    273           for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
   275    274             if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
................................................................................
   287    286               pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
   288    287               break;
   289    288             }
   290    289           }
   291    290         }
   292    291         if( pMatch ){
   293    292           pExpr->iTable = pMatch->iCursor;
   294         -        pExpr->pTab = pMatch->pTab;
          293  +        sqlite3ExprAddTab(db, pExpr, pMatch->pTab);
   295    294           /* RIGHT JOIN not (yet) supported */
   296    295           assert( (pMatch->fg.jointype & JT_RIGHT)==0 );
   297    296           if( (pMatch->fg.jointype & JT_LEFT)!=0 ){
   298    297             ExprSetProperty(pExpr, EP_CanBeNull);
   299    298           }
   300         -        pSchema = pExpr->pTab->pSchema;
          299  +        pSchema = pExpr->x.pTab->pSchema;
   301    300         }
   302    301       } /* if( pSrcList ) */
   303    302   
   304    303   #if !defined(SQLITE_OMIT_TRIGGER) || !defined(SQLITE_OMIT_UPSERT)
   305    304       /* If we have not already resolved the name, then maybe 
   306    305       ** it is a new.* or old.* trigger argument reference.  Or
   307    306       ** maybe it is an excluded.* from an upsert.
................................................................................
   350    349           if( iCol<pTab->nCol ){
   351    350             cnt++;
   352    351   #ifndef SQLITE_OMIT_UPSERT
   353    352             if( pExpr->iTable==2 ){
   354    353               testcase( iCol==(-1) );
   355    354               if( IN_RENAME_OBJECT ){
   356    355                 pExpr->iColumn = iCol;
   357         -              pExpr->pTab = pTab;
          356  +              sqlite3ExprAddTab(db, pExpr, pTab);
   358    357                 eNewExprOp = TK_COLUMN;
   359    358               }else{
   360    359                 pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
   361    360                 eNewExprOp = TK_REGISTER;
   362    361                 ExprSetProperty(pExpr, EP_Alias);
   363    362               }
   364    363             }else
................................................................................
   372    371                 testcase( iCol==32 );
   373    372                 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
   374    373               }else{
   375    374                 testcase( iCol==31 );
   376    375                 testcase( iCol==32 );
   377    376                 pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
   378    377               }
   379         -            pExpr->pTab = pTab;
          378  +            sqlite3ExprAddTab(db, pExpr, pTab);
   380    379               pExpr->iColumn = (i16)iCol;
   381    380               eNewExprOp = TK_TRIGGER;
   382    381   #endif /* SQLITE_OMIT_TRIGGER */
   383    382             }
   384    383           }
   385    384         }
   386    385       }
................................................................................
   425    424       ){
   426    425         pEList = pNC->uNC.pEList;
   427    426         assert( pEList!=0 );
   428    427         for(j=0; j<pEList->nExpr; j++){
   429    428           char *zAs = pEList->a[j].zName;
   430    429           if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
   431    430             Expr *pOrig;
   432         -          assert( pExpr->pLeft==0 && pExpr->pRight==0 );
   433         -          assert( pExpr->x.pList==0 );
   434         -          assert( pExpr->x.pSelect==0 );
          431  +          assert( pExpr->pLeft==0 );
   435    432             pOrig = pEList->a[j].pExpr;
   436    433             if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
   437    434               sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
   438    435               return WRC_Abort;
   439    436             }
   440    437             if( sqlite3ExprVectorSize(pOrig)!=1 ){
   441    438               sqlite3ErrorMsg(pParse, "row value misused");
................................................................................
   472    469     ** Because no reference was made to outer contexts, the pNC->nRef
   473    470     ** fields are not changed in any context.
   474    471     */
   475    472     if( cnt==0 && zTab==0 ){
   476    473       assert( pExpr->op==TK_ID );
   477    474       if( ExprHasProperty(pExpr,EP_DblQuoted) ){
   478    475         pExpr->op = TK_STRING;
   479         -      pExpr->pTab = 0;
   480    476         return WRC_Prune;
   481    477       }
   482    478       if( sqlite3ExprIdToTrueFalse(pExpr) ){
   483    479         return WRC_Prune;
   484    480       }
   485    481     }
   486    482   
................................................................................
   518    514       pMatch->colUsed |= ((Bitmask)1)<<n;
   519    515     }
   520    516   
   521    517     /* Clean up and return
   522    518     */
   523    519     sqlite3ExprDelete(db, pExpr->pLeft);
   524    520     pExpr->pLeft = 0;
   525         -  sqlite3ExprDelete(db, pExpr->pRight);
   526         -  pExpr->pRight = 0;
          521  +  if( pExpr->eX==EX_Right ){
          522  +    sqlite3ExprDelete(db, pExpr->x.pRight);
          523  +    pExpr->x.pRight = 0;
          524  +    pExpr->eX = EX_None;
          525  +  }
   527    526     pExpr->op = eNewExprOp;
   528    527     ExprSetProperty(pExpr, EP_Leaf);
   529    528   lookupname_end:
   530    529     if( cnt==1 ){
   531    530       assert( pNC!=0 );
   532    531       if( !ExprHasProperty(pExpr, EP_Alias) ){
   533    532         sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
................................................................................
   550    549   ** Allocate and return a pointer to an expression to load the column iCol
   551    550   ** from datasource iSrc in SrcList pSrc.
   552    551   */
   553    552   Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
   554    553     Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
   555    554     if( p ){
   556    555       struct SrcList_item *pItem = &pSrc->a[iSrc];
   557         -    p->pTab = pItem->pTab;
          556  +    sqlite3ExprAddTab(db, p, pItem->pTab);
   558    557       p->iTable = pItem->iCursor;
   559         -    if( p->pTab->iPKey==iCol ){
          558  +    if( p->x.pTab->iPKey==iCol ){
   560    559         p->iColumn = -1;
   561    560       }else{
   562    561         p->iColumn = (ynVar)iCol;
   563    562         testcase( iCol==BMS );
   564    563         testcase( iCol==BMS-1 );
   565    564         pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
   566    565       }
................................................................................
   673    672         if( pExpr->op==TK_ID ){
   674    673           zDb = 0;
   675    674           zTable = 0;
   676    675           zColumn = pExpr->u.zToken;
   677    676         }else{
   678    677           Expr *pLeft = pExpr->pLeft;
   679    678           notValid(pParse, pNC, "the \".\" operator", NC_IdxExpr);
   680         -        pRight = pExpr->pRight;
          679  +        assert( pExpr->eX==EX_Right );
          680  +        pRight = pExpr->x.pRight;
   681    681           if( pRight->op==TK_ID ){
   682    682             zDb = 0;
   683    683           }else{
   684    684             assert( pRight->op==TK_DOT );
          685  +          assert( pRight->eX==EX_Right );
   685    686             zDb = pLeft->u.zToken;
   686    687             pLeft = pRight->pLeft;
   687         -          pRight = pRight->pRight;
          688  +          pRight = pRight->x.pRight;
   688    689           }
   689    690           zTable = pLeft->u.zToken;
   690    691           zColumn = pRight->u.zToken;
   691    692           if( IN_RENAME_OBJECT ){
   692    693             sqlite3RenameTokenRemap(pParse, (void*)pExpr, (void*)pRight);
   693         -        }
   694         -        if( IN_RENAME_OBJECT ){
   695         -          sqlite3RenameTokenRemap(pParse, (void*)&pExpr->pTab, (void*)pLeft);
          694  +          if( pExpr->eX==EX_Tab ){
          695  +            sqlite3RenameTokenRemap(pParse, (void*)&pExpr->x.pTab,(void*)pLeft);
          696  +          }
   696    697           }
   697    698         }
   698    699         return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
   699    700       }
   700    701   
   701    702       /* Resolve function names
   702    703       */
   703    704       case TK_FUNCTION: {
   704         -      ExprList *pList = pExpr->x.pList;    /* The argument list */
   705         -      int n = pList ? pList->nExpr : 0;    /* Number of arguments */
          705  +      ExprList *pList;            /* The argument list */
          706  +      int n;                      /* Number of arguments */
   706    707         int no_such_func = 0;       /* True if no such function exists */
   707    708         int wrong_num_args = 0;     /* True if wrong number of arguments */
   708    709         int is_agg = 0;             /* True if is an aggregate function */
   709    710         int nId;                    /* Number of characters in function name */
   710    711         const char *zId;            /* The function name. */
   711    712         FuncDef *pDef;              /* Information about the function */
   712    713         u8 enc = ENC(pParse->db);   /* The database encoding */
   713    714   
   714         -      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
          715  +      assert( pExpr->eX==EX_List || pExpr->eX==EX_None );
          716  +      if( pExpr->eX==EX_List ){
          717  +        pList = pExpr->x.pList;
          718  +        n = pList->nExpr;
          719  +      }else{
          720  +        pList = 0;
          721  +        n = 0;
          722  +      }
   715    723         zId = pExpr->u.zToken;
   716    724         nId = sqlite3Strlen30(zId);
   717    725         pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
   718    726         if( pDef==0 ){
   719    727           pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
   720    728           if( pDef==0 ){
   721    729             no_such_func = 1;
................................................................................
   872    880       }
   873    881   #ifndef SQLITE_OMIT_SUBQUERY
   874    882       case TK_SELECT:
   875    883       case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
   876    884   #endif
   877    885       case TK_IN: {
   878    886         testcase( pExpr->op==TK_IN );
   879         -      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
          887  +      if( pExpr->eX==EX_Select ){
   880    888           int nRef = pNC->nRef;
   881    889           notValid(pParse, pNC, "subqueries", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
   882    890           sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
   883    891           assert( pNC->nRef>=nRef );
   884    892           if( nRef!=pNC->nRef ){
   885    893             ExprSetProperty(pExpr, EP_VarSelect);
   886    894             pNC->ncFlags |= NC_VarSelect;
................................................................................
   894    902       }
   895    903       case TK_IS:
   896    904       case TK_ISNOT: {
   897    905         Expr *pRight;
   898    906         assert( !ExprHasProperty(pExpr, EP_Reduced) );
   899    907         /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
   900    908         ** and "x IS NOT FALSE". */
   901         -      if( (pRight = pExpr->pRight)->op==TK_ID ){
          909  +      assert( pExpr->eX==EX_Right );
          910  +      if( (pRight = pExpr->x.pRight)->op==TK_ID ){
   902    911           int rc = resolveExprStep(pWalker, pRight);
   903    912           if( rc==WRC_Abort ) return WRC_Abort;
   904    913           if( pRight->op==TK_TRUEFALSE ){
   905    914             pExpr->op2 = pExpr->op;
   906    915             pExpr->op = TK_TRUTH;
   907    916             return WRC_Continue;
   908    917           }
................................................................................
   922    931         nLeft = sqlite3ExprVectorSize(pExpr->pLeft);
   923    932         if( pExpr->op==TK_BETWEEN ){
   924    933           nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[0].pExpr);
   925    934           if( nRight==nLeft ){
   926    935             nRight = sqlite3ExprVectorSize(pExpr->x.pList->a[1].pExpr);
   927    936           }
   928    937         }else{
   929         -        assert( pExpr->pRight!=0 );
   930         -        nRight = sqlite3ExprVectorSize(pExpr->pRight);
          938  +        assert( pExpr->eX==EX_Right );
          939  +        nRight = sqlite3ExprVectorSize(pExpr->x.pRight);
   931    940         }
   932    941         if( nLeft!=nRight ){
   933    942           testcase( pExpr->op==TK_EQ );
   934    943           testcase( pExpr->op==TK_NE );
   935    944           testcase( pExpr->op==TK_LT );
   936    945           testcase( pExpr->op==TK_LE );
   937    946           testcase( pExpr->op==TK_GT );

Changes to src/select.c.

   386    386   */
   387    387   static void setJoinExpr(Expr *p, int iTable){
   388    388     while( p ){
   389    389       ExprSetProperty(p, EP_FromJoin);
   390    390       assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
   391    391       ExprSetVVAProperty(p, EP_NoReduce);
   392    392       p->iRightJoinTable = (i16)iTable;
   393         -    if( p->op==TK_FUNCTION && p->x.pList ){
          393  +    if( p->op==TK_FUNCTION && p->eX==EX_List ){
   394    394         int i;
   395    395         for(i=0; i<p->x.pList->nExpr; i++){
   396    396           setJoinExpr(p->x.pList->a[i].pExpr, iTable);
   397    397         }
   398    398       }
   399    399       setJoinExpr(p->pLeft, iTable);
   400         -    p = p->pRight;
          400  +    p = p->eX==EX_Right ? p->x.pRight : 0;
   401    401     } 
   402    402   }
   403    403   
   404    404   /* Undo the work of setJoinExpr().  In the expression tree p, convert every
   405    405   ** term that is marked with EP_FromJoin and iRightJoinTable==iTable into
   406    406   ** an ordinary term that omits the EP_FromJoin mark.
   407    407   **
................................................................................
   409    409   */
   410    410   static void unsetJoinExpr(Expr *p, int iTable){
   411    411     while( p ){
   412    412       if( ExprHasProperty(p, EP_FromJoin)
   413    413        && (iTable<0 || p->iRightJoinTable==iTable) ){
   414    414         ExprClearProperty(p, EP_FromJoin);
   415    415       }
   416         -    if( p->op==TK_FUNCTION && p->x.pList ){
          416  +    if( p->op==TK_FUNCTION && p->eX==EX_List ){
   417    417         int i;
   418    418         for(i=0; i<p->x.pList->nExpr; i++){
   419    419           unsetJoinExpr(p->x.pList->a[i].pExpr, iTable);
   420    420         }
   421    421       }
   422    422       unsetJoinExpr(p->pLeft, iTable);
   423         -    p = p->pRight;
          423  +    p = p->eX==EX_Right ? p->x.pRight : 0;
   424    424     } 
   425    425   }
   426    426   
   427    427   /*
   428    428   ** This routine processes the join information for a SELECT statement.
   429    429   ** ON and USING clauses are converted into extra terms of the WHERE clause.
   430    430   ** NATURAL joins also create extra WHERE clause terms.
................................................................................
   799    799     int i;
   800    800     int nDefer = 0;
   801    801     ExprList *pExtra = 0;
   802    802     for(i=0; i<pEList->nExpr; i++){
   803    803       struct ExprList_item *pItem = &pEList->a[i];
   804    804       if( pItem->u.x.iOrderByCol==0 ){
   805    805         Expr *pExpr = pItem->pExpr;
   806         -      Table *pTab = pExpr->pTab;
   807         -      if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
          806  +      Table *pTab;
          807  +      if( pExpr->op==TK_COLUMN
          808  +       && pExpr->iColumn>=0
          809  +       && ALWAYS(pExpr->eX==EX_Tab)
          810  +       && (pTab = pExpr->x.pTab)!=0
          811  +       && !IsVirtual(pTab)
   808    812          && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
   809    813         ){
   810    814           int j;
   811    815           for(j=0; j<nDefer; j++){
   812    816             if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
   813    817           }
   814    818           if( j==nDefer ){
................................................................................
   818    822               int nKey = 1;
   819    823               int k;
   820    824               Index *pPk = 0;
   821    825               if( !HasRowid(pTab) ){
   822    826                 pPk = sqlite3PrimaryKeyIndex(pTab);
   823    827                 nKey = pPk->nKeyCol;
   824    828               }
          829  +            assert( pExpr->eX==EX_Tab );
   825    830               for(k=0; k<nKey; k++){
   826    831                 Expr *pNew = sqlite3PExpr(pParse, TK_COLUMN, 0, 0);
   827    832                 if( pNew ){
   828    833                   pNew->iTable = pExpr->iTable;
   829         -                pNew->pTab = pExpr->pTab;
          834  +                pNew->eX = EX_Tab;
          835  +                pNew->x.pTab = pExpr->x.pTab;
   830    836                   pNew->iColumn = pPk ? pPk->aiColumn[k] : -1;
   831    837                   pExtra = sqlite3ExprListAppend(pParse, pExtra, pNew);
   832    838                 }
   833    839               }
   834         -            pSort->aDefer[nDefer].pTab = pExpr->pTab;
          840  +            pSort->aDefer[nDefer].pTab = pExpr->x.pTab;
   835    841               pSort->aDefer[nDefer].iCsr = pExpr->iTable;
   836    842               pSort->aDefer[nDefer].nKey = nKey;
   837    843               nDefer++;
   838    844             }
   839    845           }
   840    846           pItem->bSorterRef = 1;
   841    847         }
................................................................................
  1676   1682           ** This is not a problem, as the column type of "t1.col" is never
  1677   1683           ** used. When columnType() is called on the expression 
  1678   1684           ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
  1679   1685           ** branch below.  */
  1680   1686           break;
  1681   1687         }
  1682   1688   
  1683         -      assert( pTab && pExpr->pTab==pTab );
         1689  +      assert( pTab && pExpr->eX==EX_Tab && pExpr->x.pTab==pTab );
  1684   1690         if( pS ){
  1685   1691           /* The "table" is actually a sub-select or a view in the FROM clause
  1686   1692           ** of the SELECT statement. Return the declaration type and origin
  1687   1693           ** data for the result-set column of the sub-select.
  1688   1694           */
  1689   1695           if( iCol>=0 && iCol<pS->pEList->nExpr ){
  1690   1696             /* If iCol is less than zero, then the expression requests the
................................................................................
  1732   1738         /* The expression is a sub-select. Return the declaration type and
  1733   1739         ** origin info for the single column in the result set of the SELECT
  1734   1740         ** statement.
  1735   1741         */
  1736   1742         NameContext sNC;
  1737   1743         Select *pS = pExpr->x.pSelect;
  1738   1744         Expr *p = pS->pEList->a[0].pExpr;
  1739         -      assert( ExprHasProperty(pExpr, EP_xIsSelect) );
         1745  +      assert( pExpr->eX==EX_Select );
  1740   1746         sNC.pSrcList = pS->pSrc;
  1741   1747         sNC.pNext = pNC;
  1742   1748         sNC.pParse = pNC->pParse;
  1743   1749         zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); 
  1744   1750         break;
  1745   1751       }
  1746   1752   #endif
................................................................................
  1861   1867     srcName = (db->flags & SQLITE_ShortColNames)!=0 || fullName;
  1862   1868     sqlite3VdbeSetNumCols(v, pEList->nExpr);
  1863   1869     for(i=0; i<pEList->nExpr; i++){
  1864   1870       Expr *p = pEList->a[i].pExpr;
  1865   1871   
  1866   1872       assert( p!=0 );
  1867   1873       assert( p->op!=TK_AGG_COLUMN );  /* Agg processing has not run yet */
  1868         -    assert( p->op!=TK_COLUMN || p->pTab!=0 ); /* Covering idx not yet coded */
         1874  +    /* Covering idx not yet coded: */
         1875  +    assert( p->op!=TK_COLUMN || (p->eX==EX_Tab && p->x.pTab!=0) );
  1869   1876       if( pEList->a[i].zName ){
  1870   1877         /* An AS clause always takes first priority */
  1871   1878         char *zName = pEList->a[i].zName;
  1872   1879         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
  1873   1880       }else if( srcName && p->op==TK_COLUMN ){
  1874   1881         char *zCol;
  1875   1882         int iCol = p->iColumn;
  1876         -      pTab = p->pTab;
         1883  +      assert( p->eX==EX_Tab );
         1884  +      pTab = p->x.pTab;
  1877   1885         assert( pTab!=0 );
  1878   1886         if( iCol<0 ) iCol = pTab->iPKey;
  1879   1887         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
  1880   1888         if( iCol<0 ){
  1881   1889           zCol = "rowid";
  1882   1890         }else{
  1883   1891           zCol = pTab->aCol[iCol].zName;
................................................................................
  1953   1961       /* Get an appropriate name for the column
  1954   1962       */
  1955   1963       if( (zName = pEList->a[i].zName)!=0 ){
  1956   1964         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1957   1965       }else{
  1958   1966         Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
  1959   1967         while( pColExpr->op==TK_DOT ){
  1960         -        pColExpr = pColExpr->pRight;
         1968  +        assert( pColExpr->eX==EX_Right );
         1969  +        pColExpr = pColExpr->x.pRight;
  1961   1970           assert( pColExpr!=0 );
  1962   1971         }
  1963   1972         assert( pColExpr->op!=TK_AGG_COLUMN );
  1964   1973         if( pColExpr->op==TK_COLUMN ){
  1965   1974           /* For columns use the column name name */
  1966   1975           int iCol = pColExpr->iColumn;
  1967         -        Table *pTab = pColExpr->pTab;
         1976  +        Table *pTab;
         1977  +        assert( pColExpr->eX==EX_Tab );
         1978  +        pTab = pColExpr->x.pTab;
  1968   1979           assert( pTab!=0 );
  1969   1980           if( iCol<0 ) iCol = pTab->iPKey;
  1970   1981           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
  1971   1982         }else if( pColExpr->op==TK_ID ){
  1972   1983           assert( !ExprHasProperty(pColExpr, EP_IntValue) );
  1973   1984           zName = pColExpr->u.zToken;
  1974   1985         }else{
................................................................................
  2121   2132     }
  2122   2133     return sqlite3VdbeCreate(pParse);
  2123   2134   }
  2124   2135   
  2125   2136   
  2126   2137   /*
  2127   2138   ** Compute the iLimit and iOffset fields of the SELECT based on the
  2128         -** pLimit expressions.  pLimit->pLeft and pLimit->pRight hold the expressions
         2139  +** pLimit expressions.  pLimit->pLeft and pLimit->x,pRight hold the expressions
  2129   2140   ** that appear in the original SQL statement after the LIMIT and OFFSET
  2130   2141   ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
  2131   2142   ** are the integer memory register numbers for counters used to compute 
  2132   2143   ** the limit and offset.  If there is no limit and/or offset, then 
  2133   2144   ** iLimit and iOffset are negative.
  2134   2145   **
  2135   2146   ** This routine changes the values of iLimit and iOffset only if
  2136         -** a limit or offset is defined by pLimit->pLeft and pLimit->pRight.  iLimit
         2147  +** a limit or offset is defined by pLimit->pLeft and pLimit->x.pRight.  iLimit
  2137   2148   ** and iOffset should have been preset to appropriate default values (zero)
  2138   2149   ** prior to calling this routine.
  2139   2150   **
  2140   2151   ** The iOffset register (if it exists) is initialized to the value
  2141   2152   ** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
  2142   2153   ** iOffset+1 is initialized to LIMIT+OFFSET.
  2143   2154   **
................................................................................
  2178   2189         }
  2179   2190       }else{
  2180   2191         sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
  2181   2192         sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
  2182   2193         VdbeComment((v, "LIMIT counter"));
  2183   2194         sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
  2184   2195       }
  2185         -    if( pLimit->pRight ){
         2196  +    if( pLimit->eX==EX_Right ){
  2186   2197         p->iOffset = iOffset = ++pParse->nMem;
  2187   2198         pParse->nMem++;   /* Allocate an extra register for limit+offset */
  2188         -      sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
         2199  +      sqlite3ExprCode(pParse, pLimit->x.pRight, iOffset);
  2189   2200         sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
  2190   2201         VdbeComment((v, "OFFSET counter"));
  2191   2202         sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
  2192   2203         VdbeComment((v, "LIMIT+OFFSET"));
  2193   2204       }
  2194   2205     }
  2195   2206   }
................................................................................
  3438   3449       if( pExpr->iColumn<0 ){
  3439   3450         pExpr->op = TK_NULL;
  3440   3451       }else{
  3441   3452         Expr *pNew;
  3442   3453         Expr *pCopy = pSubst->pEList->a[pExpr->iColumn].pExpr;
  3443   3454         Expr ifNullRow;
  3444   3455         assert( pSubst->pEList!=0 && pExpr->iColumn<pSubst->pEList->nExpr );
  3445         -      assert( pExpr->pRight==0 );
         3456  +      assert( pExpr->eX!=EX_Right );
  3446   3457         if( sqlite3ExprIsVector(pCopy) ){
  3447   3458           sqlite3VectorErrorMsg(pSubst->pParse, pCopy);
  3448   3459         }else{
  3449   3460           sqlite3 *db = pSubst->pParse->db;
  3450   3461           if( pSubst->isLeftJoin && pCopy->op!=TK_COLUMN ){
  3451   3462             memset(&ifNullRow, 0, sizeof(ifNullRow));
  3452   3463             ifNullRow.op = TK_IF_NULL_ROW;
................................................................................
  3467   3478         }
  3468   3479       }
  3469   3480     }else{
  3470   3481       if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
  3471   3482         pExpr->iTable = pSubst->iNewTable;
  3472   3483       }
  3473   3484       pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
  3474         -    pExpr->pRight = substExpr(pSubst, pExpr->pRight);
  3475         -    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3476         -      substSelect(pSubst, pExpr->x.pSelect, 1);
  3477         -    }else{
  3478         -      substExprList(pSubst, pExpr->x.pList);
         3485  +    switch( pExpr->eX ){
         3486  +      case EX_Select: {
         3487  +        substSelect(pSubst, pExpr->x.pSelect, 1);
         3488  +        break;
         3489  +      }
         3490  +      case EX_List: {
         3491  +        substExprList(pSubst, pExpr->x.pList);
         3492  +        break;
         3493  +      }
         3494  +      case EX_Right: {
         3495  +        pExpr->x.pRight = substExpr(pSubst, pExpr->x.pRight);
         3496  +        if( pExpr->x.pRight==0 ) pExpr->eX = EX_None;
         3497  +        break;
         3498  +      }
  3479   3499       }
  3480   3500     }
  3481   3501     return pExpr;
  3482   3502   }
  3483   3503   static void substExprList(
  3484   3504     SubstContext *pSubst, /* Description of the substitution */
  3485   3505     ExprList *pList       /* List to scan and in which to make substitutes */
................................................................................
  3711   3731     assert( pSubSrc );
  3712   3732     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
  3713   3733     ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
  3714   3734     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
  3715   3735     ** became arbitrary expressions, we were forced to add restrictions (13)
  3716   3736     ** and (14). */
  3717   3737     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
  3718         -  if( pSub->pLimit && pSub->pLimit->pRight ) return 0;   /* Restriction (14) */
         3738  +  if( pSub->pLimit && pSub->pLimit->eX==EX_Right ){
         3739  +    return 0;                                            /* Restriction (14) */
         3740  +  }
  3719   3741     if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
  3720   3742       return 0;                                            /* Restriction (15) */
  3721   3743     }
  3722   3744     if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
  3723   3745     if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (4)  */
  3724   3746     if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
  3725   3747        return 0;         /* Restrictions (8)(9) */
................................................................................
  4106   4128   ** found, add it to the pConst structure.
  4107   4129   */
  4108   4130   static void findConstInWhere(WhereConst *pConst, Expr *pExpr){
  4109   4131     Expr *pRight, *pLeft;
  4110   4132     if( pExpr==0 ) return;
  4111   4133     if( ExprHasProperty(pExpr, EP_FromJoin) ) return;
  4112   4134     if( pExpr->op==TK_AND ){
  4113         -    findConstInWhere(pConst, pExpr->pRight);
         4135  +    assert( pExpr->eX==EX_Right );
         4136  +    findConstInWhere(pConst, pExpr->x.pRight);
  4114   4137       findConstInWhere(pConst, pExpr->pLeft);
  4115   4138       return;
  4116   4139     }
  4117   4140     if( pExpr->op!=TK_EQ ) return;
  4118         -  pRight = pExpr->pRight;
         4141  +  assert( pExpr->eX==EX_Right );
         4142  +  pRight = pExpr->x.pRight;
  4119   4143     pLeft = pExpr->pLeft;
  4120   4144     assert( pRight!=0 );
  4121   4145     assert( pLeft!=0 );
  4122   4146     if( pRight->op==TK_COLUMN
  4123   4147      && !ExprHasProperty(pRight, EP_FixedCol)
  4124   4148      && sqlite3ExprIsConstant(pLeft)
  4125         -   && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
         4149  +   && sqlite3IsBinary(sqlite3ComparisonCollSeq(pConst->pParse,pLeft,pRight))
  4126   4150     ){
  4127   4151       constInsert(pConst, pRight, pLeft);
  4128   4152     }else
  4129   4153     if( pLeft->op==TK_COLUMN
  4130   4154      && !ExprHasProperty(pLeft, EP_FixedCol)
  4131   4155      && sqlite3ExprIsConstant(pRight)
  4132         -   && sqlite3IsBinary(sqlite3BinaryCompareCollSeq(pConst->pParse,pLeft,pRight))
         4156  +   && sqlite3IsBinary(sqlite3ComparisonCollSeq(pConst->pParse,pLeft,pRight))
  4133   4157     ){
  4134   4158       constInsert(pConst, pLeft, pRight);
  4135   4159     }
  4136   4160   }
  4137   4161   
  4138   4162   /*
  4139   4163   ** This is a Walker expression callback.  pExpr is a candidate expression
................................................................................
  4312   4336     }
  4313   4337   #endif
  4314   4338   
  4315   4339     if( pSubq->pLimit!=0 ){
  4316   4340       return 0; /* restriction (3) */
  4317   4341     }
  4318   4342     while( pWhere->op==TK_AND ){
  4319         -    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->pRight,
         4343  +    assert( pWhere->eX==EX_Right );
         4344  +    nChng += pushDownWhereTerms(pParse, pSubq, pWhere->x.pRight,
  4320   4345                                   iCursor, isLeftJoin);
  4321   4346       pWhere = pWhere->pLeft;
  4322   4347     }
  4323   4348     if( isLeftJoin
  4324   4349      && (ExprHasProperty(pWhere,EP_FromJoin)==0
  4325   4350            || pWhere->iRightJoinTable!=iCursor)
  4326   4351     ){
................................................................................
  4898   4923     **
  4899   4924     ** The first loop just checks to see if there are any "*" operators
  4900   4925     ** that need expanding.
  4901   4926     */
  4902   4927     for(k=0; k<pEList->nExpr; k++){
  4903   4928       pE = pEList->a[k].pExpr;
  4904   4929       if( pE->op==TK_ASTERISK ) break;
  4905         -    assert( pE->op!=TK_DOT || pE->pRight!=0 );
         4930  +    assert( pE->op!=TK_DOT || pE->eX==EX_Right );
  4906   4931       assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
  4907         -    if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
         4932  +    if( pE->op==TK_DOT && pE->x.pRight->op==TK_ASTERISK ) break;
  4908   4933       elistFlags |= pE->flags;
  4909   4934     }
  4910   4935     if( k<pEList->nExpr ){
  4911   4936       /*
  4912   4937       ** If we get here it means the result set contains one or more "*"
  4913   4938       ** operators that need to be expanded.  Loop through each expression
  4914   4939       ** in the result set and expand them one by one.
................................................................................
  4918   4943       int flags = pParse->db->flags;
  4919   4944       int longNames = (flags & SQLITE_FullColNames)!=0
  4920   4945                         && (flags & SQLITE_ShortColNames)==0;
  4921   4946   
  4922   4947       for(k=0; k<pEList->nExpr; k++){
  4923   4948         pE = a[k].pExpr;
  4924   4949         elistFlags |= pE->flags;
  4925         -      pRight = pE->pRight;
         4950  +      pRight = pE->eX==EX_Right ? pE->x.pRight : 0;
  4926   4951         assert( pE->op!=TK_DOT || pRight!=0 );
  4927   4952         if( pE->op!=TK_ASTERISK
  4928   4953          && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
  4929   4954         ){
  4930   4955           /* This particular expression does not need to be expanded.
  4931   4956           */
  4932   4957           pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
................................................................................
  5238   5263            && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
  5239   5264     }
  5240   5265   #endif
  5241   5266     sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
  5242   5267     for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
  5243   5268       if( pFunc->iDistinct>=0 ){
  5244   5269         Expr *pE = pFunc->pExpr;
  5245         -      assert( !ExprHasProperty(pE, EP_xIsSelect) );
  5246         -      if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
         5270  +      assert( pE->eX==EX_List || pE->eX==EX_None );
         5271  +      if( pE->eX==EX_None || pE->x.pList->nExpr!=1 ){
  5247   5272           sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
  5248   5273              "argument");
  5249   5274           pFunc->iDistinct = -1;
  5250   5275         }else{
  5251   5276           KeyInfo *pKeyInfo = sqlite3KeyInfoFromExprList(pParse, pE->x.pList,0,0);
  5252   5277           sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
  5253   5278                             (char*)pKeyInfo, P4_KEYINFO);
................................................................................
  5262   5287   */
  5263   5288   static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
  5264   5289     Vdbe *v = pParse->pVdbe;
  5265   5290     int i;
  5266   5291     struct AggInfo_func *pF;
  5267   5292     for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
  5268   5293       ExprList *pList = pF->pExpr->x.pList;
  5269         -    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
         5294  +    assert( pF->pExpr->eX==EX_List || pF->pExpr->eX==EX_None );
         5295  +    assert( pF->pExpr->eX==EX_List || pList==0 );
  5270   5296       sqlite3VdbeAddOp2(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0);
  5271   5297       sqlite3VdbeAppendP4(v, pF->pFunc, P4_FUNCDEF);
  5272   5298     }
  5273   5299   }
  5274   5300   
  5275   5301   
  5276   5302   /*
................................................................................
  5292   5318   
  5293   5319     pAggInfo->directMode = 1;
  5294   5320     for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
  5295   5321       int nArg;
  5296   5322       int addrNext = 0;
  5297   5323       int regAgg;
  5298   5324       ExprList *pList = pF->pExpr->x.pList;
  5299         -    assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
         5325  +    assert( pF->pExpr->eX==EX_List || pF->pExpr->eX==EX_None );
  5300   5326       if( pList ){
  5301   5327         nArg = pList->nExpr;
  5302   5328         regAgg = sqlite3GetTempRange(pParse, nArg);
  5303   5329         sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
  5304   5330       }else{
  5305   5331         nArg = 0;
  5306   5332         regAgg = 0;
................................................................................
  6182   6208       sAggInfo.nAccumulator = sAggInfo.nColumn;
  6183   6209       if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
  6184   6210         minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
  6185   6211       }else{
  6186   6212         minMaxFlag = WHERE_ORDERBY_NORMAL;
  6187   6213       }
  6188   6214       for(i=0; i<sAggInfo.nFunc; i++){
  6189         -      assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
  6190         -      sNC.ncFlags |= NC_InAggFunc;
  6191         -      sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
  6192         -      sNC.ncFlags &= ~NC_InAggFunc;
         6215  +      if( sAggInfo.aFunc[i].pExpr->eX==EX_List ){
         6216  +        sNC.ncFlags |= NC_InAggFunc;
         6217  +        sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
         6218  +        sNC.ncFlags &= ~NC_InAggFunc;
         6219  +      }
  6193   6220       }
  6194   6221       sAggInfo.mxReg = pParse->nMem;
  6195   6222       if( db->mallocFailed ) goto select_end;
  6196   6223   #if SELECTTRACE_ENABLED
  6197   6224       if( sqlite3SelectTrace & 0x400 ){
  6198   6225         int ii;
  6199   6226         SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));

Changes to src/sqliteInt.h.

  2415   2415   ** are contained within the same memory allocation.  Note, however, that
  2416   2416   ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  2417   2417   ** allocated, regardless of whether or not EP_Reduced is set.
  2418   2418   */
  2419   2419   struct Expr {
  2420   2420     u8 op;                 /* Operation performed by this node */
  2421   2421     char affinity;         /* The affinity of the column or 0 if not a column */
         2422  +  u8 eV;                 /* Which element of v-union is used */
         2423  +  u8 eX;                 /* Which element of x-union is used */
  2422   2424     u32 flags;             /* Various flags.  EP_* See below */
  2423   2425     union {
  2424   2426       char *zToken;          /* Token value. Zero terminated and dequoted */
  2425   2427       int iValue;            /* Non-negative integer value if EP_IntValue */
  2426   2428     } u;
  2427   2429   
  2428   2430     /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
  2429   2431     ** space is allocated for the fields below this point. An attempt to
  2430   2432     ** access them will result in a segfault or malfunction.
  2431   2433     *********************************************************************/
  2432   2434   
  2433   2435     Expr *pLeft;           /* Left subnode */
  2434         -  Expr *pRight;          /* Right subnode */
  2435   2436     union {
         2437  +    Expr *pRight;        /* Right subnode */
  2436   2438       ExprList *pList;     /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
  2437   2439       Select *pSelect;     /* EP_xIsSelect and op = IN, EXISTS, SELECT */
         2440  +    Table *pTab;         /* Table for TK_COLUMN expressions. Can be NULL
         2441  +                         ** for a column of an index on an expression */
  2438   2442     } x;
  2439   2443   
  2440   2444     /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  2441   2445     ** space is allocated for the fields below this point. An attempt to
  2442   2446     ** access them will result in a segfault or malfunction.
  2443   2447     *********************************************************************/
  2444   2448   
................................................................................
  2455   2459                            ** TK_SELECT_COLUMN: column of the result vector */
  2456   2460     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  2457   2461     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
  2458   2462     u8 op2;                /* TK_REGISTER: original value of Expr.op
  2459   2463                            ** TK_COLUMN: the value of p5 for OP_Column
  2460   2464                            ** TK_AGG_FUNCTION: nesting depth */
  2461   2465     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  2462         -  Table *pTab;           /* Table for TK_COLUMN expressions.  Can be NULL
  2463         -                         ** for a column of an index on an expression */
  2464   2466   #ifndef SQLITE_OMIT_WINDOWFUNC
  2465   2467     Window *pWin;          /* Window definition for window functions */
  2466   2468   #endif
  2467   2469   };
  2468   2470   
         2471  +/*
         2472  +** Allowed values for the Expr.eV and Expr.eX fields:
         2473  +*/
         2474  +#define EV_None     0    /* Expr.v is not used */
         2475  +#define EV_Left     1    /* Expr.v.pLeft */
         2476  +#define EV_Vector   2    /* Expr.v.pVector */
         2477  +#define EV_Win      3    /* Expr.v.pWin */
         2478  +
         2479  +#define EX_None     0    /* Expr.x is not used */
         2480  +#define EX_Right    1    /* Expr.x.pRight */
         2481  +#define EX_List     2    /* Expr.x.pList */
         2482  +#define EX_Select   3    /* Expr.x.pSelect */
         2483  +#define EX_Tab      4    /* Expr.x.pTab */
         2484  +
  2469   2485   /*
  2470   2486   ** The following are the meanings of bits in the Expr.flags field.
  2471   2487   */
  2472   2488   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
  2473   2489   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
  2474   2490   #define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
  2475   2491   #define EP_FixedCol  0x000008 /* TK_Column with a known fixed value */
................................................................................
  2476   2492   #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
  2477   2493   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  2478   2494   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  2479   2495   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
  2480   2496   #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
  2481   2497   #define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
  2482   2498   #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
  2483         -#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
         2499  +/*                   0x000800 -- Available for reuse */
  2484   2500   #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
  2485   2501   #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
  2486   2502   #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
  2487   2503   #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
  2488   2504   #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
  2489   2505   #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
  2490   2506   #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
................................................................................
  3790   3806   int sqlite3NoTempsInRange(Parse*,int,int);
  3791   3807   #endif
  3792   3808   Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  3793   3809   Expr *sqlite3Expr(sqlite3*,int,const char*);
  3794   3810   void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  3795   3811   Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
  3796   3812   void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
         3813  +void sqlite3PExprAddExprList(Parse*, Expr*, ExprList*);
         3814  +void sqlite3ExprAddTab(sqlite3*, Expr*, Table*);
  3797   3815   Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
  3798   3816   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
  3799   3817   void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
  3800   3818   void sqlite3ExprDelete(sqlite3*, Expr*);
         3819  +void sqlite3ExprClearXUnion(sqlite3*,Expr*);
  3801   3820   ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  3802   3821   ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
  3803   3822   void sqlite3ExprListSetSortOrder(ExprList*,int);
  3804   3823   void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
  3805   3824   void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
  3806   3825   void sqlite3ExprListDelete(sqlite3*, ExprList*);
  3807   3826   u32 sqlite3ExprListFlags(const ExprList*);
................................................................................
  4381   4400   FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
  4382   4401   sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
  4383   4402   int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
  4384   4403   int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
  4385   4404   void sqlite3ParserReset(Parse*);
  4386   4405   int sqlite3Reprepare(Vdbe*);
  4387   4406   void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
  4388         -CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
         4407  +CollSeq *sqlite3ComparisonCollSeq(Parse*,Expr*,Expr*);
         4408  +CollSeq *sqlite3ComparisonExprCollSeq(Parse*,Expr*);
  4389   4409   int sqlite3TempInMemory(const sqlite3*);
  4390   4410   const char *sqlite3JournalModename(int);
  4391   4411   #ifndef SQLITE_OMIT_WAL
  4392   4412     int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
  4393   4413     int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
  4394   4414   #endif
  4395   4415   #ifndef SQLITE_OMIT_CTE

Changes to src/treeview.c.

   234    234       }
   235    235   #endif
   236    236       if( p->pOrderBy ){
   237    237         sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
   238    238       }
   239    239       if( p->pLimit ){
   240    240         sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
   241         -      sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
   242         -      if( p->pLimit->pRight ){
          241  +      sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->eX==EX_Right);
          242  +      if( p->pLimit->eX==EX_Right ){
   243    243           sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
   244         -        sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
          244  +        sqlite3TreeViewExpr(pView, p->pLimit->x.pRight, 0);
   245    245           sqlite3TreeViewPop(pView);
   246    246         }
   247    247         sqlite3TreeViewPop(pView);
   248    248       }
   249    249       if( p->pPrior ){
   250    250         const char *zOp = "UNION";
   251    251         switch( p->op ){
................................................................................
   464    464   
   465    465       case TK_TRUTH: {
   466    466         int x;
   467    467         const char *azOp[] = {
   468    468            "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
   469    469         };
   470    470         assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
   471         -      assert( pExpr->pRight );
   472         -      assert( pExpr->pRight->op==TK_TRUEFALSE );
   473         -      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
          471  +      assert( pExpr->eX==EX_Right );
          472  +      assert( pExpr->x.pRight->op==TK_TRUEFALSE );
          473  +      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->x.pRight);
   474    474         zUniOp = azOp[x];
   475    475         break;
   476    476       }
   477    477   
   478    478       case TK_SPAN: {
   479    479         sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
   480    480         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
................................................................................
   528    528         sqlite3TreeViewLine(pView, "SELECT-expr flags=0x%x", pExpr->flags);
   529    529         sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
   530    530         break;
   531    531       }
   532    532       case TK_IN: {
   533    533         sqlite3TreeViewLine(pView, "IN flags=0x%x", pExpr->flags);
   534    534         sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
   535         -      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   536         -        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
   537         -      }else{
   538         -        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
          535  +      switch( pExpr->eX ){
          536  +        case EX_Select: {
          537  +          sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
          538  +          break;
          539  +        }
          540  +        case EX_List: {
          541  +          sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
          542  +          break;
          543  +        }
   539    544         }
   540    545         break;
   541    546       }
   542    547   #endif /* SQLITE_OMIT_SUBQUERY */
   543    548   
   544    549       /*
   545    550       **    x BETWEEN y AND z
................................................................................
   592    597         sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
   593    598         break;
   594    599       }
   595    600   #endif
   596    601       case TK_MATCH: {
   597    602         sqlite3TreeViewLine(pView, "MATCH {%d:%d}%s",
   598    603                             pExpr->iTable, pExpr->iColumn, zFlgs);
   599         -      sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
          604  +      assert( pExpr->eX==EX_Right );
          605  +      sqlite3TreeViewExpr(pView, pExpr->x.pRight, 0);
   600    606         break;
   601    607       }
   602    608       case TK_VECTOR: {
   603    609         sqlite3TreeViewBareExprList(pView, pExpr->x.pList, "VECTOR");
   604    610         break;
   605    611       }
   606    612       case TK_SELECT_COLUMN: {
................................................................................
   617    623         sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
   618    624         break;
   619    625       }
   620    626     }
   621    627     if( zBinOp ){
   622    628       sqlite3TreeViewLine(pView, "%s%s", zBinOp, zFlgs);
   623    629       sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
   624         -    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
          630  +    assert( pExpr->eX==EX_Right );
          631  +    sqlite3TreeViewExpr(pView, pExpr->x.pRight, 0);
   625    632     }else if( zUniOp ){
   626    633       sqlite3TreeViewLine(pView, "%s%s", zUniOp, zFlgs);
   627    634       sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
   628    635     }
   629    636     sqlite3TreeViewPop(pView);
   630    637   }
   631    638   

Changes to src/vdbeaux.c.

  1371   1371         sqlite3_str_appendf(p, "%s", "expr");
  1372   1372         break;
  1373   1373     }
  1374   1374   
  1375   1375     if( zOp ){
  1376   1376       sqlite3_str_appendf(p, "%s(", zOp);
  1377   1377       displayP4Expr(p, pExpr->pLeft);
  1378         -    if( pExpr->pRight ){
         1378  +    if( pExpr->eX==EX_Right ){
  1379   1379         sqlite3_str_append(p, ",", 1);
  1380         -      displayP4Expr(p, pExpr->pRight);
         1380  +      displayP4Expr(p, pExpr->x.pRight);
  1381   1381       }
  1382   1382       sqlite3_str_append(p, ")", 1);
  1383   1383     }
  1384   1384   }
  1385   1385   #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
  1386   1386   
  1387   1387   

Changes to src/vtab.c.

  1049   1049     void *pArg = 0;
  1050   1050     FuncDef *pNew;
  1051   1051     int rc = 0;
  1052   1052   
  1053   1053     /* Check to see the left operand is a column in a virtual table */
  1054   1054     if( NEVER(pExpr==0) ) return pDef;
  1055   1055     if( pExpr->op!=TK_COLUMN ) return pDef;
  1056         -  pTab = pExpr->pTab;
  1057         -  if( pTab==0 ) return pDef;
         1056  +  if( pExpr->eX!=EX_Tab || (pTab = pExpr->x.pTab)==0 ) return pDef;
  1058   1057     if( !IsVirtual(pTab) ) return pDef;
  1059   1058     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
  1060   1059     assert( pVtab!=0 );
  1061   1060     assert( pVtab->pModule!=0 );
  1062   1061     pMod = (sqlite3_module *)pVtab->pModule;
  1063   1062     if( pMod->xFindFunction==0 ) return pDef;
  1064   1063    

Changes to src/walker.c.

    41     41     testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
    42     42     testcase( ExprHasProperty(pExpr, EP_Reduced) );
    43     43     while(1){
    44     44       rc = pWalker->xExprCallback(pWalker, pExpr);
    45     45       if( rc ) return rc & WRC_Abort;
    46     46       if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
    47     47         if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
    48         -       assert( pExpr->x.pList==0 || pExpr->pRight==0 );
    49         -      if( pExpr->pRight ){
    50         -        pExpr = pExpr->pRight;
    51         -        continue;
    52         -      }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
    53         -        if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
    54         -      }else if( pExpr->x.pList ){
    55         -        if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
           48  +      switch( pExpr->eX ){
           49  +        case EX_Right: {
           50  +          pExpr = pExpr->x.pRight;
           51  +          continue;
           52  +        }
           53  +        case EX_Select: {
           54  +          if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
           55  +          break;
           56  +        }
           57  +        case EX_List: {
           58  +          if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
           59  +          break;
           60  +        }
    56     61         }
    57     62   #ifndef SQLITE_OMIT_WINDOWFUNC
    58     63         if( !ExprHasProperty(pExpr, EP_Reduced) && pExpr->pWin ){
    59     64           Window *pWin = pExpr->pWin;
    60     65           if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
    61     66           if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
    62     67           if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;

Changes to src/where.c.

   249    249            && (iColumn!=XN_EXPR
   250    250                || sqlite3ExprCompareSkip(pTerm->pExpr->pLeft,
   251    251                                          pScan->pIdxExpr,iCur)==0)
   252    252            && (pScan->iEquiv<=1 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
   253    253           ){
   254    254             if( (pTerm->eOperator & WO_EQUIV)!=0
   255    255              && pScan->nEquiv<ArraySize(pScan->aiCur)
   256         -           && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight))->op==TK_COLUMN
          256  +           && ALWAYS(pTerm->pExpr->eX==EX_Right)
          257  +           && (pX = sqlite3ExprSkipCollate(pTerm->pExpr->x.pRight))->op
          258  +                       ==TK_COLUMN
   257    259             ){
   258    260               int j;
   259    261               for(j=0; j<pScan->nEquiv; j++){
   260    262                 if( pScan->aiCur[j]==pX->iTable
   261    263                  && pScan->aiColumn[j]==pX->iColumn ){
   262    264                     break;
   263    265                 }
................................................................................
   274    276                 CollSeq *pColl;
   275    277                 Parse *pParse = pWC->pWInfo->pParse;
   276    278                 pX = pTerm->pExpr;
   277    279                 if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
   278    280                   continue;
   279    281                 }
   280    282                 assert(pX->pLeft);
   281         -              pColl = sqlite3BinaryCompareCollSeq(pParse,
   282         -                                                  pX->pLeft, pX->pRight);
          283  +              pColl = sqlite3ComparisonExprCollSeq(pParse, pX);
   283    284                 if( pColl==0 ) pColl = pParse->db->pDfltColl;
   284    285                 if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
   285    286                   continue;
   286    287                 }
   287    288               }
   288    289               if( (pTerm->eOperator & (WO_EQ|WO_IS))!=0
   289         -             && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
          290  +             && ALWAYS(pTerm->pExpr->eX==EX_Right)
          291  +             && (pX = pTerm->pExpr->x.pRight)->op==TK_COLUMN
   290    292                && pX->iTable==pScan->aiCur[0]
   291    293                && pX->iColumn==pScan->aiColumn[0]
   292    294               ){
   293    295                 testcase( pTerm->eOperator & WO_IS );
   294    296                 continue;
   295    297               }
   296    298               pScan->pWC = pWC;
................................................................................
   785    787         Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
   786    788         testcase( iCol==BMS-1 );
   787    789         testcase( iCol==BMS );
   788    790         if( (idxCols & cMask)==0 ){
   789    791           Expr *pX = pTerm->pExpr;
   790    792           idxCols |= cMask;
   791    793           pIdx->aiColumn[n] = pTerm->u.leftColumn;
   792         -        pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
          794  +        pColl = sqlite3ComparisonExprCollSeq(pParse, pX);
   793    795           pIdx->azColl[n] = pColl ? pColl->zName : sqlite3StrBINARY;
   794    796           n++;
   795    797         }
   796    798       }
   797    799     }
   798    800     assert( (u32)n==pLoop->u.btree.nEq );
   799    801   
................................................................................
  1002   1004         assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
  1003   1005         assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
  1004   1006         assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
  1005   1007         assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
  1006   1008         assert( pTerm->eOperator&(WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_AUX) );
  1007   1009   
  1008   1010         if( op & (WO_LT|WO_LE|WO_GT|WO_GE)
  1009         -       && sqlite3ExprIsVector(pTerm->pExpr->pRight) 
         1011  +       && ALWAYS(pTerm->pExpr->eX==EX_Right)
         1012  +       && sqlite3ExprIsVector(pTerm->pExpr->x.pRight) 
  1010   1013         ){
  1011   1014           if( i<16 ) mNoOmit |= (1 << i);
  1012   1015           if( op==WO_LT ) pIdxCons[j].op = WO_LE;
  1013   1016           if( op==WO_GT ) pIdxCons[j].op = WO_GE;
  1014   1017         }
  1015   1018       }
  1016   1019   
................................................................................
  1362   1365     
  1363   1366     sqlite3_value *p1 = 0;          /* Value extracted from pLower */
  1364   1367     sqlite3_value *p2 = 0;          /* Value extracted from pUpper */
  1365   1368     sqlite3_value *pVal = 0;        /* Value extracted from record */
  1366   1369   
  1367   1370     pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
  1368   1371     if( pLower ){
  1369         -    rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
         1372  +    assert( pLower->pExpr->eX==EX_Right );
         1373  +    rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->x.pRight, aff, &p1);
  1370   1374       nLower = 0;
  1371   1375     }
  1372   1376     if( pUpper && rc==SQLITE_OK ){
  1373         -    rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
         1377  +    assert( pUpper->pExpr->eX==EX_Right );
         1378  +    rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->x.pRight, aff, &p2);
  1374   1379       nUpper = p2 ? 0 : p->nSample;
  1375   1380     }
  1376   1381   
  1377   1382     if( p1 || p2 ){
  1378   1383       int i;
  1379   1384       int nDiff;
  1380   1385       for(i=0; rc==SQLITE_OK && i<p->nSample; i++){
................................................................................
  1528   1533           SWAP(WhereTerm*, pLower, pUpper);
  1529   1534           SWAP(int, nBtm, nTop);
  1530   1535         }
  1531   1536   
  1532   1537         /* If possible, improve on the iLower estimate using ($P:$L). */
  1533   1538         if( pLower ){
  1534   1539           int n;                    /* Values extracted from pExpr */
  1535         -        Expr *pExpr = pLower->pExpr->pRight;
         1540  +        Expr *pExpr;
         1541  +        assert( pLower->pExpr->eX==EX_Right );
         1542  +        pExpr = pLower->pExpr->x.pRight;
  1536   1543           rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nBtm, nEq, &n);
  1537   1544           if( rc==SQLITE_OK && n ){
  1538   1545             tRowcnt iNew;
  1539   1546             u16 mask = WO_GT|WO_LE;
  1540   1547             if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
  1541   1548             iLwrIdx = whereKeyStats(pParse, p, pRec, 0, a);
  1542   1549             iNew = a[0] + ((pLower->eOperator & mask) ? a[1] : 0);
................................................................................
  1545   1552             pLower = 0;
  1546   1553           }
  1547   1554         }
  1548   1555   
  1549   1556         /* If possible, improve on the iUpper estimate using ($P:$U). */
  1550   1557         if( pUpper ){
  1551   1558           int n;                    /* Values extracted from pExpr */
  1552         -        Expr *pExpr = pUpper->pExpr->pRight;
         1559  +        Expr *pExpr;
         1560  +        assert( pUpper->pExpr->eX==EX_Right );
         1561  +        pExpr = pUpper->pExpr->x.pRight;
  1553   1562           rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, nTop, nEq, &n);
  1554   1563           if( rc==SQLITE_OK && n ){
  1555   1564             tRowcnt iNew;
  1556   1565             u16 mask = WO_GT|WO_LE;
  1557   1566             if( sqlite3ExprVectorSize(pExpr)>n ) mask = (WO_LE|WO_LT);
  1558   1567             iUprIdx = whereKeyStats(pParse, p, pRec, 1, a);
  1559   1568             iNew = a[0] + ((pUpper->eOperator & mask) ? a[1] : 0);
................................................................................
  2275   2284           ** then use the probability provided by the application. */
  2276   2285           pLoop->nOut += pTerm->truthProb;
  2277   2286         }else{
  2278   2287           /* In the absence of explicit truth probabilities, use heuristics to
  2279   2288           ** guess a reasonable truth probability. */
  2280   2289           pLoop->nOut--;
  2281   2290           if( pTerm->eOperator&(WO_EQ|WO_IS) ){
  2282         -          Expr *pRight = pTerm->pExpr->pRight;
         2291  +          Expr *pRight;
         2292  +          assert( pTerm->pExpr->eX==EX_Right );
         2293  +          pRight = pTerm->pExpr->x.pRight;
  2283   2294             testcase( pTerm->pExpr->op==TK_IS );
  2284   2295             if( sqlite3ExprIsInteger(pRight, &k) && k>=(-1) && k<=1 ){
  2285   2296               k = 10;
  2286   2297             }else{
  2287   2298               k = 20;
  2288   2299             }
  2289   2300             if( iReduce<k ) iReduce = k;
................................................................................
  2324   2335     nCmp = MIN(nCmp, (pIdx->nColumn - nEq));
  2325   2336     for(i=1; i<nCmp; i++){
  2326   2337       /* Test if comparison i of pTerm is compatible with column (i+nEq) 
  2327   2338       ** of the index. If not, exit the loop.  */
  2328   2339       char aff;                     /* Comparison affinity */
  2329   2340       char idxaff = 0;              /* Indexed columns affinity */
  2330   2341       CollSeq *pColl;               /* Comparison collation sequence */
  2331         -    Expr *pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
  2332         -    Expr *pRhs = pTerm->pExpr->pRight;
  2333         -    if( pRhs->flags & EP_xIsSelect ){
         2342  +    Expr *pLhs;
         2343  +    Expr *pRhs;
         2344  +    pLhs = pTerm->pExpr->pLeft->x.pList->a[i].pExpr;
         2345  +    assert( pTerm->pExpr->eX==EX_Right );
         2346  +    pRhs = pTerm->pExpr->x.pRight;
         2347  +    assert( pRhs->eX==EX_Select || pRhs->eX==EX_List );
         2348  +    if( pRhs->eX==EX_Select ){
  2334   2349         pRhs = pRhs->x.pSelect->pEList->a[i].pExpr;
  2335   2350       }else{
  2336   2351         pRhs = pRhs->x.pList->a[i].pExpr;
  2337   2352       }
  2338   2353   
  2339   2354       /* Check that the LHS of the comparison is a column reference to
  2340   2355       ** the right column of the right source table. And that the sort
................................................................................
  2349   2364       }
  2350   2365   
  2351   2366       testcase( pLhs->iColumn==XN_ROWID );
  2352   2367       aff = sqlite3CompareAffinity(pRhs, sqlite3ExprAffinity(pLhs));
  2353   2368       idxaff = sqlite3TableColumnAffinity(pIdx->pTable, pLhs->iColumn);
  2354   2369       if( aff!=idxaff ) break;
  2355   2370   
  2356         -    pColl = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs);
         2371  +    pColl = sqlite3ComparisonCollSeq(pParse, pLhs, pRhs);
  2357   2372       if( pColl==0 ) break;
  2358   2373       if( sqlite3StrICmp(pColl->zName, pIdx->azColl[i+nEq]) ) break;
  2359   2374     }
  2360   2375     return i;
  2361   2376   }
  2362   2377   
  2363   2378   /*
................................................................................
  2484   2499           || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0 
  2485   2500           || (pNew->wsFlags & WHERE_COLUMN_IN)!=0 
  2486   2501           || (pNew->wsFlags & WHERE_SKIPSCAN)!=0 
  2487   2502       );
  2488   2503   
  2489   2504       if( eOp & WO_IN ){
  2490   2505         Expr *pExpr = pTerm->pExpr;
  2491         -      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  2492         -        /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
  2493         -        int i;
  2494         -        nIn = 46;  assert( 46==sqlite3LogEst(25) );
  2495         -
  2496         -        /* The expression may actually be of the form (x, y) IN (SELECT...).
  2497         -        ** In this case there is a separate term for each of (x) and (y).
  2498         -        ** However, the nIn multiplier should only be applied once, not once
  2499         -        ** for each such term. The following loop checks that pTerm is the
  2500         -        ** first such term in use, and sets nIn back to 0 if it is not. */
  2501         -        for(i=0; i<pNew->nLTerm-1; i++){
  2502         -          if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
  2503         -        }
  2504         -      }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
  2505         -        /* "x IN (value, value, ...)" */
  2506         -        nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
  2507         -        assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
  2508         -                          ** changes "x IN (?)" into "x=?". */
         2506  +      switch( pExpr->eX ){
         2507  +        case EX_Select: {
         2508  +          /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
         2509  +          int i;
         2510  +          nIn = 46;  assert( 46==sqlite3LogEst(25) );
         2511  +  
         2512  +          /* The expression may actually be of the form (x, y) IN (SELECT...).
         2513  +          ** In this case there is a separate term for each of (x) and (y).
         2514  +          ** However, the nIn multiplier should only be applied once, not once
         2515  +          ** for each such term. The following loop checks that pTerm is the
         2516  +          ** first such term in use, and sets nIn back to 0 if it is not. */
         2517  +          for(i=0; i<pNew->nLTerm-1; i++){
         2518  +            if( pNew->aLTerm[i] && pNew->aLTerm[i]->pExpr==pExpr ) nIn = 0;
         2519  +          }
         2520  +          break;
         2521  +        }
         2522  +        case EX_List: {
         2523  +          /* "x IN (value, value, ...)" */
         2524  +          nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
         2525  +          assert( nIn>0 );  /* RHS always has 2 or more terms...  The parser
         2526  +                            ** changes "x IN (?)" into "x=?". */
         2527  +          break;
         2528  +        }
  2509   2529         }
  2510   2530         if( pProbe->hasStat1 ){
  2511   2531           LogEst M, logK, safetyMargin;
  2512   2532           /* Let:
  2513   2533           **   N = the total number of rows in the table
  2514   2534           **   K = the number of entries on the RHS of the IN operator
  2515   2535           **   M = the number of rows in the table that match terms to the 
................................................................................
  2616   2636           pNew->nOut -= nIn;
  2617   2637         }else{
  2618   2638   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2619   2639           tRowcnt nOut = 0;
  2620   2640           if( nInMul==0 
  2621   2641            && pProbe->nSample 
  2622   2642            && pNew->u.btree.nEq<=pProbe->nSampleCol
  2623         -         && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
         2643  +         && ((eOp & WO_IN)==0 || pTerm->pExpr->eX==EX_List)
  2624   2644            && OptimizationEnabled(db, SQLITE_Stat34)
  2625   2645           ){
  2626   2646             Expr *pExpr = pTerm->pExpr;
  2627         -          if( (eOp & (WO_EQ|WO_ISNULL|WO_IS))!=0 ){
         2647  +          if( (eOp & (WO_EQ|WO_IS))!=0 ){
  2628   2648               testcase( eOp & WO_EQ );
  2629   2649               testcase( eOp & WO_IS );
  2630   2650               testcase( eOp & WO_ISNULL );
  2631         -            rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
  2632         -          }else{
         2651  +            assert( pExpr->eX==EX_Right );
         2652  +            rc = whereEqualScanEst(pParse, pBuilder, pExpr->x.pRight, &nOut);
         2653  +          }else if( pExpr->eX==EX_List ){
  2633   2654               rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
  2634   2655             }
  2635   2656             if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
  2636   2657             if( rc!=SQLITE_OK ) break;          /* Jump out of the pTerm loop */
  2637   2658             if( nOut ){
  2638   2659               pNew->nOut = sqlite3LogEst(nOut);
  2639   2660               if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
................................................................................
  2779   2800   */
  2780   2801   static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
  2781   2802     int i;
  2782   2803     WhereTerm *pTerm;
  2783   2804     Parse *pParse = pWC->pWInfo->pParse;
  2784   2805     while( pWhere->op==TK_AND ){
  2785   2806       if( !whereUsablePartialIndex(iTab,pWC,pWhere->pLeft) ) return 0;
  2786         -    pWhere = pWhere->pRight;
         2807  +    assert( pWhere->eX==EX_Right );
         2808  +    pWhere = pWhere->x.pRight;
  2787   2809     }
  2788   2810     if( pParse->db->flags & SQLITE_EnableQPSG ) pParse = 0;
  2789   2811     for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  2790   2812       Expr *pExpr = pTerm->pExpr;
  2791   2813       if( (!ExprHasProperty(pExpr, EP_FromJoin) || pExpr->iRightJoinTable==iTab)
  2792   2814        && sqlite3ExprImpliesExpr(pParse, pExpr, pWhere, iTab) 
  2793   2815       ){
................................................................................
  3234   3256     HiddenIndexInfo *pHidden = (HiddenIndexInfo*)&pIdxInfo[1];
  3235   3257     const char *zRet = 0;
  3236   3258     if( iCons>=0 && iCons<pIdxInfo->nConstraint ){
  3237   3259       CollSeq *pC = 0;
  3238   3260       int iTerm = pIdxInfo->aConstraint[iCons].iTermOffset;
  3239   3261       Expr *pX = pHidden->pWC->a[iTerm].pExpr;
  3240   3262       if( pX->pLeft ){
  3241         -      pC = sqlite3BinaryCompareCollSeq(pHidden->pParse, pX->pLeft, pX->pRight);
         3263  +      pC = sqlite3ComparisonExprCollSeq(pHidden->pParse,pX);
  3242   3264       }
  3243   3265       zRet = (pC ? pC->zName : sqlite3StrBINARY);
  3244   3266     }
  3245   3267     return zRet;
  3246   3268   }
  3247   3269   
  3248   3270   /*

Changes to src/wherecode.c.

   431    431           assert( pOrigLhs->a[iField].pExpr!=0 );
   432    432           pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
   433    433           pOrigLhs->a[iField].pExpr = 0;
   434    434         }
   435    435       }
   436    436       sqlite3ExprListDelete(db, pOrigRhs);
   437    437       sqlite3ExprListDelete(db, pOrigLhs);
          438  +    assert( pNew->pLeft->eX==EX_List );
   438    439       pNew->pLeft->x.pList = pLhs;
          440  +    if( pLhs==0 ) pNew->pLeft->eX = EX_None;
   439    441       pNew->x.pSelect->pEList = pRhs;
   440    442       if( pLhs && pLhs->nExpr==1 ){
   441    443         /* Take care here not to generate a TK_VECTOR containing only a
   442    444         ** single value. Since the parser never creates such a vector, some
   443    445         ** of the subroutines do not handle this case.  */
   444    446         Expr *p = pLhs->a[0].pExpr;
   445    447         pLhs->a[0].pExpr = 0;
................................................................................
   498    500     Expr *pX = pTerm->pExpr;
   499    501     Vdbe *v = pParse->pVdbe;
   500    502     int iReg;                  /* Register holding results */
   501    503   
   502    504     assert( pLevel->pWLoop->aLTerm[iEq]==pTerm );
   503    505     assert( iTarget>0 );
   504    506     if( pX->op==TK_EQ || pX->op==TK_IS ){
   505         -    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
          507  +    assert( pX->eX==EX_Right );
          508  +    iReg = sqlite3ExprCodeTarget(pParse, pX->x.pRight, iTarget);
   506    509     }else if( pX->op==TK_ISNULL ){
   507    510       iReg = iTarget;
   508    511       sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
   509    512   #ifndef SQLITE_OMIT_SUBQUERY
   510    513     }else{
   511    514       int eType = IN_INDEX_NOOP;
   512    515       int iTab;
................................................................................
   534    537         }
   535    538       }
   536    539       for(i=iEq;i<pLoop->nLTerm; i++){
   537    540         assert( pLoop->aLTerm[i]!=0 );
   538    541         if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
   539    542       }
   540    543   
   541         -    if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
          544  +    if( pX->eX!=EX_Select || pX->x.pSelect->pEList->nExpr==1 ){
   542    545         eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0);
   543    546       }else{
   544    547         sqlite3 *db = pParse->db;
   545    548         pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
   546    549   
   547    550         if( !db->mallocFailed ){
   548    551           aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
................................................................................
   730    733           sqlite3ReleaseTempReg(pParse, regBase);
   731    734           regBase = r1;
   732    735         }else{
   733    736           sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
   734    737         }
   735    738       }
   736    739       if( pTerm->eOperator & WO_IN ){
   737         -      if( pTerm->pExpr->flags & EP_xIsSelect ){
          740  +      if( pTerm->pExpr->eX==EX_Select ){
   738    741           /* No affinity ever needs to be (or should be) applied to a value
   739    742           ** from the RHS of an "? IN (SELECT ...)" expression. The 
   740    743           ** sqlite3FindInIndex() routine has already ensured that the 
   741    744           ** affinity of the comparison has been applied to the value.  */
   742    745           if( zAff ) zAff[j] = SQLITE_AFF_BLOB;
   743    746         }
   744    747       }else if( (pTerm->eOperator & WO_ISNULL)==0 ){
   745         -      Expr *pRight = pTerm->pExpr->pRight;
          748  +      Expr *pRight;
          749  +      assert( pTerm->pExpr->eX==EX_Right );
          750  +      pRight = pTerm->pExpr->x.pRight;
   746    751         if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
   747    752           sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
   748    753           VdbeCoverage(v);
   749    754         }
   750    755         if( zAff ){
   751    756           if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
   752    757             zAff[j] = SQLITE_AFF_BLOB;
................................................................................
  1070   1075   ** this case, generate code to evaluate the expression and leave the
  1071   1076   ** result in register iReg.
  1072   1077   */
  1073   1078   static void codeExprOrVector(Parse *pParse, Expr *p, int iReg, int nReg){
  1074   1079     assert( nReg>0 );
  1075   1080     if( p && sqlite3ExprIsVector(p) ){
  1076   1081   #ifndef SQLITE_OMIT_SUBQUERY
  1077         -    if( (p->flags & EP_xIsSelect) ){
         1082  +    if( p->eX==EX_Select ){
  1078   1083         Vdbe *v = pParse->pVdbe;
  1079   1084         int iSelect = sqlite3CodeSubselect(pParse, p, 0, 0);
  1080   1085         sqlite3VdbeAddOp3(v, OP_Copy, iSelect, iReg, nReg-1);
  1081   1086       }else
  1082   1087   #endif
  1083   1088       {
  1084   1089         int i;
................................................................................
  1113   1118   */
  1114   1119   static int whereIndexExprTransNode(Walker *p, Expr *pExpr){
  1115   1120     IdxExprTrans *pX = p->u.pIdxTrans;
  1116   1121     if( sqlite3ExprCompare(0, pExpr, pX->pIdxExpr, pX->iTabCur)==0 ){
  1117   1122       pExpr->op = TK_COLUMN;
  1118   1123       pExpr->iTable = pX->iIdxCur;
  1119   1124       pExpr->iColumn = pX->iIdxCol;
  1120         -    pExpr->pTab = 0;
         1125  +    /* pExpr->pTab = 0; */
  1121   1126       return WRC_Prune;
  1122   1127     }else{
  1123   1128       return WRC_Continue;
  1124   1129     }
  1125   1130   }
  1126   1131   
  1127   1132   /*
................................................................................
  1258   1263         int iTarget = iReg+j+2;
  1259   1264         pTerm = pLoop->aLTerm[j];
  1260   1265         if( NEVER(pTerm==0) ) continue;
  1261   1266         if( pTerm->eOperator & WO_IN ){
  1262   1267           codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
  1263   1268           addrNotFound = pLevel->addrNxt;
  1264   1269         }else{
  1265         -        Expr *pRight = pTerm->pExpr->pRight;
         1270  +        Expr *pExpr = pTerm->pExpr;
         1271  +        Expr *pRight = pExpr->eX==EX_Right ? pExpr->x.pRight : 0;
  1266   1272           codeExprOrVector(pParse, pRight, iTarget, 1);
  1267   1273         }
  1268   1274       }
  1269   1275       sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
  1270   1276       sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
  1271   1277       sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
  1272   1278                         pLoop->u.vtab.idxStr,
................................................................................
  1303   1309   
  1304   1310           /* Generate code that will continue to the next row if 
  1305   1311           ** the IN constraint is not satisfied */
  1306   1312           pCompare = sqlite3PExpr(pParse, TK_EQ, 0, 0);
  1307   1313           assert( pCompare!=0 || db->mallocFailed );
  1308   1314           if( pCompare ){
  1309   1315             pCompare->pLeft = pTerm->pExpr->pLeft;
  1310         -          pCompare->pRight = pRight = sqlite3Expr(db, TK_REGISTER, 0);
         1316  +          assert( pCompare->eX==EX_None );
         1317  +          pRight = sqlite3Expr(db, TK_REGISTER, 0);
  1311   1318             if( pRight ){
         1319  +            pCompare->x.pRight = pRight;
         1320  +            pCompare->eX = EX_Right;
  1312   1321               pRight->iTable = iReg+j+2;
  1313   1322               sqlite3ExprIfFalse(pParse, pCompare, pLevel->addrCont, 0);
  1314   1323             }
  1315   1324             pCompare->pLeft = 0;
  1316   1325             sqlite3ExprDelete(db, pCompare);
  1317   1326           }
  1318   1327         }
................................................................................
  1389   1398         assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
  1390   1399   
  1391   1400         assert( (pStart->wtFlags & TERM_VNULL)==0 );
  1392   1401         testcase( pStart->wtFlags & TERM_VIRTUAL );
  1393   1402         pX = pStart->pExpr;
  1394   1403         assert( pX!=0 );
  1395   1404         testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
  1396         -      if( sqlite3ExprIsVector(pX->pRight) ){
         1405  +      assert( pX->eX==EX_Right );
         1406  +      if( sqlite3ExprIsVector(pX->x.pRight) ){
  1397   1407           r1 = rTemp = sqlite3GetTempReg(pParse);
  1398         -        codeExprOrVector(pParse, pX->pRight, r1, 1);
         1408  +        codeExprOrVector(pParse, pX->x.pRight, r1, 1);
  1399   1409           testcase( pX->op==TK_GT );
  1400   1410           testcase( pX->op==TK_GE );
  1401   1411           testcase( pX->op==TK_LT );
  1402   1412           testcase( pX->op==TK_LE );
  1403   1413           op = aMoveOp[((pX->op - TK_GT - 1) & 0x3) | 0x1];
  1404   1414           assert( pX->op!=TK_GT || op==OP_SeekGE );
  1405   1415           assert( pX->op!=TK_GE || op==OP_SeekGE );
  1406   1416           assert( pX->op!=TK_LT || op==OP_SeekLE );
  1407   1417           assert( pX->op!=TK_LE || op==OP_SeekLE );
  1408   1418         }else{
  1409         -        r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
         1419  +        r1 = sqlite3ExprCodeTemp(pParse, pX->x.pRight, &rTemp);
  1410   1420           disableTerm(pLevel, pStart);
  1411   1421           op = aMoveOp[(pX->op - TK_GT)];
  1412   1422         }
  1413   1423         sqlite3VdbeAddOp3(v, op, iCur, addrBrk, r1);
  1414   1424         VdbeComment((v, "pk"));
  1415   1425         VdbeCoverageIf(v, pX->op==TK_GT);
  1416   1426         VdbeCoverageIf(v, pX->op==TK_LE);
................................................................................
  1422   1432         VdbeCoverageIf(v, bRev==0);
  1423   1433         VdbeCoverageIf(v, bRev!=0);
  1424   1434       }
  1425   1435       if( pEnd ){
  1426   1436         Expr *pX;
  1427   1437         pX = pEnd->pExpr;
  1428   1438         assert( pX!=0 );
         1439  +      assert( pX->eX==EX_Right );
  1429   1440         assert( (pEnd->wtFlags & TERM_VNULL)==0 );
  1430   1441         testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
  1431   1442         testcase( pEnd->wtFlags & TERM_VIRTUAL );
  1432   1443         memEndValue = ++pParse->nMem;
  1433         -      codeExprOrVector(pParse, pX->pRight, memEndValue, 1);
  1434         -      if( 0==sqlite3ExprIsVector(pX->pRight) 
         1444  +      codeExprOrVector(pParse, pX->x.pRight, memEndValue, 1);
         1445  +      if( 0==sqlite3ExprIsVector(pX->x.pRight) 
  1435   1446          && (pX->op==TK_LT || pX->op==TK_GT) 
  1436   1447         ){
  1437   1448           testOp = bRev ? OP_Le : OP_Ge;
  1438   1449         }else{
  1439   1450           testOp = bRev ? OP_Lt : OP_Gt;
  1440   1451         }
  1441         -      if( 0==sqlite3ExprIsVector(pX->pRight) ){
         1452  +      if( 0==sqlite3ExprIsVector(pX->x.pRight) ){
  1442   1453           disableTerm(pLevel, pEnd);
  1443   1454         }
  1444   1455       }
  1445   1456       start = sqlite3VdbeCurrentAddr(v);
  1446   1457       pLevel->op = bRev ? OP_Prev : OP_Next;
  1447   1458       pLevel->p1 = iCur;
  1448   1459       pLevel->p2 = start;
................................................................................
  1618   1629       startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
  1619   1630       endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
  1620   1631       start_constraints = pRangeStart || nEq>0;
  1621   1632   
  1622   1633       /* Seek the index cursor to the start of the range. */
  1623   1634       nConstraint = nEq;
  1624   1635       if( pRangeStart ){
  1625         -      Expr *pRight = pRangeStart->pExpr->pRight;
         1636  +      Expr *pRight;
         1637  +      assert( pRangeStart->pExpr->eX==EX_Right );
         1638  +      pRight = pRangeStart->pExpr->x.pRight;
  1626   1639         codeExprOrVector(pParse, pRight, regBase+nEq, nBtm);
  1627   1640         whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
  1628   1641         if( (pRangeStart->wtFlags & TERM_VNULL)==0
  1629   1642          && sqlite3ExprCanBeNull(pRight)
  1630   1643         ){
  1631   1644           sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  1632   1645           VdbeCoverage(v);
................................................................................
  1670   1683       }
  1671   1684   
  1672   1685       /* Load the value for the inequality constraint at the end of the
  1673   1686       ** range (if any).
  1674   1687       */
  1675   1688       nConstraint = nEq;
  1676   1689       if( pRangeEnd ){
  1677         -      Expr *pRight = pRangeEnd->pExpr->pRight;
         1690  +      Expr *pRight;
         1691  +      assert( pRangeEnd->pExpr->eX==EX_Right );
         1692  +      pRight = pRangeEnd->pExpr->x.pRight;
  1678   1693         codeExprOrVector(pParse, pRight, regBase+nEq, nTop);
  1679   1694         whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
  1680   1695         if( (pRangeEnd->wtFlags & TERM_VNULL)==0
  1681   1696          && sqlite3ExprCanBeNull(pRight)
  1682   1697         ){
  1683   1698           sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  1684   1699           VdbeCoverage(v);
................................................................................
  2190   2205       assert( !ExprHasProperty(pE, EP_FromJoin) );
  2191   2206       assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
  2192   2207       pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
  2193   2208                       WO_EQ|WO_IN|WO_IS, 0);
  2194   2209       if( pAlt==0 ) continue;
  2195   2210       if( pAlt->wtFlags & (TERM_CODED) ) continue;
  2196   2211       if( (pAlt->eOperator & WO_IN) 
  2197         -     && (pAlt->pExpr->flags & EP_xIsSelect)
         2212  +     && (pAlt->pExpr->eX==EX_Select)
  2198   2213        && (pAlt->pExpr->x.pSelect->pEList->nExpr>1)
  2199   2214       ){
  2200   2215         continue;
  2201   2216       }
  2202   2217       testcase( pAlt->eOperator & WO_EQ );
  2203   2218       testcase( pAlt->eOperator & WO_IS );
  2204   2219       testcase( pAlt->eOperator & WO_IN );

Changes to src/whereexpr.c.

   115    115   ** that the collating sequence does not change.  For example:
   116    116   ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
   117    117   ** the left hand side of a comparison overrides any collation sequence 
   118    118   ** attached to the right. For the same reason the EP_Collate flag
   119    119   ** is not commuted.
   120    120   */
   121    121   static void exprCommute(Parse *pParse, Expr *pExpr){
   122         -  u16 expRight = (pExpr->pRight->flags & EP_Collate);
   123         -  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
          122  +  u16 expRight, expLeft;
          123  +  assert( pExpr->eX==EX_Right );
          124  +  expRight = (pExpr->x.pRight->flags & EP_Collate);
          125  +  expLeft = (pExpr->pLeft->flags & EP_Collate);
   124    126     assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
   125    127     if( expRight==expLeft ){
   126    128       /* Either X and Y both have COLLATE operator or neither do */
   127    129       if( expRight ){
   128    130         /* Both X and Y have COLLATE operators.  Make sure X is always
   129    131         ** used by clearing the EP_Collate flag from Y. */
   130         -      pExpr->pRight->flags &= ~EP_Collate;
          132  +      pExpr->x.pRight->flags &= ~EP_Collate;
   131    133       }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
   132    134         /* Neither X nor Y have COLLATE operators, but X has a non-default
   133    135         ** collating sequence.  So add the EP_Collate marker on X to cause
   134    136         ** it to be searched first. */
   135    137         pExpr->pLeft->flags |= EP_Collate;
   136    138       }
   137    139     }
   138         -  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
          140  +  SWAP(Expr*,pExpr->x.pRight,pExpr->pLeft);
   139    141     if( pExpr->op>=TK_GT ){
   140    142       assert( TK_LT==TK_GT+2 );
   141    143       assert( TK_GE==TK_LE+2 );
   142    144       assert( TK_GT>TK_EQ );
   143    145       assert( TK_GT<TK_LE );
   144    146       assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
   145    147       pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
................................................................................
   276    278           ** be converted into "x<0", which is incorrect.
   277    279           */
   278    280           if( sqlite3Isdigit(zNew[0])
   279    281            || zNew[0]=='-'
   280    282            || (zNew[0]+1=='0' && iTo==1)
   281    283           ){
   282    284             if( pLeft->op!=TK_COLUMN 
   283         -           || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
   284         -           || IsVirtual(pLeft->pTab)  /* Value might be numeric */
          285  +           || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
          286  +           || NEVER(pLeft->eX!=EX_Tab)
          287  +           || IsVirtual(pLeft->x.pTab)  /* Value might be numeric */
   285    288             ){
   286    289               sqlite3ExprDelete(db, pPrefix);
   287    290               sqlite3ValueFree(pVal);
   288    291               return 0;
   289    292             }
   290    293           }
   291    294         }
................................................................................
   378    381       ** virtual table on their second argument, which is the same as
   379    382       ** the left-hand side operand in their in-fix form.
   380    383       **
   381    384       **       vtab_column MATCH expression
   382    385       **       MATCH(expression,vtab_column)
   383    386       */
   384    387       pCol = pList->a[1].pExpr;
   385         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
          388  +    if( pCol->op==TK_COLUMN && pCol->eX==EX_Tab && IsVirtual(pCol->x.pTab) ){
   386    389         for(i=0; i<ArraySize(aOp); i++){
   387    390           if( sqlite3StrICmp(pExpr->u.zToken, aOp[i].zOp)==0 ){
   388    391             *peOp2 = aOp[i].eOp2;
   389    392             *ppRight = pList->a[0].pExpr;
   390    393             *ppLeft = pCol;
   391    394             return 1;
   392    395           }
................................................................................
   400    403       **      OVERLOADED(vtab_column,expression)
   401    404       **
   402    405       ** Historically, xFindFunction expected to see lower-case function
   403    406       ** names.  But for this use case, xFindFunction is expected to deal
   404    407       ** with function names in an arbitrary case.
   405    408       */
   406    409       pCol = pList->a[0].pExpr;
   407         -    if( pCol->op==TK_COLUMN && IsVirtual(pCol->pTab) ){
          410  +    if( pCol->op==TK_COLUMN && pCol->eX==EX_Tab && IsVirtual(pCol->x.pTab) ){
   408    411         sqlite3_vtab *pVtab;
   409    412         sqlite3_module *pMod;
   410    413         void (*xNotUsed)(sqlite3_context*,int,sqlite3_value**);
   411    414         void *pNotUsed;
   412         -      pVtab = sqlite3GetVTable(db, pCol->pTab)->pVtab;
          415  +      pVtab = sqlite3GetVTable(db, pCol->x.pTab)->pVtab;
   413    416         assert( pVtab!=0 );
   414    417         assert( pVtab->pModule!=0 );
   415    418         pMod = (sqlite3_module *)pVtab->pModule;
   416    419         if( pMod->xFindFunction!=0 ){
   417    420           i = pMod->xFindFunction(pVtab,2, pExpr->u.zToken, &xNotUsed, &pNotUsed);
   418    421           if( i>=SQLITE_INDEX_CONSTRAINT_FUNCTION ){
   419    422             *peOp2 = i;
................................................................................
   422    425             return 1;
   423    426           }
   424    427         }
   425    428       }
   426    429     }else if( pExpr->op==TK_NE || pExpr->op==TK_ISNOT || pExpr->op==TK_NOTNULL ){
   427    430       int res = 0;
   428    431       Expr *pLeft = pExpr->pLeft;
   429         -    Expr *pRight = pExpr->pRight;
   430         -    if( pLeft->op==TK_COLUMN && IsVirtual(pLeft->pTab) ){
          432  +    Expr *pRight = 0;
          433  +    if( pLeft->op==TK_COLUMN && pLeft->eX==EX_Tab && IsVirtual(pLeft->x.pTab) ){
   431    434         res++;
   432    435       }
   433         -    if( pRight && pRight->op==TK_COLUMN && IsVirtual(pRight->pTab) ){
          436  +    if( pExpr->eX==EX_Right && (pRight = pExpr->x.pRight)->op==TK_COLUMN
          437  +     && pRight->eX==EX_Tab && IsVirtual(pRight->x.pTab) ){
   434    438         res++;
   435    439         SWAP(Expr*, pLeft, pRight);
   436    440       }
   437    441       *ppLeft = pLeft;
   438    442       *ppRight = pRight;
   439    443       if( pExpr->op==TK_NE ) *peOp2 = SQLITE_INDEX_CONSTRAINT_NE;
   440    444       if( pExpr->op==TK_ISNOT ) *peOp2 = SQLITE_INDEX_CONSTRAINT_ISNOT;
................................................................................
   512    516     int op;                /* Operator for the combined expression */
   513    517     int idxNew;            /* Index in pWC of the next virtual term */
   514    518   
   515    519     if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
   516    520     if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
   517    521     if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
   518    522      && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
   519         -  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
   520         -  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
   521         -  if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
   522         -  if( sqlite3ExprCompare(0,pOne->pExpr->pRight, pTwo->pExpr->pRight,-1) )return;
          523  +  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->eX==EX_Right );
          524  +  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->eX==EX_Right );
          525  +  if( sqlite3ExprCompare(0,pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ){
          526  +    return;
          527  +  }
          528  +  if( sqlite3ExprCompare(0,pOne->pExpr->x.pRight, pTwo->pExpr->x.pRight,-1) ){
          529  +    return;
          530  +  }
   523    531     /* If we reach this point, it means the two subterms can be combined */
   524    532     if( (eOp & (eOp-1))!=0 ){
   525    533       if( eOp & (WO_LT|WO_LE) ){
   526    534         eOp = WO_LE;
   527    535       }else{
   528    536         assert( eOp & (WO_GT|WO_GE) );
   529    537         eOp = WO_GE;
................................................................................
   823    831             okToChngToIN = 0;
   824    832           }else{
   825    833             int affLeft, affRight;
   826    834             /* If the right-hand side is also a column, then the affinities
   827    835             ** of both right and left sides must be such that no type
   828    836             ** conversions are required on the right.  (Ticket #2249)
   829    837             */
   830         -          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
          838  +          assert( pOrTerm->pExpr->eX==EX_Right );
          839  +          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->x.pRight);
   831    840             affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
   832    841             if( affRight!=0 && affRight!=affLeft ){
   833    842               okToChngToIN = 0;
   834    843             }else{
   835    844               pOrTerm->wtFlags |= TERM_OR_OK;
   836    845             }
   837    846           }
................................................................................
   849    858         Expr *pNew;            /* The complete IN operator */
   850    859   
   851    860         for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
   852    861           if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
   853    862           assert( pOrTerm->eOperator & WO_EQ );
   854    863           assert( pOrTerm->leftCursor==iCursor );
   855    864           assert( pOrTerm->u.leftColumn==iColumn );
   856         -        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
          865  +        assert( pOrTerm->pExpr->eX==EX_Right );
          866  +        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->x.pRight, 0);
   857    867           pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
   858    868           pLeft = pOrTerm->pExpr->pLeft;
   859    869         }
   860    870         assert( pLeft!=0 );
   861    871         pDup = sqlite3ExprDup(db, pLeft, 0);
   862    872         pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0);
   863         -      if( pNew ){
          873  +      if( pNew && pList ){
   864    874           int idxNew;
   865    875           transferJoinMarkings(pNew, pExpr);
   866         -        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
          876  +        assert( pNew->eX==EX_None );
          877  +        pNew->eX = EX_List;
   867    878           pNew->x.pList = pList;
   868    879           idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
   869    880           testcase( idxNew==0 );
   870    881           exprAnalyze(pSrc, pWC, idxNew);
   871         -        /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm where used again */
          882  +        /* pTerm = &pWC->a[idxTerm]; // would be needed if pTerm used again */
   872    883           markTermAsChild(pWC, idxNew, idxTerm);
   873    884         }else{
   874    885           sqlite3ExprListDelete(db, pList);
          886  +        sqlite3ExprDelete(db, pNew);
   875    887         }
   876    888       }
   877    889     }
   878    890   }
   879    891   #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
   880    892   
   881    893   /*
................................................................................
   896    908   static int termIsEquivalence(Parse *pParse, Expr *pExpr){
   897    909     char aff1, aff2;
   898    910     CollSeq *pColl;
   899    911     if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
   900    912     if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
   901    913     if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
   902    914     aff1 = sqlite3ExprAffinity(pExpr->pLeft);
   903         -  aff2 = sqlite3ExprAffinity(pExpr->pRight);
          915  +  assert( pExpr->eX==EX_Right );
          916  +  aff2 = sqlite3ExprAffinity(pExpr->x.pRight);
   904    917     if( aff1!=aff2
   905    918      && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
   906    919     ){
   907    920       return 0;
   908    921     }
   909         -  pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
          922  +  pColl = sqlite3ComparisonExprCollSeq(pParse, pExpr);
   910    923     if( sqlite3IsBinary(pColl) ) return 1;
   911         -  return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->pRight);
          924  +  return sqlite3ExprCollSeqMatch(pParse, pExpr->pLeft, pExpr->x.pRight);
   912    925   }
   913    926   
   914    927   /*
   915    928   ** Recursively walk the expressions of a SELECT statement and generate
   916    929   ** a bitmask indicating which tables are used in that expression
   917    930   ** tree.
   918    931   */
................................................................................
  1049   1062     pTerm = &pWC->a[idxTerm];
  1050   1063     pMaskSet = &pWInfo->sMaskSet;
  1051   1064     pExpr = pTerm->pExpr;
  1052   1065     assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
  1053   1066     prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
  1054   1067     op = pExpr->op;
  1055   1068     if( op==TK_IN ){
  1056         -    assert( pExpr->pRight==0 );
         1069  +    assert( pExpr->eX!=EX_Right );
  1057   1070       if( sqlite3ExprCheckIN(pParse, pExpr) ) return;
  1058         -    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1059         -      pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
  1060         -    }else{
  1061         -      pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
         1071  +    switch( pExpr->eX ){
         1072  +      case EX_Select: {
         1073  +        pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
         1074  +        break;
         1075  +      }
         1076  +      case EX_List: {
         1077  +        pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet,pExpr->x.pList);
         1078  +        break;
         1079  +      }
  1062   1080       }
  1063         -  }else if( op==TK_ISNULL ){
  1064         -    pTerm->prereqRight = 0;
  1065   1081     }else{
  1066         -    pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
         1082  +    assert( op!=TK_ISNULL || pExpr->eX!=EX_Right );
         1083  +    if( pExpr->eX==EX_Right ){
         1084  +      pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->x.pRight);
         1085  +    }else{
         1086  +      pTerm->prereqRight = 0;
         1087  +    }
  1067   1088     }
  1068   1089     pMaskSet->bVarSelect = 0;
  1069   1090     prereqAll = sqlite3WhereExprUsageNN(pMaskSet, pExpr);
  1070   1091     if( pMaskSet->bVarSelect ) pTerm->wtFlags |= TERM_VARSELECT;
  1071   1092     if( ExprHasProperty(pExpr, EP_FromJoin) ){
  1072   1093       Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
  1073   1094       prereqAll |= x;
................................................................................
  1080   1101     }
  1081   1102     pTerm->prereqAll = prereqAll;
  1082   1103     pTerm->leftCursor = -1;
  1083   1104     pTerm->iParent = -1;
  1084   1105     pTerm->eOperator = 0;
  1085   1106     if( allowedOp(op) ){
  1086   1107       int aiCurCol[2];
  1087         -    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
  1088         -    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
  1089         -    u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
         1108  +    Expr *pLeft, *pRight;
         1109  +    u16 opMask;
  1090   1110   
         1111  +    pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
         1112  +    if( pExpr->eX==EX_Right ){
         1113  +      pRight = sqlite3ExprSkipCollate(pExpr->x.pRight);
         1114  +    }else{
         1115  +      pRight = 0;
         1116  +    }
         1117  +    opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
  1091   1118       if( pTerm->iField>0 ){
  1092   1119         assert( op==TK_IN );
  1093   1120         assert( pLeft->op==TK_VECTOR );
  1094   1121         pLeft = pLeft->x.pList->a[pTerm->iField-1].pExpr;
  1095   1122       }
  1096   1123   
  1097   1124       if( exprMightBeIndexed(pSrc, prereqLeft, aiCurCol, pLeft, op) ){
................................................................................
  1324   1351     ** no longer used.
  1325   1352     **
  1326   1353     ** This is only required if at least one side of the comparison operation
  1327   1354     ** is not a sub-select.  */
  1328   1355     if( pWC->op==TK_AND 
  1329   1356     && (pExpr->op==TK_EQ || pExpr->op==TK_IS)
  1330   1357     && (nLeft = sqlite3ExprVectorSize(pExpr->pLeft))>1
  1331         -  && sqlite3ExprVectorSize(pExpr->pRight)==nLeft
  1332         -  && ( (pExpr->pLeft->flags & EP_xIsSelect)==0 
  1333         -    || (pExpr->pRight->flags & EP_xIsSelect)==0)
         1358  +  && ALWAYS(pExpr->eX==EX_Right)
         1359  +  && sqlite3ExprVectorSize(pExpr->x.pRight)==nLeft
         1360  +  && ( pExpr->pLeft->eX!=EX_Select || pExpr->x.pRight->eX!=EX_Select )
  1334   1361     ){
  1335   1362       int i;
  1336   1363       for(i=0; i<nLeft; i++){
  1337   1364         int idxNew;
  1338         -      Expr *pNew;
  1339         -      Expr *pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
  1340         -      Expr *pRight = sqlite3ExprForVectorField(pParse, pExpr->pRight, i);
         1365  +      Expr *pNew, *pLeft, *pRight;
  1341   1366   
         1367  +      pLeft = sqlite3ExprForVectorField(pParse, pExpr->pLeft, i);
         1368  +      assert( pExpr->eX==EX_Right );
         1369  +      pRight = sqlite3ExprForVectorField(pParse, pExpr->x.pRight, i);
  1342   1370         pNew = sqlite3PExpr(pParse, pExpr->op, pLeft, pRight);
  1343   1371         transferJoinMarkings(pNew, pExpr);
  1344   1372         idxNew = whereClauseInsert(pWC, pNew, TERM_DYNAMIC);
  1345   1373         exprAnalyze(pSrc, pWC, idxNew);
  1346   1374       }
  1347   1375       pTerm = &pWC->a[idxTerm];
  1348   1376       pTerm->wtFlags |= TERM_CODED|TERM_VIRTUAL;  /* Disable the original */
................................................................................
  1442   1470   void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
  1443   1471     Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
  1444   1472     pWC->op = op;
  1445   1473     if( pE2==0 ) return;
  1446   1474     if( pE2->op!=op ){
  1447   1475       whereClauseInsert(pWC, pExpr, 0);
  1448   1476     }else{
         1477  +    assert( pE2->eX==EX_Right );
  1449   1478       sqlite3WhereSplit(pWC, pE2->pLeft, op);
  1450         -    sqlite3WhereSplit(pWC, pE2->pRight, op);
         1479  +    sqlite3WhereSplit(pWC, pE2->x.pRight, op);
  1451   1480     }
  1452   1481   }
  1453   1482   
  1454   1483   /*
  1455   1484   ** Initialize a preallocated WhereClause structure.
  1456   1485   */
  1457   1486   void sqlite3WhereClauseInit(
................................................................................
  1502   1531       return sqlite3WhereGetMask(pMaskSet, p->iTable);
  1503   1532     }else if( ExprHasProperty(p, EP_TokenOnly|EP_Leaf) ){
  1504   1533       assert( p->op!=TK_IF_NULL_ROW );
  1505   1534       return 0;
  1506   1535     }
  1507   1536     mask = (p->op==TK_IF_NULL_ROW) ? sqlite3WhereGetMask(pMaskSet, p->iTable) : 0;
  1508   1537     if( p->pLeft ) mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pLeft);
  1509         -  if( p->pRight ){
  1510         -    mask |= sqlite3WhereExprUsageNN(pMaskSet, p->pRight);
  1511         -    assert( p->x.pList==0 );
  1512         -  }else if( ExprHasProperty(p, EP_xIsSelect) ){
  1513         -    if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
  1514         -    mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
  1515         -  }else if( p->x.pList ){
  1516         -    mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
         1538  +  switch( p->eX ){
         1539  +    case EX_Right: {
         1540  +      mask |= sqlite3WhereExprUsageNN(pMaskSet, p->x.pRight);
         1541  +      break;
         1542  +    }
         1543  +    case EX_Select: {
         1544  +      if( ExprHasProperty(p, EP_VarSelect) ) pMaskSet->bVarSelect = 1;
         1545  +      mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
         1546  +      break;
         1547  +    }
         1548  +    case EX_List: {
         1549  +      mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
         1550  +      break;
         1551  +    }
  1517   1552     }
  1518   1553     return mask;
  1519   1554   }
  1520   1555   Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
  1521   1556     return p ? sqlite3WhereExprUsageNN(pMaskSet,p) : 0;
  1522   1557   }
  1523   1558   Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
................................................................................
  1579   1614                         pTab->zName, j);
  1580   1615         return;
  1581   1616       }
  1582   1617       pColRef = sqlite3ExprAlloc(pParse->db, TK_COLUMN, 0, 0);
  1583   1618       if( pColRef==0 ) return;
  1584   1619       pColRef->iTable = pItem->iCursor;
  1585   1620       pColRef->iColumn = k++;
  1586         -    pColRef->pTab = pTab;
         1621  +    pColRef->x.pTab = pTab;
         1622  +    pColRef->eX = EX_Tab;
  1587   1623       pTerm = sqlite3PExpr(pParse, TK_EQ, pColRef,
  1588   1624                            sqlite3ExprDup(pParse->db, pArgs->a[j].pExpr, 0));
  1589   1625       whereClauseInsert(pWC, pTerm, TERM_DYNAMIC);
  1590   1626     }
  1591   1627   }

Changes to test/bestindex5.test.

    10     10   #***********************************************************************
    11     11   # Test the virtual table interface. In particular the xBestIndex
    12     12   # method.
    13     13   #
    14     14   
    15     15   set testdir [file dirname $argv0]
    16     16   source $testdir/tester.tcl
    17         -set testprefix bestindex4
           17  +set testprefix bestindex5
    18     18   
    19     19   ifcapable !vtab {
    20     20     finish_test
    21     21     return
    22     22   }
    23     23   
    24     24   #-------------------------------------------------------------------------
................................................................................
   243    243   do_execsql_test 3.3 { SELECT rowid, * FROM t4 WHERE x!=245; } {}
   244    244   do_execsql_test 3.4 { SELECT rowid, * FROM t4 WHERE x!='245'; } {}
   245    245   
   246    246   do_execsql_test 3.5 { SELECT rowid, * FROM t4 WHERE rowid!=1 OR x!='245'; } {}
   247    247   
   248    248   
   249    249   finish_test
   250         -